コード例 #1
0
 public function postAction()
 {
     $user_id = $this->zfcUserAuthentication()->getIdentity()->getId();
     $form = new NewsForm();
     $request = $this->getRequest();
     if ($request->isPost()) {
         $imagine = $this->getServiceLocator()->get('image_service');
         $data = array_merge_recursive($this->getRequest()->getPost()->toArray(), $this->getRequest()->getFiles()->toArray());
         $news = new News();
         $form->setInputFilter($news->getInputFilter());
         $form->setData($data);
         if ($form->isValid()) {
             $file_name = rand(999999, 9999999) . '.jpg';
             foreach ($this->image_sizes as $sizes) {
                 $mode = $sizes[2];
                 $folder = $this->image_folder . '/' . $sizes[0] . 'x' . $sizes[1];
                 @mkdir($this->image_folder . '/' . $sizes[0] . 'x' . $sizes[1], 0777, true);
                 $size = new Box($sizes[0], $sizes[1]);
                 $imagine->open($data['photo']['tmp_name'])->thumbnail($size, $mode)->save($folder . '/' . $file_name);
             }
             $news->exchangeArray($form->getData());
             $news->user_id = $user_id;
             $news->photo = $file_name;
             $this->getNewsTable()->saveNews($news);
             return $this->redirect()->toRoute('account');
         }
     }
     return array('form' => $form, 'user_id' => (int) $user_id);
 }
コード例 #2
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));
     }
 }
コード例 #3
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];
 }