288 lines
9.8 KiB
PHP
288 lines
9.8 KiB
PHP
|
|
<?
|
||
|
|
namespace common;
|
||
|
|
|
||
|
|
use common\support\Message;
|
||
|
|
use common\support\Message\structure\eSupportMessageType;
|
||
|
|
use common\support\structure\eSupportStatus;
|
||
|
|
use core\db\structure\Column as C;
|
||
|
|
use core\db\structure\eColumnType;
|
||
|
|
use ui\form\structure\eInputType;
|
||
|
|
use ui\Form;
|
||
|
|
use ui\input\Input;
|
||
|
|
use core\lang\structure\LangVariable as V;
|
||
|
|
use ms\ms\structure\msModuleTable;
|
||
|
|
use common\support\structure\supportLang as Vars;
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
class Support extends msModuleTable {
|
||
|
|
static $self;
|
||
|
|
static $table_name = 'support';
|
||
|
|
static $controller = 'common.support';
|
||
|
|
|
||
|
|
static $ID = 'id';
|
||
|
|
|
||
|
|
static $NAME = 'name';
|
||
|
|
static $PROFILE = 'profile';
|
||
|
|
static $STATUS = 'status';
|
||
|
|
static $TO = 'to';
|
||
|
|
static $OID = 'oid';
|
||
|
|
static $PORTAL = 'portal';
|
||
|
|
|
||
|
|
static $ACCOUNT = 'account';
|
||
|
|
static $CREATED = 'created';
|
||
|
|
static $UPDATED = 'updated';
|
||
|
|
|
||
|
|
|
||
|
|
static function getTitle(){return V::get(Vars::$MODULE_NAME);}
|
||
|
|
static function getSystemLangValues():array{ return Vars::getArray(); }
|
||
|
|
public function _update(\Where $w, array $values) {
|
||
|
|
$values[self::$UPDATED] = time();
|
||
|
|
return parent::_update($w, $values);
|
||
|
|
}
|
||
|
|
|
||
|
|
public static function installUniques() {
|
||
|
|
\DB::addIndexManyColumns(self::$table_name,[self::$TO,self::$OID]);
|
||
|
|
\DB::addIndexManyColumns(self::$table_name,[self::$PORTAL]);
|
||
|
|
\DB::addIndexManyColumns(self::$table_name,[self::$PROFILE]);
|
||
|
|
\DB::addIndexManyColumns(self::$table_name,[self::$STATUS]);
|
||
|
|
\DB::addIndexManyColumns(self::$table_name,[self::$ACCOUNT]);
|
||
|
|
}
|
||
|
|
|
||
|
|
static function isMyProfileCanBeAdmin(){
|
||
|
|
// TODO : доделать админов
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
static function closeSupport($support_id){
|
||
|
|
$r = Support::updateById($support_id,[
|
||
|
|
Support::$STATUS => eSupportStatus::CLOSE
|
||
|
|
]);
|
||
|
|
return ['success'=>$r->res];
|
||
|
|
}
|
||
|
|
|
||
|
|
static function addMessage($support_id,$text='',$isAdmin = false,$attachments = []){
|
||
|
|
$support_id = intval($support_id);
|
||
|
|
$text = trim((string)$text);
|
||
|
|
if(!$support_id or !$text) return null;
|
||
|
|
Message::insert([
|
||
|
|
Message::$TEXT => $text,
|
||
|
|
Message::$SUPPORT => $support_id,
|
||
|
|
Message::$TYPE => $isAdmin ? eSupportMessageType::ANSWER : eSupportMessageType::QUESTION,
|
||
|
|
]);
|
||
|
|
Support::updateById($support_id,[
|
||
|
|
Support::$STATUS => $isAdmin ? eSupportStatus::WAIT_PROFILE : eSupportStatus::WAIT_SUPPORT
|
||
|
|
]);
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
static function getMessagesFormatted($id){
|
||
|
|
$id = intval($id);
|
||
|
|
$res = [];
|
||
|
|
$r = Message::select($q = [
|
||
|
|
\Query::TABLES =>[Message::$table_name],
|
||
|
|
\Query::LEFT_JOINS =>[
|
||
|
|
[\Profile::$table_name,\Profile::$ID,Message::$PROFILE]
|
||
|
|
],
|
||
|
|
\Query::WHERE => new \Where(\Where::_and([
|
||
|
|
\Where::_operator(Message::$SUPPORT,'=',$id),
|
||
|
|
])),
|
||
|
|
\Query::SORT => [Message::$CREATED=>'ASC'],
|
||
|
|
]);
|
||
|
|
while($l = \DB::fetch($r)){
|
||
|
|
$res[] = Message::format($l);
|
||
|
|
}
|
||
|
|
return $res;
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
static function format($v){
|
||
|
|
$id = intval($v[Support::$ID]);
|
||
|
|
return [
|
||
|
|
'id' => $id,
|
||
|
|
'status' => intval($v[Support::$STATUS]),
|
||
|
|
'title' => (string)$v[Support::$NAME],
|
||
|
|
'emp' => \Profile::formatPublic($v),
|
||
|
|
'profile' => \Profile::formatPublic($v),
|
||
|
|
'date' => self::formatDate($v[self::$CREATED]),
|
||
|
|
];
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
static function addSupport($profile_id,$to,$oid,$name,$text){
|
||
|
|
$res = [];
|
||
|
|
$profile_id = intval($profile_id);
|
||
|
|
$to = intval($to);
|
||
|
|
$oid = intval($oid);
|
||
|
|
// TODO: Выслать уведомление админам
|
||
|
|
$r = Support::insert([
|
||
|
|
Support::$NAME => trim((string)$name),
|
||
|
|
Support::$PROFILE => intval($profile_id),
|
||
|
|
Support::$TO => intval($to),
|
||
|
|
Support::$OID => intval($oid),
|
||
|
|
]);
|
||
|
|
Message::insert([
|
||
|
|
Message::$SUPPORT => $r->id,
|
||
|
|
Message::$TEXT => trim((string)$text),
|
||
|
|
Message::$TYPE => eSupportMessageType::QUESTION,
|
||
|
|
]);
|
||
|
|
$res = Support::format(Support::getByID($r->id));
|
||
|
|
return $res;
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
static function getList($profile_id,$isAdmin,$isClosed,$isOnlyNew,$isWaitAnswer,$search,$to=0,$oid=0){
|
||
|
|
$res = [];
|
||
|
|
$isCanAdmin = static::isMyProfileCanBeAdmin();
|
||
|
|
$isClosed = boolval($isClosed);
|
||
|
|
$isOnlyNew = boolval($isOnlyNew);
|
||
|
|
$isWaitAnswer = boolval($isWaitAnswer);
|
||
|
|
$isAdmin = boolval($isAdmin) && $isCanAdmin;
|
||
|
|
$search = trim((string)$search);
|
||
|
|
$isSearch = $search!='';
|
||
|
|
$to = intval($to);
|
||
|
|
$oid = intval($oid);
|
||
|
|
|
||
|
|
$and = [];
|
||
|
|
if($to){
|
||
|
|
$and[] = \Where::_operator(Support::$TO,'=',$to);
|
||
|
|
}
|
||
|
|
if($to and $oid){
|
||
|
|
$and[] = \Where::_operator(Support::$OID,'=',$oid);
|
||
|
|
}
|
||
|
|
|
||
|
|
if($isSearch) {
|
||
|
|
$search_id = intval($search);
|
||
|
|
$or = [];
|
||
|
|
if($search_id) {
|
||
|
|
$or[] = \Where::_operator(Support::$ID, 'LIKE', $search_id.'%');
|
||
|
|
}
|
||
|
|
$or[] = \Where::_operator(Support::$NAME, 'LIKE', '%'.$search.'%');;
|
||
|
|
if($or){
|
||
|
|
$and[] = \Where::_or($or);
|
||
|
|
}
|
||
|
|
} else {
|
||
|
|
$and[] = \Where::_operator(Support::$STATUS, $isClosed ? '=' : '<>', eSupportStatus::CLOSE);
|
||
|
|
if ($isOnlyNew) {
|
||
|
|
$and[] = \Where::_operator(Support::$STATUS, '=', $isAdmin ? eSupportStatus::WAIT_SUPPORT : eSupportStatus::WAIT_PROFILE);
|
||
|
|
}
|
||
|
|
if ($isWaitAnswer) {
|
||
|
|
$and[] = \Where::_operator(Support::$STATUS, '=', $isAdmin ? eSupportStatus::WAIT_PROFILE : eSupportStatus::WAIT_SUPPORT);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if(!$isAdmin) {
|
||
|
|
$and[] = \Where::_operator(Support::$PROFILE,'=',$profile_id);
|
||
|
|
} else {
|
||
|
|
$and[] = \Where::_operator(Support::$ACCOUNT,'=',\Site::$owner_id);
|
||
|
|
}
|
||
|
|
|
||
|
|
$r = Support::select($q = [
|
||
|
|
\Query::LEFT_JOINS => [
|
||
|
|
[\Profile::$table_name,\Profile::$ID,Support::$PROFILE]
|
||
|
|
],
|
||
|
|
\Query::WHERE => new \Where(\Where::_and($and)),
|
||
|
|
\Query::SORT => [Support::$UPDATED=>'DESC'],
|
||
|
|
]);
|
||
|
|
while($l = \DB::fetch($r)){
|
||
|
|
$res[] = Support::format($l);
|
||
|
|
}
|
||
|
|
return $res;
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
static function columnInfo() : array {
|
||
|
|
return [
|
||
|
|
new C([
|
||
|
|
C::VAR_NAME =>&self::$ID,
|
||
|
|
C::TYPE =>eColumnType::INT,
|
||
|
|
C::AUTO_INCREMENT =>true,
|
||
|
|
C::PRIMARY =>true,
|
||
|
|
C::DEFAULT =>0,
|
||
|
|
C::HIDDEN =>true,
|
||
|
|
C::TH =>V::get(Vars::$ID),
|
||
|
|
]),
|
||
|
|
new C([
|
||
|
|
C::VAR_NAME => &self::$NAME,
|
||
|
|
C::TYPE => eColumnType::VARCHAR,
|
||
|
|
C::LENGTH => 1023,
|
||
|
|
C::TH => V::get(Vars::$NAME),
|
||
|
|
]),
|
||
|
|
|
||
|
|
new C([
|
||
|
|
C::VAR_NAME => &self::$PROFILE,
|
||
|
|
C::TYPE => eColumnType::INT,
|
||
|
|
C::TH => V::get(Vars::$PROFILE),
|
||
|
|
C::DEFAULT => intval(PID),
|
||
|
|
]),
|
||
|
|
new C([
|
||
|
|
C::VAR_NAME => &self::$STATUS,
|
||
|
|
C::TYPE => eColumnType::TINYINT,
|
||
|
|
C::DEFAULT => eSupportStatus::WAIT_SUPPORT,
|
||
|
|
C::TH => V::get(Vars::$STATUS),
|
||
|
|
]),
|
||
|
|
new C([
|
||
|
|
C::VAR_NAME => &self::$TO,
|
||
|
|
C::TYPE => eColumnType::INT,
|
||
|
|
C::TH => V::get(Vars::$TO),
|
||
|
|
]),
|
||
|
|
new C([
|
||
|
|
C::VAR_NAME => &self::$OID,
|
||
|
|
C::TYPE => eColumnType::INT,
|
||
|
|
C::TH => V::get(Vars::$OID),
|
||
|
|
]),
|
||
|
|
new C([
|
||
|
|
C::VAR_NAME => &self::$PORTAL,
|
||
|
|
C::TYPE => eColumnType::INT,
|
||
|
|
C::TH => V::get(Vars::$PORTAL),
|
||
|
|
C::DEFAULT => intval(\Site::$portal_id)
|
||
|
|
]),
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
new C([
|
||
|
|
C::VAR_NAME => &self::$ACCOUNT,
|
||
|
|
C::TYPE => eColumnType::INT,
|
||
|
|
C::LENGTH => 11,
|
||
|
|
C::TH => V::get(Vars::$ACCOUNT),
|
||
|
|
C::DEFAULT => intval(\SITE::$owner_id),
|
||
|
|
c::HIDDEN=>true,
|
||
|
|
c::FUNC_VALUE => function($v){
|
||
|
|
if($id = $v[self::$ACCOUNT]) {
|
||
|
|
return \Account::getName( \Account::getByID($id) );
|
||
|
|
} else return '-';
|
||
|
|
},
|
||
|
|
]),
|
||
|
|
new C([
|
||
|
|
C::VAR_NAME => &self::$CREATED,
|
||
|
|
C::TYPE => eColumnType::INT,
|
||
|
|
C::LENGTH => 11,
|
||
|
|
C::TH => V::get(Vars::$CREATED),
|
||
|
|
C::DEFAULT => time(),
|
||
|
|
c::FUNC_VALUE => function($v){return self::formatDate($v[self::$CREATED]);},
|
||
|
|
c::HIDDEN=>true,
|
||
|
|
]),
|
||
|
|
new C([
|
||
|
|
C::VAR_NAME => &self::$UPDATED,
|
||
|
|
C::TYPE => eColumnType::INT,
|
||
|
|
C::DEFAULT => time(),
|
||
|
|
C::TH => V::get(Vars::$UPDATED),
|
||
|
|
c::FUNC_VALUE => function($v){return self::formatDate($v[self::$UPDATED]);},
|
||
|
|
c::HIDDEN=>true,
|
||
|
|
]),
|
||
|
|
];
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
new Support();
|
||
|
|
|
||
|
|
|
||
|
|
///
|
||
|
|
|