예제 #1
0
 /**
  * Sets a flash message and configures response headers.
  * @param ActiveRecord $model
  * @param bool $wasNew
  */
 protected function afterSave($model, $wasNew)
 {
     if ($wasNew) {
         $message = Yii::t('app', '<strong>Success!</strong> A new record has been created.');
     } else {
         $message = Yii::t('app', '<strong>Success!</strong> Record has been updated.');
     }
     if (!isset($_POST[self::NEW_RELATED_BUTTON_NAME])) {
         $this->setFlash('success', $message);
     }
     $id = $this->exportKey($model->getPrimaryKey(true));
     $response = Yii::$app->getResponse();
     $response->setStatusCode(201);
     if (!Yii::$app->request->isAjax) {
         // when such header is set, ActiveController will change the response code set above to a redirect
         $response->getHeaders()->set('Location', isset($_POST[self::NEW_RELATED_BUTTON_NAME]) ? Url::current(['id' => $id, self::ADD_RELATED_NAME => $_POST[self::NEW_RELATED_BUTTON_NAME]], true) : Url::toRoute([$this->viewAction, 'id' => $id], true));
     }
     $response->getHeaders()->set('X-Primary-Key', $id);
 }
예제 #2
0
 /**
  * @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;
 }