public function executeSave(HTTPRequest $request) { $response = false; $manager = $this->getManagerOf($this->module(), 'Pages'); if (!$request->postExists('id')) { $userManager = $this->getManagerOf('users', 'Users'); $page = new Page(); $page->setAutor($userManager->getUnique($this->user()->id())); $page->setTitle($request->postData('title')); $page->setTemplate(trim($request->postData('template'))); $response = $manager->save($page); } else { $page = $manager->getUnique($request->postData('id')); $page->setTitle($request->postData('title')); $page->setSlug($this->to_permalink($request->postData('title'))); $page->setContent(trim($request->postData('content'))); if ($request->postExists('template')) { $page->setTemplate(trim($request->postData('template'))); } else { $page->setTemplate($page->template()); } $manager->save($page); $response = true; } echo json_encode($response); exit; }
/** * Méthode permettant d'enregistrer une page. * @param $page Page la page à enregistrer * @see self::add() * @see self::modify() * @return void */ public function save(Page $page) { if ($page->isValid()) { if ($page->isNew()) { return $this->add($page); } $this->modify($page); } else { throw new \RuntimeException('La page doit être validée pour être enregistrée'); } }
protected function modify(Page $page) { $requete = $this->dao->prepare('UPDATE pages SET autor = :autor, title = :title, slug = :slug, content = :content, template = :template, lastUpdateDate = NOW() WHERE id = :id'); $requete->bindValue(':title', ucfirst($page->title()), \PDO::PARAM_STR); $requete->bindValue(':autor', $page->autor()->id(), \PDO::PARAM_INT); $requete->bindValue(':slug', strtolower(trim(preg_replace('~[^0-9a-z]+~i', '-', html_entity_decode(preg_replace('~&([a-z]{1,2})(?:acute|cedil|circ|grave|lig|orn|ring|slash|copy|th|tilde|uml);~i', '$1', htmlentities($page->title(), ENT_QUOTES, 'UTF-8')), ENT_QUOTES, 'UTF-8')), '-')), \PDO::PARAM_STR); $requete->bindValue(':content', $page->content(), \PDO::PARAM_STR); $requete->bindValue(':template', $page->template(), \PDO::PARAM_STR); $requete->bindValue(':id', $page->id(), \PDO::PARAM_INT); $requete->execute(); }