Development

Changeset 19523

You must first sign up to be able to contribute.

Changeset 19523

Show
Ignore:
Timestamp:
06/24/09 22:18:06 (9 months ago)
Author:
rande
Message:
  • datagrid refactoring
Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • plugins/swToolboxPlugin/sf1.2/trunk/lib/datagrid/swDoctrineDatagrid.class.php

    r19435 r19523  
    3333 * SVN : $Id$ 
    3434 */ 
    35 abstract class swDoctrineDatagrid extends sfForm 
     35abstract class swDoctrineDatagrid extends swModelDatagrid 
    3636{ 
    37   protected $pager; 
    38   protected $filters = array(); 
    39   protected $schema_format_name = 'filters'; 
    4037   
    41    
    42   /** 
    43    * @see sfFrom 
     38    /** 
     39   * build the query that will be handle by the pager 
    4440   *  
     41   * @param $query 
     42   * @return query 
    4543   */ 
    46   public function __construct($params = array(), $options = array(), $CSRFSecret = null) 
    47   { 
    48     // validator options 
    49     $options['allow_extra_fields'] = isset($options['allow_extra_fields']) ? $options['allow_extra_fields'] : true; 
    50     $options['filter_extra_fields'] = isset($options['filter_extra_fields']) ? $options['filter_extra_fields'] : false; 
    51     $options['disabled_init'] = isset($options['disabled_init']) ? $options['disabled_init'] : false; 
    52      
    53     parent::__construct(array(), $options, $CSRFSecret); 
    54  
    55     // init sort features 
    56     $params['sort_by'] = isset($params['sort_by']) ? $params['sort_by'] : null; 
    57     $params['sort_order'] = isset($params['sort_order']) ? $params['sort_order'] : null; 
    58     
    59     // init values 
    60     $defaults = $this->prepareDefaultValues($params); 
    61     $this->setDefaults($defaults); 
    62     $this->setStoredValues($defaults); 
    63  
    64     if(!$options['disabled_init']) 
    65     { 
    66       $this->init(); 
    67     } 
    68      
    69   } 
    70  
    71   /** 
    72    *  
    73    * add a new filter field into the datagrid 
    74    *  
    75    * @param string $name name of the field 
    76    * @param mixed $default_value  
    77    * @param sfFormWidget $widget 
    78    * @param sfValidatorBase $validator 
    79    * @param string $label 
    80    *  
    81    */ 
    82   public function addFilter($name, $default_value, $widget, $validator, $label = null) 
    83   { 
    84      
    85     $this->filters[$name] = $default_value; 
    86     $this->widgetSchema[$name] = $widget; 
    87     $this->validatorSchema[$name] = $validator; 
    88      
    89     if($label) 
    90     { 
    91       $this->widgetSchema->setLabel($name, $label); 
    92     } 
    93   } 
    94    
    95    
    96   /** 
    97    *  
    98    * @param $field 
    99    * @return boolean true if the sortable field exists 
    100    */ 
    101   public function hasSortableField($field) 
    102   { 
    103      
    104     return array_key_exists($field, $this->getSortableFields()); 
    105   } 
    106    
    107   /** 
    108    *  
    109    * @return array of sortable field 
    110    */ 
    111   public function getSortableFields() 
    112   { 
    113     $fields = array(); 
    114      
    115     foreach ($this->widgetSchema->getFields() as $name => $widget) 
    116     { 
    117       if(in_array($name, array('sort_by', 'sort_order'))) 
    118       { 
    119          
    120         continue; 
    121       } 
    122        
    123       $fields[$name] = $widget->getLabel() ? $widget->getLabel() : $name;  
    124     } 
    125      
    126     return $fields; 
    127   } 
    128    
    129   /** 
    130    *  
    131    * Get the url for the current sort field 
    132    *  
    133    * @param $url 
    134    * @param $sort_by 
    135    * @param $sort_order 
    136    * @return unknown_type 
    137    */ 
    138   public function getSortUrl($url, $sort_by, $sort_order = null) 
    139   { 
    140  
    141     if(!$this->hasSortableField($sort_by)) 
    142     { 
    143        
    144       return $text; 
    145     } 
    146      
    147     if(is_null($sort_order)) 
    148     { 
    149       $sort_order = $this->getValue('sort_by') == $sort_by ? ($this->getValue('sort_order') == 'ASC' ? 'ASC' : 'DESC') : 'DESC'; 
    150     } 
    151      
    152     $url .= '?' . $this->getQueryString(array( 
    153       'sort_by'    => $sort_by, 
    154       'sort_order' => $sort_order 
    155     )); 
    156      
    157     return url_for($url); 
    158   } 
    159    
    160   /** 
    161    *  
    162    * get the link for the current sort field 
    163    *  
    164    * @param $text 
    165    * @param $url 
    166    * @param $sort_by 
    167    * @param $sort_order 
    168    * @param $options 
    169    * @return unknown_type 
    170    */ 
    171   public function getSortLink($text, $url, $sort_by, $sort_order = null, $options = array()) 
    172   { 
    173     if(!$this->hasSortableField($sort_by)) 
    174     { 
    175        
    176       return $text; 
    177     } 
    178      
    179     if(is_null($sort_order)) 
    180     { 
    181       $sort_order = $this->getValue('sort_by') == $sort_by ? ($this->getValue('sort_order') == 'ASC' ? 'DESC' : 'ASC') : 'DESC'; 
    182     } 
    183      
    184     $url .= '?' . $this->getQueryString(array( 
    185       'sort_by'    => $sort_by, 
    186       'sort_order' => $sort_order  
    187     )); 
    188       
    189     return link_to($text, $url , $options); 
    190   } 
    191    
    192   /** 
    193    *  
    194    * @see vendor/symfony/lib/form/sfForm#setup() 
    195    */ 
    196   public function setup() 
    197   { 
    198     // format the datagrid 
    199     $this->widgetSchema->addFormFormatter('swSchemaFormatterDatagrid', new swSchemaFormatterDatagrid($this->widgetSchema)); 
    200     $this->widgetSchema->setFormFormatterName('swSchemaFormatterDatagrid'); 
    201     $this->widgetSchema->setNameFormat($this->schema_format_name.'[%s]'); 
    202      
    203     $this->widgetSchema->getFormFormatter()->setTranslationCatalogue('datagrid'); 
    204      
    205     $this->validatorSchema->setOption('allow_extra_fields', $this->getOption('allow_extra_fields')); 
    206     $this->validatorSchema->setOption('filter_extra_fields', $this->getOption('filter_extra_fields')); 
    207      
    208     $this->addFilter( 
    209       'sort_by',  
    210       null,  
    211       new sfWidgetFormSelect(array( 
    212         'choices' => $this->getSortableFields() 
    213       )),  
    214       new sfValidatorChoice(array( 
    215         'choices' => array_keys($this->getSortableFields()), 
    216         'required' => false 
    217       )) 
    218     ); 
    219      
    220     $this->addFilter( 
    221       'sort_order',  
    222       null, 
    223       new sfWidgetFormSelect(array( 
    224         'choices' => array('ASC' => 'ASC', 'DESC' => 'DESC') 
    225       )),  
    226       new sfValidatorChoice(array( 
    227         'choices' => array('ASC', 'DESC'), 
    228         'required' => false 
    229       )) 
    230     ); 
    231      
    232     $this->setupDatagrid(); 
    233   } 
    234  
    235   /** 
    236    *  
    237    * init the datagrid with the values 
    238    *  
    239    * @return unknown_type 
    240    */ 
    241   public function init() 
    242   { 
    243     $this->bind($this->getDefaults()); 
    244      
    245     $this->preparePager(); 
    246   } 
    247    
    248   /** 
    249    * define the values to use in the datagrid 
    250    *  
    251    * @param $params 
    252    * @return unknown_type 
    253    */ 
    254   public function prepareDefaultValues(array $params) 
    255   { 
    256  
    257     $session_values = array(); 
    258      
    259     if($this->getOption('store', false)) 
    260     { 
    261       $session_values = $this->getStoredValues(); 
    262     } 
    263  
    264     if(array_key_exists('reset', $params)) 
    265     { 
    266       $reset = true; 
    267       $session_values = $params = array(); 
    268     } 
    269  
    270     $base = count($params) == 2 ? $session_values : $params; 
    271      
    272     $filters = array(); 
    273      
    274     foreach($this->filters as $name => $value) 
    275     { 
    276       $filters[$name] = array_key_exists($name, $base) ? $base[$name] : $value; 
    277     } 
    278  
    279     return $filters; 
    280   } 
    281  
    282   public function getStoreNamespace() 
    283   { 
    284     return $this->getOption('store_namespace', get_class($this)); 
    285   } 
    286    
    287   public function getStoredValues() 
    288   { 
    289     if($this->getOption('store', false) == false) 
    290     { 
    291        
    292       return array(); 
    293     } 
    294      
    295     return sfContext::getInstance()->getUser()->getAttribute('filters', array(), $this->getStoreNamespace()); 
    296   } 
    297  
    298   public function setStoredValues($values) 
    299   { 
    300      
    301     if($this->getOption('store', false) == false) 
    302     { 
    303        
    304       return ; 
    305     } 
    306      
    307     sfContext::getInstance()->getUser()->setAttribute('filters', $values, $this->getStoreNamespace()); 
    308   } 
    309  
    310   public function configure() 
    311   { 
    312    
    313     $this->configureDatagrid(); 
    314      
    315   } 
    316  
    317   public function useOnly(array $fields = array(), $use_order = false) 
    318   { 
    319      
    320     foreach($this as $field => $widget) 
    321     { 
    322       if(!in_array($field, $fields) && !in_array($field, array('sort_by', 'sort_order'))) 
    323       { 
    324  
    325         $this->offsetUnset($field); 
    326  
    327         continue; 
    328       } 
    329     } 
    330      
    331     if($use_order) 
    332     { 
    333       foreach($fields as $pos => $field) 
    334       { 
    335         $this->widgetSchema->moveField($field, sfWidgetFormSchema::LAST); 
    336       } 
    337     } 
    338   } 
    339    
    340   abstract function getModelName(); 
    341  
    342   function getQueryParameters($merge = array()) 
    343   { 
    344      
    345     $value = array_merge($this->getValues(), $merge); 
    346    
    347     if(strlen($this->schema_format_name) > 0) 
    348     { 
    349        
    350       $value = array($this->schema_format_name => $value); 
    351     } 
    352      
    353     return $value; 
    354   } 
    355      
    356   function getQueryString($merge = array()) 
    357   { 
    358      
    359     return http_build_query($this->getQueryParameters($merge)); 
    360   } 
    361    
    362   function setupDatagrid() {} 
    363  
    364   function configureDatagrid() {} 
    365  
    36644  function buildQuery(Doctrine_Query $query) { 
    36745 
     
    36947  } 
    37048 
     49  /** 
     50   * Query to return when the datagrid is not valid 
     51   *  
     52   * @return Doctrine_Query 
     53   */ 
     54  public function getInvalidQuery() 
     55  { 
     56    $query = $this->getBaseQuery(); 
     57    $query->addWhere('id IS NULL'); // set an impossible query 
     58     
     59    return $query; 
     60  } 
     61   
     62   
    37163  public function getBaseQuery() 
    37264  { 
     
    37870  { 
    37971    $page  = $this->getOption('page'); 
    380     $query = $this->buildQuery($this->getBaseQuery()); 
     72    $query = $this->isValid() ? $this->buildQuery($this->getBaseQuery()) : $this->getInvalidQuery(); 
    38173    $per_page = $this->getOption('per_page', 25); 
    38274     
     
    38880  } 
    38981 
    390   /** 
    391    * return the yaml version of the current values 
    392    *  
    393    * @return unknown_type 
    394    */ 
    395   public function getYaml($inline = 2, $indent = 0) 
    396   { 
    397     $yaml = new sfYamlDumper(); 
    398  
    399     return $yaml->dump($this->getValues(), $inline, $indent); 
    400   } 
    401    
    402   public function getPager() 
    403   { 
    404      
    405     return $this->pager; 
    406   } 
    407    
    408   // PAGER PROXY METHODS 
    409   public function getResults() 
    410   { 
    411      
    412     return $this->pager->getResults(); 
    413   } 
    414  
    415   public function haveToPaginate() 
    416   { 
    417      
    418     return $this->pager->haveToPaginate(); 
    419   } 
    420    
    421   public function getPage() 
    422   { 
    423     return $this->pager->getPage(); 
    424   } 
    425  
    426   public function getLinks() 
    427   { 
    428      
    429     return $this->pager->getLinks(); 
    430   } 
    431    
    432   public function getLastPage() 
    433   { 
    434      
    435     return $this->pager->getLastPage(); 
    436   } 
    437    
    438   public function getFirstPage() 
    439   { 
    440      
    441     return $this->pager->getFirstPage(); 
    442   } 
    443    
    444   public function getCurrentMaxLink() 
    445   { 
    446      
    447     return $this->pager->getCurrentMaxLink(); 
    448   } 
    449    
    450   public function getNextPage() 
    451   { 
    452      
    453     return $this->pager->getNextPage(); 
    454   } 
    455    
    456   public function getPreviousPage() 
    457   { 
    458      
    459     return $this->pager->getPreviousPage(); 
    460   } 
    461    
    462   public function count() 
    463   { 
    464      
    465     return $this->pager->getNbResults(); 
    466   } 
    467  
    468   public function getNbResults() 
    469   { 
    470      
    471     return $this->pager->getNbResults(); 
    472   } 
    47382} 

The Sensio Labs Network

Since 1998, Sensio Labs has been promoting the Open-Source software movement by providing quality web application development, training, consulting.
Sensio Labs also supports several large Open-Source projects.