/** * @param \netis\crud\db\ActiveRecord $model * @param string $attribute * @param array $options * @param bool $multiple * * @return \yii\bootstrap\ActiveField * @throws InvalidConfigException */ public static function createActiveField($model, $attribute, $options = [], $multiple = false) { /** @var Formatter $formatter */ $formatter = Yii::$app->formatter; $stubForm = new \stdClass(); $stubForm->layout = 'default'; /** @var \yii\bootstrap\ActiveField $field */ $field = Yii::createObject(['class' => \yii\bootstrap\ActiveField::className(), 'model' => $model, 'attribute' => $attribute, 'form' => $stubForm]); $attributeName = Html::getAttributeName($attribute); $attributeFormat = $model->getAttributeFormat($attributeName); $format = is_array($attributeFormat) ? $attributeFormat[0] : $attributeFormat; $column = $model->getTableSchema()->getColumn($attributeName); switch ($format) { case 'boolean': if ($multiple) { $field->inline()->radioList(['0' => $formatter->booleanFormat[0], '1' => $formatter->booleanFormat[1], '' => Yii::t('app', 'Any')], $options); } else { $field->checkbox($options); } break; case 'shortLength': $value = Html::getAttributeValue($model, $attribute); if (!isset($options['value'])) { $options['value'] = $value === null ? null : $formatter->asMultiplied($value, 1000); } $field->textInput($options); $field->inputTemplate = '<div class="input-group">{input}<span class="input-group-addon">m</span></div>'; break; case 'shortWeight': $value = Html::getAttributeValue($model, $attribute); if (!isset($options['value'])) { $options['value'] = $value === null ? null : $formatter->asMultiplied($value, 1000); } $field->textInput($options); $field->inputTemplate = '<div class="input-group">{input}<span class="input-group-addon">kg</span></div>'; break; case 'multiplied': $value = Html::getAttributeValue($model, $attribute); if (!isset($options['value'])) { $options['value'] = $value === null ? null : $formatter->asMultiplied($value, $attributeFormat[1]); } $field->textInput($options); break; case 'integer': if (!isset($options['value'])) { $options['value'] = Html::getAttributeValue($model, $attribute); } $field->textInput($options); break; case 'time': if (!isset($options['value'])) { $options['value'] = Html::encode(Html::getAttributeValue($model, $attribute)); } $field->textInput($options); break; case 'datetime': case 'date': if (!isset($options['value'])) { $value = Html::getAttributeValue($model, $attribute); if (!$model->hasErrors($attribute) && $value !== null) { $value = $formatter->format($value, $format); } $options['value'] = $value; } if (!isset($options['class'])) { $options['class'] = 'form-control'; } $field->parts['{input}'] = array_merge(['class' => \omnilight\widgets\DatePicker::className(), 'model' => $model, 'attribute' => $attributeName, 'options' => $options], $format !== 'datetime' ? [] : ['class' => \kartik\datetime\DateTimePicker::className(), 'convertFormat' => true]); break; case 'enum': $items = $formatter->getEnums()->get($attributeFormat[1]); if ($multiple) { $options = array_merge(['class' => 'select2', 'placeholder' => self::getPrompt(), 'multiple' => 'multiple'], $options); $field->parts['{input}'] = ['class' => \maddoger\widgets\Select2::className(), 'model' => $model, 'attribute' => $attribute, 'items' => $items, 'clientOptions' => ['allowClear' => true, 'closeOnSelect' => true], 'options' => $options]; } else { if ($column !== null && $column->allowNull) { $options['prompt'] = self::getPrompt(); } $field->dropDownList($items, $options); } break; case 'flags': throw new InvalidConfigException('Flags format is not supported by ' . get_called_class()); case 'paragraphs': if (!isset($options['value'])) { $options['value'] = Html::encode(Html::getAttributeValue($model, $attribute)); } if ($multiple) { $field->textInput($options); } else { $field->textarea(array_merge(['cols' => '80', 'rows' => '10'], $options)); } break; case 'file': if (!isset($options['value'])) { $options['value'] = Html::getAttributeValue($model, $attribute); } $field->fileInput($options); break; default: case 'text': if (!isset($options['value'])) { $options['value'] = Html::getAttributeValue($model, $attribute); } if ($column && $column->type === 'string' && $column->size !== null) { $options['maxlength'] = $column->size; } $field->textInput($options); break; } return $field; }
/** * Returns all primary and foreign key column names for specified model. * @param ActiveRecord $model * @param bool $includePrimary * @return array names of columns from primary and foreign keys */ public static function getModelKeys($model, $includePrimary = true) { $keys = array_map(function ($foreignKey) { array_shift($foreignKey); return array_keys($foreignKey); }, $model->getTableSchema()->foreignKeys); if ($includePrimary) { $keys[] = $model->primaryKey(); } return call_user_func_array('array_merge', $keys); }