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