|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @package midcom.helper.reflector |
|
4
|
|
|
* @author The Midgard Project, http://www.midgard-project.org |
|
5
|
|
|
* @copyright The Midgard Project, http://www.midgard-project.org |
|
6
|
|
|
* @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
use midgard\portable\storage\connection; |
|
10
|
|
|
use midgard\portable\api\mgdobject; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* The Grand Unified Reflector |
|
14
|
|
|
* |
|
15
|
|
|
* @package midcom.helper.reflector |
|
16
|
|
|
*/ |
|
17
|
|
|
class midcom_helper_reflector extends midcom_baseclasses_components_purecode |
|
18
|
|
|
{ |
|
19
|
|
|
public $mgdschema_class; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var midgard_reflection_property |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $_mgd_reflector; |
|
25
|
|
|
|
|
26
|
|
|
protected $_dummy_object; |
|
27
|
|
|
|
|
28
|
|
|
private static $_cache = [ |
|
29
|
|
|
'l10n' => [], |
|
30
|
|
|
'instance' => [], |
|
31
|
|
|
'title' => [], |
|
32
|
|
|
'name' => [], |
|
33
|
|
|
'fieldnames' => [], |
|
34
|
|
|
'object_icon_map' => null, |
|
35
|
|
|
'create_icon_map' => null |
|
36
|
|
|
]; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Constructor, takes classname or object, resolved MgdSchema root class automagically |
|
40
|
|
|
* |
|
41
|
|
|
* @param string|mgdobject $src classname or object |
|
42
|
|
|
*/ |
|
43
|
192 |
|
public function __construct($src) |
|
44
|
|
|
{ |
|
45
|
192 |
|
parent::__construct(); |
|
46
|
|
|
|
|
47
|
|
|
// Resolve root class name |
|
48
|
192 |
|
$this->mgdschema_class = self::resolve_baseclass($src); |
|
49
|
|
|
// Could not resolve root class name |
|
50
|
192 |
|
if (empty($this->mgdschema_class)) { |
|
51
|
|
|
// Handle object vs string |
|
52
|
|
|
$original_class = (is_object($src)) ? get_class($src) : $src; |
|
53
|
|
|
throw new midcom_error("Could not determine MgdSchema baseclass for '{$original_class}'"); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
// Instantiate midgard reflector |
|
57
|
192 |
|
$this->_mgd_reflector = new midgard_reflection_property($this->mgdschema_class); |
|
58
|
|
|
|
|
59
|
|
|
// Instantiate dummy object |
|
60
|
192 |
|
$this->_dummy_object = new $this->mgdschema_class; |
|
61
|
192 |
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Get cached reflector instance |
|
65
|
|
|
* |
|
66
|
|
|
* @param mixed $src Object or classname |
|
67
|
|
|
* @return static |
|
68
|
|
|
*/ |
|
69
|
386 |
|
public static function &get($src) |
|
70
|
|
|
{ |
|
71
|
386 |
|
$identifier = get_called_class() . (is_object($src) ? get_class($src) : $src); |
|
72
|
|
|
|
|
73
|
386 |
|
if (!isset(self::$_cache['instance'][$identifier])) { |
|
74
|
37 |
|
self::$_cache['instance'][$identifier] = new static($src); |
|
75
|
|
|
} |
|
76
|
386 |
|
return self::$_cache['instance'][$identifier]; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Get object's (mgdschema) fieldnames. |
|
81
|
|
|
* |
|
82
|
|
|
* @param object $object Object The object to query |
|
83
|
|
|
*/ |
|
84
|
104 |
|
public static function get_object_fieldnames($object) : array |
|
85
|
|
|
{ |
|
86
|
104 |
|
if (!is_object($object)) { |
|
87
|
|
|
throw new midcom_error('Invalid parameter type'); |
|
88
|
|
|
} |
|
89
|
104 |
|
$classname = get_class($object); |
|
90
|
104 |
|
$metadata = false; |
|
91
|
|
|
|
|
92
|
104 |
|
if (midcom::get()->dbclassloader->is_midcom_db_object($object)) { |
|
93
|
94 |
|
$classname = $object->__mgdschema_class_name__; |
|
94
|
85 |
|
} elseif ($object instanceof midcom_helper_metadata) { |
|
95
|
74 |
|
$metadata = true; |
|
96
|
74 |
|
$classname = $object->object->__mgdschema_class_name__; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
104 |
|
if (is_subclass_of($classname, mgdobject::class)) { |
|
100
|
104 |
|
$cm = connection::get_em()->getClassMetadata($classname); |
|
101
|
104 |
|
return $cm->get_schema_properties($metadata); |
|
|
|
|
|
|
102
|
|
|
} |
|
103
|
4 |
|
return array_keys(get_object_vars($object)); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @param string $property |
|
108
|
|
|
* @param boolean $metadata |
|
109
|
|
|
*/ |
|
110
|
147 |
|
public function property_exists($property, $metadata = false) : bool |
|
111
|
|
|
{ |
|
112
|
147 |
|
return $this->_mgd_reflector->property_exists($property, $metadata); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Gets a midcom_helper_l10n instance for component governing the type |
|
117
|
|
|
*/ |
|
118
|
67 |
|
public function get_component_l10n() : midcom_services_i18n_l10n |
|
119
|
|
|
{ |
|
120
|
|
|
// Use cache if we have it |
|
121
|
67 |
|
if (isset(self::$_cache['l10n'][$this->mgdschema_class])) { |
|
122
|
63 |
|
return self::$_cache['l10n'][$this->mgdschema_class]; |
|
123
|
|
|
} |
|
124
|
11 |
|
$midcom_dba_classname = midcom::get()->dbclassloader->get_midcom_class_name_for_mgdschema_object($this->_dummy_object); |
|
125
|
11 |
|
if (empty($midcom_dba_classname)) { |
|
126
|
|
|
// Could not resolve MidCOM DBA class name, fallback early to our own l10n |
|
127
|
1 |
|
debug_add("Could not get MidCOM DBA classname for type {$this->mgdschema_class}, using our own l10n", MIDCOM_LOG_INFO); |
|
128
|
1 |
|
self::$_cache['l10n'][$this->mgdschema_class] = $this->_l10n; |
|
129
|
1 |
|
return $this->_l10n; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
11 |
|
if ($component = midcom::get()->dbclassloader->get_component_for_class($midcom_dba_classname)) { |
|
133
|
11 |
|
self::$_cache['l10n'][$this->mgdschema_class] = $this->_i18n->get_l10n($component); |
|
134
|
11 |
|
return self::$_cache['l10n'][$this->mgdschema_class]; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
debug_add("Could not resolve component for DBA class {$midcom_dba_classname}, using our own l10n", MIDCOM_LOG_INFO); |
|
138
|
|
|
self::$_cache['l10n'][$this->mgdschema_class] = $this->_l10n; |
|
139
|
|
|
return $this->_l10n; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Get the localized label of the class |
|
144
|
|
|
* |
|
145
|
|
|
* @todo remove any hardcoded class names/prefixes |
|
146
|
|
|
*/ |
|
147
|
67 |
|
public function get_class_label() : string |
|
148
|
|
|
{ |
|
149
|
67 |
|
$component_l10n = $this->get_component_l10n(); |
|
150
|
67 |
|
$use_classname = $this->mgdschema_class; |
|
151
|
|
|
|
|
152
|
67 |
|
$midcom_dba_classname = midcom::get()->dbclassloader->get_midcom_class_name_for_mgdschema_object($use_classname); |
|
153
|
|
|
|
|
154
|
67 |
|
if (!empty($midcom_dba_classname)) { |
|
155
|
67 |
|
$use_classname = $midcom_dba_classname; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
67 |
|
$use_classname = preg_replace('/_(db|dba)$/', '', $use_classname); |
|
159
|
|
|
|
|
160
|
67 |
|
$label = $component_l10n->get($use_classname); |
|
161
|
67 |
|
if ($label == $use_classname) { |
|
162
|
|
|
// Class string not localized, try Bergie's way to pretty-print |
|
163
|
67 |
|
$classname_parts = explode('_', $use_classname); |
|
164
|
67 |
|
if (count($classname_parts) >= 3) { |
|
165
|
|
|
// Drop first two parts of class name |
|
166
|
67 |
|
array_shift($classname_parts); |
|
167
|
67 |
|
array_shift($classname_parts); |
|
168
|
|
|
} |
|
169
|
|
|
// FIXME: Remove hardcoded class prefixes |
|
170
|
67 |
|
$use_label = preg_replace('/(openpsa|notifications)_/', '', implode('_', $classname_parts)); |
|
171
|
|
|
|
|
172
|
67 |
|
$use_label = str_replace('_', ' ', $use_label); |
|
173
|
67 |
|
$label = $component_l10n->get($use_label); |
|
174
|
67 |
|
if ($use_label == $label) { |
|
175
|
62 |
|
$label = ucwords($use_label); |
|
176
|
|
|
} |
|
177
|
|
|
} |
|
178
|
67 |
|
return $label; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* Get property name to use as label |
|
183
|
|
|
* |
|
184
|
|
|
* @return string name of property to use as label (or false on failure) |
|
185
|
|
|
*/ |
|
186
|
25 |
|
public function get_label_property() |
|
187
|
|
|
{ |
|
188
|
25 |
|
$midcom_class = midcom::get()->dbclassloader->get_midcom_class_name_for_mgdschema_object($this->mgdschema_class); |
|
189
|
25 |
|
$obj = ($midcom_class) ? new $midcom_class : new $this->mgdschema_class; |
|
190
|
|
|
|
|
191
|
25 |
|
if (method_exists($obj, 'get_label_property')) { |
|
192
|
2 |
|
return $obj->get_label_property(); |
|
193
|
|
|
} |
|
194
|
23 |
|
if (midcom::get()->dbfactory->is_a($obj, midcom_db_person::class)) { |
|
195
|
1 |
|
return ['rname', 'id']; |
|
196
|
|
|
} |
|
197
|
22 |
|
if ($this->get_title_property_nonstatic($obj) !== false) { |
|
|
|
|
|
|
198
|
17 |
|
return $this->get_title_property_nonstatic($obj); |
|
199
|
|
|
} |
|
200
|
6 |
|
if ($this->get_name_property_nonstatic($obj) !== false) { |
|
201
|
5 |
|
return $this->get_name_property_nonstatic($obj); |
|
202
|
|
|
} |
|
203
|
2 |
|
return 'guid'; |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* Get the object label property value |
|
208
|
|
|
* |
|
209
|
|
|
* @param mixed $object MgdSchema object |
|
210
|
|
|
* @return string Label of the object |
|
211
|
|
|
*/ |
|
212
|
54 |
|
public function get_object_label($object) |
|
213
|
|
|
{ |
|
214
|
54 |
|
if ($object instanceof mgdobject) { |
|
215
|
|
|
try { |
|
216
|
2 |
|
$obj = midcom::get()->dbfactory->convert_midgard_to_midcom($object); |
|
217
|
|
|
} catch (midcom_error $e) { |
|
218
|
2 |
|
return false; |
|
219
|
|
|
} |
|
220
|
|
|
} else { |
|
221
|
52 |
|
$obj = $object; |
|
222
|
|
|
} |
|
223
|
54 |
|
if (method_exists($obj, 'get_label')) { |
|
224
|
42 |
|
return $obj->get_label(); |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
14 |
|
$properties = array_flip($obj->get_properties()); |
|
228
|
14 |
|
if (empty($properties)) { |
|
229
|
|
|
debug_add("Could not list object properties, aborting", MIDCOM_LOG_ERROR); |
|
230
|
|
|
return false; |
|
231
|
|
|
} |
|
232
|
14 |
|
if (isset($properties['title'])) { |
|
233
|
12 |
|
return $obj->title; |
|
234
|
|
|
} |
|
235
|
3 |
|
if (isset($properties['name'])) { |
|
236
|
2 |
|
return $obj->name; |
|
237
|
|
|
} |
|
238
|
1 |
|
if ($obj->id > 0) { |
|
239
|
|
|
return $this->get_class_label() . ' #' . $obj->id; |
|
240
|
|
|
} |
|
241
|
1 |
|
return ''; |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
/** |
|
245
|
|
|
* Get the name of the create icon image |
|
246
|
|
|
* |
|
247
|
|
|
* @param string $type Name of the type |
|
248
|
|
|
*/ |
|
249
|
28 |
|
public static function get_create_icon($type) : string |
|
250
|
|
|
{ |
|
251
|
28 |
|
if (null === self::$_cache['create_icon_map']) { |
|
252
|
1 |
|
self::$_cache['create_icon_map'] = self::_get_icon_map('create_type_magic', 'file-o'); |
|
253
|
|
|
} |
|
254
|
|
|
|
|
255
|
28 |
|
$icon_callback = [$type, 'get_create_icon']; |
|
256
|
|
|
switch (true) { |
|
257
|
|
|
// class has static method to tell us the answer ? great ! |
|
258
|
28 |
|
case (is_callable($icon_callback)): |
|
259
|
|
|
$icon = call_user_func($icon_callback); |
|
260
|
|
|
break; |
|
261
|
|
|
// configuration icon |
|
262
|
28 |
|
case (isset(self::$_cache['create_icon_map'][$type])): |
|
263
|
16 |
|
$icon = self::$_cache['create_icon_map'][$type]; |
|
264
|
16 |
|
break; |
|
265
|
|
|
|
|
266
|
|
|
// heuristics magic (instead of adding something here, take a look at config key "create_type_magic") |
|
267
|
25 |
|
case (strpos($type, 'member') !== false): |
|
268
|
25 |
|
case (strpos($type, 'organization') !== false): |
|
269
|
3 |
|
$icon = 'users'; |
|
270
|
3 |
|
break; |
|
271
|
24 |
|
case (strpos($type, 'person') !== false): |
|
272
|
1 |
|
$icon = 'user-o'; |
|
273
|
1 |
|
break; |
|
274
|
23 |
|
case (strpos($type, 'event') !== false): |
|
275
|
1 |
|
$icon = 'calendar-o'; |
|
276
|
1 |
|
break; |
|
277
|
|
|
|
|
278
|
|
|
// Fallback default value |
|
279
|
|
|
default: |
|
280
|
22 |
|
$icon = self::$_cache['create_icon_map']['__default__']; |
|
281
|
22 |
|
break; |
|
282
|
|
|
} |
|
283
|
28 |
|
return $icon; |
|
284
|
|
|
} |
|
285
|
|
|
|
|
286
|
|
|
/** |
|
287
|
|
|
* Get the object icon |
|
288
|
|
|
* |
|
289
|
|
|
* @param mixed $obj MgdSchema object |
|
290
|
|
|
*/ |
|
291
|
12 |
|
public static function get_object_icon($obj) : string |
|
292
|
|
|
{ |
|
293
|
12 |
|
if (null === self::$_cache['object_icon_map']) { |
|
294
|
1 |
|
self::$_cache['object_icon_map'] = self::_get_icon_map('object_icon_magic', 'file'); |
|
295
|
|
|
} |
|
296
|
|
|
|
|
297
|
12 |
|
$object_class = get_class($obj); |
|
298
|
12 |
|
$object_baseclass = self::resolve_baseclass($obj); |
|
299
|
|
|
|
|
300
|
|
|
switch (true) { |
|
301
|
|
|
// object knows it's icon, how handy! |
|
302
|
12 |
|
case (method_exists($obj, 'get_icon')): |
|
303
|
4 |
|
$icon = $obj->get_icon(); |
|
304
|
4 |
|
break; |
|
305
|
|
|
|
|
306
|
|
|
// configuration icon |
|
307
|
9 |
|
case (isset(self::$_cache['object_icon_map'][$object_class])): |
|
308
|
2 |
|
$icon = self::$_cache['object_icon_map'][$object_class]; |
|
309
|
2 |
|
break; |
|
310
|
8 |
|
case (isset(self::$_cache['object_icon_map'][$object_baseclass])): |
|
311
|
2 |
|
$icon = self::$_cache['object_icon_map'][$object_baseclass]; |
|
312
|
2 |
|
break; |
|
313
|
|
|
|
|
314
|
|
|
// heuristics magic (instead of adding something here, take a look at config key "object_icon_magic") |
|
315
|
7 |
|
case (strpos($object_class, 'person') !== false): |
|
316
|
3 |
|
$icon = 'user'; |
|
317
|
3 |
|
break; |
|
318
|
6 |
|
case (strpos($object_class, 'event') !== false): |
|
319
|
1 |
|
$icon = 'calendar-o'; |
|
320
|
1 |
|
break; |
|
321
|
5 |
|
case (strpos($object_class, 'member') !== false): |
|
322
|
5 |
|
case (strpos($object_class, 'organization') !== false): |
|
323
|
4 |
|
case (strpos($object_class, 'group') !== false): |
|
324
|
3 |
|
$icon = 'users'; |
|
325
|
3 |
|
break; |
|
326
|
4 |
|
case (strpos($object_class, 'element') !== false): |
|
327
|
1 |
|
$icon = 'file-code-o'; |
|
328
|
1 |
|
break; |
|
329
|
|
|
|
|
330
|
|
|
// Fallback default value |
|
331
|
|
|
default: |
|
332
|
3 |
|
$icon = self::$_cache['object_icon_map']['__default__']; |
|
333
|
3 |
|
break; |
|
334
|
|
|
} |
|
335
|
|
|
|
|
336
|
12 |
|
return '<i class="fa fa-' . $icon . '"></i>'; |
|
337
|
|
|
} |
|
338
|
|
|
|
|
339
|
2 |
|
private static function _get_icon_map(string $config_key, string $fallback) : array |
|
340
|
|
|
{ |
|
341
|
2 |
|
$config = midcom_baseclasses_components_configuration::get('midcom.helper.reflector', 'config'); |
|
342
|
2 |
|
$icons2classes = $config->get($config_key); |
|
343
|
2 |
|
$icon_map = []; |
|
344
|
|
|
//sanity |
|
345
|
2 |
|
if (!is_array($icons2classes)) { |
|
346
|
|
|
throw new midcom_error('Config key "' . $config_key . '" is not an array'); |
|
347
|
|
|
} |
|
348
|
2 |
|
foreach ($icons2classes as $icon => $classes) { |
|
349
|
2 |
|
$icon_map = array_merge($icon_map, array_fill_keys($classes, $icon)); |
|
350
|
|
|
} |
|
351
|
2 |
|
if (!isset($icon_map['__default__'])) { |
|
352
|
|
|
$icon_map['__default__'] = $fallback; |
|
353
|
|
|
} |
|
354
|
2 |
|
return $icon_map; |
|
355
|
|
|
} |
|
356
|
|
|
|
|
357
|
|
|
/** |
|
358
|
|
|
* Get class properties to use as search fields in choosers or other direct DB searches |
|
359
|
|
|
*/ |
|
360
|
15 |
|
public function get_search_properties() : array |
|
361
|
|
|
{ |
|
362
|
|
|
// Return cached results if we have them |
|
363
|
15 |
|
static $cache = []; |
|
364
|
15 |
|
if (isset($cache[$this->mgdschema_class])) { |
|
365
|
9 |
|
return $cache[$this->mgdschema_class]; |
|
366
|
|
|
} |
|
367
|
7 |
|
debug_add("Starting analysis for class {$this->mgdschema_class}"); |
|
368
|
|
|
|
|
369
|
7 |
|
$properties = self::get_object_fieldnames($this->_dummy_object); |
|
370
|
|
|
|
|
371
|
|
|
$default_properties = [ |
|
372
|
7 |
|
'title' => true, |
|
373
|
|
|
'tag' => true, |
|
374
|
|
|
'firstname' => true, |
|
375
|
|
|
'lastname' => true, |
|
376
|
|
|
'official' => true, |
|
377
|
|
|
'username' => true, |
|
378
|
|
|
]; |
|
379
|
|
|
|
|
380
|
7 |
|
$search_properties = array_intersect_key($default_properties, array_flip($properties)); |
|
381
|
|
|
|
|
382
|
7 |
|
foreach ($properties as $property) { |
|
383
|
7 |
|
if (strpos($property, 'name') !== false) { |
|
384
|
6 |
|
$search_properties[$property] = true; |
|
385
|
|
|
} |
|
386
|
|
|
// TODO: More per property heuristics |
|
387
|
|
|
} |
|
388
|
|
|
// TODO: parent and up heuristics |
|
389
|
|
|
|
|
390
|
7 |
|
$label_prop = $this->get_label_property(); |
|
391
|
|
|
|
|
392
|
7 |
|
if ( is_string($label_prop) |
|
393
|
7 |
|
&& $label_prop != 'guid' |
|
394
|
7 |
|
&& $this->_mgd_reflector->property_exists($label_prop)) { |
|
395
|
6 |
|
$search_properties[$label_prop] = true; |
|
396
|
|
|
} |
|
397
|
|
|
|
|
398
|
|
|
// Exceptions - always search these fields |
|
399
|
7 |
|
$always_search_all = $this->_config->get('always_search_fields') ?: []; |
|
400
|
7 |
|
if (!empty($always_search_all[$this->mgdschema_class])) { |
|
401
|
1 |
|
$fields = array_intersect($always_search_all[$this->mgdschema_class], $properties); |
|
402
|
1 |
|
$search_properties += array_flip($fields); |
|
403
|
|
|
} |
|
404
|
|
|
|
|
405
|
|
|
// Exceptions - never search these fields |
|
406
|
7 |
|
$never_search_all = $this->_config->get('never_search_fields') ?: []; |
|
407
|
7 |
|
if (!empty($never_search_all[$this->mgdschema_class])) { |
|
408
|
|
|
$search_properties = array_diff_key($search_properties, array_flip($never_search_all[$this->mgdschema_class])); |
|
409
|
|
|
} |
|
410
|
|
|
|
|
411
|
7 |
|
$search_properties = array_keys($search_properties); |
|
412
|
7 |
|
debug_print_r("Search properties for {$this->mgdschema_class}: ", $search_properties); |
|
413
|
7 |
|
$cache[$this->mgdschema_class] = $search_properties; |
|
414
|
7 |
|
return $search_properties; |
|
415
|
|
|
} |
|
416
|
|
|
|
|
417
|
|
|
/** |
|
418
|
|
|
* Gets a list of link properties and the links target info |
|
419
|
|
|
* |
|
420
|
|
|
* Link info key specification |
|
421
|
|
|
* 'class' string link target class name |
|
422
|
|
|
* 'target' string link target property (of target class) |
|
423
|
|
|
* 'parent' boolean link is link to "parent" in object tree |
|
424
|
|
|
* 'up' boolean link is link to "up" in object tree |
|
425
|
|
|
* |
|
426
|
|
|
* @return array multidimensional array keyed by property, values are arrays with link info (or false in case of failure) |
|
427
|
|
|
*/ |
|
428
|
4 |
|
public function get_link_properties() : array |
|
429
|
|
|
{ |
|
430
|
|
|
// Return cached results if we have them |
|
431
|
4 |
|
static $cache = []; |
|
432
|
4 |
|
if (isset($cache[$this->mgdschema_class])) { |
|
433
|
1 |
|
return $cache[$this->mgdschema_class]; |
|
434
|
|
|
} |
|
435
|
3 |
|
debug_add("Starting analysis for class {$this->mgdschema_class}"); |
|
436
|
|
|
|
|
437
|
|
|
// Shorthands |
|
438
|
3 |
|
$ref = $this->_mgd_reflector; |
|
439
|
3 |
|
$obj = $this->_dummy_object; |
|
440
|
|
|
|
|
441
|
|
|
// Get property list and start checking (or abort on error) |
|
442
|
3 |
|
$properties = self::get_object_fieldnames($obj); |
|
443
|
|
|
|
|
444
|
3 |
|
$links = []; |
|
445
|
3 |
|
$parent_property = midgard_object_class::get_property_parent($obj); |
|
446
|
3 |
|
$up_property = midgard_object_class::get_property_up($obj); |
|
447
|
3 |
|
foreach ($properties as $property) { |
|
448
|
3 |
|
if ($property == 'guid') { |
|
449
|
|
|
// GUID, even though of type MGD_TYPE_GUID, is never a link |
|
450
|
3 |
|
continue; |
|
451
|
|
|
} |
|
452
|
|
|
|
|
453
|
3 |
|
if ( !$ref->is_link($property) |
|
454
|
3 |
|
&& $ref->get_midgard_type($property) != MGD_TYPE_GUID) { |
|
455
|
3 |
|
continue; |
|
456
|
|
|
} |
|
457
|
3 |
|
debug_add("Processing property '{$property}'"); |
|
458
|
|
|
$linkinfo = [ |
|
459
|
3 |
|
'class' => null, |
|
460
|
|
|
'target' => null, |
|
461
|
|
|
'parent' => false, |
|
462
|
|
|
'up' => false, |
|
463
|
3 |
|
'type' => $ref->get_midgard_type($property), |
|
464
|
|
|
]; |
|
465
|
3 |
|
if ($parent_property === $property) { |
|
466
|
2 |
|
debug_add("Is 'parent' property"); |
|
467
|
2 |
|
$linkinfo['parent'] = true; |
|
468
|
|
|
} |
|
469
|
3 |
|
if ($up_property === $property) { |
|
470
|
1 |
|
debug_add("Is 'up' property"); |
|
471
|
1 |
|
$linkinfo['up'] = true; |
|
472
|
|
|
} |
|
473
|
|
|
|
|
474
|
3 |
|
$type = $ref->get_link_name($property); |
|
475
|
3 |
|
debug_add("get_link_name returned '{$type}'"); |
|
476
|
3 |
|
if (!empty($type)) { |
|
477
|
2 |
|
$linkinfo['class'] = $type; |
|
478
|
|
|
} |
|
479
|
|
|
|
|
480
|
3 |
|
$target = $ref->get_link_target($property); |
|
481
|
|
|
|
|
482
|
3 |
|
debug_add("get_link_target returned '{$target}'"); |
|
483
|
3 |
|
if (!empty($target)) { |
|
484
|
2 |
|
$linkinfo['target'] = $target; |
|
485
|
1 |
|
} elseif ($linkinfo['type'] == MGD_TYPE_GUID) { |
|
486
|
1 |
|
$linkinfo['target'] = 'guid'; |
|
487
|
|
|
} |
|
488
|
|
|
|
|
489
|
3 |
|
$links[$property] = $linkinfo; |
|
490
|
|
|
} |
|
491
|
|
|
|
|
492
|
3 |
|
debug_print_r("Links for {$this->mgdschema_class}: ", $links); |
|
493
|
3 |
|
$cache[$this->mgdschema_class] = $links; |
|
494
|
3 |
|
return $links; |
|
495
|
|
|
} |
|
496
|
|
|
|
|
497
|
|
|
/** |
|
498
|
|
|
* Map extended classes |
|
499
|
|
|
* |
|
500
|
|
|
* For example org.openpsa.* components often expand core objects, |
|
501
|
|
|
* in config we specify which classes we wish to substitute with which |
|
502
|
|
|
* |
|
503
|
|
|
* @param string $schema_type classname to check rewriting for |
|
504
|
|
|
* @return string new classname (or original in case no rewriting is to be done) |
|
505
|
|
|
*/ |
|
506
|
38 |
|
public static function class_rewrite(string $schema_type) : string |
|
507
|
|
|
{ |
|
508
|
38 |
|
static $extends = false; |
|
509
|
38 |
|
if ($extends === false) { |
|
510
|
|
|
$extends = midcom_baseclasses_components_configuration::get('midcom.helper.reflector', 'config')->get('class_extends'); |
|
511
|
|
|
// Safety against misconfiguration |
|
512
|
|
|
if (!is_array($extends)) { |
|
513
|
|
|
throw new midcom_error("config->get('class_extends') did not return array, invalid configuration ??"); |
|
514
|
|
|
} |
|
515
|
|
|
} |
|
516
|
38 |
|
if ( isset($extends[$schema_type]) |
|
517
|
38 |
|
&& class_exists($extends[$schema_type])) { |
|
518
|
2 |
|
return $extends[$schema_type]; |
|
519
|
|
|
} |
|
520
|
37 |
|
return $schema_type; |
|
521
|
|
|
} |
|
522
|
|
|
|
|
523
|
|
|
/** |
|
524
|
|
|
* See if two MgdSchema classes are the same |
|
525
|
|
|
* |
|
526
|
|
|
* NOTE: also takes into account the various extended class scenarios |
|
527
|
|
|
* |
|
528
|
|
|
* @param string $class_one first class to compare |
|
529
|
|
|
* @param string $class_two second class to compare |
|
530
|
|
|
*/ |
|
531
|
12 |
|
public static function is_same_class($class_one, $class_two) : bool |
|
532
|
|
|
{ |
|
533
|
12 |
|
$one = self::resolve_baseclass($class_one); |
|
534
|
12 |
|
$two = self::resolve_baseclass($class_two); |
|
535
|
12 |
|
return $one == $two; |
|
536
|
|
|
} |
|
537
|
|
|
|
|
538
|
|
|
/** |
|
539
|
|
|
* Get the MgdSchema classname for given class |
|
540
|
|
|
* |
|
541
|
|
|
* @param mixed $classname either string (class name) or object |
|
542
|
|
|
* @return string the base class name |
|
543
|
|
|
*/ |
|
544
|
225 |
|
public static function resolve_baseclass($classname) : ?string |
|
545
|
|
|
{ |
|
546
|
225 |
|
static $cached = []; |
|
547
|
|
|
|
|
548
|
225 |
|
if (is_object($classname)) { |
|
549
|
182 |
|
$class_instance = $classname; |
|
550
|
182 |
|
$classname = get_class($classname); |
|
551
|
|
|
} |
|
552
|
|
|
|
|
553
|
225 |
|
if (empty($classname)) { |
|
554
|
|
|
return null; |
|
555
|
|
|
} |
|
556
|
|
|
|
|
557
|
225 |
|
if (isset($cached[$classname])) { |
|
558
|
205 |
|
return $cached[$classname]; |
|
559
|
|
|
} |
|
560
|
|
|
|
|
561
|
36 |
|
if (!isset($class_instance)) { |
|
562
|
9 |
|
$class_instance = new $classname(); |
|
563
|
|
|
} |
|
564
|
|
|
|
|
565
|
|
|
// Check for decorators first |
|
566
|
36 |
|
if (!empty($class_instance->__mgdschema_class_name__)) { |
|
567
|
30 |
|
$parent_class = $class_instance->__mgdschema_class_name__; |
|
568
|
30 |
|
if ( !empty($class_instance->__object) |
|
569
|
30 |
|
&& !$class_instance->__object instanceof $class_instance->__mgdschema_class_name__) { |
|
570
|
|
|
$parent_class = get_class($class_instance->__object); |
|
571
|
30 |
|
debug_add('mgdschema object class ' . $parent_class . ' is not an instance of ' . $class_instance->__mgdschema_class_name__, MIDCOM_LOG_INFO); |
|
572
|
|
|
} |
|
573
|
|
|
} else { |
|
574
|
7 |
|
$parent_class = $classname; |
|
575
|
|
|
} |
|
576
|
|
|
|
|
577
|
36 |
|
$cached[$classname] = self::class_rewrite($parent_class); |
|
578
|
|
|
|
|
579
|
36 |
|
return $cached[$classname]; |
|
580
|
|
|
} |
|
581
|
|
|
|
|
582
|
|
|
/** |
|
583
|
|
|
* Resolve the "name" property of given object |
|
584
|
|
|
* |
|
585
|
|
|
* @see midcom_helper_reflector::get_name_property() |
|
586
|
|
|
* @param object $object the object to get the name property for |
|
587
|
|
|
* @return string name of property or boolean false on failure |
|
588
|
|
|
* @todo when midgard_reflection_property supports flagging name fields use that instead of heuristics |
|
589
|
|
|
*/ |
|
590
|
300 |
|
public function get_name_property_nonstatic($object) |
|
591
|
|
|
{ |
|
592
|
300 |
|
return $this->get_property('name', $object); |
|
593
|
|
|
} |
|
594
|
|
|
|
|
595
|
380 |
|
private function get_property(string $type, $object) |
|
596
|
|
|
{ |
|
597
|
|
|
// Cache results per class within request |
|
598
|
380 |
|
$key = get_class($object); |
|
599
|
380 |
|
if (isset(self::$_cache[$type][$key])) { |
|
600
|
366 |
|
return self::$_cache[$type][$key]; |
|
601
|
|
|
} |
|
602
|
55 |
|
self::$_cache[$type][$key] = false; |
|
603
|
|
|
|
|
604
|
|
|
// Configured properties |
|
605
|
55 |
|
$exceptions = $this->_config->get($type . '_exceptions'); |
|
606
|
55 |
|
foreach ($exceptions as $class => $property) { |
|
607
|
55 |
|
if (midcom::get()->dbfactory->is_a($object, $class)) { |
|
608
|
8 |
|
if ( $property !== false |
|
609
|
8 |
|
&& !$this->_mgd_reflector->property_exists($property)) { |
|
610
|
|
|
debug_add("Matched class '{$key}' to '{$class}' via is_a but property '{$property}' does not exist", MIDCOM_LOG_ERROR); |
|
611
|
|
|
} else { |
|
612
|
8 |
|
self::$_cache[$type][$key] = $property; |
|
613
|
|
|
} |
|
614
|
8 |
|
return self::$_cache[$type][$key]; |
|
615
|
|
|
} |
|
616
|
|
|
} |
|
617
|
|
|
// The simple heuristic |
|
618
|
51 |
|
if ($this->_mgd_reflector->property_exists($type)) { |
|
619
|
24 |
|
self::$_cache[$type][$key] = $type; |
|
620
|
|
|
} |
|
621
|
51 |
|
return self::$_cache[$type][$key]; |
|
622
|
|
|
} |
|
623
|
|
|
|
|
624
|
|
|
/** |
|
625
|
|
|
* Resolve the "name" property of given object |
|
626
|
|
|
* |
|
627
|
|
|
* @see midcom_helper_reflector::get_name_property_nonstatic() |
|
628
|
|
|
* @param object $object the object to get the name property for |
|
629
|
|
|
* @return string name of property or boolean false on failure |
|
630
|
|
|
*/ |
|
631
|
297 |
|
public static function get_name_property($object) |
|
632
|
|
|
{ |
|
633
|
297 |
|
return self::get($object)->get_name_property_nonstatic($object); |
|
634
|
|
|
} |
|
635
|
|
|
|
|
636
|
|
|
/** |
|
637
|
|
|
* Resolve the "title" of given object |
|
638
|
|
|
* |
|
639
|
|
|
* NOTE: This is distinctly different from get_object_label, which will always return something |
|
640
|
|
|
* even if it's just the class name and GUID, also it will for some classes include extra info (like datetimes) |
|
641
|
|
|
* which we do not want here. |
|
642
|
|
|
* |
|
643
|
|
|
* @param object $object the object to get the name property for |
|
644
|
|
|
* @param string $title_property property to use as "name", if left to default (null), will be reflected |
|
645
|
|
|
* @return string value of name property or boolean false on failure |
|
646
|
|
|
*/ |
|
647
|
180 |
|
public static function get_object_title($object, $title_property = null) |
|
648
|
|
|
{ |
|
649
|
180 |
|
if ($title_property === null) { |
|
650
|
180 |
|
$title_property = self::get_title_property($object); |
|
651
|
|
|
} |
|
652
|
180 |
|
if (empty($title_property)) { |
|
653
|
|
|
// Could not resolve valid property |
|
654
|
3 |
|
return false; |
|
655
|
|
|
} |
|
656
|
|
|
|
|
657
|
177 |
|
return (string) $object->{$title_property}; |
|
658
|
|
|
} |
|
659
|
|
|
|
|
660
|
|
|
/** |
|
661
|
|
|
* Resolve the "title" property of given object |
|
662
|
|
|
* |
|
663
|
|
|
* NOTE: This is distinctly different from get_label_property, which will always return something |
|
664
|
|
|
* even if it's just the guid |
|
665
|
|
|
* |
|
666
|
|
|
* @param object $object The object to get the title property for |
|
667
|
|
|
* @return string Name of property or boolean false on failure |
|
668
|
|
|
*/ |
|
669
|
202 |
|
public static function get_title_property($object) |
|
670
|
|
|
{ |
|
671
|
202 |
|
return self::get($object)->get_title_property_nonstatic($object); |
|
672
|
|
|
} |
|
673
|
|
|
|
|
674
|
|
|
/** |
|
675
|
|
|
* Resolve the "title" property of given object |
|
676
|
|
|
* |
|
677
|
|
|
* NOTE: This is distinctly different from get_label_property, which will always return something |
|
678
|
|
|
* even if it's just the guid |
|
679
|
|
|
* |
|
680
|
|
|
* @see midcom_helper_reflector::get_object_title() |
|
681
|
|
|
* @param object $object the object to get the title property for |
|
682
|
|
|
* @return string name of property or boolean false on failure |
|
683
|
|
|
*/ |
|
684
|
216 |
|
public function get_title_property_nonstatic($object) |
|
685
|
|
|
{ |
|
686
|
216 |
|
return $this->get_property('title', $object); |
|
687
|
|
|
} |
|
688
|
|
|
} |
|
689
|
|
|
|