/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Attribute::find(); $dataProvider = new ActiveDataProvider(['query' => $query]); $this->load($params); if (!$this->validate()) { // uncomment the following line if you do not want to any records when validation fails // $query->where('0=1'); return $dataProvider; } $query->andFilterWhere(['like', 'name', $this->name]); return $dataProvider; }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Value::find(); $dataProvider = new ActiveDataProvider(['query' => $query]); $dataProvider->sort->attributes['attribute'] = ['asc' => [Attribute::tableName() . '.presentation' => SORT_ASC], 'desc' => [Attribute::tableName() . '.presentation' => SORT_DESC]]; if (!($this->load($params) && $this->validate())) { $dataProvider->getSort() !== false ? $query->joinWith(['eAttribute']) : $query->with('eAttribute'); return $dataProvider; } else { $query->joinWith(['eAttribute']); } $query->andFilterWhere(['like', 'value', $this->value]); $query->andFilterWhere(['like', Attribute::tableName() . '.presentation', $this->attribute]); return $dataProvider; }
/** * @inheritdoc */ public function getValues() { $values = parent::getValues(); if (!$values && strncmp($this->attribute_name, 'eAttributes.', 12) === 0) { $name = substr($this->attribute_name, 12); $attribute = Attribute::findByNameAndEntityType($name, $this->entity_type); if ($attribute->isValuesPredefined()) { $values = $attribute->getValues(); if ($values) { foreach ($values as $value) { $values[] = new FacetTerm(['facet' => $this, 'term' => $value->value, 'display' => $value->presentation]); } $this->populateRelation('values', $values); } } } return $values; }
use yii\helpers\ArrayHelper; use yii\helpers\Html; use yii\widgets\ActiveForm; /* @var $this yii\web\View */ /* @var $model im\eav\models\Value */ /* @var $form yii\widgets\ActiveForm */ ?> <div class="value-form"> <?php $form = ActiveForm::begin(); ?> <?php echo $form->field($model, 'attribute_id')->widget(Select2::classname(), ['data' => ArrayHelper::map(Attribute::find()->asArray()->orderBy('name')->all(), 'id', 'presentation'), 'options' => ['prompt' => '']]); ?> <?php echo $form->field($model, 'value')->textInput(['maxlength' => true]); ?> <?php echo $form->field($model, 'presentation')->textInput(['maxlength' => true]); ?> <?php echo Html::submitButton($model->isNewRecord ? Module::t('module', 'Create') : Module::t('module', 'Update'), ['class' => $model->isNewRecord ? 'btn btn-primary' : 'btn btn-success']); ?> <?php
/** * @return ActiveQuery */ public function getEAttributeRelation() { return $this->hasOne(Attribute::className(), ['id' => 'attribute_id']); }
/** * @inheritdoc */ public function getSearchableAttributes($recursive = true) { /** @var \im\base\types\EntityTypesRegister $typesRegister */ $typesRegister = Yii::$app->get('typesRegister'); /** @var \im\search\components\SearchManager $searchManager */ $searchManager = Yii::$app->get('searchManager'); $model = $this->getModel(); if ($model instanceof AttributeValue) { return []; } $entityType = $typesRegister->getEntityType($model); $attributes = $model->attributes(); $labels = $model->attributeLabels(); $searchableAttributes = []; $key = 0; // Attributes foreach ($attributes as $attribute) { $searchableAttributes[$key] = new AttributeDescriptor(['entity_type' => $entityType, 'name' => $attribute, 'label' => isset($labels[$attribute]) ? $labels[$attribute] : $model->generateAttributeLabel($attribute)]); $key++; } // EAV $eavAttributes = Attribute::findByEntityType($entityType); foreach ($eavAttributes as $attribute) { $searchableAttributes[$key] = new AttributeDescriptor(['entity_type' => $entityType, 'name' => $attribute->getName() . '_attr', 'label' => $attribute->getPresentation()]); $key++; } // Relations $class = new \ReflectionClass($model); foreach ($class->getMethods() as $method) { if ($method->isPublic()) { $methodName = $method->getName(); if (strpos($methodName, 'get') === 0) { $math = false; $name = substr($methodName, 3); if (substr($name, -8) === 'Relation') { $name = substr($name, 0, -8); $math = true; } elseif (preg_match('/@return.*ActiveQuery/i', $method->getDocComment())) { $math = true; } if ($math && $name) { /** @var ActiveQuery $relation */ $relation = $model->{$methodName}(); $modelClass = $relation->modelClass; $searchableType = $searchManager->getSearchableTypeByClass($modelClass); if (!$searchableType) { $reflector = new ReflectionClass($modelClass); /** @var SearchableInterface $searchableType */ $searchableType = Yii::createObject(['class' => 'im\\search\\components\\service\\db\\SearchableType', 'type' => Inflector::camel2id($reflector->getShortName(), '_'), 'modelClass' => $modelClass]); } $searchableAttributes[$key] = new AttributeDescriptor(['entity_type' => $entityType, 'name' => Inflector::variablize(substr($methodName, 3)), 'label' => Inflector::titleize($name), 'value' => function ($model) use($methodName) { return $model->{$methodName}(); }, 'type' => $searchableType->getType()]); $key++; if ($recursive) { foreach ($searchableType->getSearchableAttributes(false) as $attribute) { $attribute->label = Inflector::titleize($name) . ' >> ' . $attribute->label; $attribute->name = Inflector::variablize(substr($methodName, 3)) . '.' . $attribute->name; $searchableAttributes[$key] = $attribute; $key++; } } } } } } return $searchableAttributes; }
/** * @param string $entityType * @return AttributeDescriptor[] */ protected function getSearchableEAVAttributes($entityType) { $searchableAttributes = []; $eavAttributes = Attribute::findByEntityType($entityType); foreach ($eavAttributes as $attribute) { $searchableAttributes[] = new AttributeDescriptor(['name' => $attribute->getName() . '_attr', 'label' => $attribute->getPresentation()]); } return $searchableAttributes; }
/** * @inheritdoc */ public function rules() { return array_merge(parent::rules(), [['entity_type', 'default', 'value' => Yii::$app->get('typesRegister')->getEntityType(Product::className())]]); }
/** * @inheritdoc */ public function getAttributeLabel($attribute) { $labels = $this->attributeLabels(); if (isset($labels[$attribute])) { return $labels[$attribute]; } elseif ($this->isEAttribute($attribute)) { $attribute = $this->normalizeEAttributeName($attribute); if (isset($this->_eAttributeLabels[$attribute])) { return $this->_eAttributeLabels[$attribute]; } /** @var AttributeInterface $attribute */ $model = Attribute::findOne(['name' => $attribute]); if ($model !== null) { return $this->_eAttributeLabels[$attribute] = $model->getPresentation(); } else { return $this->generateAttributeLabel($attribute); } } else { return $this->generateAttributeLabel($attribute); } }
<?php use im\eav\models\Attribute; use im\eav\Module; use yii\helpers\ArrayHelper; use yii\helpers\Html; use yii\widgets\ActiveForm; /* @var $this yii\web\View */ /* @var $attributes im\eav\models\AttributeValue[] */ /* @var $form ActiveForm|array */ ?> <div data-cont="fields"> <?php echo $this->render('@im/eav/backend/views/attribute/_fields', ['attributes' => $attributes, 'form' => $form]); ?> </div> <div class="form-inline"> <?php echo Html::dropDownList('attributes', null, ArrayHelper::map(Attribute::find()->asArray()->orderBy('name')->all(), 'id', 'presentation'), ['class' => 'form-control', 'data-field' => 'attributes', 'multiple' => true]); ?> <?php echo Html::button(Module::t('attribute', 'Add attribute'), ['class' => 'btn btn-primary', 'data-action' => 'add']); ?> </div> <div data-cont="temp"></div>
/** * Finds the Attribute model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Attribute the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Attribute::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }