-
Notifications
You must be signed in to change notification settings - Fork 451
/
ItemList.php
129 lines (112 loc) · 3.29 KB
/
ItemList.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
<?php
namespace Concrete\Core\Search\ItemList\Database;
use Concrete\Core\Search\Column\Set;
use Concrete\Core\Search\ItemList\Column;
use Concrete\Core\Search\StickyRequest;
use Database;
use Concrete\Core\Search\ItemList\ItemList as AbstractItemList;
use Doctrine\DBAL\Logging\EchoSQLLogger;
use Doctrine\DBAL\Query\QueryBuilder;
abstract class ItemList extends AbstractItemList
{
/** @var \Doctrine\DBAL\Query\QueryBuilder */
protected $query;
/** @var \Concrete\Core\Search\StickyRequest | null */
protected $searchRequest;
abstract public function createQuery();
/**
* @param \Doctrine\DBAL\Query\QueryBuilder $query
*
* @return \Doctrine\DBAL\Query\QueryBuilder
*/
public function finalizeQuery(\Doctrine\DBAL\Query\QueryBuilder $query)
{
return $query;
}
public function __construct(StickyRequest $req = null)
{
$this->query = Database::get()->createQueryBuilder();
$this->searchRequest = $req;
$this->createQuery();
}
/**
* @return StickyRequest|null
*/
public function getSearchRequest()
{
return $this->searchRequest;
}
public function getQueryObject()
{
return $this->query;
}
public function deliverQueryObject()
{
// setup the default sorting based on the request.
$this->performAutomaticSorting($this->searchRequest);
$query = clone $this->query;
$query = $this->finalizeQuery($query);
return $query;
}
public function executeGetResults()
{
return $this->deliverQueryObject()->execute()->fetchAll();
}
public function debugStart()
{
if ($this->isDebugged()) {
Database::get()->getConfiguration()->setSQLLogger(new EchoSQLLogger());
}
}
public function debugStop()
{
if ($this->isDebugged()) {
Database::get()->getConfiguration()->setSQLLogger(null);
}
}
protected function executeSortBy($column, $direction = 'asc')
{
if (in_array(strtolower($direction), array('asc', 'desc'))) {
$this->query->orderBy($column, $direction);
$this->ensureSelected($column);
}
}
protected function executeSanitizedSortBy($column, $direction = 'asc')
{
if (preg_match('/[^0-9a-zA-Z\$\.\_\x{0080}-\x{ffff}]+/u', $column) === 0) {
$this->executeSortBy($column, $direction);
}
}
/**
* @deprecated
*/
public function filter($field, $value, $comparison = '=')
{
if ($field == false) {
$this->query->andWhere($value); // ugh
} else {
$this->query->andWhere(implode(' ', array(
$field, $comparison, $this->query->createNamedParameter($value),
)));
}
}
protected function ensureSelected($field)
{
$rx = '/\b' . preg_quote($field, '/') . '\b/i';
$selects = $this->query->getQueryPart('select');
$add = true;
foreach ($selects as $select) {
if (preg_match($rx, $select)) {
$add = false;
break;
}
}
if ($add) {
$this->query->addSelect($field);
}
}
public function __clone()
{
$this->query = clone $this->query;
}
}