IconHandler::getImageSource()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace XoopsModules\Newbb;
4
5
/**
6
 * NewBB 5.0x,  the forum module for XOOPS project
7
 *
8
 * @copyright      XOOPS Project (https://xoops.org)
9
 * @license        GNU GPL 2 or later (https://www.gnu.org/licenses/gpl-2.0.html)
10
 * @author         Taiwen Jiang (phppp or D.J.) <[email protected]>
11
 * @since          4.00
12
 * @package        module::newbb
13
 */
14
15
16
17
/**
18
 * Set forum image
19
 *
20
 * Priority for path per types:
21
 *    NEWBB_ROOT    -    IF EXISTS XOOPS_THEME/modules/newbb/images/, TAKE IT;
22
 *                    ELSEIF EXISTS  XOOPS_THEME_DEFAULT/modules/newbb/assets/images/, TAKE IT;
23
 *                    ELSE TAKE  XOOPS_ROOT/modules/newbb/templates/images/.
24
 *    types:
25
 *        button/misc    -    language specified;
26
 *        //indicator    -    language specified;
27
 *        icon        -    universal;
28
 *        mime        -    universal;
29
 */
30
31
/**
32
 * Icon Renderer
33
 *
34
 * @author    D.J. (phppp)
35
 * @copyright copyright &copy; Xoops Project
36
 * @package   module::newbb
37
 */
38
class IconHandler
39
{
40
    /**
41
     * reference to XOOPS template
42
     */
43
    public $template;
44
45
    /**
46
     * image set
47
     */
48
    private $forumImage = [];
49
50
    /**
51
     * prefix
52
     */
53
    private $prefix = '';
54
55
    /**
56
     * postfix, including extension
57
     */
58
    private $postfix = '.png';
59
60
    /**
61
     * images to be assigned to template
62
     */
63
    private $images = [];
64
65
    /**
66
     * Constructor
67
     */
68
    public function __construct()
69
    {
70
    }
71
72
    /**
73
     * Access the only instance of this class
74
     * @return IconHandler
75
     */
76
    public static function getInstance()
77
    {
78
        static $instance;
79
        if (null === $instance) {
80
            $instance = new static();
81
        }
82
83
        return $instance;
84
    }
85
86
    /**
87
     * TODO: get compatible with new theme engine
88
     * @param         $type
89
     * @param string  $dirname
90
     * @param string  $default
91
     * @param string  $endDir
92
     * @return mixed
93
     */
94
    // START irmtfan - improve to get other "end dirnames" like "css" and "js" - change images with $endDir
95
    public function getPath($type, $dirname = 'newbb', $default = '', $endDir = 'images')
96
    {
97
        static $paths;
98
        if (isset($paths[$endDir . '/' . $type])) {
99
            return $paths[$endDir . '/' . $type];
100
        }
101
102
        $theme_path = $this->template->currentTheme->path;
103
        $rel_dir    = "modules/{$dirname}/{$endDir}";
104
        // START irmtfan add default for all pathes
105
        if (empty($default)) {
106
            $path = \is_dir($theme_path . "/{$rel_dir}/{$type}/") ? $theme_path . "/{$rel_dir}/{$type}" : (\is_dir(XOOPS_THEME_PATH . "/default/{$rel_dir}/{$type}/") ? XOOPS_THEME_PATH . "/default/{$rel_dir}/{$type}" : $GLOBALS['xoops']->path("modules/{$dirname}/templates/{$endDir}/{$type}"));
107
        } else {
108
            $path = \is_dir($theme_path . "/{$rel_dir}/{$type}/") ? $theme_path . "/{$rel_dir}/{$type}" : (\is_dir($theme_path . "/{$rel_dir}/{$default}/") ? $theme_path . "/{$rel_dir}/{$default}" : (\is_dir(XOOPS_THEME_PATH . "/default/{$rel_dir}/{$type}/") ? XOOPS_THEME_PATH
109
                                                                                                                                                                                                                                                                     . "/default/{$rel_dir}/{$type}" : (\is_dir(
110
                XOOPS_THEME_PATH . "/default/{$rel_dir}/{$default}/"
111
            ) ? XOOPS_THEME_PATH . "/default/{$rel_dir}/{$default}" : (\is_dir($GLOBALS['xoops']->path("modules/{$dirname}/templates/{$endDir}/{$type}/")) ? $GLOBALS['xoops']->path("modules/{$dirname}/templates/{$endDir}/{$type}") : $GLOBALS['xoops']->path(
112
                "modules/{$dirname}/templates/{$endDir}/{$default}"
113
            )) // XOOPS_ROOT_PATH
114
            ) // XOOPS_THEME_PATH {$default}
115
            ) // XOOPS_THEME_PATH
116
            ); // $theme_path {$default}
117
        }
118
        // END irmtfan add default for all pathes
119
        $paths[$endDir . '/' . $type] = \str_replace(XOOPS_ROOT_PATH, '', \str_replace('\\', '/', $path));
120
121
        return $paths[$endDir . '/' . $type];
122
    }
123
124
    // END irmtfan - improve to get other "end dirnames" like "css" and "js" - change images with $endDir
125
126
    /**
127
     * @param string $language
128
     * @param string $dirname
129
     */
130
    public function init(/*$set = "default", */ $language = 'english',
131
        $dirname = 'newbb'
132
    ) {
133
        $this->forumImage = require_once $GLOBALS['xoops']->path("modules/{$dirname}/include/images.php");
134
135
        $this->forumImage['icon'] = XOOPS_URL . $this->getPath('icon', $dirname) . '/';
136
        $this->forumImage['language'] = XOOPS_URL . $this->getPath("language/{$language}", $dirname, 'language/english') . '/';
137
    }
138
139
    /**
140
     * @param        $image
141
     * @param string $alt
142
     * @param string $extra
143
     */
144
    public function setImage($image, $alt = '', $extra = '')
145
    {
146
        if (!isset($this->images[$image])) {
147
            $imageSource = $this->getImageSource($image);
148
            // irmtfan add id={$image}
149
            $this->images[$image] = "<img src=\"{$imageSource}\" alt=\"{$alt}\" title=\"{$alt}\" align=\"middle\" {$extra} id={$image} >";
150
        }
151
    }
152
153
    /**
154
     * TODO: How about image not exist?
155
     * @param $image
156
     * @return string
157
     */
158
    public function getImageSource($image)
159
    {
160
        return $this->forumImage[$this->forumImage[$image]] . $this->prefix . $image . $this->postfix;
161
    }
162
163
    /**
164
     * @param         $image
165
     * @param string  $alt
166
     * @param string  $extra
167
     * @return mixed
168
     */
169
    public function getImage($image, $alt = '', $extra = '')
170
    {
171
        $this->setImage($image, $alt, $extra);
172
173
        return $this->images[$image];
174
    }
175
176
    /**
177
     * @param         $image
178
     * @param string  $alt
179
     * @param string  $extra
180
     * @return string
181
     */
182
    public function assignImage($image, $alt = '', $extra = '')
183
    {
184
        $this->setImage($image, $alt, $extra);
185
        // START hacked by irmtfan - improve function to CSS3 buttons - add alt and title attributes - use span instead of button to support IE7&8
186
        $tag = 'span';
187
        if ("class='forum_icon'" === $extra && \in_array(mb_substr($image, 0, 2), ['t_', 'p_', 'up'])) {
188
            $extra = "class='forum_icon forum_button'";
189
        }
190
191
        return "<{$tag} alt=\"{$alt}\" title=\"{$alt}\" align=\"middle\" {$extra} id={$image}>$alt</{$tag}>";
192
        // END hacked by irmtfan - improve function to CSS3 buttons
193
    }
194
195
    /**
196
     * @param $images
197
     */
198
    public function assignImages($images)
199
    {
200
        foreach ($images as $myImage) {
201
            [$image, $alt, $extra] = $myImage;
202
            $this->assignImage($image, $alt, $extra);
203
        }
204
    }
205
206
    /**
207
     * @return int|void
208
     */
209
    public function render()
210
    {
211
        //$this->template->assign_by_ref("image", $this->images);
212
        $this->template->assign($this->images);
213
214
        return \count($this->images);
215
    }
216
}
217