praiadeseselle/wire/modules/Process/ProcessPageEdit/PageBookmarks.php

321 lines
10 KiB
PHP
Raw Normal View History

2022-03-08 15:55:41 +01:00
<?php namespace ProcessWire;
/**
* Class PageBookmarks
*
* Class for managing Page bookmarks, currently used by ProcessPageEdit and ProcessPageList
*
*/
class PageBookmarks extends Wire {
/**
* @var Process
*
*/
protected $process;
/**
* @var array
*
*/
protected $labels = array();
/**
2023-03-10 19:41:40 +01:00
* @param Process|ProcessPageEdit $process
2022-03-08 15:55:41 +01:00
*
*/
public function __construct(Process $process) {
2023-03-10 19:41:40 +01:00
parent::__construct();
2022-03-08 15:55:41 +01:00
$this->process = $process;
$this->labels = array(
'bookmarks' => $this->_('Bookmarks'),
'edit-bookmarks' => $this->_('Edit Bookmarks'),
'all' => $this->_('all'),
);
}
/**
* Initialize/create the $options array for executeNavJSON() in Process modules
*
* @param array $options
* @return array
*
*/
public function initNavJSON(array $options = array()) {
2023-03-10 19:41:40 +01:00
$pages = $this->wire()->pages;
$user = $this->wire()->user;
$modules = $this->wire()->modules;
2022-03-08 15:55:41 +01:00
$bookmarkFields = array();
$bookmarksArray = array();
$rolesArray = array();
2023-03-10 19:41:40 +01:00
$data = $modules->getModuleConfigData($this->process);
2022-03-08 15:55:41 +01:00
$iconKey = isset($options['iconKey']) ? $options['iconKey'] : '_icon';
$classKey = isset($options['classKey']) ? $options['classKey'] : '_class';
$options['classKey'] = $classKey;
$options['iconKey'] = $iconKey;
if(!isset($options['defaultIcon'])) $options['defaultIcon'] = 'arrow-circle-right';
2023-03-10 19:41:40 +01:00
foreach($this->wire()->user->roles as $role) {
if($role->name === 'guest') continue;
2022-03-08 15:55:41 +01:00
$value = isset($data["bookmarks"]["_$role->id"]) ? $data["bookmarks"]["_$role->id"] : array();
if(empty($value)) continue;
$bookmarkFields[$role->name] = $value;
$rolesArray[$role->name] = $role;
}
$bookmarkFields['bookmarks'] = isset($data['bookmarks']["_0"]) ? $data['bookmarks']["_0"] : array();
$n = 0;
foreach($bookmarkFields as $name => $bookmarkIDs) {
2023-03-10 19:41:40 +01:00
$bookmarks = count($bookmarkIDs) ? $pages->getById($bookmarkIDs) : array();
2022-03-08 15:55:41 +01:00
$role = isset($rolesArray[$name]) ? $rolesArray[$name] : null;
foreach($bookmarks as $page) {
if($this->process == 'ProcessPageEdit' && !$page->editable()) continue;
else if($this->process == 'ProcessPageAdd' && !$page->addable()) continue;
else if(!$page->listable()) continue;
if(isset($bookmarksArray[$page->id])) continue;
$icon = $page->template->getIcon();
if(!$icon) $icon = $options['defaultIcon'];
$page->setQuietly($iconKey, $icon);
$page->setQuietly('_roleName', $role ? $role->name : $this->labels['all']);
$bookmarksArray[$page->id] = $page;
}
$n++;
}
if(empty($options['add'])) {
2023-03-10 19:41:40 +01:00
if($user->isSuperuser()) {
2022-03-08 15:55:41 +01:00
$options['add'] = 'bookmarks/?role=0';
$options['addLabel'] = $this->labels['bookmarks'];
$options['addIcon'] = 'bookmark-o';
} else {
$options['add'] = null;
}
2023-03-10 19:41:40 +01:00
} else if($user->isSuperuser()) {
2022-03-08 15:55:41 +01:00
$add = $this->wire(new WireData());
$add->set('_icon', 'bookmark-o');
$add->set('title', $this->labels['bookmarks']);
$add->set('id', 'bookmark');
$add->set($classKey, 'separator');
array_unshift($bookmarksArray, $add);
}
if(isset($options['items'])) {
$options['items'] = $options['items'] + $bookmarksArray;
} else {
$options['items'] = $bookmarksArray;
}
if(!isset($options['itemLabel'])) $options['itemLabel'] = 'title|name';
if(!isset($options['sort'])) $options['sort'] = false;
if(!isset($options['iconKey'])) $options['iconKey'] = '_icon';
if(empty($options['edit'])) {
2023-03-10 19:41:40 +01:00
$options['edit'] = $this->wire()->config->urls->admin . 'page/edit/?id={id}';
2022-03-08 15:55:41 +01:00
}
return $options;
}
/**
* Render list of current bookmarks
*
* @return string
*
*/
public function listBookmarks() {
2023-03-10 19:41:40 +01:00
$sanitizer = $this->wire()->sanitizer;
$config = $this->wire()->config;
$config->styles->add($config->urls('ProcessPageEdit') . 'PageBookmarks.css');
$superuser = $this->wire()->user->isSuperuser();
2022-03-08 15:55:41 +01:00
$out = '';
$options = $this->initNavJSON();
$noneHeadline = $this->_('There are currently no bookmarks defined');
foreach($options['items'] as $item) {
/** @var WireData $item */
if($item->id == 'bookmark') continue;
$url = str_replace('{id}', $item->id, $options['edit']);
$icon = $item->_icon ? "<i class='fa fa-fw fa-$item->_icon'></i> " : "";
$out .=
"<li class='$item->_class'>" .
2023-03-10 19:41:40 +01:00
"<a href='$url'>$icon" . $sanitizer->entities1($item->get('title|name')) . "</a>" .
2022-03-08 15:55:41 +01:00
"</li>";
}
$icon = "<i class='fa fa-fw fa-lg fa-bookmark-o'></i> ";
if($out) {
$out = "<h2>$icon" . $this->labels['bookmarks'] . "</h2><ul class='bookmarks'>$out</ul>";
} else {
$out = "<h2>$icon$noneHeadline</h2>";
}
if($superuser) {
2023-03-10 19:41:40 +01:00
/** @var InputfieldButton $button */
$button = $this->wire()->modules->get('InputfieldButton');
2022-03-08 15:55:41 +01:00
$button->href = "./?role=0";
$button->value = $this->labels['edit-bookmarks'];
$button->icon = 'edit';
$button->showInHeader();
$out .= $button->render();
}
return $out;
}
/**
* Provides the editor for bookmarks and returns InputfieldForm
*
* @return InputfieldForm
* @throws WirePermissionException|WireException
*
*/
public function editBookmarksForm() {
2023-03-10 19:41:40 +01:00
$modules = $this->wire()->modules;
$input = $this->wire()->input;
$roles = $this->wire()->roles;
$roleID = $input->get('role');
if(is_null($roleID) && $input->get('id') === 'bookmarks') $roleID = 0;
2022-03-08 15:55:41 +01:00
$roleID = (int) $roleID;
2023-03-10 19:41:40 +01:00
if(!$this->wire()->user->isSuperuser()) throw new WirePermissionException("Superuser required to define bookmarks");
2022-03-08 15:55:41 +01:00
$moduleInfo = $modules->getModuleInfo($this->process);
$this->process->breadcrumb('../', $this->_($moduleInfo['title']));
$this->process->breadcrumb('./', $this->labels['bookmarks']);
2023-03-10 19:41:40 +01:00
$role = $roleID ? $roles->get($roleID) : $this->wire()->pages->newNullPage();
2022-03-08 15:55:41 +01:00
if($roleID && !$role->id) throw new WireException("Unknown role");
$allLabel = $this->_('everyone'); // All roles
$data = $modules->getModuleConfigData($this->process);
$headline = $this->labels['edit-bookmarks'];
$title = sprintf($this->_('Bookmarks for: %s'), ($role->id ? $role->name : $allLabel));
$this->process->headline($headline);
$this->process->browserTitle($title);
2023-03-10 19:41:40 +01:00
/** @var InputfieldForm $form */
2022-03-08 15:55:41 +01:00
$form = $modules->get('InputfieldForm');
$form->action = "./?role=$role->id";
$form->addClass('InputfieldFormConfirm');
$form->description = sprintf($this->_('%s Bookmark Editor'), __($moduleInfo['title'], '/wire/templates-admin/default.php'));
$form->appendMarkup = "<p style='clear:both' class='detail'><br /><i class='fa fa-info-circle ui-priority-secondary'></i> " .
$this->_('Note that only superusers are able to see this editor.') . "</p>";
2023-03-10 19:41:40 +01:00
/** @var InputfieldPageListSelectMultiple $field */
2022-03-08 15:55:41 +01:00
$field = $modules->get('InputfieldPageListSelectMultiple');
$field->attr('name', 'bookmarks');
$field->label = $title;
$field->icon = 'bookmark-o';
$field->startLabel = $this->_('Add Bookmark');
$field->description = $this->_('Click the "add bookmark" action below to select page(s) to add as bookmarks. If you want the bookmarks to only appear for a specific user role, first select the role above.');
$class = $role->id ? '' : 'ui-state-disabled';
$out = "<ul class='PageListActions actions'><li><a class='$class' href='./?role=0'>$allLabel</a></li>";
2023-03-10 19:41:40 +01:00
foreach($roles as $r) {
if($r->name === 'guest') continue;
2022-03-08 15:55:41 +01:00
$class = $r->id == $role->id ? 'ui-state-disabled' : '';
$o = "<a class='$class' href='./?role=$r->id'>$r->name</a>";
$out .= "<li>$o</li>";
}
$out .= "</ul>";
$field->prependMarkup = $out;
if(!isset($data["bookmarks"])) $data["bookmarks"] = array();
$value = isset($data["bookmarks"]["_$role->id"]) ? $data["bookmarks"]["_$role->id"] : array();
if(!is_array($value)) $value = array();
$field->attr('value', $value);
$form->add($field);
2023-03-10 19:41:40 +01:00
/** @var InputfieldSubmit $submit */
2022-03-08 15:55:41 +01:00
$submit = $modules->get('InputfieldSubmit');
$submit->attr('name', 'submit_save_bookmarks');
$submit->showInHeader();
$form->add($submit);
2023-03-10 19:41:40 +01:00
if($form->isSubmitted('submit_save_bookmarks')) {
2022-03-08 15:55:41 +01:00
// save bookmarks
2023-03-10 19:41:40 +01:00
$form->process();
2022-03-08 15:55:41 +01:00
$bookmarks = $field->attr('value');
// clear out bookmarks for roles that no longer exist
2023-03-10 19:41:40 +01:00
foreach($data['bookmarks'] as $_roleID => $_bookmarks) {
2022-03-08 15:55:41 +01:00
if($_roleID == "_0") continue;
2023-03-10 19:41:40 +01:00
$r = $roles->get((int) ltrim($_roleID, '_'));
if(!$r->id) unset($data['bookmarks'][$_roleID]);
2022-03-08 15:55:41 +01:00
}
// update bookmarks for role
$data["bookmarks"]["_$role->id"] = $bookmarks;
// save to module config data
$modules->saveModuleConfigData($this->process, $data);
$this->message($this->_('Saved bookmarks'));
2023-03-10 19:41:40 +01:00
$this->wire()->session->location("./?role=$role->id");
2022-03-08 15:55:41 +01:00
}
return $form;
}
/**
* Provides the editor or list for bookmarks and returns rendered markup
*
* @return string
* @throws WirePermissionException
*
*/
public function editBookmarks() {
2023-03-10 19:41:40 +01:00
$input = $this->wire()->input;
$roleID = $input->get('role');
2022-03-08 15:55:41 +01:00
if(is_null($roleID)) {
2023-03-10 19:41:40 +01:00
if($input->get('id') === 'bookmarks') {
2022-03-08 15:55:41 +01:00
// ok
} else {
return $this->listBookmarks();
}
}
return $this->editBookmarksForm()->render();
}
/**
* Check and update the given process page for hidden/visible status depending on useBookmarks setting
*
* @param Page $page
*
*/
public function checkProcessPage(Page $page) {
$hidden = $page->isHidden();
if($this->process->useBookmarks) {
if($hidden) {
$page->removeStatus(Page::statusHidden);
$page->save();
}
} else if(!$hidden) {
$page->addStatus(Page::statusHidden);
$page->save();
}
}
/**
* Populate any configuration inputfields to the given $inputfields wrapper for $process
*
* @param InputfieldWrapper $inputfields
*
*/
public function addConfigInputfields(InputfieldWrapper $inputfields) {
2023-03-10 19:41:40 +01:00
/** @var InputfieldCheckbox $field */
$field = $this->wire()->modules->get('InputfieldCheckbox');
2022-03-08 15:55:41 +01:00
$field->attr('name', 'useBookmarks');
$field->label = $this->_('Allow use of bookmarks?');
$field->description = $this->_('Bookmarks enable you to create shortcuts to pages from this module, configurable by user role. Useful for large applications.');
$field->icon = 'bookmark-o';
if($this->process->useBookmarks) $field->attr('checked', 'checked');
$inputfields->add($field);
}
}