/** * 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; }
/** * @param ActiveForm|array $form * @param array $options * @return ActiveField * @throws \yii\base\InvalidConfigException */ public function getField($form, $options = []) { $attribute = "[{$this->getName()}]value"; $config = []; if ($form instanceof ActiveForm) { $config = $form->fieldConfig; } elseif (is_array($form) && isset($form['fieldConfig'])) { $config = $form['fieldConfig']; } if ($config instanceof \Closure) { $config = call_user_func($config, $this, $attribute); } if (!isset($config['class'])) { $config['class'] = is_array($form) ? DynamicActiveField::className() : $form->fieldClass; } $config['template'] = "{label}\n<div class=\"input-group\">\n{input}<span class=\"input-group-btn\"><button type=\"button\" class=\"btn btn-danger\"><span class=\"glyphicon glyphicon-remove\" aria-hidden=\"true\"></span></button>\n</span>\n</div>\n{hint}\n{error}"; $fieldConfig = $this->getEAttribute()->getFieldConfig(); $fieldOptions = ArrayHelper::getValue($fieldConfig, 'fieldOptions', []); $labelOptions = ArrayHelper::getValue($fieldConfig, 'labelOptions', []); if (is_array($form)) { $options['formConfig'] = $form; } else { $options['form'] = $form; } $field = Yii::createObject(ArrayHelper::merge($config, $fieldOptions, $options, ['model' => $this, 'attribute' => $attribute])); if ($labelOptions) { $field->label(null, $labelOptions); } else { $field->label($this->getPresentation()); } $fieldType = ArrayHelper::getValue($fieldConfig, 'fieldType', 'textInput'); $inputOptions = ArrayHelper::getValue($fieldConfig, 'inputOptions', []); if ($this->getEAttribute()->predefinedValues) { $fieldType = 'dropDownList'; $inputOptions['items'] = ArrayHelper::map(Value::find()->where(['id' => $this->getEAttribute()->id])->asArray()->all(), 'id', 'value'); $inputOptions['options']['prompt'] = ''; } if (method_exists($field, $fieldType)) { $parameters = []; $class = new \ReflectionClass(get_class($field)); $reflectionParameters = $class->getMethod($fieldType)->getParameters(); foreach ($reflectionParameters as $parameter) { $name = $parameter->getName(); $parameters[$name] = ArrayHelper::remove($inputOptions, $name, $parameter->isDefaultValueAvailable() ? $parameter->getDefaultValue() : null); } // if (array_key_exists('items', $parameters) && empty($parameters['items']) && AttributeTypes::isActiveRecordType($this->getType())) { // /** @var ActiveRecord $class */ // $class = Yii::$app->core->getEntityClass($this->getType()); // $reflector = new \ReflectionClass($class); // $typeName = Inflector::titleize($reflector->getShortName()); // $hasToString = $reflector->hasMethod('__toString'); // $items = ArrayHelper::map($class::find()->all(), function($row) { // /** @var ActiveRecord $row */ // $pk = $row->getPrimaryKey(false); // return is_array($pk) ? json_encode($pk) : $pk; // }, function ($row) use ($hasToString, $typeName) { // /** @var ActiveRecord $row */ // if ($hasToString) { // return $row->__toString(); // } else { // $pk = $row->getPrimaryKey(false); // $pk = is_array($pk) ? json_encode($pk) : $pk; // return $typeName . ' ' . $pk; // } // }); // $parameters['items'] = $items ? $items : []; // } // // //TODO multiple, promp option for dropdown call_user_func_array(array($field, $fieldType), $parameters); } return $field; }
/** * @return \yii\db\ActiveQuery */ public function getValuesRelation() { return $this->hasMany(Value::className(), ['attribute_id' => 'id']); }
/** * Finds the Value model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Value the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Value::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }