|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Licensed under The GPL-3.0 License |
|
4
|
|
|
* For full copyright and license information, please see the LICENSE.txt |
|
5
|
|
|
* Redistributions of files must retain the above copyright notice. |
|
6
|
|
|
* |
|
7
|
|
|
* @since 2.0.0 |
|
8
|
|
|
* @author Christopher Castro <[email protected]> |
|
9
|
|
|
* @link http://www.quickappscms.org |
|
10
|
|
|
* @license http://opensource.org/licenses/gpl-3.0.html GPL-3.0 License |
|
11
|
|
|
*/ |
|
12
|
|
|
namespace Eav\Model\Behavior\QueryScope; |
|
13
|
|
|
|
|
14
|
|
|
use Cake\Database\ExpressionInterface; |
|
15
|
|
|
use Cake\Database\Expression\Comparison; |
|
16
|
|
|
use Cake\Database\Expression\IdentifierExpression; |
|
17
|
|
|
use Cake\Database\Expression\UnaryExpression; |
|
18
|
|
|
use Cake\ORM\Query; |
|
19
|
|
|
use Cake\ORM\Table; |
|
20
|
|
|
use Cake\ORM\TableRegistry; |
|
21
|
|
|
use Eav\Model\Behavior\EavToolbox; |
|
22
|
|
|
use Eav\Model\Behavior\QueryScope\QueryScopeInterface; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Used by EAV Behavior to scope WHERE statements. |
|
26
|
|
|
*/ |
|
27
|
|
|
class WhereScope implements QueryScopeInterface |
|
28
|
|
|
{ |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* The table being managed. |
|
32
|
|
|
* |
|
33
|
|
|
* @var \Cake\ORM\Table |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $_table = null; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Instance of toolbox. |
|
39
|
|
|
* |
|
40
|
|
|
* @var \Eav\Model\Behavior\EavToolbox |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $_toolbox = null; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* {@inheritDoc} |
|
46
|
|
|
*/ |
|
47
|
|
|
public function __construct(Table $table) |
|
48
|
|
|
{ |
|
49
|
|
|
$this->_table = $table; |
|
50
|
|
|
$this->_toolbox = new EavToolbox($table); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* {@inheritDoc} |
|
55
|
|
|
* |
|
56
|
|
|
* Look for virtual columns in query's WHERE clause. |
|
57
|
|
|
* |
|
58
|
|
|
* @param \Cake\ORM\Query $query The query to scope |
|
59
|
|
|
* @param string|null $bundle Consider attributes only for a specific bundle |
|
60
|
|
|
* @return \Cake\ORM\Query The modified query object |
|
61
|
|
|
*/ |
|
62
|
|
|
public function scope(Query $query, $bundle = null) |
|
63
|
|
|
{ |
|
64
|
|
|
$whereClause = $query->clause('where'); |
|
65
|
|
|
if (!$whereClause) { |
|
66
|
|
|
return $query; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
$whereClause->traverse(function (&$expression) use ($bundle, $query) { |
|
70
|
|
|
if ($expression instanceof ExpressionInterface) { |
|
71
|
|
|
$expression = $this->_inspectExpression($expression, $bundle, $query); |
|
72
|
|
|
} |
|
73
|
|
|
}); |
|
74
|
|
|
|
|
75
|
|
|
return $query; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Analyzes the given WHERE expression, looks for virtual columns and alters |
|
80
|
|
|
* the expressions according. |
|
81
|
|
|
* |
|
82
|
|
|
* @param \Cake\Database\ExpressionInterface $expression Expression to scope |
|
83
|
|
|
* @param string $bundle Consider attributes only for a specific bundle |
|
84
|
|
|
* @param \Cake\ORM\Query $query The query instance this expression comes from |
|
85
|
|
|
* @return \Cake\Database\ExpressionInterface The altered expression (or not) |
|
86
|
|
|
*/ |
|
87
|
|
|
protected function _inspectExpression(ExpressionInterface $expression, $bundle, Query $query) |
|
88
|
|
|
{ |
|
89
|
|
|
if ($expression instanceof Comparison) { |
|
90
|
|
|
$expression = $this->_inspectComparisonExpression($expression, $bundle, $query); |
|
91
|
|
|
} elseif ($expression instanceof UnaryExpression) { |
|
92
|
|
|
$expression = $this->_inspectUnaryExpression($expression, $bundle, $query); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
return $expression; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Analyzes the given comparison expression and alters it according. |
|
100
|
|
|
* |
|
101
|
|
|
* @param \Cake\Database\Expression\Comparison $expression Comparison expression |
|
102
|
|
|
* @param string $bundle Consider attributes only for a specific bundle |
|
103
|
|
|
* @param \Cake\ORM\Query $query The query instance this expression comes from |
|
104
|
|
|
* @return \Cake\Database\Expression\Comparison Scoped expression (or not) |
|
105
|
|
|
*/ |
|
106
|
|
|
protected function _inspectComparisonExpression(Comparison $expression, $bundle, Query $query) |
|
107
|
|
|
{ |
|
108
|
|
|
$field = $expression->getField(); |
|
109
|
|
|
$column = is_string($field) ? $this->_toolbox->columnName($field) : ''; |
|
110
|
|
|
|
|
111
|
|
View Code Duplication |
if (empty($column) || |
|
112
|
|
|
in_array($column, (array)$this->_table->schema()->columns()) || // ignore real columns |
|
113
|
|
|
!in_array($column, $this->_toolbox->getAttributeNames()) || |
|
114
|
|
|
!$this->_toolbox->isSearchable($column) // ignore no searchable virtual columns |
|
115
|
|
|
) { |
|
116
|
|
|
// nothing to alter |
|
117
|
|
|
return $expression; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
$attr = $this->_toolbox->attributes($bundle)[$column]; |
|
121
|
|
|
$value = $expression->getValue(); |
|
122
|
|
|
$type = $this->_toolbox->getType($column); |
|
123
|
|
|
$conjunction = $expression->getOperator(); |
|
124
|
|
|
$conditions = [ |
|
125
|
|
|
'EavValues.eav_attribute_id' => $attr['id'], |
|
126
|
|
|
"EavValues.value_{$type} {$conjunction}" => $value, |
|
127
|
|
|
]; |
|
128
|
|
|
|
|
129
|
|
|
// subquery scope |
|
130
|
|
|
$subQuery = TableRegistry::get('Eav.EavValues') |
|
131
|
|
|
->find() |
|
132
|
|
|
->select('EavValues.entity_id') |
|
133
|
|
|
->where($conditions); |
|
134
|
|
|
|
|
135
|
|
|
// some variables |
|
136
|
|
|
$pk = $this->_tablePrimaryKey(); |
|
137
|
|
|
$driverClass = $this->_driverClass($query); |
|
138
|
|
|
|
|
139
|
|
View Code Duplication |
switch ($driverClass) { |
|
140
|
|
|
case 'sqlite': |
|
141
|
|
|
$concat = implode(' || ', $pk); |
|
142
|
|
|
$field = "({$concat} || '')"; |
|
143
|
|
|
break; |
|
144
|
|
|
case 'mysql': |
|
145
|
|
|
case 'postgres': |
|
146
|
|
|
case 'sqlserver': |
|
147
|
|
|
default: |
|
148
|
|
|
$concat = implode(', ', $pk); |
|
149
|
|
|
$field = "CONCAT({$concat}, '')"; |
|
150
|
|
|
break; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
// compile query, faster than raw subquery in most cases |
|
154
|
|
|
$ids = $subQuery->all()->extract('entity_id')->toArray(); |
|
155
|
|
|
$ids = empty($ids) ? ['-1'] : $ids; |
|
156
|
|
|
$expression->setField($field); |
|
157
|
|
|
$expression->setValue($ids); |
|
158
|
|
|
$expression->setOperator('IN'); |
|
159
|
|
|
|
|
160
|
|
|
$class = new \ReflectionClass($expression); |
|
161
|
|
|
$property = $class->getProperty('_type'); |
|
162
|
|
|
$property->setAccessible(true); |
|
163
|
|
|
$property->setValue($expression, 'string[]'); |
|
164
|
|
|
|
|
165
|
|
|
return $expression; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
protected function _inspectUnaryExpression(UnaryExpression $expression, $bundle, Query $query) |
|
169
|
|
|
{ |
|
170
|
|
|
$class = new \ReflectionClass($expression); |
|
171
|
|
|
$property = $class->getProperty('_value'); |
|
172
|
|
|
$property->setAccessible(true); |
|
173
|
|
|
$value = $property->getValue($expression); |
|
174
|
|
|
|
|
175
|
|
|
if ($value instanceof IdentifierExpression) { |
|
176
|
|
|
$field = $value->getIdentifier(); |
|
177
|
|
|
$column = is_string($field) ? $this->_toolbox->columnName($field) : ''; |
|
178
|
|
|
|
|
179
|
|
View Code Duplication |
if (empty($column) || |
|
180
|
|
|
in_array($column, (array)$this->_table->schema()->columns()) || // ignore real columns |
|
181
|
|
|
!in_array($column, $this->_toolbox->getAttributeNames($bundle)) || |
|
182
|
|
|
!$this->_toolbox->isSearchable($column) // ignore no searchable virtual columns |
|
183
|
|
|
) { |
|
184
|
|
|
// nothing to alter |
|
185
|
|
|
return $expression; |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
$pk = $this->_tablePrimaryKey(); |
|
189
|
|
|
$driverClass = $this->_driverClass($query); |
|
190
|
|
|
|
|
191
|
|
View Code Duplication |
switch ($driverClass) { |
|
192
|
|
|
case 'sqlite': |
|
193
|
|
|
$concat = implode(' || ', $pk); |
|
194
|
|
|
$field = "({$concat} || '')"; |
|
195
|
|
|
break; |
|
196
|
|
|
case 'mysql': |
|
197
|
|
|
case 'postgres': |
|
198
|
|
|
case 'sqlserver': |
|
199
|
|
|
default: |
|
200
|
|
|
$concat = implode(', ', $pk); |
|
201
|
|
|
$field = "CONCAT({$concat}, '')"; |
|
202
|
|
|
break; |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
$attr = $this->_toolbox->attributes($bundle)[$column]; |
|
206
|
|
|
$type = $this->_toolbox->getType($column); |
|
207
|
|
|
$subQuery = TableRegistry::get('Eav.EavValues') |
|
208
|
|
|
->find() |
|
209
|
|
|
->select("EavValues.value_{$type}") |
|
210
|
|
|
->where([ |
|
211
|
|
|
'EavValues.entity_id' => $field, |
|
212
|
|
|
'EavValues.eav_attribute_id' => $attr['id'] |
|
213
|
|
|
]) |
|
214
|
|
|
->sql(); |
|
215
|
|
|
$subQuery = str_replace([':c0', ':c1'], [$field, $attr['id']], $subQuery); |
|
216
|
|
|
$property->setValue($expression, "({$subQuery})"); |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
return $expression; |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
/** |
|
223
|
|
|
* Gets table's PK as an array. |
|
224
|
|
|
* |
|
225
|
|
|
* @return array |
|
226
|
|
|
*/ |
|
227
|
|
|
protected function _tablePrimaryKey() |
|
228
|
|
|
{ |
|
229
|
|
|
$alias = $this->_table->alias(); |
|
230
|
|
|
$pk = $this->_table->primaryKey(); |
|
231
|
|
|
|
|
232
|
|
|
if (!is_array($pk)) { |
|
233
|
|
|
$pk = [$pk]; |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
$pk = array_map(function ($key) use ($alias) { |
|
237
|
|
|
return "{$alias}.{$key}"; |
|
238
|
|
|
}, $pk); |
|
239
|
|
|
|
|
240
|
|
|
return $pk; |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
/** |
|
244
|
|
|
* Gets the name of the class driver used by the given $query to access the DB. |
|
245
|
|
|
* |
|
246
|
|
|
* @param \Cake\ORM\Query $query The query to inspect |
|
247
|
|
|
* @return string Lowercased drive name. e.g. `mysql` |
|
248
|
|
|
*/ |
|
249
|
|
|
protected function _driverClass(Query $query) |
|
250
|
|
|
{ |
|
251
|
|
|
$conn = $query->connection(null); |
|
252
|
|
|
list(, $driverClass) = namespaceSplit(strtolower(get_class($conn->driver()))); |
|
253
|
|
|
return $driverClass; |
|
254
|
|
|
} |
|
255
|
|
|
} |
|
256
|
|
|
|