Fixing content type ordering when content_type is not defined.
[akelos.git] / lib / AkActionView / helpers / pagination_helper.php
blob48605973034231143339c0f18980baec43c93e7c
1 <?php
2 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4 // +----------------------------------------------------------------------+
5 // | Akelos Framework - http://www.akelos.org |
6 // +----------------------------------------------------------------------+
7 // | Copyright (c) 2002-2006, Akelos Media, S.L. & Bermi Ferrer Martinez |
8 // | Released under the GNU Lesser General Public License, see LICENSE.txt|
9 // +----------------------------------------------------------------------+
11 /**
12 * @package ActionView
13 * @subpackage Helpers
14 * @author Bermi Ferrer <bermi a.t akelos c.om>
15 * @copyright Copyright (c) 2002-2006, Akelos Media, S.L. http://www.akelos.org
16 * @license GNU Lesser General Public License <http://www.gnu.org/copyleft/lesser.html>
20 require_once(AK_LIB_DIR.DS.'AkActionView'.DS.'AkActionViewHelper.php');
21 require_once(AK_LIB_DIR.DS.'AkActionController'.DS.'AkPaginator.php');
23 class PaginationHelper extends AkActionViewHelper
25 function sortable_link($column, $url_options = array(), $link_options = array())
27 $default_url_options = array(
28 'sort'=>$column,
29 'direction' => empty($this->_controller->params['sort']) ? 'desc' : ($this->_controller->params['sort'] == $column ? (empty($this->_controller->params['direction']) ? 'desc' : ($this->_controller->params['direction'] == 'desc' ? 'asc' : 'desc')) : 'desc'),);
31 $page_var_on_url = empty($url_options['page_var_on_url']) ? 'page' : $url_options['page_var_on_url'];
32 unset($url_options['page_var_on_url']);
34 $url_options = array_merge($default_url_options, $url_options);
36 $link_options['href'] = html_entity_decode($this->_controller->url_helper->modify_current_url($url_options,array($page_var_on_url)));
38 if(empty($link_options['title'])){
39 $link_options['title'] = $this->_controller->t("Sort by $column ({$url_options['direction']})");
41 if(!empty($link_options['link_text'])){
42 $link_text = $link_options['link_text'];
43 unset($link_options['link_text']);
44 }else{
45 $link_text = $this->_controller->t(AkInflector::humanize($column));
47 return TagHelper::content_tag('a',$link_text,$link_options);
52 function getFindOptions(&$object)
54 $paginator_name = AkInflector::underscore($object->getModelName()).'_pages';
56 $limit_and_offset = isset($this->$paginator_name) ? array('limit' => $this->$paginator_name->getItemsPerPage(),
57 'offset' => $this->$paginator_name->getOffset()) : array();
59 $find_options = array_merge($limit_and_offset,(!empty($this->_controller->params['sort']) &&
60 $object->hasColumn($this->_controller->params['sort']) ? array('order'=>$this->_getOrderColumnFromCurrentModel($object, $paginator_name).$this->getSortDirection()) : array()));
62 empty($find_options['sort']) ? ($this->_getOrderFromCustomDictionary($paginator_name, $find_options) ? null : $this->_getOrderFromAssociations($object, $paginator_name, $find_options)) : null;
64 if(!empty($this->$paginator_name->_ak_options['include'])){
65 $find_options['include'] = Ak::toArray($this->$paginator_name->_ak_options['include']);
68 return $find_options;
71 function _getOrderColumnFromCurrentModel(&$object, $paginator_name)
73 return empty($this->$paginator_name->_ak_options['include']) ? $this->_controller->params['sort'] : 'parent_'.$object->getModelName().'.'.$this->_controller->params['sort'];
76 function _getOrderFromCustomDictionary($paginator_name, &$find_options)
78 if(!empty($this->_controller->params['sort']) && !empty($this->$paginator_name->_ak_options['column_dictionary'][$this->_controller->params['sort']])){
79 $find_options['order'] = $this->$paginator_name->_ak_options['column_dictionary'][$this->_controller->params['sort']].$this->getSortDirection();
80 return true;
82 return false;
85 function _getOrderFromAssociations(&$object, $paginator_name, &$find_options)
87 if(!empty($this->_controller->params['sort']) && !empty($this->$paginator_name->_ak_options['include']) && $object->hasAssociations()){
88 $included_models = Ak::toArray($this->$paginator_name->_ak_options['include']);
89 foreach ($object->getAvailableAssociates() as $association_type=>$associated_models){
90 foreach ($associated_models as $associated_model){
91 if(in_array($this->_controller->params['sort'], array_keys($object->$associated_model->getAvailableAttributes()))){
92 $find_options['order'] = $association_type.'_'.$associated_model.'.'.$this->_controller->params['sort'].$this->getSortDirection();
93 return true;
98 return false;
101 function getSortDirection()
103 return (empty($this->_controller->params['direction']) || $this->_controller->params['direction'] == 'asc' ? ' ASC' : ' DESC');
108 * Returns a paginator object
110 * Options:
111 * 'items_per_page' => 15, // Number of items per page
112 * 'page_var_on_url' => 'page', // This var will be passed thru the url for pointing to current page
113 * 'count_method' => 'count' // This method will be called on selected model to get the total number of items.
114 * 'count_conditions' => null // A string that will be passed as the first parameter (conditions) to the count method
115 * 'count_joins' => null // A string that will be passed as the seccond parameter (join options) to the count method.
116 * 'column_dictionary' => array() // In case you need to map the sort key from the url to the database column you must define an array pair
117 * 'include' => array() // In case current sort column is not found on current model or in the column_dictionary this helper will look for the first associated model witrh a column named like given sort parameter.
119 function getPaginator(&$object, $options = array())
121 $default_options = array(
122 'items_per_page' => 15,
123 'page_var_on_url' => 'page',
124 'count_method' => 'count',
125 'count_conditions' => null,
126 'count_joins' => null
128 $options = array_merge($default_options, $options);
129 $paginator_name = AkInflector::underscore($object->getModelName()).'_pages';
130 $this->$paginator_name = new AkPaginator($this->_controller,
131 $object->{$options['count_method']}($options['count_conditions'], $options['count_joins']),
132 $options['items_per_page'], @$this->_controller->params[$options['page_var_on_url']]);
133 $this->$paginator_name->_ak_options =& $options;
134 return $this->$paginator_name;