Completed
Push — master ( 2a7c05...7c1d68 )
by Michael
02:50
created

ClassifiedsTree::getNicePathFromId()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 14
nc 3
nop 4
dl 0
loc 22
rs 9.2
c 0
b 0
f 0
1
<?php
2
/*
3
-------------------------------------------------------------------------
4
                     ADSLIGHT 2 : Module for Xoops
5
6
        Redesigned and ameliorate By Luc Bizet user at www.frxoops.org
7
        Started with the Classifieds module and made MANY changes
8
        Website : http://www.luc-bizet.fr
9
        Contact : [email protected]
10
-------------------------------------------------------------------------
11
             Original credits below Version History
12
##########################################################################
13
#                    Classified Module for Xoops                         #
14
#  By John Mordo user jlm69 at www.xoops.org and www.jlmzone.com         #
15
#      Started with the MyAds module and made MANY changes               #
16
##########################################################################
17
 Original Author: Pascal Le Boustouller
18
 Author Website : [email protected]
19
 Licence Type   : GPL
20
-------------------------------------------------------------------------
21
*/
22
23
/**
24
 * Class ClassifiedsTree
25
 */
26
class ClassifiedsTree
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
27
{
28
    public $table;
29
    public $id;
30
    public $pid;
31
    public $order;
32
    public $title;
33
    public $db;
34
35
    /**
36
     * @param $table_name
37
     * @param $id_name
38
     * @param $pid_name
39
     */
40
    public function __construct($table_name, $id_name, $pid_name)
41
    {
42
        $this->db    = XoopsDatabaseFactory::getDatabaseConnection();
43
        $this->table = $table_name;
44
        $this->id    = $id_name;
45
        $this->pid   = $pid_name;
46
    }
47
48
    /**
49
     * @param int    $sel_id
50
     * @param string $order
51
     *
52
     * @return array
53
     */
54 View Code Duplication
    public function getFirstChild($sel_id, $order = '')
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
55
    {
56
        $arr = array();
57
        $sql = 'SELECT SQL_CACHE * FROM ' . $this->table . ' WHERE ' . $this->pid . '=' . (int)$sel_id . '';
58
59
        $categories = AdslightUtility::getMyItemIds('adslight_view');
60
        if (is_array($categories) && count($categories) > 0) {
61
            $sql .= ' AND ' . $this->pid . ' IN (' . implode(',', $categories) . ') ';
62
        }
63
64
        if ('' != $order) {
65
            $sql .= " ORDER BY $order";
66
        }
67
68
        $result = $this->db->query($sql);
69
        $count  = $this->db->getRowsNum($result);
70
        if (0 == $count) {
71
            return $arr;
72
        }
73
        while ($myrow = $this->db->fetchArray($result)) {
74
            array_push($arr, $myrow);
75
        }
76
77
        return $arr;
78
    }
79
80
    /**
81
     * @param $sel_id
82
     *
83
     * @return array
84
     */
85
    public function getFirstChildId($sel_id)
86
    {
87
        $idarray = array();
88
        $sel_id  = (int)$sel_id;
89
        $result  = $this->db->query('SELECT SQL_CACHE ' . $this->id . ' FROM ' . $this->table . ' WHERE ' . $this->pid . '=' . $sel_id);
90
91
        $categories = AdslightUtility::getMyItemIds('adslight_view');
92
        if (is_array($categories) && count($categories) > 0) {
93
            $result .= ' AND ' . $this->pid . ' IN (' . implode(',', $categories) . ') ';
94
        }
95
96
        $count = $this->db->getRowsNum($result);
97
        if (0 == $count) {
98
            return $idarray;
99
        }
100
        while (list($id) = $this->db->fetchRow($result)) {
101
            array_push($idarray, $id);
102
        }
103
104
        return $idarray;
105
    }
106
107
    /**
108
     * @param        $sel_id
109
     * @param string $order
110
     * @param array  $idarray
111
     *
112
     * @return array
113
     */
114
    public function getAllChildId($sel_id, $order = '', $idarray = array())
115
    {
116
        $sel_id = (int)$sel_id;
117
        $sql    = 'SELECT SQL_CACHE ' . $this->id . ' FROM ' . $this->table . ' WHERE ' . $this->pid . '=' . $sel_id;
118
119
        $categories = AdslightUtility::getMyItemIds('adslight_view');
120
        if (is_array($categories) && count($categories) > 0) {
121
            $sql .= ' AND ' . $this->pid . ' IN (' . implode(',', $categories) . ') ';
122
        }
123
124
        if ($order != '') {
125
            $sql .= " ORDER BY {$order}";
126
        }
127
        $result = $this->db->query($sql);
128
        $count  = $this->db->getRowsNum($result);
129
        if (0 == $count) {
130
            return $idarray;
131
        }
132
        while (list($r_id) = $this->db->fetchRow($result)) {
133
            array_push($idarray, $r_id);
134
            $idarray = $this->getAllChildId($r_id, $order, $idarray);
135
        }
136
137
        return $idarray;
138
    }
139
140
    /**
141
     * @param        $sel_id
142
     * @param string $order
143
     * @param array  $idarray
144
     *
145
     * @return array
146
     */
147 View Code Duplication
    public function getAllParentId($sel_id, $order = '', $idarray = array())
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
148
    {
149
        $sql = 'SELECT ' . $this->pid . ' FROM ' . $this->table . ' WHERE ' . $this->id . '=' . (int)$sel_id;
150
151
        $categories = AdslightUtility::getMyItemIds('adslight_view');
152
        if (is_array($categories) && count($categories) > 0) {
153
            $sql .= ' AND ' . $this->pid . ' IN (' . implode(',', $categories) . ') ';
154
        }
155
156
        if ('' != $order) {
157
            $sql .= " ORDER BY {$order}";
158
        }
159
        $result = $this->db->query($sql);
160
        list($r_id) = $this->db->fetchRow($result);
161
        if (0 == $r_id) {
162
            return $idarray;
163
        }
164
        array_push($idarray, $r_id);
165
        $idarray = $this->getAllParentId($r_id, $order, $idarray);
166
167
        return $idarray;
168
    }
169
170
    /**
171
     * @param        $sel_id
172
     * @param        $title
173
     * @param string $path
174
     *
175
     * @return string
176
     */
177
    public function getPathFromId($sel_id, $title, $path = '')
178
    {
179
        $sql = 'SELECT ' . $this->pid . ', ' . $title . ' FROM ' . $this->table . ' WHERE ' . $this->id . '=' . (int)$sel_id . '';
180
        //        $result = $this->db->query('SELECT ' . $this->pid . ', ' . $title . ' FROM ' . $this->table . ' WHERE ' . $this->id . '=' . $this->db->escape($sel_id) . "'");
0 ignored issues
show
Unused Code Comprehensibility introduced by
39% 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...
181
182
        $categories = AdslightUtility::getMyItemIds('adslight_view');
183
        if (is_array($categories) && count($categories) > 0) {
184
            //            $result .= ' AND cid IN (' . implode(',', $categories) . ') ';
0 ignored issues
show
Unused Code Comprehensibility introduced by
41% 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...
185
            $sql .= ' AND cid IN (' . implode(',', $categories) . ') ';
186
        }
187
188
        $result = $this->db->query($sql);
189
190
        if (0 == $this->db->getRowsNum($result)) {
191
            return $path;
192
        }
193
        list($parentid, $name) = $this->db->fetchRow($result);
194
        $myts = MyTextSanitizer::getInstance();
195
        $name = $myts->htmlSpecialChars($name);
196
        $path = '/' . $name . $path . '';
197
        if (0 == $parentid) {
198
            return $path;
199
        }
200
        $path = $this->getPathFromId($parentid, $title, $path);
201
202
        return $path;
203
    }
204
205
    /**
206
     * @param        $title
207
     * @param string $order
208
     * @param int    $preset_id
209
     * @param int    $none
210
     * @param string $sel_name
211
     * @param string $onchange
212
     */
213
    public function makeMySelBox($title, $order = '', $preset_id = 0, $none = 0, $sel_name = '', $onchange = '')
214
    {
215
        if ($sel_name == '') {
216
            $sel_name = $this->id;
217
        }
218
        $myts = MyTextSanitizer::getInstance();
219
        echo '<select name="' . $sel_name . '"';
220
        if ($onchange != '') {
221
            echo ' onchange="' . $onchange . '"';
222
        }
223
        echo '>';
224
225
        $sql        = 'SELECT SQL_CACHE cid, title FROM ' . $this->table . ' WHERE pid=0';
226
        $categories = AdslightUtility::getMyItemIds('adslight_submit');
227
228
        if (is_array($categories) && count($categories) > 0) {
229
            $sql .= ' AND cid IN (' . implode(',', $categories) . ') ';
230
        }
231
232
        if ('' != $order) {
233
            $sql .= " ORDER BY $order";
234
        }
235
236
        $result = $this->db->query($sql);
237
        if ($none) {
238
            echo '<option value="0">----</option>';
239
        }
240
        while (list($catid, $name) = $this->db->fetchRow($result)) {
241
            $sel = '';
242
            if ($catid == $preset_id) {
243
                $sel = ' selected';
244
            }
245
            echo "<option value=\"{$catid}\"{$sel}>{$name}</option>";
246
            $sel = '';
247
            $arr = $this->getChildTreeArray($catid, $order);
248
            foreach ($arr as $option) {
249
                $option['prefix'] = str_replace('.', '--', $option['prefix']);
250
                $catpath          = $option['prefix'] . '&nbsp;' . $myts->displayTarea($option[$title]);
251
                if ($option['cid'] == $preset_id) {
252
                    $sel = ' selected';
253
                }
254
                echo "<option value=\"{$option['cid']}\"{$sel}>{$catpath}</option>";
255
                $sel = '';
256
            }
257
        }
258
        echo '</select>';
259
    }
260
261
    /**
262
     * @param        $sel_id
263
     * @param        $title
264
     * @param        $funcURL
265
     * @param string $path
266
     *
267
     * @return string
268
     */
269
    public function getNicePathFromId($sel_id, $title, $funcURL, $path = '')
270
    {
271
        $sql    = 'SELECT SQL_CACHE ' . $this->pid . ", {$title} FROM " . $this->table . ' WHERE ' . $this->id . '=' . (int)$sel_id;
272
        $result = $this->db->query($sql);
273
        if (0 == $this->db->getRowsNum($result)) {
274
            return $path;
275
        }
276
        list($parentid, $name) = $this->db->fetchRow($result);
277
        $myts = MyTextSanitizer::getInstance();
278
        $name = $myts->htmlSpecialChars($name);
279
280
        $arrow = '<img src="' . XOOPS_URL . '/modules/adslight/assets/images/arrow.gif" alt="&raquo;" >';
281
282
        $path = "&nbsp;&nbsp;{$arrow}&nbsp;&nbsp;<a title=\"" . _ADSLIGHT_ANNONCES . " {$name}\" href=\"{$funcURL}" . $this->id . '=' . (int)$sel_id . "\">{$name}</a>{$path}";
283
284
        if (0 == $parentid) {
285
            return $path;
286
        }
287
        $path = $this->getNicePathFromId($parentid, $title, $funcURL, $path);
288
289
        return $path;
290
    }
291
292
    /**
293
     * @param        $sel_id
294
     * @param string $path
295
     *
296
     * @return string
297
     */
298
    public function getIdPathFromId($sel_id, $path = '')
299
    {
300
        $sel_id = (int)$sel_id;
301
        $result = $this->db->query('SELECT SQL_CACHE ' . $this->pid . ' FROM ' . $this->table . ' WHERE ' . $this->id . '=' . $sel_id);
302
        if (0 == $this->db->getRowsNum($result)) {
303
            return $path;
304
        }
305
        list($parentid) = $this->db->fetchRow($result);
306
        $path = "/{$sel_id}{$path}";
307
        if (0 == $parentid) {
308
            return $path;
309
        }
310
        $path = $this->getIdPathFromId($parentid, $path);
311
312
        return $path;
313
    }
314
315
    /**
316
     * @param int    $sel_id
317
     * @param string $order
318
     * @param array  $parray
319
     *
320
     * @return array
321
     */
322
    public function getAllChild($sel_id = 0, $order = '', $parray = array())
323
    {
324
        $sql = 'SELECT SQL_CACHE * FROM ' . $this->table . ' WHERE ' . $this->pid . '=' . (int)$sel_id;
325
326
        $categories = AdslightUtility::getMyItemIds('adslight_view');
327
        if (is_array($categories) && count($categories) > 0) {
328
            $sql .= ' AND ' . $this->pid . ' IN (' . implode(',', $categories) . ') ';
329
        }
330
331
        if ($order != '') {
332
            $sql .= " ORDER BY {$order}";
333
        }
334
335
        $result = $this->db->query($sql);
336
        $count  = $this->db->getRowsNum($result);
337
        if ($count == 0) {
338
            return $parray;
339
        }
340
        while ($row = $this->db->fetchArray($result)) {
341
            array_push($parray, $row);
342
            $parray = $this->getAllChild($row[$this->id], $order, $parray);
343
        }
344
345
        return $parray;
346
    }
347
348
    /**
349
     * @param int    $sel_id
350
     * @param string $order
351
     * @param array  $parray
352
     * @param string $r_prefix
353
     *
354
     * @return array
355
     */
356
    public function getChildTreeArray($sel_id = 0, $order = '', $parray = array(), $r_prefix = '')
357
    {
358
        global $moduleDirName;
359
360
        $sql = 'SELECT SQL_CACHE * FROM ' . $this->table . ' WHERE ' . $this->pid . '=' . (int)$sel_id;
361
362
        $categories = AdslightUtility::getMyItemIds('adslight_view');
363
        if (is_array($categories) && count($categories) > 0) {
364
            $sql .= ' AND cid IN (' . implode(',', $categories) . ') ';
365
        }
366
367
        if ($order != '') {
368
            $sql .= " ORDER BY {$order}";
369
        }
370
        $result = $this->db->query($sql);
371
        $count  = $this->db->getRowsNum($result);
372
        if ($count == 0) {
373
            return $parray;
374
        }
375 View Code Duplication
        while ($row = $this->db->fetchArray($result)) {
376
            $row['prefix'] = $r_prefix . '.';
377
            array_push($parray, $row);
378
            $parray = $this->getChildTreeArray($row[$this->id], $order, $parray, $row['prefix']);
379
        }
380
381
        return $parray;
382
    }
383
384
    /**
385
     * @param        $title
386
     * @param string $order
387
     * @param int    $preset_id
388
     * @param int    $none
389
     * @param string $sel_name
390
     * @param string $onchange
391
     */
392
    public function makeAdSelBox($title, $order = '', $preset_id = 0, $none = 0, $sel_name = '', $onchange = '')
393
    {
394
        global $myts, $xoopsDB;
395
        $pathIcon16 = \Xmf\Module\Admin::iconUrl('', 16);
396
        require XOOPS_ROOT_PATH . '/modules/adslight/include/gtickets.php';
397
398
        if ('' == $sel_name) {
399
            $sel_name = $this->id;
0 ignored issues
show
Unused Code introduced by
$sel_name is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
400
        }
401
402
        $sql = 'SELECT ' . $this->id . ', ' . $title . ', cat_order FROM ' . $this->table . ' WHERE ' . $this->pid . '=0';
403
        if ('' != $order) {
404
            $sql .= " ORDER BY {$order}";
405
        }
406
        $result = $xoopsDB->query($sql);
407
        while (list($catid, $name, $cat_order) = $xoopsDB->fetchRow($result)) {
408
            echo '<table class="width100 bnone outer"><tr>
409
                <th class="left">';
410
            if ('cat_order' === $GLOBALS['xoopsModuleConfig']['adslight_csortorder']) {
411
                echo "({$cat_order})";
412
            }
413
            echo "&nbsp;&nbsp;{$name}&nbsp;&nbsp;</th>
414
                <th class=\"center width10\"><a href=\"category.php?op=AdsNewCat&amp;cid={$catid}\"><img src=\"{$pathIcon16}/add.png\" border=\"0\" width=\"18\" height=\"18\" alt=\""
415
                 . _AM_ADSLIGHT_ADDSUBCAT
416
                 . "\" title=\""
417
                 . _AM_ADSLIGHT_ADDSUBCAT
418
                 . "\"></a></th>
419
                <th class=\"center width10\"><a href=\"category.php?op=AdsModCat&amp;cid={$catid}\"><img src=\"{$pathIcon16}/edit.png\" border=\"0\" width=\"18\" height=\"18\" alt=\""
420
                 . _AM_ADSLIGHT_MODIFSUBCAT
421
                 . "\" title=\""
422
                 . _AM_ADSLIGHT_MODIFSUBCAT
423
                 . "\"></a></th>
424
                <th class=\"center width10\"><a href=\"category.php?op=AdsDelCat&amp;cid={$catid}\"><img src=\"{$pathIcon16}/delete.png\" border=\"0\" width=\"18\" height=\"18\" alt=\""
425
                 . _AM_ADSLIGHT_DELSUBCAT
426
                 . "\" title=\""
427
                 . _AM_ADSLIGHT_DELSUBCAT
428
                 . "\"></a></th>
429
                </tr>";
430
431
            $arr   = $this->getChildTreeMapArray($catid, $order);
432
            $class = 'odd';
433
            foreach ($arr as $option) {
434
                echo "<tr class=\"{$class}\"><td>";
435
436
                $option['prefix'] = str_replace('.', ' &nbsp;&nbsp;-&nbsp;', $option['prefix']);
437
                $catpath          = $option['prefix'] . '&nbsp;&nbsp;' . $myts->htmlSpecialChars($option[$title]);
438
                $cat_orderS       = $option['cat_order'];
439
                if ('cat_order' == $GLOBALS['xoopsModuleConfig']['adslight_csortorder']) {
440
                    echo "({$cat_orderS})";
441
                }
442
                echo ''
443
                     . $catpath
444
                     . '</a></td>
445
                    <td align="center"><a href="category.php?op=AdsNewCat&amp;cid='
446
                     . $option[$this->id]
447
                     . '"><img src="'
448
                     . $pathIcon16
449
                     . '/add.png'
450
                     . '" border=0 width=18 height=18 alt="'
451
                     . _AM_ADSLIGHT_ADDSUBCAT
452
                     . '"title="'
453
                     . _AM_ADSLIGHT_ADDSUBCAT
454
                     . '"></a></td>
455
                    <td align="center"><a href="category.php?op=AdsModCat&amp;cid='
456
                     . $option[$this->id]
457
                     . '"><img src="'
458
                     . $pathIcon16
459
                     . '/edit.png'
460
                     . '" border=0 width=18 height=18 alt="'
461
                     . _AM_ADSLIGHT_MODIFSUBCAT
462
                     . '" title ="'
463
                     . _AM_ADSLIGHT_MODIFSUBCAT
464
                     . '"></a></td>
465
                    <td align="center"><a href="category.php?op=AdsDelCat&amp;cid='
466
                     . $option[$this->id]
467
                     . '"><img src="'
468
                     . $pathIcon16
469
                     . '/delete.png'
470
                     . '" border=0 width=18 height=18 alt="'
471
                     . _AM_ADSLIGHT_DELSUBCAT
472
                     . '" title="'
473
                     . _AM_ADSLIGHT_DELSUBCAT
474
                     . '"></a></td>';
475
476
                $class = ('even' == $class) ? 'odd' : 'even';
477
            }
478
            echo '</td></tr></table><br>';
479
        }
480
    }
481
482
    /**
483
     * @param int    $sel_id
484
     * @param string $order
485
     * @param array  $parray
486
     * @param string $r_prefix
487
     *
488
     * @return array
489
     */
490
    public function getChildTreeMapArray($sel_id = 0, $order = '', $parray = array(), $r_prefix = '')
491
    {
492
        global $xoopsDB;
493
        $sql = 'SELECT SQL_CACHE * FROM ' . $this->table . ' WHERE ' . $this->pid . '=' . (int)$sel_id . '';
494
495
        $categories = AdslightUtility::getMyItemIds('adslight_view');
496
        if (is_array($categories) && count($categories) > 0) {
497
            $sql .= ' AND ' . $this->pid . ' IN (' . implode(',', $categories) . ') ';
498
        }
499
500
        if ('' != $order) {
501
            $sql .= " ORDER BY {$order}";
502
        }
503
        $result = $xoopsDB->query($sql);
504
        $count  = $xoopsDB->getRowsNum($result);
505
        if (0 == $count) {
506
            return $parray;
507
        }
508 View Code Duplication
        while ($row = $xoopsDB->fetchArray($result)) {
509
            $row['prefix'] = $r_prefix . '.';
510
            array_push($parray, $row);
511
            $parray = $this->getChildTreeMapArray($row[$this->id], $order, $parray, $row['prefix']);
512
        }
513
514
        return $parray;
515
    }
516
517
    /**
518
     * @return array
519
     */
520
    public function getCategoryList()
521
    {
522
        $result = $this->db->query('SELECT SQL_CACHE cid, pid, title FROM ' . $this->table);
523
        $ret    = array();
524
        $myts   = MyTextSanitizer::getInstance();
525
        while ($myrow = $this->db->fetchArray($result)) {
526
            $ret[$myrow['cid']] = array(
527
                'title' => $myts->htmlspecialchars($myrow['title']),
528
                'pid'   => $myrow['pid']
529
            );
530
        }
531
532
        return $ret;
533
    }
534
}
535