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 $ret = '<div class="search_quickresult">'; 272 $ret .= '<ul class="search_quickhits">'; 273 274 if (count($pids) === 0) { 275 // Produce valid XHTML (ul needs a child) 276 $ret .= '<li><div class="li">' . $this->lang['js']['nopages'] . '</div></li>'; 277 } else { 278 foreach (array_keys($pids) as $val) { 279 $ret .= '<li><div class="li">'; 280 $ret .= html_wikilink(":$val"); 281 $ret .= '</div></li>'; 282 } 283 } 284 285 $ret .= '</ul>'; 286 $ret .= '</div>'; 287 $ret .= '<div class="clearer"></div>'; 288 289 return $ret; 290 } 291 292 /** 293 * Get the link to a search for the given tag 294 * 295 * @param string $tag search for this tag 296 * @param string $ns limit search to this namespace 297 * 298 * @return string 299 */ 300 protected function linkToSearch($tag, $ns = '') { 301 return '<a href="' . hsc($this->getTagSearchURL($tag, $ns)) . '">' . $tag . '</a>'; 302 } 303 304 /** 305 * Display the Tags for the current page and prepare the tag editing form 306 * 307 * @param bool $print Should the HTML be printed or returned? 308 * 309 * @return string 310 */ 311 public function tpl_tags($print = true) { 312 global $INFO; 313 global $lang; 314 315 $filter = array('pid' => $INFO['id']); 316 if ($this->getConf('singleusermode')) { 317 $filter['tagger'] = 'auto'; 318 } 319 320 $tags = $this->findItems($filter, 'tag'); 321 322 $ret = ''; 323 324 $ret .= '<div class="plugin_tagging_edit">'; 325 $ret .= $this->html_cloud($tags, 'tag', array($this, 'linkToSearch'), true, true); 326 327 if ($this->getUser() && $INFO['writable']) { 328 $lang['btn_tagging_edit'] = $lang['btn_secedit']; 329 $ret .= '<div id="tagging__edit_buttons_group">'; 330 $ret .= html_btn('tagging_edit', $INFO['id'], '', array()); 331 if (auth_isadmin()) { 332 $ret .= '<label>' 333 . $this->getLang('toggle admin mode') 334 . '<input type="checkbox" id="tagging__edit_toggle_admin" /></label>'; 335 } 336 $ret .= '</div>'; 337 $form = new dokuwiki\Form\Form(); 338 $form->id('tagging__edit'); 339 $form->setHiddenField('tagging[id]', $INFO['id']); 340 $form->setHiddenField('call', 'plugin_tagging_save'); 341 $tags = $this->findItems(array( 342 'pid' => $INFO['id'], 343 'tagger' => $this->getUser(), 344 ), 'tag'); 345 $form->addTextarea('tagging[tags]') 346 ->val(implode(', ', array_keys($tags))) 347 ->addClass('edit') 348 ->attr('rows', 4); 349 $form->addButton('', $lang['btn_save'])->id('tagging__edit_save'); 350 $form->addButton('', $lang['btn_cancel'])->id('tagging__edit_cancel'); 351 $ret .= $form->toHTML(); 352 } 353 $ret .= '</div>'; 354 355 if ($print) { 356 echo $ret; 357 } 358 359 return $ret; 360 } 361 362 /** 363 * @param string $namespace empty for entire wiki 364 * 365 * @param string $order_by 366 * @param bool $desc 367 * @param array $filters 368 * @return array 369 */ 370 public function getAllTags($namespace = '', $order_by = 'tid', $desc = false, $filters = []) { 371 $order_fields = array('pid', 'tid', 'taggers', 'ns', 'count'); 372 if (!in_array($order_by, $order_fields)) { 373 msg('cannot sort by ' . $order_by . ' field does not exists', -1); 374 $order_by = 'tag'; 375 } 376 377 list($having, $params) = $this->getFilterSql($filters); 378 379 $db = $this->getDB(); 380 381 $query = 'SELECT "pid", 382 CLEANTAG("tag") AS "tid", 383 GROUP_SORT(GROUP_CONCAT("tagger"), \', \') AS "taggers", 384 GROUP_SORT(GROUP_CONCAT(GET_NS("pid")), \', \') AS "ns", 385 GROUP_SORT(GROUP_CONCAT("pid"), \', \') AS "pids", 386 COUNT(*) AS "count" 387 FROM "taggings" 388 WHERE "pid" GLOB ? AND GETACCESSLEVEL(pid) >= ' . AUTH_READ 389 . ' GROUP BY "tid"'; 390 $query .= $having; 391 $query .= 'ORDER BY ' . $order_by; 392 if ($desc) { 393 $query .= ' DESC'; 394 } 395 396 array_unshift($params, $this->globNamespace($namespace)); 397 $res = $db->query($query, $params); 398 399 return $db->res2arr($res); 400 } 401 402 /** 403 * Get all pages with tags and their tags 404 * 405 * @return array ['pid' => ['tag1','tag2','tag3']] 406 */ 407 public function getAllTagsByPage() { 408 $query = ' 409 SELECT pid, GROUP_CONCAT(tag) AS tags 410 FROM taggings 411 GROUP BY pid 412 '; 413 $db = $this->getDb(); 414 $res = $db->query($query); 415 return array_map( 416 function ($i) { 417 return explode(',', $i); 418 }, 419 array_column($db->res2arr($res), 'tags', 'pid') 420 ); 421 } 422 423 /** 424 * Renames a tag 425 * 426 * @param string $formerTagName 427 * @param string $newTagNames 428 */ 429 public function renameTag($formerTagName, $newTagNames) { 430 431 if (empty($formerTagName) || empty($newTagNames)) { 432 msg($this->getLang("admin enter tag names"), -1); 433 return; 434 } 435 436 $keepFormerTag = false; 437 438 // enable splitting tags on rename 439 $newTagNames = array_map(function ($tag) { 440 return $this->cleanTag($tag); 441 }, explode(',', $newTagNames)); 442 443 $db = $this->getDB(); 444 445 // non-admins can rename only their own tags 446 if (!auth_isadmin()) { 447 $queryTagger =' AND tagger = ?'; 448 $tagger = $this->getUser(); 449 } else { 450 $queryTagger = ''; 451 $tagger = ''; 452 } 453 454 $insertQuery = 'INSERT INTO taggings '; 455 $insertQuery .= 'SELECT pid, ?, tagger, lang FROM taggings'; 456 $where = ' WHERE CLEANTAG(tag) = ?'; 457 $where .= ' AND GETACCESSLEVEL(pid) >= ' . AUTH_EDIT; 458 $where .= $queryTagger; 459 460 $db->query('BEGIN TRANSACTION'); 461 462 // insert new tags first 463 foreach ($newTagNames as $newTag) { 464 if ($newTag === $this->cleanTag($formerTagName)) { 465 $keepFormerTag = true; 466 continue; 467 } 468 $params = [$newTag, $this->cleanTag($formerTagName)]; 469 if ($tagger) array_push($params, $tagger); 470 $res = $db->query($insertQuery . $where, $params); 471 if ($res === false) { 472 $db->query('ROLLBACK TRANSACTION'); 473 return; 474 } 475 $db->res_close($res); 476 } 477 478 // finally delete the renamed tags 479 if (!$keepFormerTag) { 480 $deleteQuery = 'DELETE FROM taggings'; 481 $params = [$this->cleanTag($formerTagName)]; 482 if ($tagger) array_push($params, $tagger); 483 if ($db->query($deleteQuery . $where, $params) === false) { 484 $db->query('ROLLBACK TRANSACTION'); 485 return; 486 } 487 } 488 489 $db->query('COMMIT TRANSACTION'); 490 491 msg($this->getLang("admin renamed"), 1); 492 493 return; 494 } 495 496 /** 497 * Rename or delete a tag for all users 498 * 499 * @param string $pid 500 * @param string $formerTagName 501 * @param string $newTagName 502 * 503 * @return array 504 */ 505 public function modifyPageTag($pid, $formerTagName, $newTagName) { 506 507 $db = $this->getDb(); 508 509 $res = $db->query( 510 'SELECT pid FROM taggings WHERE CLEANTAG(tag) = ? AND pid = ?', 511 $this->cleanTag($formerTagName), 512 $pid 513 ); 514 $check = $db->res2arr($res); 515 516 if (empty($check)) { 517 return array(true, $this->getLang('admin tag does not exists')); 518 } 519 520 if (empty($newTagName)) { 521 $res = $db->query( 522 'DELETE FROM taggings WHERE pid = ? AND CLEANTAG(tag) = ?', 523 $pid, 524 $this->cleanTag($formerTagName) 525 ); 526 } else { 527 $res = $db->query( 528 'UPDATE taggings SET tag = ? WHERE pid = ? AND CLEANTAG(tag) = ?', 529 $newTagName, 530 $pid, 531 $this->cleanTag($formerTagName) 532 ); 533 } 534 $db->res2arr($res); 535 536 return array(false, $this->getLang('admin renamed')); 537 } 538 539 /** 540 * Deletes a tag 541 * 542 * @param array $tags 543 * @param string $namespace current namespace context as in getAllTags() 544 */ 545 public function deleteTags($tags, $namespace = '') { 546 if (empty($tags)) { 547 return; 548 } 549 550 $namespace = cleanId($namespace); 551 552 $db = $this->getDB(); 553 554 $queryBody = 'FROM taggings WHERE pid GLOB ? AND (' . 555 implode(' OR ', array_fill(0, count($tags), 'CLEANTAG(tag) = ?')) . ')'; 556 $args = array_map(array($this, 'cleanTag'), $tags); 557 array_unshift($args, $this->globNamespace($namespace)); 558 559 // non-admins can delete only their own tags 560 if (!auth_isadmin()) { 561 $queryBody .= ' AND tagger = ?'; 562 array_push($args, $this->getUser()); 563 } 564 565 $affectedPagesQuery= 'SELECT DISTINCT pid ' . $queryBody; 566 $resAffectedPages = $db->query($affectedPagesQuery, $args); 567 $numAffectedPages = count($resAffectedPages->fetchAll()); 568 569 $deleteQuery = 'DELETE ' . $queryBody; 570 $db->query($deleteQuery, $args); 571 572 msg(sprintf($this->getLang("admin deleted"), count($tags), $numAffectedPages), 1); 573 } 574 575 /** 576 * Updates tags with a new page name 577 * 578 * @param string $oldName 579 * @param string $newName 580 */ 581 public function renamePage($oldName, $newName) { 582 $db = $this->getDB(); 583 $db->query('UPDATE taggings SET pid = ? WHERE pid = ?', $newName, $oldName); 584 } 585 586 /** 587 * Extracts tags from search query 588 * 589 * @param array $parsedQuery 590 * @return array 591 */ 592 public function getTags($parsedQuery) 593 { 594 $tags = []; 595 if (isset($parsedQuery['phrases'][0])) { 596 $tags = $parsedQuery['phrases']; 597 } elseif (isset($parsedQuery['and'][0])) { 598 $tags = $parsedQuery['and']; 599 } elseif (isset($parsedQuery['tag'])) { 600 // handle autocomplete call 601 $tags[] = $parsedQuery['tag']; 602 } 603 return $tags; 604 } 605 606 /** 607 * Search for tagged pages 608 * 609 * @return array 610 */ 611 public function searchPages() 612 { 613 global $INPUT; 614 global $QUERY; 615 $parsedQuery = ft_queryParser(new Doku_Indexer(), $QUERY); 616 617 /** @var helper_plugin_tagging_querybuilder $queryBuilder */ 618 $queryBuilder = new \helper_plugin_tagging_querybuilder(); 619 620 $queryBuilder->setField('pid'); 621 $queryBuilder->setTags($this->getTags($parsedQuery)); 622 $queryBuilder->setLogicalAnd($INPUT->str('taggings') === 'and'); 623 if (isset($parsedQuery['ns'])) $queryBuilder->includeNS($parsedQuery['ns']); 624 if (isset($parsedQuery['notns'])) $queryBuilder->excludeNS($parsedQuery['notns']); 625 if (isset($parsedQuery['tagger'])) $queryBuilder->setTagger($parsedQuery['tagger']); 626 if (isset($parsedQuery['pid'])) $queryBuilder->setPid($parsedQuery['pid']); 627 628 return $this->queryDb($queryBuilder->getPages()); 629 } 630 631 /** 632 * Syntax to allow users to manage tags on regular pages, respects ACLs 633 * @param string $ns 634 * @return string 635 */ 636 public function manageTags($ns) 637 { 638 global $INPUT; 639 640 $this->setDefaultSort(); 641 642 // initially set namespace filter to what is defined in syntax 643 if ($ns && !$INPUT->has('tagging__filters')) { 644 $INPUT->set('tagging__filters', ['ns' => $ns]); 645 } 646 647 return $this->html_table(); 648 } 649 650 /** 651 * HTML list of tagged pages 652 * 653 * @param string $tid 654 * @return string 655 */ 656 public function getPagesHtml($tid) 657 { 658 $html = ''; 659 660 $db = $this->getDB(); 661 $sql = 'SELECT pid from taggings where CLEANTAG(tag) = CLEANTAG(?)'; 662 $res = $db->query($sql, $tid); 663 $pages = $db->res2arr($res); 664 665 if ($pages) { 666 $html .= '<ul>'; 667 foreach ($pages as $page) { 668 $pid = $page['pid']; 669 $html .= '<li><a href="' . wl($pid) . '" target="_blank">' . $pid . '</li>'; 670 } 671 $html .= '</ul>'; 672 } 673 674 return $html; 675 } 676 677 /** 678 * Display tag management table 679 */ 680 public function html_table() { 681 global $ID, $INPUT; 682 683 $headers = array( 684 array('value' => $this->getLang('admin tag'), 'sort_by' => 'tid'), 685 array('value' => $this->getLang('admin occurrence'), 'sort_by' => 'count') 686 ); 687 688 if (!$this->conf['hidens']) { 689 array_push( 690 $headers, 691 ['value' => $this->getLang('admin namespaces'), 'sort_by' => 'ns'] 692 ); 693 } 694 695 array_push($headers, 696 array('value' => $this->getLang('admin taggers'), 'sort_by' => 'taggers'), 697 array('value' => $this->getLang('admin actions'), 'sort_by' => false) 698 ); 699 700 $sort = explode(',', $this->getParam('sort')); 701 $order_by = $sort[0]; 702 $desc = false; 703 if (isset($sort[1]) && $sort[1] === 'desc') { 704 $desc = true; 705 } 706 $filters = $INPUT->arr('tagging__filters'); 707 708 $tags = $this->getAllTags($INPUT->str('filter'), $order_by, $desc, $filters); 709 710 $form = new \dokuwiki\Form\Form(); 711 // required in admin mode 712 $form->setHiddenField('page', 'tagging'); 713 $form->setHiddenField('id', $ID); 714 $form->setHiddenField('[tagging]sort', $this->getParam('sort')); 715 716 /** 717 * Actions dialog 718 */ 719 $form->addTagOpen('div')->id('tagging__action-dialog')->attr('style', "display:none;"); 720 $form->addTagClose('div'); 721 722 /** 723 * Tag pages dialog 724 */ 725 $form->addTagOpen('div')->id('tagging__taggedpages-dialog')->attr('style', "display:none;"); 726 $form->addTagClose('div'); 727 728 /** 729 * Tag management table 730 */ 731 $form->addTagOpen('table')->addClass('inline plugin_tagging'); 732 733 $nscol = $this->conf['hidens'] ? '' : '<col class="wide-col"></col>'; 734 $form->addHTML( 735 '<colgroup> 736 <col></col> 737 <col class="narrow-col"></col>' 738 . $nscol . 739 '<col></col> 740 <col class="narrow-col"></col> 741 </colgroup>' 742 ); 743 744 /** 745 * Table headers 746 */ 747 $form->addTagOpen('tr'); 748 foreach ($headers as $header) { 749 $form->addTagOpen('th'); 750 if ($header['sort_by'] !== false) { 751 $param = $header['sort_by']; 752 $icon = 'arrow-both'; 753 $title = $this->getLang('admin sort ascending'); 754 if ($header['sort_by'] === $order_by) { 755 if ($desc === false) { 756 $icon = 'arrow-up'; 757 $title = $this->getLang('admin sort descending'); 758 $param .= ',desc'; 759 } else { 760 $icon = 'arrow-down'; 761 } 762 } 763 $form->addButtonHTML( 764 "tagging[sort]", 765 $header['value'] . ' ' . inlineSVG(__DIR__ . "/images/$icon.svg")) 766 ->addClass('plugin_tagging sort_button') 767 ->attr('title', $title) 768 ->val($param); 769 } else { 770 $form->addHTML($header['value']); 771 } 772 $form->addTagClose('th'); 773 } 774 $form->addTagClose('tr'); 775 776 /** 777 * Table filters for all sortable columns 778 */ 779 $form->addTagOpen('tr'); 780 foreach ($headers as $header) { 781 $form->addTagOpen('th'); 782 if ($header['sort_by'] !== false) { 783 $field = $header['sort_by']; 784 $input = $form->addTextInput("tagging__filters[$field]"); 785 $input->addClass('full-col'); 786 } 787 $form->addTagClose('th'); 788 } 789 $form->addTagClose('tr'); 790 791 792 foreach ($tags as $taginfo) { 793 $tagname = $taginfo['tid']; 794 $taggers = $taginfo['taggers']; 795 $ns = $taginfo['ns']; 796 $pids = explode(',',$taginfo['pids']); 797 798 $form->addTagOpen('tr'); 799 $form->addHTML('<td>'); 800 $form->addHTML('<a class="tagslist" href="#" data-tid="' . $taginfo['tid'] . '">'); 801 $form->addHTML( hsc($tagname) . '</a>'); 802 $form->addHTML('</td>'); 803 $form->addHTML('<td>' . $taginfo['count'] . '</td>'); 804 if (!$this->conf['hidens']) { 805 $form->addHTML('<td>' . hsc($ns) . '</td>'); 806 } 807 $form->addHTML('<td>' . hsc($taggers) . '</td>'); 808 809 /** 810 * action buttons 811 */ 812 $form->addHTML('<td>'); 813 814 // check ACLs 815 $userEdit = false; 816 /** @var \helper_plugin_sqlite $sqliteHelper */ 817 $sqliteHelper = plugin_load('helper', 'sqlite'); 818 foreach ($pids as $pid) { 819 if ($sqliteHelper->_getAccessLevel($pid) >= AUTH_EDIT) { 820 $userEdit = true; 821 continue; 822 } 823 } 824 825 if ($userEdit) { 826 $form->addButtonHTML( 827 'tagging[actions][rename][' . $taginfo['tid'] . ']', 828 inlineSVG(__DIR__ . '/images/edit.svg')) 829 ->addClass('plugin_tagging action_button') 830 ->attr('data-action', 'rename') 831 ->attr('data-tid', $taginfo['tid']); 832 $form->addButtonHTML( 833 'tagging[actions][delete][' . $taginfo['tid'] . ']', 834 inlineSVG(__DIR__ . '/images/delete.svg')) 835 ->addClass('plugin_tagging action_button') 836 ->attr('data-action', 'delete') 837 ->attr('data-tid', $taginfo['tid']); 838 } 839 840 $form->addHTML('</td>'); 841 $form->addTagClose('tr'); 842 } 843 844 $form->addTagClose('table'); 845 return '<div class="table">' . $form->toHTML() . '</div>'; 846 } 847 848 /** 849 * Returns all tagging parameters from the query string 850 * 851 * @return mixed 852 */ 853 public function getParams() 854 { 855 global $INPUT; 856 return $INPUT->param('tagging', []); 857 } 858 859 /** 860 * Get a tagging parameter, empty string if not set 861 * 862 * @param string $name 863 * @return mixed 864 */ 865 public function getParam($name) 866 { 867 $params = $this->getParams(); 868 if ($params) { 869 return $params[$name] ?: ''; 870 } 871 } 872 873 /** 874 * Sets a tagging parameter 875 * 876 * @param string $name 877 * @param string|array $value 878 */ 879 public function setParam($name, $value) 880 { 881 global $INPUT; 882 $params = $this->getParams(); 883 $params = array_merge($params, [$name => $value]); 884 $INPUT->set('tagging', $params); 885 } 886 887 /** 888 * Default sorting by tag id 889 */ 890 public function setDefaultSort() 891 { 892 if (!$this->getParam('sort')) { 893 $this->setParam('sort', 'tid'); 894 } 895 } 896 897 /** 898 * Executes the query and returns the results as array 899 * 900 * @param array $query 901 * @return array 902 */ 903 protected function queryDb($query) 904 { 905 $db = $this->getDB(); 906 if (!$db) { 907 return []; 908 } 909 910 $res = $db->query($query[0], $query[1]); 911 $res = $db->res2arr($res); 912 913 $ret = []; 914 foreach ($res as $row) { 915 $ret[$row['item']] = $row['cnt']; 916 } 917 return $ret; 918 } 919 920 /** 921 * Construct the HAVING part of the search query 922 * 923 * @param array $filters 924 * @return array 925 */ 926 protected function getFilterSql($filters) 927 { 928 $having = ''; 929 $parts = []; 930 $params = []; 931 $filters = array_filter($filters); 932 if (!empty($filters)) { 933 $having = ' HAVING '; 934 foreach ($filters as $filter => $value) { 935 $parts[] = " $filter LIKE ? "; 936 $params[] = "%$value%"; 937 } 938 $having .= implode(' AND ', $parts); 939 } 940 return [$having, $params]; 941 } 942} 943