예제 #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;
 }