itstructure /
yii2-template-multilanguage
| 1 | <?php |
||
| 2 | |||
| 3 | namespace app\models; |
||
| 4 | |||
| 5 | use Yii; |
||
| 6 | use yii\helpers\ArrayHelper; |
||
| 7 | use Itstructure\AdminModule\models\{MultilanguageTrait, Language, ActiveRecord}; |
||
| 8 | use Itstructure\AdminModule\interfaces\ModelInterface; |
||
| 9 | use Itstructure\MFUploader\behaviors\{BehaviorMediafile, BehaviorAlbum}; |
||
| 10 | use Itstructure\MFUploader\models\OwnerAlbum; |
||
| 11 | use Itstructure\MFUploader\models\album\Album; |
||
| 12 | use Itstructure\MFUploader\interfaces\UploadModelInterface; |
||
| 13 | use app\traits\ThumbnailTrait; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * This is the model class for table "products". |
||
| 17 | * |
||
| 18 | * @property int|string $thumbnail thumbnail(mediafile id or url). |
||
| 19 | * @property array $image image(array of 'mediafile id' or 'mediafile url'). |
||
| 20 | * @property array $albums Existing album ids. |
||
| 21 | * @property int $id |
||
| 22 | * @property string $created_at |
||
| 23 | * @property string $updated_at |
||
| 24 | * @property int $categoryId |
||
| 25 | * @property string $icon |
||
| 26 | * @property string $alias |
||
| 27 | * @property float $price |
||
| 28 | * @property int $active |
||
| 29 | * |
||
| 30 | * @property Category $category |
||
| 31 | * @property ProductLanguage[] $productsLanguages |
||
| 32 | * @property Language[] $languages |
||
| 33 | * |
||
| 34 | * @package app\models |
||
| 35 | */ |
||
| 36 | class Product extends ActiveRecord |
||
| 37 | { |
||
| 38 | use MultilanguageTrait, ThumbnailTrait; |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 39 | |||
| 40 | /** |
||
| 41 | * @var int|string thumbnail(mediafile id or url). |
||
| 42 | */ |
||
| 43 | public $thumbnail; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var array image(array of 'mediafile id' or 'mediafile url'). |
||
| 47 | */ |
||
| 48 | public $image; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var array |
||
| 52 | */ |
||
| 53 | public $albums = []; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Init albums. |
||
| 57 | */ |
||
| 58 | public function afterFind() |
||
| 59 | { |
||
| 60 | $this->albums = $this->getAlbums(); |
||
| 61 | |||
| 62 | parent::afterFind(); |
||
| 63 | } |
||
| 64 | |||
| 65 | /** |
||
| 66 | * {@inheritdoc} |
||
| 67 | */ |
||
| 68 | public static function tableName() |
||
| 69 | { |
||
| 70 | return 'products'; |
||
| 71 | } |
||
| 72 | |||
| 73 | /** |
||
| 74 | * {@inheritdoc} |
||
| 75 | */ |
||
| 76 | public function rules() |
||
| 77 | { |
||
| 78 | return [ |
||
| 79 | [ |
||
| 80 | [ |
||
| 81 | 'categoryId', |
||
| 82 | 'active', |
||
| 83 | 'alias', |
||
| 84 | 'price', |
||
| 85 | ], |
||
| 86 | 'required' |
||
| 87 | ], |
||
| 88 | [ |
||
| 89 | [ |
||
| 90 | 'categoryId', |
||
| 91 | 'active', |
||
| 92 | ], |
||
| 93 | 'integer' |
||
| 94 | ], |
||
| 95 | [ |
||
| 96 | [ |
||
| 97 | 'created_at', |
||
| 98 | 'updated_at' |
||
| 99 | ], |
||
| 100 | 'safe' |
||
| 101 | ], |
||
| 102 | [ |
||
| 103 | [ |
||
| 104 | 'icon', |
||
| 105 | 'alias', |
||
| 106 | ], |
||
| 107 | 'string', |
||
| 108 | 'max' => 128 |
||
| 109 | ], |
||
| 110 | [ |
||
| 111 | [ |
||
| 112 | 'price' |
||
| 113 | ], |
||
| 114 | 'number' |
||
| 115 | ], |
||
| 116 | [ |
||
| 117 | 'alias', |
||
| 118 | 'filter', |
||
| 119 | 'filter' => function ($value) { |
||
| 120 | return preg_replace( '/[^a-z0-9_]+/', '-', strtolower(trim($value))); |
||
| 121 | } |
||
| 122 | ], |
||
| 123 | [ |
||
| 124 | 'alias', |
||
| 125 | 'unique', |
||
| 126 | 'skipOnError' => true, |
||
| 127 | 'targetClass' => static::class, |
||
| 128 | 'filter' => $this->getScenario() == ModelInterface::SCENARIO_UPDATE ? 'id != '.$this->id : '' |
||
| 129 | ], |
||
| 130 | [ |
||
| 131 | [ |
||
| 132 | 'categoryId' |
||
| 133 | ], |
||
| 134 | 'exist', |
||
| 135 | 'skipOnError' => true, |
||
| 136 | 'targetClass' => Category::class, |
||
| 137 | 'targetAttribute' => ['categoryId' => 'id'] |
||
| 138 | ], |
||
| 139 | [ |
||
| 140 | UploadModelInterface::FILE_TYPE_THUMB, |
||
| 141 | function($attribute){ |
||
| 142 | if (!is_numeric($this->{$attribute}) && !is_string($this->{$attribute})){ |
||
| 143 | $this->addError($attribute, 'Tumbnail content must be a numeric or string.'); |
||
| 144 | } |
||
| 145 | }, |
||
| 146 | 'skipOnError' => false, |
||
| 147 | ], |
||
| 148 | [ |
||
| 149 | UploadModelInterface::FILE_TYPE_IMAGE, |
||
| 150 | function($attribute) { |
||
| 151 | if (!is_array($this->{$attribute})) { |
||
| 152 | $this->addError($attribute, 'Image field content must be an array.'); |
||
| 153 | } |
||
| 154 | }, |
||
| 155 | 'skipOnError' => false, |
||
| 156 | ], |
||
| 157 | [ |
||
| 158 | 'albums', |
||
| 159 | 'each', |
||
| 160 | 'rule' => ['integer'], |
||
| 161 | ], |
||
| 162 | ]; |
||
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * @inheritdoc |
||
| 167 | */ |
||
| 168 | public function behaviors() |
||
| 169 | { |
||
| 170 | return ArrayHelper::merge(parent::behaviors(), [ |
||
| 171 | 'mediafile' => [ |
||
| 172 | 'class' => BehaviorMediafile::class, |
||
| 173 | 'name' => static::tableName(), |
||
| 174 | 'attributes' => [ |
||
| 175 | UploadModelInterface::FILE_TYPE_THUMB, |
||
| 176 | UploadModelInterface::FILE_TYPE_IMAGE, |
||
| 177 | ], |
||
| 178 | ], |
||
| 179 | 'albums' => [ |
||
| 180 | 'class' => BehaviorAlbum::class, |
||
| 181 | 'name' => static::tableName(), |
||
| 182 | 'attributes' => [ |
||
| 183 | 'albums', |
||
| 184 | ], |
||
| 185 | ], |
||
| 186 | ]); |
||
| 187 | } |
||
| 188 | |||
| 189 | /** |
||
| 190 | * @return array |
||
| 191 | */ |
||
| 192 | public function attributes(): array |
||
| 193 | { |
||
| 194 | return [ |
||
| 195 | UploadModelInterface::FILE_TYPE_THUMB, |
||
| 196 | UploadModelInterface::FILE_TYPE_IMAGE, |
||
| 197 | 'albums', |
||
| 198 | 'id', |
||
| 199 | 'categoryId', |
||
| 200 | 'icon', |
||
| 201 | 'price', |
||
| 202 | 'alias', |
||
| 203 | 'active', |
||
| 204 | 'created_at', |
||
| 205 | 'updated_at' |
||
| 206 | ]; |
||
| 207 | } |
||
| 208 | |||
| 209 | /** |
||
| 210 | * {@inheritdoc} |
||
| 211 | */ |
||
| 212 | public function attributeLabels() |
||
| 213 | { |
||
| 214 | return [ |
||
| 215 | 'id' => 'ID', |
||
| 216 | 'categoryId' => Yii::t('products', 'Parent category'), |
||
| 217 | 'icon' => Yii::t('app', 'Icon'), |
||
| 218 | 'price' => Yii::t('products', 'Price'), |
||
| 219 | 'active' => Yii::t('app', 'Active status'), |
||
| 220 | 'alias' => Yii::t('app', 'URL Alias'), |
||
| 221 | 'created_at' => Yii::t('app', 'Created date'), |
||
| 222 | 'updated_at' => Yii::t('app', 'Updated date'), |
||
| 223 | ]; |
||
| 224 | } |
||
| 225 | |||
| 226 | /** |
||
| 227 | * @return \yii\db\ActiveQuery |
||
| 228 | */ |
||
| 229 | public function getCategory() |
||
| 230 | { |
||
| 231 | return $this->hasOne(Category::class, [ |
||
| 232 | 'id' => 'categoryId' |
||
| 233 | ]); |
||
| 234 | } |
||
| 235 | |||
| 236 | /** |
||
| 237 | * @return \yii\db\ActiveQuery |
||
| 238 | */ |
||
| 239 | public function getProductsLanguages() |
||
| 240 | { |
||
| 241 | return $this->hasMany(ProductLanguage::class, [ |
||
| 242 | 'products_id' => 'id' |
||
| 243 | ]); |
||
| 244 | } |
||
| 245 | |||
| 246 | /** |
||
| 247 | * @return \yii\db\ActiveQuery |
||
| 248 | */ |
||
| 249 | public function getLanguages() |
||
| 250 | { |
||
| 251 | return $this->hasMany(Language::class, [ |
||
| 252 | 'id' => 'language_id' |
||
| 253 | ])->viaTable('products_language', [ |
||
| 254 | 'products_id' => 'id' |
||
| 255 | ]); |
||
| 256 | } |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Get albums, that product has. |
||
| 260 | * |
||
| 261 | * @return Album[] |
||
| 262 | */ |
||
| 263 | public function getAlbums() |
||
| 264 | { |
||
| 265 | return OwnerAlbum::getAlbumsQuery([ |
||
| 266 | 'owner' => static::tableName(), |
||
| 267 | 'ownerId' => $this->id, |
||
| 268 | 'ownerAttribute' => 'albums', |
||
| 269 | ])->all(); |
||
| 270 | } |
||
| 271 | } |
||
| 272 |