/**
  * Adds a new News to the db or opens up the form for adding, if it isn't opened yet.
  *
  * @return array|\Zend\Http\Response
  * @throws \Exception
  */
 public function addAction()
 {
     $session = $session = new \Zend\Session\Container('user');
     if (!PermissionChecker::check(Role::ELDER)) {
         return $this->redirect()->toRoute('account', ['action' => 'noright']);
     }
     $form = new NewsForm();
     $form->get('category_id')->setValueOptions($this->createCategorySelect());
     $request = $this->getRequest();
     if ($request->isPost()) {
         $news = new News();
         $form->setInputFilter($news->getInputFilter());
         $form->setData($request->getPost());
         if ($form->isValid()) {
             $news->exchangeArray($form->getData());
             if ($this->checkWordLengths($news->getContent())) {
                 $this->getNewsTable()->saveNews($news);
                 return $this->redirect()->toRoute('news');
             } else {
                 return ['form' => $form, 'accountId' => $session->id, 'error' => 'tooLong'];
             }
         } else {
             return ['form' => $form, 'accountId' => $session->id, 'error' => 'tooLong'];
         }
     }
     return ['form' => $form, 'accountId' => $session->id];
 }
Exemple #2
0
 /**
  * Saves the given news object to the db
  *
  * @param \News\Model\News $news
  *
  * @throws \Exception
  */
 public function saveNews(News $news)
 {
     $data = ['account_id' => $news->getAccountId(), 'title' => $news->getTitle(), 'content' => $news->getContent(), 'category_id' => $news->getCategoryId(), 'date_posted' => $news->getDatePosted()];
     $id = (int) $news->getId();
     if ($id == 0) {
         $this->tableGateway->insert($data);
     } else {
         if ($this->getNews($id)) {
             $this->tableGateway->update($data, ['id' => $id]);
         } else {
             throw new \Exception('News id does not exist');
         }
     }
 }