public function addColumnFilters($columns, $searchFields) { $filterableColumns = []; foreach ($columns as $key => $column) { if (!is_array($column) || !isset($column['attribute'])) { continue; } $filterableColumns[$column['attribute']] = $key; } foreach ($searchFields as $fieldName => $searchField) { if (!$searchField instanceof ActiveField || !isset($filterableColumns[$fieldName]) || !isset($searchField->parts['{input}'])) { continue; } $key = $filterableColumns[$fieldName]; $filter = $searchField->parts['{input}']; if (is_array($filter)) { $class = $filter['class']; if ($class === Select2::className()) { $filter['clientOptions']['width'] = '15em'; } $columns[$key]['filter'] = $class::widget($filter); } else { $columns[$key]['filter'] = $filter; } } return $columns; }
/** * @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; }
echo Yii::t('maddoger/website', 'Create menu first.'); } else { $pageForm = ActiveForm::begin(['action' => '#', 'id' => 'page-form']); echo Html::activeHiddenInput($newItem, 'type', ['value' => Menu::TYPE_PAGE]); echo Html::activeHiddenInput($newItem, 'label'); echo Html::activeHiddenInput($newItem, 'link'); ?> <div class="clearfix"> <div class="pull-right"> <?php echo Html::dropDownList('sort', 'updated_at', ['updated_at' => Yii::t('maddoger/website', 'Last updated'), 'title' => Yii::t('maddoger/website', 'By title')], ['id' => 'new-item-page-sort']); ?> </div> <?php echo $pageForm->field($newItem, 'page_id')->widget(Select2::className(), ['clientOptions' => ['placeholder' => Yii::t('maddoger/website', 'Search pages...'), 'ajax' => ['url' => Url::to(['pages']), 'dataType' => 'json', 'data' => new JsExpression('function (term, page) { return { q: term, sort: $("#new-item-page-sort").val() }; }'), 'results' => new JsExpression('function (data, page) { return { results: data }; }')], 'formatResult' => $formatResult = new JsExpression('function (state) { if (!state.id) return state.text; // optgroup return state.text + " <small class=\\"text-muted\\">"+state.url+"</small>"; }'), 'formatSelection' => $formatResult], 'clientEvents' => ['change' => new JsExpression('function (event) { $("#page-form input[name=\\"MenuNewItem[label]\\"]").val(event.added.title); $("#page-form input[name=\\"MenuNewItem[link]\\"]").val(event.added.url); }')]]); ?> </div> <?php echo Html::submitButton(Yii::t('maddoger/website', 'Add to menu'), ['class' => 'btn btn-default ajax-add']); ?> <?php ActiveForm::end(); } ?>