Completed
Push — develop ( 5cb106...80f130 )
by Dmytro
13:36
created

MakeTable::setExtra()   A

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 EvolutionCMS\Support;
2
3
use EvolutionCMS\Interfaces\MakeTableInterface;
4
5
/**
6
 * A utility class for presenting a provided array as a table view.  Includes
7
 * support for pagination, sorting by any column, providing optional header arrays,
8
 * providing classes for styling the table, rows, and cells (including alternate
9
 * row styling), as well as adding form controls to each row.
10
 *
11
 * @author Jason Coward <[email protected]> (MODX)
12
 */
13
class MakeTable implements MakeTableInterface
14
{
15
    /**
16
     * @var string
17
     */
18
    public $actionField = '';
19
    /**
20
     * @var string
21
     */
22
    public $cellAction = '';
23
    /**
24
     * @var string
25
     */
26
    public $linkAction = '';
27
    /**
28
     * @var int
29
     */
30
    public $tableWidth = 0;
31
    /**
32
     * @var string
33
     */
34
    public $tableClass = '';
35
    /**
36
     * @var
37
     */
38
    public $tableID;
39
    /**
40
     * @var
41
     */
42
    public $thClass;
43
    /**
44
     * @var string
45
     */
46
    public $rowHeaderClass = '';
47
    /**
48
     * @var string
49
     */
50
    public $columnHeaderClass = '';
51
    /**
52
     * @var string
53
     */
54
    public $rowRegularClass = '';
55
    /**
56
     * @var string
57
     */
58
    public $rowAlternateClass = 'alt';
59
    /**
60
     * @var string
61
     */
62
    public $formName = 'tableForm';
63
    /**
64
     * @var string
65
     */
66
    public $formAction = '[~[*id*]~]';
67
    /**
68
     * @var string
69
     */
70
    public $formElementType = '';
71
    /**
72
     * @var string
73
     */
74
    public $formElementName = '';
75
    /**
76
     * @var string
77
     */
78
    public $rowAlternatingScheme = 'EVEN';
79
    /**
80
     * @var array
81
     */
82
    public $excludeFields = array();
83
    /**
84
     * @var int
85
     */
86
    public $allOption = 0;
87
    /**
88
     * @var
89
     */
90
    public $pageNav;
91
    /**
92
     * @var array
93
     */
94
    public $columnWidths = array();
95
    /**
96
     * @var array
97
     */
98
    public $selectedValues = array();
99
    /**
100
     * @var array
101
     */
102
    public $fieldHeaders = array();
103
    /**
104
     * @var string
105
     */
106
    public $extra = '';
107
108
    /**
109
     * Sets the default link href for all cells in the table.
110
     *
111
     * @param string $value A URL to execute when table cells are clicked.
112
     */
113
    public function setCellAction($value)
114
    {
115
        $this->cellAction = $this->prepareLink($value);
116
    }
117
118
    /**
119
     * Sets the default link href for the text presented in a cell.
120
     *
121
     * @param string $value A URL to execute when text within table cells are clicked.
122
     */
123
    public function setLinkAction($value)
124
    {
125
        $this->linkAction = $this->prepareLink($value);
126
    }
127
128
    /**
129
     * Sets the width attribute of the main HTML TABLE.
130
     *
131
     * @param int $value A valid width attribute for the HTML TABLE tag
132
     */
133
    public function setTableWidth($value)
134
    {
135
        $this->tableWidth = (int)$value;
136
    }
137
138
    /**
139
     * Sets the class attribute of the main HTML TABLE.
140
     *
141
     * @param string $value A class for the main HTML TABLE.
142
     */
143
    public function setTableClass($value)
144
    {
145
        $this->tableClass = $value;
146
    }
147
148
    /**
149
     * Sets the id attribute of the main HTML TABLE.
150
     *
151
     * @param string $value A class for the main HTML TABLE.
152
     */
153
    public function setTableID($value)
154
    {
155
        $this->tableID = $value;
156
    }
157
158
    /**
159
     * Sets the class attribute of the table header row.
160
     *
161
     * @param string $value A class for the table header row.
162
     */
163
    public function setRowHeaderClass($value)
164
    {
165
        $this->rowHeaderClass = $value;
166
    }
167
168
    /**
169
     * Sets the class attribute of the table header row.
170
     *
171
     * @param string $value A class for the table header row.
172
     */
173
    public function setThHeaderClass($value)
174
    {
175
        $this->thClass = $value;
176
    }
177
178
    /**
179
     * Sets the class attribute of the column header row.
180
     *
181
     * @param string $value A class for the column header row.
182
     */
183
    public function setColumnHeaderClass($value)
184
    {
185
        $this->columnHeaderClass = $value;
186
    }
187
188
    /**
189
     * Sets the class attribute of regular table rows.
190
     *
191
     * @param string $value A class for regular table rows.
192
     */
193
    public function setRowRegularClass($value)
194
    {
195
        $this->rowRegularClass = $value;
196
    }
197
198
    /**
199
     * Sets the class attribute of alternate table rows.
200
     *
201
     * @param string $value A class for alternate table rows.
202
     */
203
    public function setRowAlternateClass($value)
204
    {
205
        $this->rowAlternateClass = $value;
206
    }
207
208
    /**
209
     * Sets the type of INPUT form element to be presented as the first column.
210
     *
211
     * @param string $value Indicates the INPUT form element type attribute.
212
     */
213
    public function setFormElementType($value)
214
    {
215
        $this->formElementType = $value;
216
    }
217
218
    /**
219
     * Sets the name of the INPUT form element to be presented as the first column.
220
     *
221
     * @param string $value Indicates the INPUT form element name attribute.
222
     */
223
    public function setFormElementName($value)
224
    {
225
        $this->formElementName = $value;
226
    }
227
228
    /**
229
     * Sets the name of the FORM to wrap the table in when a form element has
230
     * been indicated.
231
     *
232
     * @param string $value Indicates the FORM name attribute.
233
     */
234
    public function setFormName($value)
235
    {
236
        $this->formName = $value;
237
    }
238
239
    /**
240
     * Sets the action of the FORM element.
241
     *
242
     * @param string $value Indicates the FORM action attribute.
243
     */
244
    public function setFormAction($value)
245
    {
246
        $this->formAction = $value;
247
    }
248
249
    /**
250
     * Excludes fields from the table by array key.
251
     *
252
     * @param array $value An Array of field keys to exclude from the table.
253
     */
254
    public function setExcludeFields($value)
255
    {
256
        $this->excludeFields = $value;
257
    }
258
259
    /**
260
     * Sets the table to provide alternate row colors using ODD or EVEN rows
261
     *
262
     * @param string $value 'ODD' or 'EVEN' to indicate the alternate row scheme.
263
     */
264
    public function setRowAlternatingScheme($value)
265
    {
266
        $this->rowAlternatingScheme = $value;
267
    }
268
269
    /**
270
     * Sets the default field value to be used when appending query parameters
271
     * to link actions.
272
     *
273
     * @param string $value The key of the field to add as a query string parameter.
274
     */
275
    public function setActionFieldName($value)
276
    {
277
        $this->actionField = $value;
278
    }
279
280
    /**
281
     * Sets the width attribute of each column in the array.
282
     *
283
     * @param array $value An Array of column widths in the order of the keys in the
0 ignored issues
show
Bug introduced by
There is no parameter named $value. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
284
     *            source table array.
285
     */
286
    public function setColumnWidths($widthArray)
287
    {
288
        $this->columnWidths = $widthArray;
289
    }
290
291
    /**
292
     * An optional array of values that can be preselected when using
293
     *
294
     * @param array $value Indicates the INPUT form element type attribute.
0 ignored issues
show
Bug introduced by
There is no parameter named $value. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
295
     */
296
    public function setSelectedValues($valueArray)
297
    {
298
        $this->selectedValues = $valueArray;
299
    }
300
301
    /**
302
     * Sets extra content to be presented following the table (but within
303
     * the form, if a form is being rendered with the table).
304
     *
305
     * @param string $value A string of additional content.
306
     */
307
    public function setExtra($value)
308
    {
309
        $this->extra = $value;
310
    }
311
312
    /**
313
     * Retrieves the width of a specific table column by index position.
314
     *
315
     * @param int $columnPosition The index of the column to get the width for.
316
     * @return string
317
     */
318
    public function getColumnWidth($columnPosition)
319
    {
320
        $currentWidth = '';
321
        if (is_array($this->columnWidths) && ! empty($this->columnWidths[$columnPosition])) {
322
            $currentWidth = ' width="' . $this->columnWidths[$columnPosition] . '" ';
323
        }
324
325
        return $currentWidth;
326
    }
327
328
    /**
329
     * Determines what class the current row should have applied.
330
     *
331
     * @param int $value The position of the current row being rendered.
0 ignored issues
show
Bug introduced by
There is no parameter named $value. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
332
     * @return string
333
     */
334
    public function determineRowClass($position)
335
    {
336
        switch ($this->rowAlternatingScheme) {
337
            case 'ODD' :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
338
                $modRemainder = 1;
339
                break;
340
            case 'EVEN' :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
341
            default:
342
                $modRemainder = 0;
343
                break;
344
        }
345
        if ($position % 2 == $modRemainder) {
346
            $currentClass = $this->rowRegularClass;
347
        } else {
348
            $currentClass = $this->rowAlternateClass;
349
        }
350
351
        return ' class="' . $currentClass . '"';
352
    }
353
354
    /**
355
     * Generates an onclick action applied to the current cell, to execute
356
     * any specified cell actions.
357
     *
358
     * @param string $value Indicates the INPUT form element type attribute.
0 ignored issues
show
Bug introduced by
There is no parameter named $value. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
359
     * @return string
360
     */
361 View Code Duplication
    public function getCellAction($currentActionFieldValue)
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...
362
    {
363
        $cellAction = '';
364
        if ($this->cellAction) {
365
            $cellAction = ' onClick="javascript:window.location=\'' . $this->cellAction . $this->actionField . '=' . urlencode($currentActionFieldValue) . '\'" ';
366
        }
367
368
        return $cellAction;
369
    }
370
371
    /**
372
     * Generates the cell content, including any specified action fields values.
373
     *
374
     * @param string $currentActionFieldValue The value to be applied to the link action.
375
     * @param string $value The value of the cell.
376
     * @return string
377
     */
378 View Code Duplication
    public function createCellText($currentActionFieldValue, $value)
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...
379
    {
380
        $cell = $value;
381
        if ($this->linkAction) {
382
            $cell = '<a href="' . $this->linkAction . $this->actionField . '=' . urlencode($currentActionFieldValue) . '">' . $cell . '</a>';
383
        }
384
385
        return $cell;
386
    }
387
388
    /**
389
     * Sets an option to generate a check all link when checkbox is indicated
390
     * as the table formElementType.
391
     */
392
    public function setAllOption()
393
    {
394
        $this->allOption = 1;
395
    }
396
397
    /**
398
     * Function to prepare a link generated in the table cell/link actions.
399
     *
400
     * @param string $value Indicates the INPUT form element type attribute.
0 ignored issues
show
Bug introduced by
There is no parameter named $value. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
401
     * @return string
402
     */
403
    public function prepareLink($link)
404
    {
405
        if (strstr($link, '?')) {
406
            $end = '&';
407
        } else {
408
            $end = '?';
409
        }
410
411
        return $link . $end;
412
    }
413
414
    /**
415
     * Generates the table content.
416
     *
417
     * @param array $fieldsArray The associative array representing the table rows
418
     * and columns.
419
     * @param array $fieldHeadersArray An optional array of values for providing
420
     * alternative field headers; this is an associative arrays of keys from
421
     * the $fieldsArray where the values represent the alt heading content
422
     * for each column.
423
     * @return string
424
     */
425
    public function create($fieldsArray, $fieldHeadersArray = array(), $linkpage = "")
0 ignored issues
show
Unused Code introduced by
The parameter $linkpage 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...
426
    {
427
        global $_lang;
428
        $table = '';
429
        $header = '';
430
        if (is_array($fieldsArray)) {
431
            $i = 0;
432
            foreach ($fieldsArray as $fieldName => $fieldValue) {
433
                $table .= "\t<tr" . $this->determineRowClass($i) . ">\n";
434
                $currentActionFieldValue = get_by_key($fieldValue, $this->actionField, '');
435
                if (is_array($this->selectedValues)) {
436
                    $isChecked = array_search($currentActionFieldValue, $this->selectedValues) === false ? 0 : 1;
437
                } else {
438
                    $isChecked = false;
439
                }
440
                $table .= $this->addFormField($currentActionFieldValue, $isChecked);
0 ignored issues
show
Bug introduced by
It seems like $isChecked defined by array_search($currentAct...lues) === false ? 0 : 1 on line 436 can also be of type integer; however, EvolutionCMS\Support\MakeTable::addFormField() does only seem to accept boolean, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
441
                $colPosition = 0;
442
                foreach ($fieldValue as $key => $value) {
443
                    if (!in_array($key, $this->excludeFields)) {
444
                        $table .= "\t\t<td" . $this->getCellAction($currentActionFieldValue) . ">";
445
                        $table .= $this->createCellText($currentActionFieldValue, $value);
446
                        $table .= "</td>\n";
447
                        if ($i == 0) {
448
                            if (empty ($header) && $this->formElementType) {
449
                                $header .= "\t\t<th style=\"width:32px\" " . ($this->thClass ? 'class="' . $this->thClass . '"' : '') . ">" . ($this->allOption ? '<a href="javascript:clickAll()">all</a>' : '') . "</th>\n";
450
                            }
451
                            $headerText = array_key_exists($key, $fieldHeadersArray) ? $fieldHeadersArray[$key] : $key;
452
                            $header .= "\t\t<th" . $this->getColumnWidth($colPosition) . ($this->thClass ? ' class="' . $this->thClass . '" ' : '') . ">" . $headerText . "</th>\n";
453
                        }
454
                        $colPosition++;
455
                    }
456
                }
457
                $i++;
458
                $table .= "\t</tr>\n";
459
            }
460
            $table = "\n" . '<table' . ($this->tableWidth > 0 ? ' width="' . $this->tableWidth . '"' : '') . ($this->tableClass ? ' class="' . $this->tableClass . '"' : '') . ($this->tableID ? ' id="' . $this->tableID . '"' : '') . ">\n" . ($header ? "\t<thead>\n\t<tr class=\"" . $this->rowHeaderClass . "\">\n" . $header . "\t</tr>\n\t</thead>\n" : '') . $table . "</table>\n";
461
            if ($this->formElementType) {
462
                $table = "\n" . '<form id="' . $this->formName . '" name="' . $this->formName . '" action="' . $this->formAction . '" method="POST">' . $table;
463
            }
464
            if (strlen($this->pageNav) > 1) {//changed to display the pagination if exists.
465
                /* commented this part because of cookie
0 ignored issues
show
Unused Code Comprehensibility introduced by
48% 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...
466
                $table .= '<div id="max-display-records" ><select style="display:inline" onchange="javascript:updatePageSize(this[this.selectedIndex].value);">';
467
                $pageSizes= array (10, 25, 50, 100, 250);
468
                for ($i= 0; $i < count($pageSizes); $i ++) {
469
                    $table .= '<option value="'.$pageSizes[$i].'"';
470
                    $table .= MAX_DISPLAY_RECORDS_NUM == $pageSizes[$i] ? ' selected ' : '';
471
                    $table .= '>'.$pageSizes[$i].'</option>';
472
                }
473
474
                $table .= '</select>'.$_lang["pagination_table_perpage"].'</div>';
475
                */
476
                $table .= '<div id="pagination" class="paginate">' . $_lang["pagination_table_gotopage"] . '<ul>' . $this->pageNav . '</ul></div>';
477
                //$table .= '<script language="javascript">function updatePageSize(size){window.location = \''.$this->prepareLink($linkpage).'pageSize=\'+size;}</script>';
0 ignored issues
show
Unused Code Comprehensibility introduced by
42% 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...
478
479
            }
480
            if ($this->allOption) {
481
                $table .= '
482
<script language="javascript">
483
	toggled = 0;
484
	function clickAll() {
485
		myform = document.getElementById("' . $this->formName . '");
486
		for(i=0;i<myform.length;i++) {
487
			if(myform.elements[i].type==\'checkbox\') {
488
				myform.elements[i].checked=(toggled?false:true);
489
			}
490
		}
491
		toggled = (toggled?0:1);
492
	}
493
</script>';
494
            }
495
            if ($this->formElementType) {
496
                if ($this->extra) {
497
                    $table .= "\n" . $this->extra . "\n";
498
                }
499
                $table .= "\n" . '</form>' . "\n";
500
            }
501
502
            return $table;
503
        }
504
505
        return '';
506
    }
507
508
    /**
509
     * Generates optional paging navigation controls for the table.
510
     *
511
     * @param int $numRecords The number of records to show per page.
512
     * @param string $qs An optional query string to be appended to the paging links
513
     * @return void
514
     */
515
    public function createPagingNavigation($numRecords, $qs = '')
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $qs. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
Coding Style introduced by
createPagingNavigation uses the super-global variable $_GET which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
516
    {
517
        global $_lang;
518
        $currentPage = (is_numeric($_GET['page']) ? $_GET['page'] : 1);
519
        $numPages = ceil($numRecords / MAX_DISPLAY_RECORDS_NUM);
520
        $nav = '';
521
        if ($numPages > 1) {
522
            $currentURL = empty($qs) ? '' : '?' . $qs;
523
            if ($currentPage > 6) {
524
                $nav .= $this->createPageLink($currentURL, 1, $_lang["pagination_table_first"]);
525
            }
526
            if ($currentPage != 1) {
527
                $nav .= $this->createPageLink($currentURL, $currentPage - 1, '&lt;&lt;');
528
            }
529
            $offset = -4 + ($currentPage < 5 ? (5 - $currentPage) : 0);
530
            $i = 1;
531
            while ($i < 10 && ($currentPage + $offset <= $numPages)) {
532
                if ($currentPage == $currentPage + $offset) {
533
                    $nav .= $this->createPageLink($currentURL, $currentPage + $offset, $currentPage + $offset, true);
534
                } else {
535
                    $nav .= $this->createPageLink($currentURL, $currentPage + $offset, $currentPage + $offset);
536
                }
537
                $i++;
538
                $offset++;
539
            }
540
            if ($currentPage < $numPages) {
541
                $nav .= $this->createPageLink($currentURL, $currentPage + 1, '&gt;&gt;');
542
            }
543
            if ($currentPage != $numPages) {
544
                $nav .= $this->createPageLink($currentURL, $numPages, $_lang["pagination_table_last"]);
545
            }
546
        }
547
        $this->pageNav = ' ' . $nav;
548
    }
549
550
    /**
551
     * Creates an individual page link for the paging navigation.
552
     *
553
     * @param string $link The link for the page, defaulted to the current document.
554
     * @param int $pageNum The page number of the link.
555
     * @param string $displayText The text of the link.
556
     * @param bool $currentPage Indicates if the link is to the current page.
557
     * @param string $qs And optional query string to be appended to the link.
558
     * @return string
559
     */
560
    public function createPageLink($link = '', $pageNum, $displayText, $currentPage = false, $qs = '')
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $qs. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
Coding Style introduced by
createPageLink uses the super-global variable $_GET which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
Parameters which have default values should be placed at the end.

If you place a parameter with a default value before a parameter with a default value, the default value of the first parameter will never be used as it will always need to be passed anyway:

// $a must always be passed; it's default value is never used.
function someFunction($a = 5, $b) { }
Loading history...
561
    {
562
        $modx = evolutionCMS();
563
        $orderBy = !empty($_GET['orderby']) ? '&orderby=' . $_GET['orderby'] : '';
564
        $orderDir = !empty($_GET['orderdir']) ? '&orderdir=' . $_GET['orderdir'] : '';
565
        if (!empty($qs)) {
566
            $qs = "?$qs";
567
        }
568
        $link = empty($link) ? $modx->makeUrl($modx->documentIdentifier, $modx->documentObject['alias'],
569
            $qs . "page=$pageNum$orderBy$orderDir") : $this->prepareLink($link) . "page=$pageNum";
570
        $nav = '<li' . ($currentPage ? ' class="currentPage"' : '') . '><a' . ($currentPage ? ' class="currentPage"' : '') . ' href="' . $link . '">' . $displayText . '</a></li>' . "\n";
571
572
        return $nav;
573
    }
574
575
    /**
576
     * Adds an INPUT form element column to the table.
577
     *
578
     * @param string $value The value attribute of the element.
579
     * @param bool $isChecked Indicates if the checked attribute should apply to the
580
     * element.
581
     * @return string
582
     */
583
    public function addFormField($value, $isChecked)
584
    {
585
        $field = '';
586
        if ($this->formElementType) {
587
            $checked = $isChecked ? "checked " : "";
588
            $field = "\t\t" . '<td><input type="' . $this->formElementType . '" name="' . ($this->formElementName ? $this->formElementName : $value) . '"  value="' . $value . '" ' . $checked . '/></td>' . "\n";
589
        }
590
591
        return $field;
592
    }
593
594
    /**
595
     * Generates the proper LIMIT clause for queries to retrieve paged results in
596
     * a MakeTable $fieldsArray.
597
     * @return string
598
     */
599
    public function handlePaging()
0 ignored issues
show
Coding Style introduced by
handlePaging uses the super-global variable $_GET which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
600
    {
601
        $offset = (is_numeric($_GET['page']) && $_GET['page'] > 0) ? $_GET['page'] - 1 : 0;
602
        $limitClause = ' LIMIT ' . ($offset * MAX_DISPLAY_RECORDS_NUM) . ', ' . MAX_DISPLAY_RECORDS_NUM;
603
604
        return $limitClause;
605
    }
606
607
    /**
608
     * Generates the SORT BY clause for queries used to retrieve a MakeTable
609
     * $fieldsArray
610
     *
611
     * @param bool $natural_order If true, the results are returned in natural order.
612
     * @return string
613
     */
614
    public function handleSorting($natural_order = false)
0 ignored issues
show
Coding Style introduced by
handleSorting uses the super-global variable $_GET which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style Naming introduced by
The parameter $natural_order is not named in camelCase.

This check marks parameter names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
615
    {
616
        $orderByClause = '';
617
        if ((bool)$natural_order === false) {
618
            $orderby = !empty($_GET['orderby']) ? $_GET['orderby'] : "id";
619
            $orderdir = !empty($_GET['orderdir']) ? $_GET['orderdir'] : "DESC";
620
            $orderByClause = !empty($orderby) ? ' ORDER BY ' . $orderby . ' ' . $orderdir . ' ' : "";
621
        }
622
623
        return $orderByClause;
624
    }
625
626
    /**
627
     * Generates a link to order by a specific $fieldsArray key; use to generate
628
     * sort by links in the MakeTable $fieldHeadingsArray values.
629
     *
630
     * @param string $key The $fieldsArray key for the column to sort by.
631
     * @param string $text The text for the link (e.g. table column header).
632
     * @param string $qs An optional query string to append to the order by link.
633
     * @return string
634
     */
635
    public function prepareOrderByLink($key, $text, $qs = '')
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $qs. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
Coding Style introduced by
prepareOrderByLink uses the super-global variable $_GET which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
636
    {
637
        $modx = evolutionCMS();
638
        if (!empty($_GET['orderdir'])) {
639
            $orderDir = strtolower($_GET['orderdir']) == 'desc' ? '&orderdir=asc' : '&orderdir=desc';
640
        } else {
641
            $orderDir = '&orderdir=asc';
642
        }
643
        if (!empty($qs)) {
644
            if (!strrpos($qs, '&') == strlen($qs) - 1) {
645
                $qs .= '&';
646
            }
647
        }
648
649
        return '<a href="[~' . $modx->documentIdentifier . '~]?' . $qs . 'orderby=' . $key . $orderDir . '">' . $text . '</a>';
650
    }
651
652
}
653