public function executeSave(HTTPRequest $request) { $response = false; $manager = $this->getManagerOf($this->module(), 'News'); if (empty($request->postData('id'))) { $userManager = $this->getManagerOf('users', 'Users'); $news = new News(); $news->setAutor($this->user()->nickName()); if ($request->postExists('title')) { $news->setTitle($request->postData('title')); } if ($request->postExists('content')) { $news->setContent($request->postData('content')); } $response = $manager->save($news); } else { $news = $manager->getUnique($request->postData('id')); $news->setTitle($request->postData('title')); $news->setContent(trim($request->postData('content'))); $manager->save($news); $response = true; } echo json_encode($response); exit; }
protected function modify(News $news) { $requete = $this->dao->prepare('UPDATE news SET autor = :autor, title = :title, content = :content, lastUpdateDate = NOW() WHERE id = :id'); $requete->bindValue(':title', $news->title(), \PDO::PARAM_STR); $requete->bindValue(':autor', $news->autor(), \PDO::PARAM_STR); $requete->bindValue(':content', $news->content(), \PDO::PARAM_STR); $requete->bindValue(':id', $news->id(), \PDO::PARAM_INT); $requete->execute(); }
/** * Méthode permettant d'enregistrer une news. * @param $news News la news à enregistrer * @see self::add() * @see self::modify() * @return void */ public function save(News $news) { if ($news->isValid()) { if ($news->isNew()) { return $this->add($news); } $this->modify($news); } else { throw new \RuntimeException('La news doit être validée pour être enregistrée'); } }
public function processForm(HTTPRequest $request) { if ($request->method() == 'POST') { $news = new News(['auteur' => $request->postData('auteur'), 'titre' => $request->postData('titre'), 'contenu' => $request->postData('contenu')]); if ($request->getExists('id')) { $news->setId($request->getData('id')); } } else { // L'identifiant de la news est transmis si on veut la modifier if ($request->getExists('id')) { $news = $this->getManagerOf($this->module(), 'News')->getUnique($request->getData('id')); } else { $news = new News(); } } $formBuilder = new NewsFormBuilder($news); $formBuilder->build(); $form = $formBuilder->form(); $formHandler = new FormHandler($form, $this->getManagerOf($this->module(), 'News'), $request); if ($formHandler->process()) { $this->app->session()->setFlash($news->isNew() ? 'La news a bien été ajoutée !' : 'La news a bien été modifiée !'); $this->app->httpResponse()->redirect('/admin/'); } $this->page->addVar('form', $form->createView()); }