<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	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"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Blog Symfony - Lexik Montpellier &#187; Ajax</title>
	<atom:link href="http://www.lexik.fr/blog/symfony/tag/ajax/feed" rel="self" type="application/rss+xml" />
	<link>http://www.lexik.fr/blog/symfony</link>
	<description>Blog sur le développement Web PHP</description>
	<lastBuildDate>Tue, 20 Jul 2010 08:35:36 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Liste d&#8217;objets ordonnable via AJAX partie 1</title>
		<link>http://www.lexik.fr/blog/symfony/symfony/liste-dobjets-ordonnable-via-ajax-partie-1-870</link>
		<comments>http://www.lexik.fr/blog/symfony/symfony/liste-dobjets-ordonnable-via-ajax-partie-1-870#comments</comments>
		<pubDate>Wed, 12 Aug 2009 13:07:48 +0000</pubDate>
		<dc:creator>Nikaw</dc:creator>
				<category><![CDATA[1.2.x]]></category>
		<category><![CDATA[Symfony]]></category>
		<category><![CDATA[Ajax]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[liste]]></category>
		<category><![CDATA[sortable]]></category>

		<guid isPermaLink="false">http://www.lexik.fr/sfblog/?p=870</guid>
		<description><![CDATA[Aujourd&#8217;hui, voyons les fonctions sortables permettant de changer la position d&#8217;objet, de les ordonner et de sauvegarder l&#8217;ordre. C&#8217;est toujours pratique pour organiser une galerie photo, ou des éléments dans votre site internet. Toutes les informations sur l&#8217;objet que nous souhaitons ordonner et/ou classer seront stockées en base : schema.yml ?View Code YAMLItem: columns: name: [...]]]></description>
			<content:encoded><![CDATA[<p>Aujourd&#8217;hui, voyons les fonctions sortables permettant de changer la position d&#8217;objet, de les ordonner et de sauvegarder l&#8217;ordre. C&#8217;est toujours pratique pour organiser une galerie photo, ou des éléments dans votre site internet.</p>
<p>Toutes les informations sur l&#8217;objet que nous souhaitons ordonner et/ou classer seront stockées en base :<br />
<em>schema.yml</em></p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p870code11'); return false;">View Code</a> YAML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p87011"><td class="code" id="p870code11"><pre class="yaml" style="font-family:monospace;">Item:
  columns:
    name:
      type: string(255)
    rank:
      type: integer
      notnull: true
      unique: true</pre></td></tr></table></div>

<p><span id="more-870"></span><br />
L&#8217;attribut &#8216;rank&#8217; étant notnull, il faut forcément qu&#8217;il soit définit lors du save(), et étant unique on ne peut pas le définir par défaut.<br />
Il faut donc surcharger la méthode save() :<br />
<em>Item.class.php</em></p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p870code12'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p87012"><td class="code" id="p870code12"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> save<span style="color: #009900;">&#40;</span>Doctrine_Connection <span style="color: #000088;">$con</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;">// les nouveaux enregistrements seront ajoutés à la suite, donc avec un rang élévé de 1 par rapport au rang max</span>
   <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">isNew</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
   <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setRank</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getTable</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getMaxRank</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #b1b100;">return</span> parent<span style="color: #339933;">::</span><span style="color: #004000;">save</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$con</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Il faut aussi surchager la méthode delete() pour redescendre tous les éléments de rang supérieur lors de la suppression :<br />
<em>Item.class.php</em></p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p870code13'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p87013"><td class="code" id="p870code13"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> delete<span style="color: #009900;">&#40;</span>Doctrine_Connection <span style="color: #000088;">$con</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;">//tous les enregistrements au dessus descendent d'une place</span>
  <span style="color: #000088;">$items</span> <span style="color: #339933;">=</span> Doctrine_Query<span style="color: #339933;">-&gt;</span><span style="color: #004000;">create</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">from</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Item'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">where</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'rank &gt; ?'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getRank</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">execute</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;">// delete the item</span>
   <span style="color: #000088;">$ret</span> <span style="color: #339933;">=</span> parent<span style="color: #339933;">::</span><span style="color: #004000;">delete</span><span style="color: #009900;">&#40;</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: #000088;">$items</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$item</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$item</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setRank</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$item</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getRank</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$item</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">save</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #000088;">$ret</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Nous avons donc besoin d&#8217;une méthode permettant de récupérer le rang max :<br />
<em>ItemTable.class.php</em></p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p870code14'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p87014"><td class="code" id="p870code14"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">//permet de récupérer le rang le plus haut</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getMaxRank<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> Doctrine_Query<span style="color: #339933;">::</span><span style="color: #004000;">create</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
        <span style="color: #339933;">-&gt;</span><span style="color: #004000;">select</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'MAX(rank)'</span><span style="color: #009900;">&#41;</span>
        <span style="color: #339933;">-&gt;</span><span style="color: #004000;">from</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Item i'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$tmp</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$query</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>Doctrine<span style="color: #339933;">::</span><span style="color: #004000;">HYDRATE_NONE</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$tmp</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Ici on passe en paramètre Doctrine::HYDRATE_NONE pour que la méthode ne retourne pas une collection. Le retour est un tableau à 2 entrées, mais la requête ne retournant qu&#8217;un résultat, il est disponible en [0][0].</p>
<p><strong>Passons maintenant à la vue.</strong><br />
Il s&#8217;agit d&#8217;afficher la liste des items, tout simplement.<br />
Mais pour les afficher, il faut les charger, voyons donc d&#8217;abord l&#8217;action :<br />
<em>actions.class.php</em></p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p870code15'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p87015"><td class="code" id="p870code15"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> executeIndex<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">items</span> <span style="color: #339933;">=</span> Doctrine<span style="color: #339933;">::</span><span style="color: #004000;">getTable</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Item'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getAllOrderedByRank</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Un coup d&#8217;oeil tout de suite sur la méthode getAllOrderedByRank() :<br />
<em>ItemTable.class.php</em></p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p870code16'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p87016"><td class="code" id="p870code16"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getAllOrderedByRank<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> Doctrine_Query<span style="color: #339933;">::</span><span style="color: #004000;">create</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
        <span style="color: #339933;">-&gt;</span><span style="color: #004000;">from</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Item'</span><span style="color: #009900;">&#41;</span>
        <span style="color: #339933;">-&gt;</span><span style="color: #004000;">orderBy</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'rank ASC'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$query</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Maintenant voyons la vue :<br />
<em>indexSuccess.php</em></p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p870code17'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p87017"><td class="code" id="p870code17"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>h1<span style="color: #339933;">&gt;</span>Ordered <span style="color: #990000;">list</span> of items<span style="color: #339933;">&lt;/</span>h1<span style="color: #339933;">&gt;</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: #000088;">$items</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$item</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 id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&lt;?php echo <span style="color: #006699; font-weight: bold;">$item-&gt;getId</span>() ?&gt;&quot;</span><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: #000088;">$item</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</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: #000000; font-weight: bold;">?&gt;</span>
<span style="color: #339933;">&lt;/</span>ul<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>Passons maintenant aux choses sérieuses.<br />
Le framework Javascript JQuery comporte des fonctions permettant de créer des liste dont les éléments sont &laquo;&nbsp;drag n&#8217; droppables&nbsp;&raquo;, ici c&#8217;est celles ci que l&#8217;on va utiliser.<br />
Pour cela, il faut modifier la vue, afin de rajouter le script jquery, et de spécifier une id à la liste, id que le script jquery utilisera pour cibler la liste à ordonner :</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p870code18'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p87018"><td class="code" id="p870code18"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>ul id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;order&quot;</span><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: #000088;">$items</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$item</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 id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&lt;?php echo <span style="color: #006699; font-weight: bold;">$item-&gt;getId</span>() ?&gt;&quot;</span><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: #000088;">$item</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</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: #000000; font-weight: bold;">?&gt;</span>
<span style="color: #339933;">&lt;/</span>ul<span style="color: #339933;">&gt;</span>
&nbsp;
<span style="color: #339933;">&lt;</span>input type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;button&quot;</span> value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Enregistrer&quot;</span> onClick<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;submitOrder()&quot;</span><span style="color: #339933;">/&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>
  $<span style="color: #009900;">&#40;</span><span style="color: #000000; 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: #666666; font-style: italic;">//on spécifie la liste</span>
    $<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;#order&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span>sortable<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    $<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;#order&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span>disableSelection<span style="color: #009900;">&#40;</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>
&nbsp;
  <span style="color: #000000; font-weight: bold;">function</span> submitOrder<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">//initialisation de la chaine contenant les id des items</span>
    <span style="color: #000000; font-weight: bold;">var</span> elements <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//parcours de la liste d'items</span>
    $<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'ul#order li'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #990000;">each</span><span style="color: #009900;">&#40;</span><span style="color: #000000; 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: #666666; font-style: italic;">//pour chaque item, on concatene l'id dans la chaine</span>
      elements <span style="color: #339933;">+=</span> $<span style="color: #009900;">&#40;</span>this<span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span>attr<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #0000ff;">&quot;,&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #666666; font-style: italic;">//on execute l'action, a laquelle on envoie en param la liste des id</span>
  $<span style="color: #339933;">.</span>post<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'&lt;?php echo url_for('</span><span style="color: #339933;">@</span>order<span style="color: #0000ff;">') ?&gt;'</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span> elements<span style="color: #339933;">:</span> elements<span style="color: #339933;">.</span><span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> elements<span style="color: #339933;">.</span>length<span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</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: #000000; font-weight: bold;">&lt;/script&gt;</span></pre></td></tr></table></div>

<p>Vous aurez noté l&#8217;ajout du bouton submit. C&#8217;est en cliquant sur celui-ci que l&#8217;on déclenche l&#8217;action qui sauvegarde le nouvel ordre des items.<br />
Pour enregistrer l&#8217;ordre des items, chaque
<li> a pour id l&#8217;id de l&#8217;item en question.<br />
Lors du click on exécute un script qui récupère toutes les id dans l&#8217;ordre et qui les concatène dans une chaîne de caractères en les séparant par des &laquo;&nbsp;,&nbsp;&raquo;. Cette chaîne est envoyée en paramètre à l&#8217;action.<br />
Routing :<br />
<em>routing.yml</em></p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p870code19'); return false;">View Code</a> YAML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p87019"><td class="code" id="p870code19"><pre class="yaml" style="font-family:monospace;">order:
  url:      /order-item
  params:  { module: item, action: order }</pre></td></tr></table></div>

<p>L&#8217;action :<br />
<em>actions.class.php</em></p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p870code20'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p87020"><td class="code" id="p870code20"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> executeOrder<span style="color: #009900;">&#40;</span>sfWebRequest <span style="color: #000088;">$request</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">//on récupère les id, que l'on place dans un tableau pour un parcours plus aisé</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">elements</span> <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;,&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getParameter</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'elements'</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;">//on parcours les id, on récupère l'item correspondant, et on met son rang à jour</span>
    <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">elements</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$key</span><span style="color: #339933;">=&gt;</span><span style="color: #000088;">$element_id</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$item</span> <span style="color: #339933;">=</span> Doctrine<span style="color: #339933;">::</span><span style="color: #004000;">getTable</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Item'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">find</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$element_id</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$item</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setRank</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$key</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$item</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">save</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">items</span> <span style="color: #339933;">=</span> Doctrine<span style="color: #339933;">::</span><span style="color: #004000;">getTable</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Item'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getAllByRank</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;">//on affiche à nouveau la liste des items, mis à jour</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setTemplate</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></pre></td></tr></table></div>

<p>Voilà, vous aurez noté que les nouveaux rangs sont tout simplement la position dans la liste, partant de 0.</p>
<p>Dans la prochaine partie, nous verront comment implanter cette fonctionnalité dans votre backend avec l&#8217;admin generator</p>
]]></content:encoded>
			<wfw:commentRss>http://www.lexik.fr/blog/symfony/symfony/liste-dobjets-ordonnable-via-ajax-partie-1-870/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Listes déroulantes mises à jours en Ajax avec jQuery</title>
		<link>http://www.lexik.fr/blog/symfony/symfony/listes-deroulantes-mises-a-jours-en-ajax-avec-jquery-704</link>
		<comments>http://www.lexik.fr/blog/symfony/symfony/listes-deroulantes-mises-a-jours-en-ajax-avec-jquery-704#comments</comments>
		<pubDate>Wed, 17 Jun 2009 08:49:39 +0000</pubDate>
		<dc:creator>olivier</dc:creator>
				<category><![CDATA[1.2.x]]></category>
		<category><![CDATA[Ajax]]></category>
		<category><![CDATA[Symfony]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Liste déroulante]]></category>

		<guid isPermaLink="false">http://www.lexik.fr/sfblog/?p=704</guid>
		<description><![CDATA[Un petit TIPS pour faire des listes déroulantes liées qui s&#8217;enrichissent en fonction de la valeur choisie sur l&#8217;une d&#8217;entre elles. (J&#8217;ai l&#8217;impression que la phrase est très compliqué pour pas grand chose ^^) Le tout en Ajax en utilisant jQuery. Dans mon exemple, ça sera tout simplement un formulaire Lambda avec des listes déroulante [...]]]></description>
			<content:encoded><![CDATA[<p>Un petit TIPS pour faire des listes déroulantes liées qui s&#8217;enrichissent en fonction de la valeur choisie sur l&#8217;une d&#8217;entre elles.<br />
(J&#8217;ai l&#8217;impression que la phrase est très compliqué pour pas grand chose ^^)<br />
Le tout en Ajax en utilisant jQuery.<br />
<span id="more-704"></span><br />
Dans mon exemple, ça sera tout simplement un formulaire Lambda avec des listes déroulante pour les Régions et les Départements.<br />
Le fonctionnement est le suivant. Par défaut les listes déroulantes sont initialisées avec toutes les valeurs. Dés que l&#8217;on choisi une valeur pour la région, la liste déroulante des département affine sa liste pour ne proposer que les départements de la région saisie.</p>
<p>Vite fait, le schéma utilisé :</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p704code26'); return false;">View Code</a> YML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p70426"><td class="code" id="p704code26"><pre class="yml" style="font-family:monospace;">...
Region:
  tableName:  region
  actAs:
    Sluggable:
      unique: true
      fields: [nom]
  columns:
    id:       { type: integer(4), unsigned: true, primary: true, autoincrement: true }
    nom:      { type: string(255), notnull: true }
&nbsp;
Departement:
  tableName: departement
  actAs:
    Sluggable:
      unique: true
      fields: [nom]
  columns:
    id:        { type: integer(4), unsigned: true, primary: true, autoincrement: true }
    nom:       { type: string(255), notnull: true }
    region_id: { type: integer(4), unsigned: true }
  relations:
    Region:
      local:        region_id
      foreign:      id
      foreignAlias: Departements
      onDelete:     CASCADE
...</pre></td></tr></table></div>

<p>Juste pour mettre en évidence la relation entre le Département et la Région, avec le très important le foreignAlias que j&#8217;aime tant <img src='http://www.lexik.fr/blog/symfony/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> </p>
<p>Pour faire fonctionner tout ça on va avoir besoin de :<br />
 &#8211; 1 route<br />
 &#8211; 1 action minuscule<br />
 &#8211; 1 partial de 4 lignes<br />
 &#8211; 1 tout petit bout de javascript</p>
<p>La route :</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p704code27'); return false;">View Code</a> JAVASCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p70427"><td class="code" id="p704code27"><pre class="javascript" style="font-family:monospace;"># routing.<span style="color: #660066;">yml</span>
...
<span style="color: #660066;">ajax_departement</span><span style="color: #339933;">:</span>
  url<span style="color: #339933;">:</span>   <span style="color: #339933;">/</span>ajax<span style="color: #339933;">/</span>departement
  param<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span> module<span style="color: #339933;">:</span> lambda<span style="color: #339933;">,</span> action<span style="color: #339933;">:</span> ajaxDepartement <span style="color: #009900;">&#125;</span>
...</pre></td></tr></table></div>

<p>L&#8217;action :</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p704code28'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p70428"><td class="code" id="p704code28"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// actions.class.php</span>
&nbsp;
  <span style="color: #009933; font-style: italic;">/**
   * requete ajax pour avoir la liste des déparements de la région
   *
   * @param sfWebRequest $request
   */</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> executeAjaxDepartement<span style="color: #009900;">&#40;</span>sfWebRequest <span style="color: #000088;">$request</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$region</span> <span style="color: #339933;">=</span> Doctrine<span style="color: #339933;">::</span><span style="color: #004000;">getTable</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Region'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">findOneById</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getParameter</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'region'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">renderPartial</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'lambda/selectDepartement'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'region'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$region</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></td></tr></table></div>

<p>Le partial :</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p704code29'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p70429"><td class="code" id="p704code29"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// _selectDepartement.php</span>
<span style="color: #339933;">&lt;</span>option value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">&gt;&lt;/</span>option<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: #000088;">$region</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getDepartements</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$departement</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>option value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&lt;?php echo <span style="color: #006699; font-weight: bold;">$departement-&gt;getId</span>() ?&gt;&quot;</span><span style="color: #339933;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;?</span>php <span style="color: #990000;">echo</span> <span style="color: #000088;">$departement</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getNom</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span><span style="color: #339933;">&lt;/</span>option<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></pre></td></tr></table></div>

<p>Et enfin le petit bout de code jQuery à rajouter au formulaire :</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p704code30'); return false;">View Code</a> JAVASCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p70430"><td class="code" id="p704code30"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>script language<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;javascript&quot;</span><span style="color: #339933;">&gt;</span>
$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">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;">&quot;#mon_formulaire_lambda_region_id&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">change</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: #660066;">post</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;/ajax/departement&quot;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span> region<span style="color: #339933;">:</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
    <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
      $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#mon_formulaire_lambda_departement_id&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span>data<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>
  <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>
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>Pour le javascript plusieurs solutions. Soit l&#8217;inclure directement dans la vue avec les balises <script></script> comme je l&#8217;ai fait ici.<br />
Soit faire un fichier .js que l&#8217;on met dans /web/js/mon_fichier.js (bien mais pas top <img src='http://www.lexik.fr/blog/symfony/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /> ).</p>
<p>Le plus &laquo;&nbsp;propre&nbsp;&raquo; est de faire une vue et d&#8217;utiliser le sf_format et d&#8217;inclure le fichier via une route.<br />
On verra ça lors d&#8217;un prochain post <img src='http://www.lexik.fr/blog/symfony/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>@bientôt !</p>
<p>NB : Bien entendu il faut que jQuery soi inclus :<br />
 &#8211; Soit il est inclus dans le view.yml<br />
 &#8211; Soit directement dans la vue avec un use_javascript(&#8216;jQuery.js&#8217;)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.lexik.fr/blog/symfony/symfony/listes-deroulantes-mises-a-jours-en-ajax-avec-jquery-704/feed</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Petit système de vote en AJAX</title>
		<link>http://www.lexik.fr/blog/symfony/symfony/petit-systeme-de-vote-en-ajax-590</link>
		<comments>http://www.lexik.fr/blog/symfony/symfony/petit-systeme-de-vote-en-ajax-590#comments</comments>
		<pubDate>Tue, 21 Apr 2009 07:59:48 +0000</pubDate>
		<dc:creator>Nikaw</dc:creator>
				<category><![CDATA[1.2.x]]></category>
		<category><![CDATA[Symfony]]></category>
		<category><![CDATA[Ajax]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[votes]]></category>

		<guid isPermaLink="false">http://www.lexik.fr/sfblog/?p=590</guid>
		<description><![CDATA[sytème de vote type "j'aime" / "j'aime pas" avec JQuery sur Symfony avec limitation de vote par utilisateur et par message pour un temps donné]]></description>
			<content:encoded><![CDATA[<p>Le système que je vous propose est assez simpliste : il s&#8217;agit d&#8217;un vote type &laquo;&nbsp;j&#8217;aime / j&#8217;aime pas&nbsp;&raquo;.</p>
<p>On dispose d&#8217;un ensemble d&#8217;éléments (ici des messages) que l&#8217;on nous propose de noter.</p>
<p>Dans une première étape, le principe est de créer le système de vote, dans une deuxième étape on limitera les votes : un visiteur ne peut voter sur un élément qu&#8217;une fois par jour.<br />
(Attention, ne pas pas oublier d&#8217;ajouter les include et use_javascript dans le layout de votre appli)<br />
<span id="more-590"></span><br />
<strong>Etape 1 :</strong></p>
<p><strong>Schéma :</strong></p>
<pre>
Message:
  columns:
    text:
      type: string(255)
    relations:
      Ratings:
        type: many
        class: Rating
        local: id
        foreign: message_id

Rating:
  actAs: { Timestampable: ~ } //permet d'avoir les champs "created_at" et "updated_at", pour limiter le nombre de vote à 1 par jour
  columns:
    value:
      type: boolean
    message_id:
      type: integer
  relations:
    Message:
      onDelete: CASCADE
      local: message_id
      foreign: id
</pre>
<p>On a donc une table contenant nos messages (ici on n&#8217;a besoin que du texte) et une table contenant les votes : un vote est donc décrit par une id, et un message, donc à chaque click un nouveau vote est inséré dans la base de données.</p>
<p>Pour éviter de se retrouver avec une BDD sur-remplie, on pourra rajouter une 3° étape qui consiste à créer une tâche qui compile chaque vote sur un message pour en faire un résumé (qui fait la somme par exemple) et qui ensuite supprime les votes compilés.</p>
<p>Bref, les fonctionnalités utiles maintenant sont : afficher la note du message, afficher le nombre de votes (pour pouvoir faire la différence entre un message qui a une bonne note mais 3 votes et un message qui a une bonne note mais 1000 votes, par exemple), et afficher les liens permettant de voter, tout ça à coté du message en question, bien évidemment.</p>
<p><strong>Voici donc le code de l&#8217;index des message</strong> (apps/*votre appli*/modules/message/templates/indexSuccess.php)</p>
<pre>
<h1>Message List</h1>

<!-- div ou l'on affiche la note et le nombre de votes du message, c'est cette div qui sera actualiser en AJAX -->
&lt;div id ="ratinggetId() ?&gt;"&gt;
  <!-- l'id de la div contien l'id du message, pour spécifier au moment du vote quelle div précise il faut actualiser -->
   $message)) ?&gt;
</div>

<!-- div ou l'on affiche les liens du vote, + et - -->
<div class="rating_links">
   'rating'.$message-&gt;getId(),
                          'url'      =&gt; '@message_vote_plus?id='.$message-&gt;getId()
    )); ?&gt;
   'rating'.$message-&gt;getId(),
                  'url'      =&gt; '@message_vote_moins?id='.$message-&gt;getId()
    )); ?&gt;
</div>

<!-- div du texte du message -->
<div class="text" style=" margin-bottom: 15px;">
  gettext() ?&gt;
</div>
</pre>
<p><strong>Partial message/rating</strong> (apps/*votre appli*/module/template/_rating.php) :</p>
<pre>
Note : getGlobalRating(); ?&gt;/,
Nombre de votes : getRatingsCount() ?&gt;
</pre>
<p>La syntaxe utilisée ici pour les fonction link_to_remote est :<br />
link_to_remote(&#8216;texte à afficher pour le lien&#8217;, tableau des options(&#8216;update&#8217; =&gt; id de la div a update, &#8216;url&#8217; =&gt; adresse de l&#8217;action))</p>
<p>Vous noterez aussi que les url des link_to_remote utilisent des routes,<strong> voici les routes utilisées</strong>(apps/*votre appli*/config/routes.yml) :</p>
<pre>
message_vote_plus:
  url:      /message-vote-plus/:id
  param:    { module: message, action: votePlus }

message_vote_moins:
  url:      /message-vote-moins/:id
  param:    { module: message, action: voteMoins }
</pre>
<p>Un click sur un lien va donc engendrer l&#8217;action votePlus ou voteMoins de l&#8217;objet message en question, spécifié par l&#8217;id.<br />
<strong>Voici les actions</strong> (apps/*votre appli*/modules/message/actions.class.php) :</p>
<pre>
  //execute le vote "j'aime"
  public function executeVotePlus(sfWebRequest $request)
  {
    //on récupère le message courant
    $this-&gt;message = Doctrine::getTable('Message')-&gt;find($request-&gt;getParameter('id'));
    //on effectue le vote positif
    $this-&gt;message-&gt;votePlus();
    //on génère le partial du message, c'est ce résultat qui sera utilisé par le link_to_remote pour mettre à jour la div spécifiée, cela évite également le passage par le template votePlusSuccess.php (qui donc n'est pas à créer)
    return $this-&gt;renderPartial('message/rating',array('message' =&gt; $this-&gt;message));
  }

  //execute le vote "j'aime pas"
  public function executeVoteMoins(sfWebRequest $request)
  {
    //on récupère le message courant
    $this-&gt;message = Doctrine::getTable('Message')-&gt;find($request-&gt;getParameter('id'));
    //on effectue le vote négatif
    $this-&gt;message-&gt;voteMoins();
    //on génère le partial du message, c'est ce résultat qui sera utilisé par le link_to_remote pour mettre à jour la div spécifiée, cela évite également le passage par le template voteMoinsSuccess.php (qui donc n'est pas à créer)
    return $this-&gt;renderPartial('message/rating',array('message' =&gt; $this-&gt;message));
  }
</pre>
<p>L&#8217;action utilise donc naturellement des méthodes du controlleur (lib/model/doctrine/Message.class.php) :<br />
<strong>-récupération du nombres de vote sur 1 message :</strong></p>
<pre>
  //retourne le nombre de votes sur ce message
  public function getRatingsCount()
  {
    return Doctrine::getTable('Rating')-&gt;getMessageRatingsQuery($this-&gt;getId())-&gt;count();
  }
</pre>
<p><strong>-récupération de la note d&#8217;un message :</strong></p>
<pre>
  //retourne la note globale du message courant
  public function getGlobalRating()
  {
    //on récupère tous les votes sur ce message
    $ratings = Doctrine::getTable('Rating')-&gt;getMessageRatingsQuery($this-&gt;getId())-&gt;execute();
    //on initialise la note a 0
    $note = 0;
    //pour chaque vote positif on incrémente la note (rappel : la note est stockée sous forme de boolean dans la base)
    foreach ($ratings as $rating)
    {
      if ($rating-&gt;getValue())
      {
        $note++;
      }
    }
    //on récupère le nombre de votes, pour calculer la moyenne
    $nbvotes = $this-&gt;getRatingsCount();
    //si il n'y pas encore eu de vote sur ce message, on est obligé de donner la valeur 1 à nbvotes, pour éviter une division par 0
    if($nbvotes == 0){ $nbvotes = 1; }

    //on retourne la moyenne : (note/nb_votes)*"valeur sur laquelle on rapporte la note" et on arrondi (round)
    //la valeur sur laqelle on rapporte la note est stockée dans la config (app.yml), ainsi que le nombre de chiffres apres la virgule
    return round(($note/$nbvotes)*sfConfig::get('app_moyenne_sur'), sfConfig::get('app_round'));
  }
</pre>
<p><strong>-votes :</strong></p>
<pre>
  //ajoute un vote positif relatif à ce message
  public function votePlus()
  {
    return $this-&gt;vote('1');
  }

  //ajoute un vote négatif relatif à ce message
  public function voteMoins()
  {
    return $this-&gt;vote('0');
  }

  //effectur le vote
  public function vote($value)
  {
    $rating = new Rating(); //nouveau vote
    //on set ses champs
    $rating-&gt;setMessageId($this-&gt;getId()); //message_id
    $rating-&gt;setValue($value); //value
    $rating-&gt;save(); //on l'enregistre dans la base
  }
</pre>
<p>Voilà pour la première étape, le système de votes est opérationnel, reste plus qu&#8217;à limiter les votes.</p>
<p><strong>Etape 2</strong><br />
De quoi a-t-on besoin pour la suite ?<br />
1) Modifier le schéma pour ajouter l&#8217;attribut ip_user, correspondant à l&#8217;adresse ip de l&#8217;utilisateur<br />
2) De vérifier au moment du vote dans le modèle, s&#8217;il existe déjà un vote récent pour ce message et pour cette adresse ip<br />
3) Donc, on a aussi besoin d&#8217;une méthode dans la classe RatingTable.class.php qui récupère les votes récents<br />
4) On ajoutera également un message à l&#8217;utilisateur pour le remercier de son vote ou lui indiquer qu&#8217;il n&#8217;a plus le droit de voter.</p>
<p><strong>Mise à jour de la table Rating dans le schéma : </strong></p>
<pre>
Rating:
  actAs: { Timestampable: ~ }
  columns:
    value:
      type: boolean
    message_id:
      type: integer
    user_ip:
      type: string(12)
      notnull: true
  relations:
    Message:
      onDelete: CASCADE
      local: message_id
      foreign: id
</pre>
<p><strong>1) Modification de la méthode vote()</strong></p>
<pre>
public function vote(sfWebRequest $request, $value)
  {
    //on récupère l'adresse ip du visiteur/utilisateur
    $user_ip = $request-&gt;getRemoteAddress();
    //on récupère les votes effectués par cet utilisateur sur ce message
    $recent_rating = Doctrine::getTable('Rating')-&gt;getRecentRating(array('user_ip' =&gt; $user_ip, 'message_id' =&gt; $this-&gt;getId()));
    //si il n'existe pas de vote trop récent
    if(!($recent_rating-&gt;count() &gt; 0))
    {
      $rating = new Rating(); //nouveau vote
      //on set ses champs
      $rating-&gt;setUserIp($user_ip); //user_ip
      $rating-&gt;setMessageId($this-&gt;getId()); //message_id
      $rating-&gt;setValue($value); //value
      $rating-&gt;save(); //on l'enregistre dans la base et on retourne vrai
      return true;
    }
    else //si le vote est trop récent, pas de vote, on retourne faux
    {
      return false;
    }
  }
</pre>
<p><strong>Ce qui implique évidemment la création de la méthode getRecentRating() (dans lib/model/doctrine/RatingTable.class.php) :</strong></p>
<pre>
  //retourne les votes récents dans la query
  public function getRecentRating($params, Doctrine_Query $query = NULL)
  {
    if(is_null($query)) //si pas de requete, on récupère tous les votes de la base
    {
      $query = $this-&gt;createQuery('c');
    }
    //on récupère tous les votes faits avec cette ip
    $query = $this-&gt;getRatingsByUserIp($params['user_ip'], $query);
    //on récupère tous les votes sur ce message
    $query = $this-&gt;getMessageRatingsQuery($params['message_id'], $query);
    //on sélectionne uniquement les message récents
    $query-&gt;andWhere('c.created_at &gt;= ?', date('Y-m-d h:i:s', time() - 86400 * (sfConfig::get('app_vote_time_limit') / 24)));
    return $query;
  }
</pre>
<p>La valeur de la limite de temps pour le vote et stockée dans la config, de la même manière que round et moyenne_sur dans la première partie</p>
<pre>
All:
  moyenne_sur: 20
  round: 1
  #limite de temps pour le vote (en heures)
  vote_time_limit: 24
</pre>
<p>Vous noterez sûrement la présences des fonctions getRatingsByUserIpQuery() et getMessageRatingsQuery() :</p>
<pre>
  //retourne la requête permettant de récupérer tous les votes sur un message donné, parmis ceux récupérés par la requete $query
  public function getMessageRatingsQuery($message_id, Doctrine_Query $query = NULL)
  {
    if(is_null($query)) //si aucune requête passée, on génère la requête pour récup tous les votes
    {
      $query = $this-&gt;createQuery('c');
    }
    $query-&gt;andWhere('c.message_id = ?', $message_id); //on ajoute le where pour filtrer par message_id

    return $query;
  }

  //retourne la requete permettant de récupérer tous les votes passé par une adresse ip, parmis ceux récupérés par la requete $query
  public function getRatingsByUserIp($user_ip, Doctrine_Query $query = NULL)
  {
    if(is_null($query)) //si aucune requête passée, on génère la requête pour récup tous les votes
    {
      $query = $this-&gt;createQuery('c');
    }
    $query-&gt;andWhere('c.user_ip = ?', $user_ip); //on ajoute le where pour filtrer par user_ip

    return $query;
  }
</pre>
<p>Il ne reste plus qu&#8217;à afficher un petit message à notre utilisateur (dans l&#8217;action) :</p>
<pre>
  //execute le vote "j'aime"
  public function executeVotePlus(sfWebRequest $request)
  {
    //on récupère le message courant
    $this-&gt;message = Doctrine::getTable('Message')-&gt;find($request-&gt;getParameter('id'));
    //on effectue le vote positif
    if($this-&gt;message-&gt;votePlus($request))
    {
      $this-&gt;getUser()-&gt;setFlash('vote', "Merci d'avoir voté", false);
    }
    else
    {
      $this-&gt;getUser()-&gt;setFlash('vote', "vous ne pouvez plus voter", false);
    }
    //on génère le partial du message, c'est ce résultat qui sera utilisé par le link_to_remote pour mettre à jour la div spécifiée
    //cela évite également le passage par le template votePlusSuccess.php (qui donc n'est pas à créer)
    return $this-&gt;renderPartial('message/rating',array('message' =&gt; $this-&gt;message));
  }

  //execute le vote "j'aime pas"
  public function executeVoteMoins(sfWebRequest $request)
  {
    //on récupère le message courant
    $this-&gt;message = Doctrine::getTable('Message')-&gt;find($request-&gt;getParameter('id'));
    //on effectue le vote négatif
    if($this-&gt;message-&gt;voteMoins($request))
    {
      $this-&gt;getUser()-&gt;setFlash('vote', "Merci d'avoir voté", false);
    }
    else
    {
      $this-&gt;getUser()-&gt;setFlash('vote', "vous ne pouvez plus voter", false);
    }
    //on génère le partial du message, c'est ce résultat qui sera utilisé par le link_to_remote pour mettre à jour la div spécifiée, cela évite également le passage par le template voteMoinsSuccess.php (qui donc n'est pas à créer)
    return $this-&gt;renderPartial('message/rating',array('message' =&gt; $this-&gt;message));
  }
</pre>
<p>On utilise la méthode sfUser::setFlash(). Comme la mise à jour de l&#8217;affichage sera le partial rendu, <strong>il faut donc intégrer encore quelques lignes dans le template correspondant (apps/*votre appli*/module/message/template/_rating.php :</strong></p>
<pre>
Note : getGlobalRating(); ?&gt;/,
Nombre de votes : getRatingsCount() ?&gt;
<!-- message -->
hasFlash('vote')): ?&gt;
<span class="flash_notice">getFlash('vote') ?&gt;</span>
</pre>
<p>Et voilà !!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.lexik.fr/blog/symfony/symfony/petit-systeme-de-vote-en-ajax-590/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>JEditable et formulaire Symfony 1.2</title>
		<link>http://www.lexik.fr/blog/symfony/symfony/jeditable-et-formulaire-symfony-12-502</link>
		<comments>http://www.lexik.fr/blog/symfony/symfony/jeditable-et-formulaire-symfony-12-502#comments</comments>
		<pubDate>Fri, 17 Apr 2009 07:57:17 +0000</pubDate>
		<dc:creator>Samuel Breton</dc:creator>
				<category><![CDATA[1.2.x]]></category>
		<category><![CDATA[Symfony]]></category>
		<category><![CDATA[Ajax]]></category>
		<category><![CDATA[formulaire]]></category>
		<category><![CDATA[JEditable]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://www.lexik.fr/sfblog/?p=502</guid>
		<description><![CDATA[Comment intégrer un plugin jQuery dans un projet Symfony 1.2 ? La réponse à cette question ne pouvant pas tenir dans le modeste blog de Lexik.fr, je vais seulement vous expliquer comment intégrer la modification d&#8217;un champ &#171;&#160;INPUT&#160;&#187; en ajax dans un formulaire Symfony 1.2, à l&#8217;aide du plugin jQuery JEditable. A ce stade, je [...]]]></description>
			<content:encoded><![CDATA[<p>Comment intégrer un plugin jQuery dans un projet Symfony 1.2 ?</p>
<p>La réponse à cette question ne pouvant pas tenir dans le modeste blog de Lexik.fr, je vais seulement vous expliquer comment intégrer la modification d&#8217;un champ &laquo;&nbsp;INPUT&nbsp;&raquo; en ajax dans un formulaire Symfony 1.2, à l&#8217;aide du plugin jQuery JEditable.</p>
<p>A ce stade, je vous recommande ce lien : <a href="http://www.appelsiini.net/projects/jeditable">JEditable</a><br />
<span id="more-502"></span></p>
<h2>Pré-requis</h2>
<ul>
<li>
Comme d&#8217;habitude, l&#8217;éternel plugin : <a href="http://www.symfony-project.org/plugins/sfFormExtraPlugin">sfFormExtraPlugin</a>
</li>
<li>
Les JS :</p>
<ul>
<li>jquery-1.3.2.min.js</li>
<li>jquery.jeditable.js</li>
</ul>
</li>
</ul>
<h2>Le widget</h2>
<p><b>Chemin : </b><br />
<quote>[votre_projet]/plugins/sfFormExtraPlugin/lib/widget/sfWidgetFormInputEditInPlace.class.php</quote>
</p>
<p><b>Code source :</b></p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p502code42'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p50242"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
</pre></td><td class="code" id="p502code42"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000000; font-weight: bold;">class</span> sfWidgetFormInputEditInPlace <span style="color: #000000; font-weight: bold;">extends</span> sfWidgetFormInput
<span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$object</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$route</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$option</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: #000088;">$messages</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: #000088;">$object</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">null</span><span style="color: #339933;">,</span> <span style="color: #000088;">$route</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>
    parent<span style="color: #339933;">::</span>__construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$option</span><span style="color: #339933;">,</span> <span style="color: #000088;">$messages</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">object</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$object</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">route</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$route</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> configure<span style="color: #009900;">&#40;</span><span style="color: #000088;">$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: #339933;">,</span> <span style="color: #000088;">$messages</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>
    parent<span style="color: #339933;">::</span><span style="color: #004000;">configure</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$options</span><span style="color: #339933;">,</span> <span style="color: #000088;">$messages</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> render<span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #339933;">,</span> <span style="color: #000088;">$value</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">null</span><span style="color: #339933;">,</span> <span style="color: #000088;">$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> <span style="color: #000088;">$errors</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: #000088;">$prefix</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">generateId</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>    
    <span style="color: #b1b100;">return</span> <span style="color: #990000;">sprintf</span><span style="color: #009900;">&#40;</span><span style="color: #0000cc; font-style: italic;">&lt;&lt;&lt;EOF
&lt;b id=&quot;%s&quot;&gt;%s&lt;/b&gt;
&lt;script type=&quot;text/javascript&quot;&gt;
$(function() {
  $(&quot;#%s&quot;).editable(
    &quot;%s&quot;,
    {
      indicator : 'Saving...',      
      type   : &quot;text&quot;,
      submit : &quot;OK&quot;,
      style  : &quot;inherit&quot;,
      id       : 'ajaxfield_id'
  });
});
&lt;/script&gt;
EOF</span>
    <span style="color: #339933;">,</span> <span style="color: #000088;">$prefix</span><span style="color: #339933;">,</span> <span style="color: #000088;">$value</span><span style="color: #339933;">,</span>
      <span style="color: #000088;">$prefix</span><span style="color: #339933;">,</span>
      url_for<span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">route</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">object</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'absolute'</span><span style="color: #339933;">=&gt;</span>true<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>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

</p>
<p>Pour ce widget, j&#8217;ai créer 2 attributs.</p>
<ul>
<li><b>object</b><br />
Cet attribut contient l&#8217;objet de la route objet Symfony.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p502code43'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p50243"><td class="line_numbers"><pre>4
</pre></td><td class="code" id="p502code43"><pre class="php" style="font-family:monospace;"> <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$object</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

</li>
<li><b>route</b><br />
Cet attribut contient le nom de la route objet Symfony.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p502code44'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p50244"><td class="line_numbers"><pre>5
</pre></td><td class="code" id="p502code44"><pre class="php" style="font-family:monospace;"> <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$route</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

</li>
<p>Ces attributs nous permettent de générer l&#8217;URL de la page qui va valider et éventuellement sauvegarder les données postées par le widget JQuery.
</ul>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p502code45'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p50245"><td class="line_numbers"><pre>36
</pre></td><td class="code" id="p502code45"><pre class="php" style="font-family:monospace;">url_for<span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">route</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">object</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'absolute'</span><span style="color: #339933;">=&gt;</span>true<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span></pre></td></tr></table></div>

<p>
Maintenant que l&#8217;outil est créé, utilisons le !<br />
Toute la suite de l&#8217;article n&#8217;est qu&#8217;un exemple d&#8217;utilisation.
</p>
<h2>Schéma</h2>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p502code46'); return false;">View Code</a> YML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p50246"><td class="code" id="p502code46"><pre class="yml" style="font-family:monospace;">Person:
  columns:
    firstname:
      type: string(255)
    lastname:
      type: string(255)
    email:
      type: string(255)</pre></td></tr></table></div>

<h2>Routing</h2>
<ul>
<li><b>show_person</b><br />
La route du formulaire.
</li>
<li><b>save_person_eip</b><br />
Route de l&#8217;action &laquo;&nbsp;executeSaveEIP&nbsp;&raquo; qui valide et sauve les champs du modèle &laquo;&nbsp;Person&nbsp;&raquo;
</li>
</ul>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p502code47'); return false;">View Code</a> YML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p50247"><td class="code" id="p502code47"><pre class="yml" style="font-family:monospace;"># Route du formulaire
show_person:
  url:     /person/show/:id
  class:   sfDoctrineRoute
  options: { model: Person, type: object }
  param:   { module: person, action: show }
&nbsp;
# Route de la page sauvant les données postées en ajax
save_person_eip:
  url:     /person/saveEIP/:id
  class:   sfDoctrineRoute
  options: { model: Person, type: object }
  param:   { module: person, action: saveEIP }
  requirements:
    id: d+
    sf_method: [post]</pre></td></tr></table></div>

<h2>Actions</h2>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p502code48'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p50248"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
</pre></td><td class="code" id="p502code48"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// L'action pour le formulaire de base</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> executeShow<span style="color: #009900;">&#40;</span>sfWebRequest <span style="color: #000088;">$request</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">person</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getRoute</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getObject</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">form</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> PersonForm<span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">person</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;">// L'action qui traite le post en ajax</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> executeSaveEIP<span style="color: #009900;">&#40;</span>sfWebRequest <span style="color: #000088;">$request</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">// Récupération de l'objet de la route</span>
  <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">person</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getRoute</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getObject</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;">// Récupération des données postées par le widget JQuery</span>
  <span style="color: #000088;">$id</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getParameter</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'ajaxfield_id'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">value</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getParameter</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'value'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// Récupération du nom du champ</span>
  <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">ereg</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;person_(.*)$&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$id</span><span style="color: #339933;">,</span> <span style="color: #000088;">$key</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$champ</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$key</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> 
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// Instanciation d'un formulaire lié à notre objet</span>
  <span style="color: #000088;">$form</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> PersonForm<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;">// Récupération du validateur du champ</span>
  <span style="color: #000088;">$validator</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getValidator</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$champ</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// On tente de valider le champ</span>
  try
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$cleaned</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$validator</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">clean</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">value</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">person</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$champ</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">value</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">joueur</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">save</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  catch<span style="color: #009900;">&#40;</span>sfValidatorError <span style="color: #000088;">$e</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
     <span style="color: #666666; font-style: italic;">// Erreur lors de la validation du champ </span>
     <span style="color: #666666; font-style: italic;">// La valeur à afficher dans le template doit être la valeur actuelle de l'objet</span>
     <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">value</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">person</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$champ</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></td></tr></table></div>

<h2>Templates</h2>
<ul>
<li><b>showSuccess.php</b>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p502code49'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p50249"><td class="code" id="p502code49"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>h1<span style="color: #339933;">&gt;</span>Edition d<span style="color: #0000ff;">'une personne&lt;/h1&gt;
&lt;?php echo $form['</span>firstname<span style="color: #0000ff;">']-&gt;render();?&gt;&lt;br&gt;
&lt;?php echo $form['</span>lastname<span style="color: #0000ff;">']-&gt;render();?&gt;&lt;br&gt;
&lt;?php echo $form['</span>email<span style="color: #0000ff;">']-&gt;render();?&gt;&lt;br&gt;</span></pre></td></tr></table></div>

</li>
<li><b>saveEIPSuccess.php</b>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p502code50'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p50250"><td class="code" id="p502code50"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #990000;">echo</span> <span style="color: #000088;">$value</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

</li>
</ul>
<h2>Formulaire</h2>
<ul>
<li>
On construit chacun des widgets avec l&#8217;objet et le nom de la route.
</li>
<li>
On définit les validators.
</li>
</ul>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p502code51'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p50251"><td class="code" id="p502code51"><pre class="php" style="font-family:monospace;">  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> configure<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// Widgets</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">widgetSchema</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'firstname'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> sfWidgetFormEditInPlace<span style="color: #009900;">&#40;</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: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getObject</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'save_joueur_eip'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">widgetSchema</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'lastname'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> sfWidgetFormEditInPlace<span style="color: #009900;">&#40;</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: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getObject</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'save_joueur_eip'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">widgetSchema</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'email'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> sfWidgetFormEditInPlace<span style="color: #009900;">&#40;</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: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getObject</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'save_joueur_eip'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// Validators</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">validatorSchema</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'firstname'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> sfValidatorString<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">validatorSchema</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'lastname'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> sfValidatorString<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">validatorSchema</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'email'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> sfValidatorEmail<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  
  <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<h2>View.yml</h2>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p502code52'); return false;">View Code</a> YML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p50252"><td class="code" id="p502code52"><pre class="yml" style="font-family:monospace;">[...]
default:
  javascripts:    [jquery-1.3.2.min.js, jquery.jeditable.js]
[...]</pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.lexik.fr/blog/symfony/symfony/jeditable-et-formulaire-symfony-12-502/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Simuler un loader pendant l&#8217;exécution d&#8217;une tâche</title>
		<link>http://www.lexik.fr/blog/symfony/symfony/simuler-un-loader-pendant-lexecution-dune-tache-495</link>
		<comments>http://www.lexik.fr/blog/symfony/symfony/simuler-un-loader-pendant-lexecution-dune-tache-495#comments</comments>
		<pubDate>Thu, 16 Apr 2009 11:29:07 +0000</pubDate>
		<dc:creator>yoye</dc:creator>
				<category><![CDATA[Symfony]]></category>
		<category><![CDATA[Ajax]]></category>
		<category><![CDATA[loader]]></category>
		<category><![CDATA[périodique]]></category>

		<guid isPermaLink="false">http://www.lexik.fr/sfblog/?p=495</guid>
		<description><![CDATA[J&#8217;ai parlé il y a deux semaines de la méthode que l&#8217;on pouvait utiliser pour créer des tâches dans symfony et la façon de les alimenter dynamiquement : créer des tâches avec symfony. Il serait maintenant intéressant de pouvoir l&#8217;utiliser dans une application et également de pouvoir suivre l&#8217;évolution de notre tâche. Pour lancer la [...]]]></description>
			<content:encoded><![CDATA[<p>J&#8217;ai parlé il y a deux semaines de la méthode que l&#8217;on pouvait utiliser pour créer des tâches dans symfony et la façon de les alimenter dynamiquement : <a href="http://www.lexik.fr/blog/symfony/symfony/12x/symfony-et-les-taches-436">créer des tâches avec symfony</a>. </p>
<p>Il serait maintenant intéressant de pouvoir l&#8217;utiliser dans une application et également de pouvoir suivre l&#8217;évolution de notre tâche.<br />
<span id="more-495"></span><br />
Pour lancer la tâche nous avions dans un premier penser la lancer grâce à la fonction <strong>proc_open()</strong> de PHP qui inclut une gestion des logs assez intéressante. Nos premiers tests en local se sont déroulés sans problèmes mais une fois en production il semble que la configuration du serveur ne permette pas d&#8217;utiliser cette commande.<br />
On s&#8217;est donc rabattus sur la façon qui semble la plus utilisée :</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p495code63'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p49563"><td class="code" id="p495code63"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #990000;">chdir</span><span style="color: #009900;">&#40;</span>sfConfig<span style="color: #339933;">::</span><span style="color: #004000;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'sf_root_dir'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
<span style="color: #000088;">$myTask</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> importExecuteTask<span style="color: #009900;">&#40;</span>sfContext<span style="color: #339933;">::</span><span style="color: #004000;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getEventDispatcher</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">new</span> sfFormatter<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: #000088;">$myTask</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">run</span><span style="color: #009900;">&#40;</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: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Il est important de ne pas oublier le <strong>chdir()</strong> pour permettre l&#8217;exécution de notre tâche.</p>
<p>La première étape est donc terminée, on a pu lancer notre tâche depuis notre application, on va maintenant s&#8217;intéresser à suivre l&#8217;évolution de celle-ci.<br />
Pour cela je vais créer un fichier que j&#8217;appelerai lock.txt dans mon répertoire d&#8217;import et je vais même l&#8217;utiliser comme compteur. Pour des raisons que nous verrons un peu plus bas, je vais créer une constante dans la classe de ma tâche qui correspondra au nom du fichier.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p495code64'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p49564"><td class="code" id="p495code64"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #000000; font-weight: bold;">const</span> LOCK_FILE <span style="color: #339933;">=</span> <span style="color: #0000ff;">'lock.txt'</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Au début de ma boucle je donc avoir le code suivant :</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p495code65'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p49565"><td class="code" id="p495code65"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #000088;">$handle</span> <span style="color: #339933;">=</span> <span style="color: #990000;">fopen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$options</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'file'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'r'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">fopen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$arguments</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'import_dir'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'/'</span><span style="color: #339933;">.</span><span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #004000;">LOCK_FILE</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'x'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #990000;">fgetcsv</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$handle</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">5000</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;;&quot;</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;">false</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
 <span style="color: #666666; font-style: italic;">/* Mon code d'insertion */</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Bon ok on a notre fichier mais à quoi nous sert-il ?<br />
On peut dès à présent l&#8217;utiliser pour faire un redirect dans notre action pour empêcher de lancer plusieurs tâches en même temps :</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p495code66'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p49566"><td class="code" id="p495code66"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">redirectIf</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">is_file</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'chemin/de/mon/dossier/import'</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'/'</span><span style="color: #339933;">.</span>importExecuteTask<span style="color: #339933;">::</span><span style="color: #004000;">LOCK_FILE</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'import/loading'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Ici je redirige vers une page &laquo;&nbsp;loading&nbsp;&raquo; qui permettra de faire &laquo;&nbsp;patienter&nbsp;&raquo; notre utilisateur.</p>
<p>On vient de voir la première utilisation de notre fichier lock on va maitenant l&#8217;utiliser pour suivre l&#8217;évolution de la tâche. Pour cela je vais utiliser un compteur dans la boucle de ma tâche et l&#8217;écrire dans mon fichier. Il me faut tout d&#8217;abord obtenir le nombre maximum de mon fichier CSV.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p495code67'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p49567"><td class="code" id="p495code67"><pre class="php" style="font-family:monospace;">&nbsp;
            <span style="color: #000088;">$handle</span> <span style="color: #339933;">=</span> <span style="color: #990000;">fopen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$arguments</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'import_dir'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #000088;">$file</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'r'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$contenu_fichier</span> <span style="color: #339933;">=</span> <span style="color: #990000;">fread</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$handle</span><span style="color: #339933;">,</span> <span style="color: #990000;">filesize</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$arguments</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'import_dir'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #000088;">$file</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$max</span> <span style="color: #339933;">=</span> <span style="color: #990000;">substr_count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$contenu_fichier</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: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #990000;">fclose</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$handle</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Dans ma boucle je vais maintenant incrémenter mon compteur et mettre tout ça dans notre fichier lock :</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p495code68'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p49568"><td class="code" id="p495code68"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #000088;">$count</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #990000;">fgetcsv</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$handle</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">5000</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;;&quot;</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;">false</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
<span style="color: #666666; font-style: italic;">//- Début de mon code</span>
&nbsp;
file_put_contents<span style="color: #009900;">&#40;</span><span style="color: #000088;">$arguments</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'import_dir'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #004000;">LOCK_FILE</span><span style="color: #339933;">,</span> <span style="color: #000088;">$count</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'/'</span><span style="color: #339933;">.</span><span style="color: #000088;">$max</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$count</span><span style="color: #339933;">++;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Maintenant que mon fichier contient les informations nécessaires je vais les utiliser dans l&#8217;action :</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p495code69'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p49569"><td class="code" id="p495code69"><pre class="php" style="font-family:monospace;">&nbsp;
        <span style="color: #000088;">$filename</span> <span style="color: #339933;">=</span> sfConfig<span style="color: #339933;">::</span><span style="color: #004000;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'app_chemin_import'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'/'</span><span style="color: #339933;">.</span>importExecuteTask<span style="color: #339933;">::</span><span style="color: #004000;">LOCK_FILE</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">is_file</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$filename</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$handle</span> <span style="color: #339933;">=</span> <span style="color: #990000;">fopen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$filename</span> <span style="color: #339933;">,</span> <span style="color: #0000ff;">'r'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #000088;">$moyenne</span> <span style="color: #339933;">=</span> <span style="color: #990000;">fread</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$handle</span><span style="color: #339933;">,</span> <span style="color: #990000;">filesize</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$filename</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #000088;">$a</span> <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$moyenne</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">content</span> <span style="color: #339933;">=</span> <span style="color: #990000;">floor</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">*</span> <span style="color: #cc66cc;">100</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">/</span> <span style="color: #000088;">$a</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #990000;">fclose</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$handle</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Voilà maintenant lorsque l&#8217;utilisateur sera redirigé vers notre page d&#8217;attente. On pourra lui afficher le pourcentage de l&#8217;évolution de la tâche. Petit problème si l&#8217;on veut suivre en direct cette évolution on va recharger la page indéfiniment ? Non pour cela on va utiliser un Helper Javascript : periodically_call_remote()</p>
<p>On va donc créer une action page progess qui ne sera appeler que par le javacript et qui sera inclut dans notre page &laquo;&nbsp;loading&nbsp;&raquo; par l&#8217;appel d&#8217;un component ce qui nous donnera :</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p495code70'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p49570"><td class="code" id="p495code70"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// actions.class.php</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> executeProgress<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #000088;">$filename</span> <span style="color: #339933;">=</span> sfConfig<span style="color: #339933;">::</span><span style="color: #004000;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'app_chemin_import'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'/lock.txt'</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">is_file</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$filename</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$handle</span> <span style="color: #339933;">=</span> <span style="color: #990000;">fopen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$filename</span> <span style="color: #339933;">,</span> <span style="color: #0000ff;">'r'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #000088;">$moyenne</span> <span style="color: #339933;">=</span> <span style="color: #990000;">fread</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$handle</span><span style="color: #339933;">,</span> <span style="color: #990000;">filesize</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$filename</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #000088;">$a</span> <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$moyenne</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">content</span> <span style="color: #339933;">=</span> <span style="color: #990000;">floor</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">*</span> <span style="color: #cc66cc;">100</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">/</span> <span style="color: #000088;">$a</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #990000;">fclose</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$handle</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Notre template :</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p495code71'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p49571"><td class="code" id="p495code71"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #339933;">&lt;</span>div style<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;width: 220px;&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>div style<span style="color: #339933;">=</span><span style="color: #0000ff;">'width:&lt;?php echo $content ?&gt;%; background-image: url(/images/progress.gif); float: left;'</span><span style="color: #339933;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;?</span>php <span style="color: #990000;">echo</span> <span style="color: #000088;">$content</span> <span style="color: #000000; font-weight: bold;">?&gt;</span> <span style="color: #339933;">%&lt;/</span>div<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>Une div qui contiendra une image gif que l&#8217;on fera grandir au fur et à mesure tel une barre de progression.<br />
Et pour terminer notre page d&#8217;attente :</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p495code72'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p49572"><td class="code" id="p495code72"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> use_helper<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Javascript'</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
<span style="color: #339933;">&lt;</span>div id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;progress&quot;</span><span style="color: #339933;">&gt;&lt;/</span>div<span style="color: #339933;">&gt;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #990000;">echo</span> periodically_call_remote<span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
   <span style="color: #0000ff;">'frequency'</span>  <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'1'</span><span style="color: #339933;">,</span>
   <span style="color: #0000ff;">'update'</span>     <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'progress'</span><span style="color: #339933;">,</span>
   <span style="color: #0000ff;">'url'</span>        <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'import/progress'</span>
<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Tout les secondes la div va donc être rafraîchie en appelant notre page progress ce qui simulera l&#8217;évolution de notre tâche.</p>
<p>Pour terminer, il ne faudra surtout pas oublier de supprimer le fichier lock à l&#8217;aide d&#8217;un petit unlink().</p>
]]></content:encoded>
			<wfw:commentRss>http://www.lexik.fr/blog/symfony/symfony/simuler-un-loader-pendant-lexecution-dune-tache-495/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
