<?xml version="1.0" encoding="UTF-8" ?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
  <title>Latest snippets tagged php</title>
  <link rel="alternate" href="http://snippets.prendreuncafe.com/snippets/tagged/php/order_by/date"></link>
  <id>http://snippets.prendreuncafe.com/snippets/tagged/php/order_by/date</id>
  <updated>2008-07-22T15:09:37Z</updated>
  <author>
    <name>Symfony</name>
    <author_email>noreply@symfony-project.com</author_email>
  </author>
<entry>
  <title>Autocomplétion au sein d'eclipse</title>
  <link href="http://snippets.prendreuncafe.com/snippet/95"></link>
  <updated>2008-07-22T15:09:37Z</updated>
  <id>95</id>
  <summary type="html">Copier-coller le code suivant dans un fichier xml :

[code]
&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?&gt;&lt;templates&gt;&lt;template autoinsert=&quot;true&quot; context=&quot;php&quot; deleted=&quot;false&quot; description=&quot;create a Symfony Action&quot; enabled=&quot;true&quot; name=&quot;action&quot;&gt;public function execute${Action} {
  ${body}
}&lt;/template&gt;&lt;template autoinsert=&quot;true&quot; context=&quot;php&quot; deleted=&quot;false&quot; description=&quot;Create a class controller&quot; enabled=&quot;true&quot; name=&quot;controller&quot;&gt;class ${ControllerName}Actions extends sfActions  {

    public  function execute${action}()

    {
      ${body}
    }

}
&lt;/template&gt;&lt;template autoinsert=&quot;true&quot; context=&quot;php&quot; deleted=&quot;false&quot; description=&quot;criteria doSelect&quot; enabled=&quot;true&quot; name=&quot;doselect&quot;&gt;$$c = new Criteria;
$$${objects} = ${propelObject}Peer::doSelect($$c);&lt;/template&gt;&lt;/templates&gt;
[/code]

## Installation au sein d'eclipse 

* Dans eclipse allez dans Window &gt; Preferences
* Choisir PHP dans la liste des préférences
* Choisir Template et cliquer sur Import
* Selectionner le fichier xml puis accepter

__Note:__ _Le mieux serait de s'inspirer des templates de texmate afin de se monter un système de templating complet pour Eclipse. Ajout également de commentaires compatibles phpdocumentor_
</summary>
</entry>
<entry>
  <title>[Symfony] Effacer partiellement le cache d'une application</title>
  <link href="http://snippets.prendreuncafe.com/snippet/94"></link>
  <updated>2008-05-26T15:34:00Z</updated>
  <id>94</id>
  <summary type="html">Toujours utile depuis un contexte d'application différent de celui visé :

[code=php]
sfToolkit::clearGlob(sfConfig::get('sf_cache_dir').'/frontend/*/all/*/templates/toto.cache');
[/code]</summary>
</entry>
<entry>
  <title>Passer outre le &quot;allow_url_fopen et allow_url_include&quot; a Off</title>
  <link href="http://snippets.prendreuncafe.com/snippet/91"></link>
  <updated>2008-05-13T21:22:11Z</updated>
  <id>91</id>
  <summary type="html">Qui n'a jamais eu un Warning de type : 
&quot;file_get_contents() function.file-get-contents: URL file-access is disabled in the server configuration in % on line %&quot;  sur un hebergement mutualisé ? 

la solution et d'utiliser lynx en ligne de commande pour attaquer une url, on utilisera la fonction exec(), exemple avec l'api GoogleMaps

[code]
&lt;?php

define('GOOGLE_API_KEY', 'your_google_api_key_here');
$wsurl = 'http://maps.google.com/maps/geo?q=%s&amp;output=csv&amp;key=%s';
$location = 'Paris, France';

$data = explode(',', exec('lynx --dump \''.sprintf($wsurl, urlencode($location), GOOGLE_API_KEY).'\''));
/*** Au lieu de ***/
$data = explode(',', file_get_contents(sprintf($wsurl, urlencode($location), GOOGLE_API_KEY)));


$coord = 200 === (int)$data[0] ? array((float)$data[2], (float)$data[3]) : null;
var_dump($coord);
?&gt;

[/code]</summary>
</entry>
<entry>
  <title>[Symfony] Un validateur de date qu'il est bien</title>
  <link href="http://snippets.prendreuncafe.com/snippet/76"></link>
  <updated>2008-03-16T08:14:55Z</updated>
  <id>76</id>
  <summary type="html">Quelques fonctionnalités en plus par rapport à sfDateValidator (qu'il étend), notamment l'interdiction de date futures ou passées et l'évitement du traitement de la langue courante utilisateur (utile pour les formats de date figés).

[code php]
/**
 * Custom date validator
 *
 */
class myDateValidator extends sfDateValidator
{
  /**
   * Execute this validator.
   *
   * @param mixed A file or parameter value/array
   * @param error An error message reference
   *
   * @return bool true, if this validator executes successfully, otherwise false
   */
  public function execute(&amp;$value, &amp;$error)
  {
    $culture = $this-&gt;getContext()-&gt;getUser()-&gt;getCulture();

    // Validate the given date
    if ($this-&gt;getParameter('with_culture'))
    {
      $value1 = $this-&gt;getValidDate($value, $culture);
    }
    else
    {
      $value1 = strtotime($value);
    }

    if (!$value1 || -1 == $value1) // Before php 5.1, strtotime() returns -1 on fail
    {
      $error = $this-&gt;getParameter('date_error');
      return false;
    }

    // Is there a compare to do?
    $compareDateParam = $this-&gt;getParameter('compare');
    $compareDate = $this-&gt;getContext()-&gt;getRequest()-&gt;getParameter($compareDateParam);

    // If the compare date is given
    if ($compareDate)
    {
      $operator = trim($this-&gt;getParameter('operator', '=='), '\'&quot; ');
      $value2 = $this-&gt;getValidDate($compareDate, $culture);

      // If the check date is valid, compare it. Otherwise ignore the comparison
      if ($value2)
      {
        $valid = false;
        switch ($operator)
        {
          case '&gt;':
            $valid = $value1 &gt;  $value2;
            break;
          case '&gt;=':
            $valid = $value1 &gt;= $value2;
            break;
          case '==':
            $valid = $value1 == $value2;
            break;
          case '&lt;=':
            $valid = $value1 &lt;= $value2;
            break;
          case '&lt;':
            $valid = $value1 &lt;  $value2;
            break;

          default:
            throw new sfValidatorException(sprintf('Invalid date comparison operator &quot;%s&quot;', $operator));
        }

        if (!$valid)
        {
          $error = $this-&gt;getParameter('compare_error');

          return false;
        }
      }
    }

    if (!$this-&gt;getParameter('allow_future') &amp;&amp; $value1 &gt; time())
    {
      $error = $this-&gt;getParameter('future_error');
      return false;
    }

    if (!$this-&gt;getParameter('allow_past') &amp;&amp; $value1 &lt; time())
    {
      $error = $this-&gt;getParameter('past_error');
      return false;
    }

    return true;
  }

  /**
   * Initializes the validator.
   *
   * @param sfContext The current application context
   * @param array   An associative array of initialization parameters
   *
   * @return bool true, if initialization completes successfully, otherwise false
   */
  public function initialize($context, $parameters = null)
  {
    // Initialize parent
    parent::initialize($context, $parameters);

    // Set defaults
    $this-&gt;getParameterHolder()-&gt;set('with_culture', true);
    $this-&gt;getParameterHolder()-&gt;set('allow_future', true);
    $this-&gt;getParameterHolder()-&gt;set('future_error', 'Future dates not allowed');
    $this-&gt;getParameterHolder()-&gt;set('allow_past', true);
    $this-&gt;getParameterHolder()-&gt;set('past_error', 'Past dates not allowed');
    $this-&gt;getParameterHolder()-&gt;add($parameters);

    return true;
  }
}

[code]</summary>
</entry>
<entry>
  <title>Compiler tous les fichiers PHP en un seul</title>
  <link href="http://snippets.prendreuncafe.com/snippet/75"></link>
  <updated>2008-03-15T20:29:08Z</updated>
  <id>75</id>
  <summary type="html">Pour faire un gros fichier comportant l'intégralité d'un projet par exemple :

[code]
$ find . -name &quot;*.php&quot; |xargs php -w &gt; mongrosprojet.php
[/code]</summary>
</entry>
<entry>
  <title>Formatter un document XML avec DOM et PHP5</title>
  <link href="http://snippets.prendreuncafe.com/snippet/66"></link>
  <updated>2007-04-16T22:23:23Z</updated>
  <id>66</id>
  <summary type="html">Ça peut bien aider.

[code php]
$dom = new DOMDocument('1.0', 'UTF-8');
$dom-&gt;preserveWhiteSpace = false;
$dom-&gt;load('/path/to/file.xml');
$dom-&gt;formatOutput = true;
file_put_contents('formatted.xml', $dom-&gt;saveXML());
[/code]</summary>
</entry>
<entry>
  <title>[Symfony] Chemins systèmes</title>
  <link href="http://snippets.prendreuncafe.com/snippet/48"></link>
  <updated>2007-01-29T19:43:31Z</updated>
  <id>48</id>
  <summary type="html">Voici les chemins systèmes par défaut dans Symfony.

[code]
// root directory structure
'sf_cache_dir_name'   =&gt; 'cache',
'sf_log_dir_name'     =&gt; 'log',
'sf_lib_dir_name'     =&gt; 'lib',
'sf_model_dir_name'   =&gt; 'model',
'sf_web_dir_name'     =&gt; 'web',
'sf_data_dir_name'    =&gt; 'data',
'sf_config_dir_name'  =&gt; 'config',
'sf_apps_dir_name'    =&gt; 'apps',
 
// global directory structure
'sf_app_dir'        =&gt; $sf_root_dir.DIRECTORY_SEPARATOR.'apps'.DIRECTORY_SEPARATOR.$sf_app,
'sf_model_dir'      =&gt; $sf_root_dir.DIRECTORY_SEPARATOR.'model',
'sf_lib_dir'        =&gt; $sf_root_dir.DIRECTORY_SEPARATOR.'lib',
'sf_web_dir'        =&gt; $sf_root_dir.DIRECTORY_SEPARATOR.'web',
'sf_upload_dir'     =&gt; $sf_root_dir.DIRECTORY_SEPARATOR.'web'.DIRECTORY_SEPARATOR.'uploads',
'sf_base_cache_dir' =&gt; $sf_root_dir.DIRECTORY_SEPARATOR.'cache'.DIRECTORY_SEPARATOR.$sf_app,
'sf_cache_dir'      =&gt; $sf_root_dir.DIRECTORY_SEPARATOR.'cache'.DIRECTORY_SEPARATOR.$sf_app.DIRECTORY_SEPARATOR.$sf_environment,
'sf_log_dir'        =&gt; $sf_root_dir.DIRECTORY_SEPARATOR.'log',
'sf_data_dir'       =&gt; $sf_root_dir.DIRECTORY_SEPARATOR.'data',
'sf_config_dir'     =&gt; $sf_root_dir.DIRECTORY_SEPARATOR.'config',
[/code]</summary>
</entry>
<entry>
  <title>[Symfony] Autoloader toutes les classes d'un répertoire particulier</title>
  <link href="http://snippets.prendreuncafe.com/snippet/44"></link>
  <updated>2007-01-17T11:05:08Z</updated>
  <id>44</id>
  <summary type="html">Il est très simple de charger automatiquement toutes les classes PHP définies dans un répertoire avec Symfony :

[code]
require_once($sf_symfony_lib_dir.'/util/sfCore.class.php');
sfCore::initSimpleAutoload('/path/to/libs');
[/code]</summary>
</entry>
<entry>
  <title>Réduire le coût de performances de require_once</title>
  <link href="http://snippets.prendreuncafe.com/snippet/41"></link>
  <updated>2007-01-15T23:17:55Z</updated>
  <id>41</id>
  <summary type="html">La fonction require_once étant assez coûteuse en PHP, il peut être intéressant de tester l'existence de l'objet que le fichier définit avant de le charger d'emblée :

[code]
class_exists('sfCache') or require_once($sf_symfony_lib_dir.'/cache/sfCache.class.php');
[/code]</summary>
</entry>
<entry>
  <title>[Symfony] Traiter les OR sql avec Propel</title>
  <link href="http://snippets.prendreuncafe.com/snippet/40"></link>
  <updated>2007-01-15T12:05:04Z</updated>
  <id>40</id>
  <summary type="html">Voici comment gérer les &quot;ou&quot; SQL dans Propel :

[code]
$criteria = new Criteria();
$criteria-&gt;add(TotoPeer::NAME, 'Gérard Bouchard');
$criterion = $criteria-&gt;getNewCriterion (
  TotoPeer::ID, 5
)-&gt;addOr($criteria-&gt;getNewCriterion (
  TotoPeer::ID, 10
));
$criteria-&gt;addAnd($criterion);
TotoPeer::doSelect($criteria);
[/code]

Ceci donnera quelque chose comme :

[code]
SELECT 
  * 
FROM 
  toto 
WHERE 
  toto.NAME = 'Gérard Bouchard' 
  AND 
  (
    toto.ID = 5 
    OR 
    toto.ID = 10
  );
[/code]</summary>
</entry>
</feed>