<?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 d&#039;un développeur multi-support &#187; php</title>
	<atom:link href="http://www.oni-ecchi.info/blog/tag/php/feed" rel="self" type="application/rss+xml" />
	<link>http://www.oni-ecchi.info/blog</link>
	<description>[DIM] pour les intimes :)</description>
	<lastBuildDate>Mon, 06 Feb 2012 18:51:26 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Symfony 1.2, behavior doctrine en actions</title>
		<link>http://www.oni-ecchi.info/blog/decouverte-php/symfony-1-2-behavior-doctrine-en-actions.html</link>
		<comments>http://www.oni-ecchi.info/blog/decouverte-php/symfony-1-2-behavior-doctrine-en-actions.html#comments</comments>
		<pubDate>Sat, 03 Oct 2009 21:49:46 +0000</pubDate>
		<dc:creator>Guillaume chave</dc:creator>
				<category><![CDATA[Découverte PHP]]></category>
		<category><![CDATA[Symfony]]></category>
		<category><![CDATA[découverte]]></category>
		<category><![CDATA[framework]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[tutoriel]]></category>

		<guid isPermaLink="false">http://www.oni-ecchi.info/blog/?p=478</guid>
		<description><![CDATA[J&#8217;ai (re)découvert un truc vraiment sympathique au boulot c&#8217;est le système de behavior doctrine intégré à symfony. Hein ? Mais à quoi ça sert ? Ce que j&#8217;en retiens c&#8217;est que cela peut permettre d&#8217;automatiser certaines actions (répétitives) à l&#8217;enregistrement en base, et donc d&#8217;enrichir son modèle et deux coup de cuillère à pot. Prenons [...]]]></description>
			<content:encoded><![CDATA[<p>J&#8217;ai (re)découvert un truc vraiment sympathique au boulot c&#8217;est le système de <strong>behavior doctrine</strong> intégré à <a href="http://www.oni-ecchi.info/blog/tag/symfony" class="st_tag internal_tag" rel="tag" title="Voir les articles classés avec Symfony">symfony</a>.</p>
<p><strong>Hein ? Mais à quoi ça sert ?</strong></p>
<p>Ce que j&#8217;en retiens c&#8217;est que cela peut permettre d&#8217;<strong>automatiser certaines actions</strong> (répétitives) à l&#8217;enregistrement en base, et donc d&#8217;enrichir son modèle et deux coup de cuillère à pot.</p>
<p><strong>Prenons un exemple,</strong> vous devez faire un site basé sur la créations de contenus par vos utilisateurs. Ils peuvent écrire des billets, commenter, s&#8217;envoyer des messages privés, gérer un annuaire .. bref un site où l&#8217;on doit quasiment tout relié à un utilisateur. Chaques tables auraient donc au moins un &laquo;&nbsp;user_id&nbsp;&raquo; comme clée étrangère ce qui implique dans vos actions d&#8217;associer l&#8217;utilisateur courant à l&#8217;objet pour le sauvegarder .. et ce pour chaque table &#8230; lourd non ?</p>
<p>Hé bien grâce aux behaviors vous pouvez sortir ce comportement générique dans des classes et avec seulement de la configuration au niveau de votre schéma, vous pouvez associer ce comportement à n&#8217;importe quel table. L&#8217;exemple que j&#8217;ai pris est tiré du behavior &laquo;&nbsp;<em>Signable</em>&nbsp;&raquo; du plugin <span><em>sfDoctrineActAsSignablePlugin</em>.</span></p>
<h3>Configuration</h3>

<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('p478code6'); return false;">View Code</a> YML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p4786"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code" id="p478code6"><pre class="yml" style="font-family:monospace;">Item:
  actAs:
    Signable:
  columns:
    id:
      type:             integer
      primary:          true
      autoincrement:    true
    champ1:             string(255</pre></td></tr></table></div>

<h3>Comment ça marche ?</h3>
<p>Donc si on résume, il faut dire à Doctrineque l&#8217;on va rajouter des colonnes .. et que l&#8217;on veut être prévénu lors de l&#8217;insertion en base pour associé nos actions. Donc avec seulement 2 classes, une de définition et une d&#8217;écoute, on peut s&#8217;en sortir et crée notre behavior.</p>
<p>Lors de la configuration, en rajoutant &laquo;&nbsp;actAs : Signable&nbsp;&raquo;, Doctrine va chercher une classe de définition qui ira  étendre une classe Doctrine_Template. Par convention il ira chercher une classe nommé Doctrine_Template_Signable. Le rôle de cette classe est de déclarer ses colonnes et de rajouter une écoute, un listener, vers l&#8217;autre classe d&#8217;actions.</p>
<p>Pour mon exemple, je vais volontairement raccourcir les classes du sfDoctrineActAsSignablePlugin et prendre pour acquis que vos utilisateurs sont gérés via sfGuardDoctrinePlugin.</p>
<h3>Doctrine_Template</h3>

<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('p478code7'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p4787"><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
</pre></td><td class="code" id="p478code7"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Doctrine_Template_Signable <span style="color: #000000; font-weight: bold;">extends</span> Doctrine_Template <span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// Définitions des relations</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setUp<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;">// Bim rajoute une clée étrangère sur sfGuard</span>
  <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">hasOne</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'sfGuardUser as Author'</span><span style="color: #339933;">,</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span>
            <span style="color: #0000ff;">'local'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'created_by'</span><span style="color: #339933;">,</span>
            <span style="color: #0000ff;">'foreign'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'id'</span>
            <span style="color: #009900;">&#41;</span>
            <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
 <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setTableDefinition<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// Bim une colonne en plus</span>
  <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">hasColumn</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'created_by'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'integer'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span>
          <span style="color: #0000ff;">'type'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'integer'</span><span style="color: #339933;">,</span>
          <span style="color: #0000ff;">'length'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'4'</span><span style="color: #339933;">,</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;">// Lien avec notre 2eme classe .. on passe le nom de la colonnette rajouté</span>
  <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addListener</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Doctrine_Template_Listener_Signable<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'created_by));
  }
}</span></pre></td></tr></table></div>

<p>Là pour l&#8217;exemple aucune configuration au niveau du schéma n&#8217;est possible mais c&#8217;est facile à rajouter via d&#8217;un tableau d&#8217;options et le constructeur 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('p478code8'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p4788"><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code" id="p478code8"><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> __construct<span style="color: #009900;">&#40;</span><a href="http://www.php.net/array"><span style="color: #990000;">array</span></a> <span style="color: #000088;">$options</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><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;">$this</span><span style="color: #339933;">-&gt;</span>_options <span style="color: #339933;">=</span> Doctrine_Lib<span style="color: #339933;">::</span><span style="color: #004000;">arrayDeepMerge</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_options<span style="color: #339933;">,</span> <span style="color: #000088;">$options</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<h3>Doctrine_Template_Listener</h3>
<p>Nous avons vu que notre Template rajoute un listener vers notre 2e classe en lui passant la colonne à surveiller (dans la vrai vie, un tableau d&#8217;options ..). Le rôle de notre listener de pouvoir agir avant/après l&#8217;insertion/édition d&#8217;un objet .. facile :</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('p478code9'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p4789"><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
</pre></td><td class="code" id="p478code9"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Doctrine_Template_Listener_Signable <span style="color: #000000; font-weight: bold;">extends</span> Doctrine_Record_Listener <span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000088;">$_colonette</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;created_by&quot;</span><span style="color: #339933;">;</span>
&nbsp;
  <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;">$colonne</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>_colonette <span style="color: #339933;">=</span> <span style="color: #000088;">$colonne</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> preInsert<span style="color: #009900;">&#40;</span>Doctrine_Event <span style="color: #000088;">$event</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$createdName</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_colonette<span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #666666; font-style: italic;">// recupère l'objet appellant :</span>
      <span style="color: #000088;">$objet</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$event</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getInvoker</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;">// Affectation de valeur</span>
       <span style="color: #000088;">$objet</span><span style="color: #339933;">-&gt;</span><span style="color: #000088;">$createdName</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getUserId</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: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getUserId<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;">// Echappe le mode cli</span>
     <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span> <span style="color: #339933;">!=</span> <a href="http://www.php.net/strncasecmp"><span style="color: #990000;">strncasecmp</span></a><span style="color: #009900;">&#40;</span>PHP_SAPI<span style="color: #339933;">,</span> <span style="color: #0000ff;">'cli'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
       <span style="color: #666666; font-style: italic;">// L'user courant</span>
       <span style="color: #b1b100;">return</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;">getUser</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'user_id'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'sfGuardSecurityUser'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span>
     <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> preUpdate<span style="color: #009900;">&#40;</span>Doctrine_Event <span style="color: #000088;">$event</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> postUpdate<span style="color: #009900;">&#40;</span>Doctrine_Event <span style="color: #000088;">$event</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> postInsert<span style="color: #009900;">&#40;</span>Doctrine_Event <span style="color: #000088;">$event</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// A tester : public function Delete(Doctrine_Event $event) ..</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p> Vous n&#8217;avez plus qu&#8217;a rebuilder votre model et magie ça doit marcher !! </p>
<p> Vu que l&#8217;on a fait une relation de type One-to-many sur sfGuard vous pouvez accéder à l&#8217;utilisateur sfGuard via la propriété Author sur n&#8217;importr quel table marqué comme &laquo;&nbsp;Signable&nbsp;&raquo; </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('p478code10'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p47810"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p478code10"><pre class="php" style="font-family:monospace;">  <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$objet</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Author</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">id</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Voilà .. simple au final  non ? Ah vous de créez le votre, :p</p>
<p>Comme on dirait au boulot &laquo;&nbsp;Amaaziiiing !!&nbsp;&raquo;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.oni-ecchi.info/blog/decouverte-php/symfony-1-2-behavior-doctrine-en-actions.html/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>phpFlickr &amp; Flex</title>
		<link>http://www.oni-ecchi.info/blog/decouverte-flex-flash/phpflickr-flex.html</link>
		<comments>http://www.oni-ecchi.info/blog/decouverte-flex-flash/phpflickr-flex.html#comments</comments>
		<pubDate>Fri, 30 Jan 2009 06:00:35 +0000</pubDate>
		<dc:creator>Guillaume chave</dc:creator>
				<category><![CDATA[Découverte PHP]]></category>
		<category><![CDATA[Flex Flash Floush]]></category>
		<category><![CDATA[flex]]></category>
		<category><![CDATA[framework]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[tutoriel]]></category>

		<guid isPermaLink="false">http://www.oni-ecchi.info/blog/?p=200</guid>
		<description><![CDATA[Pour un cours de , il nous avait été demandé de créer une application très simple constitué de deux écrans pour nous apprendre à manipuler les &#171;&#160;states&#160;&#187;, les composants personnalisés, le &#171;&#160;data binding&#160;&#187; etc. Le sujet de l&#8217;application était d&#8217;afficher des images en fonction d&#8217;une ville &#8211; en vue de faire du data binding sur [...]]]></description>
			<content:encoded><![CDATA[<p>Pour un cours de <a class="shal" href=" http://www.oni-ecchi.info/blog/decouverte-flex-flash/flex-et-numberformatter.html" title=" Découverte Flex : new NumberFormatter();">Flex</a>, il nous avait été demandé de créer une application très simple constitué de deux écrans pour nous apprendre à manipuler les &laquo;&nbsp;states&nbsp;&raquo;, les composants personnalisés, le &laquo;&nbsp;data binding&nbsp;&raquo; etc.</p>
<p>Le sujet de l&#8217;application était d&#8217;afficher des images en fonction d&#8217;une ville &#8211; en vue de faire du data binding sur un XML &laquo;&nbsp;statique&nbsp;&raquo; de villes &#8211; et de récupérer des images issues d&#8217;un XML dynamique via l&#8217;objet HTTPService.</p>
<p>Alors quitte à faire du dynamique, autant interroger directement un web service existant en vue de récupérer pleins d&#8217;images sans se prendre la tête. Et là j&#8217;ai pensé à FlickR et sa monstrueuse API. Après 5mn montre en main j&#8217;ai eu ma clé pour accèder à l&#8217;API et 4mn après je découvrait <a rel="nofollow" href="http://phpflickr.com/" target="_blank">phpFlickR</a>.</p>
<p>Upload de la librairie et voici mon bout de script qui me renvoit suivant deux paramètres en $_GET un joli XML près à être utilisé en <a href="http://www.oni-ecchi.info/blog/tag/flex" class="st_tag internal_tag" rel="tag" title="Voir les articles classés avec flex">Flex</a>.</p>
<ul>
<li> $_GET['town'] est le terme rechercher dans l&#8217;api FlickR sur les tags et le text qui commente une photo.</li>
<li>$_GET['max'] est le nombre de photos qu&#8217;il faudra retourner.</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('p200code13'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p20013"><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
</pre></td><td class="code" id="p200code13"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;phpFlickr.php&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$flickR</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> phpFlickr<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;ta_cle_de_connection_flickR&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// 10 résultat par défaut ..</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><a href="http://www.php.net/isset"><span style="color: #990000;">isset</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'max'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #000088;">$max</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'max'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">else</span> <span style="color: #000088;">$max</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">10</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$search</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;tags&quot;</span><span style="color: #339933;">=&gt;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'town'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'per_page'</span><span style="color: #339933;">=&gt;</span><span style="color: #000088;">$max</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;tag_mode&quot;</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;any&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'text'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'town'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$tab</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$flickR</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">photos_search</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$search</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// On renvoit un XML à Flex</span>
<a href="http://www.php.net/header"><span style="color: #990000;">header</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;content-type: application/xml&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// La balise qui va bien</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;?xml version=&quot;</span><span style="color:#800080;">1.0</span><span style="color: #0000ff;">&quot; encoding=&quot;</span>utf<span style="color: #339933;">-</span><span style="color: #cc66cc;">8</span><span style="color: #0000ff;">&quot; ?&gt;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">!=</span> <span style="color: #000088;">$max</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// on s'assure d'avoir des photos valides</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$tab</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'photo'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">!=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
	<span style="color: #666666; font-style: italic;">// Simple de créer des urls FlickR non ?</span>
	<span style="color: #000088;">$url</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$flickR</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">buildPhotoURL</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$tab</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'photo'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;thumbnail&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$link</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$flickR</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">buildPhotoURL</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$tab</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'photo'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;medium&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;image&gt;&lt;url&gt;&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$url</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&lt;/url&gt;&lt;urlLarge&gt;&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$link</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&lt;/urlLarge&gt;&lt;/image&gt;&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>La partie Flex est tout aussi simple, voici la partie essentielle qui nous intéresse :</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('p200code14'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p20014"><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
44
45
46
</pre></td><td class="code" id="p200code14"><pre class="actionscript" style="font-family:monospace;"><span style="color: #66cc66;">&lt;</span>?<span style="color: #0066CC;">xml</span> <span style="color: #0066CC;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> encoding=<span style="color: #ff0000;">&quot;utf-8&quot;</span>?<span style="color: #66cc66;">&gt;</span>
<span style="color: #66cc66;">&lt;</span>mx:Application xmlns:mx=<span style="color: #ff0000;">&quot;http://www.adobe.com/2006/mxml&quot;</span> layout=<span style="color: #ff0000;">&quot;absolute&quot;</span> creationComplete=<span style="color: #ff0000;">&quot;searchPhoto('france',20)&quot;</span><span style="color: #66cc66;">&gt;</span>
&nbsp;
<span style="color: #66cc66;">&lt;</span>mx:Script<span style="color: #66cc66;">&gt;</span>
<span style="color: #66cc66;">&lt;!</span><span style="color: #66cc66;">&#91;</span>CDATA<span style="color: #66cc66;">&#91;</span>
<span style="color: #0066CC;">import</span> mx.<span style="color: #006600;">rpc</span>.<span style="color: #006600;">http</span>.<span style="color: #006600;">HTTPService</span>;
<span style="color: #0066CC;">import</span> mx.<span style="color: #006600;">rpc</span>.<span style="color: #006600;">events</span>.<span style="color: #006600;">ResultEvent</span>;
<span style="color: #0066CC;">import</span> mx.<span style="color: #006600;">collections</span>.<span style="color: #006600;">ArrayCollection</span>;
&nbsp;
<span style="color: #66cc66;">&#91;</span>Bindable<span style="color: #66cc66;">&#93;</span>
<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> servicePhoto:ArrayCollection = <span style="color: #000000; font-weight: bold;">new</span> ArrayCollection<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; 
&nbsp;
&nbsp;
<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> searchPhoto<span style="color: #66cc66;">&#40;</span>search:<span style="color: #0066CC;">String</span>,<span style="color: #0066CC;">max</span>:<span style="color: #0066CC;">int</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">var</span> modService:HTTPService = <span style="color: #000000; font-weight: bold;">new</span> HTTPService<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	modService.<span style="color: #006600;">method</span> = <span style="color: #ff0000;">&quot;get&quot;</span>;
	modService.<span style="color: #0066CC;">url</span> = <span style="color: #ff0000;">&quot;http://oni-ecchi.info/labo/FlexPhotos/service.php&quot;</span>;
&nbsp;
	<span style="color: #000000; font-weight: bold;">var</span> parameters:<span style="color: #0066CC;">Object</span> = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Object</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	parameters.<span style="color: #006600;">town</span> = search;
	parameters.<span style="color: #0066CC;">max</span> = <span style="color: #0066CC;">max</span>;
	modService.<span style="color: #006600;">request</span> = parameters;                
	modService.<span style="color: #0066CC;">send</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	modService.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>ResultEvent.<span style="color: #006600;">RESULT</span>,onDataCharged<span style="color: #66cc66;">&#41;</span>;                
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> onDataCharged<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:ResultEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
	servicePhoto = <span style="color: #000000; font-weight: bold;">new</span> ArrayCollection<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	servicePhoto = <span style="color: #0066CC;">e</span>.<span style="color: #006600;">result</span>.<span style="color: #006600;">root</span>.<span style="color: #006600;">image</span> as ArrayCollection;		
	vignettes.<span style="color: #006600;">dataProvider</span> = <span style="color: #0066CC;">e</span>.<span style="color: #006600;">result</span>.<span style="color: #006600;">root</span>.<span style="color: #006600;">image</span> as ArrayCollection;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&gt;</span>
<span style="color: #66cc66;">&lt;/</span>mx:Script<span style="color: #66cc66;">&gt;</span>
<span style="color: #66cc66;">&lt;</span>mx:TileList id=<span style="color: #ff0000;">&quot;vignettes&quot;</span> dataProvider=<span style="color: #ff0000;">&quot;{servicePhoto}&quot;</span> <span style="color: #0066CC;">width</span>=<span style="color: #ff0000;">&quot;481&quot;</span> <span style="color: #0066CC;">height</span>=<span style="color: #ff0000;">&quot;279&quot;</span> columnWidth=<span style="color: #ff0000;">&quot;110&quot;</span> rowHeight=<span style="color: #ff0000;">&quot;110&quot;</span> columnCount=<span style="color: #ff0000;">&quot;4&quot;</span> horizontalCenter=<span style="color: #ff0000;">&quot;0&quot;</span> paddingRight=<span style="color: #ff0000;">&quot;5&quot;</span> verticalCenter=<span style="color: #ff0000;">&quot;0&quot;</span><span style="color: #66cc66;">&gt;</span>
    <span style="color: #66cc66;">&lt;</span>mx:itemRenderer<span style="color: #66cc66;">&gt;</span>
   		<span style="color: #66cc66;">&lt;</span>mx:Component<span style="color: #66cc66;">&gt;</span>
   			<span style="color: #66cc66;">&lt;</span>mx:Image  source=<span style="color: #ff0000;">&quot;{data.url}&quot;</span> verticalAlign=<span style="color: #ff0000;">&quot;middle&quot;</span> horizontalAlign=<span style="color: #ff0000;">&quot;center&quot;</span> <span style="color: #66cc66;">/&gt;</span>
   		<span style="color: #66cc66;">&lt;/</span>mx:Component<span style="color: #66cc66;">&gt;</span>
    <span style="color: #66cc66;">&lt;/</span>mx:itemRenderer<span style="color: #66cc66;">&gt;</span>       
<span style="color: #66cc66;">&lt;/</span>mx:TileList<span style="color: #66cc66;">&gt;</span>
&nbsp;
&nbsp;
<span style="color: #66cc66;">&lt;/</span>mx:Application<span style="color: #66cc66;">&gt;</span></pre></td></tr></table></div>

<p>Ce n&#8217;est qu&#8217;un exemple très simple. Dans le fichier source qui va suivre, j&#8217;ai un peu plus travaillé la chose en utilisant un composant de chargement d&#8217;image sympathique, un spinner de chargement 2.0 et enfin la gestion des states (très simple).</p>
<ul>
<li><a href="/blog/wp-content/uploads/2009/01/FlexFlickR.zip" target="_blank">Mon Projet Flex (zip)<br />
</a></li>
<li><a href="http://oni-ecchi.info/labo/FlexPhotos/FlexFlickR.html" target="_blank">Le site d&#8217;exemple</a></li>
</ul>
<p>Voilà, c&#8217;est tout pour aujourd&#8217;hui</p>
]]></content:encoded>
			<wfw:commentRss>http://www.oni-ecchi.info/blog/decouverte-flex-flash/phpflickr-flex.html/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Et un framework un !</title>
		<link>http://www.oni-ecchi.info/blog/decouverte-php/et-un-framework-un.html</link>
		<comments>http://www.oni-ecchi.info/blog/decouverte-php/et-un-framework-un.html#comments</comments>
		<pubDate>Fri, 12 Dec 2008 13:31:34 +0000</pubDate>
		<dc:creator>Guillaume chave</dc:creator>
				<category><![CDATA[Découverte PHP]]></category>
		<category><![CDATA[cakePHP]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[framework]]></category>
		<category><![CDATA[generateur]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[vidéo]]></category>

		<guid isPermaLink="false">http://www.oni-ecchi.info/blog/?p=107</guid>
		<description><![CDATA[Aller sur http://blog.jaysalvat.com/articles/model-baker-une-interface-graphique-a-la-creation-dapplications-cakephp.php ou regarder simplement son screencast : Model Baker in action from Jay Salvat on Vimeo. Ca a l&#8217;air génial :) Dommage que ça soit seulement pour mac pour l&#8217;instant, mais c&#8217;est très prometteurs ! Apres un tour sur Symfony, sur Zend, faudrait que j&#8217;aille voir CakePHP donc :)]]></description>
			<content:encoded><![CDATA[<p>Aller sur <a href="http://blog.jaysalvat.com/articles/model-baker-une-interface-graphique-a-la-creation-dapplications-cakephp.php">http://blog.jaysalvat.com/articles/model-baker-une-interface-graphique-a-la-creation-dapplications-cakephp.php</a> ou regarder simplement son screencast :<br />
<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="400" height="267" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="src" value="http://vimeo.com/moogaloop.swf?clip_id=2337610&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=&amp;fullscreen=1" /><embed type="application/x-shockwave-flash" width="400" height="267" src="http://vimeo.com/moogaloop.swf?clip_id=2337610&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=&amp;fullscreen=1" allowscriptaccess="always" allowfullscreen="true"></embed></object><br />
<a href="http://vimeo.com/2337610">Model Baker in action</a> from <a href="http://vimeo.com/user740872">Jay Salvat</a> on <a href="http://vimeo.com">Vimeo</a>.</p>
<p>Ca a l&#8217;air génial :) Dommage que ça soit seulement pour mac pour l&#8217;instant, mais c&#8217;est très prometteurs ! Apres un tour sur <a href="http://www.oni-ecchi.info/blog/tag/symfony" class="st_tag internal_tag" rel="tag" title="Voir les articles classés avec Symfony">Symfony</a>, sur Zend, faudrait que j&#8217;aille voir CakePHP donc :)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.oni-ecchi.info/blog/decouverte-php/et-un-framework-un.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Utiliser une procédure stocké mysql avec Zend !</title>
		<link>http://www.oni-ecchi.info/blog/decouverte-php/utiliser-une-procedure-stocke-en-mysql.html</link>
		<comments>http://www.oni-ecchi.info/blog/decouverte-php/utiliser-une-procedure-stocke-en-mysql.html#comments</comments>
		<pubDate>Tue, 02 Dec 2008 19:40:29 +0000</pubDate>
		<dc:creator>Guillaume chave</dc:creator>
				<category><![CDATA[Découverte PHP]]></category>
		<category><![CDATA[framework]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[procédure stocké]]></category>
		<category><![CDATA[transact-sql]]></category>
		<category><![CDATA[zend]]></category>
		<category><![CDATA[Zend framework]]></category>

		<guid isPermaLink="false">http://www.oni-ecchi.info/blog/?p=94</guid>
		<description><![CDATA[Dans un article précédent, je créais la procédure stocké &#171;&#160;import&#160;&#187;, voilà comment je l&#8217;utilise dans Zend. Je n&#8217;invente rien, j&#8217;ai trouvé la manip sur http://www.ericmery.fr/blog/zend_db-sql-server-procedure-stockee. ?View Code PHP1 2 3 4 5 6 7 8 9 10 $registry = Zend_Registry::getInstance&#40;&#41;; $config = $registry-&#62;get&#40;&#34;config&#34;&#41;; &#160; $db = Zend_Db::factory&#40;$config-&#62;db&#41;; &#160; $stmt = $db-&#62;prepare&#40;&#34;call import(?,?,?);&#34;&#41;; $stmt-&#62;bindParam&#40;1, $param1, PDO::PARAM_STR,1000&#41;; [...]]]></description>
			<content:encoded><![CDATA[<p>Dans un article précédent, je créais la <a href="http://www.oni-ecchi.info/blog/tag/procedure-stocke" class="st_tag internal_tag" rel="tag" title="Voir les articles classés avec procédure stocké">procédure stocké</a> &laquo;&nbsp;import&nbsp;&raquo;, voilà comment je l&#8217;utilise dans Zend.</p>
<p>Je n&#8217;invente rien, j&#8217;ai trouvé la manip sur <a href="http://www.ericmery.fr/blog/zend_db-sql-server-procedure-stockee/">http://www.ericmery.fr/blog/zend_db-sql-server-procedure-stockee.<br />
</a></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('p94code16'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p9416"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code" id="p94code16"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$registry</span> <span style="color: #339933;">=</span> Zend_Registry<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;">;</span>
<span style="color: #000088;">$config</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$registry</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;config&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$db</span> <span style="color: #339933;">=</span> Zend_Db<span style="color: #339933;">::</span><span style="color: #004000;">factory</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$config</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">db</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$stmt</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prepare</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;call import(?,?,?);&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$stmt</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">bindParam</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #000088;">$param1</span><span style="color: #339933;">,</span> PDO<span style="color: #339933;">::</span><span style="color: #004000;">PARAM_STR</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$stmt</span><span style="color: #339933;">-&gt;;</span>bindParam<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> <span style="color: #000088;">$param2</span><span style="color: #339933;">,</span> PDO<span style="color: #339933;">::</span><span style="color: #004000;">PARAM_STR</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">255</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$stmt</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">bindParam</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span> <span style="color: #000088;">$param3</span><span style="color: #339933;">,</span> PDO<span style="color: #339933;">::</span><span style="color: #004000;">PARAM_STR</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">255</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$res</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$stmt</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></pre></td></tr></table></div>

<p>Magie, c&#8217;est la même chose. On remercie PDO et son abstracteur de base de donnée utilisé !</p>
]]></content:encoded>
			<wfw:commentRss>http://www.oni-ecchi.info/blog/decouverte-php/utiliser-une-procedure-stocke-en-mysql.html/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>[Mysql] Procédure Stocké</title>
		<link>http://www.oni-ecchi.info/blog/sql/mysql-procedure-stocke.html</link>
		<comments>http://www.oni-ecchi.info/blog/sql/mysql-procedure-stocke.html#comments</comments>
		<pubDate>Tue, 02 Dec 2008 19:29:45 +0000</pubDate>
		<dc:creator>Guillaume chave</dc:creator>
				<category><![CDATA[SQL]]></category>
		<category><![CDATA[framework]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[procédure stocké]]></category>
		<category><![CDATA[Zend framework]]></category>

		<guid isPermaLink="false">http://www.oni-ecchi.info/blog/?p=91</guid>
		<description><![CDATA[Pour un projet d&#8217;annuaire de Flux RSS, j&#8217;ai du faire en sorte que l&#8217;on puisse importer ses Flux dans l&#8217;annuaire via un fichier XML opml que tout bon agrégateur (google reader, netvibes) fournit. Donc admettons que l&#8217;on a 100 flux RSS à importer il faut pour chaque flux vérifier si le flux n&#8217;existe pas déjà [...]]]></description>
			<content:encoded><![CDATA[<p>Pour un projet d&#8217;annuaire de Flux RSS, j&#8217;ai du faire en sorte que l&#8217;on puisse importer ses Flux dans l&#8217;annuaire via un fichier XML opml que tout bon agrégateur (google reader, netvibes) fournit. Donc admettons que l&#8217;on a 100 flux RSS à importer il faut pour chaque flux vérifier si le flux n&#8217;existe pas déjà sinon le créer en base. Donc il faut prévoir un maximum de 200 requêtes SQL.</p>
<p>Seulement voilà, si on fait les instructions de test d&#8217;existence et d&#8217;insertion au niveau <a href="http://www.oni-ecchi.info/blog/tag/php" class="st_tag internal_tag" rel="tag" title="Voir les articles classés avec php">PHP</a>, cela fait 200 aller retour entre <a href="http://www.oni-ecchi.info/blog/tag/php" class="st_tag internal_tag" rel="tag" title="Voir les articles classés avec php">PHP</a> et la base <a href="http://www.oni-ecchi.info/blog/tag/mysql" class="st_tag internal_tag" rel="tag" title="Voir les articles classés avec mysql">Mysql</a>. Hors ce qui plombe un script c&#8217;est souvent ces aller retour incessants .. Un moyen d&#8217;optimiser ce traitement a été de créer une <a href="http://www.oni-ecchi.info/blog/tag/procedure-stocke" class="st_tag internal_tag" rel="tag" title="Voir les articles classés avec procédure stocké">procédure stocké</a> qui prend en paramètre le fichier XML (où plutôt une représentation épuré plus strict), et de faire exécuter les 200 requêtes DANS <a href="http://www.oni-ecchi.info/blog/tag/mysql" class="st_tag internal_tag" rel="tag" title="Voir les articles classés avec mysql">Mysql</a>. Chouette non ? Plus rapide, plus stable, plus optimisé .. que du bonheur.</p>
<h3>Procédure Stocké en Mysql</h3>
<p>Vous allez rire, mais d&#8217;après mes recherches googlidiennes très peu de personnes font des procédures en Mysql. Cela vient sans doute du fait qu&#8217;elles sont apparru avec mysql 5 et que c&#8217;est encore &laquo;&nbsp;neuf&nbsp;&raquo;.</p>
<p>Tous les exemples que l&#8217;on trouve sur le sujet sont pour SQL serveur PRo, donc du transact-SQL. Et le souci c&#8217;est qu&#8217;il n&#8217;y pas forcement d&#8217;équivalence entre les fonctions transact-SQL et les fonctions mysql. La synthaxe n&#8217;est décidement pas la même.</p>
<h3>Outil utilisé</h3>
<p><a href="http://dev.mysql.com/downloads/gui-tools/5.0.html">Mysql query browser !</a></p>
<p>L&#8217;exemple que je donne n&#8217;est valable qu&#8217;a travers sont interface .. zarb.. m&#8217;enfin c&#8217;est un très bon outil quand même, rien à voir avec un phpMyAdmin tout POURRI !</p>
<h3>Création de la requête</h3>
<p>Voilà la précédure allégé que j&#8217;ai crée (je fais d&#8217;autres test en version un peu plus lourde). Je vais essayer de la commenter au mieux.</p>
<p>La procédure s&#8217;appelle import, elle est dans la base de donné nommé &laquo;&nbsp;Fluxe&nbsp;&raquo; et on l&#8217;appelle et l&#8217;utilise comme ceci</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('p91code19'); return false;">View Code</a> SQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p9119"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p91code19"><pre class="sql" style="font-family:monospace;">call import<span style="color: #66cc66;">&#40;</span>
<span style="color: #ff0000;">&quot;premier flux|http://monsite|http://monsite/fluxrss|description;
deuxieme flix|http://monsite2|http://www.google.fr/bidule.xml|autre description;
troisieme|http://monsite3|http://monsite2/rss.xml|description;&quot;</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#41;</span>;</pre></td></tr></table></div>

<p>Le premier paramètre est la chaine retravaillé coté PHP qui correspond à chaque ligne de l&#8217;XML, et le deuxième paramètre est le nombre de flux. Une entré se fini via un &laquo;&nbsp;;&nbsp;&raquo; et chaque colonne est séparé via un &nbsp;&raquo; | &laquo;&nbsp;. Bon je sais, mes délimiteurs ne sont pas assez complexe dans cet exemple et cela serait risque de bug si une description ou url comporterait des points virgules et des pipes&#8230;</p>
<p>Voici la requête :</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('p91code20'); return false;">View Code</a> SQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p9120"><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
44
45
</pre></td><td class="code" id="p91code20"><pre class="sql" style="font-family:monospace;">DELIMITER $$  <span style="color: #808080; font-style: italic;">-- pas trop compris l'interet O_o</span>
<span style="color: #993333; font-weight: bold;">DROP</span> PROCEDURE <span style="color: #993333; font-weight: bold;">IF</span> <span style="color: #993333; font-weight: bold;">EXISTS</span> <span style="color: #ff0000;">`fluxe`</span><span style="color: #66cc66;">.</span><span style="color: #ff0000;">`import`</span> $$
<span style="color: #808080; font-style: italic;">-- Supprime la procédure si elle existe (je suis fort en anglais hein ? ^^) </span>
&nbsp;
<span style="color: #993333; font-weight: bold;">CREATE</span> DEFINER<span style="color: #66cc66;">=</span><span style="color: #ff0000;">`root`</span>@<span style="color: #ff0000;">`localhost`</span> PROCEDURE <span style="color: #ff0000;">`import`</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">In</span> myCsv Text<span style="color: #66cc66;">,</span> <span style="color: #993333; font-weight: bold;">In</span> nbElement Int<span style="color: #66cc66;">&#41;</span>
<span style="color: #808080; font-style: italic;">-- crée la procédure avec deux paramètres &quot;IN&quot; de type Text et Int. C'est à dire que c'est deux paramètres obligatoires pour exécuter la requête.  </span>
&nbsp;
BEGIN <span style="color: #808080; font-style: italic;">-- Debut de la procédure</span>
&nbsp;
<span style="color: #808080; font-style: italic;">-- Declare mes variables correspondant aux colonnes de ma table Flux</span>
DECLARE nom varchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span>;
DECLARE urlSite varchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span>;
DECLARE myurlFlux varchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span>;
DECLARE description text;
DECLARE  ligne_flux VARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1000</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">-- correspondra à une ligne de Flux</span>
DECLARE cpt int; <span style="color: #808080; font-style: italic;">-- un petit compteur pour faire une jolie boucle While</span>
&nbsp;
<span style="color: #993333; font-weight: bold;">set</span> cpt <span style="color: #66cc66;">=</span> nbElement;  <span style="color: #808080; font-style: italic;">-- / !  Voilà comment on définit une variable SET var = valeur; </span>
&nbsp;
WHILE cpt <span style="color: #66cc66;">&lt;=</span> nbElement <span style="color: #993333; font-weight: bold;">and</span> cpt <span style="color: #66cc66;">&gt;</span> <span style="color: #cc66cc;">0</span> do  <span style="color: #808080; font-style: italic;">-- j'aurais pu utiliser une boucle Loop aussi.</span>
&nbsp;
  <span style="color: #993333; font-weight: bold;">select</span> <span style="color: #993333; font-weight: bold;">replace</span><span style="color: #66cc66;">&#40;</span>substring<span style="color: #66cc66;">&#40;</span>substring_index<span style="color: #66cc66;">&#40;</span>myCsv<span style="color: #66cc66;">,</span> <span style="color: #ff0000;">';'</span><span style="color: #66cc66;">,</span> cpt<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> length<span style="color: #66cc66;">&#40;</span>substring_index<span style="color: #66cc66;">&#40;</span>myCsv<span style="color: #66cc66;">,</span> <span style="color: #ff0000;">';'</span><span style="color: #66cc66;">,</span> cpt <span style="color: #66cc66;">-</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">';'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">''</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">as</span> ligne <span style="color: #993333; font-weight: bold;">into</span> ligne_flux;
&nbsp;
  <span style="color: #808080; font-style: italic;">-- Outch &quot;KEKECER ?&quot; vous devez vous pausez comme question :)</span>
  <span style="color: #808080; font-style: italic;">-- c'est simple, je découpe le premier paramètre In pour en extraire la 1er ligne trouvé.</span>
  <span style="color: #808080; font-style: italic;">-- Les fonctions appellés ne sont pas du php mais bien du SQL ! Puissant non ?</span>
  <span style="color: #808080; font-style: italic;">-- l'idée de récupèrer la 1er ligne DANS une variable &quot;ligne_flux&quot;, d'où le &quot;select ... INTO variable&quot; </span>
&nbsp;
  <span style="color: #808080; font-style: italic;">-- Parsage de la chaine &quot;ligne_flux&quot; pour en extraire les colonnes, rebelote quoi ..</span>
  <span style="color: #993333; font-weight: bold;">select</span>  <span style="color: #993333; font-weight: bold;">replace</span><span style="color: #66cc66;">&#40;</span>substring<span style="color: #66cc66;">&#40;</span>substring_index<span style="color: #66cc66;">&#40;</span>ligne_flux<span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'|'</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> length<span style="color: #66cc66;">&#40;</span>substring_index<span style="color: #66cc66;">&#40;</span>ligne_flux<span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'|'</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">-</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'|'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">''</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">into</span> nom;
  <span style="color: #993333; font-weight: bold;">select</span>  <span style="color: #993333; font-weight: bold;">replace</span><span style="color: #66cc66;">&#40;</span>substring<span style="color: #66cc66;">&#40;</span>substring_index<span style="color: #66cc66;">&#40;</span>ligne_flux<span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'|'</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> length<span style="color: #66cc66;">&#40;</span>substring_index<span style="color: #66cc66;">&#40;</span>ligne_flux<span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'|'</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">2</span> <span style="color: #66cc66;">-</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'|'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">''</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">into</span> urlSite;
  <span style="color: #993333; font-weight: bold;">select</span>  <span style="color: #993333; font-weight: bold;">replace</span><span style="color: #66cc66;">&#40;</span>substring<span style="color: #66cc66;">&#40;</span>substring_index<span style="color: #66cc66;">&#40;</span>ligne_flux<span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'|'</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> length<span style="color: #66cc66;">&#40;</span>substring_index<span style="color: #66cc66;">&#40;</span>ligne_flux<span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'|'</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">3</span> <span style="color: #66cc66;">-</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'|'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">''</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">into</span> myurlFlux;
  <span style="color: #993333; font-weight: bold;">select</span>  <span style="color: #993333; font-weight: bold;">replace</span><span style="color: #66cc66;">&#40;</span>substring<span style="color: #66cc66;">&#40;</span>substring_index<span style="color: #66cc66;">&#40;</span>ligne_flux<span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'|'</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">4</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> length<span style="color: #66cc66;">&#40;</span>substring_index<span style="color: #66cc66;">&#40;</span>ligne_flux<span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'|'</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">4</span> <span style="color: #66cc66;">-</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'|'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">''</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">into</span> description;
&nbsp;
  <span style="color: #808080; font-style: italic;">-- Insertion que si la ligne n'existe pas. Le critère est l'url du flux.</span>
  <span style="color: #993333; font-weight: bold;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">EXISTS</span> <span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">select</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">from</span> flux <span style="color: #993333; font-weight: bold;">where</span> urlFlux <span style="color: #66cc66;">=</span> myurlFlux<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> THEN
       <span style="color: #993333; font-weight: bold;">insert</span> <span style="color: #993333; font-weight: bold;">into</span> flux <span style="color: #66cc66;">&#40;</span>nom<span style="color: #66cc66;">,</span>urlSite<span style="color: #66cc66;">,</span>urlFlux<span style="color: #66cc66;">,</span>description<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">values</span> <span style="color: #66cc66;">&#40;</span>nom<span style="color: #66cc66;">,</span>urlSite<span style="color: #66cc66;">,</span>myurlFlux<span style="color: #66cc66;">,</span>description<span style="color: #66cc66;">&#41;</span>;
   end <span style="color: #993333; font-weight: bold;">if</span>;
&nbsp;
 <span style="color: #808080; font-style: italic;">-- on décrémente le compteur</span>
 <span style="color: #993333; font-weight: bold;">set</span> cpt <span style="color: #66cc66;">=</span> cpt <span style="color: #66cc66;">-</span> <span style="color: #cc66cc;">1</span>;
end while;
&nbsp;
END $$
DELIMITER ;</pre></td></tr></table></div>

<p>Et voilà c&#8217;est fini !</p>
<h3>Lien de référence</h3>
<p>La base :<a href="http://dev.mysql.com/doc/refman/5.0/fr/stored-procedure-syntax.html">http://dev.mysql.com/doc/refman/5.0/fr/stored-procedure-syntax.html</a></p>
<p>les variables : <a href="http://dev.mysql.com/doc/refman/5.0/fr/variables-in-stored-procedures.html">http://dev.mysql.com/doc/refman/5.0/fr/variables-in-stored-procedures.html</a><br />
If, while, case, et tous les contrôles : <a href="http://dev.mysql.com/doc/refman/5.0/fr/flow-control-constructs.html">http://dev.mysql.com/doc/refman/5.0/fr/flow-control-constructs.html</a></p>
<p>Mis à part la documentation officiel, j&#8217;ai rien trouvé d&#8217;autres à me mettre sous la dent ..</p>
]]></content:encoded>
			<wfw:commentRss>http://www.oni-ecchi.info/blog/sql/mysql-procedure-stocke.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Hébergement de BOT</title>
		<link>http://www.oni-ecchi.info/blog/decouverte-php/hebergement-de-bot.html</link>
		<comments>http://www.oni-ecchi.info/blog/decouverte-php/hebergement-de-bot.html#comments</comments>
		<pubDate>Sat, 27 Sep 2008 19:49:42 +0000</pubDate>
		<dc:creator>Guillaume chave</dc:creator>
				<category><![CDATA[Découverte PHP]]></category>
		<category><![CDATA[irc]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[tutoriel]]></category>

		<guid isPermaLink="false">http://www.oni-ecchi.info/blog/?p=72</guid>
		<description><![CDATA[Dans un article précédent, nous avions vu comment créer un bot IRC en PHP. Seulement assurer la connexion du bot sur le salon était assez laborieux. Un processus php par bot qui tourne sur votre machine, c&#8217;est pas folichon. Mais grâce à http://new.imified.com, ce souci n&#8217;en est plus un ! Ce service web permet de [...]]]></description>
			<content:encoded><![CDATA[<p>Dans un article précédent, nous avions vu comment créer un bot <a href="http://www.oni-ecchi.info/blog/tag/irc" class="st_tag internal_tag" rel="tag" title="Voir les articles classés avec irc">IRC</a> en <a href="http://www.oni-ecchi.info/blog/tag/php" class="st_tag internal_tag" rel="tag" title="Voir les articles classés avec php">PHP</a>. Seulement assurer la connexion du bot sur le salon était assez laborieux. Un processus <a href="http://www.oni-ecchi.info/blog/tag/php" class="st_tag internal_tag" rel="tag" title="Voir les articles classés avec php">php</a> par bot qui tourne sur votre machine, c&#8217;est pas folichon.</p>
<p>Mais grâce à <a href="http://new.imified.com/">http://new.imified.com,</a> ce souci n&#8217;en est plus un ! Ce service web permet de connecter un bot à MSN, IRC, Gtalk etc et ce bot demandera à votre page PHP ce qu&#8217;il doit faire.</p>
<p>Au final nous n&#8217;avons plus qu&#8217;a se consacrer sur les réponses et les traitements à faire en fonction du dialogue de l&#8217;utilisateur (donnée récupérer sous forme de $_GET). Fini le parsage du stream pour détecter la bonne ligne ^^</p>
<p>Je vous avouerais, j&#8217;ai pas encore tester. Mais je compte le faire un de ces quatres :) Je pense que c&#8217;est LA solution pour créer rapidement un compagnon virtuel &#8230; Imaginer un bot de ce style qui pilote votre twitter, facebook etc. Vous lui parlez, il s&#8217;occupe du reste :)</p>
<p><cite class="aligncenter">Vu sur le channel #<a href="http://www.oni-ecchi.info/blog/tag/symfony" class="st_tag internal_tag" rel="tag" title="Voir les articles classés avec Symfony">symfony</a>-fr (irc.freenode.com) grâce à [MA]Pascal.<br />
Il a crée un bot qui relaie ce que vous lui dites sur un site donné. (Système de prise de notes rapide) <br /> Ajouter <span style="font-size: small;"><span style="color: #000000;"><a href="mailto:paste@bot.im">paste@bot.im</a></span></span> à votre compte Gtalk et parler lui. Enfin, cela reste surtout un bot de test, réalisé avec Symfony 1.2 et Doctrine (un ORM comme propel) </cite></p>
]]></content:encoded>
			<wfw:commentRss>http://www.oni-ecchi.info/blog/decouverte-php/hebergement-de-bot.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Validé un formulaire en Ajax avec Symfony 1.1</title>
		<link>http://www.oni-ecchi.info/blog/decouverte-php/valide-un-formulaire-en-ajax-avec-symfony-11.html</link>
		<comments>http://www.oni-ecchi.info/blog/decouverte-php/valide-un-formulaire-en-ajax-avec-symfony-11.html#comments</comments>
		<pubDate>Sat, 06 Sep 2008 16:59:29 +0000</pubDate>
		<dc:creator>Guillaume chave</dc:creator>
				<category><![CDATA[Découverte PHP]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[formulaire]]></category>
		<category><![CDATA[framework]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[Symfony]]></category>

		<guid isPermaLink="false">http://www.oni-ecchi.info/blog/?p=35</guid>
		<description><![CDATA[Dans ce billet nous allons voir comment utiliser Ajax pour valider champ par champ un formulaire constuit avec sfForm de Symfony 1.1. Je prend pour acquis que vous ayez lu / que vous connaissez la validatation de formulaire &#171;&#160;classique&#160;&#187; offert par sfForm. Tout est expliqué là et surtout là. En fait le souci avec les [...]]]></description>
			<content:encoded><![CDATA[<p>Dans ce billet nous allons voir comment utiliser Ajax pour valider champ par champ un formulaire constuit avec sfForm de <a href="http://www.oni-ecchi.info/blog/tag/symfony" class="st_tag internal_tag" rel="tag" title="Voir les articles classés avec Symfony">Symfony</a> 1.1.</p>
<p>Je prend pour acquis que vous ayez lu / que vous connaissez la validatation de formulaire &laquo;&nbsp;classique&nbsp;&raquo; offert par sfForm. Tout est expliqué <a href="http://www.symfony-project.org/book/forms/1_1/fr/" target="_blank">là </a>et surtout <a href="http://www.symfony-project.org/book/forms/1_1/fr/02-Form-Validation" target="_blank">là</a>.</p>
<p>En fait le souci avec les validateurs symfony 1.1 est qu&#8217;ils ne possèdent pas la méthode isValid() contraitement aux validateurs symfony 1.0. Du coup, quand on fait de l&#8217;ajax, nous sommes obligés de passer d&#8217;un type de validateur à un autre.</p>
<p>Cette exemple illuste le fait avec le validateur sfValidatorEmail (sf 1.1) et sfEmailValidator (sf 1.0). L&#8217;idée est de définir les paramêtres du validateur (les messages d&#8217;erreurs dans notre cas) qu&#8217;à un seul endroid afin de respecter le concept Dry (Don&#8217;t Repeat Yourself).</p>
<p>Voilà les étapes nécessaires pour mettre en place une validation de formulaire classique et &laquo;&nbsp;Ajaxé&nbsp;&raquo; :</p>
<ol>
<li>Vous créez vos widgets dans lib/form/MaClasseForm.class.<a href="http://www.oni-ecchi.info/blog/tag/php" class="st_tag internal_tag" rel="tag" title="Voir les articles classés avec php">php</a>

<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('p35code29'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p3529"><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
</pre></td><td class="code" id="p35code29"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> MaClasseForm <span style="color: #000000; font-weight: bold;">extends</span> BaseMaClassForm
<span style="color: #009900;">&#123;</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: #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;">setWidgets</span><span style="color: #009900;">&#40;</span><a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span>
		<span style="color: #0000ff;">'id'</span>         <span style="color: #339933;">=&gt;</span> <span style="color: #000000; font-weight: bold;">new</span> sfWidgetFormInputHidden<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'FirstName'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000000; font-weight: bold;">new</span> sfWidgetFormInput<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'LastName'</span>  <span style="color: #339933;">=&gt;</span> <span style="color: #000000; font-weight: bold;">new</span> sfWidgetFormInput<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'Email'</span>      <span style="color: #339933;">=&gt;</span> <span style="color: #000000; font-weight: bold;">new</span> sfWidgetFormInput<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'password_repeat'</span>      <span style="color: #339933;">=&gt;</span> <span style="color: #000000; font-weight: bold;">new</span> sfWidgetFormInput<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'password'</span>      <span style="color: #339933;">=&gt;</span> <span style="color: #000000; font-weight: bold;">new</span> sfWidgetFormInput<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</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 peut modifier les labels si besoin est ...</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">widgetSchema</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setLabels</span><span style="color: #009900;">&#40;</span><a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span>
		<span style="color: #0000ff;">'FirstName'</span>    <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Prenom'</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'Email'</span>   <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'addresse Email '</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'LastName'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Nom'</span><span style="color: #339933;">,</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>

</li>
<li>Vous définissez vos validateurs dans lib/form/MaClasseForm.class.php à la suite de la méthode configure()

<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('p35code30'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p3530"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code" id="p35code30"><pre class="php" style="font-family:monospace;">&nbsp;
 <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setValidators</span><span style="color: #009900;">&#40;</span><a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span>
<span style="color: #0000ff;">'id'</span>         <span style="color: #339933;">=&gt;</span> <span style="color: #000000; font-weight: bold;">new</span> sfValidatorPropelChoice<span style="color: #009900;">&#40;</span><a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'model'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Utilisateurs'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'column'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'id'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'required'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
<span style="color: #0000ff;">'FirstName'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000000; font-weight: bold;">new</span> sfValidatorString<span style="color: #009900;">&#40;</span><a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'max_length'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">20</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'required'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
<span style="color: #0000ff;">'LastName'</span>  <span style="color: #339933;">=&gt;</span> <span style="color: #000000; font-weight: bold;">new</span> sfValidatorString<span style="color: #009900;">&#40;</span><a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'max_length'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">20</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'required'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
<span style="color: #0000ff;">'password'</span>      <span style="color: #339933;">=&gt;</span> <span style="color: #000000; font-weight: bold;">new</span> sfValidatorString<span style="color: #009900;">&#40;</span><a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'max_length'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">40</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'required'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
<span style="color: #0000ff;">'password_repeat'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000000; font-weight: bold;">new</span> sfValidatorString<span style="color: #009900;">&#40;</span><a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'max_length'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">40</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'required'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
<span style="color: #0000ff;">'Email'</span>      <span style="color: #339933;">=&gt;</span> <span style="color: #000000; font-weight: bold;">new</span> sfValidatorEmail<span style="color: #009900;">&#40;</span><a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'required'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
<span style="color: #0000ff;">'Birthday'</span>   <span style="color: #339933;">=&gt;</span> <span style="color: #000000; font-weight: bold;">new</span> sfValidatorDate<span style="color: #009900;">&#40;</span><a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'required'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</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;">// J'en profite pour definir un nom formater des champs pour avoir $_POST['utilisateurs['email']] etc</span>
<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">widgetSchema</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setNameFormat</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'utilisateurs[%s]'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

</li>
<li> Normallement à ce stade vous pouvez utiliser la classe de formulaire dans votre fichier apps/MonApp/MonModule/actions/actions.class.php

<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('p35code31'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p3531"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code" id="p35code31"><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> executeMonAction<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
   <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">formInscription</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MaClasseForm<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</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;">request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">isMethod</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'post'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
   <span style="color: #009900;">&#123;</span>
      <span style="color: #666666; font-style: italic;">// récupère les champs du formulaire sous forme de tableau.</span>
     <span style="color: #000088;">$array_champ</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getRequest</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getParameter</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;utilisateurs&quot;</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;">formInscription</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">bind</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$array_champ</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</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;">formInscription</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">isValid</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: #666666; font-style: italic;">// Formulaire valide.</span>
       <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

</li>
<li>Voilà la validation &laquo;&nbsp;classique&nbsp;&raquo; et faite. Et l&#8217;ajax dans tout ça ? On y vient ^^ Alors au lieu d&#8217;afficher le formulaire de manière classique (via le template adéquate apps/MonApp/MonModule/templates/MonActionSuccess.php)

<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('p35code32'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p3532"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
</pre></td><td class="code" id="p35code32"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// Au lieu de</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$form</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// On va rendre le template comme on le souhaite</span>
<span style="color: #666666; font-style: italic;">// Par souci de flemme je ne fait que le champ Email</span>
<span style="color: #339933;">&lt;</span>table<span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;</span>tr<span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;</span>td<span style="color: #339933;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;?</span>php <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$form</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Email '</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">renderLabel</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>td<span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;</span>td<span style="color: #339933;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;?</span>php <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$form</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Email '</span><span style="color: #009900;">&#93;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span><span style="color: #339933;">&lt;/</span>td<span style="color: #339933;">&gt;</span>
   <span style="color: #339933;">&lt;</span>td<span style="color: #339933;">&gt;&lt;</span>span id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;error_for_email &quot;</span><span style="color: #339933;">&gt;</span>
                <span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$form</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Email '</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">hasError</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
		<span style="color: #339933;">&lt;</span>ul <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;error_list&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;">$form</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Email '</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-&lt;</span>getError<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$error</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
			<span style="color: #339933;">&lt;</span>li <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;error_for&quot;</span><span style="color: #339933;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;?</span>php <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$error</span> <span style="color: #000000; font-weight: bold;">?&gt;</span><span style="color: #339933;">&lt;/</span>li<span style="color: #339933;">&gt;</span>
			<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">endforeach</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
		<span style="color: #339933;">&lt;/</span>ul<span style="color: #339933;">&gt;</span>
		<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">endif</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span> <span style="color: #339933;">&lt;/</span>span<span style="color: #339933;">&gt;&lt;/</span>td<span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;/</span>tr<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>table<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

</li>
<li> Et maintenant on va jouer avec l&#8217;ajax pour vérifier ce champ email lors de la saisie.

<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('p35code33'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p3533"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code" id="p35code33"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">/************** Ajax Time :) *****************/</span>
<span style="color: #666666; font-style: italic;">// Dans le template on met :</span>
<span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">// Crée l'observateur pour le champ utilisateurs_email du formulaire.</span>
<span style="color: #b1b100;">echo</span> observe_field<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;utilisateurs_email&quot;</span><span style="color: #339933;">,</span><a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span>
<span style="color: #0000ff;">'update'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;error_for_email&quot;</span><span style="color: #339933;">,</span>
<span style="color: #0000ff;">'url'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;MonModule/VerifierEmail&quot;</span><span style="color: #339933;">,</span>
<span style="color: #0000ff;">'with'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;'email='+ value&quot;</span><span style="color: #339933;">,</span>
<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

</li>
<li>On rajoute l&#8217;action VerifierEmail() dans le fichier d&#8217;actions.

<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('p35code34'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p3534"><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="p35code34"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> executeVerifierEmail<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 des variables internes</span>
	 <span style="color: #000088;">$chaine_a_verifier</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getRequestParameter</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;email&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	 <span style="color: #000088;">$array_erreur</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><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ère le validateur d'email définit dans lib/form/MaClasseForm.class.php</span>
	 <span style="color: #000088;">$form</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MaClasseForm<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	 <span style="color: #000088;">$validateur_form</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getValidatorSchema</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	 <span style="color: #000088;">$all_validateurs_field</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$validateur_form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getFields</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	 <span style="color: #000088;">$email</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$all_validateurs_field</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;email&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
	 <span style="color: #666666; font-style: italic;">// Récupere le bon code d'erreur</span>
	 <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$chaine_a_verifier</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #009900;">&#41;</span>
	 <span style="color: #009900;">&#123;</span>
	 	<span style="color: #000088;">$array_erreur</span> <span style="color: #339933;">=</span>	<a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'email_error'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$email</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;required&quot;</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: #b1b100;">else</span>
	 <span style="color: #009900;">&#123;</span>
	 	<span style="color: #000088;">$array_erreur</span> <span style="color: #339933;">=</span>	<a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'email_error'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$email</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;invalid&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	 <span style="color: #009900;">&#125;</span>
&nbsp;
	 <span style="color: #666666; font-style: italic;">// Crée un validateur d'email hérité de Symfony 1.0</span>
	 <span style="color: #000088;">$validateur_email</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> sfEmailValidator<span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getContext</span><span style="color: #009900;">&#40;</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;">// Un peu de Dry (Don't Repeat Yourself) afin d'éviter d'avoir des messages d'erreurs définit à deux endroits différents.</span>
	 <span style="color: #000088;">$validateur_email</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">initialize</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getContext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$array_erreur</span>  <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	 <span style="color: #666666; font-style: italic;">// Dispatcheur vers le bon templates</span>
	 <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$validateur_email</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$chaine_a_verifier</span><span style="color: #339933;">,</span><span style="color: #000088;">$message_retour</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
	 <span style="color: #009900;">&#123;</span>
	 	<span style="color: #000088;">$message_a_afficher</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getPartial</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;ajax_error&quot;</span><span style="color: #339933;">,</span><a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;message&quot;</span><span style="color: #339933;">=&gt;</span><span style="color: #000088;">$message_retour</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;reussi&quot;</span><span style="color: #339933;">=&gt;</span>true<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: #b1b100;">else</span>
	 <span style="color: #009900;">&#123;</span>
	 	<span style="color: #000088;">$message_a_afficher</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getPartial</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;ajax_error&quot;</span><span style="color: #339933;">,</span><a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;message&quot;</span><span style="color: #339933;">=&gt;</span><span style="color: #000088;">$message_retour</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;reussi&quot;</span><span style="color: #339933;">=&gt;</span>false<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	 <span style="color: #009900;">&#125;</span>
&nbsp;
	 <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">renderText</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$message_a_afficher</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

</li>
<li>Donc en fait on met à jour le &laquo;&nbsp;span&nbsp;&raquo; error_for_email avec le résultat de l&#8217;appel de la méthode VerifierEmail(). J&#8217;ai utilisé un template partial car le but est de le réutiliser pour tous les appels en Ajax. Celui ci n&#8217;est pas bien compliqué

<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('p35code35'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p3535"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p35code35"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>ul<span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;</span>li <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&lt;?php if(<span style="color: #006699; font-weight: bold;">$reussi</span>) echo &quot;</span>accept_for<span style="color: #0000ff;">&quot;; else echo &quot;</span>error_for<span style="color: #0000ff;">&quot;; ?&gt;&quot;</span><span style="color: #339933;">&gt;&amp;</span>amp<span style="color: #339933;">;</span>nbsp<span style="color: #339933;">;</span><span style="color: #000000; font-weight: bold;">&lt;?</span>php <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$message</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
    <span style="color: #339933;">&lt;/</span>li<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>ul<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

</li>
<li>Et voilà, le formulaire fonctionne classiquement et dispose de vérifications en Ajax aussi.</li>
</ol>
<p>Nous venons de voir le validateur d&#8217;Email, mais tous fonctionnent sur le même principe. Voici la méthode pour utiliser le validateur de Sting qui diffère un peu.</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><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('p35code36'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p3536"><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
</pre></td><td class="code" id="p35code36"><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> executeVerifierPseudo<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 des variables internes</span>
    <span style="color: #000088;">$chaine_a_verifier</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/trim"><span style="color: #990000;">trim</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getRequestParameter</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;pseudo&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// Récupère le validateur de pseudo</span>
    <span style="color: #000088;">$form</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MaClasseForm<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$validateur_form</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getValidatorSchema</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$all_validateurs_field</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$validateur_form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getFields</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$validateurs</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$all_validateurs_field</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;FirstName&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$pseudo</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$validateurs</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getValidators</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$pseudo</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$pseudo</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// Crée un validateur de String sf1.0</span>
    <span style="color: #000088;">$validateur_pseudo</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> sfStringValidator<span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getContext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$validateur_pseudo</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">initialize</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getContext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span>
    <span style="color: #0000ff;">'min'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$pseudo</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getOption</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;min_length&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'min_error'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$pseudo</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;min_length&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'max'</span> <span style="color: #339933;">=&gt;</span><span style="color: #000088;">$pseudo</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getOption</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;max_length&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'max_error'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$pseudo</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;max_length&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'required'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$pseudo</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;required&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">// Dispatcheur vers le bon templates</span>
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$validateur_pseudo</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$chaine_a_verifier</span><span style="color: #339933;">,</span><span style="color: #000088;">$message_retour</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;</span>amp<span style="color: #339933;">;&amp;</span>amp<span style="color: #339933;">;</span> <span style="color: #000088;">$chaine_a_verifier</span> <span style="color: #339933;">!=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
       <span style="color: #000088;">$message_a_afficher</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getPartial</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;ajax_error&quot;</span><span style="color: #339933;">,</span><a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;message&quot;</span><span style="color: #339933;">=&gt;</span><span style="color: #000088;">$message_retour</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;reussi&quot;</span><span style="color: #339933;">=&gt;</span><span style="color: #000088;">$bool</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: #b1b100;">else</span>
    <span style="color: #009900;">&#123;</span>
     <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$chaine_a_verifier</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$test</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$pseudo</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;required&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
     <span style="color: #666666; font-style: italic;">// Remplace les jokers des messages d'erreurs.</span>
     <span style="color: #000088;">$min</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$pseudo</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getOption</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;min_length&quot;</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: #000088;">$pseudo</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getOption</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;max_length&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     <span style="color: #000088;">$message_retour</span><span style="color: #339933;">=</span>    <a href="http://www.php.net/preg_replace"><span style="color: #990000;">preg_replace</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;#%value%#&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$chaine_a_verifier</span><span style="color: #339933;">,</span><span style="color: #000088;">$message_retour</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     <span style="color: #000088;">$message_retour</span><span style="color: #339933;">=</span>    <a href="http://www.php.net/preg_replace"><span style="color: #990000;">preg_replace</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;#%min_length%#&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$min</span><span style="color: #339933;">,</span><span style="color: #000088;">$message_retour</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     <span style="color: #000088;">$message_retour</span><span style="color: #339933;">=</span>    <a href="http://www.php.net/preg_replace"><span style="color: #990000;">preg_replace</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;#%max_length%#&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$max</span><span style="color: #339933;">,</span><span style="color: #000088;">$message_retour</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     <span style="color: #000088;">$message_a_afficher</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getPartial</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;ajax_error&quot;</span><span style="color: #339933;">,</span><a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;message&quot;</span><span style="color: #339933;">=&gt;</span><span style="color: #000088;">$message_retour</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;reussi&quot;</span><span style="color: #339933;">=&gt;</span>false<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
   <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">renderText</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$message_a_afficher</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p> Et voilà c&#8217;est fini :) </p>
]]></content:encoded>
			<wfw:commentRss>http://www.oni-ecchi.info/blog/decouverte-php/valide-un-formulaire-en-ajax-avec-symfony-11.html/feed</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Découverte : BOT IRC</title>
		<link>http://www.oni-ecchi.info/blog/decouverte-php/decouverte-bot-irc.html</link>
		<comments>http://www.oni-ecchi.info/blog/decouverte-php/decouverte-bot-irc.html#comments</comments>
		<pubDate>Sun, 31 Aug 2008 15:41:12 +0000</pubDate>
		<dc:creator>Guillaume chave</dc:creator>
				<category><![CDATA[Découverte PHP]]></category>
		<category><![CDATA[bot]]></category>
		<category><![CDATA[irc]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[tutoriel]]></category>

		<guid isPermaLink="false">http://www.oni-ecchi.info/blog/?p=3</guid>
		<description><![CDATA[Bonjour, j&#8217;ai pu découvrir comment créer un bot IRC en PHP grâce à la communauté IRCienne de symfony. (Qu&#8217;est ce qu&#8217;IRC ? Qu&#8217;est ce qu&#8217;un Bot ?). Tout est expliqué sur cette page. Squattant en ce moment le chan (salon de discutions) de symfony-fr, je passe mon temps à remercier les uns les autres. Depuis [...]]]></description>
			<content:encoded><![CDATA[<p>Bonjour, j&#8217;ai pu découvrir comment créer un bot <a href="http://www.oni-ecchi.info/blog/tag/irc" class="st_tag internal_tag" rel="tag" title="Voir les articles classés avec irc">IRC</a> en <a href="http://www.oni-ecchi.info/blog/tag/php" class="st_tag internal_tag" rel="tag" title="Voir les articles classés avec php">PHP</a> grâce à la communauté IRCienne de <a href="http://www.oni-ecchi.info/blog/tag/symfony" class="st_tag internal_tag" rel="tag" title="Voir les articles classés avec Symfony">symfony</a>. (<a href="http://fr.wikipedia.org/wiki/Internet_Relay_Chat" target="_blank">Qu&#8217;est ce qu&#8217;IRC </a>?<a href="http://fr.wikipedia.org/wiki/Robot_IRC" target="_blank"> Qu&#8217;est ce qu&#8217;un Bot ?</a>). Tout est expliqué sur <a href="http://classes.scriptsphp.org/article.Un-bot-IRC" target="_blank">cette page</a>.</p>
<p>Squattant en ce moment le chan (salon de discutions) de <a href="http://trac.symfony-project.org/wiki/Resources/fr_FR" target="_blank">symfony-fr</a>, je passe mon temps à remercier les uns les autres. Depuis peu même j&#8217;arrive à aider les nouveaux ^^. Je me suis donc dit que ca serait bien d&#8217;avoir un système pour compatibiliser le nombre de &laquo;&nbsp;merci&nbsp;&raquo; par personnes et d&#8217;en sortir quelques statistiques.</p>
<p>Ainsi est née le bot qui sert à avoir une système à points. Chacun peut donc donner, ou enlever un point, et le robot fera le compte.</p>
<h3>Fonctionnement<a href="http://www.oni-ecchi.info/blog/wp-content/uploads/2008/08/bot_example.png" target="_blank"><img class="size-thumbnail wp-image-15 alignright" title="bot_example" src="http://www.oni-ecchi.info/blog/wp-content/uploads/2008/08/bot_example-150x150.png" alt="" width="150" height="150" /></a></h3>
<p>Après quelques discutions sur le channel de symfony, j&#8217;ai mis au point 4 petites commandes que le bot va interpréter :</p>
<p>!point guillaume<br />
!malus guillaume<br />
!bonus guillaume<br />
!total</p>
<h3>Comment ca marche ?</h3>
<p>Le bot est écrit en PHP 5 et se lance via une ligne de commande du style :</p>
<blockquote><p>php -f utilisation.php</p></blockquote>
<p>Il n&#8217;y a que peu de classes pour le faire fonctionner :</p>
<ol>
<li>Une classe de connexion qui s&#8217;occupe de créer un flux de donnée (une socket via fsockopen), de se connecter à un channel irc,  de pouvoir transmettre des commandes IRC et de récupérer ligne par ligne ce qui se passe sur le channel.</li>
<li>Une classe principal qui permet de garder le flux de donné ouvert (et donc fait en sorte que le bot reste sur le channel IRC), et qui permet de lancer des Plugins. Son rôle tient plus d&#8217;un controlleur PHP que d&#8217;une classe à mon sens.</li>
<li>Une interface et une classe de base pour la gestion des Plugins. En fait un plugin est une classe qui contient le coeur d&#8217;un programme et permet de réagir vis à vis d&#8217;un évènement sur le channel. Un plugin fait donc office <a href="http://www.design-patterns.fr/Observateur.html" target="_blank">d&#8217;observateur</a> puisqu&#8217;il permet d&#8217;écouter des évènements mais il s&#8217;obverse lui même vu qu&#8217;il traite ces évènements et y répond.</li>
</ol>
<p>J&#8217;ai donc créer un plugin et modifier un temps soit peu la classe principal pour avoir à tout moment la liste des utilisateurs du channel. Au final le <a href="http://www.oni-ecchi.info/blog/tag/code" class="st_tag internal_tag" rel="tag" title="Voir les articles classés avec code">code</a> n&#8217;est pas bien compliquer et bien long (330 lignes à tout casser). Les points sont stocker dans une base de données local (voui, pas besoin de serveur en ligne vu que l&#8217;on lance le bot en local on peut accèder à une base en local ^^). Quelques random rendent le bot sympathiques pour avoir des phrases de réponses un peu plus humaine.</p>
<p>Je ne vous donne pas le code source de cette petite application car je le juge &laquo;&nbsp;pas très propre&nbsp;&raquo; et je pense y faire encore pas mal de modifications dessus :)</p>
<h3>Pour la suite</h3>
<p>Je prévois de faire une application <a href="http://symfony-project.com/" target="_blank">symfony </a>pour ajouter/modifier des phrases de réponses clées. Faire des graphiques de statistiques, et ré-initalisé la base de données à souhait. Tout n&#8217;est pas encore fixé, mais j&#8217;aimerais bien une console d&#8217;administration du bot à distance sympathique :)</p>
<p>Voilà voilà</p>
]]></content:encoded>
			<wfw:commentRss>http://www.oni-ecchi.info/blog/decouverte-php/decouverte-bot-irc.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

