/
list.php
1160 lines (1005 loc) · 33.6 KB
/
list.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
// Nötige Konstanten
define('REX_LIST_OPT_SORT', 0);
define('REX_LIST_OPT_SORT_DIRECTION', 1);
/*
EXAMPLE:
$list = rex_list::factory('SELECT id,name FROM rex_article');
$list->setColumnFormat('id', 'date');
$list->setColumnLabel('name', 'Artikel-Name');
$list->setColumnSortable('name');
$list->addColumn('testhead','###id### - ###name###',-1);
$list->addColumn('testhead2','testbody2');
$list->setCaption('thomas macht das css');
$list->show();
EXAMPLE USING CUSTOM CALLBACKS WITH setColumnFormat() METHOD:
function callback_func($params)
{
// $params['subject'] current value
// $params['list'] rex_list object
// $params['params'] custom params
return $custom_string; // return value showed in list (note: no htmlspechialchars!)
}
// USING setColumnFormat() BY CALLING A FUNCTION
$list->setColumnFormat('id', // field name
'custom', // format type
'callback_func', // callback function name
array('foo' => 'bar', '123' => '456') // optional params for callback function
);
// USING setColumnFormat() BY CALLING CLASS & METHOD
$list->setColumnFormat('id', // field name
'custom', // format type
array('CLASS','METHOD'), // callback class/method name
array('foo' => 'bar', '123' => '456') // optional params for callback function
);
*/
/**
* Klasse zum erstellen von Listen.
*
* @package redaxo\core
*/
class rex_list implements rex_url_provider_interface
{
use rex_factory_trait;
/**
* @var int
* @psalm-var positive-int
*/
private $db;
/** @var rex_sql */
private $sql;
/** @var bool */
private $debug;
/** @var string */
private $noRowsMessage;
// --------- List Attributes
/** @var string */
private $name;
/** @psalm-var array<string, string|int> */
private $params;
/** @var int */
private $rows;
// --------- Form Attributes
/** @psalm-var array<string, string|int> */
private $formAttributes;
// --------- Column Attributes
/** @psalm-var array<string, string> */
private $customColumns;
/** @psalm-var list<string> */
private $columnNames;
/** @psalm-var array<string, string> */
private $columnLabels;
/** @psalm-var array<string, array{string, mixed, array}> */
private $columnFormates;
/** @psalm-var array<string, array<string|int, mixed>> */
private $columnOptions;
/** @psalm-var array<string, array{string, string}> */
private $columnLayouts;
/** @psalm-var array<string, array> */
private $columnParams;
/** @psalm-var list<string> */
private $columnDisabled;
// --------- Layout, Default
/** @psalm-var array{string, string} */
private $defaultColumnLayout;
// --------- Table Attributes
/** @var string */
private $caption;
/** @psalm-var array<string, string|int> */
private $tableAttributes;
/** @var array<int, array> */
private $tableColumnGroups;
// --------- Link Attributes
/** @psalm-var array<string, array<string, string|int>> */
private $linkAttributes;
// --------- Pagination Attributes
/** @var rex_pager */
private $pager;
/**
* Erstellt ein rex_list Objekt.
*
* @param string $query SELECT Statement
* @param int $rowsPerPage Anzahl der Elemente pro Zeile
* @param string|null $listName Name der Liste
* @param bool $debug
* @param int $db
*
* @psalm-param positive-int $db
*/
protected function __construct($query, $rowsPerPage = 30, $listName = null, $debug = false, $db = 1)
{
// --------- Validation
if (!$listName) {
// use a hopefully unique (per page) hash
$listName = substr(md5($query), 0, 8);
}
// --------- List Attributes
$this->db = $db;
$this->sql = rex_sql::factory($db);
$this->debug = $debug;
$this->sql->setDebug($this->debug);
$this->name = $listName;
$this->caption = '';
$this->rows = 0;
$this->params = [];
$this->tableAttributes = [];
$this->noRowsMessage = rex_i18n::msg('list_no_rows');
// --------- Form Attributes
$this->formAttributes = [];
// --------- Column Attributes
$this->customColumns = [];
$this->columnLabels = [];
$this->columnFormates = [];
$this->columnParams = [];
$this->columnOptions = [];
$this->columnLayouts = [];
$this->columnDisabled = [];
// --------- Default
$this->defaultColumnLayout = ['<th>###VALUE###</th>', '<td data-title="###LABEL###">###VALUE###</td>'];
// --------- Table Attributes
$this->tableAttributes = [];
$this->tableColumnGroups = [];
// --------- Link Attributes
$this->linkAttributes = [];
// --------- Pagination Attributes
$cursorName = $listName .'_start';
if (null === rex_request($cursorName, 'int', null) && rex_request('start', 'int')) {
// BC: Fallback to "start"
$cursorName = 'start';
}
$this->pager = new rex_pager($rowsPerPage, $cursorName);
// --------- Load Data, Row-Count
$this->sql->setQuery($this->prepareQuery($query));
$sql = rex_sql::factory($db);
$sql->setQuery('SELECT FOUND_ROWS() as '. $sql->escapeIdentifier('rows'));
$this->rows = $sql->getValue('rows');
$this->pager->setRowCount($this->rows);
foreach ($this->sql->getFieldnames() as $columnName) {
$this->columnNames[] = $columnName;
}
// --------- Load Env
if (rex::isBackend()) {
$this->loadBackendConfig();
}
$this->init();
}
/**
* @param string $query
* @param int $rowsPerPage
* @param string|null $listName
* @param bool $debug
* @param int $db DB connection ID
*
* @psalm-var positive-int $db
*
* @return static
*/
public static function factory($query, $rowsPerPage = 30, $listName = null, $debug = false, $db = 1)
{
$class = static::getFactoryClass();
return new $class($query, $rowsPerPage, $listName, $debug, $db);
}
public function init()
{
// nichts tun
}
// ---------------------- setters/getters
/**
* Gibt den Namen es Formulars zurück.
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Gibt eine Status Nachricht zurück.
*
* @return string
*/
public function getMessage()
{
return rex_escape(rex_request($this->getName() . '_msg', 'string'));
}
/**
* Gibt eine Warnung zurück.
*
* @return string
*/
public function getWarning()
{
return rex_escape(rex_request($this->getName() . '_warning', 'string'));
}
/**
* Setzt die Caption/den Titel der Tabelle
* Gibt den Namen es Formulars zurück.
*
* @param string $caption Caption/Titel der Tabelle
*/
public function setCaption($caption)
{
$this->caption = $caption;
}
/**
* Gibt die Caption/den Titel der Tabelle zurück.
*
* @return string
*/
public function getCaption()
{
return $this->caption;
}
/**
* @param string $message
*/
public function setNoRowsMessage($message)
{
$this->noRowsMessage = $message;
}
public function getNoRowsMessage()
{
return $this->noRowsMessage;
}
/**
* @param string $name
* @param string|int $value
*/
public function addParam($name, $value)
{
$this->params[$name] = $value;
}
/**
* @return array<string, string|int>
*/
public function getParams()
{
return $this->params;
}
protected function loadBackendConfig()
{
$this->addParam('page', rex_be_controller::getCurrentPage());
}
/**
* @param string $name
* @param string|int $value
*/
public function addTableAttribute($name, $value)
{
$this->tableAttributes[$name] = $value;
}
public function getTableAttributes()
{
return $this->tableAttributes;
}
/**
* @param string $name
* @param string|int $value
*/
public function addFormAttribute($name, $value)
{
$this->formAttributes[$name] = $value;
}
/**
* @return array<string, string|int>
*/
public function getFormAttributes()
{
return $this->formAttributes;
}
public function addLinkAttribute($columnName, $attrName, $attrValue)
{
$this->linkAttributes[$columnName][$attrName] = $attrValue;
}
public function getLinkAttributes($column, $default = null)
{
return $this->linkAttributes[$column] ?? $default;
}
// ---------------------- Column setters/getters/etc
/**
* Methode, um eine Spalte einzufügen.
*
* @param string $columnHead Titel der Spalte
* @param string $columnBody Text/Format der Spalte
* @param int $columnIndex Stelle, an der die neue Spalte erscheinen soll
* @param array $columnLayout Layout der Spalte
*/
public function addColumn($columnHead, $columnBody, $columnIndex = -1, $columnLayout = null)
{
// Bei negativem columnIndex, das Element am Ende anfügen
if ($columnIndex < 0) {
$columnIndex = count($this->columnNames);
}
array_splice($this->columnNames, $columnIndex, 0, [$columnHead]);
$this->customColumns[$columnHead] = $columnBody;
$this->setColumnLayout($columnHead, $columnLayout);
}
/**
* Entfernt eine Spalte aus der Anzeige.
*
* @param string $columnName Name der Spalte
*/
public function removeColumn($columnName)
{
$this->columnDisabled[] = $columnName;
}
/**
* Methode, um das Layout einer Spalte zu setzen.
*
* @param string $columnHead Titel der Spalte
* @param array $columnLayout Layout der Spalte
*/
public function setColumnLayout($columnHead, $columnLayout)
{
$this->columnLayouts[$columnHead] = $columnLayout;
}
/**
* Gibt das Layout einer Spalte zurück.
*
* @param string $columnName Name der Spalte
*
* @return array
*/
public function getColumnLayout($columnName)
{
if (isset($this->columnLayouts[$columnName]) && is_array($this->columnLayouts[$columnName])) {
return $this->columnLayouts[$columnName];
}
return $this->defaultColumnLayout;
}
/**
* Gibt die Layouts aller Spalten zurück.
*/
public function getColumnLayouts()
{
return $this->columnLayouts;
}
/**
* Gibt den Namen einer Spalte zurück.
*
* @param int $columnIndex Nummer der Spalte
* @param mixed $default Defaultrückgabewert, falls keine Spalte mit der angegebenen Nummer vorhanden ist
*
* @return string|null
*/
public function getColumnName($columnIndex, $default = null)
{
if (isset($this->columnNames[$columnIndex])) {
return $this->columnNames[$columnIndex];
}
return $default;
}
/**
* Gibt alle Namen der Spalten als Array zurück.
*
* @return array
*/
public function getColumnNames()
{
return $this->columnNames;
}
/**
* Setzt ein Label für eine Spalte.
*
* @param string $columnName Name der Spalte
* @param string $label Label für die Spalte
*/
public function setColumnLabel($columnName, $label)
{
$this->columnLabels[$columnName] = $label;
}
/**
* Gibt das Label der Spalte zurück, falls gesetzt.
*
* Falls nicht vorhanden und der Parameter $default auf null steht,
* wird der Spaltenname zurückgegeben
*
* @param string $columnName Name der Spalte
* @param mixed $default Defaultrückgabewert, falls kein Label gesetzt ist
*
* @return string|null
*
* @template T as null|string
* @phpstan-template T
* @psalm-param T $default
* @psalm-return (T is null ? string : ?string)
*/
public function getColumnLabel($columnName, $default = null)
{
if (isset($this->columnLabels[$columnName])) {
return $this->columnLabels[$columnName];
}
return null === $default ? $columnName : $default;
}
/**
* Setzt ein Format für die Spalte.
*
* @param string $columnName Name der Spalte
* @param string $formatType Formatierungstyp
* @param mixed $format Zu verwendentes Format
* @param array $params Custom params für callback func bei format_type 'custom'
*/
public function setColumnFormat($columnName, $formatType, $format = '', array $params = [])
{
$this->columnFormates[$columnName] = [$formatType, $format, $params];
}
/**
* Gibt das Format für eine Spalte zurück.
*
* @param string $columnName Name der Spalte
* @param mixed $default Defaultrückgabewert, falls keine Formatierung gesetzt ist
*
* @return array|null
*/
public function getColumnFormat($columnName, $default = null)
{
if (isset($this->columnFormates[$columnName])) {
return $this->columnFormates[$columnName];
}
return $default;
}
/**
* Markiert eine Spalte als sortierbar.
*
* @param string $columnName Name der Spalte
* @param string $direction Startsortierrichtung der Spalte [ASC|DESC]
*/
public function setColumnSortable($columnName, $direction = 'asc')
{
$this->setColumnOption($columnName, REX_LIST_OPT_SORT, true);
$this->setColumnOption($columnName, REX_LIST_OPT_SORT_DIRECTION, strtolower($direction));
}
/**
* Setzt eine Option für eine Spalte
* (z.b. Sortable,..).
*
* @param string $columnName Name der Spalte
* @param string|int $option Name/Id der Option
* @param mixed $value Wert der Option
*/
public function setColumnOption($columnName, $option, $value)
{
$this->columnOptions[$columnName][$option] = $value;
}
/**
* Gibt den Wert einer Option für eine Spalte zurück.
*
* @param string $columnName Name der Spalte
* @param string|int $option Name/Id der Option
* @param mixed $default Defaultrückgabewert, falls die Option nicht gesetzt ist
*
* @return mixed|null
*/
public function getColumnOption($columnName, $option, $default = null)
{
if ($this->hasColumnOption($columnName, $option)) {
return $this->columnOptions[$columnName][$option];
}
return $default;
}
/**
* Gibt zurück, ob für eine Spalte eine Option gesetzt wurde.
*
* @param string $columnName Name der Spalte
* @param string|int $option Name/Id der Option
*
* @return bool
*/
public function hasColumnOption($columnName, $option)
{
return isset($this->columnOptions[$columnName][$option]);
}
/**
* Verlinkt eine Spalte mit den übergebenen Parametern.
*
* @param string $columnName Name der Spalte
* @param array $params Array von Parametern
*/
public function setColumnParams($columnName, array $params = [])
{
$this->columnParams[$columnName] = $params;
}
/**
* Gibt die Parameter für eine Spalte zurück.
*
* @param string $columnName Name der Spalte
*
* @return array
*/
public function getColumnParams($columnName)
{
if (isset($this->columnParams[$columnName]) && is_array($this->columnParams[$columnName])) {
return $this->columnParams[$columnName];
}
return [];
}
/**
* Gibt zurück, ob Parameter für eine Spalte existieren.
*
* @param string $columnName Name der Spalte
*
* @return bool
*/
public function hasColumnParams($columnName)
{
return isset($this->columnParams[$columnName]) && is_array($this->columnParams[$columnName]) && count($this->columnParams[$columnName]) > 0;
}
// ---------------------- TableColumnGroup setters/getters/etc
/**
* Methode um eine Colgroup einzufügen.
*
* Beispiel 1:
*
* $list->addTableColumnGroup([40, '*', 240, 140]);
*
* Beispiel 2:
*
* $list->addTableColumnGroup([
* ['width' => 40],
* ['width' => 140, 'span' => 2],
* ['width' => 240]
* ]);
*
* Beispiel 3:
*
* $list->addTableColumnGroup([
* ['class' => 'classname-a'],
* ['class' => 'classname-b'],
* ['class' => 'classname-c']
* ]);
*
* @param array $columns Array von Spalten
* @param int $columnGroupSpan Span der Columngroup
*/
public function addTableColumnGroup(array $columns, $columnGroupSpan = null)
{
$tableColumnGroup = ['columns' => []];
if ($columnGroupSpan) {
$tableColumnGroup['span'] = $columnGroupSpan;
}
$this->tableColumnGroups[] = $tableColumnGroup;
foreach ($columns as $column) {
if (is_array($column)) {
$this->addTableColumn($column['width'] ?? null, $column['span'] ?? null, $column['class'] ?? null);
} else {
$this->addTableColumn($column);
}
}
}
/**
* @return array
*/
public function getTableColumnGroups()
{
return $this->tableColumnGroups;
}
/**
* Fügt der zuletzte eingefügten TableColumnGroup eine weitere Spalte hinzu.
*
* @param int|'*' $width Breite der Spalte
* @param int $span Span der Spalte
*/
public function addTableColumn($width, $span = null, $class = null)
{
$tableColumn = [];
if (is_numeric($width)) {
$width = $width . 'px';
}
if ($width && '*' != $width) {
$tableColumn['style'] = 'width:' . $width;
}
if ($span) {
$tableColumn['span'] = $span;
}
if ($class) {
$tableColumn['class'] = $class;
}
$lastIndex = count($this->tableColumnGroups) - 1;
if ($lastIndex < 0) {
// Falls noch keine TableColumnGroup vorhanden, eine leere anlegen!
$this->addTableColumnGroup([]);
++$lastIndex;
}
$groupColumns = $this->tableColumnGroups[$lastIndex]['columns'];
$groupColumns[] = $tableColumn;
$this->tableColumnGroups[$lastIndex]['columns'] = $groupColumns;
}
// ---------------------- Url generation
/**
* {@inheritdoc}
*/
public function getUrl(array $params = [], $escape = true)
{
$params = array_merge($this->getParams(), $params);
$params['list'] = $this->getName();
if (!isset($params['sort'])) {
$sortColumn = $this->getSortColumn();
if (null != $sortColumn) {
$params['sort'] = $sortColumn;
$params['sorttype'] = $this->getSortType();
}
}
$_params = [];
foreach ($params as $name => $value) {
if (is_array($value)) {
foreach ($value as $v) {
$_params[$name] = $v;
}
} else {
$_params[$name] = $value;
}
}
return rex::isBackend() ? rex_url::backendController($_params, $escape) : rex_url::frontendController($_params, $escape);
}
/**
* Gibt eine Url zurück, die die Parameter $params enthält
* Dieser Url werden die Standard rexList Variablen zugefügt.
*
* Innerhalb dieser Url werden variablen ersetzt
*
* @see #replaceVariable, #replaceVariables
*
* @param array $params
* @param bool $escape Flag whether the argument separator "&" should be escaped (&)
*
* @return string
*/
public function getParsedUrl($params = [], $escape = true)
{
$params = array_merge($this->getParams(), $params);
$params['list'] = $this->getName();
if (!isset($params['sort'])) {
$sortColumn = $this->getSortColumn();
if (null != $sortColumn) {
$params['sort'] = $sortColumn;
$params['sorttype'] = $this->getSortType();
}
}
$_params = [];
foreach ($params as $name => $value) {
if (is_array($value)) {
foreach ($value as $v) {
$_params[$name] = $this->replaceVariables($v);
}
} else {
$_params[$name] = $this->replaceVariables((string) $value);
}
}
return rex::isBackend() ? rex_url::backendController($_params, $escape) : rex_url::frontendController($_params, $escape);
}
// ---------------------- Pagination
/**
* Prepariert das SQL Statement vorm anzeigen der Liste.
*
* @param string $query SQL Statement
*
* @return string
*/
protected function prepareQuery($query)
{
$rowsPerPage = $this->pager->getRowsPerPage();
$startRow = $this->pager->getCursor();
// prepare query for fast rowcount calculation
$query = preg_replace('/^\s*SELECT/i', 'SELECT SQL_CALC_FOUND_ROWS', $query, 1);
$sortColumn = $this->getSortColumn();
if ('' != $sortColumn) {
$sortType = $this->getSortType();
$sql = rex_sql::factory($this->db);
$sortColumn = $sql->escapeIdentifier($sortColumn);
if (false === stripos($query, ' ORDER BY ')) {
$query .= ' ORDER BY ' . $sortColumn . ' ' . $sortType;
} else {
$query = preg_replace('/ORDER\sBY\s[^ ]*(\sasc|\sdesc)?/i', 'ORDER BY ' . $sortColumn . ' ' . $sortType, $query);
}
}
if (false === stripos($query, ' LIMIT ')) {
$query .= ' LIMIT ' . $startRow . ',' . $rowsPerPage;
}
return $query;
}
/**
* Gibt die Anzahl der Zeilen zurück, welche vom ursprüngliche SQL Statement betroffen werden.
*
* @return int
*/
public function getRows()
{
return $this->rows;
}
/**
* Returns the pager for this list.
*
* @return rex_pager
*/
public function getPager()
{
return $this->pager;
}
/**
* Gibt zurück, nach welcher Spalte sortiert werden soll.
*
* @param string|null $default
*
* @return string|null
*/
public function getSortColumn($default = null)
{
if (rex_request('list', 'string') == $this->getName()) {
return rex_request('sort', 'string', $default);
}
return $default;
}
/**
* Gibt zurück, in welcher Art und Weise sortiert werden soll (ASC/DESC).
*
* @param 'asc'|'desc'|null $default
*
* @return string|null
*
* @psalm-taint-escape html
* @psalm-taint-escape sql
*/
public function getSortType($default = null)
{
if (rex_request('list', 'string') == $this->getName()) {
$sortType = strtolower(rex_request('sorttype', 'string'));
if (in_array($sortType, ['asc', 'desc'], true)) {
return $sortType;
}
}
if (null === $default) {
return null;
}
$default = strtolower($default);
if (!in_array($default, ['asc', 'desc'], true)) {
throw new InvalidArgumentException('Default sort type must be "asc", "desc" or null, but "'.$default.'" given');
}
return $default;
}
/**
* Gibt die Navigation der Liste zurück.
*
* @return string
*/
protected function getPagination()
{
$fragment = new rex_fragment();
$fragment->setVar('urlprovider', $this);
$fragment->setVar('pager', $this->pager);
return $fragment->parse('core/navigations/pagination.php');
}
/**
* Gibt den Footer der Liste zurück.
*
* @return string
*/
public function getFooter()
{
$s = '';
/*
$s .= ' <tr>'. "\n";
$s .= ' <td colspan="'. count($this->getColumnNames()) .'"><input type="text" name="items" value="'. $this->getRowsPerPage() .'" maxlength="2" /><input type="submit" value="Anzeigen" /></td>'. "\n";
$s .= ' </tr>'. "\n";
*/
return $s;
}
/**
* Gibt den Header der Liste zurück.
*
* @return string
*/
public function getHeader()
{
$s = '';
$s .= $this->getPagination();
return $s;
}
// ---------------------- Generate Output
/**
* @return string
*/
public function replaceVariable($string, $varname)
{
return str_replace('###' . $varname . '###', rex_escape($this->getValue($varname)), $string);
}
/**
* Ersetzt alle Variablen im Format ###<Spaltenname>###.
*
* @param string $value Zu durchsuchender String
*
* @return string
*
* @psalm-taint-specialize
*/
public function replaceVariables($value)
{
if (false === strpos($value, '###')) {
return $value;
}
$columnNames = $this->getColumnNames();
if (is_array($columnNames)) {
foreach ($columnNames as $columnName) {
// Spalten, die mit addColumn eingefügt wurden
if (isset($this->customColumns[$columnName])) {
continue;
}
$value = $this->replaceVariable($value, $columnName);
}
}
return $value;
}
/**
* @return bool
*/
public function isCustomFormat($format)
{
return is_array($format) && isset($format[0]) && 'custom' == $format[0];
}
/**
* Formatiert einen übergebenen String anhand der rexFormatter Klasse.
*
* @param string $value Zu formatierender String
* @param null|array $format mit den Formatierungsinformationen
* @param bool $escape Flag, Ob escapen von $value erlaubt ist
* @param string $field
*
* @return string
*/
public function formatValue($value, $format, $escape, $field = null)
{
if (is_array($format)) {
// Callbackfunktion -> Parameterliste aufbauen
if ($this->isCustomFormat($format)) {
$format[2] = $format[2] ?? [];
$format[1] = [$format[1], ['list' => $this, 'field' => $field, 'value' => $value, 'format' => $format[0], 'escape' => $escape, 'params' => $format[2]]];
}
$value = rex_formatter::format($value, $format[0], $format[1]);
}
// Nur escapen, wenn formatter aufgerufen wird, der kein html zurückgeben können soll
if ($escape && (!isset($format[0]) || !in_array($format[0], ['custom', 'email', 'url'], true))) {
$value = rex_escape($value);
}
return $value;
}
/**
* @return string
*/
protected function _getAttributeString($array)
{
$s = '';
foreach ($array as $name => $value) {
$s .= ' ' . rex_escape($name, 'html_attr') . '="' . rex_escape($value) . '"';
}
return $s;
}