Beispiel #1
0
 /**
  * Builds an array containing all possible status changes and result of validating every transition.
  * @param \netis\crud\db\ActiveRecord|IStateful $model
  * @return array
  */
 public function prepareStates($model)
 {
     $checkedAccess = [];
     $result = [];
     $attribute = $model->stateAttributeName;
     $sourceState = $model->getAttribute($attribute);
     foreach ($model->getTransitionsGroupedByTarget() as $targetState => $target) {
         $state = $target['state'];
         $sources = $target['sources'];
         if (!isset($sources[$sourceState])) {
             continue;
         }
         $enabled = null;
         $sourceStateObject = $sources[$sourceState];
         $authItem = $sourceStateObject->auth_item_name;
         if (trim($authItem) === '') {
             $checkedAccess[$authItem] = true;
         } elseif (!isset($checkedAccess[$authItem])) {
             $checkedAccess[$authItem] = Yii::$app->user->can($authItem, ['model' => $model]);
         }
         $enabled = ($enabled === null || $enabled) && $checkedAccess[$authItem];
         $valid = !$enabled || $model->isTransitionAllowed($targetState);
         $entry = ['post' => $state->post_label, 'label' => $sources[$sourceState]->label, 'icon' => $state->icon, 'class' => $state->css_class, 'target' => $targetState, 'enabled' => $enabled && $valid, 'valid' => $valid, 'url' => Url::toRoute([$this->id, $this->getUrlParams($state, $model, $targetState, $this->id)])];
         if ($state->display_order) {
             $result[$state->display_order] = $entry;
         } else {
             $result[] = $entry;
         }
     }
     ksort($result);
     return $result;
 }
 /**
  * @param \yii\base\Action $action
  * @param ActiveRecord $model
  * @param bool $horizontal
  * @param array $privs
  * @param array $defaultActions
  * @param array $confirms
  * @return array
  */
 public function getMenuCurrent(\yii\base\Action $action, $model, $horizontal, $privs, $defaultActions, $confirms)
 {
     $menu = [];
     $id = null;
     if (!$model->isNewRecord) {
         $id = $action instanceof Action ? $action->exportKey($model->getPrimaryKey(true)) : implode(';', $model->getPrimaryKey(true));
     }
     if ($privs['read'] && $defaultActions['history'] && (!$model->isNewRecord || $action->id === 'update') && $model->getBehavior('trackable') !== null) {
         $menu['history'] = ['label' => Yii::t('app', 'History of changes'), 'icon' => 'list-alt', 'url' => array_merge(['history', 'id' => $id], $this->getUrlParams('history', $id))];
     }
     if (!$horizontal && $model->isNewRecord) {
         return $menu;
     }
     if ($privs['update'] && ($horizontal || $action->id !== 'update')) {
         $menu['update'] = ['label' => Yii::t('app', 'Update'), 'icon' => 'pencil', 'url' => array_merge(['update', 'id' => $id], $this->getUrlParams('update', $id)), 'active' => $action->id === 'update' && !$model->isNewRecord];
     }
     if ($privs['read'] && $defaultActions['view'] && ($horizontal || $action->id !== 'view')) {
         $menu['view'] = ['label' => Yii::t('app', 'View'), 'icon' => 'eye-open', 'url' => array_merge(['view', 'id' => $id], $this->getUrlParams('view', $id)), 'linkOptions' => $action->id === 'view' ? [] : ['data-confirm' => $confirms['leave']], 'active' => $action->id === 'view'];
     }
     if ($privs['read'] && $defaultActions['print'] && ($horizontal || $action->id !== 'print')) {
         $menu['print'] = ['label' => Yii::t('app', 'Print'), 'icon' => 'print', 'url' => array_merge(['print', 'id' => $id, '_format' => 'pdf'], $this->getUrlParams('print', $id)), 'linkOptions' => $action->id === 'print' ? [] : ['data-confirm' => $confirms['leave']], 'active' => $action->id === 'print'];
     }
     if ($privs['delete']) {
         $menu['delete'] = ['label' => Yii::t('app', 'Delete'), 'icon' => 'trash', 'url' => array_merge(['delete', 'id' => $id], $this->getUrlParams('delete', $id)), 'linkOptions' => ['data-confirm' => $confirms['delete'], 'data-method' => 'post']];
     }
     if ($privs['toggle']) {
         $enabled = !$model->isNewRecord && $model->getIsEnabled();
         $menu['toggle'] = ['label' => $enabled ? Yii::t('app', 'Disable') : Yii::t('app', 'Enable'), 'icon' => $enabled ? 'ban' : 'reply', 'url' => array_merge(['toggle', 'id' => $id], $this->getUrlParams('toggle', $id)), 'linkOptions' => $enabled ? ['data-confirm' => $confirms['disable']] : []];
     }
     if (!$model->isNewRecord && class_exists('netis\\fsm\\components\\StateAction') && $model instanceof \netis\fsm\components\IStateful && $privs['state']) {
         $controllerActions = $this->owner->actions();
         $action = null;
         if (isset($controllerActions['state'])) {
             $action = Yii::createObject($controllerActions['state'], ['state', $this->owner]);
         }
         if ($action === null || !$action instanceof StateActionInterface) {
             $action = Yii::createObject(\netis\fsm\components\StateAction::className(), ['state', $this->owner]);
         }
         $transitions = $model->getTransitionsGroupedByTarget();
         $stateAttribute = $model->getStateAttributeName();
         $stateMenu = $action->getContextMenuItem('state', $transitions, $model, $model->{$stateAttribute}, [$this->owner, 'hasAccess'], $this->useDropDownMenuForTransitions);
         //if label is set then it's drop down menu
         //todo set active item for buttons
         if (isset($stateMenu['label']) && $action->id === 'state') {
             $stateMenu['active'] = true;
         }
         $menu = array_merge($menu, $stateMenu);
     }
     foreach ($menu as $key => $item) {
         if ($model->isNewRecord) {
             $menu[$key]['disabled'] = true;
         }
     }
     return $menu;
 }