-
Notifications
You must be signed in to change notification settings - Fork 2
/
AdminThemeModestaHelpers.php
468 lines (399 loc) · 13.4 KB
/
AdminThemeModestaHelpers.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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
<?php
/**
* AdminThemeModestaHelpers.php
*
* Rendering helper functions for use with ProcessWire Modesta admin theme.
*
* __('FOR TRANSLATORS: please translate the file /wire/templates-admin/default.php rather than this one.');
*
*/
class AdminThemeModestaHelpers extends WireData {
public function checkExtension() {
if(!extension_loaded('iconv')) {
$this->error('Warning: iconv extension is not loaded! It\'s used for date formatting in admin theme settings.');
}
}
public function __construct() {
$renderType = $this->input->get->admin_theme_render;
if($renderType && $this->wire('user')->isSuperuser()) {
if($renderType == 'templates') echo $this->renderTemplatesNav();
if($renderType == 'fields') echo $this->renderFieldsNav();
exit;
}
}
/**
* Render admin theme name in the footer
*
* @return string
*
*/
public function renderThemeName() {
if($this->wire('adminTheme')->modestaThemeName == 1) {
$info = AdminThemeModesta::getModuleInfo();
return ' / ' . $info['title'];
}
}
/**
* Perform a translation, based on text from shared admin file: /wire/templates-admin/default.php
*
* @param string $text
* @return string
*
*/
public function _($text) {
return __($text, $this->wire('config')->paths->root . 'wire/templates-admin/default.php');
}
/**
* Get the title for the current admin page
*
* @return string
*
*/
public function getHeadline() {
$headline = $this->wire('processHeadline');
if(!$headline) $headline = $this->wire('page')->get('title|name');
$headline = $this->_($headline);
return $headline;
}
/**
* Render site name in the header
*
* @return string
*
*/
public function renderSitename() {
return $this->wire('adminTheme')->modestaSitename;
}
/**
* Render date in the header
*
* @return string
*
*/
public function renderDate() {
if($this->wire('adminTheme')->modestaDate == 1) {
setlocale(LC_ALL, '' . $this->wire('adminTheme')->modestaDateLocale . '');
if(extension_loaded("iconv")) {
return "<div class='date'>" . iconv($this->wire('adminTheme')->modestaDateCodePage, 'UTF-8', strftime($this->wire('adminTheme')->modestaDateFormat, strtotime(date("d.m.Y.")))) . "</div>";
} else {
return "<div class='date'>" . strftime($this->wire('adminTheme')->modestaDateFormat, strtotime(date("d.m.Y."))) . "</div>";
}
}
}
/**
* Render pages in side panel
*
* @return string
*
*/
public function renderPageStatus($status, $template, $limit) {
$out = '<ul class="list">';
$pages = $this->wire('pages')->find('template!=' . $template . ', limit=' . $limit . ', sort=-' . $status . '');
foreach($pages as $page) {
if ($page->editable()) {
$out .= '<li><span>' . iconv($this->wire('adminTheme')->modestaDateCodePage, 'UTF-8', strftime($this->wire('adminTheme')->modestaDateFormat, $page->$status)) . '</span><a href="' . $this->wire('config')->urls->admin . 'page/edit/?id=' . $page->id .'">' . $page->title . '</a></li>';
}
}
$out .= '</ul>';
return $out;
}
/**
* Render page ID before page title
*
* @return string
*
*/
public function renderPageId() {
if($this->wire('adminTheme')->modestaPageId == 1 && $this->wire('user')->isSuperuser()) {
if($this->wire('input')->get->id == "") {
$id = $this->wire('page')->id;
} else {
$id = $this->wire('input')->get->id;
}
$out = "<div class='id'>ID: <span>" . $id ."</span></div>";
return $out;
}
}
/**
* Render unpublished pages counter
*
* @return string
*
*/
public function renderCounter() {
if($this->wire('adminTheme')->modestaUnpublished == 1) {
$pages = $this->wire('pages')->find('status=unpublished');
$counter = 0;
foreach($pages as $page) {
if(!$page->is(Page::statusHidden)) {
$counter++;
}
}
$out = '';
if($counter > 0) {
$out .= '<a class="counter">' . $counter .'</a>';
}
return $out;
}
}
/**
* Render unpublished pages
*
* @return string
*
*/
public function renderUnpublished() {
if($this->wire('adminTheme')->modestaUnpublished == 1) {
$pages = $this->wire('pages')->find('status=unpublished');
$out = '';
if($pages->getTotal() > 0) {
$out = '<ul class="list">';
foreach($pages as $page) {
if(!$page->is(Page::statusHidden)) {
$out .= '
<li><a class="tooltip" href="' . $this->wire('config')->urls->admin . 'page/edit/?id=' . $page->id .'" title="' . iconv($this->wire('adminTheme')->modestaDateCodePage, 'UTF-8', strftime($this->wire('adminTheme')->modestaDateFormat, $page->created)) . '">' . $page->title . '</a><a class="edit" href="' . $this->wire('config')->urls->admin . 'page/edit/?id=' . $page->id .'">Edit</a></li>';
}
}
$out .= '</ul>';
}
return $out;
}
}
/**
* Get user IP address
*
* @return string
*
*/
public function getIp() {
if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
$ip = $_SERVER['HTTP_CLIENT_IP'];
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
$ip = $_SERVER['REMOTE_ADDR'];
}
return $ip;
}
/**
* Render a list of breadcrumbs (list items), excluding the containing <ul>
*
* @param bool $appendCurrent Whether to append the current title/headline to the breadcrumb trail (default=true)
* @return string
*
*/
public function renderBreadcrumbs($appendCurrent = true) {
$out = "<ul>";
foreach($this->wire('breadcrumbs') as $breadcrumb) {
$title = $this->_($breadcrumb->title);
$out .= "<li><a href='{$breadcrumb->url}'>{$title}</a><i class='fa fa-angle-right'></i></li>";
}
if($appendCurrent) $out .= "<li class='title'>" . $this->getHeadline() . "</li>";
$out .= "</ul>";
return $out;
}
/**
* Render the populated shortcuts head button or blank when not applicable
*
* @return string
*
*/
public function renderAdminShortcuts() {
$user = $this->wire('user');
$config = $this->wire('config');
if($user->isGuest() || !$user->hasPermission('page-edit')) return '';
$language = $user->language && $user->language->id && !$user->language->isDefault() ? $user->language : null;
$url = $config->urls->admin . 'page/add/';
$out = '';
foreach(wire('templates') as $template) {
$parent = $template->getParentPage(true);
if(!$parent) continue;
if($parent->id) {
// one parent possible
$qs = "?parent_id=$parent->id";
} else {
// multiple parents possible
$qs = "?template_id=$template->id";
}
$label = $template->label;
if($language) $label = $template->get("label$language");
if(empty($label)) $label = $template->name;
$out .= "<li><a href='$url$qs'>$label</a></li>";
}
if(empty($out)) return '';
$label = $this->_('Add New');
$out = "
<div class='templateButton'>
<button class='dropdown-toggle'><i class='fa fa-angle-down'></i> $label</button>
<ul class='dropdown-menu'>$out</ul>
</div>
";
return $out;
}
/**
* Render runtime notices div#notices
*
* @param Notices $notices
* @return string
*
*/
public function renderAdminNotices($notices) {
if(!count($notices)) return '';
$config = $this->wire('config');
$out = "<ul id='notices' class='ui-widget'>";
foreach($notices as $n => $notice) {
$class = 'ui-state-highlight NoticeMessage';
$text = $notice->text;
$icon = '';
if($notice->flags & Notice::allowMarkup) {
// leave $text alone
} else {
// unencode entities, just in case module already entity encoded otuput
if(strpos($text, '&') !== false) $text = html_entity_decode($text, ENT_QUOTES, "UTF-8");
// entity encode it
$text = $this->wire('sanitizer')->entities($text);
}
if($notice instanceof NoticeError || $notice->flags & Notice::warning) {
$class = 'ui-state-error';
if($notice->flags & Notice::warning) {
$class .= ' NoticeWarning';
$icon = 'warning';
} else {
$class .= ' ui-priority-primary NoticeError';
$icon = 'exclamation-triangle';
}
}
if($notice->flags & Notice::debug) {
$class .= ' ui-priority-secondary NoticeDebug';
$icon = 'gear';
}
if(!$icon) $icon = 'check-square';
if($notice->class && $config->debug) $text = "{$notice->class}: $text";
$remove = $n ? '' : "<a class='notice-remove' href='#'><i class='fa fa-times-circle'></i></a>";
$out .= "\n\t\t<li class='$class'><div class='container'><p>$remove<i class='fa fa-$icon'></i> {$text}</p></div></li>";
}
$out .= "\n\t</ul><!--/notices-->";
return $out;
}
/**
* Render a single top navigation item for the given page
*
* This function designed primarily to be called by the renderNavigation() function.
*
* @param Page $page
* @param int $level Recursion level (default=0)
* @return string
*
*/
public function renderNavigationItem(Page $page, $level = 0) {
$isSuperuser = $this->wire('user')->isSuperuser();
$showItem = $isSuperuser;
$children = $page->numChildren && !$level && $page->name != 'page' ? $page->children("check_access=0") : array();
$adminURL = $this->wire('config')->urls->admin;
$out = '';
if(!$showItem) {
$checkPages = count($children) ? $children : array($page);
foreach($checkPages as $child) {
if($child->viewable()) {
$showItem = true;
break;
}
}
}
if(!$showItem) return '';
$class = strpos(wire('page')->path, $page->path) === 0 ? 'active' : '';
$title = $this->_(strip_tags((string)$page->get('title|name')));
$out .= "<li>";
if(!$level && count($children)) {
$class = trim("$class dropdown-toggle");
$out .= "<a href='$page->url' class='$class'>$title</a>";
$my = 'left-1 top';
if($page->name == 'access') $my = 'left top';
$out .= "<ul class='dropdown-menu dropdown-main' data-my='$my' data-at='left bottom'>";
foreach($children as $child) {
if($isSuperuser && ($child->id == 11 || $child->id == 16)) {
// has ajax items
$addLabel = $this->_('Add New');
$out .= "<li><a class='has-ajax-items' data-from='topnav-page-$page' href='$child->url'>" . $this->_($child->title) . "</a><ul>" .
"<li><a class='add' href='{$child->url}add'><i class='fa fa-plus-circle'></i> $addLabel</a></li>" .
"</ul></li>";
} else {
$out .= $this->renderNavigationItem($child, $level+1);
}
}
$out .= "</ul>";
} else {
$class = $class ? " class='$class'" : '';
$out .= "<a href='$page->url'$class>$title</a>";
}
$out .= "</li>";
return $out;
}
/**
* Render navigation
*
* @return string
*
*/
public function renderNavigation() {
$out = '<ul class="navigation">';
$admin = $this->wire('pages')->get(wire('config')->adminRootPageID);
foreach($admin->children("check_access=0") as $page) {
if(!$page->viewable()) continue;
$out .= $this->renderNavigationItem($page);
}
$out .= "</ul>";
return $out;
}
/**
* Render the browser <title>
*
* @return string
*
*/
public function renderBrowserTitle() {
$browserTitle = $this->wire('processBrowserTitle');
if(!$browserTitle) $browserTitle = $this->_(strip_tags(wire('page')->get('title|name'))) . ' • ProcessWire';
if(strpos($browserTitle, '&') !== false) $browserTitle = html_entity_decode($browserTitle, ENT_QUOTES, 'UTF-8'); // we don't want to make assumptions here
$browserTitle = $this->wire('sanitizer')->entities($browserTitle, ENT_QUOTES, 'UTF-8');
$httpHost = $this->wire('config')->httpHost;
if(strpos($httpHost, 'www.') === 0) $browserTitle = substr($browserTitle, 4); // remove www
if(strpos($httpHost, ':')) $httpHost = preg_replace('/:\d+/', '', $httpHost); // remove port
$browserTitle = $this->wire('sanitizer')->entities($httpHost) . ' • ' . $browserTitle;
return $browserTitle;
}
/**
* Render the class that will be used in the <body class=''> tag
*
* @return string
*
*/
public function renderBodyClass() {
$page = $this->wire('page');
$bodyClass = $this->wire('input')->get->modal ? 'modal ' : '';
$bodyClass .= "id-{$page->id} template-{$page->template->name}";
if(wire('config')->js('JqueryWireTabs')) $bodyClass .= " hasWireTabs";
return $bodyClass;
}
/**
* Render the required javascript 'config' variable for the document <head>
*
* @return string
*
*/
public function renderJSConfig() {
$config = $this->wire('config');
$jsConfig = $config->js();
$jsConfig['debug'] = $config->debug;
$jsConfig['urls'] = array(
'root' => $config->urls->root,
'admin' => $config->urls->admin,
'modules' => $config->urls->modules,
'core' => $config->urls->core,
'files' => $config->urls->files,
'templates' => $config->urls->templates,
'adminTemplates' => $config->urls->adminTemplates,
);
return "var config = " . json_encode($jsConfig);
}
}