/** * @inheritdoc */ public function attach($owner) { if (property_exists($owner, Module::MODEL_MULTILANG_PROPERTY_NAME)) { foreach ((array) $owner->attributes_multilang as $attribute_multilang) { foreach (ModelHelper::getMultiLangAttributes($owner, $attribute_multilang) as $attribute) { $this->rules[] = [[$attribute], 'safe', 'on' => 'default']; } } } parent::attach($owner); }
public function relation($relation_name) { $relation = ModelHelper::getRelation($this->model, $relation_name); $relationClass = $relation->modelClass; $relationField = $relationClass::tableName() . '.' . ($relation->multiple ? array_values($relation->link)[0] : array_keys($relation->link)[0]); $relationValue = $this->model->{$relation_name}; if ($relationValue instanceof ActiveRecord) { $relationValue = ModelHelper::getPkColumnValue($relationValue); } $this->query->joinWith($relation_name)->andFilterWhere([$relationField => $relationValue]); }
public function actionChoices($name, $q = null, $id = null) { $out = ['results' => ['id' => '', 'text' => '']]; \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; /* @var ActiveRecord $model */ $model = $this->module->loadModel($name); if (!is_null($q)) { $out['results'] = ModelHelper::findChoicesByLabel($model, $q); } elseif ($id > 0) { $out['results'] = [ModelHelper::getPkColumnName($model) => $id, 'text' => ModelHelper::getLabelColumnName($model->findOne($id))]; } return $out; }
/** * Create ActiveField object. * * @param \yii\widgets\ActiveForm $form * @param \yii\db\ActiveRecord $model Model * @param string $attribute Model attribute * @param array $options Attribute options * @param string $type ActiveField type * @return \yii\widgets\ActiveField ActiveField object * @throws InvalidConfigException */ protected function createField($form, $model, $attribute, $options, $type = 'textInput') { $options = $this->getAttributeOptions($attribute, $options); $fieldOptions = []; if (isset($options['fieldOptions'])) { $fieldOptions = $options['fieldOptions']; unset($options['fieldOptions']); } $field = $form->field($model, $attribute, $fieldOptions); if (isset($options['hint'])) { $hintOptions = []; if (isset($options['hintOptions'])) { $hintOptions = $options['hintOptions']; unset($options['hintOptions']); } $field->hint($options['hint'], $hintOptions); unset($options['hint']); } if (isset($options['label'])) { $labelOptions = []; if (isset($options['labelOptions'])) { $labelOptions = $options['labelOptions']; unset($options['labelOptions']); } $field->label($options['label'], $labelOptions); unset($options['label']); } if (isset($options['input'])) { $input = $options['input']; unset($options['input']); $field = $field->input($input, $options); } else { if ($type == 'dropDownList' || $type == 'listBox' || $type == 'checkboxList' || $type == 'radioList') { $items = $this->getAttributeChoices($model, $attribute); $field->{$type}($items, $options); } elseif ($type == 'select') { if (isset($options['data'])) { $options['data'] = $options['data'] + $this->getAttributeChoices($model, $attribute); } else { $options['data'] = $this->getAttributeChoices($model, $attribute); } $field->widget(Select2::className(), $options); } elseif ($type == 'widget') { if (isset($options['widgetClass'])) { $class = $options['widgetClass']; unset($options['widgetClass']); } else { throw new InvalidConfigException('Widget class missing from configuration.'); } if (property_exists($class, 'form')) { $options['form'] = $form; } $field->widget($class, $options); } else { $field->{$type}($options); } } /** * Multilanguage DB fields support */ if ($attributes_multilang = ModelHelper::getMultiLangAttributes($model, $attribute)) { $field->addon = ['prepend' => ['content' => strtoupper(substr(\Yii::$app->sourceLanguage, 0, 2))]]; $field .= implode('', array_map(function ($attribute_multilang) use($form, $model, $attribute, $options, $type) { $options['label'] = false; $field = $this->createField($form, $model, $attribute_multilang, $options, $type); $field->addon = ['prepend' => ['content' => strtoupper(trim(str_replace($attribute, '', $attribute_multilang), '-_'))]]; return $field; }, $attributes_multilang)); } return $field; }