Code snippets for symfony 1.x

Navigation

Refine Tags

Snippets tagged "sort"

Collate in propel sort

$c->addAscendingOrderByColumn( 'NAME COLLATE utf8_bin' );

quite obvious

by karol _ on 2011-06-13, tagged criteria  orderby  propel  sort 

sort helper for displaying lists

Developers which are used to the admin generator are used of the functionality how this admin genereator sorts a list.

In the th tag of the list, there is used something like this:

          <?php if ($sf_user->getAttribute('sort', null, 'sf_admin/news/sort') == 'is_published'): ?>
      <?php echo link_to(__('Is published'), 'news/list?sort=is_published&type='.($sf_user->getAttribute('type', 'asc', 'sf_admin/news/sort') == 'asc' ? 'desc' : 'asc')) ?>
      (<?php echo __($sf_user->getAttribute('type', 'asc', 'sf_admin/news/sort')) ?>)
      <?php else: ?>
      <?php echo link_to(__('Is published'), 'news/list?sort=is_published&type=asc') ?>
      <?php endif; ?>

This is pretty hard to read and i have put this stuff of code in a helper method:

/**
 * This helper function does exactly the same code output than
 * ---------
 *           <?php if ($sf_user->getAttribute('sort', null, 'sf_admin/news/sort') == 'is_published'): ?>
 *     <?php echo link_to(__('Is published'), 'news/list?sort=is_published&type='.($sf_user->getAttribute('type',   'asc', 'sf_admin/news/sort') == 'asc' ? 'desc' : 'asc')) ?>
 *     (<?php echo __($sf_user->getAttribute('type', 'asc', 'sf_admin/news/sort')) ?>)
 *     <?php else: ?>
 *     <?php echo link_to(__('Is published'), 'news/list?sort=is_published&type=asc') ?>
 *     <?php endif; ?>
 * ---------
  * 
 * @author Frank Stelzer
 * 
 * @param string $name
 * @param string $base_internal_uri
 * @param string $sort_name
 * @param string $ns the name space
 * @return string html code
 */
function sortable_link_to($name, $base_internal_uri = '', $sort_name = '', $ns = ''){
    $retval = '';
 
    $sort_name = $sort_name=='' ? $name : $sort_name;
 
    $user = sfContext::getInstance()->getUser();
    /*@var $user sfBasicSecurityUser */
 
    if ($user->getAttribute('sort', null, $ns) == $sort_name){
        $retval .= link_to($name, $base_internal_uri.(strpos($base_internal_uri,'?')? '&' : '?'). 'sort='.$sort_name.'&type='.($user->getAttribute('type', 'asc', $ns) == 'asc' ? 'desc' : 'asc'));
        $retval .=show_sort_type($user->getAttribute('type', 'asc', $ns));
    }else {
        $retval = link_to($name, $base_internal_uri.(strpos($base_internal_uri,'?')? '&' : '?'). 'sort='.$sort_name.'&type=asc');
    }
 
    return $retval;
}
 
 
/**
 * helper function which is used in the sortable_link_to function
 *
 * @author Frank Stelzer
 *
 * @param string $type
 * @return string html code
 */
function show_sort_type($type){
    $t=$type=='asc'? '&darr;' : '&uarr;';
    // return " ($t) ";
    return " $t ";
}

You can put in the show_sort_type whatever you want. You can display some arrow images or other stuff....

So, when you have a list/table, which you want to sort, you only have to put this code above in a helper and to write this in a template:

echo sortable_link_to(__('Content'),'module/action'),'content', 'module/action/sort');

This is much better to read than the first code, or? :)

Look in generated action code of the admin generator to see how one has to use an incoming sort request.

These methods are mainly used:

    protected function addSortCriteria($c)
    {
        if ($sort_column = $this->getUser()->getAttribute('sort', null, 'namespace'))
        {
            $sort_column = CustomPeer::translateFieldName($sort_column, BasePeer::TYPE_FIELDNAME, BasePeer::TYPE_COLNAME);
            if ($this->getUser()->getAttribute('type', null, 'namespace') == 'asc')
            {
                $c->addAscendingOrderByColumn($sort_column);
            }
            else
            {
                $c->addDescendingOrderByColumn($sort_column);
            }
        }
    }
 
    protected function processSort()
    {
        if ($this->getRequestParameter('sort'))
        {
            $this->getUser()->setAttribute('sort', $this->getRequestParameter('sort'), 'namespace');
            $this->getUser()->setAttribute('type', $this->getRequestParameter('type', 'asc'), 'namespace');
        }
 
        if (!$this->getUser()->getAttribute('sort', null, 'namespace'))
        {
        }
    }

In Your custom action you than need:

        $this->processSort();
 
        $c=new Criteria();
        $this->addSortCriteria($c);
// your other criterias here
by Frank Stelzer on 2007-04-18, tagged helper  list  sort 
(2 comments)

Multi sort in admin generator

Here we go !

The sort in admin generator is for a single field only, but in some complex list, it can be usefull to sort by multiple criterias. This is the main goal of this snippet. Those functions therefore override the ones of your auto-generated class. (1) And to display what are the ongoing sort criterias, you have to modify your '_list_th_tabular.php' file.

1 - In your 'action.class.php'

//
    /**
     * Add a sort criteria
     */
    protected function processSort ()
    {
        $sort = $this->getRequestParameter('sort');
        $type = $this->getRequestParameter('type');            
 
        // Register sort                 
        if ($sort) {
            $this->getUser()->setAttribute($sort, $type, 'sf_admin/produits/sort');
        }
    }    
 
    /**
     * Add the sort criterias to the query
     */
    protected function addSortCriteria(&$c)
    {
        $multisort = $this->getUser()->getAttributeHolder()->getAll('sf_admin/produits/sort');
 
        if ($multisort) {
            foreach($multisort as $sort_column => $sort_type) {
                $sort_column = Propel::getDB($c->getDbName())->quoteIdentifier($sort_column);
                if ($sort_type == 'asc') {
                    $c->addAscendingOrderByColumn($sort_column);
                }
                elseif ($sort_type == 'desc') {
                    $c->addDescendingOrderByColumn($sort_column);
                }
            }
        } else {
            // Default sort 
            $sort_column = Propel::getDB($c->getDbName())->quoteIdentifier('libelle');
            $c->addAscendingOrderByColumn($sort_column);            
        }
    }
 
    /**
     * Specific function for multi-sort
     */
    protected function processFilters ()
    {
        if ($this->getRequest()->hasParameter('filter'))
        {
            $filters = $this->getRequestParameter('filters');            
 
            // Multi-sort initialisation
            if (!is_array($filters)) {
                $this->getUser()->getAttributeHolder()->removeNamespace('sf_admin/produits/sort');
            }
 
            $this->getUser()->getAttributeHolder()->removeNamespace('sf_admin/produits/filters');
            $this->getUser()->getAttributeHolder()->add($filters, 'sf_admin/produits/filters');
        }
    }

2 - In '_list_th_tabular.php'

(for each sortable field)

<?php 
 
$multisort = $sf_user->getAttributeHolder()->getAll('sf_admin/produits/sort');
 
?>
 
    <th id="sf_admin_list_th_libelle">
        <?php 
        if (isset($multisort['libelle'])) {
            echo link_to('Libellé', 'Produits/list?sort=libelle&type='. ($multisort['libelle'] == 'asc' ? 'desc' : 'asc'));
            echo ' ('. $multisort['libelle'] . ')';
        } else {
            echo link_to('Libellé', 'Produits/list?sort=libelle&type=asc'); 
        }
        ?>
    </th>

'Produits' is the module name, 'libelle' is the field to sort.


Notes:

The 'reset button' of filters also initialize the multi-sort. The sort is made from the first field clicked to the last. That means, if you want a different primary sort you will have to use the reset filter button before.

PS: Obviously, this modification can be easly integrated in your backoffice theme, note that the default sort criteria set in the 'generator.yml' is used in the addSortCriteria function (from line // default sort)

COil :)

by Loïc Vernet on 2006-10-17, tagged admin  generator  multi  sort 
(6 comments)