Passed
Pull Request — master (#29)
by Michael
05:21 queued 02:44
created

Tagbar   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 30
c 2
b 0
f 0
dl 0
loc 64
rs 10
wmc 11

1 Method

Rating   Name   Duplication   Size   Complexity  
B getTagbar() 0 54 11
1
<?php declare(strict_types=1);
2
3
namespace XoopsModules\Tag;
4
5
/*
6
 You may not change or alter any portion of this comment or credits
7
 of supporting developers from this source code or any supporting source code
8
 which is considered copyrighted (c) material of the original comment or credit authors.
9
10
 This program is distributed in the hope that it will be useful,
11
 but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
*/
14
15
/**
16
 * XOOPS tag management module
17
 *
18
 * @copyright      {@link https://sourceforge.net/projects/xoops/ The XOOPS Project}
19
 * @license        {@link https://www.fsf.org/copyleft/gpl.html GNU public license}
20
 * @author         Taiwen Jiang <[email protected]>
21
 * @since          1.00
22
 */
23
(\defined('XOOPS_ROOT_PATH') && ($GLOBALS['xoopsModule'] instanceof \XoopsModule)) || exit('Restricted access');
24
25
class Tagbar
26
{
27
    /**
28
     * Display tag list
29
     *
30
     * @param int|array $tags  array of tag string
31
     *                         OR
32
     * @return array
33
     *                         {@internal param int $itemid }}
34
     */
35
    public function getTagbar($tags, int $catid = 0, int $modid = 0): array
36
    {
37
        static $loaded, $delimiter;
38
39
        if (empty($tags)) {
40
            return [];
41
        }
42
43
        $helper = Helper::getInstance();
44
45
        if (null === $loaded) {
46
            require_once $helper->path('include/vars.php');
47
            //require_once $helper->path('include/functions.php');
48
            Utility::tag_define_url_delimiter();
49
            $helper->loadLanguage('main'); // load Main lang file
50
            /*
51
            if (!($GLOBALS['xoopsModule'] instanceof \XoopsModule)
52
                || ('tag' !== $GLOBALS['xoopsModule']->getVar('dirname'))) {
53
                $helper->loadLanguage('main');
54
            }
55
            */
56
            if (\file_exists($helper->path('assets/images/delimiter.gif'))) {
57
                $delimiter = "<img src='" . $helper->url('assets/images/delimiter.gif') . "' alt=''>";
58
            } else {
59
                $delimiter = "<img src='" . $GLOBALS['xoops']->url('www/images/pointer.gif') . "' alt=''>";
60
            }
61
            $loaded = 1;
62
        }
63
64
        // itemid
65
        if (\is_numeric($tags)) {
66
            if (empty($modid) && ($GLOBALS['xoopsModule'] instanceof \XoopsModule)) {
67
                $modid = $GLOBALS['xoopsModule']->getVar('mid');
68
            }
69
            /** @var \XoopsModules\Tag\TagHandler $tagHandler */
70
            $tagHandler = $helper->getHandler('Tag');
71
            if (!$tags = $tagHandler->getByItem($tags, $modid, $catid)) {
72
                return [];
73
            }
74
            // if ready, do nothing
75
        } elseif (\is_array($tags)) {
0 ignored issues
show
introduced by
The condition is_array($tags) is always true.
Loading history...
76
            // parse
77
        } elseif (!$tags = Utility::tag_parse_tag($tags)) {
78
            return [];
79
        }
80
        $tags_data = [];
81
        foreach ($tags as $tag) {
82
            $tags_data[] = "<a href='" . $helper->url('view.tag.php' . URL_DELIMITER . \urlencode($tag)) . "' title='" . \htmlspecialchars($tag, \ENT_QUOTES | \ENT_HTML5) . "'>" . \htmlspecialchars($tag, \ENT_QUOTES | \ENT_HTML5) . '</a>';
83
        }
84
85
        return [
86
            'title'     => \_MD_TAG_TAGS,
87
            'delimiter' => $delimiter,
88
            'tags'      => $tags_data,
89
        ];
90
    }
91
}
92