/** * To update/edit a blog post * * @parmam void * @return ViewModel * @throws AccessProhibitedException **/ public function editAction() { $this->_checkAcl('edit'); $request = $this->getRequest(); $post = $this->_postService->getFromId($request->getQuery('id')); $categoryService = new CategoryService($this->_em); $form = new PostForm(); $form->bind($post); $form->setCategoryList($categoryService->getAll()); $this->_postService->setForm($form); if ($request->isPost()) { $this->_postService->save($request->getPost()); } return new ViewModel(array('form' => $form, 'post' => $post, 'messages' => $this->_postService->getMessages(PostService::MSG_NOTICE), 'errors' => $this->_postService->getMessages(PostService::MSG_ERROR))); }
/** * Edit action * */ public function editAction() { $post = new Post(); if ($this->params('id') > 0) { $post = $this->getEntityManager()->getRepository('Blog\\Entity\\Post')->find($this->params('id')); } $form = new PostForm($this->getEntityManager()); $form->setHydrator(new DoctrineEntity($this->getEntityManager(), 'Blog\\Entity\\Post')); $form->bind($post); $request = $this->getRequest(); if ($request->isPost()) { $form->setInputFilter($post->getInputFilter()); $form->setData($request->getPost()); if ($form->isValid()) { $em = $this->getEntityManager(); $em->persist($post); $em->flush(); $this->flashMessenger()->addSuccessMessage('Post Saved'); $this->flashMessenger()->addMessage('sadf'); $this->flashMessenger()->addErrorMessage('2312'); return $this->redirect()->toRoute('post'); } } return new ViewModel(array('post' => $post, 'form' => $form)); }
public function addAction() { $formPost = new PostForm($this->categoryTable); $request = $this->getRequest(); if ($request->isPost()) { $post = new Post(); $formPost->setInputFilter($post->getInputFilter()); $nonFiles = $this->getRequest()->getPost()->toArray(); $files = $this->getRequest()->getFiles()->toArray(); // Pour ZF 2.2.x uniquement $data = array_merge_recursive($nonFiles, $files); $formPost->setData($data); if ($formPost->isValid()) { $size = new Size(array('max' => 716800)); $adapter = new \Zend\File\Transfer\Adapter\Http(); $adapter->setValidators(array($size), $files['image']); if (!$adapter->isValid()) { $dataError = $adapter->getMessages(); $error = array(); foreach ($dataError as $key => $row) { $error[] = $row; } $formPost->setMessages(array('image' => $error)); } else { $adapter->setDestination('./public/img/'); if ($adapter->receive($files['image']['name'])) { $post->exchangeArray($formPost->getData()); $post->image = $files['image']['name']; $this->postTable->savePost($post); return $this->redirect()->toRoute('home'); } } } } return new ViewModel(array('form' => $formPost)); }
public function editAction() { $id = (int) $this->params('id'); if (!$id) { return $this->redirect()->toRoute('posts', array('action' => 'new')); } try { $post = $this->getPostTable()->getPost($id); } catch (\Exception $ex) { return $this->redirect()->toRoute('posts', array('action' => 'index')); } $table = $this->getServiceLocator()->get('Blog\\Model\\CategoryTable'); $form = new PostForm($table); $form->bind($post); $form->get('submit'); $request = $this->getRequest(); if ($request->isPost()) { $form->setInputFilter($post->getInputFilter()); $form->setData($request->getPost()); if ($form->isValid()) { $this->getPostTable()->savePost($post); return $this->redirect()->toRoute('posts', array('action' => view, 'id' => $id)); } } return array('id' => $id, 'form' => $form, 'post' => $post); }