Skip to content

Commit 07fd7db

Browse files
committed
Merge pull request #1 from sergeypavlenko/7.x-1.x
Ported to Drupal 7.
2 parents daa38ce + 8149c79 commit 07fd7db

File tree

7 files changed

+243
-125
lines changed

7 files changed

+243
-125
lines changed

README.md

Lines changed: 21 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,30 @@
1-
altpager
1+
Alternative Pager
22
========
33

4-
Alternative Pager. API for alternative pager. It is alternative view point on Pager functionality
4+
Alternative Pager. API for alternative pager. It is alternative view point on
5+
Pager functionality
56

67

7-
Example:
8+
Example code or module "altpager_example":
89

9-
```
10+
```php
1011
<?php
1112

12-
//is module available?
13-
if(module_exists('altpager')){
14-
15-
//lets select some data
16-
$sql='SELECT nid FROM node';
17-
18-
//generate pager
19-
$pagerCountShow=altpager_Show($sql);
20-
21-
//get total count output
22-
$count = altpager_getCount();
23-
24-
//let's collect result data
25-
$result = db_query_range($sql,array(),0,$count);
26-
$output_nodes ='';
27-
while ($n = db_fetch_object($result)) {
28-
$n2 = node_load($n->nid);
29-
$output_nodes .= theme('node', $n2, TRUE,FALSE);
30-
}//while
31-
32-
//output data
33-
$output .= $pagerCountShow;
34-
$output .= '<div class="nodes">';
35-
$output .= $output_nodes;
36-
$output .= "</div>";
37-
$output .= $pagerCountShow;
38-
39-
echo $output;
13+
$query = db_select('node', 'n')->extend('AltPager');
14+
$nids = $query
15+
->fields('n', array('nid', 'sticky', 'created'))
16+
->execute()
17+
->fetchAll();
18+
$pager = theme('altpager');
19+
20+
$result = $pager;
21+
22+
foreach ($nids as $row) {
23+
$node = node_load($row->nid);
24+
$result .= render(node_view($node));
4025
}
41-
?>
42-
```
4326

44-
Output is:
45-
![alt tag](http://i61.tinypic.com/wbs7rb.png)
27+
$result .= theme('altpager');
28+
29+
echo $result;
30+
```

altpager.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
div.altpager {
22
text-align: right;
33
padding: 5px 0px;
4-
}
4+
}
55

66
div.altpager ul {
77
list-style: none;

altpager.info

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
; $Id$
21
name = Alternative Pager
3-
description = API for alternative pager. It is alternative view point on Pager functionality
4-
version = VERSION
5-
core = 6.x
2+
description = API for alternative pager. It is alternative view point on Pager functionality.
3+
core = 7.x

altpager.module

Lines changed: 148 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,115 +1,179 @@
11
<?php
2-
// $Id$
3-
/********************************************************************
4-
* Constants
5-
********************************************************************/
62

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+
*/
1010

11+
// Global items count.
12+
$_altpager_items_count = 0;
1113

1214
/**
13-
* Implementation of hook_theme()
15+
* Implements hook_theme().
1416
*/
1517
function altpager_theme() {
1618
return array(
17-
'showPager' => array(
18-
'arguments' => array('sql' => ''),
19+
'altpager' => array(
20+
'variables' => array(
21+
'total' => 0,
22+
'items' => '',
23+
),
24+
'template' => 'altpager',
1925
),
2026
'altpager_link' => array(
21-
'arguments' => array('text' => NULL, 'count' => NULL),
27+
'variables' => array(
28+
'count' => 0,
29+
'title' => '',
30+
),
2231
),
32+
);
33+
}
2334

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,
2453
);
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;
2669

27-
/********************************************************************
28-
* API Functions
29-
********************************************************************/
70+
if ($count > $_altpager_items_count || $count < 0) {
71+
$count = $_altpager_items_count;
72+
}
3073

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;
3476
}
35-
return 10;
77+
78+
return $count;
3679
}
3780

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+
}
42108
}
43109

44-
return theme('showPager',$sql,$args,$noreplace);
110+
$variables['total'] = $_altpager_items_count;
45111
}
46112

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);
58133
}
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>';
63139
}
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>';
69142
}
143+
70144
return $output;
71145
}
72146

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');
112156
}
113-
return $output;
114157

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+
}
115179
}

altpager.tpl.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
/**
4+
* @file
5+
* Default theme implementation to present pager item.
6+
*
7+
* Available variables:
8+
* - $total: Count all items.
9+
* - $items: Items for output.
10+
*
11+
* @see altpager.tpl.php
12+
* Block with pager.
13+
* @see template_preprocess_altpager()
14+
*
15+
* @ingroup themeable
16+
*/
17+
?>
18+
<div class="altpager">
19+
<ul>
20+
<li class="prefix"><?php print t('Show'); ?></li>
21+
<?php print $items; ?>
22+
<li class="sufix"><?php print t('of !total records', array('!total' => $total)); ?></li>
23+
</ul>
24+
</div>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
name = Alternative Pager ( Example )
2+
description = Example work module.
3+
core = 7.x

0 commit comments

Comments
 (0)