PageRenderTime 37ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/backend/modules/systemAdmin/Catalog/models/base/CatalogItem.php

https://gitlab.com/aintenebris/memoria
PHP | 228 lines | 136 code | 24 blank | 68 comment | 3 complexity | c4822d1662fc2471facbf7179080773c MD5 | raw file
  1. <?php
  2. namespace backend\modules\systemAdmin\Catalog\models\base;
  3. use backend\modules\systemAdmin\Catalog\models\Models;
  4. use backend\modules\systemAdmin\Catalog\models\Section;
  5. use app\modules\systemAdmin\Materials\models\Materials;
  6. use app\modules\systemAdmin\models\details\Detail;
  7. use app\modules\systemAdmin\models\details\DetailTypes;
  8. use app\modules\systemAdmin\Stella\models\InstanceModels;
  9. use backend\modules\systemAdmin\AssignMaterial\models\CatalogItemMaterialLink;
  10. use backend\modules\systemAdmin\Catalog\models\Size;
  11. use backend\modules\systemAdmin\Catalog\models\SystemCatalogs;
  12. use common\models\catalog\CatalogItemSize;
  13. use Yii;
  14. use app\modules\systemAdmin\Stella\models\Stellas;
  15. /**
  16. * This is the model class for table "catalog_item".
  17. *
  18. * @property integer $id
  19. * @property string $name
  20. * @property integer $section_id
  21. *
  22. * @property Section $section
  23. * @property Details[] $details
  24. */
  25. class CatalogItem extends \yii\db\ActiveRecord
  26. {
  27. public $catalogs;
  28. // public $size;
  29. /**
  30. * @inheritdoc
  31. */
  32. public static function tableName()
  33. {
  34. return 'catalog_item';
  35. }
  36. /**
  37. * @inheritdoc
  38. */
  39. public function rules()
  40. {
  41. return [
  42. [['name'], 'required'],
  43. [['catalog'], 'required'],
  44. [['material_route'], 'required'],
  45. [['section_id'], 'required'],
  46. [['parent_id'], 'required'],
  47. [['image'], 'required'],
  48. [['image'], 'extensions' => 'png'],
  49. // [['name'], 'uniqueValidation'],
  50. [['section_id', 'parent_id', 'detail_id'], 'integer'],
  51. [['catalog'], 'safe'],
  52. [['name'], 'string', 'max' => 255],
  53. [['size'], 'safe'],
  54. [['type'], 'safe'],
  55. ];
  56. }
  57. /**
  58. * @inheritdoc
  59. */
  60. public function attributeLabels()
  61. {
  62. return [
  63. 'id' => Yii::t('app', 'ID'),
  64. 'name' => Yii::t('app', 'Name'),
  65. 'material_route' => Yii::t('app', 'Material Route'),
  66. 'parent_id' => Yii::t('app', 'Parent item'),
  67. 'section_id' => Yii::t('app', 'Section ID'),
  68. // 'section' => Yii::t('app', 'Section name'),
  69. // 'catalog' => Yii::t('app', 'Catalog'),
  70. ];
  71. }
  72. public function uniqueValidation($attribute, $params){
  73. if($this::find()
  74. ->where(['name' => $this->name])
  75. ->andWhere(['parent_id' => $this->parent_id])
  76. ->andWhere(['detail_id' => $this->detail_id])
  77. // ->andWhere(['parent_id' => $this->parent_id])
  78. ->one()
  79. ){
  80. $this->addError($attribute, Yii::t('app', 'Item already exist in this section'));
  81. }
  82. }
  83. public function extensionValidate($attribute, $params){
  84. preg_match('/(.png)$/', $this->image, $data);
  85. if($data[1] != '.png')
  86. $this->addError($attribute, Yii::t('app', 'Only files with these extensions are allowed: png.'));
  87. }
  88. /**
  89. * @return \yii\db\ActiveQuery
  90. */
  91. public function getSection()
  92. {
  93. return $this->hasOne(Section::className(), ['id' => 'section_id']);
  94. }
  95. /**
  96. * @return \yii\db\ActiveQuery
  97. */
  98. public function getMaterial()
  99. {
  100. return $this->hasOne(Materials::className(), ['id' => 'material_id']);
  101. }
  102. /**
  103. * @return \yii\db\ActiveQuery
  104. */
  105. public function getDetailId()
  106. {
  107. return $this->hasOne(InstanceModels::className(), ['id' => 'detail_instances.model_id'])
  108. ->viaTable('detail_instances di', ['detail_instances.id' => 'details.instance_id'])
  109. ->viaTable('details d', ['d.id' => 'detail_id'])
  110. ;
  111. }
  112. /**
  113. * @return \yii\db\ActiveQuery
  114. */
  115. public function getModel()
  116. {
  117. return $this
  118. // ->hasOne(InstanceModels::className(), ['id' => 'detail_instances.model_id'])
  119. // ->viaTable('detail_instances di', ['detail_instances.id' => 'details.instance_id'])
  120. // ->viaTable('details d', ['d.id' => 'detail_id'])
  121. ->hasOne(Models::className(), ['id' => 'detail_id'])
  122. ;
  123. }
  124. public function getIcon()
  125. {
  126. return $this
  127. ->hasOne(Models::className(), ['id' => 'detail_id']);
  128. }
  129. /**
  130. * @return \yii\db\ActiveQuery
  131. */
  132. public function setDetailId()
  133. {
  134. return $this->hasOne(InstanceModels::className(), ['id' => 'detail_instances.model_id'])
  135. ->viaTable('detail_instances di', ['detail_instances.id' => 'details.instance_id'])
  136. ->viaTable('details d', ['details.id' => 'detail_id'])
  137. ;
  138. }
  139. /**
  140. * @return \yii\db\ActiveQuery
  141. */
  142. public function getCatalog()
  143. {
  144. return $this->hasOne(SystemCatalogs::className(), ['id' => 'system_catalog_id'])
  145. ->viaTable('section s', ['id' => 'section_id']);
  146. }
  147. /**
  148. * @return \yii\db\ActiveQuery
  149. */
  150. public function setCatalog()
  151. {
  152. return $this->hasOne(SystemCatalogs::className(), ['id' => 'system_catalog_id'])
  153. ->viaTable('section s', ['id' => 'section_id']);
  154. }
  155. /**
  156. * @return \yii\db\ActiveQuery
  157. */
  158. public function getParent()
  159. {
  160. return $this->hasOne(CatalogItem::className(), ['id' => 'parent_id'])
  161. ->from(CatalogItem::tableName() . ' c');
  162. }
  163. /**
  164. * @return \yii\db\ActiveQuery
  165. */
  166. public function getSize()
  167. {
  168. return $this->hasMany(Size::className(), ['id' => 'size_id'])
  169. ->viaTable(CatalogItemSize::tableName(), ['catalog_item_id' => 'id']);
  170. }
  171. /**
  172. * @return \yii\db\ActiveQuery
  173. */
  174. public function getSizeRelation()
  175. {
  176. return $this->hasMany(CatalogItemSize::className(), ['catalog_item_id' => 'id']);
  177. }
  178. /**
  179. * @return \yii\db\ActiveQuery
  180. */
  181. public function getType()
  182. {
  183. return $this->hasOne(DetailTypes::className(), ['id' => 'type_id'])
  184. ->viaTable(Detail::tableName(), ['id' => 'detail_id']);
  185. }
  186. /**
  187. * @return \yii\db\ActiveQuery
  188. */
  189. public function setType()
  190. {
  191. return $this->hasOne(DetailTypes::className(), ['id' => 'type_id'])
  192. ->viaTable(Detail::tableName(), ['id' => 'detail_id']);
  193. }
  194. // public function getSizeMaterials(){
  195. // return $this->hasMany(CatalogItemMaterialLink::className(), ['id' => 'catalog_item_size_id'])
  196. // ->viaTable(CatalogItemSize::tableName(), ['catalog_item_id' => 'id'])
  197. // ;
  198. // }
  199. public function getSizeMaterials(){
  200. return $this->hasMany(CatalogItemMaterialLink::className(), ['catalog_item_size_id' => 'id'])
  201. ->viaTable(CatalogItemSize::tableName(), ['catalog_item_id' => 'id'])
  202. ;
  203. }
  204. }