Completed
Push — master ( 047d50...4f5633 )
by Michael
02:19
created

CategoryHandler::hasValidParent()   A

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 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php namespace XoopsModules\Extgallery;
2
3
/**
4
 * ExtGallery Class Manager
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
 * This program is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
 *
13
 * @copyright   {@link https://xoops.org/ XOOPS Project}
14
 * @license     GNU GPL 2 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
15
 * @author      Zoullou (http://www.zoullou.net)
16
 * @package     ExtGallery
17
 */
18
19
use XoopsModules\Extgallery;
20
21
// 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...
22
23
/**
24
 * Class Extgallery\CategoryHandler
25
 */
26
class CategoryHandler extends Extgallery\PersistableObjectHandler
27
{
28
    //var $_nestedTree;
29
    public $_photoHandler;
30
31
    /**
32
     * @param $db
33
     * @param $type
34
     */
35
    public function __construct(\XoopsDatabase $db, $type)
0 ignored issues
show
Bug introduced by
The type XoopsDatabase was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
36
    {
37
        parent::__construct($db, 'extgallery_' . $type . 'cat', ucfirst($type) . 'Category', 'cat_id');
38
        //$this->_nestedTree = new NestedTree($db, 'extgallery_'.$type.'cat', 'cat_id', 'cat_pid', 'cat_id');
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% 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...
39
        $this->_photoHandler = Extgallery\Helper::getInstance()->getHandler($type . 'Photo');
40
    }
41
42
    /**
43
     * @param $data
44
     *
45
     * @return bool
46
     */
47
    public function createCat($data)
48
    {
49
        $cat = $this->create();
50
        $cat->setVars($data);
51
52
        if (!$this->hasValidParent($cat)) {
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->hasValidParent($cat) targeting XoopsModules\Extgallery\...ndler::hasValidParent() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
Unused Code introduced by
The call to XoopsModules\Extgallery\...ndler::hasValidParent() has too many arguments starting with $cat. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

52
        if (!$this->/** @scrutinizer ignore-call */ hasValidParent($cat)) {

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
53
            return false;
54
        }
55
56
        $this->insert($cat, true);
57
        $this->rebuild();
58
59
        return true;
60
    }
61
62
    /**
63
     * @param $data
64
     *
65
     * @return bool
66
     */
67
    public function modifyCat($data)
68
    {
69
        $cat = $this->get($data['cat_id']);
70
        $cat->setVars($data);
71
72
        if (!$this->hasValidParent($cat)) {
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->hasValidParent($cat) targeting XoopsModules\Extgallery\...ndler::hasValidParent() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
Unused Code introduced by
The call to XoopsModules\Extgallery\...ndler::hasValidParent() has too many arguments starting with $cat. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

72
        if (!$this->/** @scrutinizer ignore-call */ hasValidParent($cat)) {

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
73
            return false;
74
        }
75
        $this->insert($cat, true);
76
77
        // Rebluid the tree only if the structure is modified
78
        if (isset($data['cat_pid']) || isset($data['nlevel']) || isset($data['nright']) || isset($data['nleft'])) {
79
            $this->rebuild();
80
        }
81
        return '';
82
    }
83
84
    /**
85
     * @param int $catId
86
     */
87
    public function deleteCat($catId)
88
    {
89
        $children = $this->getDescendants($catId, false, true);
90
        foreach ($children as $child) {
91
            $this->_photoHandler->deletePhotoByCat($child->getVar('cat_id'));
92
            $this->deleteCat($child->getVar('cat_id'));
93
        }
94
        $this->_photoHandler->deletePhotoByCat($catId);
95
        $this->deleteById($catId);
96
    }
97
98
    /**
99
     * @param int    $id
100
     * @param bool   $includeSelf
101
     * @param bool   $childrenOnly
102
     * @param bool   $withRestrict
103
     * @param string $permType
104
     *
105
     * @return array
106
     */
107
    public function getDescendants(
108
        $id = 0,
109
        $includeSelf = false,
110
        $childrenOnly = false,
111
        $withRestrict = true,
112
        $permType = 'public_access'
113
    ) {
114
        $cat = $this->get($id);
115
116
        $nleft     = $cat->getVar('nleft');
117
        $nright    = $cat->getVar('nright');
118
        $parent_id = $cat->getVar('cat_id');
119
120
        $criteria = new \CriteriaCompo();
0 ignored issues
show
Bug introduced by
The type CriteriaCompo was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
121
122
        if ($childrenOnly) {
123
            $criteria->add(new \Criteria('cat_pid', $parent_id), 'OR');
0 ignored issues
show
Bug introduced by
The type Criteria was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
124
            if ($includeSelf) {
125
                $criteria->add(new \Criteria('cat_id', $parent_id));
126
                //$query = sprintf('select * from %s where %s = %d or %s = %d order by nleft', $this->table, $this->fields['id'], $parent_id, $this->fields['parent'], $parent_id);
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...
127
            }/* else {
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% 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...
128
                //$query = sprintf('select * from %s where %s = %d order by nleft', $this->table, $this->fields['parent'], $parent_id);
129
            }*/
130
        } else {
131
            if ($nleft > 0 && $includeSelf) {
132
                $criteria->add(new \Criteria('nleft', $nleft, '>='));
133
                $criteria->add(new \Criteria('nright', $nright, '<='));
134
            //$query = sprintf('select * from %s where nleft >= %d and nright <= %d order by nleft', $this->table, $nleft, $nright);
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% 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...
135
            } else {
136
                if ($nleft > 0) {
137
                    $criteria->add(new \Criteria('nleft', $nleft, '>'));
138
                    $criteria->add(new \Criteria('nright', $nright, '<'));
139
                    //$query = sprintf('select * from %s where nleft > %d and nright < %d order by nleft', $this->table, $nleft, $nright);
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% 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...
140
                }/* else {
0 ignored issues
show
Unused Code Comprehensibility introduced by
53% 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...
141
                $query = sprintf('select * from %s order by nleft', $this->table);
142
                }*/
143
            }
144
        }
145
        if ($withRestrict) {
146
            $temp = $this->getCatRestrictCriteria($permType);
147
            if (null !== $temp) {
0 ignored issues
show
introduced by
The condition null !== $temp is always true.
Loading history...
148
                $criteria->add($temp);
149
            }
150
            $temp = $this->getCatRestrictCriteria('public_displayed');
151
            if (null !== $temp) {
0 ignored issues
show
introduced by
The condition null !== $temp is always true.
Loading history...
152
                $criteria->add($temp);
153
            }
154
        }
155
        $criteria->setSort('nleft');
156
157
        return $this->getObjects($criteria);
158
    }
159
160
    /**
161
     * @param int $id
162
     *
163
     * @return null
164
     */
165
    public function getCat($id = 0)
166
    {
167
        $criteria = new \CriteriaCompo();
168
        $criteria->add($this->getCatRestrictCriteria('public_displayed'));
169
        $criteria->add(new \Criteria('cat_id', $id));
170
        $ret =& $this->getObjects($criteria);
171
172
        if (count($ret) > 0) {
173
            return $ret[0];
174
        }
175
176
        return null;
177
    }
178
179
    public function hasValidParent()
180
    {
181
        exit('hasValidParent() method must be defined on sub classes');
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
182
    }
183
184
    /**
185
     * @param $cat
186
     *
187
     * @return bool
188
     */
189
    public function _isAlbum($cat)
190
    {
191
        $nbPhoto = $this->nbPhoto($cat);
192
193
        return 0 != $nbPhoto;
194
    }
195
196
    /**
197
     * @param Extgallery\Category $cat
198
     *
199
     * @return mixed
200
     */
201
    public function nbPhoto(&$cat)
202
    {
203
        /** @var Extgallery\CategoryHandler $this ->_photoHandler */
204
        return $this->_photoHandler->nbPhoto($cat);
205
    }
206
207
    /**
208
     * @param int  $id
209
     * @param bool $includeSelf
210
     *
211
     * @return array
212
     */
213
    public function getPath($id = 0, $includeSelf = false)
214
    {
215
        $cat = $this->get($id);
216
        if (null === $cat) {
217
            return [];
218
        }
219
220
        $criteria = new \CriteriaCompo();
221
        if ($includeSelf) {
222
            $criteria->add(new \Criteria('nleft', $cat->getVar('nleft'), '<='));
223
            $criteria->add(new \Criteria('nright', $cat->getVar('nright'), '>='));
224
        //$query = sprintf('select * from %s where nleft <= %d and nright >= %d order by nlevel', $this->table, $node['nleft'], $node['nright']);
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...
225
        } else {
226
            $criteria->add(new \Criteria('nleft', $cat->getVar('nleft'), '<'));
227
            $criteria->add(new \Criteria('nright', $cat->getVar('nright'), '>'));
228
            //$query = sprintf('select * from %s where nleft < %d and nright > %d order by nlevel', $this->table, $node['nleft'], $node['nright']);
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...
229
        }
230
        $criteria->add($this->getCatRestrictCriteria());
231
        $criteria->add($this->getCatRestrictCriteria('public_displayed'));
232
        $criteria->setSort('nlevel');
233
234
        return $this->getObjects($criteria);
235
    }
236
237
    /**
238
     * @return array
239
     */
240
    public function getTree()
241
    {
242
        return $this->getDescendants(0, false, false, false);
243
    }
244
245
    /**
246
     * @param int  $id
247
     * @param bool $includeSelf
248
     *
249
     * @return array
250
     */
251
    public function getChildren($id = 0, $includeSelf = false)
252
    {
253
        return $this->getDescendants($id, $includeSelf, true);
254
    }
255
256
    /**
257
     * @param int $id
258
     *
259
     * @return int
260
     */
261
    public function nbAlbum($id = 0)
262
    {
263
        $criteria = new \CriteriaCompo(new \Criteria('nright - nleft', 1));
264
        //$query = sprintf('select count(*) as num_leef from %s where nright - nleft = 1', $this->table);
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% 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...
265
        if (0 != $id) {
266
            $cat = $this->get($id);
267
            $criteria->add(new \Criteria('nleft', $cat->getVar('nleft'), '>'));
268
            $criteria->add(new \Criteria('nright', $cat->getVar('nright'), '<'));
269
            //$query .= sprintf(' AND nleft > %d AND nright < %d', $node['nleft'], $node['nright']);
0 ignored issues
show
Unused Code Comprehensibility introduced by
69% 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...
270
        }
271
272
        return $this->getCount($criteria);
273
    }
274
275
    /**
276
     * @param        $name
277
     * @param        $selectMode
278
     * @param bool   $addEmpty
279
     * @param int    $selected
280
     * @param string $extra
281
     * @param bool   $displayWeight
282
     * @param string $permType
283
     *
284
     * @return string
285
     */
286
    public function getSelect(
287
        $name,
288
        $selectMode,
289
        $addEmpty = false,
290
        $selected = 0,
291
        $extra = '',
292
        $displayWeight = false,
293
        $permType = 'public_access'
294
    ) {
295
        $cats = $this->getDescendants(0, false, false, true, $permType);
296
297
        return $this->makeSelect($cats, $name, $selectMode, $addEmpty, $selected, $extra, $displayWeight);
298
    }
299
300
    /**
301
     * @param        $name
302
     * @param bool   $addEmpty
303
     * @param int    $selected
304
     * @param string $extra
305
     * @param string $permType
306
     *
307
     * @return string
308
     */
309
    public function getLeafSelect($name, $addEmpty = false, $selected = 0, $extra = '', $permType = 'public_access')
310
    {
311
        return $this->getSelect($name, 'node', $addEmpty, $selected, $extra, false, $permType);
312
    }
313
314
    /**
315
     * @param        $name
316
     * @param bool   $addEmpty
317
     * @param int    $selected
318
     * @param string $extra
319
     *
320
     * @return string
321
     */
322
    public function getNodeSelect($name, $addEmpty = false, $selected = 0, $extra = '')
323
    {
324
        return $this->getSelect($name, 'leaf', $addEmpty, $selected, $extra);
325
    }
326
327
    /**
328
     * @param array  $cats
329
     * @param string $name
330
     * @param string $selectMode
331
     * @param        $addEmpty
332
     * @param        $selected
333
     * @param        $extra
334
     * @param        $displayWeight
335
     *
336
     * @return string
337
     */
338
    public function makeSelect($cats, $name, $selectMode, $addEmpty, $selected, $extra, $displayWeight)
339
    {
340
        $ret = '<select name="' . $name . '" id="' . $name . '"' . $extra . '>';
341
        if ($addEmpty) {
342
            $ret .= '<option value="0">-----</option>';
343
        }
344
        /** @var Extgallery\Category $cat */
345
        foreach ($cats as $cat) {
346
            $disableOption = '';
347
            if ('node' === $selectMode && (1 != $cat->getVar('nright') - $cat->getVar('nleft'))) {
348
                // If the brownser is IE the parent cat isn't displayed
349
                //                if (preg_match('`MSIE`', $_SERVER['HTTP_USER_AGENT'])) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
71% 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...
350
                if (false !== strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE')) {
351
                    continue;
352
                }
353
                $disableOption = ' disabled="disabled"';
354
            } elseif ('leaf' === $selectMode && (1 == $cat->getVar('cat_isalbum'))) {
355
                continue;
356
            }
357
358
            $selectedOption = '';
359
            if ($cat->getVar('cat_id') == $selected) {
360
                $selectedOption = ' selected';
361
            }
362
363
            $prefix = '';
364
            for ($i = 0; $i < $cat->getVar('nlevel') - 1; ++$i) {
365
                $prefix .= '--';
366
            }
367
            $catName = $prefix . ' ' . $cat->getVar('cat_name');
368
            if ($displayWeight) {
369
                $catName .= ' [' . $cat->getVar('cat_weight') . ']';
370
            }
371
372
            $ret .= '<option value="' . $cat->getVar('cat_id') . '"' . $selectedOption . '' . $disableOption . '>' . $catName . '</option>';
373
        }
374
        $ret .= '</select>';
375
376
        return $ret;
377
    }
378
379
    /**
380
     * @param array $selected
381
     *
382
     * @return string
383
     */
384
    public function getBlockSelect($selected = [])
385
    {
386
        $cats           = $this->getDescendants();
387
        $ret            = '<select name="options[]" multiple="multiple">';
388
        $selectedOption = '';
389
        if ($allCat = in_array(0, $selected)) {
390
            $selectedOption = ' selected';
391
        }
392
        $ret .= '<option value="0"' . $selectedOption . '>' . _MB_EXTGALLERY_ALL_CATEGORIES . '</option>';
393
        foreach ($cats as $cat) {
394
            $prefix = '';
395
            for ($i = 0; $i < $cat->getVar('nlevel') - 1; ++$i) {
396
                $prefix .= '-';
397
            }
398
            $selectedOption = '';
399
            $disableOption  = '';
400
401
            if (!$allCat && in_array($cat->getVar('cat_id'), $selected)) {
402
                $selectedOption = ' selected';
403
            }
404
405
            if (1 != $cat->getVar('nright') - $cat->getVar('nleft')) {
406
                $disableOption = ' disabled="disabled"';
407
            }
408
409
            $ret .= '<option value="' . $cat->getVar('cat_id') . '"' . $selectedOption . '' . $disableOption . '>' . $prefix . ' ' . $cat->getVar('cat_name') . '</option>';
410
        }
411
        $ret .= '</select>';
412
413
        return $ret;
414
    }
415
416
    /**
417
     * @return array
418
     */
419
    public function getTreeWithChildren()
420
    {
421
        $criteria = new \CriteriaCompo();
422
        $criteria->setSort('cat_weight, cat_name');
423
        //$query = sprintf('select * from %s order by %s', $this->table, $this->fields['sort']);
0 ignored issues
show
Unused Code Comprehensibility introduced by
64% 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...
424
425
        //$result = $this->db->query($query);
0 ignored issues
show
Unused Code Comprehensibility introduced by
62% 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...
426
        $categories =& $this->getObjects($criteria, false, false);
427
428
        // create a root node to hold child data about first level items
429
        $root             = [];
430
        $root['cat_id']   = 0;
431
        $root['children'] = [];
432
433
        $arr = [
434
            $root
435
        ];
436
437
        // populate the array and create an empty children array
438
        /*while (false !== ($row = $this->db->fetchArray($result))) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
69% 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...
439
            $arr[$row[$this->fields['id']]] = $row;
440
            $arr[$row[$this->fields['id']]]['children'] = array ();
441
        }*/
442
        foreach ($categories as $row) {
443
            $arr[$row['cat_id']]             = $row;
444
            $arr[$row['cat_id']]['children'] = [];
445
        }
446
447
        // now process the array and build the child data
448
        foreach ($arr as $id => $row) {
449
            if (isset($row['cat_pid'])) {
450
                $arr[$row['cat_pid']]['children'][$id] = $id;
451
            }
452
        }
453
454
        return $arr;
455
    }
456
457
    /**
458
     * Rebuilds the tree data and saves it to the database
459
     */
460
    public function rebuild()
461
    {
462
        $data = $this->getTreeWithChildren();
463
464
        $n     = 0; // need a variable to hold the running n tally
465
        $level = 0; // need a variable to hold the running level tally
0 ignored issues
show
Unused Code introduced by
The assignment to $level is dead and can be removed.
Loading history...
466
467
        // invoke the recursive function. Start it processing
468
        // on the fake "root node" generated in getTreeWithChildren().
469
        // because this node doesn't really exist in the database, we
470
        // give it an initial nleft value of 0 and an nlevel of 0.
471
        $this->_generateTreeData($data, 0, 0, $n);
472
        //echo "<pre>";print_r($data);echo "</pre>";
0 ignored issues
show
Unused Code Comprehensibility introduced by
77% 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...
473
        // at this point the root node will have nleft of 0, nlevel of 0
474
        // and nright of (tree size * 2 + 1)
475
476
        // Errase category and photo counter
477
        $query = sprintf('UPDATE `%s` SET cat_nb_album = 0, cat_nb_photo = 0;', $this->table);
478
        $this->db->queryF($query);
479
480
        foreach ($data as $id => $row) {
481
482
            // skip the root node
483
            if (0 == $id) {
484
                continue;
485
            }
486
487
            // Update the photo number
488
            if (1 == $row['nright'] - $row['nleft']) {
489
                // Get the number of photo in this album
490
                $criteria = new \CriteriaCompo();
491
                $criteria->add(new \Criteria('cat_id', $id));
492
                $criteria->add(new \Criteria('photo_approved', 1));
493
                /** @var Extgallery\CategoryHandler $this ->_photoHandler */
494
                $nbPhoto = $this->_photoHandler->getCount($criteria);
495
496
                // Update all parent of this album
497
                $upNbAlbum = '';
498
                if (0 != $nbPhoto) {
499
                    $upNbAlbum = 'cat_nb_album = cat_nb_album + 1, ';
500
                }
501
                $sql   = 'UPDATE `%s` SET ' . $upNbAlbum . 'cat_nb_photo = cat_nb_photo + %d WHERE nleft < %d AND nright > %d;';
502
                $query = sprintf($sql, $this->table, $nbPhoto, $row['nleft'], $row['nright']);
503
                $this->db->queryF($query);
504
505
                // Update this album if needed
506
                if (0 != $nbPhoto) {
507
                    $sql   = 'UPDATE `%s`SET cat_nb_photo = %d WHERE `%s` = %d';
508
                    $query = sprintf($sql, $this->table, $nbPhoto, $this->keyName, $id);
509
                    $this->db->queryF($query);
510
                }
511
            }
512
513
            $query = sprintf('UPDATE `%s`SET nlevel = %d, nleft = %d, nright = %d WHERE `%s` = %d;', $this->table, $row['nlevel'], $row['nleft'], $row['nright'], $this->keyName, $id);
514
            $this->db->queryF($query);
515
        }
516
    }
517
518
    /**
519
     * Generate the tree data. A single call to this generates the n-values for
520
     * 1 node in the tree. This function assigns the passed in n value as the
521
     * node's nleft value. It then processes all the node's children (which
522
     * in turn recursively processes that node's children and so on), and when
523
     * it is finally done, it takes the update n-value and assigns it as its
524
     * nright value. Because it is passed as a reference, the subsequent changes
525
     * in subrequests are held over to when control is returned so the nright
526
     * can be assigned.
527
     *
528
     * @param array &$arr  A reference to the data array, since we need to
529
     *                     be able to update the data in it
530
     * @param int   $id    The ID of the current node to process
531
     * @param int   $level The nlevel to assign to the current node
532
     * @param int   &$n    A reference to the running tally for the n-value
533
     */
534
    public function _generateTreeData(&$arr, $id, $level, &$n)
535
    {
536
        $arr[$id]['nlevel'] = $level;
537
        $arr[$id]['nleft']  = ++$n;
538
539
        // loop over the node's children and process their data
540
        // before assigning the nright value
541
        foreach ($arr[$id]['children'] as $child_id) {
542
            $this->_generateTreeData($arr, $child_id, $level + 1, $n);
543
        }
544
        $arr[$id]['nright'] = ++$n;
545
    }
546
547
    /**
548
     * @param string $permType
549
     *
550
     * @return bool|\Criteria
551
     */
552
    public function getCatRestrictCriteria($permType = 'public_access')
553
    {
554
        if (null !== $GLOBALS['xoopsUser'] && is_object($GLOBALS['xoopsUser'])) {
555
            $permHandler       = $this->getPermHandler();
556
            $allowedCategories = $permHandler->getAuthorizedPublicCat($GLOBALS['xoopsUser'], $permType);
557
558
            $count = count($allowedCategories);
559
            if ($count > 0) {
560
                $in = '(' . $allowedCategories[0];
561
                array_shift($allowedCategories);
562
                foreach ($allowedCategories as $allowedCategory) {
563
                    $in .= ',' . $allowedCategory;
564
                }
565
                $in       .= ')';
566
                $criteria = new \Criteria('cat_id', $in, 'IN');
567
            } else {
568
                $criteria = new \Criteria('cat_id', '(0)', 'IN');
569
            }
570
571
            return $criteria;
572
        }
573
        return false;
574
    }
575
576
    /**
577
     * @return Extgallery\PublicPermHandler
578
     */
579
    public function getPermHandler()
580
    {
581
        return Extgallery\PublicPermHandler::getInstance();
582
    }
583
}
584