Table::fetchObjects()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php namespace XoopsModules\Smartobject;
2
3
/**
4
 * Contains the classes responsible for displaying a simple table filled with records of SmartObjects
5
 *
6
 * @license    GNU
7
 * @author     marcan <[email protected]>
8
 * @link       http://smartfactory.ca The SmartFactory
9
 * @package    SmartObject
10
 * @subpackage SmartObjectTable
11
 */
12
13
use CriteriaElement;
14
use XoopsModules\Smartobject;
15
16
17
/**
18
 * SmartObjectTable base class
19
 *
20
 * Base class representing a table for displaying SmartObjects
21
 *
22
 * @package SmartObject
23
 * @author  marcan <[email protected]>
24
 * @link    http://smartfactory.ca The SmartFactory
25
 */
26
class Table
27
{
28
    public $_id;
29
    public $_objectHandler;
30
    public $_columns;
31
    public $_criteria;
32
    public $_actions;
33
    public $_objects = false;
34
    public $_aObjects;
35
    public $_custom_actions;
36
    public $_sortsel;
37
    public $_ordersel;
38
    public $_limitsel;
39
    public $_filtersel;
40
    public $_filterseloptions;
41
    public $_filtersel2;
42
    public $_filtersel2options;
43
    public $_filtersel2optionsDefault;
44
45
    public $_tempObject;
46
    public $_tpl;
47
    public $_introButtons;
48
    public $_quickSearch            = false;
49
    public $_actionButtons          = false;
50
    public $_head_css_class         = 'bg3';
51
    public $_hasActions             = false;
52
    public $_userSide               = false;
53
    public $_printerFriendlyPage    = false;
54
    public $_tableHeader            = false;
55
    public $_tableFooter            = false;
56
    public $_showActionsColumnTitle = true;
57
    public $_isTree                 = false;
58
    public $_showFilterAndLimit     = true;
59
    public $_enableColumnsSorting   = true;
60
    public $_customTemplate         = false;
61
    public $_withSelectedActions    = [];
62
63
    /**
64
     * Constructor
65
     *
66
     * @param Smartobject\PersistableObjectHandler $objectHandler {@link Smartobject\SmartPersistableObjectHandler}
67
     * @param \CriteriaElement                     $criteria
68
     * @param array                                $actions       array representing the actions to offer
69
     *
70
     * @param bool                                 $userSide
71
     */
72
    public function __construct(Smartobject\PersistableObjectHandler $objectHandler, \CriteriaElement $criteria = null, $actions = ['edit', 'delete'], $userSide = false) {
73
        $this->_id            = $objectHandler->className;
74
        $this->_objectHandler = $objectHandler;
75
76
        if (!$criteria) {
77
            $criteria = new \CriteriaCompo();
78
        }
79
        $this->_criteria       = $criteria;
80
        $this->_actions        = $actions;
81
        $this->_custom_actions = [];
82
        $this->_userSide       = $userSide;
83
        if ($userSide) {
84
            $this->_head_css_class = 'head';
85
        }
86
    }
87
88
    /**
89
     * @param      $op
90
     * @param bool $caption
91
     * @param bool $text
92
     */
93
    public function addActionButton($op, $caption = false, $text = false)
94
    {
95
        $action                 = [
96
            'op'      => $op,
97
            'caption' => $caption,
98
            'text'    => $text
99
        ];
100
        $this->_actionButtons[] = $action;
101
    }
102
103
    /**
104
     * @param $columnObj
105
     */
106
    public function addColumn($columnObj)
107
    {
108
        $this->_columns[] = $columnObj;
109
    }
110
111
    /**
112
     * @param $name
113
     * @param $location
114
     * @param $value
115
     */
116
    public function addIntroButton($name, $location, $value)
117
    {
118
        $introButton             = [];
119
        $introButton['name']     = $name;
120
        $introButton['location'] = $location;
121
        $introButton['value']    = $value;
122
        $this->_introButtons[]   = $introButton;
123
        unset($introButton);
124
    }
125
126
    public function addPrinterFriendlyLink()
127
    {
128
        $current_urls               = Smartobject\Utility::getCurrentUrls();
129
        $current_url                = $current_urls['full'];
130
        $this->_printerFriendlyPage = $current_url . '&print';
0 ignored issues
show
Documentation Bug introduced by
The property $_printerFriendlyPage was declared of type boolean, but $current_url . '&print' is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
131
    }
132
133
    /**
134
     * @param        $fields
135
     * @param string $caption
136
     */
137
    public function addQuickSearch($fields, $caption = _CO_SOBJECT_QUICK_SEARCH)
138
    {
139
        $this->_quickSearch = ['fields' => $fields, 'caption' => $caption];
0 ignored issues
show
Documentation Bug introduced by
It seems like array('fields' => $fields, 'caption' => $caption) of type array<string,?,{"fields":"?","caption":"string"}> is incompatible with the declared type boolean of property $_quickSearch.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
140
    }
141
142
    /**
143
     * @param $content
144
     */
145
    public function addHeader($content)
146
    {
147
        $this->_tableHeader = $content;
148
    }
149
150
    /**
151
     * @param $content
152
     */
153
    public function addFooter($content)
154
    {
155
        $this->_tableFooter = $content;
156
    }
157
158
    /**
159
     * @param $caption
160
     */
161
    public function addDefaultIntroButton($caption)
162
    {
163
        $this->addIntroButton($this->_objectHandler->_itemname, $this->_objectHandler->_page . '?op=mod', $caption);
164
    }
165
166
    /**
167
     * @param $method
168
     */
169
    public function addCustomAction($method)
170
    {
171
        $this->_custom_actions[] = $method;
172
    }
173
174
    /**
175
     * @param $default_sort
176
     */
177
    public function setDefaultSort($default_sort)
178
    {
179
        $this->_sortsel = $default_sort;
180
    }
181
182
    /**
183
     * @return string
184
     */
185
    public function getDefaultSort()
186
    {
187
        if ($this->_sortsel) {
188
            return Smartobject\Utility::getCookieVar($_SERVER['PHP_SELF'] . '_' . $this->_id . '_sortsel', $this->_sortsel);
189
        } else {
190
            return Smartobject\Utility::getCookieVar($_SERVER['PHP_SELF'] . '_' . $this->_id . '_sortsel', $this->_objectHandler->identifierName);
191
        }
192
    }
193
194
    /**
195
     * @param $default_order
196
     */
197
    public function setDefaultOrder($default_order)
198
    {
199
        $this->_ordersel = $default_order;
200
    }
201
202
    /**
203
     * @return string
204
     */
205 View Code Duplication
    public function getDefaultOrder()
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...
206
    {
207
        if ($this->_ordersel) {
208
            return Smartobject\Utility::getCookieVar($_SERVER['PHP_SELF'] . '_' . $this->_id . '_ordersel', $this->_ordersel);
209
        } else {
210
            return Smartobject\Utility::getCookieVar($_SERVER['PHP_SELF'] . '_' . $this->_id . '_ordersel', 'ASC');
211
        }
212
    }
213
214
    /**
215
     * @param array $actions
216
     */
217
    public function addWithSelectedActions($actions = [])
218
    {
219
        $this->addColumn(new ObjectColumn('checked', 'center', 20, false, false, '&nbsp;'));
0 ignored issues
show
Documentation introduced by
20 is of type integer, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
'&nbsp;' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
220
        $this->_withSelectedActions = $actions;
221
    }
222
223
    /**
224
     * Adding a filter in the table
225
     *
226
     * @param string $key    key to the field that will be used for sorting
227
     * @param string $method method of the handler that will be called to populate the options when this filter is selected
228
     * @param bool   $default
229
     */
230
    public function addFilter($key, $method, $default = false)
231
    {
232
        $this->_filterseloptions[$key]   = $method;
233
        $this->_filtersel2optionsDefault = $default;
234
    }
235
236
    /**
237
     * @param $default_filter
238
     */
239
    public function setDefaultFilter($default_filter)
240
    {
241
        $this->_filtersel = $default_filter;
242
    }
243
244
    public function isForUserSide()
245
    {
246
        $this->_userSide = true;
247
    }
248
249
    /**
250
     * @param $template
251
     */
252
    public function setCustomTemplate($template)
253
    {
254
        $this->_customTemplate = $template;
255
    }
256
257
    public function setSortOrder()
258
    {
259
        $this->_sortsel = isset($_GET[$this->_objectHandler->_itemname . '_' . 'sortsel']) ? $_GET[$this->_objectHandler->_itemname . '_' . 'sortsel'] : $this->getDefaultSort();
260
        //$this->_sortsel = isset($_POST['sortsel']) ? $_POST['sortsel']: $this->_sortsel;
261
        Smartobject\Utility::setCookieVar($_SERVER['PHP_SELF'] . '_' . $this->_id . '_sortsel', $this->_sortsel);
262
        $fieldsForSorting = $this->_tempObject->getFieldsForSorting($this->_sortsel);
0 ignored issues
show
Unused Code introduced by
$fieldsForSorting 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...
263
264
        if (isset($this->_tempObject->vars[$this->_sortsel]['itemName'])
265
            && $this->_tempObject->vars[$this->_sortsel]['itemName']) {
266
            $this->_criteria->setSort($this->_tempObject->vars[$this->_sortsel]['itemName'] . '.' . $this->_sortsel);
267
        } else {
268
            $this->_criteria->setSort($this->_objectHandler->_itemname . '.' . $this->_sortsel);
269
        }
270
271
        $this->_ordersel = isset($_GET[$this->_objectHandler->_itemname . '_' . 'ordersel']) ? $_GET[$this->_objectHandler->_itemname . '_' . 'ordersel'] : $this->getDefaultOrder();
272
        //$this->_ordersel = isset($_POST['ordersel']) ? $_POST['ordersel']:$this->_ordersel;
273
        Smartobject\Utility::setCookieVar($_SERVER['PHP_SELF'] . '_' . $this->_id . '_ordersel', $this->_ordersel);
274
        $ordersArray = $this->getOrdersArray();
0 ignored issues
show
Unused Code introduced by
$ordersArray 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...
275
        $this->_criteria->setOrder($this->_ordersel);
276
    }
277
278
    /**
279
     * @param $id
280
     */
281
    public function setTableId($id)
282
    {
283
        $this->_id = $id;
284
    }
285
286
    /**
287
     * @param $objects
288
     */
289
    public function setObjects($objects)
290
    {
291
        $this->_objects = $objects;
292
    }
293
294
    public function createTableRows()
295
    {
296
        $this->_aObjects = [];
297
298
        $doWeHaveActions = false;
299
300
        $objectclass = 'odd';
301
        if (count($this->_objects) > 0) {
302
            foreach ($this->_objects as $object) {
0 ignored issues
show
Bug introduced by
The expression $this->_objects of type boolean is not traversable.
Loading history...
303
                $aObject = [];
304
305
                $i = 0;
306
307
                $aColumns = [];
308
309
                foreach ($this->_columns as $column) {
310
                    $aColumn = [];
311
312 View Code Duplication
                    if (0 == $i) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
313
                        $class = 'head';
314
                    } elseif (0 == $i % 2) {
315
                        $class = 'even';
316
                    } else {
317
                        $class = 'odd';
318
                    }
319
                    if (method_exists($object, 'initiateCustomFields')) {
320
                        //$object->initiateCustomFields();
321
                    }
322
                    if ('checked' === $column->_keyname) {
323
                        $value = '<input type ="checkbox" name="selected_smartobjects[]" value="' . $object->id() . '">';
324
                    } elseif ($column->_customMethodForValue
325
                              && method_exists($object, $column->_customMethodForValue)) {
326
                        $method = $column->_customMethodForValue;
327
                        if ($column->_param) {
328
                            $value = $object->$method($column->_param);
329
                        } else {
330
                            $value = $object->$method();
331
                        }
332 View Code Duplication
                    } else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
333
                        /**
334
                         * If the column is the identifier, then put a link on it
335
                         */
336
                        if ($column->getKeyName() == $this->_objectHandler->identifierName) {
337
                            $value = $object->getItemLink();
338
                        } else {
339
                            $value = $object->getVar($column->getKeyName());
340
                        }
341
                    }
342
343
                    $aColumn['value'] = $value;
344
                    $aColumn['class'] = $class;
345
                    $aColumn['width'] = $column->getWidth();
346
                    $aColumn['align'] = $column->getAlign();
347
348
                    $aColumns[] = $aColumn;
349
                    ++$i;
350
                }
351
352
                $aObject['columns'] = $aColumns;
353
                $aObject['id']      = $object->id();
354
355
                $objectclass = ('even' === $objectclass) ? 'odd' : 'even';
356
357
                $aObject['class'] = $objectclass;
358
359
                $actions = [];
360
361
                // Adding the custom actions if any
362
                foreach ($this->_custom_actions as $action) {
363
                    if (method_exists($object, $action)) {
364
                        $actions[] = $object->$action();
365
                    }
366
                }
367
368
//                require_once SMARTOBJECT_ROOT_PATH . 'class/smartobjectcontroller.php';
369
                $controller = new ObjectController($this->_objectHandler);
370
371 View Code Duplication
                if ((!is_array($this->_actions)) || in_array('edit', $this->_actions)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
372
                    $actions[] = $controller->getEditItemLink($object, false, true, $this->_userSide);
373
                }
374 View Code Duplication
                if ((!is_array($this->_actions)) || in_array('delete', $this->_actions)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
375
                    $actions[] = $controller->getDeleteItemLink($object, false, true, $this->_userSide);
376
                }
377
                $aObject['actions'] = $actions;
378
379
                $this->_tpl->assign('smartobject_actions_column_width', count($actions) * 30);
380
381
                $doWeHaveActions = $doWeHaveActions ? true : count($actions) > 0;
382
383
                $this->_aObjects[] = $aObject;
384
            }
385
            $this->_tpl->assign('smartobject_objects', $this->_aObjects);
386
        } else {
387
            $colspan = count($this->_columns) + 1;
388
            $this->_tpl->assign('smartobject_colspan', $colspan);
389
        }
390
        $this->_hasActions = $doWeHaveActions;
391
    }
392
393
    /**
394
     * @param  bool $debug
395
     * @return mixed
396
     */
397
    public function fetchObjects($debug = false)
398
    {
399
        return $this->_objectHandler->getObjects($this->_criteria, true, true, false, $debug);
400
    }
401
402
    /**
403
     * @return string
404
     */
405 View Code Duplication
    public function getDefaultFilter()
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...
406
    {
407
        if ($this->_filtersel) {
408
            return Smartobject\Utility::getCookieVar($_SERVER['PHP_SELF'] . '_' . $this->_id . '_filtersel', $this->_filtersel);
409
        } else {
410
            return Smartobject\Utility::getCookieVar($_SERVER['PHP_SELF'] . '_' . $this->_id . '_filtersel', 'default');
411
        }
412
    }
413
414
    /**
415
     * @return array|bool
416
     */
417
    public function getFiltersArray()
418
    {
419
        $ret               = [];
420
        $field             = [];
421
        $field['caption']  = _CO_OBJ_NONE;
422
        $field['selected'] = '';
423
        $ret['default']    = $field;
424
        unset($field);
425
426
        if ($this->_filterseloptions) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->_filterseloptions of type array<string,string> is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
427
            foreach ($this->_filterseloptions as $key => $value) {
428
                $field = [];
429
                if (is_array($value)) {
430
                    $field['caption']  = $key;
431
                    $field['selected'] = $this->_filtersel == $key ? 'selected' : '';
432
                } else {
433
                    $field['caption']  = $this->_tempObject->vars[$key]['form_caption'];
434
                    $field['selected'] = $this->_filtersel == $key ? 'selected' : '';
435
                }
436
                $ret[$key] = $field;
437
                unset($field);
438
            }
439
        } else {
440
            $ret = false;
441
        }
442
443
        return $ret;
444
    }
445
446
    /**
447
     * @param $default_filter2
448
     */
449
    public function setDefaultFilter2($default_filter2)
450
    {
451
        $this->_filtersel2 = $default_filter2;
452
    }
453
454
    /**
455
     * @return string
456
     */
457
    public function getDefaultFilter2()
458
    {
459
        if ($this->_filtersel2) {
460
            return Smartobject\Utility::getCookieVar($_SERVER['PHP_SELF'] . '_filtersel2', $this->_filtersel2);
461
        } else {
462
            return Smartobject\Utility::getCookieVar($_SERVER['PHP_SELF'] . '_filtersel2', 'default');
463
        }
464
    }
465
466
    /**
467
     * @return array
468
     */
469
    public function getFilters2Array()
470
    {
471
        $ret = [];
472
473
        foreach ($this->_filtersel2options as $key => $value) {
474
            $field             = [];
475
            $field['caption']  = $value;
476
            $field['selected'] = $this->_filtersel2 == $key ? 'selected' : '';
477
            $ret[$key]         = $field;
478
            unset($field);
479
        }
480
481
        return $ret;
482
    }
483
484
    /**
485
     * @param $limitsArray
486
     * @param $params_of_the_options_sel
487
     */
488
    public function renderOptionSelection($limitsArray, $params_of_the_options_sel)
0 ignored issues
show
Unused Code introduced by
The parameter $params_of_the_options_sel is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
489
    {
490
        // Rendering the form to select options on the table
491
        $current_urls = Smartobject\Utility::getCurrentUrls();
492
        $current_url  = $current_urls['full'];
493
494
        /**
495
         * What was $params_of_the_options_sel doing again ?
496
         */
497
        //$this->_tpl->assign('smartobject_optionssel_action', $_SERVER['PHP_SELF'] . "?" . implode('&', $params_of_the_options_sel));
498
        $this->_tpl->assign('smartobject_optionssel_action', $current_url);
499
        $this->_tpl->assign('smartobject_optionssel_limitsArray', $limitsArray);
500
    }
501
502
    /**
503
     * @return array
504
     */
505
    public function getLimitsArray()
506
    {
507
        $ret                    = [];
508
        $ret['all']['caption']  = _CO_SOBJECT_LIMIT_ALL;
509
        $ret['all']['selected'] = ('all' === $this->_limitsel) ? 'selected' : '';
510
511
        $ret['5']['caption']  = '5';
512
        $ret['5']['selected'] = ('5' == $this->_limitsel) ? 'selected' : '';
513
514
        $ret['10']['caption']  = '10';
515
        $ret['10']['selected'] = ('10' == $this->_limitsel) ? 'selected' : '';
516
517
        $ret['15']['caption']  = '15';
518
        $ret['15']['selected'] = ('15' == $this->_limitsel) ? 'selected' : '';
519
520
        $ret['20']['caption']  = '20';
521
        $ret['20']['selected'] = ('20' == $this->_limitsel) ? 'selected' : '';
522
523
        $ret['25']['caption']  = '25';
524
        $ret['25']['selected'] = ('25' == $this->_limitsel) ? 'selected' : '';
525
526
        $ret['30']['caption']  = '30';
527
        $ret['30']['selected'] = ('30' == $this->_limitsel) ? 'selected' : '';
528
529
        $ret['35']['caption']  = '35';
530
        $ret['35']['selected'] = ('35' == $this->_limitsel) ? 'selected' : '';
531
532
        $ret['40']['caption']  = '40';
533
        $ret['40']['selected'] = ('40' == $this->_limitsel) ? 'selected' : '';
534
535
        return $ret;
536
    }
537
538
    /**
539
     * @return bool
540
     */
541
    public function getObjects()
542
    {
543
        return $this->_objects;
544
    }
545
546
    public function hideActionColumnTitle()
547
    {
548
        $this->_showActionsColumnTitle = false;
549
    }
550
551
    public function hideFilterAndLimit()
552
    {
553
        $this->_showFilterAndLimit = false;
554
    }
555
556
    /**
557
     * @return array
558
     */
559
    public function getOrdersArray()
560
    {
561
        $ret                    = [];
562
        $ret['ASC']['caption']  = _CO_SOBJECT_SORT_ASC;
563
        $ret['ASC']['selected'] = ('ASC' === $this->_ordersel) ? 'selected' : '';
564
565
        $ret['DESC']['caption']  = _CO_SOBJECT_SORT_DESC;
566
        $ret['DESC']['selected'] = ('DESC' === $this->_ordersel) ? 'selected' : '';
567
568
        return $ret;
569
    }
570
571
    /**
572
     * @return mixed|string|void
573
     */
574
    public function renderD()
575
    {
576
        return $this->render(false, true);
577
    }
578
579
    public function renderForPrint()
580
    {
581
    }
582
583
    /**
584
     * @param  bool $fetchOnly
585
     * @param  bool $debug
586
     * @return mixed|string|void
587
     */
588
    public function render($fetchOnly = false, $debug = false)
589
    {
590
        require_once XOOPS_ROOT_PATH . '/class/template.php';
591
592
        $this->_tpl = new \XoopsTpl();
593
594
        /**
595
         * We need access to the vars of the SmartObject for a few things in the table creation.
596
         * Since we may not have a SmartObject to look into now, let's create one for this purpose
597
         * and we will free it after
598
         */
599
        $this->_tempObject = $this->_objectHandler->create();
600
601
        $this->_criteria->setStart(isset($_GET['start' . $this->_objectHandler->keyName]) ? (int)$_GET['start' . $this->_objectHandler->keyName] : 0);
602
603
        $this->setSortOrder();
604
605
        if (!$this->_isTree) {
606
            $this->_limitsel = isset($_GET['limitsel']) ? $_GET['limitsel'] : Smartobject\Utility::getCookieVar($_SERVER['PHP_SELF'] . '_limitsel', '15');
607
        } else {
608
            $this->_limitsel = 'all';
609
        }
610
611
        $this->_limitsel = isset($_POST['limitsel']) ? $_POST['limitsel'] : $this->_limitsel;
612
        Smartobject\Utility::setCookieVar($_SERVER['PHP_SELF'] . '_limitsel', $this->_limitsel);
613
        $limitsArray = $this->getLimitsArray();
614
        $this->_criteria->setLimit($this->_limitsel);
615
616
        $this->_filtersel = isset($_GET['filtersel']) ? $_GET['filtersel'] : $this->getDefaultFilter();
617
        $this->_filtersel = isset($_POST['filtersel']) ? $_POST['filtersel'] : $this->_filtersel;
618
        Smartobject\Utility::setCookieVar($_SERVER['PHP_SELF'] . '_' . $this->_id . '_filtersel', $this->_filtersel);
619
        $filtersArray = $this->getFiltersArray();
620
621
        if ($filtersArray) {
622
            $this->_tpl->assign('smartobject_optionssel_filtersArray', $filtersArray);
623
        }
624
625
        // Check if the selected filter is defined and if so, create the selfilter2
626
        if (isset($this->_filterseloptions[$this->_filtersel])) {
627
            // check if method associate with this filter exists in the handler
628
            if (is_array($this->_filterseloptions[$this->_filtersel])) {
629
                $filter = $this->_filterseloptions[$this->_filtersel];
630
                $this->_criteria->add($filter['criteria']);
631
            } else {
632
                if (method_exists($this->_objectHandler, $this->_filterseloptions[$this->_filtersel])) {
633
634
                    // then we will create the selfilter2 options by calling this method
635
                    $method                   = $this->_filterseloptions[$this->_filtersel];
636
                    $this->_filtersel2options = $this->_objectHandler->$method();
637
638
                    $this->_filtersel2 = isset($_GET['filtersel2']) ? $_GET['filtersel2'] : $this->getDefaultFilter2();
639
                    $this->_filtersel2 = isset($_POST['filtersel2']) ? $_POST['filtersel2'] : $this->_filtersel2;
640
641
                    $filters2Array = $this->getFilters2Array();
642
                    $this->_tpl->assign('smartobject_optionssel_filters2Array', $filters2Array);
643
644
                    Smartobject\Utility::setCookieVar($_SERVER['PHP_SELF'] . '_filtersel2', $this->_filtersel2);
645
                    if ('default' !== $this->_filtersel2) {
646
                        $this->_criteria->add(new \Criteria($this->_filtersel, $this->_filtersel2));
647
                    }
648
                }
649
            }
650
        }
651
        // Check if we have a quicksearch
652
653
        if (isset($_POST['quicksearch_' . $this->_id]) && '' != $_POST['quicksearch_' . $this->_id]) {
654
            $quicksearch_criteria = new \CriteriaCompo();
655
            if (is_array($this->_quickSearch['fields'])) {
656
                foreach ($this->_quickSearch['fields'] as $v) {
657
                    $quicksearch_criteria->add(new \Criteria($v, '%' . $_POST['quicksearch_' . $this->_id] . '%', 'LIKE'), 'OR');
658
                }
659
            } else {
660
                $quicksearch_criteria->add(new \Criteria($this->_quickSearch['fields'], '%' . $_POST['quicksearch_' . $this->_id] . '%', 'LIKE'));
661
            }
662
            $this->_criteria->add($quicksearch_criteria);
663
        }
664
665
        $this->_objects = $this->fetchObjects($debug);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->fetchObjects($debug) of type array is incompatible with the declared type boolean of property $_objects.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
666
667
        require_once XOOPS_ROOT_PATH . '/class/pagenav.php';
668
        if ($this->_criteria->getLimit() > 0) {
669
670
            /**
671
             * Geeting rid of the old params
672
             * $new_get_array is an array containing the new GET parameters
673
             */
674
            $new_get_array = [];
675
676
            /**
677
             * $params_of_the_options_sel is an array with all the parameters of the page
678
             * but without the pagenave parameters. This array will be used in the
679
             * OptionsSelection
680
             */
681
            $params_of_the_options_sel = [];
682
683
            $not_needed_params = ['sortsel', 'limitsel', 'ordersel', 'start' . $this->_objectHandler->keyName];
684
            foreach ($_GET as $k => $v) {
685
                if (!in_array($k, $not_needed_params)) {
686
                    $new_get_array[]             = "$k=$v";
687
                    $params_of_the_options_sel[] = "$k=$v";
688
                }
689
            }
690
691
            /**
692
             * Adding the new params of the pagenav
693
             */
694
            $new_get_array[] = 'sortsel=' . $this->_sortsel;
695
            $new_get_array[] = 'ordersel=' . $this->_ordersel;
696
            $new_get_array[] = 'limitsel=' . $this->_limitsel;
697
            $otherParams     = implode('&', $new_get_array);
698
699
            $pagenav = new \XoopsPageNav($this->_objectHandler->getCount($this->_criteria), $this->_criteria->getLimit(), $this->_criteria->getStart(), 'start' . $this->_objectHandler->keyName, $otherParams);
700
            $this->_tpl->assign('smartobject_pagenav', $pagenav->renderNav());
701
        }
702
        $this->renderOptionSelection($limitsArray, $params_of_the_options_sel);
0 ignored issues
show
Bug introduced by
The variable $params_of_the_options_sel does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
703
704
        // retreive the current url and the query string
705
        $current_urls = Smartobject\Utility::getCurrentUrls();
706
        $current_url  = $current_urls['full_phpself'];
707
        $query_string = $current_urls['querystring'];
708
        if ($query_string) {
709
            $query_string = str_replace('?', '', $query_string);
710
        }
711
        $query_stringArray     = explode('&', $query_string);
712
        $new_query_stringArray = [];
713
        foreach ($query_stringArray as $query_string) {
714
            if (false === strpos($query_string, 'sortsel') && false === strpos($query_string, 'ordersel')) {
715
                $new_query_stringArray[] = $query_string;
716
            }
717
        }
718
        $new_query_string = implode('&', $new_query_stringArray);
719
720
        $orderArray                     = [];
721
        $orderArray['ASC']['image']     = 'desc.png';
722
        $orderArray['ASC']['neworder']  = 'DESC';
723
        $orderArray['DESC']['image']    = 'asc.png';
724
        $orderArray['DESC']['neworder'] = 'ASC';
725
726
        $aColumns = [];
727
728
        foreach ($this->_columns as $column) {
729
            $qs_param         = '';
730
            $aColumn          = [];
731
            $aColumn['width'] = $column->getWidth();
732
            $aColumn['align'] = $column->getAlign();
733
            $aColumn['key']   = $column->getKeyName();
734
            if ('checked' === $column->_keyname) {
735
                $aColumn['caption'] = '<input type ="checkbox" id="checkall_smartobjects" name="checkall_smartobjects"' . ' value="checkall_smartobjects" onclick="smartobject_checkall(window.document.form_' . $this->_id . ', \'selected_smartobjects\');">';
736
            } elseif ($column->getCustomCaption()) {
737
                $aColumn['caption'] = $column->getCustomCaption();
738
            } else {
739
                $aColumn['caption'] = isset($this->_tempObject->vars[$column->getKeyName()]['form_caption']) ? $this->_tempObject->vars[$column->getKeyName()]['form_caption'] : $column->getKeyName();
740
            }
741
            // Are we doing a GET sort on this column ?
742
            $getSort = (isset($_GET[$this->_objectHandler->_itemname . '_' . 'sortsel'])
743
                        && $_GET[$this->_objectHandler->_itemname . '_' . 'sortsel'] == $column->getKeyName())
744
                       || ($this->_sortsel == $column->getKeyName());
745
            $order   = isset($_GET[$this->_objectHandler->_itemname . '_' . 'ordersel']) ? $_GET[$this->_objectHandler->_itemname . '_' . 'ordersel'] : 'DESC';
746
747
            if (isset($_REQUEST['quicksearch_' . $this->_id]) && '' != $_REQUEST['quicksearch_' . $this->_id]) {
748
                $qs_param = '&quicksearch_' . $this->_id . '=' . $_REQUEST['quicksearch_' . $this->_id];
749
            }
750
            if (!$this->_enableColumnsSorting || 'checked' === $column->_keyname || !$column->isSortable()) {
751
                $aColumn['caption'] = $aColumn['caption'];
752
            } elseif ($getSort) {
753
                $aColumn['caption'] = '<a href="'
754
                                      . $current_url
755
                                      . '?'
756
                                      . $this->_objectHandler->_itemname
757
                                      . '_'
758
                                      . 'sortsel='
759
                                      . $column->getKeyName()
760
                                      . '&'
761
                                      . $this->_objectHandler->_itemname
762
                                      . '_'
763
                                      . 'ordersel='
764
                                      . $orderArray[$order]['neworder']
765
                                      . $qs_param
766
                                      . '&'
767
                                      . $new_query_string
768
                                      . '">'
769
                                      . $aColumn['caption']
770
                                      . ' <img src="'
771
                                      . SMARTOBJECT_IMAGES_ACTIONS_URL
772
                                      . $orderArray[$order]['image']
773
                                      . '" alt="ASC"></a>';
774
            } else {
775
                $aColumn['caption'] = '<a href="' . $current_url . '?' . $this->_objectHandler->_itemname . '_' . 'sortsel=' . $column->getKeyName() . '&' . $this->_objectHandler->_itemname . '_' . 'ordersel=ASC' . $qs_param . '&' . $new_query_string . '">' . $aColumn['caption'] . '</a>';
776
            }
777
            $aColumns[] = $aColumn;
778
        }
779
        $this->_tpl->assign('smartobject_columns', $aColumns);
780
781
        if ($this->_quickSearch) {
782
            $this->_tpl->assign('smartobject_quicksearch', $this->_quickSearch['caption']);
783
        }
784
785
        $this->createTableRows();
786
787
        $this->_tpl->assign('smartobject_showFilterAndLimit', $this->_showFilterAndLimit);
788
        $this->_tpl->assign('smartobject_isTree', $this->_isTree);
789
        $this->_tpl->assign('smartobject_show_action_column_title', $this->_showActionsColumnTitle);
790
        $this->_tpl->assign('smartobject_table_header', $this->_tableHeader);
791
        $this->_tpl->assign('smartobject_table_footer', $this->_tableFooter);
792
        $this->_tpl->assign('smartobject_printer_friendly_page', $this->_printerFriendlyPage);
793
        $this->_tpl->assign('smartobject_user_side', $this->_userSide);
794
        $this->_tpl->assign('smartobject_has_actions', $this->_hasActions);
795
        $this->_tpl->assign('smartobject_head_css_class', $this->_head_css_class);
796
        $this->_tpl->assign('smartobject_actionButtons', $this->_actionButtons);
797
        $this->_tpl->assign('smartobject_introButtons', $this->_introButtons);
798
        $this->_tpl->assign('smartobject_id', $this->_id);
799
        if (!empty($this->_withSelectedActions)) {
800
            $this->_tpl->assign('smartobject_withSelectedActions', $this->_withSelectedActions);
801
        }
802
803
        $smartobjectTable_template = $this->_customTemplate ?: 'smartobject_smarttable_display.tpl';
804
        if ($fetchOnly) {
805
            return $this->_tpl->fetch('db:' . $smartobjectTable_template);
806
        } else {
807
            $this->_tpl->display('db:' . $smartobjectTable_template);
808
        }
809
    }
810
811
    public function disableColumnsSorting()
812
    {
813
        $this->_enableColumnsSorting = false;
814
    }
815
816
    /**
817
     * @param  bool $debug
818
     * @return mixed|string|void
819
     */
820
    public function fetch($debug = false)
821
    {
822
        return $this->render(true, $debug);
823
    }
824
}
825