This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathext.ed_pagination.php
360 lines (293 loc) · 12.7 KB
/
ext.ed_pagination.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
<?php
/**
* Erskine Design Pagination (PHP5 only)
* REQUIRES ExpressionEngine 2+
*
* @package Ed_pagination_ext
* @version 0.1
* @author Wil Linssen (Erskine Design)
* @copyright Copyright (c) 2011 Erskine Design
* @license http://creativecommons.org/licenses/by-sa/3.0/ Attribution-Share Alike 3.0 Unported
*
*/
Class Ed_pagination_ext
{
public $settings = array();
public $name = 'ED pagination links';
public $version = '0.1';
public $description = 'Friendly pagination links';
public $settings_exist = 'n';
public $docs_url = 'https://github.com/erskinedesign/ed.pagination.ee_addon';
/**
* Constructicons
*
* @return void
*/
public function __construct( $settings = '' )
{
$this->EE =& get_instance();
}
/**
* Activate Extension
*
* This function enters the extension into the exp_extensions table
*
* @see http://codeigniter.com/user_guide/database/index.html for
* more information on the db class.
*
* @return void
*/
function activate_extension()
{
$data = array(
'class' => __CLASS__,
'method' => '_paginate',
'hook' => 'channel_module_create_pagination',
'settings' => serialize($this->settings),
'priority' => 1,
'version' => $this->version,
'enabled' => 'y'
);
$this->EE->db->insert('extensions', $data);
}
/**
* Disable Extension
*
* This method removes information from the exp_extensions table
*
* @return void
*/
function disable_extension()
{
$this->EE->db->where('class', __CLASS__);
$this->EE->db->delete('extensions');
}
/**
* Paginate
*
* We're basically replicating EE's core functionality and adding our own stuff at the end
*
* @return void
*/
function _paginate(&$data)
{
$data->EE->extensions->end_script = TRUE;
$query = $data->EE->db->query($data->pager_sql);
$count = ( !empty($query->num_rows) ) ? $query->num_rows : FALSE;
// Only proceed if the option is set
if ($data->paginate == TRUE)
{
$tagdata = $data->paginate_data;
/*
What follows is pretty much a rip from the EE core, I'm subsidising it at the end
but I should really just make better use of the hook?
*/
/* --------------------------------------
/* For subdomain's or domains using $template_group and $template
/* in path.php, the pagination for the main index page requires
/* that the template group and template are specified.
/* --------------------------------------*/
if (($this->EE->uri->uri_string == '' OR $this->EE->uri->uri_string == '/') && $this->EE->config->item('template_group') != '' && $this->EE->config->item('template') != '')
{
$data->basepath = $this->EE->functions->create_url($this->EE->config->slash_item('template_group').'/'.$this->EE->config->item('template'));
}
if ($data->basepath == '')
{
$data->basepath = $this->EE->functions->create_url($this->EE->uri->uri_string);
if (preg_match("#^P(\d+)|/P(\d+)#", $data->query_string, $match))
{
$data->p_page = (isset($match[2])) ? $match[2] : $match[1];
$data->basepath = $this->EE->functions->remove_double_slashes(str_replace($match[0], '', $data->basepath));
}
}
// Standard pagination - base values
if ($data->field_pagination == FALSE)
{
if ($data->display_by == '')
{
if ($count == 0)
{
$data->sql = '';
return;
}
$data->total_rows = $count;
}
if ($data->dynamic_sql == FALSE)
{
$cat_limit = FALSE;
if ((in_array($data->reserved_cat_segment, explode("/", $this->EE->uri->uri_string))
AND $this->EE->TMPL->fetch_param('dynamic') != 'no'
AND $this->EE->TMPL->fetch_param('channel'))
OR (preg_match("#(^|\/)C(\d+)#", $this->EE->uri->uri_string, $match) AND $this->EE->TMPL->fetch_param('dynamic') != 'no'))
{
$cat_limit = TRUE;
}
if ($cat_limit AND is_numeric($this->EE->TMPL->fetch_param('cat_limit')))
{
$data->p_limit = $this->EE->TMPL->fetch_param('cat_limit');
}
else
{
$data->p_limit = ( ! is_numeric($this->EE->TMPL->fetch_param('limit'))) ? $data->limit : $this->EE->TMPL->fetch_param('limit');
}
}
$data->p_page = ($data->p_page == '' OR ($data->p_limit > 1 AND $data->p_page == 1)) ? 0 : $data->p_page;
if ($data->p_page > $data->total_rows)
{
$data->p_page = 0;
}
$data->current_page = floor(($data->p_page / $data->p_limit) + 1);
$data->total_pages = intval(floor($data->total_rows / $data->p_limit));
}
else
{
// Field pagination - base values
if ($count == 0)
{
$data->sql = '';
return;
}
$m_fields = array();
foreach ($data->multi_fields as $val)
{
foreach($data->cfields as $site_id => $cfields)
{
if (isset($cfields[$val]))
{
if (isset($row['field_id_'.$cfields[$val]]) AND $row['field_id_'.$cfields[$val]] != '')
{
$m_fields[] = $val;
}
}
}
}
$data->p_limit = 1;
$data->total_rows = count($m_fields);
$data->total_pages = $data->total_rows;
if ($data->total_pages == 0)
$data->total_pages = 1;
$data->p_page = ($data->p_page == '') ? 0 : $data->p_page;
if ($data->p_page > $data->total_rows)
{
$data->p_page = 0;
}
$data->current_page = floor(($data->p_page / $data->p_limit) + 1);
if (isset($m_fields[$data->p_page]))
{
$this->EE->TMPL->tagdata = preg_replace("/".LD."multi_field\=[\"'].+?[\"']".RD."/s", LD.$m_fields[$data->p_page].RD, $this->EE->TMPL->tagdata);
$this->EE->TMPL->var_single[$m_fields[$data->p_page]] = $m_fields[$data->p_page];
}
}
// Create the pagination
if ($data->total_rows > 0 && $data->p_limit > 0)
{
if ($data->total_rows % $data->p_limit)
{
$data->total_pages++;
}
}
if ($data->total_rows > $data->p_limit)
{
$this->EE->load->library('pagination');
if (strpos($data->basepath, SELF) === FALSE && $this->EE->config->item('site_index') != '')
{
$data->basepath .= SELF;
}
if ($this->EE->TMPL->fetch_param('paginate_base'))
{
// Load the string helper
$this->EE->load->helper('string');
$data->basepath = $this->EE->functions->create_url(trim_slashes($this->EE->TMPL->fetch_param('paginate_base')));
}
$config['base_url'] = $data->basepath;
$config['prefix'] = 'P';
$config['total_rows'] = $data->total_rows;
$config['per_page'] = $data->p_limit;
$config['cur_page'] = $data->p_page;
$config['first_link'] = $this->EE->lang->line('pag_first_link');
$config['last_link'] = $this->EE->lang->line('pag_last_link');
// Allows $config['cur_page'] to override
$config['uri_segment'] = 0;
$this->EE->pagination->initialize($config);
$data->pagination_links = $this->EE->pagination->create_links();
if ((($data->total_pages * $data->p_limit) - $data->p_limit) > $data->p_page)
{
$data->page_next = reduce_double_slashes($data->basepath.'/P'.($data->p_page + $data->p_limit));
}
if (($data->p_page - $data->p_limit ) >= 0)
{
$data->page_previous = reduce_double_slashes($data->basepath.'/P'.($data->p_page - $data->p_limit));
}
}
else
{
$data->p_page = '';
}
/* --------------------------------------
* Now we use all that lovely data, and just look for our own tag
* which we'll replace with nicer links, it'll ignore it if it's
* not there.
/* --------------------------------------*/
// Limit represents the number of pages to show in the list
// ExpressionEngine's own pagination defaults to 5
$limit = 5;
// Make sure it's odd, and increment if not
$limit = $limit % 2 == 0 ? $limit + 1 : $limit;
// Side puts the current page in the center of the limit
$side = ($limit - 1) / 2;
if ( $data->total_pages <= $limit )
{
$lower = 1;
$upper = $data->total_pages;
}
// If there can't be a page to the left of the current page
elseif ( $data->current_page - $side <= 0 )
{
$lower = 1;
$upper = $lower -1 + $limit;
}
// If there can't be enough pages to the right of the current page
elseif ( $data->current_page + $side > $data->total_pages )
{
$lower = $data->total_pages + 1 - $limit;
$upper = $data->total_pages;
}
// Otherwise just show the current page in the middle
else
{
$lower = $data->current_page - $side;
$upper = $data->current_page + $side;
}
// Find the {ed_pages} pair, and work away
$pattern = '/'.LD.'ed_pages'.RD.'(.*)'.LD.'\/ed_pages'.RD.'/si';
if ( preg_match($pattern, $tagdata, $matches) )
{
// Build it!
$links = '';
for ( $i=$lower; $i<=$upper; $i++ )
{
// The data between the tag pair
$link = $matches[1];
// Just a string for now for current page
$cur = $i == $data->current_page ? 'cur' : '';
// href is either the basepath, or the current page * num on each page
$href = $i > 1 ? $data->basepath.'/P'.( ($i-1) * $data->p_limit) : $data->basepath;
// Single variable replacements
$link = str_replace(LD.'href'.RD, $href, $link);
$link = str_replace(LD.'cur'.RD, $cur, $link);
$link = str_replace(LD.'page_no'.RD, $i, $link);
// Add a new line for each link
$links .= $link."\n";
}
// Replace the original tag with our newly built one
$data->paginate_data = str_replace($matches[0], $links, $data->paginate_data);
}
// First and last pages
$last_page = ($data->total_pages -1 ) * $data->p_limit;
$data->paginate_data = str_replace(LD.'ed_first_page_href'.RD, $data->basepath, $data->paginate_data);
$data->paginate_data = str_replace(LD.'ed_last_page_href'.RD, $data->basepath.'/P'.$last_page, $data->paginate_data);
}
}
}
/* End of file ext.ed_pagination.php */
/* Location: ./system/expressionengine/third_party/ed_pagination/ext.ed_pagination.php */