<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="http://feeds.feedburner.com/~d/styles/rss2full.xsl" type="text/xsl" media="screen"?><?xml-stylesheet href="http://feeds.feedburner.com/~d/styles/itemcontent.css" type="text/css" media="screen"?><rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">

<channel>
	<title>Formation CakePHP</title>
	
	<link>http://www.formation-cakephp.com</link>
	<description>Le cadre de développement PHP et son utilisation au jour le jour.</description>
	<pubDate>Fri, 24 Oct 2008 06:39:27 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6</generator>
	<language>fr</language>
			<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" href="http://feeds.feedburner.com/FormationCakephp" type="application/rss+xml" /><item>
		<title>Optimiser les URL pour le référencement</title>
		<link>http://www.formation-cakephp.com/127/optimiser-suffixes-url-rewriting-referencement</link>
		<comments>http://www.formation-cakephp.com/127/optimiser-suffixes-url-rewriting-referencement#comments</comments>
		<pubDate>Wed, 22 Oct 2008 19:34:22 +0000</pubDate>
		<dc:creator>Pierre-Emmanuel Fringant</dc:creator>
		
		<category><![CDATA[Interaction]]></category>

		<category><![CDATA[Optimisation]]></category>

		<category><![CDATA[Référencement]]></category>

		<category><![CDATA[Routes]]></category>

		<category><![CDATA[URL Rewriting]]></category>

		<guid isPermaLink="false">http://www.formation-cakephp.com/?p=127</guid>
		<description><![CDATA[Nous allons voir comment générer facilement des suffixes d&#8217;URL optimisés pour le référencement. Prenons l&#8217;exemple d&#8217;un catalogue de produits. Par défaut, une URL que CakePHP va produire pour une fiche produit va ressembler à ceci : /produits/view/12 (le nom du Contrôleur, le nom de l&#8217;action et l&#8217;id du produit à afficher). Nous aimerions qu&#8217;elle soit [...]]]></description>
			<content:encoded><![CDATA[<p>Nous allons voir comment générer facilement des suffixes d&#8217;URL optimisés pour le référencement. Prenons l&#8217;exemple d&#8217;un catalogue de produits. Par défaut, une URL que CakePHP va produire pour une fiche produit va ressembler à ceci : <code>/produits/view/12</code> (le nom du Contrôleur, le nom de l&#8217;action et l&#8217;id du produit à afficher). Nous aimerions qu&#8217;elle soit plutôt de la forme <code>/article/12/Livre-de-recettes</code>.<span id="more-127"></span></p>
<h2>1. Génération du suffixe dans les Vues</h2>
<p>Voyons d&#8217;abord comment définir les liens vers nos fiches produits, par exemple dans la liste des produits, soit l&#8217;action index du Contrôleur Produits.<br />
Nous allons utiliser la méthode statique <code>Inflector::slug()</code> qui prend en paramètre :</p>
<ul>
<li>une chaîne de caractères et la retourne exempte de tout caractère &#8220;interdit&#8221; dans une URL (accents, espaces, ponctuation) ;</li>
<li>éventuellement un caractère séparateur, l&#8217;underscore _ par défaut.</li>
</ul>

<div class="wp_syntax"><div class="code"><pre class="php"><span style="color: #666666; font-style: italic;">// {app}/views/produits/index.ctp</span>
<span style="color: #339933;">&lt;</span>ul<span style="color: #339933;">&gt;</span>
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000033;">$produits</span> <span style="color: #b1b100;">as</span> <span style="color: #000033;">$produit</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
	<span style="color: #339933;">&lt;</span>li<span style="color: #339933;">&gt;</span>
	<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #990000;">echo</span> <span style="color: #000033;">$html</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">link</span><span style="color: #009900;">&#40;</span>
		<span style="color: #000033;">$produit</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Produit'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'nom'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
		<span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
			<span style="color: #0000ff;">'controller'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'produits'</span><span style="color: #339933;">,</span>
			<span style="color: #0000ff;">'action'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'view'</span><span style="color: #339933;">,</span>
			<span style="color: #0000ff;">'id'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000033;">$produit</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Produit'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
			<span style="color: #0000ff;">'suffixe'</span> <span style="color: #339933;">=&gt;</span> Inflector<span style="color: #339933;">::</span><span style="color: #004000;">slug</span><span style="color: #009900;">&#40;</span><span style="color: #000033;">$produit</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Produit'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'nom'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'-'</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
	<span style="color: #339933;">&lt;/</span>li<span style="color: #339933;">&gt;</span>
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">endforeach</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
<span style="color: #339933;">&lt;/</span>ul<span style="color: #339933;">&gt;</span></pre></div></div>

<p>Nous obtenons une URL du style : <code>/produits/view/12/suffixe:Livre-de-recettes</code>. Nous progressons, mais nous voulons changer le mot &#8220;produits&#8221; en &#8220;article&#8221;, supprimer le nom de l&#8217;action, qui n&#8217;apporte rien, et le mot &#8220;suffixe:&#8221; qui n&#8217;est pas des plus esthétiques. Pour y parvenir, nous allons tirer parti de la puissance des Routes.</p>
<h2>2. Une route personnalisée</h2>
<p>Rendons-nous dans le Router :</p>

<div class="wp_syntax"><div class="code"><pre class="php"><span style="color: #666666; font-style: italic;">// {app}/config/routes.php</span>
Router<span style="color: #339933;">::</span><span style="color: #004000;">connect</span><span style="color: #009900;">&#40;</span>
	<span style="color: #0000ff;">'/article/:id/:suffixe'</span><span style="color: #339933;">,</span>
	<span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'controller'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'produits'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'action'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'view'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
	<span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'id'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000033;">$ID</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'suffixe'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'[a-zA-Z0-9_-]+'</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Voyons un peu les trois arguments que nous donnons à la méthode <code>Router::connect()</code> :</p>
<ul>
<li><code>'/article/:id/:suffixe'</code> : c&#8217;est le format d&#8217;URL que nous attendons ;</li>
<li><code>array('controller' => 'produits', 'action' => 'view')</code> : lorsque le Router va reconnaître une URL ressemblant à notre premier argument, il devra renvoyer sur l&#8217;action view du Contrôleur Produits</li>
<li><code>array('id' => $ID, 'suffixe' => '[a-zA-Z0-9_-]+&#8217;)</code> : nous définissons ce que nous entendions dans le premier argument par &#8220;<code>:id</code>&#8221; et &#8220;<code>:suffixe</code>&#8221; :
<ul>
<li><code>$ID</code> contient une expression régulière prédéfinie dans le cœur de Cake, correspondant à un ou plusieurs chiffres ;</li>
<li><code>'[a-zA-Z0-9_-]+&#8217;</code> est l&#8217;expression régulière que nous définissons pour que le Router reconnaissent nos suffixes d&#8217;URL : ce suffixe peut contenir des lettres minuscules ou majuscules, des chiffres, des underscores ou des tirets.</li>
</ul>
</li>
</ul>
<p>Rafraîchissons notre page <code>/produits/index</code>, et nous constatons que nos URL sont bien de la forme <code>/article/12/Livre-de-recettes</code> !</p>
<h2>3. Contrôler la validité d&#8217;un suffixe d&#8217;URL</h2>
<p>Mais que se passe-t-il si un visiteur poste un lien vers notre site dans un forum, et qu&#8217;il tronque le suffixe, ou le modifie ? Cela va pénaliser le référencement de la page ciblée, car les moteurs de recherche vont enregistrer plusieurs URL pointant vers la même page : la bonne (<code>/article/12/Livre-de-recettes</code>) et les éventuelles mauvaises (<code>/article/12/Suffixe_incorrect</code>, <code>/article/12</code>, etc.).<br />
Pour pallier à cette éventualité, nous devons nous assurer que le suffixe de l&#8217;URL qui arrive est bien celui attendu. Nous allons effectuer un petit contrôle dans l&#8217;action view du Contrôleur Produits :</p>

<div class="wp_syntax"><div class="code"><pre class="php"><span style="color: #666666; font-style: italic;">// {app}/controllers/produits_controller.php</span>
<span style="color: #000000; font-weight: bold;">function</span> view<span style="color: #009900;">&#40;</span><span style="color: #000033;">$id</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">null</span><span style="color: #339933;">,</span> <span style="color: #000033;">$suffixe</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #000033;">$produit</span> <span style="color: #339933;">=</span> <span style="color: #000033;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Produit</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">read</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">null</span><span style="color: #339933;">,</span> <span style="color: #000033;">$id</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// Produit inexistant : erreur 404</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000033;">$produit</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000033;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">cakeError</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'error404'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'url'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000033;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">action</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// Suffixe incorrect : redirection 301 vers la bonne URL</span>
	<span style="color: #000033;">$suffixe_attendu</span> <span style="color: #339933;">=</span> Inflector<span style="color: #339933;">::</span><span style="color: #004000;">slug</span><span style="color: #009900;">&#40;</span><span style="color: #000033;">$produit</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Produit'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'nom'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'-'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000033;">$suffixe</span> <span style="color: #339933;">!=</span> <span style="color: #000033;">$suffixe_attendu</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000033;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">redirect</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'id'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000033;">$id</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'suffixe'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000033;">$suffixe_attendu</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">301</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000033;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">compact</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'produit'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Ainsi nous nous assurons que toute URL mal formée sera redirigée vers la bonne URL avec un code HTTP 301 : ce code va forcer les moteurs de recherche qui suivront les liens, éventuellement mal formés, vers cette page, à ne prendre en considération que la bonne URL.</p>
<img src="http://feeds.feedburner.com/~r/FormationCakephp/~4/428975186" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.formation-cakephp.com/127/optimiser-suffixes-url-rewriting-referencement/feed</wfw:commentRss>
		<feedburner:awareness xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0">http://api.feedburner.com/awareness/1.0/GetItemData?uri=FormationCakephp&amp;itemurl=http%3A%2F%2Fwww.formation-cakephp.com%2F127%2Foptimiser-suffixes-url-rewriting-referencement</feedburner:awareness></item>
		<item>
		<title>Offre d’emploi/stage</title>
		<link>http://www.formation-cakephp.com/123/offre-demploistage</link>
		<comments>http://www.formation-cakephp.com/123/offre-demploistage#comments</comments>
		<pubDate>Fri, 03 Oct 2008 14:58:22 +0000</pubDate>
		<dc:creator>Pierre-Emmanuel Fringant</dc:creator>
		
		<category><![CDATA[Emploi]]></category>

		<guid isPermaLink="false">http://www.formation-cakephp.com/?p=123</guid>
		<description><![CDATA[Petit apparté sur notre site avec un message de notre partenaire, l&#8217;agence MRJ à Paris.
[CDI / STAGE] Offre d&#8217;emploi, développeur PHP (H/F) à Paris
L&#8217;entreprise :
Filiale informatique d’un grand groupe de presse français créé il y a plus de 15 ans, l’Agence MRJ est une PME innovante reconnue par ses clients pour ses compétences et son [...]]]></description>
			<content:encoded><![CDATA[<p>Petit apparté sur notre site avec un message de notre partenaire, l&#8217;agence MRJ à Paris.<span id="more-123"></span></p>
<h2>[CDI / STAGE] Offre d&#8217;emploi, développeur PHP (H/F) à Paris</h2>
<p><strong>L&#8217;entreprise :</strong></p>
<p>Filiale informatique d’un grand groupe de presse français créé il y a plus de 15 ans, l’Agence MRJ est une PME innovante reconnue par ses clients pour ses compétences et son savoir-faire. Nous accompagnons nos clients dans la conception, le développement, le déploiement et le suivi de leurs outils de gestion de contenu Web (Portails Internet, Intranet d’entreprise, Extranets de gestion&#8230;), de Gestion Documentaire, de travail collaboratif, d’Ebusiness et de Gestion de la Relation Client.</p>
<p>Quelques références : Azzaro Homme, Arrow, Française des jeux, etc.</p>
<p>Dans le cadre de la croissance de son activité et pour accompagner le lancement de nouveaux projets innovants, l’agence recherche un développeur PHP / MYSQL (H/F) en stage et un CDI.</p>
<p>De formation Bac+2 minimum, vous justifiez d&#8217;une première expérience réussie sur des projets web (portails, e-commerce, e-CRM, etc). Vous maîtrisez l’intégration HTML (Javascript inclus), PHP orienté objet et au minimum MySQL. </p>
<p>Vous connaissez ou à défaut possédez un grand intérêt pour :<br />
Administration d’un serveur web (Debian), Versionning (SVN), PostgreSQL, Framework de développement.</p>
<p>Vous serez formé à nos méthodes et outils de travail, et nous vous offrons un environnement à taille humaine, jeune et expérimenté, où vos initiatives sont valorisées !</p>
<p><strong>Candidature :</strong><br />
Si vous désirez rejoindre notre équipe dynamique, n&#8217;hésitez pas à nous transmettre votre candidature à : <strong>recrut-dev0809[at]mrj-webagency.com</strong>.</p>
<img src="http://feeds.feedburner.com/~r/FormationCakephp/~4/410312143" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.formation-cakephp.com/123/offre-demploistage/feed</wfw:commentRss>
		<feedburner:awareness xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0">http://api.feedburner.com/awareness/1.0/GetItemData?uri=FormationCakephp&amp;itemurl=http%3A%2F%2Fwww.formation-cakephp.com%2F123%2Foffre-demploistage</feedburner:awareness></item>
		<item>
		<title>Afficher le chemin parcouru dans une arborescence</title>
		<link>http://www.formation-cakephp.com/120/chemin-arborescence-breadcrumbs</link>
		<comments>http://www.formation-cakephp.com/120/chemin-arborescence-breadcrumbs#comments</comments>
		<pubDate>Sun, 07 Sep 2008 21:05:47 +0000</pubDate>
		<dc:creator>Pierre-Emmanuel Fringant</dc:creator>
		
		<category><![CDATA[Interaction]]></category>

		<category><![CDATA[Arborescence]]></category>

		<category><![CDATA[MPTT Tree]]></category>

		<guid isPermaLink="false">http://www.formation-cakephp.com/?p=120</guid>
		<description><![CDATA[Dernier article de notre série sur la gestion et la présentation d&#8217;une arborescence, nous allons voir ici comment afficher le chemin parcouru dans l&#8217;arborescence, sous la forme : Catégorie 1 &#62; Catégorie 1.2 &#62; Catégorie 1.2.8, en utilisant deux méthodes peu connues du Helper Html, addCrumbs et getCrumbs.
1. Détermination du chemin parcouru
Nous allons appeler la [...]]]></description>
			<content:encoded><![CDATA[<p>Dernier article de notre série sur la gestion et la présentation d&#8217;une arborescence, nous allons voir ici comment afficher le chemin parcouru dans l&#8217;arborescence, sous la forme : Catégorie 1 &gt; Catégorie 1.2 &gt; Catégorie 1.2.8, en utilisant deux méthodes peu connues du Helper Html, <code>addCrumbs</code> et <code>getCrumbs</code>.<span id="more-120"></span></p>
<h2>1. Détermination du chemin parcouru</h2>
<p>Nous allons appeler la méthode <code>getpath</code>, automatiquement ajoutée au Modèle Category par le Comportement Tree, qui renvoie un tableau avec tous les ancêtres d&#8217;une catégorie, elle-même incluse à la fin du tableau. Nous plaçons cette logique dans l&#8217;action <code>view</code> du Controller Categories :</p>

<div class="wp_syntax"><div class="code"><pre class="php"><span style="color: #666666; font-style: italic;">// {app}/controllers/categories_controller.php</span>
<span style="color: #000000; font-weight: bold;">function</span> view<span style="color: #009900;">&#40;</span><span style="color: #000033;">$id</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #666666; font-style: italic;">// La catégorie</span>
	<span style="color: #000033;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'category'</span><span style="color: #339933;">,</span> <span style="color: #000033;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Category</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">read</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">null</span><span style="color: #339933;">,</span> <span style="color: #000033;">$id</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// La liste de ses ancêtres</span>
	<span style="color: #000033;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'path'</span><span style="color: #339933;">,</span> <span style="color: #000033;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Category</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getpath</span><span style="color: #009900;">&#40;</span><span style="color: #000033;">$id</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Nous passons à la vue les données de la catégorie choisie, sous le nom <code>$category</code>, et le tableau de ses ancêtres, sous le nom <code>$path</code>.</p>
<h2>2. Affichage</h2>
<h3>2.1 La méthode HtmlHelper::addCrumb</h3>
<p>Cette méthode du Helper Html permet d&#8217;ajouter une &#8220;tranche&#8221; au chemin parcouru, sous forme d&#8217;un lien ou d&#8217;un texte simple. Nous voulons dans notre cas que le chemin permette de remonter facilement dans l&#8217;arborescence, les ancêtres de la catégorie en cours devront donc être cliquables, mais pas la catégorie elle-même puisque nous y sommes déjà.</p>

<div class="wp_syntax"><div class="code"><pre class="php"><span style="color: #666666; font-style: italic;">// {app}/views/categories/view.ctp</span>
<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000033;">$path</span> <span style="color: #b1b100;">as</span> <span style="color: #000033;">$crumb</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #000033;">$url</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">null</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// Catégorie en cours ?</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000033;">$crumb</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Category'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">!=</span> <span style="color: #000033;">$category</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Category'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000033;">$url</span> <span style="color: #339933;">=</span> <span style="color: #000033;">$html</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">url</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #000033;">$crumb</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Category'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000033;">$html</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addCrumb</span><span style="color: #009900;">&#40;</span><span style="color: #000033;">$crumb</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Category'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'libelle'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000033;">$url</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h3>2.2 La méthode HtmlHelper::getCrumb</h3>
<p>Nous n&#8217;avons plus qu&#8217;à afficher notre chemin avec la méthode getCrumb, à qui nous indiquons que nous souhaitons séparer les tranches du chemin par le signe &#8220;&gt;&#8221; ou &#8220;strictement supérieur à&#8221;, dont l&#8217;entité html s&#8217;écrit &#8220;&amp;gt;&#8221;.</p>

<div class="wp_syntax"><div class="code"><pre class="php"><span style="color: #666666; font-style: italic;">// {app}/views/categories/view.ctp</span>
<span style="color: #339933;">&lt;</span>div id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;breadcrumbs&quot;</span><span style="color: #339933;">&gt;</span>
	<span style="color: #339933;">&lt;</span>strong<span style="color: #339933;">&gt;</span>Vous êtes ici <span style="color: #339933;">:&lt;/</span>strong<span style="color: #339933;">&gt;</span> <span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #990000;">echo</span> <span style="color: #000033;">$html</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getCrumbs</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">' &amp;gt; '</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
<span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span></pre></div></div>

<p>Nous obtenons un joli chemin sous la forme :<br />
<strong>Vous êtes ici :</strong> <a href="javascript:;">Catégorie 1</a> &gt; <a href="javascript:;">Catégorie 1.2</a> &gt; Catégorie 1.2.8.</p>
<img src="http://feeds.feedburner.com/~r/FormationCakephp/~4/391789338" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.formation-cakephp.com/120/chemin-arborescence-breadcrumbs/feed</wfw:commentRss>
		<feedburner:awareness xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0">http://api.feedburner.com/awareness/1.0/GetItemData?uri=FormationCakephp&amp;itemurl=http%3A%2F%2Fwww.formation-cakephp.com%2F120%2Fchemin-arborescence-breadcrumbs</feedburner:awareness></item>
		<item>
		<title>Présentation dynamique d’une arborescence avec jQuery</title>
		<link>http://www.formation-cakephp.com/116/presentation-dynamique-arborescence-jquery</link>
		<comments>http://www.formation-cakephp.com/116/presentation-dynamique-arborescence-jquery#comments</comments>
		<pubDate>Sat, 06 Sep 2008 12:51:35 +0000</pubDate>
		<dc:creator>Pierre-Emmanuel Fringant</dc:creator>
		
		<category><![CDATA[Interaction]]></category>

		<category><![CDATA[Arborescence]]></category>

		<category><![CDATA[jQuery]]></category>

		<category><![CDATA[MPTT Tree]]></category>

		<guid isPermaLink="false">http://www.formation-cakephp.com/?p=116</guid>
		<description><![CDATA[Après avoir vu comment gérer une arborescence et comment la présenter de façon statique, voyons maintenant comment offrir la possibilité de naviguer dans l&#8217;arbre sans changer de page. Nous nous imposons les contraintes suivantes :

Le traitement javascript devra être non-obstrusif ;
Lorsqu&#8217;une catégorie est cliquée, la page d&#8217;arrivée doit faire apparaître la branche de la catégorie [...]]]></description>
			<content:encoded><![CDATA[<p>Après avoir vu comment <a href="/94/arborescence-mptt-tree-behavior">gérer une arborescence</a> et <a href="/102/presentation-statique-arborescence">comment la présenter de façon statique</a>, voyons maintenant comment offrir la possibilité de naviguer dans l&#8217;arbre sans changer de page. Nous nous imposons les contraintes suivantes :</p>
<ol>
<li>Le traitement javascript devra être non-obstrusif ;</li>
<li>Lorsqu&#8217;une catégorie est cliquée, la page d&#8217;arrivée doit faire apparaître la branche de la catégorie choisie dépliée (enfants éventuels + ancêtres) et les autres repliées.</li>
</ol>
<p><span id="more-116"></span></p>
<h2>1. Installation du plugin jQuery, TreeView</h2>
<p>Nous choisissons d&#8217;utiliser la librairie javascript jQuery et l&#8217;un de ses plugins, <a href="http://docs.jquery.com/Plugins/Treeview" class="ico-ext" target="_blank">TreeView</a>. Le plugin offre plusieurs styles pour la présentation de l&#8217;arborescence que nous pouvons voir sur cette <a href="http://jquery.bassistance.de/treeview/demo/" class="ico-ext" target="_blank">page d&#8217;exemples</a>. Nous choisissons le premier exemple, &#8220;Sample 0&#8243;.</p>
<p>Nous téléchargeons l&#8217;archive et répartissons son contenu de la façon suivante :</p>
<ul>
<li><code>jquery.treeview.pack.js</code> (la version la plus compacte) dans le dossier <code>{app}/webroot/js/</code></li>
<li><code>jquery.treeview.css</code> dans le dossier <code>{app}/webroot/css/</code></li>
<li><code>images/treeview-default-line.gif</code> et <code>images/treeview-default.gif</code> dans le dossier <code>{app}/img/</code> (les deux images nécessaires à la présentation du &#8220;Sample 0&#8243;)</li>
</ul>
<h2>2. Génération de l&#8217;arbre</h2>
<p>Comme nous souhaitons que l&#8217;ajout du javascript soit non-obstrusif, la navigation dans l&#8217;arborescence doit être possible si le javascript est désactivé ou indisponible sur le navigateur client. Nous reprenons donc le code présenté dans le sujet précédent, quasiment à l&#8217;identique :</p>

<div class="wp_syntax"><div class="code"><pre class="php"><span style="color: #666666; font-style: italic;">// {app}/app_controller.php</span>
<span style="color: #000000; font-weight: bold;">class</span> AppController <span style="color: #000000; font-weight: bold;">extends</span> Controller
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000033;">$helpers</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Html'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Form'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Tree'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000033;">$uses</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Category'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">function</span> beforeRender<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000033;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Category</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">recursive</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">-1</span><span style="color: #339933;">;</span>
		<span style="color: #000033;">$layout_categories</span> <span style="color: #339933;">=</span> <span style="color: #000033;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Category</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">children</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000033;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'layout_categories'</span><span style="color: #339933;">,</span> <span style="color: #000033;">$layout_categories</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="php"><span style="color: #666666; font-style: italic;">// /views/layout/default.ctp</span>
<span style="color: #990000;">echo</span> <span style="color: #000033;">$tree</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">generate</span><span style="color: #009900;">&#40;</span>
	<span style="color: #000033;">$layout_categories</span><span style="color: #339933;">,</span>
	<span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
		<span style="color: #0000ff;">'model'</span>   <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Category'</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'element'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'category_layout'</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'id'</span>      <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'arborescence'</span><span style="color: #339933;">,</span>
	<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Seul différence ici, l&#8217;ajout de l&#8217;identifiant &#8216;arborescence&#8217; au premier &lt;ul&gt; de la liste, qui va permettre au plugin TreeView d&#8217;identifier l&#8217;élément à transformer en arbre.</p>

<div class="wp_syntax"><div class="code"><pre class="php"><span style="color: #666666; font-style: italic;">// /views/elements/category_layout.ctp</span>
<span style="color: #990000;">echo</span> <span style="color: #000033;">$html</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">link</span><span style="color: #009900;">&#40;</span>
	<span style="color: #000033;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Category'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'libelle'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
	<span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
		<span style="color: #0000ff;">'controller'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'categories'</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'action'</span>     <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'view'</span><span style="color: #339933;">,</span>
		<span style="color: #000033;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Category'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#93;</span>
	<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="php"><span style="color: #666666; font-style: italic;">// {app}/controllers/categories_controller.php</span>
<span style="color: #000000; font-weight: bold;">function</span> view<span style="color: #009900;">&#40;</span><span style="color: #000033;">$id</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #000033;">$category</span> <span style="color: #339933;">=</span> <span style="color: #000033;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Category</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">read</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">null</span><span style="color: #339933;">,</span> <span style="color: #000033;">$id</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000033;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">compact</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'category'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Dans l&#8217;action du Contrôleur Categories, nous n&#8217;essayons plus de retrouver le chemin parcouru jusqu&#8217;à la catégorie cliquée, le plugin sera en effet capable d&#8217;afficher lui-même la catégorie choisie dépliée avec ses enfants et ses ancêtres en se basant sur l&#8217;url de la page.</p>
<h2>3. Mis en place du plugin</h2>
<p>Il ne nous reste qu&#8217;à appeler la librairie jQuery et le plugin TreeView dans le header du layout.</p>

<div class="wp_syntax"><div class="code"><pre class="php"><span style="color: #666666; font-style: italic;">// /views/layout/default.ctp</span>
<span style="color: #339933;">&lt;</span>head<span style="color: #339933;">&gt;</span>
<span style="color: #000000; font-weight: bold;">&lt;?php</span> e<span style="color: #009900;">&#40;</span><span style="color: #000033;">$javascript</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">link</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'http://www.google.com/jsapi'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span> 
<span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span>google<span style="color: #339933;">.</span>load<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;jquery&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;1.2.6&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;&lt;/</span>script<span style="color: #339933;">&gt;</span>
<span style="color: #000000; font-weight: bold;">&lt;?php</span>
e<span style="color: #009900;">&#40;</span><span style="color: #000033;">$javascript</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">link</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'jquery.treeview.pack.js'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
e<span style="color: #009900;">&#40;</span><span style="color: #000033;">$javascript</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">link</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'jquery.treeview.init.js'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
e<span style="color: #009900;">&#40;</span><span style="color: #000033;">$html</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">css</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'jquery.treeview.css'</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">null</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'media'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'screen'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span> 
<span style="color: #339933;">&lt;/</span>head<span style="color: #339933;">&gt;</span></pre></div></div>

<p>Voyons l&#8217;effet de ces 5 lignes :</p>
<ul>
<li>Les deux premières lignes servent à inclure la librairie javascript jQuery en se servant d&#8217;un service gratuit de Google, qui se charge de mettre à disposition du site la version choisie de jQuery, ici la 1.2.6. Nous économisons un peu de bande passante, mais surtout nous profitons de la vitesse de réponse des serveurs de Google, et du fait qu&#8217;il est possible que le navigateur client ait déjà visité un site qui profite de ce service avant d&#8217;arriver sur le nôtre, et donc que la librairie soit déjà présente dans le cache ;</li>
<li>la 3ème ligne inclut le plugin TreeView ;</li>
<li>la 4ème ligne inclut un petit bout de code javascript que nous allons créer juste après pour initialiser le plugin ;</li>
<li>la 5ème ligne inclut la feuille de style CSS pour présenter l&#8217;arborescence.</li>
</ul>
<h2>4. Initialisation du plugin TreeView</h2>
<p>Il ne nous reste qu&#8217;à créer un petit fichier javascript pour initialiser le plugin :</p>

<div class="wp_syntax"><div class="code"><pre class="javascript"><span style="color: #006600; font-style: italic;">// {app}/webroot/js/jquery.treeview.init.js</span>
$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #006600;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#arborescence'</span><span style="color: #009900;">&#41;</span>.<span style="color: #006600;">treeview</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
		persist<span style="color: #339933;">:</span>   <span style="color: #3366CC;">'location'</span><span style="color: #339933;">,</span>
		collapsed<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span>
		animated<span style="color: #339933;">:</span>  <span style="color: #3366CC;">'fast'</span>
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Voyons les options définies :</p>
<ul>
<li><code>persist</code> : cette option peut prendre deux valeurs : &#8216;cookie&#8217; et &#8216;location&#8217;. Avec &#8216;cookie&#8217;, le plugin se sert d&#8217;un cookie pour savoir quelle branche afficher, avec &#8216;location&#8217;, le plugin compare l&#8217;url en cours avec chaque url de l&#8217;arbre ;</li>
<li><code>collapsed</code> : si vrai, l&#8217;arbre est affiché replié sauf la branche de la catégorie choisie ; si faux, l&#8217;arbre est affiché déplié par défaut, quelle que soit la catégorie cliquée ;</li>
<li><code>animated</code> : définie la vitesse d&#8217;animation lors de l&#8217;ouverture ou la fermeture d&#8217;une branche. Si cette option n&#8217;est pas définie, il n&#8217;y aura pas d&#8217;animation.</li>
</ul>
<p>Nous laissons le lecteur explorer les autres options du plugin sur cette page : <a href="http://docs.jquery.com/Plugins/Treeview/treeview#options" target="_blank" class="ico-ext">TreeView options</a>.</p>
<img src="http://feeds.feedburner.com/~r/FormationCakephp/~4/391789339" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.formation-cakephp.com/116/presentation-dynamique-arborescence-jquery/feed</wfw:commentRss>
		<feedburner:awareness xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0">http://api.feedburner.com/awareness/1.0/GetItemData?uri=FormationCakephp&amp;itemurl=http%3A%2F%2Fwww.formation-cakephp.com%2F116%2Fpresentation-dynamique-arborescence-jquery</feedburner:awareness></item>
		<item>
		<title>Présentation statique d’une arborescence</title>
		<link>http://www.formation-cakephp.com/102/presentation-statique-arborescence</link>
		<comments>http://www.formation-cakephp.com/102/presentation-statique-arborescence#comments</comments>
		<pubDate>Tue, 02 Sep 2008 09:33:00 +0000</pubDate>
		<dc:creator>Pierre-Emmanuel Fringant</dc:creator>
		
		<category><![CDATA[Interaction]]></category>

		<category><![CDATA[Arborescence]]></category>

		<category><![CDATA[Behavior]]></category>

		<category><![CDATA[Comportement]]></category>

		<category><![CDATA[MPTT Tree]]></category>

		<guid isPermaLink="false">http://www.formation-cakephp.com/?p=102</guid>
		<description><![CDATA[Après avoir vu comment gérer une arborescence avec CakePHP, intéressons-nous maintenant à la présentation de cette arborescence en tant que moyen de navigation principal dans les pages du site. Nous entendons par &#8220;statique&#8221; le fait que seules les branches de premier niveau soient affichées à l&#8217;arrivée sur le site, puis que la branche cliquée fasse [...]]]></description>
			<content:encoded><![CDATA[<p>Après avoir vu comment <a href="/94/arborescence-mptt-tree-behavior">gérer une arborescence avec CakePHP</a>, intéressons-nous maintenant à la présentation de cette arborescence en tant que moyen de navigation principal dans les pages du site. Nous entendons par &#8220;statique&#8221; le fait que seules les branches de premier niveau soient affichées à l&#8217;arrivée sur le site, puis que la branche cliquée fasse apparaitre ses filles, et ainsi de suite.<span id="more-102"></span></p>
<h2>1. Passage de l&#8217;arborescence au layout</h2>
<p>Comme pour l&#8217;administration de l&#8217;arbre, nous allons utiliser dans les Vues le <a href="http://bakery.cakephp.org/articles/view/tree-helper-1" class="ico-ext" target="_blank">Helper Tree</a> créé par AD7six, mais cette fois nous allons nous en servir pour afficher l&#8217;arborescence sur toute la partie publique du site. C&#8217;est pourquoi nous devons changer l&#8217;appel au Helper, nous le passons du Contrôleur Categories au Contrôleur général.</p>
<p>C&#8217;est également ce Contrôleur général qui va se charger de passer l&#8217;arborescence au layout public. Le meilleur endroit pour effectuer cette opération est sans doute la méthode <code>beforeRender</code> qui sera appelée avant la génération des Vues.</p>

<div class="wp_syntax"><div class="code"><pre class="php"><span style="color: #666666; font-style: italic;">// {app}/app_controller.php</span>
<span style="color: #000000; font-weight: bold;">class</span> AppController <span style="color: #000000; font-weight: bold;">extends</span> Controller
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000033;">$helpers</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Html'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Form'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Tree'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000033;">$uses</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Category'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">function</span> beforeRender<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000033;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Category</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">recursive</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">-1</span><span style="color: #339933;">;</span>
		<span style="color: #000033;">$layout_categories</span> <span style="color: #339933;">=</span> <span style="color: #000033;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Category</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">children</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000033;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'layout_categories'</span><span style="color: #339933;">,</span> <span style="color: #000033;">$layout_categories</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Rappelons que la méthode <code>children()</code>, ajoutée par le Comportement Tree, renvoie l&#8217;arborescence complète quand elle est appelée avec le seul paramètre <code>false</code>.</p>
<h2>2. Le layout public</h2>
<p>Affichons maintenant notre arborescence dans le layout principal :</p>

<div class="wp_syntax"><div class="code"><pre class="php"><span style="color: #666666; font-style: italic;">// /views/layout/default.ctp</span>
<span style="color: #990000;">echo</span> <span style="color: #000033;">$tree</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">generate</span><span style="color: #009900;">&#40;</span>
	<span style="color: #000033;">$layout_categories</span><span style="color: #339933;">,</span>
	<span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
		<span style="color: #0000ff;">'model'</span>   <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Category'</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'element'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'category_layout'</span>
	<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>N&#8217;oublions pas ici de préciser quel Modèle est concerné, sinon le Helper ne pourra pas afficher l&#8217;arbre correctement.</p>
<h2>3. L&#8217;élément d&#8217;affichage d&#8217;une catégorie</h2>
<p>Le Helper permet d&#8217;appeler un élément pour chaque branche, découvrons-le :</p>

<div class="wp_syntax"><div class="code"><pre class="php"><span style="color: #666666; font-style: italic;">// /views/elements/category_layout.ctp</span>
<span style="color: #990000;">echo</span> <span style="color: #000033;">$html</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">link</span><span style="color: #009900;">&#40;</span>
	<span style="color: #000033;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Category'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'libelle'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
	<span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
		<span style="color: #0000ff;">'controller'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'categories'</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'action'</span>     <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'view'</span><span style="color: #339933;">,</span>
		<span style="color: #000033;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Category'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#93;</span>
	<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Nous avançons, mais toutes les catégories sont affichées, quel que soit leur niveau. Or nous ne voulons afficher que les catégories de premier niveau, plus les enfants d&#8217;une catégorie, si l&#8217;une d&#8217;elle a été cliquée.</p>
<p>Nous devons commencer par savoir si une catégorie a été cliquée, et si oui, laquelle. Cette logique s&#8217;effectue dans le Contrôleur Categories, dans l&#8217;action <code>view</code> qui est la destination de tous les liens de notre menu. Mais attention, la catégorie cliquée peut ne pas être de premier niveau, mais se trouver profondément dans l&#8217;arbre, nous devons donc pour une catégorie donnée remonter toute l&#8217;arborescence pour identifier ses parents :</p>

<div class="wp_syntax"><div class="code"><pre class="php"><span style="color: #666666; font-style: italic;">// {app}/controllers/categories_controller.php</span>
<span style="color: #000000; font-weight: bold;">function</span> view<span style="color: #009900;">&#40;</span><span style="color: #000033;">$id</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #000033;">$category</span> <span style="color: #339933;">=</span> <span style="color: #000033;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Category</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">read</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">null</span><span style="color: #339933;">,</span> <span style="color: #000033;">$id</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000033;">$path_ids</span> <span style="color: #339933;">=</span> <span style="color: #000033;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Category</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get_path_ids</span><span style="color: #009900;">&#40;</span><span style="color: #000033;">$id</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000033;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">compact</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'category'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'path_ids'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Voyons la méthode <code>get_path_ids</code>, que nous plaçons dans le Modèle Category :</p>

<div class="wp_syntax"><div class="code"><pre class="php"><span style="color: #666666; font-style: italic;">// {app}/models/category.php</span>
<span style="color: #000000; font-weight: bold;">function</span> get_path_ids<span style="color: #009900;">&#40;</span><span style="color: #000033;">$id</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #000033;">$path</span> <span style="color: #339933;">=</span> <span style="color: #000033;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getpath</span><span style="color: #009900;">&#40;</span><span style="color: #000033;">$id</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #b1b100;">return</span> Set<span style="color: #339933;">::</span><span style="color: #990000;">extract</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/Category/id'</span><span style="color: #339933;">,</span> <span style="color: #000033;">$path</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>La méthode <code>getpath</code> a été ajoutée automatiquement par le Comportement Tree au Modèle Category, et renvoie un tableau des parents d&#8217;un élément de l&#8217;arbre (ainsi que l&#8217;élément lui-même, en dernier). Seuls les id des catégories nous intéressent, nous les extrayons donc avec la méthode statique <code>extract</code> de la class <code>Set</code>. Grâce à cette liste d&#8217;id de catégories, nous pouvons boucler sur les éléments de l&#8217;arbre et voir si l&#8217;id de catégorie en cours appartient à cette liste, auquel cas nous devons l&#8217;afficher ainsi que ses filles éventuelles.</p>
<h2>4. Affichage de l&#8217;arborescence</h2>
<p>Nous avons ici deux possibilités pour parvenir au même résultat visuel : soit nous continuons d&#8217;afficher toutes les branches dans le source HTML et cachons les catégories qui doivent l&#8217;être avec une propriété CSS, soit nous effectuons un traitement préalable du tableau des catégories avant le boucler dessus.</p>
<h3>4.1. Méthode 1 : Arborescence complète et camouflage CSS</h3>
<p>Commençons par la méthode la plus simple : reprenons simplement l&#8217;élément d&#8217;affichage d&#8217;une catégorie, appelé par le Helper Tree, et ajoutons un petit test :</p>

<div class="wp_syntax"><div class="code"><pre class="php"><span style="color: #666666; font-style: italic;">// /views/elements/category_layout.ctp</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000033;">$path_ids</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #000033;">$path_ids</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000033;">$hasChildren</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #339933;">!</span><span style="color: #990000;">in_array</span><span style="color: #009900;">&#40;</span><span style="color: #000033;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Category'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000033;">$path_ids</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #000033;">$tree</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addTypeAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'style'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'display'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'none'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #990000;">echo</span> <span style="color: #000033;">$html</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">link</span><span style="color: #009900;">&#40;</span>
	<span style="color: #000033;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Category'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'libelle'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
	<span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
		<span style="color: #0000ff;">'controller'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'categories'</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'action'</span>     <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'view'</span><span style="color: #339933;">,</span>
		<span style="color: #000033;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Category'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#93;</span>
	<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Le test est assez limpide : si la catégorie en train d&#8217;être analysée a des enfants (la variable <code>$hasChildren</code> nous est automatiquement fournie par le Helper Tree) et que l&#8217;id de la catégorie n&#8217;est pas dans la liste <code>$path_ids</code> (la catégorie cliquée et ses parents), alors nous ajoutons un style CSS grâce à la méthode <code>addTypeAttribute</code> pour cacher les enfants. Nous obtenons bien le résultat attendu, mais ce n&#8217;est que visuel, les catégories cachées sont toujours dans le code source HTML.</p>
<h3>4.2. Méthode 2 : Suppression des catégories non voulues dans l&#8217;arbre à postériori</h3>
<p>Pour cette deuxième méthode, revenons en arrière et reprenons l&#8217;élément tel qu&#8217;il l&#8217;était au début.</p>

<div class="wp_syntax"><div class="code"><pre class="php"><span style="color: #666666; font-style: italic;">// /views/elements/category_layout.ctp</span>
<span style="color: #990000;">echo</span> <span style="color: #000033;">$html</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">link</span><span style="color: #009900;">&#40;</span>
	<span style="color: #000033;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Category'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'libelle'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
	<span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
		<span style="color: #0000ff;">'controller'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'categories'</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'action'</span>     <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'view'</span><span style="color: #339933;">,</span>
		<span style="color: #000033;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Category'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#93;</span>
	<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>La logique va intervenir avant de passer l&#8217;arborescence au layout, dans le Contrôleur général.</p>

<div class="wp_syntax"><div class="code"><pre class="php"><span style="color: #666666; font-style: italic;">// {app}/app_controller.php</span>
<span style="color: #000000; font-weight: bold;">class</span> AppController <span style="color: #000000; font-weight: bold;">extends</span> Controller
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000033;">$helpers</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Html'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Form'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Tree'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000033;">$uses</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Category'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000033;">$path_ids</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">function</span> beforeRender<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000033;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Category</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">recursive</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">-1</span><span style="color: #339933;">;</span>
		<span style="color: #000033;">$layout_categories</span> <span style="color: #339933;">=</span> <span style="color: #000033;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Category</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">children</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000033;">$layout_categories</span> <span style="color: #b1b100;">as</span> <span style="color: #000033;">$i</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000033;">$cat</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000033;">$cat</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Category'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'parent_id'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #339933;">!</span><span style="color: #990000;">in_array</span><span style="color: #009900;">&#40;</span><span style="color: #000033;">$cat</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Category'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000033;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">path_ids</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #339933;">!</span><span style="color: #990000;">in_array</span><span style="color: #009900;">&#40;</span><span style="color: #000033;">$cat</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Category'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'parent_id'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000033;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">path_ids</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
			<span style="color: #009900;">&#123;</span>
				<span style="color: #990000;">unset</span><span style="color: #009900;">&#40;</span><span style="color: #000033;">$layout_categories</span><span style="color: #009900;">&#91;</span><span style="color: #000033;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #000033;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'layout_categories'</span><span style="color: #339933;">,</span> <span style="color: #000033;">$layout_categories</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Notons l&#8217;ajout de la variable de classe <code>$path_ids</code>, un tableau vide par défaut.<br />
Le raisonnement est le même : pour chaque catégorie de l&#8217;arbre, si le parent n&#8217;est pas nul (catégorie qui n&#8217;est pas de premier niveau), que l&#8217;id du parent n&#8217;est pas dans la liste <code>$path_ids</code> et que l&#8217;id de la catégorie n&#8217;est pas dans la liste <code>$path_ids</code>, alors nous enlevons la catégorie du tableau.</p>
<p>Il ne nous reste qu&#8217;à changer légèrement le comportement de l&#8217;action <code>view</code> dans le Contrôleur Categories :</p>

<div class="wp_syntax"><div class="code"><pre class="php"><span style="color: #666666; font-style: italic;">// {app}/controllers/categories_controller.php</span>
<span style="color: #000000; font-weight: bold;">function</span> view<span style="color: #009900;">&#40;</span><span style="color: #000033;">$id</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #000033;">$category</span> <span style="color: #339933;">=</span> <span style="color: #000033;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Category</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">read</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">null</span><span style="color: #339933;">,</span> <span style="color: #000033;">$id</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000033;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">path_ids</span> <span style="color: #339933;">=</span> <span style="color: #000033;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Category</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get_path_ids</span><span style="color: #009900;">&#40;</span><span style="color: #000033;">$id</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000033;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">compact</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'category'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Cette fois nous n&#8217;avons plus besoin de passer la liste des id des parents à la Vue, mais nous la passons à la variable de classe pour que le Contrôleur général puisse y accéder.</p>
<p>Notre arborescence est terminée. Nous verrons dans un prochain sujet comment faire une présentation sous forme d&#8217;<a href="/116/presentation-dynamique-arborescence-jquery">arbre interactif avec jQuery</a>.</p>
<img src="http://feeds.feedburner.com/~r/FormationCakephp/~4/381906699" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.formation-cakephp.com/102/presentation-statique-arborescence/feed</wfw:commentRss>
		<feedburner:awareness xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0">http://api.feedburner.com/awareness/1.0/GetItemData?uri=FormationCakephp&amp;itemurl=http%3A%2F%2Fwww.formation-cakephp.com%2F102%2Fpresentation-statique-arborescence</feedburner:awareness></item>
		<item>
		<title>Gérer une arborescence avec CakePHP</title>
		<link>http://www.formation-cakephp.com/94/arborescence-mptt-tree-behavior</link>
		<comments>http://www.formation-cakephp.com/94/arborescence-mptt-tree-behavior#comments</comments>
		<pubDate>Fri, 29 Aug 2008 21:52:43 +0000</pubDate>
		<dc:creator>Pierre-Emmanuel Fringant</dc:creator>
		
		<category><![CDATA[Interaction]]></category>

		<category><![CDATA[Arborescence]]></category>

		<category><![CDATA[Behavior]]></category>

		<category><![CDATA[Comportement]]></category>

		<category><![CDATA[MPTT Tree]]></category>

		<guid isPermaLink="false">http://www.formation-cakephp.com/?p=94</guid>
		<description><![CDATA[Pouvoir gérer des données sous forme d&#8217;une arborescence est un besoin courant lors de la réalisation d&#8217;un site web, nous prendrons ici l&#8217;exemple des catégories d&#8217;un catalogue de produits. Nous allons nous servir du Comportement Tree fourni par Cake.
1. La table categories
Le Comportement Tree utilise la méthode dite de &#8220;représentation intervallaire&#8221; pour gérer l&#8217;arborescence (en [...]]]></description>
			<content:encoded><![CDATA[<p>Pouvoir gérer des données sous forme d&#8217;une arborescence est un besoin courant lors de la réalisation d&#8217;un site web, nous prendrons ici l&#8217;exemple des catégories d&#8217;un catalogue de produits. Nous allons nous servir du Comportement Tree fourni par Cake.<span id="more-94"></span></p>
<h2>1. La table <code>categories</code></h2>
<p>Le Comportement Tree utilise la méthode dite de &#8220;représentation intervallaire&#8221; pour gérer l&#8217;arborescence (en anglais, <em>Modified Preorder Tree Traversal</em>). Nous sortons ici du cadre de CakePHP, et laissons le lecteur découvrir cette méthode de représentation en lisant l&#8217;excellent article de SQL Pro : <a href="http://sqlpro.developpez.com/cours/arborescence/" class="ico-ext" target="_blank">Gestion d&#8217;arbres par représentation intervallaire</a>. Retenons simplement que le Comportement Tree impose la présence de 3 champs dans la table de l&#8217;arborescence :</p>
<ul>
<li><code>parent_id</code> : l&#8217;id du parent</li>
<li><code>lft</code> : borne basse</li>
<li><code>rght</code> : borne haute</li>
</ul>
<p>Voici donc la déclaration complète de notre table des catégories :</p>

<div class="wp_syntax"><div class="code"><pre class="sql"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #ff0000;">`categories`</span> <span style="color: #66cc66;">&#40;</span>
  <span style="color: #ff0000;">`id`</span> int<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">10</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">UNSIGNED</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">AUTO_INCREMENT</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`parent_id`</span> int<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">10</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">UNSIGNED</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`lft`</span> int<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">10</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">UNSIGNED</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`rght`</span> int<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">10</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">UNSIGNED</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`libelle`</span> varchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">50</span><span style="color: #66cc66;">&#41;</span> collate utf8_unicode_ci <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span>  <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">`id`</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>
  <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #ff0000;">`parent_id`</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">`parent_id`</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>
  <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #ff0000;">`lft`</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">`lft`</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span></pre></div></div>

<h2>2. Le Modèle <code>Category</code></h2>
<p>Voyons le Modèle associé à notre table :</p>

<div class="wp_syntax"><div class="code"><pre class="php"><span style="color: #000000; font-weight: bold;">class</span> Category <span style="color: #000000; font-weight: bold;">extends</span> AppModel
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000033;">$displayField</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'libelle'</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000033;">$hasMany</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Product'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000033;">$actsAs</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Tree'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000033;">$validate</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
		<span style="color: #0000ff;">'libelle'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
			<span style="color: #0000ff;">'rule'</span>       <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'/<span style="color: #000099; font-weight: bold;">\S</span>+/'</span><span style="color: #339933;">,</span>
			<span style="color: #0000ff;">'required'</span>   <span style="color: #339933;">=&gt;</span> <span style="color: #000000; font-weight: bold;">true</span><span style="color: #339933;">,</span>
			<span style="color: #0000ff;">'allowEmpty'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000000; font-weight: bold;">false</span><span style="color: #339933;">,</span>
			<span style="color: #0000ff;">'message'</span>    <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;Le libellé de la catégorie doit être renseigné.&quot;</span>
		<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'parent_id'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
			<span style="color: #0000ff;">'rule'</span>    <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'checkParadox'</span><span style="color: #339933;">,</span>
			<span style="color: #0000ff;">'on'</span>      <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'update'</span><span style="color: #339933;">,</span>
			<span style="color: #0000ff;">'message'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;Une catégorie ne peut pas devenir sa propre fille !&quot;</span>
		<span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">/**
	 * Retourne faux si l'id est égal au nouvel id parent
	 *
	 * @param array $data Données à valider, en provenance du formulaire.
	 * @return boolean Faux si id == parent_id, vrai sinon.
	 */</span>
	<span style="color: #000000; font-weight: bold;">function</span> checkParadox<span style="color: #009900;">&#40;</span><span style="color: #000033;">$data</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000033;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</span><span style="color: #009900;">&#91;</span><span style="color: #000033;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">alias</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #000033;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'parent_id'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">!=</span> <span style="color: #000033;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</span><span style="color: #009900;">&#91;</span><span style="color: #000033;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">alias</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">true</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Quelques éclaircissements :</p>
<ul>
<li>la variable <code>$displayField</code> sera nécessaire pour présenter correctement les catégories sous forme d&#8217;une liste déroulante dans un &lt;select&gt; ;</li>
<li>nous définissons une relation <code>hasMany</code> entre une catégorie et un produit ;</li>
<li>$<code>actsAs</code> : nous indiquons à Cake que le Modèle Category doit se comporter comme une arborescence ;</li>
<li><code>$validate</code> : deux règles de validation sont mises en place : la première indique que le libellé d&#8217;une catégorie ne doit jamais être vide, la deuxième fait appel à une méthode définie juste après qui garantie qu&#8217;une catégorie ne peut pas s&#8217;avoir elle-même comme parent.</li>
</ul>
<h2>3. Les Vues</h2>
<p>Nous n&#8217;allons voir que les Vues de l&#8217;administration des catégories, à savoir la vue d&#8217;ajout/édition, et la vue des catégories sous forme d&#8217;un arbre avec la possibilité de monter ou descendre une catégorie dans l&#8217;arborescence.</p>
<h3>3.1. La Vue d&#8217;ajout/édition</h3>

<div class="wp_syntax"><div class="code"><pre class="php"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">// {app}/views/categories/admin_edit.ctp</span>
<span style="color: #000033;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">pageTitle</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Ajouter&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000033;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Category'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000033;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">pageTitle</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Modifier&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000033;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">pageTitle</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot; une catégorie&quot;</span><span style="color: #339933;">;</span>
&nbsp;
e<span style="color: #009900;">&#40;</span><span style="color: #000033;">$form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">create</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Category'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'action'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'edit'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
e<span style="color: #009900;">&#40;</span><span style="color: #000033;">$form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">input</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
<span style="color: #339933;">&lt;</span>fieldset<span style="color: #339933;">&gt;</span>
	<span style="color: #339933;">&lt;</span>legend<span style="color: #339933;">&gt;&lt;</span>?php e<span style="color: #009900;">&#40;</span><span style="color: #000033;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">pageTitle</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> ?<span style="color: #339933;">&gt;&lt;/</span>legend<span style="color: #339933;">&gt;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">&lt;?php</span>
	e<span style="color: #009900;">&#40;</span><span style="color: #000033;">$form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">input</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'parent_id'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'label'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;Parent :&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'empty'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;Racine&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	e<span style="color: #009900;">&#40;</span><span style="color: #000033;">$form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">input</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'libelle'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'label'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;Nom de la catégorie :&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">?&gt;</span>
<span style="color: #339933;">&lt;/</span>fieldset<span style="color: #339933;">&gt;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> e<span style="color: #009900;">&#40;</span><span style="color: #000033;">$form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">end</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Valider'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
<span style="color: #339933;">&lt;</span>p<span style="color: #339933;">&gt;&lt;</span>?php e<span style="color: #009900;">&#40;</span><span style="color: #000033;">$html</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">link</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Liste des catégories&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'index'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> ?<span style="color: #339933;">&gt;&lt;/</span>p<span style="color: #339933;">&gt;</span></pre></div></div>

<h3>3.2. La Vue des catégories</h3>
<p>Nous allons utiliser pour cette Vue un formidable Helper créé par AD7six (Andy Dawson), que nous récupérons ici : <a href="http://bakery.cakephp.org/articles/view/tree-helper-1" class="ico-ext" target="_blank">Tree Helper</a> et recopions dans un nouveau fichier <code>{app}/views/helpers/tree.php</code></p>

<div class="wp_syntax"><div class="code"><pre class="php"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">// {app}/views/categories/admin_index.ctp</span>
<span style="color: #000033;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">pageTitle</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Arborescence des catégories&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #990000;">echo</span> <span style="color: #000033;">$tree</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">generate</span><span style="color: #009900;">&#40;</span>
	<span style="color: #000033;">$categories</span><span style="color: #339933;">,</span>
	<span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'element'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'categories_admin_index'</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>La superbe simplicité de ce Helper réside dans la possibilité d&#8217;utiliser un Elément qui sera appelé pour chaque catégorie de l&#8217;arborescence, le tout entouré par les balises &lt;ul&gt;&#8230;&lt;/ul&gt;. Voyons sans attendre ce fameux élément :</p>

<div class="wp_syntax"><div class="code"><pre class="php"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">// {app}/views/elements/categories_admin_index.ctp</span>
<span style="color: #000033;">$id</span> <span style="color: #339933;">=</span> <span style="color: #000033;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Category'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span>
<span style="color: #339933;">&lt;</span>h4<span style="color: #339933;">&gt;&lt;</span>?php e<span style="color: #009900;">&#40;</span><span style="color: #000033;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Category'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'libelle'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> ?<span style="color: #339933;">&gt;&lt;/</span>h4<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>ul <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;options&quot;</span><span style="color: #339933;">&gt;</span>
	<span style="color: #339933;">&lt;</span>li<span style="color: #339933;">&gt;&lt;</span>?php e<span style="color: #009900;">&#40;</span><span style="color: #000033;">$html</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">link</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Monter'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'move_up/'</span><span style="color: #339933;">.</span><span style="color: #000033;">$id</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> ?<span style="color: #339933;">&gt;&lt;/</span>li<span style="color: #339933;">&gt;</span>
	<span style="color: #339933;">&lt;</span>li<span style="color: #339933;">&gt;&lt;</span>?php e<span style="color: #009900;">&#40;</span><span style="color: #000033;">$html</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">link</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Descendre'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'move_down/'</span><span style="color: #339933;">.</span><span style="color: #000033;">$id</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> ?<span style="color: #339933;">&gt;&lt;/</span>li<span style="color: #339933;">&gt;</span>
	<span style="color: #339933;">&lt;</span>li<span style="color: #339933;">&gt;&lt;</span>?php e<span style="color: #009900;">&#40;</span><span style="color: #000033;">$html</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">link</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Modifier&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'edit/'</span><span style="color: #339933;">.</span><span style="color: #000033;">$id</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> ?<span style="color: #339933;">&gt;&lt;/</span>li<span style="color: #339933;">&gt;</span>
	<span style="color: #339933;">&lt;</span>li<span style="color: #339933;">&gt;&lt;</span>?php e<span style="color: #009900;">&#40;</span><span style="color: #000033;">$html</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">link</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Supprimer&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'delete/'</span><span style="color: #339933;">.</span><span style="color: #000033;">$id</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">null</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;Etes-vous sûr ?&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> ?<span style="color: #339933;">&gt;&lt;/</span>li<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>ul<span style="color: #339933;">&gt;</span></pre></div></div>

<p>Résultat : moyennant quelques petites règles CSS, nous obtenons une belle arborescence sous forme de listes non-ordonnées imbriquées !</p>
<h2>4. Le Contrôleur <code>Categories</code></h2>
<p>Nous terminons par les actions d&#8217;administration dans le Contrôleur :</p>

<div class="wp_syntax"><div class="code"><pre class="php"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">// {app}/controllers/categories_controller.php</span>
<span style="color: #000000; font-weight: bold;">class</span> CategoriesController <span style="color: #000000; font-weight: bold;">extends</span> AppController
<span style="color: #009900;">&#123;</span>
	<span style="color: #666666; font-style: italic;">// Appel du Helper Tree</span>
	<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000033;">$helpers</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Tree'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">/**
	 * Liste des catégories
	 */</span>
	<span style="color: #000000; font-weight: bold;">function</span> admin_index<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000033;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Category</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">recursive</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">-1</span><span style="color: #339933;">;</span>
		<span style="color: #000033;">$categories</span> <span style="color: #339933;">=</span> <span style="color: #000033;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Category</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">children</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000033;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">compact</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'categories'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">/**
	 * Ajout/édition d'une catégorie
	 *
	 * @param int $id Id de la catégorie
	 */</span>
	<span style="color: #000000; font-weight: bold;">function</span> admin_edit<span style="color: #009900;">&#40;</span><span style="color: #000033;">$id</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000033;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #000033;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Category</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set</span><span style="color: #009900;">&#40;</span><span style="color: #000033;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
			<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000033;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Category</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">validates</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
			<span style="color: #009900;">&#123;</span>
				<span style="color: #000033;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Session</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setFlash</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Corrigez les erreurs mentionnées.&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'message_notice'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #b1b100;">return</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
&nbsp;
			<span style="color: #000033;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Category</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">save</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">null</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
			<span style="color: #000033;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Session</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setFlash</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Données enregistrées.&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'message_ok'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000033;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">redirect</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'index'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #000033;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</span> <span style="color: #339933;">=</span> <span style="color: #000033;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Category</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">read</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">null</span><span style="color: #339933;">,</span> <span style="color: #000033;">$id</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000033;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'parents'</span><span style="color: #339933;">,</span> <span style="color: #000033;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Category</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">generatetreelist</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
	<span style="color: #666666; font-style: italic;">/**
	 * Monte une catégorie d'un cran
	 *
	 * @param int $id Id de la catégorie
	 */</span>
	<span style="color: #000000; font-weight: bold;">function</span> admin_move_up<span style="color: #009900;">&#40;</span><span style="color: #000033;">$id</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000033;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Category</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">moveup</span><span style="color: #009900;">&#40;</span><span style="color: #000033;">$id</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #000033;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Session</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setFlash</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;La catégorie ne peut pas aller plus haut.&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'message_notice'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #b1b100;">else</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #000033;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Session</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setFlash</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Ordre mis à jour.&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'message_ok'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #000033;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">redirect</span><span style="color: #009900;">&#40;</span><span style="color: #000033;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">referer</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">/**
	 * Descend une catégorie d'un cran
	 *
	 * @param int $id Id de la catégorie
	 */</span>
	<span style="color: #000000; font-weight: bold;">function</span> admin_move_down<span style="color: #009900;">&#40;</span><span style="color: #000033;">$id</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000033;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Category</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">movedown</span><span style="color: #009900;">&#40;</span><span style="color: #000033;">$id</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #000033;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Session</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setFlash</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;La catégorie ne peut pas aller plus bas.&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'message_notice'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #b1b100;">else</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #000033;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Session</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setFlash</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Ordre mis à jour.&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'message_ok'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #000033;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">redirect</span><span style="color: #009900;">&#40;</span><span style="color: #000033;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">referer</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">/**
	 * Suppression d'une catégorie
	 *
	 * @param int $id Id de la catégorie
	 */</span>
	<span style="color: #000000; font-weight: bold;">function</span> admin_delete<span style="color: #009900;">&#40;</span><span style="color: #000033;">$id</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000033;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Category</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">id</span> <span style="color: #339933;">=</span> <span style="color: #000033;">$id</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000033;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Category</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">exists</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #000033;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Session</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setFlash</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Enregistrement introuvable.&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'message_error'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #b1b100;">else</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #000033;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Category</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">del</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000033;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Session</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setFlash</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Données supprimées.&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'message_ok'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #000033;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">redirect</span><span style="color: #009900;">&#40;</span><span style="color: #000033;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">referer</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Quelques points intéressants :</p>
<ul>
<li><code>admin_index</code> : nous faisons appel à la méthode <code>children</code>, ajoutée par le Comportement, et qui appelée avec le seul argument <code>false</code> renvoie l&#8217;arborescence complète ;</li>
<li><code>admin_edit</code> : rappelons que dans la Vue de cette action, nous affichons la liste déroulante des catégories déjà existantes pour pouvoir définir le parent de la catégorie en train d&#8217;être créée ou modifiée. Pour obtenir cette liste, nous faisons appel à la méthode <code>generatetreelist</code>, ajoutée par le Comportement Tree au Modèle Category. Dans la Vue d&#8217;ajout/édition, l&#8217;arborescence pour le choix du parent sera sommairement représentée par des underscores _ :
<ul style="list-style:none;">
<li>Racine (ajouté dans la définition du &lt;select&gt;)</li>
<li>Catégorie 1
<ul style="list-style:none;">
<li>_Catégorie 1.1</li>
<li>_Catégorie 1.2
<ul style="list-style:none;">
<li>__Catégorie 1.2.1</li>
</ul>
</li>
</ul>
</li>
</ul>
<p>Notons l&#8217;utilisation d&#8217;un petit raccourci qui fait aimer CakePHP : une fois que nous avons passé la liste des parents à la vue avec <code>$this->set('parents',...)</code>, il n&#8217;est même pas besoin de le dire dans la définition du &lt;select&gt;, la méthode <code>$form->input('parent_id')</code> s&#8217;en charge !</li>
<li><code>admin_move_up</code> et <code>admin_move_down</code> : ces deux petites actions font appel là encore à deux méthodes ajoutées automatiquement par le Comportement : <code>moveup</code> et <code>movedown</code> qui permettent de modifier l&#8217;ordre des catégories partageant le même parent.</li>
<li><code>admin_delete</code> : nous procédons exactement comme avec n&#8217;importe quelle suppression, le Comportement se charge seul de mettre l&#8217;arborescence à jour.</li>
</ul>
<p>Nous verrons très prochainement comme présenter une telle arborescence, tout d&#8217;abord de manière <a href="/102/presentation-statique-arborescence">statique</a> avec l&#8217;affichage des catégories filles au fur et à mesure de la progression dans l&#8217;arbre, puis de manière <a href="/116/presentation-dynamique-arborescence-jquery">dynamique</a> où la navigation dans l&#8217;arbre se fera en javascript avec jQuery, sans recharger la page.</p>
<img src="http://feeds.feedburner.com/~r/FormationCakephp/~4/378440351" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.formation-cakephp.com/94/arborescence-mptt-tree-behavior/feed</wfw:commentRss>
		<feedburner:awareness xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0">http://api.feedburner.com/awareness/1.0/GetItemData?uri=FormationCakephp&amp;itemurl=http%3A%2F%2Fwww.formation-cakephp.com%2F94%2Farborescence-mptt-tree-behavior</feedburner:awareness></item>
		<item>
		<title>Insérer proprement une animation Flash avec SWFObject</title>
		<link>http://www.formation-cakephp.com/80/helper-flash-swfobject</link>
		<comments>http://www.formation-cakephp.com/80/helper-flash-swfobject#comments</comments>
		<pubDate>Wed, 27 Aug 2008 17:49:36 +0000</pubDate>
		<dc:creator>Pierre-Emmanuel Fringant</dc:creator>
		
		<category><![CDATA[Extension]]></category>

		<category><![CDATA[Flash]]></category>

		<category><![CDATA[helper]]></category>

		<guid isPermaLink="false">http://www.formation-cakephp.com/?p=80</guid>
		<description><![CDATA[Insérer proprement une animation Flash dans une Vue n&#8217;est pas simple. Il existe heureusement un outil qui peut prendre en charge cette tâche délicate, SWFObject. Gwoo, développeur de la core team de CakePHP, avait il y a bien longtemps proposé un Helper pour intégrer facilement SWFObject dans Cake. Nous proposons ici au lecteur un Helper [...]]]></description>
			<content:encoded><![CDATA[<p>Insérer proprement une animation Flash dans une Vue n&#8217;est pas simple. Il existe heureusement un outil qui peut prendre en charge cette tâche délicate, <a href="http://code.google.com/p/swfobject/" class="ico-ext" target="_blank">SWFObject</a>. Gwoo, développeur de la core team de CakePHP, avait <a href="http://bin.cakephp.org/saved/1033" class="ico-ext" target="_blank">il y a bien longtemps</a> proposé un Helper pour intégrer facilement SWFObject dans Cake. Nous proposons ici au lecteur un Helper optimisé et mis à jour pour convenir à la dernière version de SWFObject.<span id="more-80"></span></p>
<h2>1. Pré-requis</h2>
<p>Pour faire fonctionner le Helper, nous devons commencer par télécharger la dernière version de SWFObject : <a href="http://code.google.com/p/swfobject/" class="ico-ext" target="_blank">Télécharger SWFObject</a><br />
Nous récupérons une archive dans laquelle nous trouvons le fichier <code>swfobject.js</code> à placer dans <code>{app}/www/js/</code>, et le fichier <code>expressInstall.swf</code> à placer où bon nous semble dans <code>{app}/www/</code> (nous choisissons de le placer dans <code>{app}/www/img/</code>).</p>
<h2>2. SwfobjectHelper</h2>
<p>Voici sans plus tarder le Helper Swfobject, à recopier dans un fichier <code>{app}/views/helpers/swfobject.php</code> :</p>

<div class="wp_syntax"><div class="code"><pre class="php"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">/**
 * Insertion d'une animation Flash avec SWFObject
 * http://code.google.com/p/swfobject/
 */</span>
<span style="color: #000000; font-weight: bold;">class</span> SwfobjectHelper <span style="color: #000000; font-weight: bold;">extends</span> Helper
<span style="color: #009900;">&#123;</span>
<span style="color: #666666; font-style: italic;">/**
* Swfobject Flash vars.
*
* @var array
*/</span>
    <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000033;">$__flashvars</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">/**
* Swfobject params.
*
* @var array
*/</span>
    <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000033;">$__swfparams</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">/**
* Swfobject Attributes.
* 
* @var array
*/</span>
    <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000033;">$__attributes</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/**
* Options par défaut
*
* @var array
*/</span>
    <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000033;">$__baseOptions</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
    	<span style="color: #0000ff;">'width'</span>   <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'640'</span><span style="color: #339933;">,</span>
    	<span style="color: #0000ff;">'height'</span>  <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'480'</span><span style="color: #339933;">,</span>
    	<span style="color: #0000ff;">'version'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'9.0.0'</span><span style="color: #339933;">,</span>
    	<span style="color: #0000ff;">'express'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'expressInstall.swf'</span>
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  
&nbsp;
<span style="color: #666666; font-style: italic;">/**
* Création de l'objet Flash
*
* @param string $path Chemin vers l'animation Flash
* @param string $id Id de l'élément HTML qui sera remplacé par l'animation
* @param array $options
* @return Code HTML à insérer entre &lt;head&gt;...&lt;/head&gt;
*/</span>
	<span style="color: #000000; font-weight: bold;">function</span> create<span style="color: #009900;">&#40;</span><span style="color: #000033;">$path</span><span style="color: #339933;">,</span> <span style="color: #000033;">$id</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'flashcontent'</span><span style="color: #339933;">,</span> <span style="color: #000033;">$options</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000033;">$options</span> <span style="color: #339933;">=</span> am<span style="color: #009900;">&#40;</span><span style="color: #000033;">$this</span><span style="color: #339933;">-&gt;</span>__baseOptions<span style="color: #339933;">,</span> <span style="color: #000033;">$options</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000033;">$out</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'&lt;script type=&quot;text/javascript&quot;&gt;'</span><span style="color: #339933;">;</span>
		<span style="color: #000033;">$out</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// Flashvars</span>
		<span style="color: #000033;">$out</span> <span style="color: #339933;">.=</span> <span style="color: #000033;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">javascriptObject</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'flashvars'</span><span style="color: #339933;">,</span> <span style="color: #000033;">$this</span><span style="color: #339933;">-&gt;</span>__flashvars<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// Params</span>
		<span style="color: #000033;">$out</span> <span style="color: #339933;">.=</span> <span style="color: #000033;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">javascriptObject</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'params'</span><span style="color: #339933;">,</span> <span style="color: #000033;">$this</span><span style="color: