/backend/modules/systemAdmin/Catalog/models/base/CatalogItem.php
PHP | 228 lines | 136 code | 24 blank | 68 comment | 3 complexity | c4822d1662fc2471facbf7179080773c MD5 | raw file
- <?php
- namespace backend\modules\systemAdmin\Catalog\models\base;
- use backend\modules\systemAdmin\Catalog\models\Models;
- use backend\modules\systemAdmin\Catalog\models\Section;
- use app\modules\systemAdmin\Materials\models\Materials;
- use app\modules\systemAdmin\models\details\Detail;
- use app\modules\systemAdmin\models\details\DetailTypes;
- use app\modules\systemAdmin\Stella\models\InstanceModels;
- use backend\modules\systemAdmin\AssignMaterial\models\CatalogItemMaterialLink;
- use backend\modules\systemAdmin\Catalog\models\Size;
- use backend\modules\systemAdmin\Catalog\models\SystemCatalogs;
- use common\models\catalog\CatalogItemSize;
- use Yii;
- use app\modules\systemAdmin\Stella\models\Stellas;
- /**
- * This is the model class for table "catalog_item".
- *
- * @property integer $id
- * @property string $name
- * @property integer $section_id
- *
- * @property Section $section
- * @property Details[] $details
- */
- class CatalogItem extends \yii\db\ActiveRecord
- {
- public $catalogs;
- // public $size;
- /**
- * @inheritdoc
- */
- public static function tableName()
- {
- return 'catalog_item';
- }
- /**
- * @inheritdoc
- */
- public function rules()
- {
- return [
- [['name'], 'required'],
- [['catalog'], 'required'],
- [['material_route'], 'required'],
- [['section_id'], 'required'],
- [['parent_id'], 'required'],
- [['image'], 'required'],
- [['image'], 'extensions' => 'png'],
- // [['name'], 'uniqueValidation'],
- [['section_id', 'parent_id', 'detail_id'], 'integer'],
- [['catalog'], 'safe'],
- [['name'], 'string', 'max' => 255],
- [['size'], 'safe'],
- [['type'], 'safe'],
- ];
- }
- /**
- * @inheritdoc
- */
- public function attributeLabels()
- {
- return [
- 'id' => Yii::t('app', 'ID'),
- 'name' => Yii::t('app', 'Name'),
- 'material_route' => Yii::t('app', 'Material Route'),
- 'parent_id' => Yii::t('app', 'Parent item'),
- 'section_id' => Yii::t('app', 'Section ID'),
- // 'section' => Yii::t('app', 'Section name'),
- // 'catalog' => Yii::t('app', 'Catalog'),
- ];
- }
- public function uniqueValidation($attribute, $params){
- if($this::find()
- ->where(['name' => $this->name])
- ->andWhere(['parent_id' => $this->parent_id])
- ->andWhere(['detail_id' => $this->detail_id])
- // ->andWhere(['parent_id' => $this->parent_id])
- ->one()
- ){
- $this->addError($attribute, Yii::t('app', 'Item already exist in this section'));
- }
- }
- public function extensionValidate($attribute, $params){
- preg_match('/(.png)$/', $this->image, $data);
- if($data[1] != '.png')
- $this->addError($attribute, Yii::t('app', 'Only files with these extensions are allowed: png.'));
- }
- /**
- * @return \yii\db\ActiveQuery
- */
- public function getSection()
- {
- return $this->hasOne(Section::className(), ['id' => 'section_id']);
- }
- /**
- * @return \yii\db\ActiveQuery
- */
- public function getMaterial()
- {
- return $this->hasOne(Materials::className(), ['id' => 'material_id']);
- }
- /**
- * @return \yii\db\ActiveQuery
- */
- public function getDetailId()
- {
- return $this->hasOne(InstanceModels::className(), ['id' => 'detail_instances.model_id'])
- ->viaTable('detail_instances di', ['detail_instances.id' => 'details.instance_id'])
- ->viaTable('details d', ['d.id' => 'detail_id'])
- ;
- }
- /**
- * @return \yii\db\ActiveQuery
- */
- public function getModel()
- {
- return $this
- // ->hasOne(InstanceModels::className(), ['id' => 'detail_instances.model_id'])
- // ->viaTable('detail_instances di', ['detail_instances.id' => 'details.instance_id'])
- // ->viaTable('details d', ['d.id' => 'detail_id'])
- ->hasOne(Models::className(), ['id' => 'detail_id'])
- ;
- }
- public function getIcon()
- {
- return $this
- ->hasOne(Models::className(), ['id' => 'detail_id']);
- }
- /**
- * @return \yii\db\ActiveQuery
- */
- public function setDetailId()
- {
- return $this->hasOne(InstanceModels::className(), ['id' => 'detail_instances.model_id'])
- ->viaTable('detail_instances di', ['detail_instances.id' => 'details.instance_id'])
- ->viaTable('details d', ['details.id' => 'detail_id'])
- ;
- }
- /**
- * @return \yii\db\ActiveQuery
- */
- public function getCatalog()
- {
- return $this->hasOne(SystemCatalogs::className(), ['id' => 'system_catalog_id'])
- ->viaTable('section s', ['id' => 'section_id']);
- }
- /**
- * @return \yii\db\ActiveQuery
- */
- public function setCatalog()
- {
- return $this->hasOne(SystemCatalogs::className(), ['id' => 'system_catalog_id'])
- ->viaTable('section s', ['id' => 'section_id']);
- }
- /**
- * @return \yii\db\ActiveQuery
- */
- public function getParent()
- {
- return $this->hasOne(CatalogItem::className(), ['id' => 'parent_id'])
- ->from(CatalogItem::tableName() . ' c');
- }
- /**
- * @return \yii\db\ActiveQuery
- */
- public function getSize()
- {
- return $this->hasMany(Size::className(), ['id' => 'size_id'])
- ->viaTable(CatalogItemSize::tableName(), ['catalog_item_id' => 'id']);
- }
- /**
- * @return \yii\db\ActiveQuery
- */
- public function getSizeRelation()
- {
- return $this->hasMany(CatalogItemSize::className(), ['catalog_item_id' => 'id']);
- }
- /**
- * @return \yii\db\ActiveQuery
- */
- public function getType()
- {
- return $this->hasOne(DetailTypes::className(), ['id' => 'type_id'])
- ->viaTable(Detail::tableName(), ['id' => 'detail_id']);
- }
- /**
- * @return \yii\db\ActiveQuery
- */
- public function setType()
- {
- return $this->hasOne(DetailTypes::className(), ['id' => 'type_id'])
- ->viaTable(Detail::tableName(), ['id' => 'detail_id']);
- }
- // public function getSizeMaterials(){
- // return $this->hasMany(CatalogItemMaterialLink::className(), ['id' => 'catalog_item_size_id'])
- // ->viaTable(CatalogItemSize::tableName(), ['catalog_item_id' => 'id'])
- // ;
- // }
- public function getSizeMaterials(){
- return $this->hasMany(CatalogItemMaterialLink::className(), ['catalog_item_size_id' => 'id'])
- ->viaTable(CatalogItemSize::tableName(), ['catalog_item_id' => 'id'])
- ;
- }
- }