1<?php 2// must be run within Dokuwiki 3if (!defined('DOKU_INC')) { 4 die(); 5} 6 7class admin_plugin_tagging extends DokuWiki_Admin_Plugin { 8 9 /** @var helper_plugin_tagging */ 10 private $hlp; 11 12 public function __construct() { 13 $this->hlp = plugin_load('helper', 'tagging'); 14 } 15 16 /** 17 * We allow use by managers 18 * 19 * @return bool always false 20 */ 21 function forAdminOnly() { 22 return false; 23 } 24 25 /** 26 * Handle tag actions 27 * 28 * FIXME remove obsolete actions 29 */ 30 function handle() { 31 global $ID, $INPUT; 32 33 //by default sort by tag name 34 if (!$INPUT->has('sort')) { 35 $INPUT->set('sort', 'tid'); 36 } 37 38 //now starts functions handle 39 if (!$INPUT->has('fn')) { 40 return false; 41 } 42 if (!checkSecurityToken()) { 43 return false; 44 } 45 46 // extract the command and any specific parameters 47 // submit button name is of the form - fn[cmd][param(s)] 48 $fn = $INPUT->param('fn'); 49 50 if (is_array($fn)) { 51 $cmd = key($fn); 52 $param = is_array($fn[$cmd]) ? key($fn[$cmd]) : null; 53 } else { 54 $cmd = $fn; 55 $param = null; 56 } 57 58 switch ($cmd) { 59 case 'rename': 60 $this->hlp->renameTag($INPUT->str('old'), $INPUT->str('new')); 61 break; 62 case 'delete': 63 $this->hlp->deleteTags(array_keys($INPUT->arr('tags')), $INPUT->str('filter')); 64 break; 65 case 'sort': 66 $INPUT->set('sort', $param); 67 break; 68 69 } 70 } 71 72 /** 73 * Draw the interface 74 */ 75 function html() { 76 echo $this->locale_xhtml('intro'); 77 $this->html_table(); 78 } 79 80 /** 81 * Display ALL the tags! 82 */ 83 protected function html_table() { 84 global $ID, $INPUT; 85 86 $headers = array( 87 array('value' => $this->getLang('admin tag'), 'sort_by' => 'tid'), 88 array('value' => $this->getLang('admin occurrence'), 'sort_by' => 'count'), 89 array('value' => $this->getLang('admin writtenas'), 'sort_by' => 'orig'), 90 array('value' => $this->getLang('admin namespaces'), 'sort_by' => 'ns'), 91 array('value' => $this->getLang('admin taggers'), 'sort_by' => 'taggers'), 92 array('value' => $this->getLang('admin actions'), 'sort_by' => false), 93 ); 94 95 $sort = explode(',', $INPUT->str('sort')); 96 $order_by = $sort[0]; 97 $desc = false; 98 if (isset($sort[1]) && $sort[1] === 'desc') { 99 $desc = true; 100 } 101 $filters = $INPUT->arr('tagging__filters'); 102 103 $tags = $this->hlp->getAllTags($INPUT->str('filter'), $order_by, $desc, $filters); 104 105 $form = new dokuwiki\Form\Form(); 106 $form->setHiddenField('do', 'admin'); 107 $form->setHiddenField('page', 'tagging'); 108 $form->setHiddenField('id', $ID); 109 $form->setHiddenField('sort', $INPUT->str('sort')); 110 111 // actions dialog 112 $form->addTagOpen('div')->id('tagging__action-dialog')->attr('style', "display:none;"); 113 $form->addTagClose('div'); 114 115 $form->addTagOpen('table')->addClass('inline plugin_tagging'); 116 117 118 /** 119 * Table headers 120 */ 121 $form->addTagOpen('tr'); 122 foreach ($headers as $header) { 123 $form->addTagOpen('th'); 124 if ($header['sort_by'] !== false) { 125 $param = $header['sort_by']; 126 $icon = 'arrow-both'; 127 $title = $this->getLang('admin sort ascending'); 128 if ($header['sort_by'] === $order_by) { 129 if ($desc === false) { 130 $icon = 'arrow-up'; 131 $title = $this->getLang('admin sort descending'); 132 $param .= ',desc'; 133 } else { 134 $icon = 'arrow-down'; 135 } 136 } 137 $form->addButtonHTML("fn[sort][$param]", $header['value'] . ' ' . inlineSVG(dirname(__FILE__) . "/images/$icon.svg")) 138 ->addClass('plugin_tagging sort_button') 139 ->attr('title', $title); 140 } else { 141 $form->addHTML($header['value']); 142 } 143 $form->addTagClose('th'); 144 } 145 $form->addTagClose('tr'); 146 147 /** 148 * Table filters for all sortable columns 149 */ 150 $form->addTagOpen('tr'); 151 foreach ($headers as $header) { 152 $form->addTagOpen('th'); 153 if ($header['sort_by'] !== false) { 154 $field = $header['sort_by']; 155 $form->addTextInput("tagging__filters[$field]"); 156 } 157 $form->addTagClose('th'); 158 } 159 $form->addTagClose('tr'); 160 161 162 foreach ($tags as $taginfo) { 163 $tagname = $taginfo['tid']; 164 $taggers = $taginfo['taggers']; 165 $written = $taginfo['orig']; 166 $ns = $taginfo['ns']; 167 168 $form->addTagOpen('tr'); 169 $form->addHTML('<td><a class="tagslist" href="' . 170 $this->hlp->getTagSearchURL($tagname) . '">' . hsc($tagname) . '</a></td>'); 171 $form->addHTML('<td>' . $taginfo['count'] . '</td>'); 172 $form->addHTML('<td>' . hsc($written) . '</td>'); 173 $form->addHTML('<td>' . hsc($ns) . '</td>'); 174 $form->addHTML('<td>' . hsc($taggers) . '</td>'); 175 // action buttons 176 $form->addHTML('<td>'); 177 $form->addButtonHTML('fn[actions][rename][' . $taginfo['tid'] . ']', inlineSVG(dirname(__FILE__) . '/images/edit.svg')) 178 ->addClass('plugin_tagging action_button')->attr('data-action', 'rename')->attr('data-tid', $taginfo['tid']); 179 $form->addButtonHTML('fn[actions][delete][' . $taginfo['tid'] . ']', inlineSVG(dirname(__FILE__) . '/images/delete.svg')) 180 ->addClass('plugin_tagging action_button')->attr('data-action', 'delete')->attr('data-tid', $taginfo['tid']); 181 $form->addHTML('</td>'); 182 $form->addTagClose('tr'); 183 } 184 185 $form->addTagClose('table'); 186 echo $form->toHTML(); 187 } 188} 189