public function editNewsAction()
 {
     $this->layout('layout/home');
     $catalog = $this->forward()->dispatch('Catalog\\Controller\\Index', array('action' => 'getdata'));
     $this->layout()->catalog = $catalog;
     $allcat = $this->forward()->dispatch('Catalog\\Controller\\Index', array('action' => 'getall'));
     $this->layout()->allcat = $allcat;
     $getuser = $this->forward()->dispatch('Admin\\Controller\\Index', array('action' => 'getuser'));
     if (!$getuser) {
         // not yet login
         $this->redirect()->toRoute('home');
     }
     $this->layout()->getuser = $getuser;
     $news_id = $this->params()->fromRoute('id', 0);
     if ($news_id == 0) {
         return $this->redirect()->toRoute('News', array('controller' => 'news', 'action' => 'list-news'));
     } else {
         $newsTable = $this->getServiceLocator()->get('NewsTable');
         $form = new NewsForm();
         $form->setInputFilter(new NewsFilter());
         $form->get('news_thumbnail')->removeAttributes(array('required'));
         $filter = $form->getInputFilter();
         $filter->get('news_thumbnail')->setRequired(false);
         $newsDetail = $newsTable->getNewsById($news_id);
         $this->_fileName = $newsDetail->news_thumbnail;
         $form->bind($newsDetail);
         if ($this->getRequest()->isPost()) {
             $data = array_merge_recursive($this->getRequest()->getPost()->toArray(), $this->getRequest()->getFiles()->toArray());
             $form->setData($data);
             if (!$form->isValid()) {
                 return new ViewModel(array('error' => true, 'form' => $form, 'image' => $this->_fileName, 'news_id' => $news_id));
             } else {
                 $exchange_data = array();
                 $exchange_data['news_id'] = $news_id;
                 if ($data['news_thumbnail']['name'] == "") {
                     $exchange_data['news_thumbnail'] = '';
                 } else {
                     $fileName = $this->upload();
                     $exchange_data['news_thumbnail'] = $fileName;
                 }
                 $exchange_data['news_name'] = $data['news_name'];
                 $exchange_data['news_content'] = $data['news_content'];
                 $newsModel = new News();
                 $newsModel->exchangeArray($exchange_data);
                 $newsTable->saveNews($newsModel);
                 return $this->redirect()->toRoute('News', array('controller' => 'news', 'action' => 'list-news'));
             }
         }
         return new ViewModel(array('form' => $form, 'image' => $this->_fileName, 'news_id' => $news_id));
     }
 }
示例#2
0
 /**
  * Edits a already existing news and updates it-
  *
  * @return array|\Zend\Http\Response
  * @throws \Exception
  */
 public function editAction()
 {
     $id = (int) $this->params()->fromRoute('id', 0);
     if (!$id) {
         return $this->redirect()->toRoute('news', ['action' => 'add']);
     }
     try {
         $news = $this->getNewsTable()->getNews($id);
     } catch (\Exception $ex) {
         return $this->redirect()->toRoute('news');
     }
     $session = new \Zend\Session\Container('user');
     if (!isset($session->id) || $session->id != $news->getAccountId()) {
         return $this->redirect()->toRoute('account', ['action' => 'noright']);
     }
     $form = new NewsForm();
     $form->get('category_id')->setValueOptions($this->createCategorySelect());
     $form->bind($news);
     $form->get('category_id')->setValue($news->getCategoryId());
     $form->get('submit')->setAttribute('value', 'Edit');
     $request = $this->getRequest();
     if ($request->isPost()) {
         $form->setInputFilter($news->getInputFilter());
         $form->setData($request->getPost());
         if ($form->isValid()) {
             if ($this->checkWordLengths($news->getContent())) {
                 $this->getNewsTable()->saveNews($news);
                 return $this->redirect()->toRoute('news');
             } else {
                 return ['id' => $id, 'form' => $form, 'accountId' => $session->id, 'error' => 'tooLong'];
             }
         } else {
             return ['id' => $id, 'form' => $form, 'accountId' => $session->id, 'error' => 'tooLong'];
         }
     }
     return ['id' => $id, 'form' => $form, 'accountId' => $session->id];
 }