My Developpement Blog

Aller au contenu | Aller au menu | Aller à la recherche

Développement Web › Zend Framework

Sous-catégories

View_Helper

Quelques aides de vue développer lors de ma prise en main du Zend Framework

Classes

Le paradoxe de ZendX_JQuery et Zend_Dojo

On parle beaucoup de découplage que ce soit niveau front-end ( HTML + CSS + Javascript ), chacun ayant une utilité propre. Je développerai cela dans un prochain billet.
mais aussi coté serveur et en particulier lors de l’utilisation du design pattern MVC

Et là, nous voyons apparaître Zend_Dojo et ZendX_JQuery, qui permet de coupler du javascript et du PHP.

Que l’on m’explique l’intérêt de faire cela ?

Je vais pour ma part vous expliquer les inconvénients que j’entrevois :

  • Du code PHP mort dans le cas d’un client n’ayant pas javascript activé. Pour moi, c’est une perte de temps serveur, des calculs inutiles même si on peut utiliser des caches à plusieurs niveau (opcode ou autres)
  • Une modification fonctionnelle du javascript doit faire intervenir le développeur PHP voire potentiellement remettre en cause le développement d’une partie du back-end,
  • Le développeur back-end et le développeur front-end ont 2 métiers différents, complémentaires mais différents.

ND_Form

ND_Form est une classe héritant de Zend_Form permettant de ne s’attarder que sur la configuration du formulaire via fichier Ini.

Lire la suite

Classe ZF permettant de faire des representations intervallaire

Si vous utilisez le Zend Framework et que vous cherchez à représenter une arborescence type Menu, Organigramme, ...

C'est juste une simple mise en forme de cet article concernant la représentation intervallaire d'une arborescence

Il me reste la gestion des niveaux à prendre en compte, histoire de faire des filtres supplémentaire. Mais bon, chaque chose en son temps.

Comme toujours, je suis tout ouïe aux remarques voire même aux critiques

Appel de la classe

<?php
 
class Kernel_Models_Data_Role_Table extends ND_Db_Table_Hierarchical_Table{
 
   protected $_name      = 'role';
   protected $_rowClass = 'Kernel_Models_Data_Role_Row';
   protected $_left          = 'bg'; // defini la borne gauche (obligatoire)
   protected $_right        = 'bd'; // defini la borne droite (obligatoire) 
   protected $_level        = 'level' // defini la profondeur de l'arborescence (obligatoire) 
 
 
}

Et pour ceux qui ont la flemme de télécharger le fichier

<?php
/**
 * 
 * @author ndesaleux
 * @package ND
 * @subpackage Db
 * 
 */
 
class ND_Db_Table_Hierarchical_Table extends Zend_Db_Table{
 
   protected $_left   = null ;
   protected $_right  = null ;
   protected $_level  = null ;   
 
   /**
    * 
    * @param (array) $config 
    * @return null
    */
   public function __construct($config = array()){
      parent::__construct($config);
      if (null == $this->_left)   throw new ND_Db_Table_Hierarchical_Exception(ND_Db_Table_Hierarchical_Exception::NO_LEFT_BORDER_FIELD_DEFINED);
      if (null == $this->_right)  throw new ND_Db_Table_Hierarchical_Exception(ND_Db_Table_Hierarchical_Exception::NO_RIGHT_BORDER_FIELD_DEFINED);
      if (null == $this->_level)  throw new ND_Db_Table_Hierarchical_Exception(ND_Db_Table_Hierarchical_Exception::NO_LEVEL_FIELD_DEFINED);      
   }
 
   /**
    * 
    * @param (int) $id
    * @return (mixed) false|Zend_Db_Table_Row_Abstract
    */
   public function getById($id) {
      $rows = $this->find($id);
      if ($rows) {
         return $rows->current();
      }
      return false;
   }
 
   /**
    * Delete an element get by id (this delete also node and leaf)
    * Update bor
    * @param (int) $id
    * @return (boolean)
    */
   public function deleteById($id){
 
       $elt = $this->getById($id)    ;    
       if ($elt instanceof Zend_Db_Table_Row_Abstract ){
 
           $left  = $this->_left;
           $right = $this->_right;           
           $dif   = $elt->$right- $elt->$left + 1; 
           // on supprime les elements noeuds et feuilles
           $dlElt = 'DELETE FROM '.$this->_name.' 
           		      WHERE '.$this->_left.' >= '.$elt->$left.'
           		        AND '.$this->_right.' <= '.$elt->$right ;            
           // MAJ des bornes droites 
           $upRb = 'UPDATE '.$this->_name.' 
                       SET '.$this->_right.' = '.$this->_right.' - '.$dif.' 
                     WHERE '.$this->_right.' >= '.$elt->$left ;
           // MAj des bornes gauches
           $upLb = 'UPDATE '.$this->_name.' 
                       SET '.$this->_left.' = '.$this->_left.' - '.$dif.' 
                     WHERE '.$this->_left.' > '.$elt->$left ;
 
           try{
               $this->_db->beginTransaction();
               $this->_db->prepare($dlElt)->execute();
               $this->_db->prepare($upRb)->execute();
               $this->_db->prepare($upLb)->execute();
               $this->_db->commit();
               return true;
           }catch(Exception $e){
                $this->_db->rollBack();
                return false ;
           }                  
       }
 
 
   }
 
   /**
    * Add a leaf at the node $id 
    * 
    * @param (array) $data
    * @param (int) $id
    * @return (mixed) false|int
    */
   public function insertByParent(array $data, $id){
       $elt = $this->getById($id);
       if ($elt instanceof Zend_Db_Table_Row_Abstract ){
           $right = $this->_right;
           $level = $this->_level;     
 
           // MAJ des bornes droites 
           $upRb = 'UPDATE '.$this->_name.' 
                       SET '.$this->_right.' = '.$this->_right.' + 2 
                     WHERE '.$this->_right.' >= '.$elt->$right ;
           // MAj des bornes gauches
           $upLb = 'UPDATE '.$this->_name.' 
                       SET '.$this->_left.' = '.$this->_left.' + 2 
                     WHERE '.$this->_left.' >= '.$elt->$right ;           
 
           $inElt = 'INSERT INTO '.$this->_name.' 
                      ( '.$this->_left.', '.$this->_right.', '.$this->_level.' ) 
                     VALUES ( '.$elt->$right.', '.($elt->$right+1).', '.($elt->$level+1).' ) ';
 
           try{
               $this->_db->beginTransaction();
               $this->_db->prepare($upRb)->execute();
               $this->_db->prepare($upLb)->execute();
               $this->_db->prepare($inElt)->execute();
               $this->_db->commit();
 
               $oElt = $this->fetchRow($this->_left.' = '.$elt->$right.' AND '.$this->_right.' = '.($elt->$right+1));
 
               // clean data
               foreach( $this->_primary as $primaryK ){
                   unset( $data[$primaryK] );
               }
               unset( $data[$this->_left] );
               unset( $data[$this->_right] );
               unset( $data[$this->_level] );
               $oElt->setFromArray($data);
               Zend_Debug::dump($data);                              
               return $oElt->save();
 
           }catch(Exception $e){
               $this->_db->rollBack();
           }            
       }
       return false ; 
   }
 
   /**
    * Update a node or a leaf, left and right border can not be update
    * 
    * @param (array) $data
    * @param (int) $id
    * @return boolean
    */
   public function updateById(array $data, $id){
       unset( $data[$this->_left] );
       unset( $data[$this->_right] );
       unset( $data[$this->_level] );
       $elt = $this->getById($id);
       if ( $elt instanceof Zend_Db_Table_Row_Abstract ){           
           try{
               $elt->setFromArray($data);
               $elt->save();
               return true;  
           }catch(Zend_Db_Exception $e){
               //@todo
           }
       }
       return false;
   }
 
 
   /**
    * Get node and leaf come from node identified by $id
    * @param (int) $id
    * @param (int) $level optional
    * @return (mixed) array|false
    */
   public function getChildren($id, $lvl = null){
      $elt = $this->getById($id);
      if ( $elt instanceof Zend_Db_Table_Row_Abstract ){
          $left  = $this->_left;
          $right = $this->_right;        
          $select = $this->select()->from($this->_name)  
                                   ->where($this->_left.' > ? ', $elt->$left )
                                   ->where($this->_right.' < ? ', $elt->$right );
          if ( $lvl !== null && (int) $lvl > 0 ){
              $level = $this->_level ;
              $select->where($this->_level.' <= ? ', $elt->$level + (int) $lvl );
          }
          return $select->query()->fetchAll();          
      }else{
          return false;
      }   
   }
 
 
   /**
    * get parent of the element 
    * @param (int) $id
    * @return (mixed) Zend_Db_Table_Row_Abstract|false
    */
   public function getParent($id){
       $elt = $this->getById($id);
       if ( $elt instanceof Zend_Db_Table_Row_Abstract ){
           $left  = $this->_left  ;
           $level = $this->_level ;
           $select = $this->select()->from($this->_name)
                                    ->where($this->_left.' < ?',$elt->$left )
                                    ->where($this->_level.' = ?',$elt->$level-1)
                                     ->order($this->_left.' DESC' )
                                    ->limit(1);
           return $select->query()->fetchAll(); 
       }else{
           return false;
       }
   }
 
 
   /**
    * count children of an element 
    * @param $id
    * @return (mixed) false|int
    */
   public function countChildren($id){
       $return = $this->getChildren($id) ;
       return $return === false  ? false : count($return) ;
   }
 
   /**
    * return number of node of the element $id
    * @param (int) $id
    * @return (mixed) false|int
    */
   public function countNode($id){
       $oElt = $this->getById($id);
       if ( $oElt instanceof Zend_Db_Table_Row_Abstract ){
           $left   = $this->_left  ;
           $right  = $this->_right ;     
           $select = $this->select()
                          ->from($this->_name, array('count(id) as nb') )  
                          ->where($this->_right.' - '.$this->_left.' <> 1 ')
                          ->where($this->_left.' > ? ' , $oElt->$left )
                          ->where($this->_right.' < ? ', $oElt->$right );
           return $select->query()->fetchColumn(0);
       }
       return false ;
   }
 
   /**
    * 
    * @param (int) $id
    * @return (mixed) false|int 
    */
   public function countLeaf($id){
       $oElt = $this->getById($id);
       if ( $oElt instanceof Zend_Db_Table_Row_Abstract ){
           $left   = $this->_left  ;
           $right  = $this->_right ;
           $select = $this->select()
                          ->from($this->_name, array('count(id) as nb') )  
                          ->where($this->_right.' - '.$this->_left.' = 1 ')
                          ->where($this->_left.' > ? ' , $oElt->$left )
                          ->where($this->_right.' < ? ', $oElt->$right );
           return $select->query()->fetchColumn(0);           
       }
       return false ;       
   }
 
   /**
    * Move a subtree (node or leaf are allowed to move)
    * 
    * @param (int) $id
    * @param (int) $newParentId
    * @return (boolean)
    */
 
   public function moveTree($id, $newParentId){       
       $oEltToMove    = $this->getById($id);
       $oEltToReceive = $this->getById($newParentId);
 
       if ( $oEltToMove    instanceof Zend_Db_Table_Row_Abstract &&
            $oEltToReceive instanceof Zend_Db_Table_Row_Abstract ){
           $left   = $this->_left  ;
           $right  = $this->_right ;
 
           if ( $this->countChildren($id) >= 0 ){
               $data = $oEltToMove->toArray();
               $newId = $this->insertByParent($data, $newParentId);              
               $children = $this->getChildren($id, 1);
               foreach( $children as $child ){
                   $this->moveTree($child['id'], $newId );
               }
               $this->deleteById($id);
           }
 
      }
      return false;
 
   }
 
}

NB: moveTree est une fonction récursive elle fonctionne de manière atomique mais il peut potentiellement avoir des données orphelines si la procédure est interrompue pour X raisons :'(. Il faut que je regardes comment je pourrais gérer ca de manière transactionnelle, histoire qu'une interruption ne cause aucun soucis.

Un petit view_helper pour les formulaire

Je recherchais a afficher un récapitulatif des erreurs en début de formulaire. Et ce en sus, de l’affichage classique du formulaire.

Lire la suite

Aide de vue pour un listing de caractère

Cette aide de vue permet de créer facilement une liste de lien avec 1 lettre en paramètre. Très utile pour réaliser un filtre sur la 1ere lettre d’un listing de personne.

Lire la suite