コード例 #1
0
 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));
     }
 }