1<?php 2/** 3 * Tagging Plugin (hlper component) 4 * 5 * @license GPL 2 6 */ 7class helper_plugin_tagging extends DokuWiki_Plugin { 8 9 /** 10 * Gives access to the database 11 * 12 * Initializes the SQLite helper and register the CLEANTAG function 13 * 14 * @return helper_plugin_sqlite|bool false if initialization fails 15 */ 16 public function getDB() { 17 static $db = null; 18 if ($db !== null) { 19 return $db; 20 } 21 22 /** @var helper_plugin_sqlite $db */ 23 $db = plugin_load('helper', 'sqlite'); 24 if ($db === null) { 25 msg('The tagging plugin needs the sqlite plugin', -1); 26 27 return false; 28 } 29 $db->init('tagging', __DIR__ . '/db/'); 30 $db->create_function('CLEANTAG', array($this, 'cleanTag'), 1); 31 $db->create_function('GROUP_SORT', 32 function ($group, $newDelimiter) { 33 $ex = array_filter(explode(',', $group)); 34 sort($ex); 35 36 return implode($newDelimiter, $ex); 37 }, 2); 38 $db->create_function('GET_NS', 'getNS', 1); 39 40 return $db; 41 } 42 43 /** 44 * Return the user to use for accessing tags 45 * 46 * Handles the singleuser mode by returning 'auto' as user. Returnes false when no user is logged in. 47 * 48 * @return bool|string 49 */ 50 public function getUser() { 51 if (!isset($_SERVER['REMOTE_USER'])) { 52 return false; 53 } 54 if ($this->getConf('singleusermode')) { 55 return 'auto'; 56 } 57 58 return $_SERVER['REMOTE_USER']; 59 } 60 61 /** 62 * Canonicalizes the tag to its lower case nospace form 63 * 64 * @param $tag 65 * 66 * @return string 67 */ 68 public function cleanTag($tag) { 69 $tag = str_replace(array(' ', '-', '_'), '', $tag); 70 $tag = utf8_strtolower($tag); 71 72 return $tag; 73 } 74 75 /** 76 * Canonicalizes the namespace, remove the first colon and add glob 77 * 78 * @param $namespace 79 * 80 * @return string 81 */ 82 public function globNamespace($namespace) { 83 return cleanId($namespace) . '*'; 84 } 85 86 /** 87 * Create or Update tags of a page 88 * 89 * Uses the translation plugin to store the language of a page (if available) 90 * 91 * @param string $id The page ID 92 * @param string $user 93 * @param array $tags 94 * 95 * @return bool|SQLiteResult 96 */ 97 public function replaceTags($id, $user, $tags) { 98 global $conf; 99 /** @var helper_plugin_translation $trans */ 100 $trans = plugin_load('helper', 'translation'); 101 if ($trans) { 102 $lang = $trans->realLC($trans->getLangPart($id)); 103 } else { 104 $lang = $conf['lang']; 105 } 106 107 $db = $this->getDB(); 108 $db->query('BEGIN TRANSACTION'); 109 $queries = array(array('DELETE FROM taggings WHERE pid = ? AND tagger = ?', $id, $user)); 110 foreach ($tags as $tag) { 111 $queries[] = array('INSERT INTO taggings (pid, tagger, tag, lang) VALUES(?, ?, ?, ?)', $id, $user, $tag, $lang); 112 } 113 114 foreach ($queries as $query) { 115 if (!call_user_func_array(array($db, 'query'), $query)) { 116 $db->query('ROLLBACK TRANSACTION'); 117 118 return false; 119 } 120 } 121 122 return $db->query('COMMIT TRANSACTION'); 123 } 124 125 /** 126 * Get a list of Tags or Pages matching search criteria 127 * 128 * @param array $filter What to search for array('field' => 'searchterm') 129 * @param string $type What field to return 'tag'|'pid' 130 * @param int $limit Limit to this many results, 0 for all 131 * 132 * @return array associative array in form of value => count 133 */ 134 public function findItems($filter, $type, $limit = 0) { 135 136 global $INPUT; 137 138 /** @var helper_plugin_tagging_querybuilder $queryBuilder */ 139 $queryBuilder = new \helper_plugin_tagging_querybuilder(); 140 141 $queryBuilder->setField($type); 142 $queryBuilder->setLimit($limit); 143 $queryBuilder->setTags($this->getTags($filter)); 144 if (isset($filter['ns'])) $queryBuilder->includeNS($filter['ns']); 145 if (isset($filter['notns'])) $queryBuilder->excludeNS($filter['notns']); 146 if (isset($filter['tagger'])) $queryBuilder->setTagger($filter['tagger']); 147 if (isset($filter['pid'])) $queryBuilder->setPid($filter['pid']); 148 149 return $this->queryDb($queryBuilder->getQuery()); 150 151 } 152 153 /** 154 * Constructs the URL to search for a tag 155 * 156 * @param string $tag 157 * @param string $ns 158 * 159 * @return string 160 */ 161 public function getTagSearchURL($tag, $ns = '') { 162 // wrap tag in quotes if non clean 163 $ctag = utf8_stripspecials($this->cleanTag($tag)); 164 if ($ctag != utf8_strtolower($tag)) { 165 $tag = '"' . $tag . '"'; 166 } 167 168 $ret = '?do=search&sf=1&id=' . rawurlencode($tag); 169 if ($ns) { 170 $ret .= rawurlencode(' @' . $ns); 171 } 172 173 return $ret; 174 } 175 176 /** 177 * Calculates the size levels for the given list of clouds 178 * 179 * Automatically determines sensible tresholds 180 * 181 * @param array $tags list of tags => count 182 * @param int $levels 183 * 184 * @return mixed 185 */ 186 public function cloudData($tags, $levels = 10) { 187 $min = min($tags); 188 $max = max($tags); 189 190 // calculate tresholds 191 $tresholds = array(); 192 for ($i = 0; $i <= $levels; $i++) { 193 $tresholds[$i] = pow($max - $min + 1, $i / $levels) + $min - 1; 194 } 195 196 // assign weights 197 foreach ($tags as $tag => $cnt) { 198 foreach ($tresholds as $tresh => $val) { 199 if ($cnt <= $val) { 200 $tags[$tag] = $tresh; 201 break; 202 } 203 $tags[$tag] = $levels; 204 } 205 } 206 207 return $tags; 208 } 209 210 /** 211 * Display a tag cloud 212 * 213 * @param array $tags list of tags => count 214 * @param string $type 'tag' 215 * @param Callable $func The function to print the link (gets tag and ns) 216 * @param bool $wrap wrap cloud in UL tags? 217 * @param bool $return returnn HTML instead of printing? 218 * @param string $ns Add this namespace to search links 219 * 220 * @return string 221 */ 222 public function html_cloud($tags, $type, $func, $wrap = true, $return = false, $ns = '') { 223 global $INFO; 224 225 $hidden_str = $this->getConf('hiddenprefix'); 226 $hidden_len = strlen($hidden_str); 227 228 $ret = ''; 229 if ($wrap) { 230 $ret .= '<ul class="tagging_cloud clearfix">'; 231 } 232 if (count($tags) === 0) { 233 // Produce valid XHTML (ul needs a child) 234 $this->setupLocale(); 235 $ret .= '<li><div class="li">' . $this->lang['js']['no' . $type . 's'] . '</div></li>'; 236 } else { 237 $tags = $this->cloudData($tags); 238 foreach ($tags as $val => $size) { 239 // skip hidden tags for users that can't edit 240 if ($type === 'tag' and 241 $hidden_len and 242 substr($val, 0, $hidden_len) == $hidden_str and 243 !($this->getUser() && $INFO['writable']) 244 ) { 245 continue; 246 } 247 248 $ret .= '<li class="t' . $size . '"><div class="li">'; 249 $ret .= call_user_func($func, $val, $ns); 250 $ret .= '</div></li>'; 251 } 252 } 253 if ($wrap) { 254 $ret .= '</ul>'; 255 } 256 if ($return) { 257 return $ret; 258 } 259 echo $ret; 260 261 return ''; 262 } 263 264 /** 265 * Display a List of Page Links 266 * 267 * @param array $pids list of pids => count 268 * @return string 269 */ 270 public function html_page_list($pids) { 271 global $INFO; 272 273 $ret = '<div class="search_quickresult">'; 274 $ret .= '<ul class="search_quickhits">'; 275 276 if (count($pids) === 0) { 277 // Produce valid XHTML (ul needs a child) 278 $this->setupLocale(); 279 $ret .= '<li><div class="li">' . $this->lang['js']['nopages'] . '</div></li>'; 280 } else { 281 foreach ($pids as $val => $size) { 282 $ret .= '<li><div class="li">'; 283 $ret .= html_wikilink($val); 284 $ret .= '</div></li>'; 285 } 286 } 287 288 $ret .= '</ul>'; 289 $ret .= '</div>'; 290 $ret .= '<div class="clearer"></div>'; 291 292 return $ret; 293 } 294 295 /** 296 * Get the link to a search for the given tag 297 * 298 * @param string $tag search for this tag 299 * @param string $ns limit search to this namespace 300 * 301 * @return string 302 */ 303 protected function linkToSearch($tag, $ns = '') { 304 return '<a href="' . hsc($this->getTagSearchURL($tag, $ns)) . '">' . $tag . '</a>'; 305 } 306 307 /** 308 * Display the Tags for the current page and prepare the tag editing form 309 * 310 * @param bool $print Should the HTML be printed or returned? 311 * 312 * @return string 313 */ 314 public function tpl_tags($print = true) { 315 global $INFO; 316 global $lang; 317 318 $filter = array('pid' => $INFO['id']); 319 if ($this->getConf('singleusermode')) { 320 $filter['tagger'] = 'auto'; 321 } 322 323 $tags = $this->findItems($filter, 'tag'); 324 325 $ret = ''; 326 327 $ret .= '<div class="plugin_tagging_edit">'; 328 $ret .= $this->html_cloud($tags, 'tag', array($this, 'linkToSearch'), true, true); 329 330 if ($this->getUser() && $INFO['writable']) { 331 $lang['btn_tagging_edit'] = $lang['btn_secedit']; 332 $ret .= '<div id="tagging__edit_buttons_group">'; 333 $ret .= html_btn('tagging_edit', $INFO['id'], '', array()); 334 if (auth_isadmin()) { 335 $ret .= '<label>' 336 . $this->getLang('toggle admin mode') 337 . '<input type="checkbox" id="tagging__edit_toggle_admin" /></label>'; 338 } 339 $ret .= '</div>'; 340 $form = new dokuwiki\Form\Form(); 341 $form->id('tagging__edit'); 342 $form->setHiddenField('tagging[id]', $INFO['id']); 343 $form->setHiddenField('call', 'plugin_tagging_save'); 344 $tags = $this->findItems(array( 345 'pid' => $INFO['id'], 346 'tagger' => $this->getUser(), 347 ), 'tag'); 348 $form->addTextarea('tagging[tags]') 349 ->val(implode(', ', array_keys($tags))) 350 ->addClass('edit') 351 ->attr('rows', 4); 352 $form->addButton('', $lang['btn_save'])->id('tagging__edit_save'); 353 $form->addButton('', $lang['btn_cancel'])->id('tagging__edit_cancel'); 354 $ret .= $form->toHTML(); 355 } 356 $ret .= '</div>'; 357 358 if ($print) { 359 echo $ret; 360 } 361 362 return $ret; 363 } 364 365 /** 366 * @param string $namespace empty for entire wiki 367 * 368 * @param string $order_by 369 * @param bool $desc 370 * @param array $filters 371 * @return array 372 */ 373 public function getAllTags($namespace = '', $order_by = 'tid', $desc = false, $filters = []) { 374 $order_fields = array('pid', 'tid', 'taggers', 'ns', 'count'); 375 if (!in_array($order_by, $order_fields)) { 376 msg('cannot sort by ' . $order_by . ' field does not exists', -1); 377 $order_by = 'tag'; 378 } 379 380 list($having, $params) = $this->getFilterSql($filters); 381 382 $db = $this->getDB(); 383 384 $query = 'SELECT "pid", 385 CLEANTAG("tag") AS "tid", 386 GROUP_SORT(GROUP_CONCAT("tagger"), \', \') AS "taggers", 387 GROUP_SORT(GROUP_CONCAT(GET_NS("pid")), \', \') AS "ns", 388 GROUP_SORT(GROUP_CONCAT("pid"), \', \') AS "pids", 389 COUNT(*) AS "count" 390 FROM "taggings" 391 WHERE "pid" GLOB ? AND GETACCESSLEVEL(pid) >= ' . AUTH_READ 392 . ' GROUP BY "tid"'; 393 $query .= $having; 394 $query .= 'ORDER BY ' . $order_by; 395 if ($desc) { 396 $query .= ' DESC'; 397 } 398 399 array_unshift($params, $this->globNamespace($namespace)); 400 $res = $db->query($query, $params); 401 402 return $db->res2arr($res); 403 } 404 405 /** 406 * Get all pages with tags and their tags 407 * 408 * @return array ['pid' => ['tag1','tag2','tag3']] 409 */ 410 public function getAllTagsByPage() { 411 $query = ' 412 SELECT pid, GROUP_CONCAT(tag) AS tags 413 FROM taggings 414 GROUP BY pid 415 '; 416 $db = $this->getDb(); 417 $res = $db->query($query); 418 return array_map( 419 function ($i) { 420 return explode(',', $i); 421 }, 422 array_column($db->res2arr($res), 'tags', 'pid') 423 ); 424 } 425 426 /** 427 * Renames a tag 428 * 429 * @param string $formerTagName 430 * @param string $newTagNames 431 */ 432 public function renameTag($formerTagName, $newTagNames) { 433 434 if (empty($formerTagName) || empty($newTagNames)) { 435 msg($this->getLang("admin enter tag names"), -1); 436 return; 437 } 438 439 $keepFormerTag = false; 440 441 // enable splitting tags on rename 442 $newTagNames = array_map(function ($tag) { 443 return $this->cleanTag($tag); 444 }, explode(',', $newTagNames)); 445 446 $db = $this->getDB(); 447 448 // non-admins can rename only their own tags 449 if (!auth_isadmin()) { 450 $queryTagger =' AND tagger = ?'; 451 $tagger = $this->getUser(); 452 } else { 453 $queryTagger = ''; 454 $tagger = ''; 455 } 456 457 $insertQuery = 'INSERT INTO taggings '; 458 $insertQuery .= 'SELECT pid, ?, tagger, lang FROM taggings'; 459 $where = ' WHERE CLEANTAG(tag) = ?'; 460 $where .= ' AND GETACCESSLEVEL(pid) >= ' . AUTH_EDIT; 461 $where .= $queryTagger; 462 463 $db->query('BEGIN TRANSACTION'); 464 465 // insert new tags first 466 foreach ($newTagNames as $newTag) { 467 if ($newTag === $this->cleanTag($formerTagName)) { 468 $keepFormerTag = true; 469 continue; 470 } 471 $params = [$newTag, $this->cleanTag($formerTagName)]; 472 if ($tagger) array_push($params, $tagger); 473 $res = $db->query($insertQuery . $where, $params); 474 if ($res === false) { 475 $db->query('ROLLBACK TRANSACTION'); 476 return; 477 } 478 $db->res_close($res); 479 } 480 481 // finally delete the renamed tags 482 if (!$keepFormerTag) { 483 $deleteQuery = 'DELETE FROM taggings'; 484 $params = [$this->cleanTag($formerTagName)]; 485 if ($tagger) array_push($params, $tagger); 486 if ($db->query($deleteQuery . $where, $params) === false) { 487 $db->query('ROLLBACK TRANSACTION'); 488 return; 489 } 490 } 491 492 $db->query('COMMIT TRANSACTION'); 493 494 msg($this->getLang("admin renamed"), 1); 495 496 return; 497 } 498 499 /** 500 * Rename or delete a tag for all users 501 * 502 * @param string $pid 503 * @param string $formerTagName 504 * @param string $newTagName 505 * 506 * @return array 507 */ 508 public function modifyPageTag($pid, $formerTagName, $newTagName) { 509 510 $db = $this->getDb(); 511 512 $res = $db->query( 513 'SELECT pid FROM taggings WHERE CLEANTAG(tag) = ? AND pid = ?', 514 $this->cleanTag($formerTagName), 515 $pid 516 ); 517 $check = $db->res2arr($res); 518 519 if (empty($check)) { 520 return array(true, $this->getLang('admin tag does not exists')); 521 } 522 523 if (empty($newTagName)) { 524 $res = $db->query( 525 'DELETE FROM taggings WHERE pid = ? AND CLEANTAG(tag) = ?', 526 $pid, 527 $this->cleanTag($formerTagName) 528 ); 529 } else { 530 $res = $db->query( 531 'UPDATE taggings SET tag = ? WHERE pid = ? AND CLEANTAG(tag) = ?', 532 $newTagName, 533 $pid, 534 $this->cleanTag($formerTagName) 535 ); 536 } 537 $db->res2arr($res); 538 539 return array(false, $this->getLang('admin renamed')); 540 } 541 542 /** 543 * Deletes a tag 544 * 545 * @param array $tags 546 * @param string $namespace current namespace context as in getAllTags() 547 */ 548 public function deleteTags($tags, $namespace = '') { 549 if (empty($tags)) { 550 return; 551 } 552 553 $namespace = cleanId($namespace); 554 555 $db = $this->getDB(); 556 557 $queryBody = 'FROM taggings WHERE pid GLOB ? AND (' . 558 implode(' OR ', array_fill(0, count($tags), 'CLEANTAG(tag) = ?')) . ')'; 559 $args = array_map(array($this, 'cleanTag'), $tags); 560 array_unshift($args, $this->globNamespace($namespace)); 561 562 // non-admins can delete only their own tags 563 if (!auth_isadmin()) { 564 $queryBody .= ' AND tagger = ?'; 565 array_push($args, $this->getUser()); 566 } 567 568 $affectedPagesQuery= 'SELECT DISTINCT pid ' . $queryBody; 569 $resAffectedPages = $db->query($affectedPagesQuery, $args); 570 $numAffectedPages = count($resAffectedPages->fetchAll()); 571 572 $deleteQuery = 'DELETE ' . $queryBody; 573 $db->query($deleteQuery, $args); 574 575 msg(sprintf($this->getLang("admin deleted"), count($tags), $numAffectedPages), 1); 576 } 577 578 /** 579 * Updates tags with a new page name 580 * 581 * @param string $oldName 582 * @param string $newName 583 */ 584 public function renamePage($oldName, $newName) { 585 $db = $this->getDB(); 586 $db->query('UPDATE taggings SET pid = ? WHERE pid = ?', $newName, $oldName); 587 } 588 589 /** 590 * Extracts tags from search query 591 * 592 * @param array $parsedQuery 593 * @return array 594 */ 595 public function getTags($parsedQuery) 596 { 597 $tags = []; 598 if (isset($parsedQuery['phrases'][0])) { 599 $tags = $parsedQuery['phrases']; 600 } elseif (isset($parsedQuery['and'][0])) { 601 $tags = $parsedQuery['and']; 602 } elseif (isset($parsedQuery['tag'])) { 603 // handle autocomplete call 604 $tags[] = $parsedQuery['tag']; 605 } 606 return $tags; 607 } 608 609 /** 610 * Search for tagged pages 611 * 612 * @return array 613 */ 614 public function searchPages() 615 { 616 global $INPUT; 617 global $QUERY; 618 $parsedQuery = ft_queryParser(new Doku_Indexer(), $QUERY); 619 620 /** @var helper_plugin_tagging_querybuilder $queryBuilder */ 621 $queryBuilder = new \helper_plugin_tagging_querybuilder(); 622 623 $queryBuilder->setField('pid'); 624 $queryBuilder->setTags($this->getTags($parsedQuery)); 625 $queryBuilder->setLogicalAnd($INPUT->str('taggings') === 'and'); 626 if (isset($parsedQuery['ns'])) $queryBuilder->includeNS($parsedQuery['ns']); 627 if (isset($parsedQuery['notns'])) $queryBuilder->excludeNS($parsedQuery['notns']); 628 if (isset($parsedQuery['tagger'])) $queryBuilder->setTagger($parsedQuery['tagger']); 629 if (isset($parsedQuery['pid'])) $queryBuilder->setPid($parsedQuery['pid']); 630 631 return $this->queryDb($queryBuilder->getPages()); 632 } 633 634 /** 635 * Syntax to allow users to manage tags on regular pages, respects ACLs 636 * @param string $ns 637 * @return string 638 */ 639 public function manageTags($ns) 640 { 641 global $INPUT; 642 643 $this->setDefaultSort(); 644 645 // initially set namespace filter to what is defined in syntax 646 if ($ns && !$INPUT->has('tagging__filters')) { 647 $INPUT->set('tagging__filters', ['ns' => $ns]); 648 } 649 650 return $this->html_table(); 651 } 652 653 /** 654 * HTML list of tagged pages 655 * 656 * @param string $tid 657 * @return string 658 */ 659 public function getPagesHtml($tid) 660 { 661 $html = ''; 662 663 $db = $this->getDB(); 664 $sql = 'SELECT pid from taggings where CLEANTAG(tag) = CLEANTAG(?)'; 665 $res = $db->query($sql, $tid); 666 $pages = $db->res2arr($res); 667 668 if ($pages) { 669 $html .= '<ul>'; 670 foreach ($pages as $page) { 671 $pid = $page['pid']; 672 $html .= '<li><a href="' . wl($pid) . '" target="_blank">' . $pid . '</li>'; 673 } 674 $html .= '</ul>'; 675 } 676 677 return $html; 678 } 679 680 /** 681 * Display tag management table 682 */ 683 public function html_table() { 684 global $ID, $INPUT; 685 686 $headers = array( 687 array('value' => $this->getLang('admin tag'), 'sort_by' => 'tid'), 688 array('value' => $this->getLang('admin occurrence'), 'sort_by' => 'count') 689 ); 690 691 if (!$this->conf['hidens']) { 692 array_push( 693 $headers, 694 ['value' => $this->getLang('admin namespaces'), 'sort_by' => 'ns'] 695 ); 696 } 697 698 array_push($headers, 699 array('value' => $this->getLang('admin taggers'), 'sort_by' => 'taggers'), 700 array('value' => $this->getLang('admin actions'), 'sort_by' => false) 701 ); 702 703 $sort = explode(',', $this->getParam('sort')); 704 $order_by = $sort[0]; 705 $desc = false; 706 if (isset($sort[1]) && $sort[1] === 'desc') { 707 $desc = true; 708 } 709 $filters = $INPUT->arr('tagging__filters'); 710 711 $tags = $this->getAllTags($INPUT->str('filter'), $order_by, $desc, $filters); 712 713 $form = new \dokuwiki\Form\Form(); 714 // required in admin mode 715 $form->setHiddenField('page', 'tagging'); 716 $form->setHiddenField('id', $ID); 717 $form->setHiddenField('[tagging]sort', $this->getParam('sort')); 718 719 /** 720 * Actions dialog 721 */ 722 $form->addTagOpen('div')->id('tagging__action-dialog')->attr('style', "display:none;"); 723 $form->addTagClose('div'); 724 725 /** 726 * Tag pages dialog 727 */ 728 $form->addTagOpen('div')->id('tagging__taggedpages-dialog')->attr('style', "display:none;"); 729 $form->addTagClose('div'); 730 731 /** 732 * Tag management table 733 */ 734 $form->addTagOpen('table')->addClass('inline plugin_tagging'); 735 736 $nscol = $this->conf['hidens'] ? '' : '<col class="wide-col"></col>'; 737 $form->addHTML( 738 '<colgroup> 739 <col></col> 740 <col class="narrow-col"></col>' 741 . $nscol . 742 '<col></col> 743 <col class="narrow-col"></col> 744 </colgroup>' 745 ); 746 747 /** 748 * Table headers 749 */ 750 $form->addTagOpen('tr'); 751 foreach ($headers as $header) { 752 $form->addTagOpen('th'); 753 if ($header['sort_by'] !== false) { 754 $param = $header['sort_by']; 755 $icon = 'arrow-both'; 756 $title = $this->getLang('admin sort ascending'); 757 if ($header['sort_by'] === $order_by) { 758 if ($desc === false) { 759 $icon = 'arrow-up'; 760 $title = $this->getLang('admin sort descending'); 761 $param .= ',desc'; 762 } else { 763 $icon = 'arrow-down'; 764 } 765 } 766 $form->addButtonHTML( 767 "tagging[sort]", 768 $header['value'] . ' ' . inlineSVG(__DIR__ . "/images/$icon.svg")) 769 ->addClass('plugin_tagging sort_button') 770 ->attr('title', $title) 771 ->val($param); 772 } else { 773 $form->addHTML($header['value']); 774 } 775 $form->addTagClose('th'); 776 } 777 $form->addTagClose('tr'); 778 779 /** 780 * Table filters for all sortable columns 781 */ 782 $form->addTagOpen('tr'); 783 foreach ($headers as $header) { 784 $form->addTagOpen('th'); 785 if ($header['sort_by'] !== false) { 786 $field = $header['sort_by']; 787 $input = $form->addTextInput("tagging__filters[$field]"); 788 $input->addClass('full-col'); 789 } 790 $form->addTagClose('th'); 791 } 792 $form->addTagClose('tr'); 793 794 795 foreach ($tags as $taginfo) { 796 $tagname = $taginfo['tid']; 797 $taggers = $taginfo['taggers']; 798 $ns = $taginfo['ns']; 799 $pids = explode(',',$taginfo['pids']); 800 801 $form->addTagOpen('tr'); 802 $form->addHTML('<td>'); 803 $form->addHTML('<a class="tagslist" href="#" data-tid="' . $taginfo['tid'] . '">'); 804 $form->addHTML( hsc($tagname) . '</a>'); 805 $form->addHTML('</td>'); 806 $form->addHTML('<td>' . $taginfo['count'] . '</td>'); 807 if (!$this->conf['hidens']) { 808 $form->addHTML('<td>' . hsc($ns) . '</td>'); 809 } 810 $form->addHTML('<td>' . hsc($taggers) . '</td>'); 811 812 /** 813 * action buttons 814 */ 815 $form->addHTML('<td>'); 816 817 // check ACLs 818 $userEdit = false; 819 /** @var \helper_plugin_sqlite $sqliteHelper */ 820 $sqliteHelper = plugin_load('helper', 'sqlite'); 821 foreach ($pids as $pid) { 822 if ($sqliteHelper->_getAccessLevel($pid) >= AUTH_EDIT) { 823 $userEdit = true; 824 continue; 825 } 826 } 827 828 if ($userEdit) { 829 $form->addButtonHTML( 830 'tagging[actions][rename][' . $taginfo['tid'] . ']', 831 inlineSVG(__DIR__ . '/images/edit.svg')) 832 ->addClass('plugin_tagging action_button') 833 ->attr('data-action', 'rename') 834 ->attr('data-tid', $taginfo['tid']); 835 $form->addButtonHTML( 836 'tagging[actions][delete][' . $taginfo['tid'] . ']', 837 inlineSVG(__DIR__ . '/images/delete.svg')) 838 ->addClass('plugin_tagging action_button') 839 ->attr('data-action', 'delete') 840 ->attr('data-tid', $taginfo['tid']); 841 } 842 843 $form->addHTML('</td>'); 844 $form->addTagClose('tr'); 845 } 846 847 $form->addTagClose('table'); 848 return '<div class="table">' . $form->toHTML() . '</div>'; 849 } 850 851 /** 852 * Returns all tagging parameters from the query string 853 * 854 * @return mixed 855 */ 856 public function getParams() 857 { 858 global $INPUT; 859 return $INPUT->param('tagging', []); 860 } 861 862 /** 863 * Get a tagging parameter, empty string if not set 864 * 865 * @param string $name 866 * @return mixed 867 */ 868 public function getParam($name) 869 { 870 $params = $this->getParams(); 871 if ($params) { 872 return $params[$name] ?: ''; 873 } 874 } 875 876 /** 877 * Sets a tagging parameter 878 * 879 * @param string $name 880 * @param string|array $value 881 */ 882 public function setParam($name, $value) 883 { 884 global $INPUT; 885 $params = $this->getParams(); 886 $params = array_merge($params, [$name => $value]); 887 $INPUT->set('tagging', $params); 888 } 889 890 /** 891 * Default sorting by tag id 892 */ 893 public function setDefaultSort() 894 { 895 if (!$this->getParam('sort')) { 896 $this->setParam('sort', 'tid'); 897 } 898 } 899 900 /** 901 * Executes the query and returns the results as array 902 * 903 * @param array $query 904 * @return array 905 */ 906 protected function queryDb($query) 907 { 908 $db = $this->getDB(); 909 if (!$db) { 910 return []; 911 } 912 913 $res = $db->query($query[0], $query[1]); 914 $res = $db->res2arr($res); 915 916 $ret = []; 917 foreach ($res as $row) { 918 $ret[$row['item']] = $row['cnt']; 919 } 920 return $ret; 921 } 922 923 /** 924 * Construct the HAVING part of the search query 925 * 926 * @param array $filters 927 * @return array 928 */ 929 protected function getFilterSql($filters) 930 { 931 $having = ''; 932 $parts = []; 933 $params = []; 934 $filters = array_filter($filters); 935 if (!empty($filters)) { 936 $having = ' HAVING '; 937 foreach ($filters as $filter => $value) { 938 $parts[] = " $filter LIKE ? "; 939 $params[] = "%$value%"; 940 } 941 $having .= implode(' AND ', $parts); 942 } 943 return [$having, $params]; 944 } 945} 946