<commit_before><?php

/**
 *
 * (c) 2008 <NAME> <<EMAIL>>
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This software consists of voluntary contributions made by many individuals
 * and is licensed under the LGPL. For more information, see
 * <http://www.soleoweb.com>.
 * 
 */

/**
 * Create a datagrid based on sfForm framework from symfony-project
 * 
 * The sfForm is used to define filters and display the filters elements.
 * 
 * @author <NAME> <<EMAIL>> 
 *
 * SVN : $Id$
 */
abstract class swDatagrid extends sfForm
{

  protected 
    $pager,
    $filters = array(),
    $schema_format_name = 'filters';
  
  
  /**
   * @see sfFrom
   * 
   */
  public function __construct($params = array(), $options = array(), $CSRFSecret = null)
  {
    // validator options
    $options['allow_extra_fields'] = isset($options['allow_extra_fields']) ? $options['allow_extra_fields'] : true;
    $options['filter_extra_fields'] = isset($options['filter_extra_fields']) ? $options['filter_extra_fields'] : false;
    $options['disabled_init'] = isset($options['disabled_init']) ? $options['disabled_init'] : false;
    
    parent::__construct(array(), $options, $CSRFSecret);

    // init sort features
    $params['sort_by'] = isset($params['sort_by']) ? $params['sort_by'] : null;
    $params['sort_order'] = isset($params['sort_order']) ? $params['sort_order'] : null;
   
    // init values
    $defaults = $this->prepareDefaultValues($params);
    $this->setDefaults($defaults);
    $this->setStoredValues($defaults);

    if(!$options['disabled_init'])
    {
      $this->init();
    }
    
  }

  /**
   * 
   * add a new filter field into the datagrid
   * 
   * @param string $name name of the field
   * @param mixed $default_value 
   * @param sfFormWidget $widget
   * @param sfValidatorBase $validator
   * @param string $label
   * 
   */
  public function addFilter($name, $default_value, $widget, $validator, $label = null)
  {
    
    $this->filters[$name] = $default_value;
    $this->widgetSchema[$name] = $widget;
    $this->validatorSchema[$name] = $validator;
    
    if($label)
    {
      $this->widgetSchema->setLabel($name, $label);
    }
  }
  
  
  /**
   * 
   * @param $field
   * @return boolean true if the sortable field exists
   */
  public function hasSortableField($field)
  {
    
    return array_key_exists($field, $this->getSortableFields());
  }
  
  /**
   * 
   * @return array of sortable field
   */
  public function getSortableFields()
  {
    $fields = array();
    
    foreach ($this->widgetSchema->getFields() as $name => $widget)
    {
      if(in_array($name, array('sort_by', 'sort_order')))
      {
        
        continue;
      }
      
      $fields[$name] = $widget->getLabel() ? $widget->getLabel() : $name; 
    }
    
    return $fields;
  }
  
  /**
   * 
   * Get the url for the current sort field
   * 
   * @param $url
   * @param $sort_by
   * @param $sort_order
   * @return unknown_type
   */
  public function getSortUrl($url, $sort_by, $sort_order = null)
  {

    if(!$this->hasSortableField($sort_by))
    {
      
      return $text;
    }
    
    if(is_null($sort_order))
    {
      $sort_order = $this->getValue('sort_by') == $sort_by ? ($this->getValue('sort_order') == 'ASC' ? 'ASC' : 'DESC') : 'DESC';
    }
    
    $url .= '?' . $this->getQueryString(array(
      'sort_by'    => $sort_by,
      'sort_order' => $sort_order<commit_msg>add getSortBy and getOrderBy<commit_after><?php

/**
 *
 * (c) 2008 <NAME> <<EMAIL>>
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This software consists of voluntary contributions made by many individuals
 * and is licensed under the LGPL. For more information, see
 * <http://www.soleoweb.com>.
 * 
 */

/**
 * Create a datagrid based on sfForm framework from symfony-project
 * 
 * The sfForm is used to define filters and display the filters elements.
 * 
 * @author <NAME> <<EMAIL>> 
 *
 * SVN : $Id$
 */
abstract class swDatagrid extends sfForm
{

  protected 
    $pager,
    $filters = array(),
    $schema_format_name = 'filters';
  
  
  /**
   * @see sfFrom
   * 
   */
  public function __construct($params = array(), $options = array(), $CSRFSecret = null)
  {
    // validator options
    $options['allow_extra_fields'] = isset($options['allow_extra_fields']) ? $options['allow_extra_fields'] : true;
    $options['filter_extra_fields'] = isset($options['filter_extra_fields']) ? $options['filter_extra_fields'] : false;
    $options['disabled_init'] = isset($options['disabled_init']) ? $options['disabled_init'] : false;
    
    parent::__construct(array(), $options, $CSRFSecret);

    // init sort features
    $params['sort_by'] = isset($params['sort_by']) ? $params['sort_by'] : null;
    $params['sort_order'] = isset($params['sort_order']) ? $params['sort_order'] : null;
   
    // init values
    $defaults = $this->prepareDefaultValues($params);
    $this->setDefaults($defaults);
    $this->setStoredValues($defaults);

    if(!$options['disabled_init'])
    {
      $this->init();
    }
    
  }

  /**
   * 
   * add a new filter field into the datagrid
   * 
   * @param string $name name of the field
   * @param mixed $default_value 
   * @param sfFormWidget $widget
   * @param sfValidatorBase $validator
   * @param string $label
   * 
   */
  public function addFilter($name, $default_value, $widget, $validator, $label = null)
  {
    
    $this->filters[$name] = $default_value;
    $this->widgetSchema[$name] = $widget;
    $this->validatorSchema[$name] = $validator;
    
    if($label)
    {
      $this->widgetSchema->setLabel($name, $label);
    }
  }
  
  
  /**
   * 
   * @param $field
   * @return boolean true if the sortable field exists
   */
  public function hasSortableField($field)
  {
    
    return array_key_exists($field, $this->getSortableFields());
  }
  
  /**
   * 
   * @return array of sortable field
   */
  public function getSortableFields()
  {
    $fields = array();
    
    foreach ($this->widgetSchema->getFields() as $name => $widget)
    {
      if(in_array($name, array('sort_by', 'sort_order')))
      {
        
        continue;
      }
      
      $fields[$name] = $widget->getLabel() ? $widget->getLabel() : $name; 
    }
    
    return $fields;
  }
  
  public function getSortBy()
  {
    return $this->getValue('sort_by');
  }
  
  public function getSortOrder()
  {
    return $this->getValue('sort_order');
  }
  
  /**
   * 
   * Get the url for the current sort field
   * 
   * @param $url
   * @param $sort_by
   * @param $sort_order
   * @return unknown_type
   */
  public function getSortUrl($url, $sort_by, $sort_order = null)
  {

    if(!$this->hasSortableField($sort_by))
    {
      
      return $text;
    }
    
    if(is_null($sort_order))
    {
      $sort_order = $this->getValue('sort_by') == $sort_by ? ($this->getValue('sort_order') == 'ASC' ? 'ASC' : 'DESC') : 'DESC';
    }
    
    $url .= '?' . $this->getQueryString(array(
      'sort_by'    => $sort_by,
      'sort_order' => $sort_order
{'fraction_non_alphanumeric': 0.13841642228739004, 'fraction_numerical': 0.00093841642228739, 'mean_word_length': 3.106936416184971, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 61, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2955803', 'n_tokens_mistral': 2772, 'n_tokens_neox': 2643, 'n_words': 712}