/** * Retrieves grid columns configuration using the modelClass. * @param Model $model * @param array $fields * @return array grid columns */ public function getIndexGridColumns($model, $fields) { $id = Yii::$app->request->getQueryParam('id'); $relationName = Yii::$app->request->getQueryParam('relation'); $multiple = Yii::$app->request->getQueryParam('multiple', 'true') === 'true'; foreach ($fields as $key => $field) { if ((is_array($field) || !is_string($field) && is_callable($field)) && $key === $relationName || $field === $relationName) { unset($fields[$key]); } } return array_merge([['class' => 'yii\\grid\\CheckboxColumn', 'multiple' => $multiple, 'headerOptions' => ['class' => 'column-serial'], 'checkboxOptions' => function ($model, $key, $index, $column) use($id, $relationName) { /** @var \yii\db\ActiveRecord $model */ $options = ['value' => is_array($key) ? json_encode($key, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) : $key]; if (empty($relationName) || $model->{$relationName} === null || trim($id) === '') { return $options; } $relation = $model->getRelation($relationName); if ($relation->multiple) { /** @var \yii\db\ActiveRecord $relationClass */ $relationClass = $relation->modelClass; if (Action::importKey($relationClass::primaryKey(), $id) === $model->getAttributes(array_keys($relation->link))) { $options['checked'] = true; $options['disabled'] = true; } } elseif (Action::exportKey($relation->one()->getPrimaryKey()) === $id) { $options['checked'] = true; $options['disabled'] = true; } return $options; }], ['class' => 'yii\\grid\\SerialColumn', 'headerOptions' => ['class' => 'column-serial']]], self::getGridColumns($model, $fields)); }
/** * @return ActiveSearchInterface|ActiveRecord */ public function getSearchModel() { /** @var ActiveRecord $model */ if ($this->controller instanceof ActiveController) { $model = $this->controller->getSearchModel(); } else { $model = new $this->modelClass(); } $params = Yii::$app->request->queryParams; $scope = $model->formName(); if ($scope === '' && is_array($params) || $scope !== '' && isset($params[$scope]) && is_array($params[$scope])) { $model->load($params); } if (isset($params['ids'])) { $keys = Action::importKey($model::primaryKey(), Action::explodeKeys($params['ids'])); $model->setAttributes($keys); } return $model; }
/** * Reads relations information from data sent by end user and uses it to link records to current model. * This is an equivalent to load() and save() methods. * * The data to be loaded is `$data[formName]`, where `formName` refers to the value of [[formName()]]. * If [[formName()]] is empty, the whole `$data` array will be used. * Keys should be relation names. Values are either: * - numeric arrays containing primary key values; * - associative array with keys: 'add', 'remove'. * Warning! Data will NOT be filtered or validated in any way. * @param array $data the data array. This is usually `$_POST` or `$_GET`, but can also be any valid array * supplied by end user. * @param string $formName the form name to be used for loading the data into the model. * If not set, [[formName()]] will be used. * @return bool whether no data was sent or the relations has been successfully linked. * @throws ForbiddenHttpException * @throws \yii\base\InvalidConfigException */ public function saveRelations($data, $formName = null) { /** @var \yii\db\ActiveRecord $owner */ $owner = $this->owner; $scope = $formName === null ? $owner->formName() : $formName; if ($scope !== '') { $data = isset($data[$scope]) ? $data[$scope] : []; } foreach ($data as $relationName => $keys) { if (($relation = $owner->getRelation($relationName, false)) === null) { continue; } if (is_string($keys) && trim($keys) === '') { continue; } if (!is_array($keys) || isset($keys[0])) { $keys = is_array($keys) ? $keys : Action::explodeKeys($keys); $addKeys = Action::importKey($owner::primaryKey(), $keys); $removeKeys = null; } elseif (is_array($keys) && isset($keys['add']) && isset($keys['remove'])) { $addKeys = Action::importKey($owner::primaryKey(), Action::explodeKeys($keys['add'])); $removeKeys = Action::importKey($owner::primaryKey(), Action::explodeKeys($keys['remove'])); } else { throw new InvalidCallException('Relation keys must be either a string, a numeric array or an array with \'add\' and \'remove\' keys.'); continue; } $this->linkByKeys($relation, $addKeys, $removeKeys); } return true; }