{"id":64,"date":"2007-08-06T02:36:00","date_gmt":"2007-08-06T02:36:00","guid":{"rendered":"http:\/\/grummfy.be\/blog\/?p=64"},"modified":"2009-12-31T03:16:43","modified_gmt":"2009-12-31T01:16:43","slug":"zend-framework-zf-un-livre-dor-cinquieme-etapes-modification-et-suppression-des-messages","status":"publish","type":"post","link":"https:\/\/grummfy.be\/blog\/64","title":{"rendered":"Zend Framework (ZF) : un livre d&rsquo;or, cinqui\u00e8me \u00e9tapes : Modification et suppression des messages"},"content":{"rendered":"<h2>Cr\u00e9ations d&rsquo;un livre d&rsquo;or en 6 \u00e9tapes<\/h2>\n<ol>\n<li><a href=\"?p=58\">D\u00e9finition<\/a><\/li>\n<li><a href=\"?p=59\">Cr\u00e9ations des tables sql et des r\u00e9pertoires<\/a><\/li>\n<li><a href=\"?p=60\">Bases des fichiers<\/a><\/li>\n<li><a href=\"?p=63\">Affichages et ajout de messages<\/a><\/li>\n<li><a href=\"?p=64\">Modification et suppression des messages<\/a><\/li>\n<li><a href=\"?p=65\">Conclusions<\/a><\/li>\n<\/ol>\n<h3>Modification et suppression des messages<\/h3>\n<p>Modifions notre contr\u00f4leur\u00a0:<\/p>\n<pre> &lt;?php \/** Zend_Controller_Action *\/ require_once 'Zend\/Controller\/Action.php'; class LivreController extends Zend_Controller_Action { \tpublic function indexAction() \t{ \t\t$livre = new livre(); \t\t$this-&gt;view-&gt;livres = $livre-&gt;fetchAll(); \t} \tpublic function ajouterAction() \t{ \t\t$this-&gt;view-&gt;title .= ' .: Ajout d\\'un message'; \t\tif ($this-&gt;getRequest()-&gt;isPost()) \t\t{ \t\t\tZend_Loader::loadClass('Zend_Filter_StripTags'); \t\t\t$filter = new Zend_Filter_StripTags(); \t\t\t$livre = $this-&gt;_getEmptyMsg(); \t\t\t$livre-&gt;id = null; \t\t\t$livre-&gt;nom = trim($filter-&gt;filter($this-&gt;getRequest()-&gt;getPost('nom'))); \t\t\t$livre-&gt;message = trim($filter-&gt;filter($this-&gt;getRequest()-&gt;getPost('message'))); \t\t\tif ($livre-&gt;nom != htmlspecialchars('Pseudo')) \t\t\t{ \t\t\t\t$livre_ = new Livre(); \t\t\t\t$livre_-&gt;insertObject($livre); \t\t\t\t$this-&gt;_redirect('\/livre'); \t\t\t\treturn; \t\t\t} \t\t\telse \t\t\t{ \t\t\t\t$this-&gt;view-&gt;message = 'Merci de donner votre pseudo!'; \t\t\t} \t\t} \t\t\/\/cr\u00e9ation d'un message vide \t\t$this-&gt;view-&gt;livre = $this-&gt;_getEmptyMsg(); \t} \tpublic function modifierAction() \t{ \t\t\/\/si on est aps connecter -&gt;redirection sur le logeur \t\tif (!$_SESSION['connecter']) \t\t\t$this-&gt;getResponse()-&gt;setRedirect($this-&gt;view-&gt;baseUrl . '\/livre\/admin\/'); \t\t$this-&gt;view-&gt;title .= ' .: Modification d\\'un message'; \t\t\/\/r\u00e9cup\u00e9ration du message correspondant au param\u00e8tre id \t\t$livreTable = new Livre(); \t\t$where  = $livreTable-&gt;getAdapter()-&gt;quoteInto('id = ?', intval($this-&gt;getRequest()-&gt;getParam('id'))); \t\t$livre = $livreTable-&gt;fetchRow($where); \t\tif ($this-&gt;getRequest()-&gt;isPost()) \t\t{ \t\t\tZend_Loader::loadClass('Zend_Filter_StripTags'); \t\t\t$filter = new Zend_Filter_StripTags(); \t\t\t$livre-&gt;nom = trim($filter-&gt;filter($this-&gt;getRequest()-&gt;getPost('nom'))); \t\t\t$livre-&gt;message = trim($filter-&gt;filter($this-&gt;getRequest()-&gt;getPost('message'))); \t\t\tif ($livre-&gt;nom != htmlspecialchars('Pseudo')) \t\t\t{ \t\t\t\t$livre_ = new Livre(); \t\t\t\t$livre_-&gt;updateObject($livre, $livre_-&gt;getAdapter()-&gt;quoteInto('id = ?', $livre-&gt;id)); \t\t\t\t$this-&gt;_redirect('\/livre\/'); \t\t\t\treturn; \t\t\t} \t\t} \t\t$this-&gt;view-&gt;livre = $livre; \t\t$this-&gt;view-&gt;action .= '\/id\/' . $livre-&gt;id; \t\t$this-&gt;render('ajouter'); \t} \tpublic function adminAction() \t{ \t\t$this-&gt;view-&gt;title .= ' .: Administration'; \t\tif ($this-&gt;getRequest()-&gt;isPost() &amp;&amp; !$_SESSION['connecter']) \t\t{ \t\t\tif ($this-&gt;getRequest()-&gt;getPost('login') == 'admin' &amp;&amp; $this-&gt;getRequest()-&gt;getPost('pass') == '1234') \t\t\t{ \t\t\t\t\/\/bon password et bon login \t\t\t\t$_SESSION['connecter'] = true; \t\t\t} \t\t} \t\telseif (!$_SESSION['connecter']) \t\t{ \t\t\t$this-&gt;render(); \t\t\treturn; \t\t} \t\t$this-&gt;getResponse()-&gt;setRedirect($this-&gt;view-&gt;baseUrl . '\/livre\/'); \t} \tpublic function disadminAction() \t{ \t\t$this-&gt;view-&gt;title .= ' .: D\u00e9connection'; \t\tif ($_SESSION['connecter']) \t\t{ \t\t\t$_SESSION['connecter'] = false; \t\t} \t\t$this-&gt;getResponse()-&gt;setRedirect($this-&gt;view-&gt;baseUrl . '\/livre\/'); \t} \tpublic function supprimerAction() \t{ \t\t\/\/si on est aps connecter -&gt;redirection sur le logeur \t\tif (!$_SESSION['connecter']) \t\t\t$this-&gt;getResponse()-&gt;setRedirect($this-&gt;view-&gt;baseUrl . '\/livre\/admin\/'); \t\t$this-&gt;view-&gt;title .= ' .: Suppretion d\\'un message'; \t\t$livre = $this-&gt;_getEmptyMsg(); \t\t$livre-&gt;id = intval($this-&gt;getRequest()-&gt;getParam('id')); \t\t$this-&gt;view-&gt;action .= '\/id\/' . $livre-&gt;id; \t\tif ($this-&gt;getRequest()-&gt;isPost()) \t\t{ \t\t\t$livre_ = new Livre(); \t\t\tif ($livre_-&gt;delete($livre_-&gt;getAdapter()-&gt;quoteInto('id = ?', $livre-&gt;id))) \t\t\t\t$this-&gt;view-&gt;message = 'Message supprim\u00e9!'; \t\t\telse \t\t\t\t$this-&gt;view-&gt;message = 'Message &lt;b&gt;non&lt;\/b&gt; supprim\u00e9!'; \t\t\t$this-&gt;view-&gt;message .= '&lt;br \/&gt; Vous aller \u00eatre rediriger!&lt;br \/&gt;'; \t\t\t$this-&gt;getResponse()-&gt;setRawHeader('Refresh:3; url=' . $this-&gt;view-&gt;baseUrl . '\/livre\/'); \t\t} \t} \tfunction init() \t{ \t\tsession_name('goldbook'); \t\tsession_start(); \t\t$_SESSION['connecter'] = isset($_SESSION['connecter'])?$_SESSION['connecter']:false; \t\t$this-&gt;view-&gt;connecter = $_SESSION['connecter']; \t\t$this-&gt;view-&gt;title = 'Livre d\\'or'; \t\t\/\/url de base... \t\t$this-&gt;view-&gt;baseUrl = $this-&gt;_request-&gt;getBaseUrl(); \t\t$this-&gt;view-&gt;action = $this-&gt;getRequest()-&gt;getActionName(); \t\t\/\/pr\u00eat \u00e0 travailler sur les livres \t\tZend_Loader::loadClass('Livre'); \t\t\/\/ajout des aides de view \t\t$this-&gt;view-&gt;addHelperPath('.\/app\/views\/helpers', 'MyWsp_View_Helper'); \t\t$this-&gt;view-&gt;setEscape('utf8_encode'); \t} \t\/** \t * Cr\u00e9er un message vide \t * \t * @return Object \t *\/ \tprotected function _getEmptyMsg() \t{ \t\t\/\/cr\u00e9ation d'un message vide \t\t$livre = new stdClass(); \t\t$livre\t-&gt;id = -99; \t\t$livre\t-&gt;nom = 'Pseudo'; \t\t$livre\t-&gt;message = 'Entrez votre message'; \t\t$livre\t-&gt;date = time(); \t\treturn $livre; \t} }<\/pre>\n<p>Il nous faut cr\u00e9er un syst\u00e8me de session, et une authentification pour l&rsquo;administration. Ici les choses sont tr\u00e8s basiques, car le but n&rsquo;y est pas! Donc on utilise els session php et une authentification des plus basique (login: <q>admin<\/q>, password: <q>1234<\/q>)!<\/p>\n<p>Il nous faut encore modifier index.phtml<\/p>\n<pre> &lt;?php echo $this-&gt;render('header.phtml'); ?&gt; &lt;?php if ($this-&gt;connecter) echo '&lt;a href=\"' , $this-&gt;baseUrl , '\/livre\/disadmin\"&gt;Se d\u00e9connecter&lt;\/a&gt;'; ?&gt; \t&lt;div id=\"livre\"&gt; \t\t&lt;p&gt;&lt;a href=\"&lt;?php echo $this-&gt;baseUrl; ?&gt;\/livre\/ajouter\"&gt;Ajouter un message&lt;\/a&gt;&lt;\/p&gt; \t\t&lt;div id=\"livre_message\"&gt; \t\t&lt;?php foreach($this-&gt;livres as $livre) : ?&gt; \t\t\t&lt;div class=\"livre_block_message\"&gt; \t\t\t\t&lt;span class=\"livre_pseudo\"&gt;&lt;?php \t\t\t\t\techo $this-&gt;escape($livre-&gt;nom); \t\t\t\t?&gt;&lt;\/span&gt; | \t\t\t\t&lt;span class=\"livre_date\"&gt;Le &lt;?php echo $this-&gt;date('DDDD DD MMMM YYYY', $this-&gt;escape($livre-&gt;date)); ?&gt;&lt;\/span&gt; \t\t\t\t&lt;br \/&gt; \t\t\t\t&lt;span class=\"livre_message\"&gt;&lt;?php echo nl2br($this-&gt;escape($livre-&gt;message)); ?&gt;&lt;\/span&gt; \t\t\t\t&lt;?php \t\t\t\tif ($this-&gt;connecter) \t\t\t\t\techo '&lt;br \/&gt;&lt;a href=\"' , $this-&gt;baseUrl , '\/livre\/modifier\/id\/' , $livre-&gt;id , '\"&gt;Modifier&lt;\/a&gt; \t\t\t\t\t&lt;a href=\"' , $this-&gt;baseUrl , '\/livre\/supprimer\/id\/' , $livre-&gt;id , '\"&gt;Supprimer&lt;\/a&gt;'; \t\t\t\t?&gt; \t\t\t&lt;\/div&gt; \t\t&lt;?php endforeach; ?&gt; \t\t&lt;\/div&gt; \t&lt;\/div&gt; &lt;?php echo $this-&gt;render('footer.phtml'); ?&gt;<\/pre>\n<p>N&rsquo;oublions pas d&rsquo;ajouter les fichier admin.phtml et supprimer.phtml<\/p>\n<p><strong>suprimer.html<\/strong><\/p>\n<pre> &lt;?php echo $this-&gt;render('header.phtml'); ?&gt; \t&lt;div id=\"livre\"&gt; \t\t&lt;strong&gt;&lt;?php echo $this-&gt;message; ?&gt;&lt;\/strong&gt; \t\t&lt;form method=\"post\" action=\"&lt;?php echo $this-&gt;baseUrl ?&gt;\/livre\/&lt;?php echo $this-&gt;action ?&gt;\" name=\"form_nom\"&gt; \t\t\t&lt;div id=\"livre_haut\"&gt; \t\t\t\t&lt;button type=\"submit\" name=\"del\"&gt;Supprimer?&lt;\/button&gt; \t\t\t&lt;\/div&gt; \t\t&lt;\/form&gt; \t\t&lt;br \/&gt; \t\t&lt;br \/&gt; \t&lt;\/div&gt; &lt;?php echo $this-&gt;render('footer.phtml'); ?&gt;<\/pre>\n<p><strong>admin.phtml<\/strong><\/p>\n<pre> &lt;?php echo $this-&gt;render('header.phtml'); ?&gt; \t&lt;div id=\"livre\"&gt; \t\t&lt;form method=\"post\" action=\"&lt;?php echo $this-&gt;baseUrl ?&gt;\/livre\/admin\" name=\"form_nom\"&gt; \t\t\t&lt;div id=\"livre_haut\"&gt; \t\t\t\tLogin : &lt;input type=\"text\" name=\"login\"&gt; \t\t\t\t&lt;br \/&gt; \t\t\t\tPassword : &lt;input type=\"password\" name=\"pass\"&gt; \t\t\t\t&lt;br \/&gt; \t\t\t\t&lt;button type=\"submit\" name=\"admin\"&gt;Poster&lt;\/button&gt; \t\t\t&lt;\/div&gt; \t\t&lt;\/form&gt; \t\t&lt;br \/&gt; \t\t&lt;br \/&gt; \t&lt;\/div&gt; &lt;?php echo $this-&gt;render('footer.phtml'); ?&gt;<\/pre>\n<p>Comme on peux le remarquer pas besoin d&rsquo;un fichier modifier.phtml car nous r\u00e9utilisons ajouter.phtml via <q>$this-&gt;render(&lsquo;ajouter&rsquo;);<\/q>!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Cr\u00e9ations d&rsquo;un livre d&rsquo;or en 6 \u00e9tapes D\u00e9finition Cr\u00e9ations des tables sql et des r\u00e9pertoires Bases des fichiers Affichages et ajout de messages Modification et suppression des messages Conclusions Modification et suppression des messages Modifions notre contr\u00f4leur\u00a0: &lt;?php \/** Zend_Controller_Action *\/ require_once &lsquo;Zend\/Controller\/Action.php&rsquo;; class LivreController extends Zend_Controller_Action { public function indexAction() { $livre = new [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"webmentions_disabled_pings":false,"webmentions_disabled":false,"footnotes":""},"categories":[9],"tags":[22,110,24,42,65,37,64],"class_list":["post-64","post","type-post","status-publish","format-standard","hentry","category-dev","tag-jouons","tag-php","tag-programmation","tag-script","tag-tutoriel","tag-web","tag-zf"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/grummfy.be\/blog\/wp-json\/wp\/v2\/posts\/64","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/grummfy.be\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/grummfy.be\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/grummfy.be\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/grummfy.be\/blog\/wp-json\/wp\/v2\/comments?post=64"}],"version-history":[{"count":1,"href":"https:\/\/grummfy.be\/blog\/wp-json\/wp\/v2\/posts\/64\/revisions"}],"predecessor-version":[{"id":208,"href":"https:\/\/grummfy.be\/blog\/wp-json\/wp\/v2\/posts\/64\/revisions\/208"}],"wp:attachment":[{"href":"https:\/\/grummfy.be\/blog\/wp-json\/wp\/v2\/media?parent=64"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/grummfy.be\/blog\/wp-json\/wp\/v2\/categories?post=64"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/grummfy.be\/blog\/wp-json\/wp\/v2\/tags?post=64"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}