|
1 | 1 | <?php |
2 | | -// $Id$ |
3 | | -/******************************************************************** |
4 | | - * Constants |
5 | | - ********************************************************************/ |
6 | 2 |
|
7 | | -/******************************************************************** |
8 | | - * Drupal Hooks |
9 | | - ********************************************************************/ |
| 3 | +/** |
| 4 | + * @file |
| 5 | + * Alternative Pager. |
| 6 | + * |
| 7 | + * API for alternative pager. It is alternative view point on |
| 8 | + * Pager functionality. |
| 9 | + */ |
10 | 10 |
|
| 11 | +// Global items count. |
| 12 | +$_altpager_items_count = 0; |
11 | 13 |
|
12 | 14 | /** |
13 | | - * Implementation of hook_theme() |
| 15 | + * Implements hook_theme(). |
14 | 16 | */ |
15 | 17 | function altpager_theme() { |
16 | 18 | return array( |
17 | | - 'showPager' => array( |
18 | | - 'arguments' => array('sql' => ''), |
| 19 | + 'altpager' => array( |
| 20 | + 'variables' => array( |
| 21 | + 'total' => 0, |
| 22 | + 'items' => '', |
| 23 | + ), |
| 24 | + 'template' => 'altpager', |
19 | 25 | ), |
20 | 26 | 'altpager_link' => array( |
21 | | - 'arguments' => array('text' => NULL, 'count' => NULL), |
| 27 | + 'variables' => array( |
| 28 | + 'count' => 0, |
| 29 | + 'title' => '', |
| 30 | + ), |
22 | 31 | ), |
| 32 | + ); |
| 33 | +} |
23 | 34 |
|
| 35 | +/** |
| 36 | + * Default items count pager. |
| 37 | + * |
| 38 | + * @return array |
| 39 | + * Array list items. |
| 40 | + */ |
| 41 | +function altpager_default_items() { |
| 42 | + global $_altpager_items_count; |
| 43 | + |
| 44 | + return array( |
| 45 | + 10 => 10, |
| 46 | + 20 => 20, |
| 47 | + 50 => 50, |
| 48 | + 100 => 100, |
| 49 | + 200 => 200, |
| 50 | + 400 => 400, |
| 51 | + 600 => 600, |
| 52 | + $_altpager_items_count => $_altpager_items_count, |
24 | 53 | ); |
25 | | -}; |
| 54 | +} |
| 55 | + |
| 56 | +/** |
| 57 | + * Count items viewed in current page. |
| 58 | + * |
| 59 | + * @return int|mixed |
| 60 | + * Count page viewed. |
| 61 | + */ |
| 62 | +function altpager_count_items() { |
| 63 | + global $_altpager_items_count; |
| 64 | + |
| 65 | + $default_items = altpager_default_items(); |
| 66 | + $min_count_items = key($default_items); |
| 67 | + |
| 68 | + $count = isset($_GET['apcount']) ? (int) $_GET['apcount'] : $min_count_items; |
26 | 69 |
|
27 | | -/******************************************************************** |
28 | | - * API Functions |
29 | | - ********************************************************************/ |
| 70 | + if ($count > $_altpager_items_count || $count < 0) { |
| 71 | + $count = $_altpager_items_count; |
| 72 | + } |
30 | 73 |
|
31 | | -function altpager_getCount(){ |
32 | | - if(isset($_REQUEST['apcount']) && $_REQUEST['apcount'] > 0 ){ |
33 | | - return $_REQUEST['apcount']; |
| 74 | + if (!in_array($count, $default_items)) { |
| 75 | + $count = $min_count_items; |
34 | 76 | } |
35 | | - return 10; |
| 77 | + |
| 78 | + return $count; |
36 | 79 | } |
37 | 80 |
|
38 | | -function altpager_Show($sql='',$args=array(),$noreplace = 0){ |
39 | | - if(empty($sql)){ |
40 | | - drupal_set_message(t('showPager: SQL couldnot be empty'),'error'); |
41 | | - return ''; |
| 81 | +/** |
| 82 | + * Process variables for altpager.tpl.php. |
| 83 | + * |
| 84 | + * The $variables array contains the following arguments: |
| 85 | + * - $total: Count all items. |
| 86 | + * - $items: Items for output. |
| 87 | + * |
| 88 | + * @see altpager.tpl.php |
| 89 | + */ |
| 90 | +function template_preprocess_altpager(&$variables) { |
| 91 | + global $_altpager_items_count; |
| 92 | + |
| 93 | + drupal_add_css(drupal_get_path('module', 'altpager') . '/altpager.css'); |
| 94 | + |
| 95 | + $variables['items'] = ''; |
| 96 | + $items = altpager_default_items(); |
| 97 | + $min_count = key($items); |
| 98 | + |
| 99 | + if ($_altpager_items_count > $min_count) { |
| 100 | + foreach ($items as $count => $title) { |
| 101 | + if ($count <= $_altpager_items_count) { |
| 102 | + $variables['items'] .= theme('altpager_link', array( |
| 103 | + 'count' => $count, |
| 104 | + 'title' => $title, |
| 105 | + )); |
| 106 | + } |
| 107 | + } |
42 | 108 | } |
43 | 109 |
|
44 | | - return theme('showPager',$sql,$args,$noreplace); |
| 110 | + $variables['total'] = $_altpager_items_count; |
45 | 111 | } |
46 | 112 |
|
47 | | -/******************************************************************** |
48 | | - * Functions theme |
49 | | - ********************************************************************/ |
50 | | -function theme_altpager_link($text,$count){ |
51 | | - |
52 | | - $query=array(); |
53 | | - $query[]='apcount='.$count; |
54 | | - |
55 | | - $querystring = drupal_query_string_encode($_REQUEST, array_merge(array('q', 'apcount', 'pass'), array_keys($_COOKIE))); |
56 | | - if ($querystring != '') { |
57 | | - $query[] = $querystring; |
| 113 | +/** |
| 114 | + * Link theme. |
| 115 | + * |
| 116 | + * @param array $variables |
| 117 | + * An associative array containing: |
| 118 | + * - count: Count items viewed. |
| 119 | + * - text: Item title. |
| 120 | + * |
| 121 | + * @return string |
| 122 | + * Link. |
| 123 | + */ |
| 124 | +function theme_altpager_link($variables) { |
| 125 | + $query = array(); |
| 126 | + $query['apcount'] = $variables['count']; |
| 127 | + |
| 128 | + $exclude = array_merge(array('q', 'apcount', 'pass'), array_keys($_COOKIE)); |
| 129 | + $querystring = drupal_get_query_parameters($_GET, $exclude); |
| 130 | + |
| 131 | + if (!empty($querystring)) { |
| 132 | + $query = array_merge($querystring, $query); |
58 | 133 | } |
59 | | - |
60 | | - $current_count=10; |
61 | | - if(isset($_REQUEST['apcount'])){ |
62 | | - $current_count = $_REQUEST['apcount']; |
| 134 | + |
| 135 | + $count_items = altpager_count_items(); |
| 136 | + |
| 137 | + if ($count_items == $variables['count']) { |
| 138 | + $output = '<li class="active">' . $variables['title'] . '</li>'; |
63 | 139 | } |
64 | | - |
65 | | - if($current_count == $count){ |
66 | | - $output = '<li class="active">'.$text.'</li>'; |
67 | | - }else{ |
68 | | - $output = '<li>'.l($text, $_GET['q'], array('attributes' => $attributes, 'query' => count($query) ? implode('&', $query) : NULL)).'</li>'; |
| 140 | + else { |
| 141 | + $output = '<li>' . l($variables['title'], $_GET['q'], array('query' => $query)) . '</li>'; |
69 | 142 | } |
| 143 | + |
70 | 144 | return $output; |
71 | 145 | } |
72 | 146 |
|
73 | | -function theme_showPager($sql='',$args=array(),$noreplace = 0){ |
74 | | - drupal_add_css(drupal_get_path('module', 'altpager') .'/altpager.css'); |
75 | | - |
76 | | - if($noreplace == 0){ |
77 | | - $count_query = preg_replace(array('/SELECT.*?FROM /As', '/ORDER BY .*/'), array('SELECT COUNT(*) AS count FROM ', ''), $sql); |
78 | | - }else{ |
79 | | - $count_query = $sql; |
80 | | - } |
81 | | - |
82 | | - $total=db_fetch_array(db_query($count_query,$args)); |
83 | | - |
84 | | - $total=$total['count']; |
85 | | - $output =''; |
86 | | - |
87 | | - if( 10 < $total){ |
88 | | - |
89 | | - $items = array( |
90 | | - 10 => 10, |
91 | | - 20 => 20, |
92 | | - 50 => 50, |
93 | | - 100 => 100, |
94 | | - 200 => 200, |
95 | | - 400 => 400, |
96 | | - 600 => 600, |
97 | | - $total => $total, |
98 | | - ); |
99 | | - |
100 | | - |
101 | | - $output='<div class="altpager"><ul>'; |
102 | | - $output .= '<li class="prefix">'.t('Show').'</li>'; |
103 | | - |
104 | | - foreach ($items as $count => $title){ |
105 | | - if($count <= $total){ |
106 | | - $output .= theme('altpager_link',$count,$title); |
107 | | - } |
108 | | - } |
109 | | - $output .= '<li class="sufix">'.t('of !total records', array ('!total' => $total)).'</li>'; |
110 | | - $output .= '</ul></div>'; |
111 | | - |
| 147 | +/** |
| 148 | + * Query extender for pager queries. |
| 149 | + */ |
| 150 | +class AltPager extends SelectQueryExtender { |
| 151 | + |
| 152 | + public function __construct(SelectQueryInterface $query, DatabaseConnection $connection) { |
| 153 | + parent::__construct($query, $connection); |
| 154 | + |
| 155 | + $this->addTag('altpager'); |
112 | 156 | } |
113 | | - return $output; |
114 | 157 |
|
| 158 | + /** |
| 159 | + * Override the execute method. |
| 160 | + * |
| 161 | + * Before we run the query, we need to add pager-based range() instructions |
| 162 | + * to it. |
| 163 | + */ |
| 164 | + public function execute() { |
| 165 | + global $_altpager_items_count; |
| 166 | + |
| 167 | + $_altpager_items_count = $this |
| 168 | + ->query |
| 169 | + ->countQuery() |
| 170 | + ->execute() |
| 171 | + ->fetchField(); |
| 172 | + |
| 173 | + $count_items = altpager_count_items(); |
| 174 | + |
| 175 | + $this->range(0, $count_items); |
| 176 | + |
| 177 | + return $this->query->execute(); |
| 178 | + } |
115 | 179 | } |
0 commit comments