1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @package midcom.helper.datamanager2 |
4
|
|
|
* @author CONTENT CONTROL http://www.contentcontrol-berlin.de/ |
5
|
|
|
* @copyright CONTENT CONTROL http://www.contentcontrol-berlin.de/ |
6
|
|
|
* @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Datamanager 2 Autocomplete result lister |
11
|
|
|
* |
12
|
|
|
* @package midcom.helper.datamanager2 |
13
|
|
|
*/ |
14
|
|
|
class midcom_helper_datamanager2_ajax_autocomplete |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* The request data we're working on |
18
|
|
|
* |
19
|
|
|
* @var array |
20
|
|
|
*/ |
21
|
|
|
private $_request; |
22
|
|
|
|
23
|
2 |
|
public function __construct($data) |
24
|
|
|
{ |
25
|
2 |
|
$this->_request = $data; |
26
|
2 |
|
$this->_verify_request(); |
27
|
2 |
|
} |
28
|
|
|
|
29
|
2 |
|
private function _verify_request() |
30
|
|
|
{ |
31
|
|
|
// Load component if possible |
32
|
2 |
|
midcom::get()->componentloader->load_graceful($this->_request['component']); |
33
|
|
|
|
34
|
2 |
|
if (!class_exists($this->_request['class'])) { |
35
|
|
|
throw new midcom_error("Class {$this->_request['class']} could not be loaded"); |
36
|
|
|
} |
37
|
|
|
|
38
|
2 |
|
if (empty($this->_request['searchfields'])) { |
39
|
|
|
throw new midcom_error("No fields to search for defined"); |
40
|
|
|
} |
41
|
|
|
|
42
|
2 |
|
if (empty($this->_request["term"])) { |
43
|
|
|
throw new midcom_error("Empty query string."); |
44
|
|
|
} |
45
|
|
|
|
46
|
2 |
|
if (!isset($this->_request['get_label_for'])) { |
47
|
2 |
|
$this->_request['get_label_for'] = null; |
48
|
|
|
} |
49
|
2 |
|
} |
50
|
|
|
|
51
|
2 |
|
private function _prepare_qb() |
52
|
|
|
{ |
53
|
2 |
|
$qb = call_user_func(array($this->_request['class'], 'new_query_builder')); |
54
|
|
|
|
55
|
2 |
|
if ( !empty($this->_request['constraints']) |
56
|
2 |
|
&& is_array($this->_request['constraints'])) { |
57
|
|
|
$this->_apply_constraints($qb, $this->_request['constraints']); |
58
|
|
|
} |
59
|
|
|
|
60
|
2 |
|
$constraints = $this->_get_search_constraints(); |
61
|
2 |
|
if (!empty($constraints)) { |
62
|
2 |
|
$qb->begin_group('OR'); |
63
|
2 |
|
$this->_apply_constraints($qb, $constraints); |
64
|
2 |
|
$qb->end_group(); |
65
|
|
|
} |
66
|
|
|
|
67
|
2 |
|
if ( !empty($this->_request['orders']) |
68
|
2 |
|
&& is_array($this->_request['orders'])) { |
69
|
|
|
ksort($this->_request['orders']); |
70
|
|
|
foreach ($this->_request['orders'] as $data) { |
71
|
|
|
foreach ($data as $field => $order) { |
72
|
|
|
$qb->add_order($field, $order); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
} |
76
|
2 |
|
return $qb; |
77
|
|
|
} |
78
|
|
|
|
79
|
2 |
|
private function _apply_constraints(midcom_core_query $query, array $constraints) |
80
|
|
|
{ |
81
|
2 |
|
$mgdschema_class = midcom_helper_reflector::resolve_baseclass($query->get_classname()); |
82
|
2 |
|
$reflector = new midgard_reflection_property($mgdschema_class); |
83
|
|
|
|
84
|
2 |
|
ksort($constraints); |
85
|
2 |
|
foreach ($constraints as $key => $data) { |
86
|
2 |
|
if ( !array_key_exists('value', $data) |
87
|
2 |
|
|| empty($data['field']) |
88
|
2 |
|
|| empty($data['op'])) { |
89
|
|
|
debug_add("Constraint #{$key} is not correctly defined, skipping", MIDCOM_LOG_WARN); |
90
|
|
|
continue; |
91
|
|
|
} |
92
|
2 |
|
if ($data['field'] === 'username') { |
93
|
|
|
midcom_core_account::add_username_constraint($query, $data['op'], $data['value']); |
94
|
|
|
} else { |
95
|
2 |
|
switch ($reflector->get_midgard_type($data['field'])) { |
96
|
2 |
|
case MGD_TYPE_INT: |
97
|
2 |
|
case MGD_TYPE_UINT: |
98
|
1 |
|
$data['value'] = (int) $data['value']; |
99
|
1 |
|
break; |
100
|
1 |
|
case MGD_TYPE_FLOAT: |
101
|
|
|
$data['value'] = (float) $data['value']; |
102
|
|
|
break; |
103
|
1 |
|
case MGD_TYPE_BOOLEAN: |
104
|
|
|
$data['value'] = (boolean) $data['value']; |
105
|
|
|
break; |
106
|
|
|
} |
107
|
2 |
|
$query->add_constraint($data['field'], $data['op'], $data['value']); |
108
|
|
|
} |
109
|
|
|
} |
110
|
2 |
|
} |
111
|
|
|
|
112
|
2 |
|
private function _get_search_constraints() |
113
|
|
|
{ |
114
|
2 |
|
$constraints = array(); |
115
|
2 |
|
$query = $this->_request["term"]; |
116
|
2 |
|
if (preg_match('/^%+$/', $query)) { |
117
|
|
|
debug_add('query is all wildcards, don\'t waste time in adding LIKE constraints'); |
118
|
|
|
return $constraints; |
119
|
|
|
} |
120
|
|
|
|
121
|
2 |
|
$reflector = new midgard_reflection_property(midcom_helper_reflector::resolve_baseclass($this->_request['class'])); |
122
|
|
|
|
123
|
2 |
|
foreach ($this->_request['searchfields'] as $field) { |
124
|
2 |
|
$field_type = $reflector->get_midgard_type($field); |
125
|
2 |
|
$operator = 'LIKE'; |
126
|
2 |
|
if (strpos($field, '.')) { |
127
|
|
|
//TODO: This should be resolved properly |
128
|
1 |
|
$field_type = MGD_TYPE_STRING; |
129
|
|
|
} |
130
|
|
|
switch ($field_type) { |
131
|
2 |
|
case MGD_TYPE_GUID: |
132
|
2 |
|
case MGD_TYPE_STRING: |
133
|
1 |
|
case MGD_TYPE_LONGTEXT: |
134
|
1 |
|
$query = $this->get_querystring(); |
135
|
1 |
|
break; |
136
|
1 |
|
case MGD_TYPE_INT: |
137
|
1 |
|
case MGD_TYPE_UINT: |
138
|
|
|
case MGD_TYPE_FLOAT: |
139
|
1 |
|
$operator = '='; |
140
|
1 |
|
break; |
141
|
|
|
default: |
142
|
|
|
debug_add("can't handle field type " . $field_type, MIDCOM_LOG_WARN); |
143
|
|
|
continue; |
144
|
|
|
} |
145
|
2 |
|
debug_add("adding search (ORed) constraint: {$field} {$operator} '{$query}'"); |
146
|
2 |
|
$constraints[] = array( |
147
|
2 |
|
'field' => $field, |
148
|
2 |
|
'op' => $operator, |
149
|
2 |
|
'value' => $query |
150
|
|
|
); |
151
|
|
|
} |
152
|
2 |
|
return $constraints; |
153
|
|
|
} |
154
|
|
|
|
155
|
1 |
|
public function get_querystring() |
156
|
|
|
{ |
157
|
1 |
|
$query = $this->_request["term"]; |
158
|
1 |
|
$wildcard_query = $query; |
159
|
1 |
|
if ( isset($this->_request['auto_wildcards']) |
160
|
1 |
|
&& strpos($query, '%') === false) { |
161
|
|
|
switch ($this->_request['auto_wildcards']) { |
162
|
|
|
case 'start': |
163
|
|
|
$wildcard_query = '%' . $query; |
164
|
|
|
break; |
165
|
|
|
case 'end': |
166
|
|
|
$wildcard_query = $query . '%'; |
167
|
|
|
break; |
168
|
|
|
case 'both': |
169
|
|
|
$wildcard_query = '%' . $query . '%'; |
170
|
|
|
break; |
171
|
|
|
default: |
172
|
|
|
debug_add("Don't know how to handle auto_wildcards value '" . $this->_request['auto_wildcards'] . "'", MIDCOM_LOG_WARN); |
173
|
|
|
break; |
174
|
|
|
} |
175
|
|
|
} |
176
|
1 |
|
$wildcard_query = str_replace("*", "%", $wildcard_query); |
177
|
1 |
|
$wildcard_query = preg_replace('/%+/', '%', $wildcard_query); |
178
|
1 |
|
return $wildcard_query; |
179
|
|
|
} |
180
|
|
|
|
181
|
2 |
|
public function get_objects() |
182
|
|
|
{ |
183
|
2 |
|
return $this->_prepare_qb()->execute(); |
184
|
|
|
} |
185
|
|
|
|
186
|
2 |
|
public function get_results() |
187
|
|
|
{ |
188
|
2 |
|
if (empty($this->_request["id_field"])) { |
189
|
|
|
throw new midcom_error("Empty ID field."); |
190
|
|
|
} |
191
|
|
|
|
192
|
2 |
|
$results = $this->get_objects(); |
193
|
2 |
|
$items = array(); |
194
|
|
|
|
195
|
2 |
|
foreach ($results as $object) { |
196
|
|
|
$item = array( |
197
|
2 |
|
'id' => $object->{$this->_request['id_field']}, |
198
|
2 |
|
'label' => midcom_helper_datamanager2_widget_autocomplete::create_item_label($object, $this->_request['result_headers'], $this->_request['get_label_for']), |
199
|
|
|
); |
200
|
2 |
|
if (!empty($this->_request['categorize_by_parent_label'])) { |
201
|
|
|
$item['category'] = ''; |
202
|
|
|
if ($parent = $object->get_parent()) { |
203
|
|
|
$item['category'] = midcom_helper_reflector::get($parent)->get_object_label($parent); |
204
|
|
|
} |
205
|
|
|
} |
206
|
2 |
|
$item['value'] = $item['label']; |
207
|
|
|
|
208
|
2 |
|
$items[] = $item; |
209
|
|
|
} |
210
|
|
|
|
211
|
2 |
|
usort($items, array('midcom_helper_datamanager2_widget_autocomplete', 'sort_items')); |
212
|
|
|
|
213
|
2 |
|
return $items; |
214
|
|
|
} |
215
|
|
|
|
216
|
31 |
|
public static function get_property_string($object, $item_name) |
217
|
|
|
{ |
218
|
31 |
|
if (preg_match('/^metadata\.(.+)$/', $item_name, $regs)) { |
219
|
|
|
$metadata_property = $regs[1]; |
220
|
|
|
$value = $object->metadata->$metadata_property; |
221
|
|
|
|
222
|
|
|
switch ($metadata_property) { |
223
|
|
|
case 'created': |
224
|
|
|
case 'revised': |
225
|
|
|
case 'published': |
226
|
|
|
case 'schedulestart': |
227
|
|
|
case 'scheduleend': |
228
|
|
|
case 'imported': |
229
|
|
|
case 'exported': |
230
|
|
|
case 'approved': |
231
|
|
|
if ($value) { |
232
|
|
|
return strftime('%x %X', $value); |
233
|
|
|
} |
234
|
|
|
break; |
235
|
|
|
case 'creator': |
236
|
|
|
case 'revisor': |
237
|
|
|
case 'approver': |
238
|
|
|
case 'locker': |
239
|
|
|
if ($value) { |
240
|
|
|
$person = new midcom_db_person($value); |
241
|
|
|
return $person->name; |
242
|
|
|
} |
243
|
|
|
break; |
244
|
|
|
} |
245
|
|
|
} elseif ( $item_name == 'username' |
246
|
31 |
|
&& $object instanceof midcom_db_person) { |
247
|
|
|
$account = new midcom_core_account($object); |
248
|
31 |
|
return $account->get_username(); |
249
|
|
|
} |
250
|
|
|
return $object->$item_name; |
251
|
|
|
} |
252
|
|
|
} |
253
|
|
|
|