Passed
Pull Request — master (#70)
by Pierre-Henry
03:15
created

IconHandler::getImage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 3
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php namespace XoopsModules\Newbb;
2
3
/**
4
 * NewBB 5.0x,  the forum module for XOOPS project
5
 *
6
 * @copyright      XOOPS Project (https://xoops.org)
7
 * @license        GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
8
 * @author         Taiwen Jiang (phppp or D.J.) <[email protected]>
9
 * @since          4.00
10
 * @package        module::newbb
11
 */
12
13
// defined('XOOPS_ROOT_PATH') || die('Restricted access');
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
14
15
/**
16
 * Set forum image
17
 *
18
 * Priority for path per types:
19
 *    NEWBB_ROOT    -    IF EXISTS XOOPS_THEME/modules/newbb/images/, TAKE IT;
20
 *                    ELSEIF EXISTS  XOOPS_THEME_DEFAULT/modules/newbb/assets/images/, TAKE IT;
21
 *                    ELSE TAKE  XOOPS_ROOT/modules/newbb/templates/images/.
22
 *    types:
23
 *        button/misc    -    language specified;
24
 *        //indicator    -    language specified;
25
 *        icon        -    universal;
26
 *        mime        -    universal;
27
 */
28
29
/**
30
 * Icon Renderer
31
 *
32
 * @author    D.J. (phppp)
33
 * @copyright copyright &copy; Xoops Project
34
 * @package   module::newbb
35
 *
36
 */
37
class IconHandler
38
{
39
    /**
40
     * reference to XOOPS template
41
     */
42
    public $template;
43
44
    /**
45
     * image set
46
     */
47
    private $forumImage = [];
48
49
    /**
50
     * prefix
51
     */
52
    private $prefix = '';
53
54
    /**
55
     * postfix, including extension
56
     */
57
    private $postfix = '.png';
58
59
    /**
60
     * images to be assigned to template
61
     */
62
    private $images = [];
63
64
    /**
65
     * Constructor
66
     */
67
    public function __construct()
68
    {
69
    }
70
71
    /**
72
     * Access the only instance of this class
73
     * @return IconHandler
74
     */
75
    public static function getInstance()
76
    {
77
        static $instance;
78
        if (null === $instance) {
79
            $instance = new static();
80
        }
81
82
        return $instance;
83
    }
84
85
    /**
86
     * TODO: get compatible with new theme engine
87
     * @param         $type
88
     * @param  string $dirname
89
     * @param  string $default
90
     * @param  string $endDir
91
     * @return mixed
92
     */
93
    // START irmtfan - improve to get other "end dirnames" like "css" and "js" - change images with $endDir
94
    public function getPath($type, $dirname = 'newbb', $default = '', $endDir = 'images')
95
    {
96
        static $paths;
97
        if (isset($paths[$endDir . '/' . $type])) {
98
            return $paths[$endDir . '/' . $type];
99
        }
100
101
        $theme_path = $this->template->currentTheme->path;
102
        $rel_dir    = "modules/{$dirname}/{$endDir}";
103
        // START irmtfan add default for all pathes
104
        if (empty($default)) {
105
            $path = is_dir($theme_path . "/{$rel_dir}/{$type}/") ? $theme_path . "/{$rel_dir}/{$type}"
106
                : (is_dir(XOOPS_THEME_PATH . "/default/{$rel_dir}/{$type}/") ? XOOPS_THEME_PATH . "/default/{$rel_dir}/{$type}"
0 ignored issues
show
Bug introduced by
The constant XoopsModules\Newbb\XOOPS_THEME_PATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
107
                : $GLOBALS['xoops']->path("modules/{$dirname}/templates/{$endDir}/{$type}"));
108
        } else {
109
            $path = is_dir($theme_path . "/{$rel_dir}/{$type}/") ? $theme_path . "/{$rel_dir}/{$type}" : (
110
                is_dir($theme_path . "/{$rel_dir}/{$default}/") ? $theme_path . "/{$rel_dir}/{$default}" : (
111
                is_dir(XOOPS_THEME_PATH . "/default/{$rel_dir}/{$type}/") ? XOOPS_THEME_PATH
112
                 . "/default/{$rel_dir}/{$type}" : (
113
                     is_dir(XOOPS_THEME_PATH . "/default/{$rel_dir}/{$default}/") ? XOOPS_THEME_PATH . "/default/{$rel_dir}/{$default}"
114
                    : (is_dir($GLOBALS['xoops']->path("modules/{$dirname}/templates/{$endDir}/{$type}/")) ? $GLOBALS['xoops']->path("modules/{$dirname}/templates/{$endDir}/{$type}")
115
                    : $GLOBALS['xoops']->path("modules/{$dirname}/templates/{$endDir}/{$default}")) // XOOPS_ROOT_PATH
116
            ) // XOOPS_THEME_PATH {$default}
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
117
            ) // XOOPS_THEME_PATH
118
            ); // $theme_path {$default}
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
119
        }
120
        // END irmtfan add default for all pathes
121
        $paths[$endDir . '/' . $type] = str_replace(XOOPS_ROOT_PATH, '', str_replace('\\', '/', $path));
0 ignored issues
show
Bug introduced by
The constant XoopsModules\Newbb\XOOPS_ROOT_PATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
122
123
        return $paths[$endDir . '/' . $type];
124
    }
125
126
    // END irmtfan - improve to get other "end dirnames" like "css" and "js" - change images with $endDir
127
128
    /**
129
     * @param string $language
130
     * @param string $dirname
131
     */
132
    public function init(/*$set = "default", */
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
133
        $language = 'english',
134
        $dirname = 'newbb'
135
    ) {
136
        $this->forumImage = include $GLOBALS['xoops']->path("modules/{$dirname}/include/images.php");
137
138
        $this->forumImage['icon']     = XOOPS_URL . $this->getPath('icon', $dirname) . '/';
0 ignored issues
show
Bug introduced by
The constant XoopsModules\Newbb\XOOPS_URL was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
139
        $this->forumImage['language'] = XOOPS_URL . $this->getPath("language/{$language}", $dirname, 'language/english') . '/';
140
    }
141
142
    /**
143
     * @param        $image
144
     * @param string $alt
145
     * @param string $extra
146
     */
147
    public function setImage($image, $alt = '', $extra = '')
148
    {
149
        if (!isset($this->images[$image])) {
150
            $imageSource = $this->getImageSource($image);
151
            // irmtfan add id={$image}
152
            $this->images[$image] = "<img src=\"{$imageSource}\" alt=\"{$alt}\" title=\"{$alt}\" align=\"middle\" {$extra} id={$image} />";
153
        }
154
    }
155
156
    /**
157
     * TODO: How about image not exist?
158
     * @param $image
159
     * @return string
160
     */
161
    public function getImageSource($image)
162
    {
163
        return $this->forumImage[$this->forumImage[$image]] . $this->prefix . $image . $this->postfix;
164
    }
165
166
    /**
167
     * @param         $image
168
     * @param  string $alt
169
     * @param  string $extra
170
     * @return mixed
171
     */
172
    public function getImage($image, $alt = '', $extra = '')
173
    {
174
        $this->setImage($image, $alt, $extra);
175
176
        return $this->images[$image];
177
    }
178
179
    /**
180
     * @param         $image
181
     * @param  string $alt
182
     * @param  string $extra
183
     * @return string
184
     */
185
    public function assignImage($image, $alt = '', $extra = '')
186
    {
187
        $this->setImage($image, $alt, $extra);
188
        // START hacked by irmtfan - improve function to CSS3 buttons - add alt and title attributes - use span instead of button to support IE7&8
189
        $tag = 'span';
190
        if ("class='forum_icon'" === $extra && in_array(substr($image, 0, 2), ['t_', 'p_', 'up'])) {
191
            $extra = "class='forum_icon forum_button'";
192
        }
193
194
        return "<{$tag} alt=\"{$alt}\" title=\"{$alt}\" align=\"middle\" {$extra} id={$image}>$alt</{$tag}>";
195
        // END hacked by irmtfan - improve function to CSS3 buttons
196
    }
197
198
    /**
199
     * @param $images
200
     */
201
    public function assignImages($images)
202
    {
203
        foreach ($images as $myImage) {
204
            list($image, $alt, $extra) = $myImage;
205
            $this->assignImage($image, $alt, $extra);
206
        }
207
    }
208
209
    /**
210
     * @return int|void
211
     */
212
    public function render()
213
    {
214
        //$this->template->assign_by_ref("image", $this->images);
0 ignored issues
show
Unused Code Comprehensibility introduced by
72% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
215
        $this->template->assign($this->images);
216
217
        return count($this->images);
218
    }
219
}
220