itstructure /
yii2-template-multilanguage
| 1 | <?php |
||
| 2 | |||
| 3 | namespace app\models; |
||
| 4 | |||
| 5 | use Yii; |
||
| 6 | use Itstructure\MultiLevelMenu\MenuWidget; |
||
| 7 | use Itstructure\AdminModule\models\{MultilanguageTrait, Language, ActiveRecord}; |
||
| 8 | use Itstructure\AdminModule\interfaces\ModelInterface; |
||
| 9 | |||
| 10 | /** |
||
| 11 | * This is the model class for table "categories". |
||
| 12 | * |
||
| 13 | * @property int $id |
||
| 14 | * @property string $created_at |
||
| 15 | * @property string $updated_at |
||
| 16 | * @property int $parentId |
||
| 17 | * @property string $icon |
||
| 18 | * @property string $alias |
||
| 19 | * @property int $active |
||
| 20 | * |
||
| 21 | * @property CategoryLanguage[] $categoriesLanguages |
||
| 22 | * @property Language[] $languages |
||
| 23 | * |
||
| 24 | * @package app\models |
||
| 25 | */ |
||
| 26 | class Category extends ActiveRecord |
||
| 27 | { |
||
| 28 | use MultilanguageTrait; |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 29 | |||
| 30 | /** |
||
| 31 | * @inheritdoc |
||
| 32 | */ |
||
| 33 | public static function tableName() |
||
| 34 | { |
||
| 35 | return 'categories'; |
||
| 36 | } |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @inheritdoc |
||
| 40 | */ |
||
| 41 | public function rules() |
||
| 42 | { |
||
| 43 | return [ |
||
| 44 | [ |
||
| 45 | [ |
||
| 46 | 'created_at', |
||
| 47 | 'updated_at', |
||
| 48 | ], |
||
| 49 | 'safe', |
||
| 50 | ], |
||
| 51 | [ |
||
| 52 | [ |
||
| 53 | 'active', |
||
| 54 | ], |
||
| 55 | 'required', |
||
| 56 | ], |
||
| 57 | [ |
||
| 58 | [ |
||
| 59 | 'parentId', |
||
| 60 | 'active' |
||
| 61 | ], |
||
| 62 | 'integer', |
||
| 63 | ], |
||
| 64 | [ |
||
| 65 | [ |
||
| 66 | 'icon', |
||
| 67 | 'alias', |
||
| 68 | ], |
||
| 69 | 'string', |
||
| 70 | 'max' => 128, |
||
| 71 | ], |
||
| 72 | [ |
||
| 73 | 'parentId', |
||
| 74 | 'filter', |
||
| 75 | 'filter' => function ($value) { |
||
| 76 | if (empty($value)) { |
||
| 77 | return null; |
||
| 78 | } else { |
||
| 79 | return MenuWidget::checkNewParentId($this, $value) ? $value : $this->getOldAttribute('parentId'); |
||
| 80 | } |
||
| 81 | } |
||
| 82 | ], |
||
| 83 | [ |
||
| 84 | 'alias', |
||
| 85 | 'filter', |
||
| 86 | 'filter' => function ($value) { |
||
| 87 | return preg_replace( '/[^a-z0-9_]+/', '-', strtolower(trim($value))); |
||
| 88 | } |
||
| 89 | ], |
||
| 90 | [ |
||
| 91 | 'alias', |
||
| 92 | 'unique', |
||
| 93 | 'skipOnError' => true, |
||
| 94 | 'targetClass' => static::class, |
||
| 95 | 'filter' => $this->getScenario() == ModelInterface::SCENARIO_UPDATE ? 'id != '.$this->id : '' |
||
| 96 | ], |
||
| 97 | ]; |
||
| 98 | } |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @return array |
||
| 102 | */ |
||
| 103 | public function attributes(): array |
||
| 104 | { |
||
| 105 | return [ |
||
| 106 | 'id', |
||
| 107 | 'parentId', |
||
| 108 | 'icon', |
||
| 109 | 'alias', |
||
| 110 | 'active', |
||
| 111 | 'created_at', |
||
| 112 | 'updated_at', |
||
| 113 | ]; |
||
| 114 | } |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @inheritdoc |
||
| 118 | */ |
||
| 119 | public function attributeLabels() |
||
| 120 | { |
||
| 121 | return [ |
||
| 122 | 'id' => 'ID', |
||
| 123 | 'parentId' => Yii::t('app', 'Parent object'), |
||
| 124 | 'icon' => Yii::t('app', 'Icon'), |
||
| 125 | 'active' => Yii::t('app', 'Active status'), |
||
| 126 | 'alias' => Yii::t('app', 'URL Alias'), |
||
| 127 | 'created_at' => Yii::t('app', 'Created date'), |
||
| 128 | 'updated_at' => Yii::t('app', 'Updated date'), |
||
| 129 | ]; |
||
| 130 | } |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Reassigning child objects to their new parent after delete the main model record. |
||
| 134 | */ |
||
| 135 | public function afterDelete() |
||
| 136 | { |
||
| 137 | MenuWidget::afterDeleteMainModel($this); |
||
| 138 | |||
| 139 | parent::afterDelete(); |
||
| 140 | } |
||
| 141 | |||
| 142 | /** |
||
| 143 | * @return array|\yii\db\ActiveRecord[] |
||
| 144 | */ |
||
| 145 | public static function getMenu() |
||
| 146 | { |
||
| 147 | return static::find()->select([ |
||
| 148 | 'id', 'parentId' |
||
| 149 | ])->all(); |
||
| 150 | } |
||
| 151 | |||
| 152 | /** |
||
| 153 | * @return array|\yii\db\ActiveRecord[] |
||
| 154 | */ |
||
| 155 | public static function getActiveMenu() |
||
| 156 | { |
||
| 157 | return static::find()->select([ |
||
| 158 | 'id', 'parentId', 'alias' |
||
| 159 | ])->where([ |
||
| 160 | 'active' => 1 |
||
| 161 | ])->all(); |
||
| 162 | } |
||
| 163 | |||
| 164 | /** |
||
| 165 | * @return \yii\db\ActiveQuery |
||
| 166 | */ |
||
| 167 | public function getCategoriesLanguages() |
||
| 168 | { |
||
| 169 | return $this->hasMany(CategoryLanguage::class, [ |
||
| 170 | 'categories_id' => 'id' |
||
| 171 | ]); |
||
| 172 | } |
||
| 173 | |||
| 174 | /** |
||
| 175 | * @return \yii\db\ActiveQuery |
||
| 176 | */ |
||
| 177 | public function getLanguages() |
||
| 178 | { |
||
| 179 | return $this->hasMany(Language::class, [ |
||
| 180 | 'id' => 'language_id' |
||
| 181 | ])->viaTable('categories_language', [ |
||
| 182 | 'categories_id' => 'id' |
||
| 183 | ]); |
||
| 184 | } |
||
| 185 | } |
||
| 186 |