Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Implemented 'Edit Tags' dialog #68

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions MenuItem.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

namespace dokuwiki\plugin\tagging;

use dokuwiki\Menu\Item\AbstractItem;

/**
* Class MenuItemTagging
*
* Implements the tagging button for DokuWiki's menu system
*
* @package dokuwiki\plugin\tagging
*/
class MenuItem extends AbstractItem {

/** @var string do action for this plugin */
protected $type = 'plugin_tagging__edit';

/** @var string icon file */
protected $svg = DOKU_INC . 'lib/plugins/tagging/admin.svg';

/**
* MenuItem constructor.
*/
public function __construct() {
parent::__construct();
global $REV;
if($REV) $this->params['rev'] = $REV;
}

/**
* Get label from plugin language file
*
* @return string
*/
public function getLabel() {
$hlp = plugin_load('helper', 'tagging');
return $hlp->getLang('edit_tags_button');
}

/**
* Return the link this item links to
*
* @return string
*/
public function getLink() {
return 'javascript:void(0);';
}

/**
* Convenience method to get the attributes for constructing an <a> element
*
* @see buildAttributes()
* @return array
*/
public function getLinkAttributes($classprefix = 'menuitemtagging ') {
global $INFO;
global $lang;

$attr = array(
'href' => $this->getLink(),
'title' => $this->getTitle(),
);
$attr['rel'] = 'nofollow';

/** @var helper_plugin_tagging $hlp */
$hlp = plugin_load('helper', 'tagging');

$filter = array('pid' => $INFO['id']);
if ($hlp->getConf('singleusermode')) {
$filter['tagger'] = 'auto';
}

$tags = $hlp->findItems($filter, 'tag');
$attr['data-tags'] = implode(', ', array_keys($tags));

return $attr;
}
}
160 changes: 160 additions & 0 deletions action.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ function register(Doku_Event_Handler $controller) {
'PLUGIN_MOVE_PAGE_RENAME', 'AFTER', $this,
'update_moved_page'
);

$controller->register_hook(
'MENU_ITEMS_ASSEMBLY', 'AFTER', $this,
'add_menu', array());
}

/**
Expand Down Expand Up @@ -103,6 +107,12 @@ function handle_ajax_call_unknown(Doku_Event &$event, $param) {
$this->deleteTag();
} elseif ($event->data === 'plugin_tagging_rename') {
$this->renameTag();
} elseif ($event->data === 'plugin_tagging_get') {
$this->getTags();
} elseif ($event->data === 'plugin_tagging_add_tag') {
$this->addTag();
} elseif ($event->data === 'plugin_tagging_remove_tag') {
$this->removeTag();
} else {
$handled = false;
}
Expand Down Expand Up @@ -602,4 +612,154 @@ protected function restoreSearchQuery()
global $QUERY;
$QUERY = $this->originalQuery;
}

/**
* Add tagging button to page tools menu
*
* @param Doku_Event $event
*/
public function add_menu(Doku_Event $event) {
if($event->data['view'] != 'page') return;
// ToDo: only insert button if configured and if logged in
// user has got the required permissions
if($this->getConf('showedittagsbutton')) {
array_splice($event->data['items'], -1, 0, [new \dokuwiki\plugin\tagging\MenuItem()]);
}
}

protected function getTags() {
global $INFO;

/** @var helper_plugin_tagging $hlp */
$hlp = plugin_load('helper', 'tagging');

$filter = array('pid' => $INFO['id']);
if ($hlp->getConf('singleusermode')) {
$filter['tagger'] = 'auto';
}

$tags = $hlp->findItems($filter, 'tag');
$tags = implode(', ', array_keys($tags));
$tags = array('tags' => $tags);

header('Content-Type: application/json');
echo json_encode($tags);
}

/**
* Add single tag, return current tags
*/
function addTag() {
global $INPUT;
global $INFO;

$error = false;

/** @var helper_plugin_tagging $hlp */
$hlp = plugin_load('helper', 'tagging');

$data = $INPUT->arr('tagging');
$id = $INPUT->str('id');
$tag = $INPUT->str('tag');
$user = $hlp->getUser();
$INFO['writable'] = auth_quickaclcheck($id) >= AUTH_EDIT; // we also need this in findItems

if (empty($id) || empty($tag) || empty($user)) {
$error = true;
$msg = 'missing parameters';
} else if ($INFO['writable'] && $hlp->getUser()) {
// Get saved tags
$filter = array('pid' => $INFO['id']);
if ($hlp->getConf('singleusermode')) {
$filter['tagger'] = 'auto';
}

$tags = $hlp->findItems($filter, 'tag');
$tags = array_keys($tags);

// Add new tag, if not yet existing
if (array_search($tag, $tags) === false) {
array_push($tags, $tag);
$hlp->replaceTags($id, $user, $tags);
$msg = 'added \''.$tag.'\' to page \''.$id.'\'';
} else {
$error = true;
$msg = 'tag already exists';
}
} else {
$error = true;
$msg = 'permission denied';
}

// Return JSON encoded list of current tags
$tags = $hlp->findItems($filter, 'tag');
$tags = implode(', ', array_keys($tags));
if ($error) {
$result = array('tags' => $tags, 'error' => $error, 'message' => $msg);
} else {
$result = array('tags' => $tags, 'error' => $error, 'message' => $msg);
}

header('Content-Type: application/json');
echo json_encode($result);
}

/**
* Remove single tag, return current tags
*/
function removeTag() {
global $INPUT;
global $INFO;

$error = false;

/** @var helper_plugin_tagging $hlp */
$hlp = plugin_load('helper', 'tagging');

$data = $INPUT->arr('tagging');
$id = $INPUT->str('id');
$tag = $INPUT->str('tag');
$user = $hlp->getUser();
$INFO['writable'] = auth_quickaclcheck($id) >= AUTH_EDIT; // we also need this in findItems

if (empty($id) || empty($tag) || empty($user)) {
$error = true;
$msg = 'missing parameters';
} else if ($INFO['writable'] && $hlp->getUser()) {
// Get saved tags
$filter = array('pid' => $INFO['id']);
if ($hlp->getConf('singleusermode')) {
$filter['tagger'] = 'auto';
}

$tags = $hlp->findItems($filter, 'tag');
$tags = array_keys($tags);

// Remove tag, if existing
$key = array_search($tag, $tags);
if ($key !== false) {
unset($tags[$key]);
$hlp->replaceTags($id, $user, $tags);
$msg = 'removed \''.$tag.'\' from page \''.$id.'\'';
} else {
$error = true;
$msg = 'tag does not exist';
}
} else {
$error = true;
$msg = 'permission denied';
}

// Return JSON encoded list of current tags
$tags = $hlp->findItems($filter, 'tag');
$tags = implode(', ', array_keys($tags));
if ($error) {
$result = array('tags' => $tags, 'error' => $error, 'message' => $msg);
} else {
$result = array('tags' => $tags, 'error' => $error, 'message' => $msg);
}

header('Content-Type: application/json');
echo json_encode($result);
}
}
9 changes: 5 additions & 4 deletions conf/default.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php
$conf['cloudlimit'] = 50;
$conf['singleusermode'] = 0;
$conf['hiddenprefix'] = '@';
$conf['hidens'] = 0;
$conf['cloudlimit'] = 50;
$conf['singleusermode'] = 0;
$conf['hiddenprefix'] = '@';
$conf['hidens'] = 0;
$conf['showedittagsbutton'] = 0;
1 change: 1 addition & 0 deletions conf/metadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
$meta['singleusermode'] = array('onoff');
$meta['hiddenprefix'] = array('string');
$meta['hidens'] = array('onoff');
$meta['showedittagsbutton'] = array('onoff');
7 changes: 6 additions & 1 deletion lang/en/lang.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
$lang['admin no invalid'] = 'No invalid tags found.';
$lang['admin clean'] = 'Irrevocably delete all invalid taggings';
$lang['toggle admin mode'] = 'Tag Admin';
$lang['edit_tags_button'] = 'Edit tags';

$lang['no_admin'] = 'Error. Only admins can modify tags.';

Expand All @@ -50,6 +51,10 @@
$lang['js']['admin_sure'] = 'Are you sure?';
$lang['js']['admin_newtags'] = 'Please enter the new tag name. You can provide multiple comma separated tags to split the tag into multiple.';
$lang['js']['search_nofilter'] = 'nothing found';

$lang['js']['edit_dialog_title'] = 'Edit Tags';
$lang['js']['edit_dialog_text_list'] = 'The following tags are set:';
$lang['js']['edit_dialog_button_delete'] = 'Delete';
$lang['js']['edit_dialog_button_add'] = 'Add';
$lang['js']['edit_dialog_label_add'] = 'Add a new tag:';

$lang['tagjmp_error'] = 'Could not find any page tagged with %s';
9 changes: 5 additions & 4 deletions lang/en/settings.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php
$lang['cloudlimit'] = 'Maximum tags to show in in-pgae tag clouds. Set 0 for all matching tags';
$lang['singleusermode'] = 'Enable single user moder, eg. all tags can be edited by anyone.';
$lang['hiddenprefix'] = 'Tags with this prefix will not be shown to read-only users';
$lang['hidens'] = 'Hide the amespace column in overview tables';
$lang['cloudlimit'] = 'Maximum tags to show in in-pgae tag clouds. Set 0 for all matching tags';
$lang['singleusermode'] = 'Enable single user moder, eg. all tags can be edited by anyone.';
$lang['hiddenprefix'] = 'Tags with this prefix will not be shown to read-only users';
$lang['hidens'] = 'Hide the amespace column in overview tables';
$lang['showedittagsbutton'] = 'Show the "Edit tags" button in page menu';
Loading