id; } public function scenarios() { return [ 'insert' => ['description'], 'update' => ['description'], 'delete' => [] ]; } public function rules() { return []; } public function createEmptyTicket() { $defaultData = [ 'description' => '', 'statusId' => 'draft', 'latitude' => '', 'longitude' => '', 'rating' => 0, 'noteForModerator' => '', 'noteForUser' => '' ]; $this->scenario = 'insert'; $this->load([$this->formName() => $defaultData]); return $this->save(); } public function getRecommendationInfo($id) { $result = [ 'tagNames' => null, 'attributes' => null ]; $recommend = new Recommendation(); if (($groups = $recommend->getGroups($id))) { foreach ($groups as $group) { if ('attribute' === $group['internalTypeId']) { $result['attributes'] = $recommend->getRootGeoAttributes($id); } elseif ('tag' === $group['internalTypeId']) { $result['tagNames'] = $recommend->getRootGetTagTypeNames($id); } } } return $result; } public function getRecommendGeoTag($id, $prefix = '') { $recommendation = new Recommendation(); return $recommendation->getRecommendedLinks($id, $prefix, 'geo', 'y'); } public function getLinkedGeoTag($id) { $query = static::createQuery() ->from(['objectWithTagV owt']) ->select(['owt.srcId id', 'owt.srcTypeCode typeCode', 'owt.srcName name', 'owt.srcTypeName typeName']) ->where(['owt.objectId' => $id]) ->andWhere('owt.srcIsRoot = "n"') ->andWhere('owt.srcGeoRoot = 1') ->andWhere('owt.srcInternalTypeId = "tag"') ->andWhere('owt.linked = 1') ->orderBy('owt.srcTypeSorter, owt.srcSorter') ; return $query->createCommand()->queryAll(); } public function getLinkedGeoAttributes($id) { $query = static::createQuery() ->from(['objectWithTagV owt']) ->select([ 'owt.srcId id', 'owt.srcTypeCode typeCode', 'owt.srcName name', 'owt.srcTypeName typeName', 'srcAttributeDataType dataType', 'srcRequired required', 'srcUofmPre uofmPre', 'srcUofmPost uofmPost', 'srcNumericValue numericValue', 'srcDecimalMin decimalMin', 'srcDecimalMax decimalMax', 'srcDecimalRound decimalRound', 'srcTextValue textValue', 'srcTextIsLong textIsLong', 'srcDatetimeStorageId as datetimeStorageId', 'srcDatetimeValue datetimeValue', 'srcDatetimeName datetimeName', 'srcDatetimeOutputFormatJs datetimeOutputFormatJs', ]) ->where(['owt.objectId' => $id]) ->andWhere('owt.srcIsRoot = "n"') ->andWhere('owt.srcGeoRoot = 1') ->andWhere('owt.srcInternalTypeId = "attribute"') ->andWhere('owt.linked = 1') ->orderBy('owt.srcTypeSorter, owt.srcSorter') ; return $query->createCommand()->queryAll(); } public function linkGeoTag($srcId, $dstId) { return self::_tagLinkUnlink('nodeLink_Relink', $srcId, $dstId); } public static function unlinkGeoTag($srcId, $dstId) { return self::_tagLinkUnlink('nodeLink_Unlink', $srcId, $dstId); } public function linkUser($userId, $objectId) { return self::_tagLinkUnlink('nodeLink_Relink', $userId, $objectId); } public static function unlinkUser($userId, $objectId) { return self::_tagLinkUnlink('nodeLink_Unlink', $userId, $objectId); } protected static function _tagLinkUnlink($functionName, $srcId, $dstId) { $sql = 'SELECT ' . $functionName . '("owns", ' . static::getDb()->quoteValue($srcId) . ', ' . static::getDb()->quoteValue($dstId) . ')'; $command = static::getDb()->createCommand(); $command->setSql($sql); $result = $command->execute(); return $result; } public function getRecommendAmenitySubmissionTag($id) { $recommendation = new Recommendation(); return $recommendation->getRecommendAmenitySubmissionTag($id); } public static function getStatusList() { $query = static::createQuery() ->from(['objectStatusV']) ->select(['id', 'name', 'public', 'userEditable', 'userDraftable']) ->orderBy('sorter') ; return $query->createCommand()->queryAll(); } public function linkTag($srcId, $dstId) { return self::_tagLinkUnlink('nodeLink_Relink', $srcId, $dstId); } public static function unlinkTag($srcId, $dstId) { return self::_tagLinkUnlink('nodeLink_Unlink', $srcId, $dstId); } public function getTagGrouped($id) { return self::groupTagsByType($this->getLinkedByObjectId($id)); } protected function _getLinkedTagByObjectIdQuery($id) { $query = static::createCastQuery() ->from(['objectWithTagV owt']) ->select(['owt.srcId as id', 'owt.srcTypeCode as typeCode', 'owt.srcName as name', 'owt.srcTypeName as typeName', 'IF (owt.linked = 1, "y", "n") as selected', 'srcAttributeDataType dataType', 'srcRequired required', 'srcUofmPre uofmPre', 'srcUofmPost uofmPost', 'srcNumericValue numericValue', 'srcDecimalMin decimalMin', 'srcDecimalMax decimalMax', 'srcDecimalRound decimalRound', 'srcTextValue textValue', 'srcTextIsLong textIsLong', 'srcDatetimeStorageId as datetimeStorageId', 'srcDatetimeValue datetimeValue', 'srcDatetimeName datetimeName', 'srcDatetimeOutputFormatJs datetimeOutputFormatJs', ]) ->where('owt.srcIsRoot = "n"') ->andWhere('owt.srcGeoRoot IS NULL') ->andWhere(['owt.objectId' => $id]) ->orderBy('`owt`.`srcTypeSorter`, `owt`.`srcSorter`') ->groupBy('owt.srcId') ; return $query; } public function getLinkedByObjectId($id) { $query = $this->_getLinkedTagByObjectIdQuery($id); return $query->createCommand()->queryAll(); } public function getLinkedAttribute($objectId, $tagId) { $query = $this->_getLinkedTagByObjectIdQuery($objectId); $query->where(['owt.srcId' => $tagId]); $query->limit(1); return $query->createCommand()->queryOne(); } public function setStatus($status) { $this->setAttribute('statusId', $status); return $this->_changeStatus(); } protected function _changeStatus() { $sql = 'SELECT object_SetStatusByUser(' . static::getDb()->quoteValue($this->getAttribute('id')) . ', ' . static::getDb()->quoteValue($this->getAttribute('statusId')) . ');'; $command = static::getDb()->createCommand(); $command->setSql($sql); try { return $command->execute(); } catch (Exception $e) { return false; } } public static function groupTagsByType(array $list) { $result = []; foreach ($list as $row) { $typeCode = $row['typeCode']; if (!array_key_exists($typeCode, $result)) { $result[$typeCode] = [ 'typeCode' => $row['typeCode'], 'typeName' => $row['typeName'] ]; } if (isset($row['selected'])) { $row['selected'] = ($row['selected'] == 'y'); } unset($row['typeCode']); unset($row['typeName']); $result[$typeCode]['list'][] = $row; } return array_values($result); } public function attributeSetValue($objectId, $tagId, $value) { $_objectId = (int) $objectId; $_tagId = (int) $tagId; $_value = (null === $value) ? 'NULL' : static::getDb()->quoteValue($value); $sql = "SELECT attribute_SetValue({$_tagId}, {$_objectId}, NULL, $_value);"; $command = static::getDb()->createCommand(); $command->setSql($sql); return $command->execute(); } public static function getTicketUsers($ticketId) { $q = static::createQuery(); $q->select(['id', 'name', 'phone']); $q->from(['objectWithUserV']); $q->where(['objectId' => $ticketId, 'roleId' => 'user']); $q->orderBy('name'); return $q->createCommand()->queryAll(); } }