/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = EavAttribute::find(); $dataProvider = new ActiveDataProvider(['query' => $query, 'pagination' => ['pageSize' => Yii::$app->request->cookies->getValue('_grid_page_size', 20)], 'sort' => ['defaultOrder' => ['id' => SORT_DESC]]]); $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; } $query->andFilterWhere(['id' => $this->id, 'type_id' => $this->type_id, 'required' => $this->required]); $query->andFilterWhere(['like', 'name', $this->name])->andFilterWhere(['like', 'label', $this->label])->andFilterWhere(['like', 'icon', $this->label])->andFilterWhere(['like', 'default_value', $this->default_value])->andFilterWhere(['like', 'description', $this->description]); return $dataProvider; }
protected function loadEavAttributes() { if (!$this->isAttributesLoaded) { $modelTable = EavEntityModel::tableName(); $entityTable = EavEntity::tableName(); $attributeTable = EavAttribute::tableName(); $entityToAttributeTable = '{{%eav_entity_attribute}}'; $attributes = EavAttribute::find()->innerJoin($entityToAttributeTable, "{$attributeTable}.id = {$entityToAttributeTable}.attribute_id")->innerJoin($entityTable, "{$entityToAttributeTable}.entity_id = {$entityTable}.id")->innerJoin($modelTable, "{$entityTable}.model_id = {$modelTable}.id")->andWhere(["{$modelTable}.entity_model" => $this->ownerClassName])->andWhere(["{$entityTable}.category_id" => $this->getEavCatgoryId()])->orderBy(['order' => SORT_DESC])->all(); $validators = $this->owner->getValidators(); foreach ($attributes as $attribute) { $this->eavAttributes->{$attribute['name']} = $attribute; $validators[] = \yii\validators\Validator::createValidator('safe', $this->owner, [$attribute['name']], []); } $this->isAttributesLoaded = true; } }
?> <div class="eav-attribute-option-form"> <?php $form = ActiveForm::begin(['id' => 'eav-attribute-option-form', 'validateOnBlur' => false]); ?> <div class="row"> <div class="col-md-9"> <div class="panel panel-default"> <div class="panel-body"> <?php echo $form->field($model, 'attribute_id')->dropDownList(EavAttribute::getEavAttributes(), ['prompt' => '']); ?> <?php echo $form->field($model, 'value')->textInput(['maxlength' => true]); ?> </div> </div> </div> <div class="col-md-3"> <div class="panel panel-default"> <div class="panel-body">
public function actionGetAttributes() { Yii::$app->response->format = Response::FORMAT_JSON; if (!($modelId = Yii::$app->getRequest()->post('model_id'))) { return null; } $params['model_id'] = (int) $modelId; $category_id = Yii::$app->getRequest()->post('category_id'); $params['category_id'] = $category_id ? $category_id : null; $entity = EavEntity::findOne($params); if (!$entity) { try { $entity = new EavEntity($params); $entity->save(); } catch (Exception $exc) { return false; } } $querySelected = $entity->getEavAttributes()->orderBy('label')->limit(null); $queryAvailable = clone $querySelected; $selectedDataProvider = new ActiveDataProvider(['query' => $querySelected]); $ids = $queryAvailable->select('id')->all(); $ids = array_map(function ($item) { return $item->id; }, $ids); $ids = !empty($ids) ? implode(', ', $ids) : '0'; $availableDataProvider = new ActiveDataProvider(['query' => EavAttribute::find()->where("id NOT IN ({$ids})")->limit(null)]); $selected = $this->renderIsAjax('attributes', ['dataProvider' => $selectedDataProvider]); $available = $this->renderIsAjax('attributes', ['dataProvider' => $availableDataProvider]); return ['available' => $available, 'selected' => $selected]; }
?> </div> <div class="col-sm-6 text-right"> <?php echo GridPageSize::widget(['pjaxId' => 'eav-attribute-option-grid-pjax']); ?> </div> </div> <?php Pjax::begin(['id' => 'eav-attribute-option-grid-pjax']); ?> <?php echo GridView::widget(['id' => 'eav-attribute-option-grid', 'dataProvider' => $dataProvider, 'filterModel' => $searchModel, 'bulkActionOptions' => ['gridId' => 'eav-attribute-option-grid', 'actions' => [Url::to(['bulk-delete']) => Yii::t('yee', 'Delete')]], 'columns' => [['class' => 'yeesoft\\grid\\CheckboxColumn', 'options' => ['style' => 'width:10px']], ['attribute' => 'id', 'options' => ['style' => 'width:20px']], ['class' => 'yeesoft\\grid\\columns\\TitleActionColumn', 'attribute' => 'value', 'controller' => '/eav/attribute-option', 'buttonsTemplate' => '{update} {delete}', 'title' => function (EavAttributeOption $model) { return Html::a($model->value, ['update', 'id' => $model->id], ['data-pjax' => 0]); }], ['attribute' => 'attribute_id', 'value' => function (EavAttributeOption $model) { return "{$model->attribute->id} - {$model->attribute->name} - {$model->attribute->label}"; }, 'filter' => ArrayHelper::merge(['' => Yii::t('yee', 'Not Selected')], EavAttribute::getEavAttributes()), 'options' => ['style' => 'width:300px']]]]); ?> <?php Pjax::end(); ?> </div> </div> </div>
/** * @return \yii\db\ActiveQuery */ public function getAttribute($name = '') { return $this->hasOne(EavAttribute::className(), ['id' => 'attribute_id']); }
/** * @return \yii\db\ActiveQuery */ public function getEavAttributes() { return $this->hasMany(EavAttribute::className(), ['id' => 'entity_id'])->viaTable('{{%eav_entity_attribute}}', ['attribute_id' => 'id'])->orderBy(['order' => SORT_DESC]); }