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
|
|
|
/** |
10
|
|
|
* The Grand Unified Reflector, Tree information |
11
|
|
|
* |
12
|
|
|
* @package midcom.helper.reflector |
13
|
|
|
*/ |
14
|
|
|
class midcom_helper_reflector_tree extends midcom_helper_reflector |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Creates a QB instance for root objects |
18
|
|
|
*/ |
19
|
4 |
|
public function _root_objects_qb() |
20
|
|
|
{ |
21
|
4 |
|
$schema_type = $this->mgdschema_class; |
22
|
|
|
|
23
|
4 |
|
$qb = $this->_get_type_qb($schema_type, false); |
24
|
4 |
|
if (!$qb) { |
25
|
|
|
debug_add("Could not get QB for type '{$schema_type}'", MIDCOM_LOG_ERROR); |
26
|
|
|
return false; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
// Only get top level objects |
30
|
4 |
|
if ($upfield = midgard_object_class::get_property_up($schema_type)) { |
31
|
4 |
|
$qb->add_constraint($upfield, '=', 0); |
32
|
|
|
} |
33
|
4 |
|
return $qb; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Get rendered path for object |
38
|
|
|
* |
39
|
|
|
* @param midgard\portable\api\mgdobject $object The object to get path for |
40
|
|
|
* @param string $separator the string used to separate path components |
41
|
|
|
*/ |
42
|
2 |
|
public static function resolve_path($object, string $separator = ' > ') : string |
43
|
|
|
{ |
44
|
2 |
|
$parts = self::resolve_path_parts($object); |
45
|
2 |
|
return implode($separator, array_column($parts, 'label')); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Get path components for object |
50
|
|
|
* |
51
|
|
|
* @param midgard\portable\api\mgdobject $object The object to get path for |
52
|
|
|
*/ |
53
|
4 |
|
public static function resolve_path_parts($object) : array |
54
|
|
|
{ |
55
|
4 |
|
static $cache = []; |
56
|
4 |
|
if (isset($cache[$object->guid])) { |
57
|
3 |
|
return $cache[$object->guid]; |
58
|
|
|
} |
59
|
|
|
|
60
|
2 |
|
$ret = []; |
61
|
2 |
|
$ret[] = [ |
62
|
2 |
|
'object' => $object, |
63
|
2 |
|
'label' => parent::get($object)->get_object_label($object), |
64
|
2 |
|
]; |
65
|
|
|
|
66
|
2 |
|
$parent = $object->get_parent(); |
67
|
2 |
|
while (is_object($parent)) { |
68
|
1 |
|
$ret[] = [ |
69
|
1 |
|
'object' => $parent, |
70
|
1 |
|
'label' => parent::get($parent)->get_object_label($parent), |
71
|
1 |
|
]; |
72
|
1 |
|
$parent = $parent->get_parent(); |
73
|
|
|
} |
74
|
|
|
|
75
|
2 |
|
$cache[$object->guid] = array_reverse($ret); |
76
|
2 |
|
return $cache[$object->guid]; |
77
|
|
|
} |
78
|
|
|
|
79
|
2 |
|
private static function _check_permissions(bool $deleted) : bool |
80
|
|
|
{ |
81
|
|
|
// PONDER: Check for some generic user privilege instead ?? |
82
|
2 |
|
if ( $deleted |
83
|
2 |
|
&& !midcom::get()->auth->admin |
84
|
2 |
|
&& !midcom::get()->auth->is_component_sudo()) { |
85
|
|
|
debug_add('Non-admins are not allowed to list deleted objects', MIDCOM_LOG_ERROR); |
86
|
|
|
return false; |
87
|
|
|
} |
88
|
2 |
|
return true; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Get direct children of given object |
93
|
|
|
* |
94
|
|
|
* @param midgard\portable\api\mgdobject $object object to get children for |
95
|
|
|
* @param boolean $deleted whether to get (only) deleted or not-deleted objects |
96
|
|
|
* @return array multidimensional array (keyed by classname) of objects |
97
|
|
|
*/ |
98
|
2 |
|
public static function get_child_objects(object $object, bool $deleted = false) : array |
99
|
|
|
{ |
100
|
2 |
|
if (!self::_check_permissions($deleted)) { |
101
|
|
|
return []; |
102
|
|
|
} |
103
|
2 |
|
$resolver = new self($object); |
104
|
|
|
|
105
|
2 |
|
$child_objects = []; |
106
|
2 |
|
foreach ($resolver->get_child_classes() as $schema_type) { |
107
|
2 |
|
$qb = $resolver->_child_objects_type_qb($schema_type, $object, $deleted); |
108
|
2 |
|
if (!$qb) { |
109
|
|
|
debug_add('Could not get QB instance', MIDCOM_LOG_ERROR); |
110
|
|
|
continue; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
// Sort by title and name if available |
114
|
2 |
|
self::add_schema_sorts_to_qb($qb, $schema_type); |
115
|
|
|
|
116
|
2 |
|
if ($type_children = $qb->execute()) { |
117
|
|
|
$child_objects[$schema_type] = $type_children; |
118
|
|
|
} |
119
|
|
|
} |
120
|
2 |
|
return $child_objects; |
121
|
|
|
} |
122
|
|
|
|
123
|
96 |
|
private function _get_type_qb(string $schema_type, bool $deleted) |
124
|
|
|
{ |
125
|
96 |
|
if (empty($schema_type)) { |
126
|
|
|
debug_add('Passed schema_type argument is empty, this is fatal', MIDCOM_LOG_ERROR); |
127
|
|
|
return false; |
128
|
|
|
} |
129
|
96 |
|
if ($deleted) { |
130
|
|
|
$qb = new midgard_query_builder($schema_type); |
131
|
|
|
$qb->include_deleted(); |
132
|
|
|
$qb->add_constraint('metadata.deleted', '<>', 0); |
133
|
|
|
return $qb; |
134
|
|
|
} |
135
|
|
|
// Figure correct MidCOM DBA class to use and get midcom QB |
136
|
96 |
|
$midcom_dba_classname = midcom::get()->dbclassloader->get_midcom_class_name_for_mgdschema_object($schema_type); |
137
|
96 |
|
if (empty($midcom_dba_classname)) { |
138
|
|
|
debug_add("MidCOM DBA does not know how to handle {$schema_type}", MIDCOM_LOG_ERROR); |
139
|
|
|
return false; |
140
|
|
|
} |
141
|
|
|
|
142
|
96 |
|
return $midcom_dba_classname::new_query_builder(); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Figure out if $schema_type can be a child of $target_class |
147
|
|
|
*/ |
148
|
94 |
|
private function get_link_field(string $schema_type, string $target_class) : ?array |
149
|
|
|
{ |
150
|
94 |
|
static $cache = []; |
151
|
94 |
|
$cache_key = $schema_type . '-' . $target_class; |
152
|
94 |
|
if (!array_key_exists($cache_key, $cache)) { |
153
|
5 |
|
$cache[$cache_key] = null; |
154
|
5 |
|
$ref = new midgard_reflection_property($schema_type); |
155
|
|
|
|
156
|
5 |
|
$candidates = array_filter([ |
157
|
5 |
|
'up' => midgard_object_class::get_property_up($schema_type), |
158
|
5 |
|
'parent' => midgard_object_class::get_property_parent($schema_type) |
159
|
5 |
|
]); |
160
|
|
|
|
161
|
5 |
|
foreach ($candidates as $type => $field) { |
162
|
5 |
|
$info = [ |
163
|
5 |
|
'type' => $type, |
164
|
5 |
|
'name' => $field, |
165
|
5 |
|
'target' => $ref->get_link_target($field), |
166
|
5 |
|
'upfield' => $candidates['up'] ?? null |
167
|
5 |
|
]; |
168
|
|
|
|
169
|
5 |
|
if ($linked_class = $ref->get_link_name($field)) { |
170
|
5 |
|
if (!self::is_same_class($linked_class, $target_class)) { |
171
|
|
|
// This link points elsewhere |
172
|
5 |
|
continue; |
173
|
|
|
} |
174
|
5 |
|
} elseif ($ref->get_midgard_type($field) === MGD_TYPE_GUID && empty($info['target'])) { |
175
|
|
|
// Guid link without class specification, valid for all classes |
176
|
5 |
|
$info['target'] = 'guid'; |
177
|
|
|
} |
178
|
5 |
|
$cache[$cache_key] = $info; |
179
|
5 |
|
break; |
180
|
|
|
} |
181
|
|
|
} |
182
|
94 |
|
return $cache[$cache_key]; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Creates a QB instance for get_child_objects |
187
|
|
|
*/ |
188
|
94 |
|
public function _child_objects_type_qb(string $schema_type, object $for_object, bool $deleted) |
189
|
|
|
{ |
190
|
94 |
|
$qb = $this->_get_type_qb($schema_type, $deleted); |
191
|
94 |
|
if (!$qb) { |
192
|
|
|
debug_add("Could not get QB for type '{$schema_type}'", MIDCOM_LOG_ERROR); |
193
|
|
|
return false; |
194
|
|
|
} |
195
|
|
|
|
196
|
94 |
|
if ($info = $this->get_link_field($schema_type, $for_object::class)) { |
197
|
94 |
|
$qb->add_constraint($info['name'], '=', $for_object->{$info['target']}); |
198
|
|
|
// we only return direct children (otherwise they would turn up twice in recursive queries) |
199
|
94 |
|
if ($info['type'] == 'parent' && $info['upfield']) { |
200
|
90 |
|
$qb->add_constraint($info['upfield'], '=', 0); |
201
|
|
|
} |
202
|
94 |
|
return $qb; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
debug_add("Class '{$schema_type}' has no valid link properties pointing to class '" . $for_object::class . "', this should not happen here", MIDCOM_LOG_ERROR); |
206
|
|
|
return false; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Get the parent class of the class this reflector was instantiated for |
211
|
|
|
*/ |
212
|
2 |
|
public function get_parent_class() : ?string |
213
|
|
|
{ |
214
|
2 |
|
$parent_property = midgard_object_class::get_property_parent($this->mgdschema_class); |
215
|
2 |
|
if (!$parent_property) { |
216
|
2 |
|
return null; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
return $this->get_link_name($parent_property); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Get the child classes of the class this reflector was instantiated for |
224
|
|
|
*/ |
225
|
102 |
|
public function get_child_classes() : array |
226
|
|
|
{ |
227
|
102 |
|
static $cache = []; |
228
|
102 |
|
if (!isset($cache[$this->mgdschema_class])) { |
229
|
4 |
|
$cache[$this->mgdschema_class] = []; |
230
|
|
|
|
231
|
4 |
|
$types = array_diff(midcom_connection::get_schema_types(), $this->_config->get_array('child_class_exceptions_neverchild')); |
232
|
4 |
|
foreach ($types as $schema_type) { |
233
|
4 |
|
if ($this->get_link_field($schema_type, $this->mgdschema_class)) { |
234
|
4 |
|
$cache[$this->mgdschema_class][] = $schema_type; |
235
|
|
|
} |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
//make sure children of the same type come out on top |
239
|
4 |
|
if ($key = array_search($this->mgdschema_class, $cache[$this->mgdschema_class])) { |
240
|
2 |
|
unset($cache[$this->mgdschema_class][$key]); |
241
|
2 |
|
array_unshift($cache[$this->mgdschema_class], $this->mgdschema_class); |
242
|
|
|
} |
243
|
|
|
} |
244
|
102 |
|
return $cache[$this->mgdschema_class]; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* Get an array of "root level" classes |
249
|
|
|
*/ |
250
|
8 |
|
public static function get_root_classes() : array |
251
|
|
|
{ |
252
|
8 |
|
static $root_classes = null; |
253
|
8 |
|
return $root_classes ??= self::_resolve_root_classes(); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* Resolves the "root level" DBA classes, used by get_root_classes() |
258
|
|
|
*/ |
259
|
|
|
private static function _resolve_root_classes() : array |
260
|
|
|
{ |
261
|
|
|
$root_exceptions_notroot = midcom_baseclasses_components_configuration::get('midcom.helper.reflector', 'config')->get_array('root_class_exceptions_notroot'); |
262
|
|
|
$root_classes = []; |
263
|
|
|
$types = array_diff(midcom_connection::get_schema_types(), $root_exceptions_notroot); |
264
|
|
|
foreach ($types as $schema_type) { |
265
|
|
|
// Class extensions mapping |
266
|
|
|
$schema_type = self::class_rewrite($schema_type); |
267
|
|
|
|
268
|
|
|
// Make sure we only add classes once |
269
|
|
|
if (in_array($schema_type, $root_classes)) { |
270
|
|
|
// Already listed |
271
|
|
|
continue; |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
if (midgard_object_class::get_property_parent($schema_type)) { |
275
|
|
|
// type has parent set, thus cannot be root type |
276
|
|
|
continue; |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
if (!midcom::get()->dbclassloader->get_midcom_class_name_for_mgdschema_object($schema_type)) { |
280
|
|
|
// Not a MidCOM DBA object, skip |
281
|
|
|
continue; |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
$root_classes[] = $schema_type; |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
usort($root_classes, strnatcmp(...)); |
|
|
|
|
288
|
|
|
return $root_classes; |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* Add default ("title" and "name") sorts to a QB instance |
293
|
|
|
* |
294
|
|
|
* @param midgard_query_builder $qb QB instance |
295
|
|
|
*/ |
296
|
2 |
|
public static function add_schema_sorts_to_qb($qb, string $schema_type) |
297
|
|
|
{ |
298
|
|
|
// Sort by "title" and "name" if available |
299
|
2 |
|
$dummy = new $schema_type(); |
300
|
2 |
|
if ($title_property = self::get_title_property($dummy)) { |
301
|
2 |
|
$qb->add_order($title_property); |
302
|
|
|
} |
303
|
2 |
|
if ($name_property = self::get_name_property($dummy)) { |
304
|
2 |
|
$qb->add_order($name_property); |
305
|
|
|
} |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
/** |
309
|
|
|
* List object children |
310
|
|
|
*/ |
311
|
|
|
public static function get_tree(midcom_core_dbaobject $parent) : array |
312
|
|
|
{ |
313
|
|
|
$tree = []; |
314
|
|
|
|
315
|
|
|
foreach (self::get_child_objects($parent) as $class => $objects) { |
316
|
|
|
$reflector = parent::get($class); |
317
|
|
|
|
318
|
|
|
foreach ($objects as $object) { |
319
|
|
|
$leaf = [ |
320
|
|
|
'title' => $reflector->get_object_label($object), |
321
|
|
|
'icon' => $reflector->get_object_icon($object), |
322
|
|
|
'class' => $class |
323
|
|
|
]; |
324
|
|
|
if ($grandchildren = self::get_tree($object)) { |
325
|
|
|
$leaf['children'] = $grandchildren; |
326
|
|
|
} |
327
|
|
|
$tree[] = $leaf; |
328
|
|
|
} |
329
|
|
|
} |
330
|
|
|
return $tree; |
331
|
|
|
} |
332
|
|
|
} |
333
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths