Exemplo n.º 1
0
 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]);
 }
Exemplo n.º 2
0
 /**
  * @param ActiveRecord $model
  * @param $options
  * @return array
  */
 public function createListWidget($model, $options)
 {
     if (is_array($options) && isset($options[0]) && isset($options[1])) {
         $attribute = array_shift($options);
         $type = array_shift($options);
         $editable = isset($options['editable']) && $options['editable'] == true;
         $editableType = isset($options['editableType']) ? $options['editableType'] : Editable::INPUT_TEXT;
         unset($options['editable'], $options['editableType']);
         $config = ['attribute' => $attribute];
         $choices = [];
         switch ($type) {
             case 'date':
                 $config = ['attribute' => $attribute, 'format' => ['datetime', 'php:Y-m-d'], 'options' => ['style' => 'width:240px'], 'filterWidgetOptions' => ['type' => DatePicker::TYPE_RANGE, 'attribute2' => $attribute . '_to'], 'filterType' => DatePicker::className()];
                 break;
             case 'datetime':
                 $config = ['attribute' => $attribute, 'format' => ['datetime', 'php:Y-m-d H:i:s'], 'options' => ['style' => 'width:240px'], 'filterWidgetOptions' => ['type' => DatePicker::TYPE_RANGE, 'attribute2' => $attribute . '_to'], 'filterType' => DatePicker::className()];
                 break;
             case 'boolean':
                 $choices = ModelHelper::getBooleanChoices($model, $attribute);
                 $config = ['class' => \kartik\grid\BooleanColumn::className(), 'attribute' => $attribute, 'filterWidgetOptions' => ['data' => $choices, 'pluginOptions' => ['allowClear' => true, 'placeholder' => 'Select...']], 'filterType' => Select2::className()];
                 break;
             case 'enumerate':
                 $choices = ModelHelper::getEnumChoices($model, $attribute);
                 $config = ['attribute' => $attribute, 'filterWidgetOptions' => ['data' => $choices, 'pluginOptions' => ['allowClear' => true, 'placeholder' => 'Select...']], 'filterType' => Select2::className()];
                 break;
             case 'relation':
                 $relation = ModelHelper::getRelation($model, $attribute);
                 $choices = ModelHelper::getSelectChoices(new $relation->modelClass());
                 $config = ['label' => ucfirst(strpos($attribute, '.') !== false ? substr($attribute, strrpos($attribute, '.') + 1) : $attribute), 'attribute' => $attribute, 'value' => function ($model, $key, $index, $widget) use($attribute) {
                     return $model ? ModelHelper::getLabelRelationValue($model, $attribute) : null;
                 }, 'filter' => !$relation->multiple ? $choices : false, 'filterWidgetOptions' => ['data' => $choices, 'pluginOptions' => ['allowClear' => true, 'placeholder' => '...']], 'filterType' => Select2::className()];
                 break;
         }
         if ($editable) {
             switch ($type) {
                 case 'date':
                     $config['editableOptions'] = ['inputType' => Editable::INPUT_DATE];
                     break;
                 case 'datetime':
                     $config['editableOptions'] = ['inputType' => Editable::INPUT_DATETIME];
                     break;
                 case 'boolean':
                 case 'enumerate':
                     $config['editableOptions'] = ['inputType' => Editable::INPUT_SELECT2, 'options' => ['data' => $choices], 'displayValueConfig' => $choices];
                     break;
                 case 'relation':
                     $relation = ModelHelper::getRelation($model, $attribute);
                     $config['editableOptions'] = ['inputType' => Editable::INPUT_SELECT2, 'size' => 'lg', 'options' => ['options' => ['multiple' => $relation->multiple], 'data' => $choices, 'pluginOptions' => count($choices) > 20 ? ['minimumInputLength' => 3, 'ajax' => ['url' => Url::to(['/ycm/model/choices', 'name' => $this->getModelName(new $relation->modelClass())]), 'dataType' => 'json', 'processResults' => new JsExpression('function (results) { return results; }')]] : null], 'displayValueConfig' => !$relation->multiple ? $choices : null];
                     break;
                 default:
                     $config['editableOptions']['options'] = $config;
                     break;
             }
             $config = ArrayHelper::merge(['editableOptions' => ['inputType' => $editableType]], $config, ['class' => EditableColumn::className(), 'editableOptions' => ['ajaxSettings' => ['url' => Url::to(['/ycm/model/editable', 'name' => $this->getModelName($model)])]]]);
         }
         $options = ArrayHelper::merge($config, $options);
     }
     return $options;
 }