Warning: ob_start(): function ” not found or invalid function name Symfony

If you are installing a symfony 1.x project on a PHP 5.4.1 (or later) version, you may encounter this warning message :
Warning: ob_start(): function ” not found or invalid function name in /var/www/lib/config/sfApplicationConfiguration.class.php on line 157 Notice: ob_start(): failed to create buffer in /var/www/lib/config/sfApplicationConfiguration.class.php on line 157

To fix it, go on your /lib/config/sfApplicationConfiguration.class.php file and change this

// compress output
    if (!self::$coreLoaded)
    {
      ob_start(sfConfig::get('sf_compressed') ? 'ob_gzhandler' : '');
    }

to this

// compress output
    if (!self::$coreLoaded)
    {
      ob_start(sfConfig::get('sf_compressed') ? 'ob_gzhandler' : null);
    }

PHP sort array by sub value

Here my array structure :

Array
(
    [33] => Array
        (
            [id_site] => 33
            [datas] => Array
                (
                    [id] => 2965
                    [site_id] => 33
                    [operation_id] => 20
                    [positionnement] => 1
                )
            [statut] => Validé DM
            [sort] => 0119
        )
    [32] => Array
        (
            [id_site] => 32
            [datas] => Array
                (
                    [id] => 1929
                    [site_id] => 32
                    [operation_id] => 20
                    [positionnement] => 1
                 )
            [statut] => Validé DM
            [sort] => 0114
        )
    [34] => Array
        (
            [id_site] => 34
            [datas] => Array
                (
                    [id] => 2230
                    [site_id] => 34
                    [operation_id] => 20
                    [positionnement] => 1
                )
            [statut] => Validé DM
            [sort] => 0128
        )
)

How to sort by the [sort] value inside my array ?

I have to use the usort() function :

function sortByCustom($a, $b) {
    return strcmp($a['sort'], $b['sort']);
}
...
usort($array, 'sortByCustom');

Here the result :

Array
(
    [0] => Array
        (
            [id_site] => 32
            [datas] => Array
                (
                    [id] => 1929
                    [site_id] => 32
                    [operation_id] => 20
                    [positionnement] => 1
                 )
            [statut] => Validé DM
            [sort] => 0114
        )

    [1] => Array
        (
            [id_site] => 33
            [datas] => Array
                (
                    [id] => 2965
                    [site_id] => 33
                    [operation_id] => 20
                    [positionnement] => 1
                )
            [statut] => Validé DM
            [sort] => 0119
        )
    [2] => Array
        (
            [id_site] => 34
            [datas] => Array
                (
                    [id] => 2230
                    [site_id] => 34
                    [operation_id] => 20
                    [positionnement] => 1
                )
            [statut] => Validé DM
            [sort] => 0128
        )
)

If you want to maintain the index value association, use the uasort() function

uasort($array, 'sortByCustom');

Here the result :

Array
(
    [32] => Array
        (
            [id_site] => 32
            [datas] => Array
                (
                    [id] => 1929
                    [site_id] => 32
                    [operation_id] => 20
                    [positionnement] => 1
                 )
            [statut] => Validé DM
            [sort] => 0114
        )

    [33] => Array
        (
            [id_site] => 33
            [datas] => Array
                (
                    [id] => 2965
                    [site_id] => 33
                    [operation_id] => 20
                    [positionnement] => 1
                )
            [statut] => Validé DM
            [sort] => 0119
        )

    [34] => Array
        (
            [id_site] => 34
            [datas] => Array
                (
                    [id] => 2230
                    [site_id] => 34
                    [operation_id] => 20
                    [positionnement] => 1
                )
            [statut] => Validé DM
            [sort] => 0128
        )
)

If you use this in a class, you have to define the sort function outside the class

<?php
function sortByCustom($a, $b)
{
        return strcmp($a['sort'], $b['sort']);
}
class positionnemen {
...

for strict number comparaison, you can also use this function

  function sortByCustom($a, $b)
  {
      if($a['nbr'] > $b['nbr'])
      {
          return 1;
      }
      return -1;
  }

fpdf add page number

Here an easy way to add page number at the end of a fpdf document

In your fpdf class, before calling addpage(), call the AliasNbPages() fonction

$this->AliasNbPages();

then create a footer() fonction to override the default empty footer

function Footer()
{
        $this->SetY(-10);
        $this->SetFont('Arial', '', 8);
        $this->Cell(0,10,'Page '.$this->PageNo().'/{nb}', 0, 0, 'C');
        $this->SetY(-10);
        $this->Cell(0,10,"date d'impression: ".date('d/m/Y'), 0, 0, 'R');
}

The complete class : file pdfListeDemandes.class.php

<?php

class pdfListeDemandes extends FPDF
{
   function init()
   {
                $this->AliasNbPages();
                ...
   }
   function writeListe()
   {
         $this->init();
         ...
   }

   function Footer()
   {
        $this->SetY(-10);
        $this->SetFont('Arial', '', 8);
        $this->Cell(0,10,'Page '.$this->PageNo().'/{nb}', 0, 0, 'C');
        $this->SetY(-10);
        $this->Cell(0,10,"date d'impression: ".date('d/m/Y'), 0, 0, 'R');
   }

}

And the code to call your class

$pdf = new pdfListeDemandes();
$pdf->writeListe();
$pdf->output('Liste_demande.pdf', 'D');
exit;

Symfony 1.4 format date doctrine object

To format a date from a doctrine object, you can use this code, quick and easy.

$demande->getDateTimeObject('date_creation')->format('d/m/Y');

Sometimes, this first solution return current date even if the doctrine record is null.
You then have to use this other solution

use_helper('Date');
format_date($demande->getDateCreation(), 'dd-MM-yyyy');

You can found format rules here

Symfony 1.4 multi fields validator

In your form class (lib/form/doctrine/classForm.class.php)

  $this->validatorSchema->setPostValidator(
      new sfValidatorMultiFields(
        array('field1', 'field2', 'field3'),
        array('labels' => array('name1', 'name2', 'name3')),
        array('global_invalid' => 'Multi fields error')));

save this class in a lib folder, for exemple apps/frontend/lib/sfValidatorMultiFields.class.php

<?
class sfValidatorMultiFields extends sfValidatorSchema
{
 
  public function __construct(array $fields, $options = array(), $messages = array())
  {
    $this->addOption('fields', $fields);

    $this->addMessage('invalid', 'Error with this field');
    $this->addMessage('global_invalid', 'Multi fields error');

    parent::__construct(null, $options, $messages);
  }

  protected function doClean($values)
  {
    $valid = false;
    foreach ($this->getOption('fields') as $field) {
	//put here your validation code
        // ex: $values[$field] ....
    }
    //another example, at least one field in not empty
    if($values['field1'] != '' || $values['field2'] != '' || $values['field3'] != '')
    {
        $valid = true;
    }

    if ($valid)
    {
      $errorSchema = new sfValidatorErrorSchema($this);

        // The global error
        $errorSchema->addError(new sfValidatorError($this, 'global_invalid', array()));
      }

      // Fields error
      $error = new sfValidatorError($this, 'invalid', array()));

      // Add the error for each defined fields
      foreach ($this->getOption('fields') as $field)
      {
        $errorSchema->addError($error, $field);
      }

      throw $errorSchema;
    }

    return $values;
  }
}
?>

The keys points are :
– use $this->validatorSchema->setPostValidator() in your form class
– all fields are now available from the $values array ($values[‘field1′], $values[‘field2′] …)

php unzip no 1024 limit

Here, a simple script to unzip a .zip file in php without the 1024 characters limitations

function unzip($zip, $root, $target_folder = "") 
{
    $zip_file = zip_open($root . $zip);
    while ($zip = zip_read($zip_file))
    {
        $zip_object = zip_entry_name($zip);
        if (!is_dir(dirname($root . $target_folder . $zip_object))) 
        {
            mkdir(dirname($root . $target_folder . $zip_object), 0777, true);
        }
        $hedef_doc = $root . $target_folder . $zip_object;
        if(substr($hedef_doc, -1) != "/")
        {
            touch($hedef_doc);
            $target_file = fopen($hedef_doc, 'w');
            $size = 0;
            while($size < zip_entry_filesize($zip))
            {
                 fwrite($target_file, zip_entry_read($zip));
                 $size += 1024;
            }
     	     fclose($target_file);
        }
    }
    return true;
}

unzip("file.zip", "./", "target_directory");