Пример #1
0
 /**
  * Add new News entry
  * @return array
  */
 public function addAction()
 {
     //Get current user id and data from db
     $user = new Container('user');
     $userId = $user->identity->getId();
     $currentUser = $this->getObjectManager()->getRepository('Application\\Entity\\User')->findOneBy(array('id' => $userId));
     //Prepare news object
     $news = new News();
     //Create form
     $formManager = $this->serviceLocator->get('FormElementManager');
     $form = $formManager->get('newsForm');
     $form->setInputFilter(new Form\NewsFilter($this->getObjectManager()));
     $request = $this->getRequest();
     if ($request->isPost()) {
         $form->setData($request->getPost());
         if ($form->isValid()) {
             //Update news information
             $news->setTitle($request->getPost('title'));
             $news->setText($request->getPost('text'));
             $news->setCreated(new \DateTime("now"));
             $news->setLastModified(new \DateTime("now"));
             $news->setUserId($userId);
             //Track activity
             $activity = new Activity();
             $activity->setUserId($userId);
             $activity->setDescription('Removed News');
             $activity->setUri($request->getUriString());
             $activity->setQuery(var_export($request->getQuery(), true));
             $activity->setPost(var_export($request->getPost(), true));
             $activity->setObject(var_export($currentUser, true));
             $activity->setCreated(new \DateTime("now"));
             $this->getObjectManager()->persist($activity);
             //Store updated news to db
             $this->getObjectManager()->persist($news);
             $this->getObjectManager()->flush();
             //REDIRECT TO NEWS INDEX PAGE WHEN SAVED
             return $this->redirect()->toRoute('news');
         }
     }
     return array('form' => $form);
 }