250, 'height' => 250, 'crop' => 'inner']; protected static $castQueryResult = true; public $sections = ['parent', 'child', 'recommendParent', 'recommendChild']; protected $_getTagTypeArray = [ 'child' => ['table' => 'tagChildV', 'fieldPrefix' => 'dst', 'wherePrefix' => 'src'], 'recommendChild' => ['table' => 'tagRecommendChildV', 'fieldPrefix' => 'dst', 'wherePrefix' => 'src'], 'parent' => ['table' => 'tagParentV', 'fieldPrefix' => 'src', 'wherePrefix' => 'dst'], 'recommendParent' => ['table' => 'tagRecommendParentV', 'fieldPrefix' => 'src', 'wherePrefix' => 'dst'], ]; public $file; public $fileId; public static function tableName() { return 'tagForAdminV'; } public static function tableNameLocale() { return 'tagTL'; } public function getId() { return $this->id; } public function rules() { return [ ['name', 'filter', 'filter' => 'trim'], ['name', 'required'], ['typeId', 'required'], ['canSearchRaw', 'boolean', 'trueValue' => 'y', 'falseValue' => 'n'], ['enabled', 'boolean', 'trueValue' => 'y', 'falseValue' => 'n'] ]; } public static function getAttributeTypeList() { return [ ['id' => 'text', 'name' => Yii::t('api', 'Text')], ['id' => 'numeric', 'name' => Yii::t('api', 'Numeric')], ['id' => 'datetime', 'name' => Yii::t('api', 'Date Time')], ]; } public static function getAttributeStorageIdList() { $query = self::createQuery(); $query->select(['id', 'name', 'outputFormatJs']); $query->from('attributeDatetimeFormatV'); $query->orderBy('sorter'); return $query->createCommand()->queryAll(); } public static function getTag4Select() { $query = static::createQuery() ->from(['nodeTypeV']) ->select( [ 'id', 'code', 'name', 'internalTypeId', 'searchAnd as typeSearchAnd', 'canHavePhoto as typeCanHavePhoto', 'canHaveRatingPoints as typeCanHaveRatingPoints', 'canSearch', 'displayInObject', 'displayInObjectList' ] ) ->where('internalTypeId IN ("tag", "attribute")') ->andWhere(['canEditNode' => 'y']) ->andWhere(['displayNode' => 'y']) ->orderBy('sorter') ; return $query->createCommand()->queryAll(); } public function getRecommendParentTagById($dstNodeId, $filter, $pagination) { return $this->_getAbstractTagById('recommendParent', $dstNodeId, $filter, $pagination); } public function getParentTagById($dstNodeId, $filter, $pagination) { return $this->_getAbstractTagById('parent', $dstNodeId, $filter, $pagination); } public function getChildTagById($srcNodeId, $filter, $pagination) { return $this->_getAbstractTagById('child', $srcNodeId, $filter, $pagination); } public function getRecommendChildTagById($srcNodeId, $filter, $pagination) { return $this->_getAbstractTagById('recommendChild', $srcNodeId, $filter, $pagination); } protected function _getAbstractTagById($type, $goalId, $filter, $pagination) { $query = $this->_getAbstractTagByIdQuery($type, $goalId); $this->_prepareLinksQuery($query, $type, $filter); $dataProvider = new ActiveDataProvider([ 'query' => $query, ]); if (isset($pagination['pageSize'])) { $dataProvider->pagination->pageSize = $pagination['pageSize']; } else { $dataProvider->pagination->pageSize = 10; } if (isset($pagination['page'])) { $dataProvider->pagination->page = $pagination['page']; } if (isset($pagination['sort'])) { $dataProvider->getSort()->params = ['sort' => $pagination['sort']]; } $dataProvider->prepareQuery(); /** @var ActiveQuery $query */ $query = $dataProvider->query; return [ 'list' => $query->createCommand()->queryAll(), 'pagination' => [ 'pageSize' => (int) $dataProvider->pagination->pageSize, 'totalCount' => (int) $dataProvider->pagination->totalCount, 'pageCount' => (int) $dataProvider->pagination->getPageCount(), 'currentPageIndex' => (int) $dataProvider->pagination->getPage(), ] ]; } protected function _prepareLinksQuery(ActiveQuery &$query, $type, $filter){ $prefix = $this->_getTagTypeArray[$type]['fieldPrefix']; if (isset($filter['query']) && !empty($this->_filterFields)) { $conditionArray = []; foreach ($this->_filterFields as $field) { $conditionArray[] = $prefix . $field . ' LIKE "%' . strtr($filter['query'], ['%' => '\%', '_' => '\_', '\\' => '\\\\']) . '%"'; } if (!empty($conditionArray)) { $query->andWhere('(' . implode(' || ', $conditionArray) . ')'); } } } protected function _getAbstractTagByIdQuery($type, $goalId) { $prefix = $this->_getTagTypeArray[$type]['fieldPrefix']; $wherePrefix = $this->_getTagTypeArray[$type]['wherePrefix']; return static::createQuery() ->from([$this->_getTagTypeArray[$type]['table'] . ' table']) ->select([$prefix . 'Id id', $prefix . 'TypeName typeName', $prefix . 'Name name', $prefix . 'CanEdit canEdit']) ->where([$wherePrefix . 'Id' => $goalId]) ->andWhere($prefix . 'Id IS NOT NULL') ->orderBy($this->_getTagTypeArray[$type]['wherePrefix'] . 'Sorter') ; } public function getAttributes4Link() { return [ 'id' => $this->getAttribute('id'), 'typeName' => $this->getAttribute('typeName'), 'name' => $this->getAttribute('name'), 'canEdit' => $this->getAttribute('typeCanEdit') ]; } public static function tagLink($srcId, $dstId) { return self::_tagLinkUnlink('nodeLink_Relink', $srcId, $dstId); } public static function tagUnlink($srcId, $dstId) { return self::_tagLinkUnlink('nodeLink_Unlink', $srcId, $dstId); } protected static function _tagLinkUnlink($functionName, $srcId, $dstId) { $sql = 'SELECT ' . $functionName . '("' . self::TAG_LINK_TYPE . '", ' . static::getDb()->quoteValue($srcId) . ', ' . static::getDb()->quoteValue($dstId) . ')'; $command = static::getDb()->createCommand(); $command->setSql($sql); $result = $command->execute(); return $result; } public function afterSave($insert) { $this->updateNodeIdForImage(); parent::afterSave($insert); } public function updateNodeIdForImage() { if ((int) $this->fileId) { $sql = 'SELECT image_UpdateNodeId(' . (int) $this->fileId . ', ' . (int) $this->getAttribute('id') . ');'; $command = static::getDb()->createCommand(); $command->setSql($sql); $command->execute(); } } public function getImageByNodeId() { return Image::getRawNodeImageFile($this->getAttribute('id')); } /** * Returns node images with thumbnails */ public function getImagesList(array $imageParams) { return Image::getNodeImageList($this->getAttribute('id'), $imageParams); } }