Passed
Push — master ( 38629d...b08bf7 )
by
unknown
34s queued 12s
created

Utility   A

Complexity

Total Complexity 35

Size/Duplication

Total Lines 216
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 35
eloc 105
dl 0
loc 216
rs 9.6
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getPathTree() 0 18 3
A getPopularImage() 0 19 4
A getItemIds() 0 17 4
B getStatusImage() 0 31 8
B getPathTreeUrl() 0 48 10
A getNumbersOfEntries() 0 20 6
1
<?php
2
3
namespace XoopsModules\Tdmdownloads;
4
5
/*
6
 Utility Class Definition
7
8
 You may not change or alter any portion of this comment or credits of
9
 supporting developers from this source code or any supporting source code
10
 which is considered copyrighted (c) material of the original comment or credit
11
 authors.
12
13
 This program is distributed in the hope that it will be useful, but
14
 WITHOUT ANY WARRANTY; without even the implied warranty of
15
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16
 */
17
18
/**
19
 * Class Utility
20
 */
21
class Utility
22
{
23
    use Common\VersionChecks; //checkVerXoops, checkVerPhp Traits
24
25
    use Common\ServerStats; // getServerStats Trait
26
27
    use Common\FilesManagement; // Files Management Trait
28
29
    //--------------- Custom module methods -----------------------------
30
31
    /**
32
     * @param $permtype
33
     * @param $dirname
34
     * @return mixed
35
     */
36
    public function getItemIds($permtype, $dirname)
37
    {
38
        global $xoopsUser;
39
        static $permissions = [];
40
        if (is_array($permissions) && array_key_exists($permtype, $permissions)) {
41
            return $permissions[$permtype];
42
        }
43
        /** @var \XoopsModuleHandler $moduleHandler */
44
        $moduleHandler = xoops_getHandler('module');
45
        $tdmModule     = $moduleHandler->getByDirname($dirname);
46
        $groups        = is_object($xoopsUser) ? $xoopsUser->getGroups() : XOOPS_GROUP_ANONYMOUS;
47
48
        /** @var \XoopsGroupPermHandler $grouppermHandler */
49
        $grouppermHandler = xoops_getHandler('groupperm');
50
        $categories       = $grouppermHandler->getItemIds($permtype, $groups, $tdmModule->getVar('mid'));
51
52
        return $categories;
53
    }
54
55
    /**
56
     * retourne le nombre de téléchargements dans le catégories enfants d'une catégorie
57
     * @param \XoopsModules\Tdmdownloads\Tree        $mytree
58
     * @param                                        $categories
59
     * @param                                        $entries
60
     * @param                                        $cid
61
     * @return int
62
     */
63
    public function getNumbersOfEntries($mytree, $categories, $entries, $cid)
64
    {
65
        $count     = 0;
66
        $child_arr = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $child_arr is dead and can be removed.
Loading history...
67
        if (in_array($cid, $categories, true)) {
68
            $child = $mytree->getAllChild($cid);
69
            foreach (array_keys($entries) as $i) {
70
                /** @var \XoopsModules\Tdmdownloads\Downloads[] $entries */
71
                if ($entries[$i]->getVar('cid') == $cid) {
72
                    $count++;
73
                }
74
                foreach (array_keys($child) as $j) {
75
                    if ($entries[$i]->getVar('cid') == $j) {
76
                        $count++;
77
                    }
78
                }
79
            }
80
        }
81
82
        return $count;
83
    }
84
85
    /**
86
     * retourne une image "nouveau" ou "mise à jour"
87
     * @param $time
88
     * @param $status
89
     * @return string
90
     */
91
    public function getStatusImage($time, $status)
92
    {
93
        global $xoopsModuleConfig;
94
        $count     = 7;
95
        $new       = '';
96
        $startdate = (time() - (86400 * $count));
97
        if (1 == $xoopsModuleConfig['showupdated']) {
98
            if ($startdate < $time) {
99
                $language = $GLOBALS['xoopsConfig']['language'];
100
                if (!is_dir(XOOPS_ROOT_PATH . '/modules/tdmdownloads/language/' . $language . '/')) {
101
                    $language = 'english';
102
                }
103
                $img_path = XOOPS_ROOT_PATH . '/modules/tdmdownloads/language/' . $language . '/';
104
                $img_url  = XOOPS_URL . '/modules/tdmdownloads/language/' . $language . '/';
105
                if (1 == $status) {
106
                    if (is_readable($img_path . 'new.png')) {
107
                        $new = '&nbsp;<img src="' . $img_url . 'new.png" alt="' . _MD_TDMDOWNLOADS_INDEX_NEWTHISWEEK . '" title="' . _MD_TDMDOWNLOADS_INDEX_NEWTHISWEEK . '">';
108
                    } else {
109
                        $new = '&nbsp;<img src="' . XOOPS_URL . '/modules/tdmdownloads/language/english/new.png" alt="' . _MD_TDMDOWNLOADS_INDEX_NEWTHISWEEK . '" title="' . _MD_TDMDOWNLOADS_INDEX_NEWTHISWEEK . '">';
110
                    }
111
                } elseif (2 == $status) {
112
                    if (is_readable($img_path . 'updated.png')) {
113
                        $new = '&nbsp;<img src="' . $img_url . 'updated.png" alt="' . _MD_TDMDOWNLOADS_INDEX_UPTHISWEEK . '" title="' . _MD_TDMDOWNLOADS_INDEX_UPTHISWEEK . '">';
114
                    } else {
115
                        $new = '&nbsp;<img src="' . XOOPS_URL . '/modules/tdmdownloads/language/english/updated.png" alt="' . _MD_TDMDOWNLOADS_INDEX_UPTHISWEEK . '" title="' . _MD_TDMDOWNLOADS_INDEX_UPTHISWEEK . '">';
116
                    }
117
                }
118
            }
119
        }
120
121
        return $new;
122
    }
123
124
    /**
125
     * retourne une image "populaire"
126
     * @param $hits
127
     * @return string
128
     */
129
    public function getPopularImage($hits)
130
    {
131
        global $xoopsModuleConfig;
132
        $pop = '';
133
        if ($hits >= $xoopsModuleConfig['popular']) {
134
            $language = $GLOBALS['xoopsConfig']['language'];
135
            if (!is_dir(XOOPS_ROOT_PATH . '/modules/tdmdownloads/language/' . $language . '/')) {
136
                $language = 'english';
137
            }
138
            $img_path = XOOPS_ROOT_PATH . '/modules/tdmdownloads/language/' . $language . '/';
139
            $img_url  = XOOPS_URL . '/modules/tdmdownloads/language/' . $language . '/';
140
            if (is_readable($img_path . 'popular.png')) {
141
                $pop = '&nbsp;<img src="' . $img_url . 'popular.png" alt="' . _MD_TDMDOWNLOADS_INDEX_POPULAR . '" title="' . _MD_TDMDOWNLOADS_INDEX_POPULAR . '">';
142
            } else {
143
                $pop = '&nbsp;<img src ="' . XOOPS_URL . '/modules/tdmdownloads/language/english/popular.png" alt="' . _MD_TDMDOWNLOADS_INDEX_POPULAR . '" title="' . _MD_TDMDOWNLOADS_INDEX_POPULAR . '">';
144
            }
145
        }
146
147
        return $pop;
148
    }
149
150
    /**
151
     * @param        $mytree
152
     * @param        $key
153
     * @param        $category_array
154
     * @param        $title
155
     * @param string $prefix
156
     * @return string
157
     */
158
    public function getPathTree($mytree, $key, $category_array, $title, $prefix = '')
159
    {
160
        $categoryParent = $mytree->getAllParent($key);
161
        $categoryParent = array_reverse($categoryParent);
162
        $path            = '';
163
        foreach (array_keys($categoryParent) as $j) {
164
            /** @var \XoopsModules\Tdmdownloads\Category[] $categoryParent */
165
            $path .= $categoryParent[$j]->getVar($title) . $prefix;
166
        }
167
        if (array_key_exists($key, $category_array)) {
168
            /** @var \XoopsModules\Tdmdownloads\Category[] $category_array */
169
            $firstCategory = $category_array[$key]->getVar($title);
170
        } else {
171
            $firstCategory = '';
172
        }
173
        $path .= $firstCategory;
174
175
        return $path;
176
    }
177
178
    /**
179
     * @param \XoopsModules\Tdmdownloads\Tree $mytree
180
     * @param                                 $key
181
     * @param                                 $category_array
182
     * @param                                 $title
183
     * @param string                          $prefix
184
     * @param bool                            $link
185
     * @param string                          $order
186
     * @param bool                            $lasturl
187
     * @return string
188
     */
189
    public function getPathTreeUrl($mytree, $key, $category_array, $title, $prefix = '', $link = false, $order = 'ASC', $lasturl = false)
190
    {
191
        global $xoopsModule;
192
        $categoryParent = $mytree->getAllParent($key);
193
        if ('ASC' === $order) {
194
            $categoryParent = array_reverse($categoryParent);
195
            if (true === $link) {
196
                $path = '<a href="index.php">' . $xoopsModule->name() . '</a>' . $prefix;
197
            } else {
198
                $path = $xoopsModule->name() . $prefix;
199
            }
200
        } else {
201
            if (array_key_exists($key, $category_array)) {
202
                /** @var \XoopsModules\Tdmdownloads\Category[] $category_array */
203
                $firstCategory = $category_array[$key]->getVar($title);
204
            } else {
205
                $firstCategory = '';
206
            }
207
            $path = $firstCategory . $prefix;
208
        }
209
        foreach (array_keys($categoryParent) as $j) {
210
            /** @var \XoopsModules\Tdmdownloads\Category[] $categoryParent */
211
            if (true === $link) {
212
                $path .= '<a href="viewcat.php?cid=' . $categoryParent[$j]->getVar('cat_cid') . '">' . $categoryParent[$j]->getVar($title) . '</a>' . $prefix;
213
            } else {
214
                $path .= $categoryParent[$j]->getVar($title) . $prefix;
215
            }
216
        }
217
        if ('ASC' === $order) {
218
            if (array_key_exists($key, $category_array)) {
219
                if (true === $lasturl) {
220
                    $firstCategory = '<a href="viewcat.php?cid=' . $category_array[$key]->getVar('cat_cid') . '">' . $category_array[$key]->getVar($title) . '</a>';
221
                } else {
222
                    $firstCategory = $category_array[$key]->getVar($title);
223
                }
224
            } else {
225
                $firstCategory = '';
226
            }
227
            $path .= $firstCategory;
228
        } else {
229
            if (true === $link) {
230
                $path .= '<a href="index.php">' . $xoopsModule->name() . '</a>';
231
            } else {
232
                $path .= $xoopsModule->name();
233
            }
234
        }
235
236
        return $path;
237
    }
238
}
239