/
Column.php
131 lines (108 loc) · 3.23 KB
/
Column.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
<?php
namespace Concrete\Core\Search\Column;
use Concrete\Core\Search\Result\Result;
class Column implements ColumnInterface
{
/** These properties are to be treated as protected. Use the set and get methods instead */
/** @deprecated */
public $columnKey;
/** @deprecated */
public $columnName;
/** @deprecated */
public $sortDirection = 'asc';
/** @deprecated */
public $isSortable;
/** @deprecated */
public $callback;
public function getColumnValue($obj)
{
$callback = $this->getColumnCallback();
if (is_array($callback)) {
// PHP 8.0 only allows static functions with call_user_func
// So institate the callback if its not callable
// (php 8 will return false on is_callable, < less than php 8 will return true)
if (is_string($callback[0]) && !is_callable($callback)) {
return call_user_func([new $callback[0],$callback[1]], $obj);
}
return call_user_func($callback, $obj);
}
return call_user_func([$obj, $callback]);
}
public function getColumnKey()
{
return $this->columnKey;
}
public function getColumnName()
{
return $this->columnName;
}
/**
* @deprecated
*/
public function getColumnDefaultSortDirection()
{
return $this->sortDirection;
}
public function isColumnSortable()
{
return $this->isSortable;
}
public function getColumnCallback()
{
return $this->callback;
}
/**
* @deprecated
*/
public function setColumnDefaultSortDirection($dir)
{
$this->setColumnSortDirection($dir);
}
public function getSortClassName(Result $result)
{
$il = $result->getItemListObject();
return $il->getSortClassName($this->getColumnKey());
}
public function getColumnSortDirection()
{
return $this->sanitizeSortDirection($this->sortDirection);
}
public function setColumnSortDirection($sortDirection)
{
return $this->sortDirection = $this->sanitizeSortDirection($sortDirection);
}
/**
* Normalize a sort direction to "asc" or "desc".
*
* @param $direction
*
* @return string
*/
private function sanitizeSortDirection($direction)
{
return strtolower(trim($direction)) === 'asc' ? 'asc' : 'desc';
}
public function getSortURL(Result $result)
{
$il = $result->getItemListObject();
$dir = $this->getColumnSortDirection();
return $il->getSortURL($this->getColumnKey(), $dir, $result->getBaseURL());
}
public function __construct($key = null, $name = null, $callback = null, $isSortable = true, $sort = 'asc')
{
$this->columnKey = $key;
$this->columnName = $name;
$this->isSortable = $isSortable;
$this->callback = $callback;
$this->sortDirection = $this->sanitizeSortDirection($sort);
}
#[\ReturnTypeWillChange]
public function jsonSerialize()
{
return [
'columnKey' => $this->getColumnKey(),
'isSortable' => $this->isColumnSortable(),
'sortDirection' => $this->getColumnSortDirection(),
];
}
}