forked from outflux3/PrevNextTabs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PrevNextTabs.module
287 lines (228 loc) · 9.68 KB
/
PrevNextTabs.module
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
<?php
/**
* ProcessWire PrevNextTabs
*
* Add Previous and Next Page Links
*
* Portions of this module based on Page References Tab written by Niklas Lakanen
*
*/
class PrevNextTabs extends WireData implements Module, ConfigurableModule {
/**
* @var Page Page being edited
*
*/
private $editedPage;
/**
* Return information about this module (required)
*
* @return array
*
*/
static public function getModuleInfo() {
return array(
'title' => 'Prev/Next Tabs',
'summary' => 'Adds prev/next links (tabs) to page editor.',
'version' => '016',
'author' => 'Macrura',
'autoload' => true
);
}
protected static $configDefaults = array(
'enabledTemplates' => [],
'uikit_location' => 'top',
'reno_location' => 'top',
);
/**
* Data as used by the get/set functions
*
*/
protected $data = array();
/**
* __construct() is the right place to init config vars before they'll get populated
*
*/
public function __construct() {
$configDefaults = self::$configDefaults;
foreach($configDefaults as $key => $value) {
$this->set($key, $value);
}
}
/**
* ready() is called when both PW's API is ready and $page object is initialized (for autoload modules!)
* Add the hook here to be able to skip it based on the template of the edited page.
*
*/
public function ready() {
$this->ajaxSendData();
// we're interested in page editor only
if(wire('page')->process != 'ProcessPageEdit') return;
// skip changing templates (only target the actual edit form)
$id = (int)$this->input->get('id');
if(!$id) return;
// wire('page') would be the page with ProcessPageEdit
// GET parameter id tells the page that's being edited
$this->editedPage = wire('pages')->get($id);
// don't even consider system templates
if($this->editedPage->template->flags & Template::flagSystem) return;
// if there's only the default admin theme, $user->admin_theme is not available
$this->adminTheme = $this->user->admin_theme ? $this->user->admin_theme : $this->config->defaultAdminTheme;
// hook only if the template of the edited page has been chosen
if(in_array($this->editedPage->template->name, $this->enabledTemplates)) {
$hookTabs = true;
if($this->adminTheme == 'AdminThemeUikit' && $this->uikit_location == 'top') {
$hookTabs = false;
$this->addAssets();
$this->addHookAfter('AdminThemeUikit::renderBreadcrumbs', $this, 'hookAddPrevNextBC');
}
if($this->adminTheme == 'AdminThemeReno' && $this->reno_location == 'top') {
$hookTabs = false;
$this->addAssets();
$this->addHookAfter('Page::render', $this, 'hookAddPrevNextBC', array('priority'=>1000));
}
if($hookTabs) {
$this->addHookAfter('ProcessPageEdit::getTabs', $this, 'hookAddPrevNext');
}
}
}
/**
* This method handles the ready() state
* for ajax calls to get the urls for prev/next etc.
*/
public function ajaxSendData() {
if($this->config->ajax && $this->input->get->prevNextTabs != '') {
$id = $this->input->get->prevNextTabs;
$this->editedPage = wire('pages')->get($id);
$query = 'include=all, sort=' . $this->editedPage->parent->sortfield . ', parent='.$this->editedPage->parent;
$next = $this->editedPage->next($query);
$prev = $this->editedPage->prev($query);
$first = $this->editedPage->parent->children('include=all')->first();
$last = $this->editedPage->parent->children('include=all')->last();
$a = [
'next' => $next->id,
'nextTitle' => wire('sanitizer')->unentities($next->title),
'prev' => $prev->id,
'prevTitle' => wire('sanitizer')->unentities($prev->title),
'first' => $first->id,
'firstTitle' => wire('sanitizer')->unentities($first->title),
'last' => $last->id,
'lastTitle' => wire('sanitizer')->unentities($last->title),
];
echo json_encode($a);
exit;
}
}
public function addAssets() {
$this->config->styles->add($this->config->urls->siteModules . __CLASS__ . '/' . __CLASS__ . ".css?v=" . time());
$this->config->scripts->add($this->config->urls->siteModules . __CLASS__ . '/' . __CLASS__ . ".js?v=" . time());
}
/**
* Standard Addition of Prev/Next Links to the WireTabs
* @param HookEvent $event [The array of tabs for the page]
* @return [array] [Tabs with the added links]
*/
public function hookAddPrevNext(HookEvent $event) {
// add styles & init tooltip only if the links are to show
$this->config->styles->add($this->config->urls->siteModules . __CLASS__ . '/' . __CLASS__ . ".css?v=" . time());
$this->config->scripts->add($this->config->urls->siteModules . __CLASS__ . '/' . __CLASS__ . ".js?v=" . time());
$tabs = $event->return;
$event->replace = true;
$pnTabs = array();
$label = $this->_('Next');
$id = $this->className() . 'Next';
$a = "<a id='_ProcessPageEditNext' class='' target='_top' href='#' title='Loading...' data-action='this'>$label <i class='fa fa-spinner fa-spin'></i></a>";
$pnTabs[$id] = $a;
$label = $this->_('Prev');
$id = $this->className() . 'Prev';
$class = ' class="space-right"';
$a = "<a id='_ProcessPageEditPrev' target='_top' href='#' title='Loading...' data-action='this'{$class}><i class='fa fa-spinner fa-spin'></i> $label</a>";
$pnTabs[$id] = $a;
if($this->wire('user')->admin_theme == 'AdminThemeUikit') {
$pnTabs = array_reverse($pnTabs);
}
$tabs = array_merge($tabs,$pnTabs);
$event->return = $tabs;
}
/**
* Add Prev/Next Links (Pagination) for UiKit & Reno
* Uikit: sets up a 4/4 grid and places the breadcrumbs in the
* left 3/4 and the pagination links in the right 1/4
* @param HookEvent $event [UiKit Breadcrumbs]
* @return [string] [Markup for the breadcrumbs and prev/next pagination]
* Note - the spinners get replaced by arrows
* fa-angle-left
* fa-angle-right
*/
public function hookAddPrevNextBC(HookEvent $event) {
$liClassPrev = 'uk-pagination-previous';
$liClassNext = 'uk-pagination-next';
if($this->wire('user')->admin_theme == 'AdminThemeReno') {
$liClassPrev = 'pagination-previous';
$liClassNext = 'pagination-next';
}
$pnLinks = array();
$label = $this->_('Prev');
$url = '#';
$title = 'Loading...';
$a = "<li class='$liClassPrev'><a href='$url' title='$title' accesskey='<'><i class='fa fa-spinner fa-spin'></i> <span>$label</span></a></li>";
$pnLinks[0] = $a;
$label = $this->_('Next');
$url = '#';
$title = 'Loading...';
$a = "<li class='$liClassNext'><a href='$url' title='$title' accesskey='>'><span>$label</span> <i class='fa fa-spinner fa-spin'></i></a></li>";
$pnLinks[1] = $a;
if($this->wire('user')->admin_theme == 'AdminThemeReno') {
$links = '<div class="prev-next-links"><ul class="pagination">' . implode($pnLinks) . '</ul></div>'; //
$event->return = str_replace('<div id="breadcrumbs">', $links . '<div id="breadcrumbs">', $event->return);
} else {
$links = '<div class="uk-width-1-4 prev-next-links"><ul class="uk-pagination uk-float-right">' . implode($pnLinks) . '</ul></div>'; //
$event->return = '<div class="uk-grid uk-margin-bottom"><div class="uk-width-3-4">' . str_replace('</ul>', '</ul></div>' . $links, $event->return) . '</div>';
}
}
/**
* Return an InputfieldsWrapper of Inputfields used to configure the class
*
* @param array $data Array of config values indexed by field name
* @return InputfieldsWrapper
*
*/
public static function getModuleConfigInputfields(array $data) {
// not sure if this is needed...
// foreach(self::$configDefaults as $key => $value) {
// if(!isset($data[$key])||$data[$key]=="") $data[$key] = $value;
// }
$wrapper = new InputfieldWrapper();
$fieldEnabledTemplates = wire('modules')->get('InputfieldAsmSelect');
$fieldEnabledTemplates->attr('name+id', 'enabledTemplates');
$fieldEnabledTemplates->label = __('Enabled templates', __FILE__);
$fieldEnabledTemplates->description = __('"Prev/Next" tabs/links will be shown only for the templates selected.', __FILE__);
$fieldEnabledTemplates->attr('title', __('Enable template', __FILE__));
$fieldEnabledTemplates->setAsmSelectOption('sortable', false);
// populate with all available templates
foreach(wire('templates') as $t) {
// filter out system templates
if(!($t->flags & Template::flagSystem)) $fieldEnabledTemplates->addOption($t->name);
}
if(isset($data['enabledTemplates'])) $fieldEnabledTemplates->value = $data['enabledTemplates'];
$wrapper->add($fieldEnabledTemplates);
//--- UiKit Placement Preference ---//
$f = wire('modules')->get('InputfieldRadios');
$f->attr('name', __('uikit_location'));
$f->label = __('UiKit Placement');
if(!isset($data['uikit_location'])) $data['uikit_location'] = 'top';
$f->description = __('For users who are on the UiKit admin theme, you can have the pagination links at the top.');
$f->addOption('top', __('Top – Right of Breadcrumbs'), ($data['uikit_location'] == 'top') ? array('selected'=>'selected') : null);
$f->addOption('tabs',__('In Page Tabs, Right Side (old skool)'), ($data['uikit_location'] == 'tabs') ? array('selected'=>'selected') : null );
$wrapper->add($f);
//--- Reno Placement Preference ---//
$f = wire('modules')->get('InputfieldRadios');
$f->attr('name', __('reno_location'));
$f->label = __('Reno Placement');
$f->description = __('For users who are on the Reno admin theme, you can have the pagination at the top.');
if(!isset($data['reno_location'])) $data['reno_location'] = 'top';
$f->addOption('top', __('Top – Right of Breadcrumbs'), ($data['reno_location'] == 'top') ? array('selected'=>'selected') : null);
$f->addOption('tabs',__('In Page Tabs, Right Side (old skool)'), ($data['reno_location'] == 'tabs') ? array('selected'=>'selected') : null);
$wrapper->add($f);
return $wrapper;
}
}