Inheritance: extends yii\db\ActiveRecord
Example #1
0
 public function actionSave()
 {
     if (Yii::$app->request->isPost) {
         $post = Yii::$app->request->post();
         if ($post['payload'] && $post['entityModel']) {
             $payload = Json::decode($post['payload']);
             if (!isset($payload['fields'])) {
                 return;
             }
             $categoryId = isset($post['categoryId']) ? $post['categoryId'] : 0;
             $entityId = EavEntity::find()->select(['id'])->where(['entityModel' => $post['entityModel'], 'categoryId' => $categoryId])->scalar();
             if (!$entityId) {
                 $entity = new EavEntity();
                 $entity->entityName = isset($post['entityName']) ? $post['entityName'] : 'Untitled';
                 $entity->entityModel = $post['entityModel'];
                 $entity->categoryId = $categoryId;
                 $entity->save(false);
                 $entityId = $entity->id;
             }
             foreach ($payload['fields'] as $order => $field) {
                 $attribute = EavAttribute::findOne(['name' => $field['cid'], 'entityId' => $entityId]);
                 if (!$attribute) {
                     $attribute = new EavAttribute();
                     $lastId = EavAttribute::find()->select(['id'])->orderBy(['id' => SORT_DESC])->limit(1)->scalar() + 1;
                     $attribute->name = 'c' . $lastId;
                 }
                 //$attribute->name = $field['cid'];
                 $attribute->type = $field['type'];
                 $attribute->entityId = $entityId;
                 $attribute->typeId = EavAttributeType::find()->select(['id'])->where(['name' => $field['field_type']])->scalar();
                 $attribute->label = $field['label'];
                 //$attribute->defaultValue = '';
                 //$attribute->defaultOptionId = 0;
                 $attribute->required = $field['required'];
                 $attribute->order = $order;
                 $attribute->description = isset($field['field_options']['description']) ? $field['field_options']['description'] : '';
                 $attribute->save(false);
                 if (!isset($field['field_options']['options'])) {
                     continue;
                 }
                 EavAttributeOption::deleteAll(['attributeId' => $attribute->id]);
                 foreach ($field['field_options']['options'] as $o) {
                     $option = new EavAttributeOption();
                     $option->attributeId = $attribute->id;
                     $option->value = $o['label'];
                     $option->defaultOptionId = (int) $o['checked'];
                     $option->save();
                 }
             }
         }
     }
 }
Example #2
0
 public static function activeEavInput($model, $attribute, $options = [])
 {
     $handlerClass = EavAttribute::find()->select(['{{%eav_attribute_type}}.handlerClass'])->innerJoin('{{%eav_attribute_type}}', '{{%eav_attribute_type}}.id = {{%eav_attribute}}.typeId')->innerJoin('{{%eav_entity}}', '{{%eav_entity}}.id = {{%eav_attribute}}.entityId')->where(['{{%eav_attribute}}.name' => $attribute, '{{%eav_entity}}.entityModel' => $model::className()])->scalar();
     $name = isset($options['name']) ? $options['name'] : static::getInputName($model, $attribute);
     $eavModel = static::getAttributeValue($model, $attribute);
     $handler = $eavModel->handlers[$attribute];
     $handler->owner->activeForm = $options['form'];
     return $handler->run();
 }
Example #3
0
 /**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $query = EavAttribute::find();
     // add conditions that should always apply here
     $dataProvider = new ActiveDataProvider(['query' => $query]);
     $this->load($params);
     if (!$this->validate()) {
         // uncomment the following line if you do not want to return any records when validation fails
         // $query->where('0=1');
         return $dataProvider;
     }
     // grid filtering conditions
     $query->andFilterWhere(['id' => $this->id, 'entityId' => $this->entityId, 'typeId' => $this->typeId, 'defaultOptionId' => $this->defaultOptionId]);
     $query->andFilterWhere(['like', 'name', $this->name])->andFilterWhere(['like', 'label', $this->label])->andFilterWhere(['like', 'defaultValue', $this->defaultValue]);
     return $dataProvider;
 }
Example #4
0
 /**
  * Finds the EavAttribute model based on its primary key value.
  * If the model is not found, a 404 HTTP exception will be thrown.
  * @param integer $id
  * @return EavAttribute the loaded model
  * @throws NotFoundHttpException if the model cannot be found
  */
 protected function findModel($id)
 {
     if (($model = EavAttribute::findOne($id)) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }
Example #5
0
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getEavAttributes()
 {
     return $this->hasMany(EavAttribute::className(), ['typeId' => 'id'])->orderBy(['order' => SORT_DESC]);
 }
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getEavAttribute()
 {
     return $this->hasOne(EavAttribute::className(), ['id' => 'attributeId']);
 }
Example #7
0
 public function getListAttributes()
 {
     $models = EavAttribute::find()->select(['id', 'label'])->asArray()->all();
     return ArrayHelper::map($models, 'id', 'label');
 }
Example #8
0
 public function getLabel($attribute)
 {
     return EavAttribute::find()->select(['label'])->where(['name' => $attribute])->scalar();
 }
Example #9
0
 public function actionSave()
 {
     if (Yii::$app->request->isPost) {
         $post = Yii::$app->request->post();
         if ($post['payload'] && $post['entityModel']) {
             $payload = Json::decode($post['payload']);
             if (!isset($payload['fields'])) {
                 return;
             }
             $transaction = \Yii::$app->db->beginTransaction();
             try {
                 $categoryId = isset($post['categoryId']) ? $post['categoryId'] : 0;
                 $entityId = EavEntity::find()->select(['id'])->where(['entityModel' => $post['entityModel'], 'categoryId' => $categoryId])->scalar();
                 if (!$entityId) {
                     $entity = new EavEntity();
                     $entity->entityName = isset($post['entityName']) ? $post['entityName'] : 'Untitled';
                     $entity->entityModel = $post['entityModel'];
                     $entity->categoryId = $categoryId;
                     $entity->save(false);
                     $entityId = $entity->id;
                 }
                 $attributes = [];
                 foreach ($payload['fields'] as $order => $field) {
                     if (!isset($field['cid'])) {
                         continue;
                     }
                     // Attribute
                     if (isset($field['cid'])) {
                         $attribute = EavAttribute::findOne(['name' => $field['cid'], 'entityId' => $entityId]);
                     }
                     if (empty($attribute)) {
                         $attribute = new EavAttribute();
                         $lastId = EavAttribute::find()->select(['id'])->orderBy(['id' => SORT_DESC])->limit(1)->scalar() + 1;
                         $attribute->name = 'c' . $lastId;
                     }
                     $attribute->type = $field['group_name'];
                     $attribute->entityId = $entityId;
                     $attribute->typeId = EavAttributeType::find()->select(['id'])->where(['name' => $field['field_type']])->scalar();
                     $attribute->label = $field['label'];
                     $attribute->order = $order;
                     if (isset($field['field_options']['description'])) {
                         $attribute->description = $field['field_options']['description'];
                         unset($field['field_options']['description']);
                     } else {
                         $attribute->description = '';
                     }
                     $attribute->save(false);
                     $attributes[] = $attribute->id;
                     if (isset($field['field_options']['options'])) {
                         $options = [];
                         foreach ($field['field_options']['options'] as $k => $o) {
                             $option = EavAttributeOption::find()->where(['attributeId' => $attribute->id, 'value' => $o['label']])->one();
                             if (!$option) {
                                 $option = new EavAttributeOption();
                             }
                             $option->attributeId = $attribute->id;
                             $option->value = $o['label'];
                             $option->defaultOptionId = (int) $o['checked'];
                             $option->order = $k;
                             $option->save();
                             $options[] = $option->value;
                         }
                         EavAttributeOption::deleteAll(['and', ['attributeId' => $attribute->id], ['NOT', ['IN', 'value', $options]]]);
                         unset($field['field_options']['options']);
                     }
                     // Rule
                     $rule = EavAttributeRule::find()->where(['attributeId' => $attribute->id])->one();
                     if (!$rule) {
                         $rule = new EavAttributeRule();
                     }
                     $rule->attributeId = $attribute->id;
                     if (!empty($field['field_options'])) {
                         foreach ($field['field_options'] as $key => $param) {
                             $rule->{$key} = $param;
                         }
                     }
                     $rule->required = isset($field['field_options']['required']) ? (int) $field['field_options']['required'] : 0;
                     $rule->visible = isset($field['field_options']['visible']) ? (int) $field['field_options']['visible'] : 0;
                     $rule->locked = isset($field['field_options']['locked']) ? (int) $field['field_options']['locked'] : 0;
                     $rule->save();
                 }
                 $attrArray = EavAttribute::find()->select('id')->where(['AND', ['NOT IN', 'id', $attributes], ['IN', 'entityId', $entityId]])->asArray()->all();
                 if (!is_null($attrArray)) {
                     EavAttribute::deleteAll(['IN', 'id', $attrArray]);
                     EavAttributeValue::deleteAll(['IN', 'attributeId', $attrArray]);
                     EavAttributeOption::deleteAll(['IN', 'attributeId', $attrArray]);
                     EavAttributeRule::deleteAll(['IN', 'attributeId', $attrArray]);
                 }
                 $transaction->commit();
             } catch (\Exception $e) {
                 $transaction->rollBack();
                 throw $e;
             }
         }
     }
 }