/** * 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)); }
/** * Get drop down list items using provided Query. * * __WARNING__: This method does not append authorized conditions to query and you need append those if needed. * * @param \yii\db\ActiveQuery $query * * @return array */ public static function getDropDownItems($query) { if ($query instanceof ActiveQuery) { $query->defaultOrder(); } /** @var \yii\db\ActiveRecord|\netis\rbac\AuthorizerBehavior $model */ $model = new $query->modelClass(); $fields = $model::primaryKey(); if (($labelAttributes = $model->getBehavior('labels')->attributes) !== null) { $fields = array_merge($model::primaryKey(), $labelAttributes); } $flippedPrimaryKey = array_flip($model::primaryKey()); return ArrayHelper::map($query->from($model::tableName() . ' t')->all(), function ($item) use($fields, $flippedPrimaryKey) { /** @var \netis\crud\db\ActiveRecord $item */ return Action::exportKey(array_intersect_key($item->toArray($fields, []), $flippedPrimaryKey)); }, function ($item) use($fields) { /** @var \netis\crud\db\ActiveRecord $item */ $data = $item->toArray($fields, []); return $data['_label']; }); }
use yii\helpers\Html; use yii\helpers\Url; /* @var $this yii\web\View */ /* @var $model netis\crud\db\ActiveRecord */ /* @var mixed $sourceState */ /* @var mixed $targetState */ /* @var array $states */ /* @var $controller netis\crud\crud\ActiveController */ $controller = $this->context; $this->title = $model->getCrudLabel('update') . ': ' . $model->__toString(); $this->params['breadcrumbs'] = $controller->getBreadcrumbs($controller->action, $model); $this->params['menu'] = $controller->getMenu($controller->action, $model); $format = $model->getAttributeFormat($model->getStateAttributeName()); $confirmUrl = Url::toRoute([$action->id, 'id' => \netis\crud\crud\Action::exportKey($model->getPrimaryKey(true)), 'targetState' => $targetState, 'confirmed' => 1]); $cancelUrl = Url::toRoute([isset($_GET['return']) ? $_GET['return'] : $controller->action->viewAction, 'id' => \netis\crud\crud\Action::exportKey($model->getPrimaryKey(true))]); ?> <?php echo Yii::t('netis/fsm/app', 'Change status from {source} to {target}', ['source' => '<span class="badge badge-default">' . Yii::$app->formatter->format($sourceState, $format) . '</span>', 'target' => '<span class="badge badge-primary">' . Yii::$app->formatter->format($targetState, $format) . '</span>']); ?> <?php echo netis\crud\web\Alerts::widget(); ?> <div class="form"> <?php echo Html::label(Yii::t('netis/fsm/app', 'Reason'), 'reason'); ?> <div class="row">
/** * Formats the value as a link to a matching controller. * @param Model|Model[] $value the value to be formatted. * @param array $options the tag options in terms of name-value pairs. See [[Html::a()]]. * @param string $action name of the action to link to * @param string|callable $label if not null, will be used instead of casting the value to string, should be encoded * @return string the formatted result. */ public function asCrudLink($value, $options = [], $action = 'view', $label = null) { if ($value === null || is_array($value) && empty($value)) { return $this->nullDisplay; } $values = is_array($value) ? $value : [$value]; $route = false; $result = []; foreach ($values as $value) { if ($label === null) { $labelString = Html::encode((string) $value); } elseif (is_callable($label)) { $labelString = call_user_func($label, $value); } else { $labelString = $label; } if (!$value instanceof ActiveRecord) { $result[] = $labelString; continue; } if ($route === false) { $route = Yii::$app->crudModelsMap[$value::className()]; } if ($route === null || !Yii::$app->user->can($value::className() . '.read', ['model' => $value])) { $result[] = $labelString; continue; } $result[] = Html::a($labelString, [$route . '/' . $action, 'id' => Action::exportKey($value->getPrimaryKey())], $options); } return implode(', ', $result); }
echo netis\crud\web\Alerts::widget(); ?> <?php if ($targetState === null && is_array($states)) { ?> <?php foreach ($states as $state) { if (!$state['enabled']) { continue; } echo Html::a('<i class="fa fa-' . $state['icon'] . '"></i> ' . $state['label'], $state['url'], ['class' => 'btn ' . $state['class'], 'style' => 'margin-left: 2em;']); } ?> <?php } else { ?> <?php if (!isset($buttons)) { $icon = Html::tag('i', '', ['class' => 'fa fa-' . $stateChange['state']->icon]); $buttons = [Html::submitButton($icon . $stateChange['state']->label, ['class' => 'btn ' . $stateChange['state']->css_class]), Html::a(Yii::t('app', 'Back'), Url::toRoute([Yii::$app->request->getQueryParam('return', $controller->action->viewAction), 'id' => \netis\crud\crud\Action::exportKey($model->getPrimaryKey(true))]), ['class' => 'btn btn-default'])]; } echo Html::hiddenInput('target-state', $targetState, ['id' => 'target-state']); echo $this->render('_form', ['model' => $model, 'targetState' => $targetState, 'fields' => $fields, 'relations' => $relations, 'formOptions' => array_merge(['action' => Url::toRoute([$controller->action->id, 'id' => $id, 'targetState' => $targetState, 'confirmed' => 1]), 'enableAjaxValidation' => false], isset($formOptions) ? $formOptions : []), 'buttons' => $buttons], $this->context); ?> <?php }