-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTextformatterPagination.module
More file actions
244 lines (206 loc) · 7.76 KB
/
TextformatterPagination.module
File metadata and controls
244 lines (206 loc) · 7.76 KB
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
<?php
/**
* ProcessWire Pagination Textformatter
*
* ProcessWire 2.x
* Copyright (C) 2014 by Ryan Cramer
* Licensed under GNU/GPL v2, see LICENSE.TXT
*
* http://processwire.com
*
*/
class TextformatterPagination extends Textformatter {
public static function getModuleInfo() {
return array(
'title' => 'Pagination',
'version' => 3,
'summary' => "Provides pagination capability within a block of copy. Put 5+ hyphens '-----' on a single line (within paragraph tags) for regular pagination, or '----- Your Text' on a single line for named pagination. See the README for more.",
'href' => 'https://github.com/ryancramerdesign/TextformatterPagination',
'author' => 'Ryan Cramer',
'requires' => 'MarkupPagerNav'
);
}
/**
* Pagination options
*
* You may also include any options present in MarkupPagerNav's options list
*
* Populate this array from your /site/config.php with:
* $config->TextformatterPagination = array( ... );
*
*/
protected $options = array(
'titleListMarkup' => "<hr /><ol class='TextformatterPagination'>{out}</ol>",
'titleItemMarkup' => "<li><a href='{url}'>{out}</a></li>",
'titleCurrentItemMarkup' => "<li>{out}</li>",
'titleNextMarkup' => "<p><strong>Next: <a href='{url}'>{out}</a> »</strong></p>",
'useNumberPagination' => true, // Whether to use MarkupPagerNav style numbered pagination
'useTitlePagination' => true, // Whether to use a Title pagination list, when available
'useTitleNext' => true, // Whether to use "Next: [title]" links above Title Pagination, when available
'titlePaginationFirst' => true, // Whether to display Title pagination above Numbered pagination
'throw404' => true, // Throw a 404 exception if invalid pagination is accessed?
);
protected $populatePagination = true;
protected $initialized = false;
/**
* Process the text and insert pagination
*
*/
public function formatValue(Page $page, Field $field, &$value) {
if(!$this->initialized) {
if(is_array($this->wire('config')->TextformatterPagination)) {
$this->options = array_merge($this->options, $this->wire('config')->TextformatterPagination);
}
$this->initialized = true;
}
$key = '~~~PAG~~~';
if(strpos($value, '-----') === false) return; // quick exit
if(strpos($value, $key) !== false) return;
if(!$page->template->allowPageNum) {
if($page->editable()) $value = $this->getAdminError($page) . $value;
return;
}
if(stripos($value, 'pagination-off-all') !== false && preg_match('{>\s*pagination-off-all\s*<}i', $value)) {
$this->populatePagination = false;
}
// paragraph version
$value = preg_replace('{<p>\s*-----+\s*(.*?)</p>}i', $key . '$1~~~' . "\t", $value);
// headline version
$value = preg_replace('{<(h\d)>\s*-----+\s*(.+?)</\\1>}i', $key . '$2~~~' . "\t\n<$1>$2</$1>", $value);
if(strpos($value, $key) === false) return;
$blocks = explode($key, $value);
if(!count($blocks)) return;
if(empty($blocks[0])) array_shift($blocks);
$titles = array();
$hasTitles = false;
foreach($blocks as $k => $v) {
$pos = strpos($v, "~~~\t");
if($pos === false && !$k) {
// first item, with no named title at top: use page title
$titles[$k] = $page->title;
continue;
}
$title = trim(substr($v, 0, $pos));
$titles[$k] = trim($title);
if(strlen($title)) $hasTitles = true;
$blocks[$k] = substr($v, $pos+4);
}
$numPages = count($blocks);
$pageNum = ($this->wire('input')->pageNum - 1);
if(!isset($blocks[$pageNum])) {
if($this->options['throw404']) throw new Wire404Exception();
$blocks[$pageNum] = '';
}
$value = $blocks[$pageNum];
$numberPagination = '';
$titlePagination = '';
// values we will set to $page->TextformatterPagination variable
$extras = array(
'numPages' => $numPages,
'pageNum' => $pageNum, // zero-based page number
'numberPagination' => '',
'titlePagination' => '',
'title' => isset($titles[$pageNum]) ? $titles[$pageNum] : '', // current title
'titles' => $titles,
'titleNextLink' => ''
);
if($this->options['useNumberPagination']) {
$numberPagination = $this->getNumberPagination($page, $blocks, $pageNum, $extras);
}
if($hasTitles && $this->options['useTitlePagination']) {
$titlePagination = $this->getTitlePagination($page, $titles, $pageNum, $extras);
}
$this->populateTokens($value, $extras);
if($this->populatePagination) {
if($this->options['titlePaginationFirst']) {
$value .= "$titlePagination$numberPagination";
} else {
$value .= "$numberPagination$titlePagination";
}
}
// set a value to the page to let the developer know pagination is active
// as well as to give them the opportunity to re-use any of the pagination
// links if they want to (like at the top of the page)
$page->set('TextformatterPagination', $extras);
}
/**
* Look for tokens in $value and replace with pagination data when found
*
*/
protected function populateTokens(&$value, &$extras) {
// these tokens consume the surrounding tag
$tokens = array(
'pagination-titles' => $extras['titlePagination'],
'pagination-titles-next' => $extras['titleNextLink'],
'pagination-numbers' => $extras['numberPagination'],
'pagination-off-all' => '',
'pagination-off' => '',
);
foreach($tokens as $tokenKey => $tokenValue) {
if(stripos($value, $tokenKey) === false) continue;
if($tokenKey == 'pagination-off') $this->populatePagination = false;
$value = preg_replace('{<([a-z]+[0-9]?)>\s*' . $tokenKey . '\s*</\\1>}si', $tokenValue, $value);
}
// these tokens do not consume the surrounding tag (nor do they require one)
$tokens = array(
'pagination-current' => $extras['pageNum']+1,
'pagination-total' => $extras['numPages'],
'pagination-title' => $extras['title'],
);
foreach($tokens as $tokenKey => $tokenValue) {
if(stripos($value, $tokenKey) === false) continue;
$value = preg_replace('{\b' . $tokenKey . '\b}i', $tokenValue, $value);
}
}
/**
* Render the title pagination
*
*/
protected function getTitlePagination(Page $page, array $titles, $pageNum, &$extras) {
$prefix = $this->wire('config')->pageNumUrlPrefix;
if(!$prefix) $prefix = 'page';
$out = '';
$nextOut = '';
foreach($titles as $k => $title) {
if(empty($title)) continue;
$url = $k > 0 ? (rtrim($page->url, '/') . "/$prefix" . ($k+1)) : $page->url;
$markup = $k == $pageNum ? $this->options['titleCurrentItemMarkup'] : $this->options['titleItemMarkup'];
$out .= str_replace(array('{url}', '{out}'), array($url, $title), $markup);
if($k == $pageNum+1) {
$nextOut = str_replace(array('{url}', '{out}'), array($url, $title), $this->options['titleNextMarkup']);
}
}
$out = str_replace('{out}', $out, $this->options['titleListMarkup']);
$extras['titlePagination'] = $out;
$extras['titleNextLink'] = $nextOut;
if($this->options['useTitleNext']) $out = $nextOut . $out;
return $out;
}
/**
* Render the numbered pagination
*
*/
protected function getNumberPagination(Page $page, array $blocks, $pageNum, &$extras) {
$pager = $this->wire('modules')->get('MarkupPagerNav');
$pageArray = new PageArray();
$pageArray->setStart($pageNum);
$pageArray->setTotal(count($blocks));
$pageArray->setLimit(1);
$out = $pager->render($pageArray, $this->options);
$extras['numberPagination'] = $out;
return $out;
}
/**
* Tell the admin they need to enable page numbers for the template
*
*/
protected function getAdminError(Page $page) {
return "<p style='background: crimson; color: white; padding: 0.5em;'>
Pagination Textformatter Alert:
Please enable <i>page numbers</i> for template <b>$page->template</b> in
<u>Setup</u> > <u>Templates</u> > <u>URLs</u>.
This message only appears to editors of this page.
</p>
";
}
}