Completed
Branch master (616741)
by Michael
03:30 queued 01:05
created

ClassifiedsTree::makeAdSelBox()   C

Complexity

Conditions 8
Paths 44

Size

Total Lines 105
Code Lines 84

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 84
nc 44
nop 6
dl 0
loc 105
rs 5.2676
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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        $sel_id
50
     * @param string $order
51
     *
52
     * @return array
53
     */
54
    public function getFirstChild($sel_id, $order = '')
55
    {
56
        $arr = array();
57
        $sql = 'SELECT SQL_CACHE * FROM ' . $this->table . ' WHERE ' . $this->pid . '=' . $sel_id . '';
58
59
        $categories = AdslightUtilities::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 ($count == 0) {
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
        $result  = $this->db->query('SELECT SQL_CACHE ' . $this->id . ' FROM ' . $this->table . ' WHERE ' . $this->pid . '=' . $this->db->escape($sel_id));
89
90
        $categories = AdslightUtilities::getMyItemIds('adslight_view');
91
        if (is_array($categories) && count($categories) > 0) {
92
            $result .= ' AND ' . $this->pid . ' IN (' . implode(',', $categories) . ') ';
93
        }
94
95
        $count = $this->db->getRowsNum($result);
96
        if ($count == 0) {
97
            return $idarray;
98
        }
99
        while (list($id) = $this->db->fetchRow($result)) {
100
            array_push($idarray, $id);
101
        }
102
103
        return $idarray;
104
    }
105
106
    /**
107
     * @param        $sel_id
108
     * @param string $order
109
     * @param array  $idarray
110
     *
111
     * @return array
112
     */
113 View Code Duplication
    public function getAllChildId($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...
114
    {
115
        $sql = 'SELECT SQL_CACHE ' . $this->id . ' FROM ' . $this->table . ' WHERE ' . $this->pid . '=' . $this->db->escape($sel_id) . '';
116
117
        $categories = AdslightUtilities::getMyItemIds('adslight_view');
118
        if (is_array($categories) && count($categories) > 0) {
119
            $sql .= ' AND ' . $this->pid . ' IN (' . implode(',', $categories) . ') ';
120
        }
121
122
        if ($order != '') {
123
            $sql .= " ORDER BY {$order}";
124
        }
125
        $result = $this->db->query($sql);
126
        $count  = $this->db->getRowsNum($result);
127
        if ($count == 0) {
128
            return $idarray;
129
        }
130
        while (list($r_id) = $this->db->fetchRow($result)) {
131
            array_push($idarray, $r_id);
132
            $idarray = $this->getAllChildId($r_id, $order, $idarray);
133
        }
134
135
        return $idarray;
136
    }
137
138
    /**
139
     * @param        $sel_id
140
     * @param string $order
141
     * @param array  $idarray
142
     *
143
     * @return array
144
     */
145 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...
146
    {
147
        $sql = 'SELECT ' . $this->pid . ' FROM ' . $this->table . ' WHERE ' . $this->id . '=' . $this->db->escape($sel_id) . '';
148
149
        $categories = AdslightUtilities::getMyItemIds('adslight_view');
150
        if (is_array($categories) && count($categories) > 0) {
151
            $sql .= ' AND ' . $this->pid . ' IN (' . implode(',', $categories) . ') ';
152
        }
153
154
        if ($order != '') {
155
            $sql .= " ORDER BY {$order}";
156
        }
157
        $result = $this->db->query($sql);
158
        list($r_id) = $this->db->fetchRow($result);
159
        if ($r_id == 0) {
160
            return $idarray;
161
        }
162
        array_push($idarray, $r_id);
163
        $idarray = $this->getAllParentId($r_id, $order, $idarray);
164
165
        return $idarray;
166
    }
167
168
    /**
169
     * @param        $sel_id
170
     * @param        $title
171
     * @param string $path
172
     *
173
     * @return string
174
     */
175
    public function getPathFromId($sel_id, $title, $path = '')
176
    {
177
178
        $sql = 'SELECT ' . $this->pid . ', ' . $title . ' FROM ' . $this->table . ' WHERE ' . $this->id . '=' . $this->db->escape($sel_id) . '';
179
        //        $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...
180
181
        $categories = AdslightUtilities::getMyItemIds('adslight_view');
182
        if (is_array($categories) && count($categories) > 0) {
183
            //            $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...
184
            $sql .= ' AND cid IN (' . implode(',', $categories) . ') ';
185
        }
186
187
        $result = $this->db->query($sql);
188
189
        if ($this->db->getRowsNum($result) == 0) {
190
            return $path;
191
        }
192
        list($parentid, $name) = $this->db->fetchRow($result);
193
        $myts = MyTextSanitizer::getInstance();
194
        $name = $myts->htmlSpecialChars($name);
195
        $path = '/' . $name . $path . '';
196
        if ($parentid == 0) {
197
            return $path;
198
        }
199
        $path = $this->getPathFromId($parentid, $title, $path);
200
201
        return $path;
202
    }
203
204
    /**
205
     * @param        $title
206
     * @param string $order
207
     * @param int    $preset_id
208
     * @param int    $none
209
     * @param string $sel_name
210
     * @param string $onchange
211
     */
212
    public function makeMySelBox($title, $order = '', $preset_id = 0, $none = 0, $sel_name = '', $onchange = '')
213
    {
214
        if ($sel_name == '') {
215
            $sel_name = $this->id;
216
        }
217
        $myts = MyTextSanitizer::getInstance();
218
        echo '<select name="' . $sel_name . '"';
219
        if ($onchange != '') {
220
            echo ' onchange="' . $onchange . '"';
221
        }
222
        echo '>';
223
224
        $sql        = 'SELECT SQL_CACHE cid, title FROM ' . $this->table . ' WHERE pid=0';
225
        $categories = AdslightUtilities::getMyItemIds('adslight_submit');
226
227
        if (is_array($categories) && count($categories) > 0) {
228
            $sql .= ' AND cid IN (' . implode(',', $categories) . ') ';
229
        }
230
        if ($order != '') {
231
            $sql .= " ORDER BY $order";
232
        }
233
234
        $result = $this->db->query($sql);
235
        if ($none) {
236
            echo '<option value="0">----</option>';
237
        }
238
        while (list($catid, $name) = $this->db->fetchRow($result)) {
239
            $sel = '';
240
            if ($catid == $preset_id) {
241
                $sel = ' selected';
242
            }
243
            echo '<option value=' . $catid . '' . $sel . '>' . $name . '</option>';
244
            $sel = '';
245
            $arr = $this->getChildTreeArray($catid, $order);
246
            foreach ($arr as $option) {
247
                $option['prefix'] = str_replace('.', '--', $option['prefix']);
248
                $catpath          = $option['prefix'] . '&nbsp;' . $myts->displayTarea($option[$title]);
249
                if ($option['cid'] == $preset_id) {
250
                    $sel = ' selected';
251
                }
252
                echo '<option value="' . $option['cid'] . '"' . $sel . '>' . $catpath . '</option>';
253
                $sel = '';
254
            }
255
        }
256
        echo '</select>';
257
    }
258
259
    /**
260
     * @param        $sel_id
261
     * @param        $title
262
     * @param        $funcURL
263
     * @param string $path
264
     *
265
     * @return string
266
     */
267
    public function getNicePathFromId($sel_id, $title, $funcURL, $path = '')
268
    {
269
        $sql    = 'SELECT SQL_CACHE ' . $this->pid . ', ' . $title . ' FROM ' . $this->table . ' WHERE ' . $this->id . '=' . $this->db->escape($sel_id) . '';
270
        $result = $this->db->query($sql);
271
        if ($this->db->getRowsNum($result) == 0) {
272
            return $path;
273
        }
274
        list($parentid, $name) = $this->db->fetchRow($result);
275
        $myts = MyTextSanitizer::getInstance();
276
        $name = $myts->htmlSpecialChars($name);
277
278
        $arrow = '<img src="' . XOOPS_URL . '/modules/adslight/assets/images/arrow.gif" alt="&raquo;" />';
279
280
        $path = '&nbsp;&nbsp;'
281
                . $arrow
282
                . '&nbsp;&nbsp;<a title="'
283
                . _ADSLIGHT_ANNONCES
284
                . ' '
285
                . $name
286
                . '" href="'
287
                . $funcURL
288
                . ''
289
                . $this->id
290
                . '='
291
                . $this->db->escape($sel_id)
292
                . '">'
293
                . $name
294
                . '</a>'
295
                . $path
296
                . '';
297
298
        if ($parentid == 0) {
299
            return $path;
300
        }
301
        $path = $this->getNicePathFromId($parentid, $title, $funcURL, $path);
302
303
        return $path;
304
    }
305
306
    /**
307
     * @param        $sel_id
308
     * @param string $path
309
     *
310
     * @return string
311
     */
312
    public function getIdPathFromId($sel_id, $path = '')
313
    {
314
        $result = $this->db->query('SELECT SQL_CACHE ' . $this->pid . ' FROM ' . $this->table . ' WHERE ' . $this->id . '=' . $this->db->escape($sel_id));
315
        if ($this->db->getRowsNum($result) == 0) {
316
            return $path;
317
        }
318
        list($parentid) = $this->db->fetchRow($result);
319
        $path = '/' . $sel_id . $path . '';
320
        if ($parentid == 0) {
321
            return $path;
322
        }
323
        $path = $this->getIdPathFromId($parentid, $path);
324
325
        return $path;
326
    }
327
328
    /**
329
     * @param int    $sel_id
330
     * @param string $order
331
     * @param array  $parray
332
     *
333
     * @return array
334
     */
335 View Code Duplication
    public function getAllChild($sel_id = 0, $order = '', $parray = 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...
336
    {
337
        $sql = 'SELECT SQL_CACHE * FROM ' . $this->table . ' WHERE ' . $this->pid . '=' . $this->db->escape($sel_id) . '';
338
339
        $categories = AdslightUtilities::getMyItemIds('adslight_view');
340
        if (is_array($categories) && count($categories) > 0) {
341
            $sql .= ' AND ' . $this->pid . ' IN (' . implode(',', $categories) . ') ';
342
        }
343
344
        if ($order != '') {
345
            $sql .= " ORDER BY {$order}";
346
        }
347
348
        $result = $this->db->query($sql);
349
        $count  = $this->db->getRowsNum($result);
350
        if ($count == 0) {
351
            return $parray;
352
        }
353
        while ($row = $this->db->fetchArray($result)) {
354
            array_push($parray, $row);
355
            $parray = $this->getAllChild($row[$this->id], $order, $parray);
356
        }
357
358
        return $parray;
359
    }
360
361
    /**
362
     * @param int    $sel_id
363
     * @param string $order
364
     * @param array  $parray
365
     * @param string $r_prefix
366
     *
367
     * @return array
368
     */
369
    public function getChildTreeArray($sel_id = 0, $order = '', $parray = array(), $r_prefix = '')
370
    {
371
        global $moduleDirName;
372
373
        $sql = 'SELECT SQL_CACHE * FROM ' . $this->table . ' WHERE ' . $this->pid . '=' . $this->db->escape($sel_id) . '';
374
375
        $categories = AdslightUtilities::getMyItemIds('adslight_view');
376
        if (is_array($categories) && count($categories) > 0) {
377
            $sql .= ' AND cid IN (' . implode(',', $categories) . ') ';
378
        }
379
380
        if ($order != '') {
381
            $sql .= " ORDER BY {$order}";
382
        }
383
        $result = $this->db->query($sql);
384
        $count  = $this->db->getRowsNum($result);
385
        if ($count == 0) {
386
            return $parray;
387
        }
388 View Code Duplication
        while ($row = $this->db->fetchArray($result)) {
389
            $row['prefix'] = $r_prefix . '.';
390
            array_push($parray, $row);
391
            $parray = $this->getChildTreeArray($row[$this->id], $order, $parray, $row['prefix']);
392
        }
393
394
        return $parray;
395
    }
396
397
    /**
398
     * @param        $title
399
     * @param string $order
400
     * @param int    $preset_id
401
     * @param int    $none
402
     * @param string $sel_name
403
     * @param string $onchange
404
     */
405
    public function makeAdSelBox($title, $order = '', $preset_id = 0, $none = 0, $sel_name = '', $onchange = '')
406
    {
407
        global $myts, $xoopsDB, $pathIcon16;
408
        require XOOPS_ROOT_PATH . '/modules/adslight/include/gtickets.php';
409
410
        if ($sel_name == '') {
411
            $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...
412
        }
413
414
        $sql = 'SELECT ' . $this->id . ', ' . $title . ', ordre FROM ' . $this->table . ' WHERE ' . $this->pid . '=0';
415
        if ($order != '') {
416
            $sql .= " ORDER BY {$order}";
417
        }
418
        $result = $xoopsDB->query($sql);
419
        while (list($catid, $name, $ordre) = $xoopsDB->fetchRow($result)) {
420
            echo '<table width="100%" border="0" class="outer"><tr>
421
                <th align="left">';
422
            if ($GLOBALS['xoopsModuleConfig']['adslight_csortorder'] === 'ordre') {
423
                echo '(' . $ordre . ')';
424
            }
425
            echo '&nbsp;&nbsp;'
426
                 . $name
427
                 . '&nbsp;&nbsp;</th>
428
                <th align="center" width="10%"><a href="category.php?op=AdsNewCat&amp;cid='
429
                 . addslashes($catid)
430
                 . '"><img src="'
431
                 . $pathIcon16
432
                 . '/add.png'
433
                 . '" border=0 width=18 height=18 alt="'
434
                 . _AM_ADSLIGHT_ADDSUBCAT
435
                 . '" title="'
436
                 . _AM_ADSLIGHT_ADDSUBCAT
437
                 . '"></a></th>
438
                <th align="center" width="10%"><a href="category.php?op=AdsModCat&amp;cid='
439
                 . addslashes($catid)
440
                 . '"><img src="'
441
                 . $pathIcon16
442
                 . '/edit.png'
443
                 . '" border=0 width=18 height=18 alt="'
444
                 . _AM_ADSLIGHT_MODIFSUBCAT
445
                 . '" title ="'
446
                 . _AM_ADSLIGHT_MODIFSUBCAT
447
                 . '"></a></th>
448
                <th align="center" width="10%"><a href="category.php?op=AdsDelCat&amp;cid='
449
                 . addslashes($catid)
450
                 . '"><img src="'
451
                 . $pathIcon16
452
                 . '/delete.png'
453
                 . '" border=0 width=18 height=18 alt="'
454
                 . _AM_ADSLIGHT_DELSUBCAT
455
                 . '" title="'
456
                 . _AM_ADSLIGHT_DELSUBCAT
457
                 . '"></a></th>
458
                </tr>';
459
460
            $arr   = $this->getChildTreeMapArray($catid, $order);
461
            $class = 'odd';
462
            foreach ($arr as $option) {
463
                echo '<tr class="' . $class . '"><td>';
464
465
                $option['prefix'] = str_replace('.', ' &nbsp;&nbsp;-&nbsp;', $option['prefix']);
466
                $catpath          = $option['prefix'] . '&nbsp;&nbsp;' . $myts->htmlSpecialChars($option[$title]);
467
                $ordreS           = $option['ordre'];
468
                if ($GLOBALS['xoopsModuleConfig']['adslight_csortorder'] === 'ordre') {
469
                    echo '(' . $ordreS . ')';
470
                }
471
                echo ''
472
                     . $catpath
473
                     . '</a></td>
474
                    <td align="center"><a href="category.php?op=AdsNewCat&amp;cid='
475
                     . $option[$this->id]
476
                     . '"><img src="'
477
                     . $pathIcon16
478
                     . '/add.png'
479
                     . '" border=0 width=18 height=18 alt="'
480
                     . _AM_ADSLIGHT_ADDSUBCAT
481
                     . '"title="'
482
                     . _AM_ADSLIGHT_ADDSUBCAT
483
                     . '"></a></td>
484
                    <td align="center"><a href="category.php?op=AdsModCat&amp;cid='
485
                     . $option[$this->id]
486
                     . '"><img src="'
487
                     . $pathIcon16
488
                     . '/edit.png'
489
                     . '" border=0 width=18 height=18 alt="'
490
                     . _AM_ADSLIGHT_MODIFSUBCAT
491
                     . '" title ="'
492
                     . _AM_ADSLIGHT_MODIFSUBCAT
493
                     . '"></a></td>
494
                    <td align="center"><a href="category.php?op=AdsDelCat&amp;cid='
495
                     . $option[$this->id]
496
                     . '"><img src="'
497
                     . $pathIcon16
498
                     . '/delete.png'
499
                     . '" border=0 width=18 height=18 alt="'
500
                     . _AM_ADSLIGHT_DELSUBCAT
501
                     . '" title="'
502
                     . _AM_ADSLIGHT_DELSUBCAT
503
                     . '"></a></td>';
504
505
                $class = ($class === 'even') ? 'odd' : 'even';
506
            }
507
            echo '</td></tr></table><br>';
508
        }
509
    }
510
511
    /**
512
     * @param int    $sel_id
513
     * @param string $order
514
     * @param array  $parray
515
     * @param string $r_prefix
516
     *
517
     * @return array
518
     */
519
    public function getChildTreeMapArray($sel_id = 0, $order = '', $parray = array(), $r_prefix = '')
520
    {
521
        global $xoopsDB;
522
        $sql = 'SELECT SQL_CACHE * FROM ' . $this->table . ' WHERE ' . $this->pid . '=' . $xoopsDB->escape($sel_id) . '';
523
524
        $categories = AdslightUtilities::getMyItemIds('adslight_view');
525
        if (is_array($categories) && count($categories) > 0) {
526
            $sql .= ' AND ' . $this->pid . ' IN (' . implode(',', $categories) . ') ';
527
        }
528
529
        if ($order != '') {
530
            $sql .= " ORDER BY {$order}";
531
        }
532
        $result = $xoopsDB->query($sql);
533
        $count  = $xoopsDB->getRowsNum($result);
534
        if ($count == 0) {
535
            return $parray;
536
        }
537 View Code Duplication
        while ($row = $xoopsDB->fetchArray($result)) {
538
            $row['prefix'] = $r_prefix . '.';
539
            array_push($parray, $row);
540
            $parray = $this->getChildTreeMapArray($row[$this->id], $order, $parray, $row['prefix']);
541
        }
542
543
        return $parray;
544
    }
545
546
    /**
547
     * @return array
548
     */
549
    public function getCategoryList()
550
    {
551
        $result = $this->db->query('SELECT SQL_CACHE cid, pid, title FROM ' . $this->table);
552
        $ret    = array();
553
        $myts   = MyTextSanitizer::getInstance();
554
        while ($myrow = $this->db->fetchArray($result)) {
555
            $ret[$myrow['cid']] = array('title' => $myts->htmlspecialchars($myrow['title']), 'pid' => $myrow['pid']);
556
        }
557
558
        return $ret;
559
    }
560
}
561