![]() |
|
Snippets |
|
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'? '↓' : '↑'; // 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