Пример #1
0
 /**
  * Update personal content items or add new content item
  * @param null|int $id
  * @return string
  * @throws ForbiddenException
  * @throws NotFoundException
  * @throws NativeException
  * @throws \Ffcms\Core\Exception\SyntaxException
  */
 public function actionUpdate($id = null)
 {
     // check if user is auth
     if (!App::$User->isAuth()) {
         throw new ForbiddenException(__('Only authorized users can add content'));
     }
     // check if user add enabled
     $configs = $this->getConfigs();
     if (!(bool) $configs['userAdd']) {
         throw new NotFoundException(__('User add is disabled'));
     }
     // find record in db
     $record = ContentRecord::findOrNew($id);
     $new = $record->id === null;
     // reject edit published items and items from other authors
     if ($new === false && (int) $record->author_id !== App::$User->identity()->getId() || (int) $record->display === 1) {
         throw new ForbiddenException(__('You have no permissions to edit this content'));
     }
     // initialize model
     $model = new FormNarrowContentUpdate($record, $configs);
     if ($model->send() && $model->validate()) {
         $model->make();
         // if is new - make redirect to listing & add notify
         if ($new === true) {
             App::$Session->getFlashBag()->add('success', __('Content successfully added'));
             $this->response->redirect('content/my');
         } else {
             App::$Session->getFlashBag()->add('success', __('Content successfully updated'));
         }
     }
     // render view output
     return $this->view->render('update', ['model' => $model, 'configs' => $configs]);
 }