|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @package midgard.admin.asgard |
|
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 midcom\datamanager\datamanager; |
|
10
|
|
|
use midcom\datamanager\controller; |
|
11
|
|
|
use midcom\datamanager\schemadb; |
|
12
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Object management interface |
|
16
|
|
|
* |
|
17
|
|
|
* @package midgard.admin.asgard |
|
18
|
|
|
*/ |
|
19
|
|
|
class midgard_admin_asgard_handler_object_manage extends midcom_baseclasses_components_handler |
|
20
|
|
|
{ |
|
21
|
|
|
use midgard_admin_asgard_handler; |
|
|
|
|
|
|
22
|
|
|
|
|
23
|
|
|
private ?midcom_core_dbaobject $_object = null; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var datamanager |
|
27
|
|
|
*/ |
|
28
|
|
|
private $datamanager; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var controller |
|
32
|
|
|
*/ |
|
33
|
|
|
private $controller; |
|
34
|
|
|
|
|
35
|
|
|
private schemadb $schemadb; |
|
36
|
|
|
|
|
37
|
7 |
|
public function _on_initialize() |
|
38
|
|
|
{ |
|
39
|
7 |
|
midcom::get()->auth->require_user_do('midgard.admin.asgard:manage_objects', class: 'midgard_admin_asgard_plugin'); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Retrieve the object from the db |
|
44
|
|
|
*/ |
|
45
|
4 |
|
private function _load_object(string $guid) |
|
46
|
|
|
{ |
|
47
|
|
|
try { |
|
48
|
4 |
|
$this->_object = midcom::get()->dbfactory->get_object_by_guid($guid); |
|
49
|
|
|
} catch (midcom_error $e) { |
|
50
|
|
|
if (midcom_connection::get_error() == MGD_ERR_OBJECT_DELETED) { |
|
51
|
|
|
$relocate = $this->router->generate('object_deleted', ['guid' => $guid]); |
|
52
|
|
|
midcom::get()->relocate($relocate); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
throw $e; |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Simple helper which references all important members to the request data listing |
|
61
|
|
|
* for usage within the style listing. |
|
62
|
|
|
*/ |
|
63
|
6 |
|
private function _prepare_request_data() |
|
64
|
|
|
{ |
|
65
|
6 |
|
$this->_request_data['object'] = $this->_object; |
|
66
|
6 |
|
$this->_request_data['controller'] = $this->controller; |
|
67
|
6 |
|
$this->_request_data['datamanager'] = $this->datamanager; |
|
68
|
6 |
|
$this->_request_data['style_helper'] = new midgard_admin_asgard_stylehelper($this->_request_data); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Loads the schemadb from the helper class |
|
73
|
|
|
*/ |
|
74
|
7 |
|
private function _load_schemadb(?midcom_core_dbaobject $object = null, ?array $include_fields = null, bool $add_copy_fields = false) |
|
75
|
|
|
{ |
|
76
|
7 |
|
$schema_helper = new midgard_admin_asgard_schemadb($object ?? $this->_object, $this->_config); |
|
|
|
|
|
|
77
|
7 |
|
$schema_helper->add_copy_fields = $add_copy_fields; |
|
78
|
7 |
|
$this->schemadb = $schema_helper->create($include_fields); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Looks up the user's default mode and redirects there. This is mainly useful for links from outside Asgard |
|
83
|
|
|
*/ |
|
84
|
|
|
public function _handler_open(string $guid, array &$data) |
|
85
|
|
|
{ |
|
86
|
|
|
$relocate = $this->router->generate('object_' . $data['default_mode'], ['guid' => $guid]); |
|
87
|
|
|
return new midcom_response_relocate($relocate); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Object display |
|
92
|
|
|
*/ |
|
93
|
1 |
|
public function _handler_view(string $handler_id, string $guid, array &$data) |
|
94
|
|
|
{ |
|
95
|
1 |
|
$this->_load_object($guid); |
|
96
|
1 |
|
$this->_load_schemadb(); |
|
97
|
|
|
|
|
98
|
|
|
// Hide the revision message |
|
99
|
1 |
|
$this->schemadb->get_first()->get_field('_rcs_message')['hidden'] = true; |
|
100
|
|
|
|
|
101
|
1 |
|
$this->datamanager = (new datamanager($this->schemadb)) |
|
102
|
1 |
|
->set_storage($this->_object); |
|
103
|
|
|
|
|
104
|
1 |
|
midgard_admin_asgard_plugin::bind_to_object($this->_object, $handler_id, $data); |
|
105
|
1 |
|
$this->_prepare_request_data(); |
|
106
|
1 |
|
return $this->get_response('midgard_admin_asgard_object_view'); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Object editing view |
|
111
|
|
|
*/ |
|
112
|
1 |
|
public function _handler_edit(Request $request, string $handler_id, string $guid, array &$data) |
|
113
|
|
|
{ |
|
114
|
1 |
|
$this->_load_object($guid); |
|
115
|
1 |
|
$this->_object->require_do('midgard:update'); |
|
116
|
1 |
|
$this->_load_schemadb(); |
|
117
|
|
|
|
|
118
|
1 |
|
$this->controller = (new datamanager($this->schemadb)) |
|
119
|
1 |
|
->set_storage($this->_object, 'default') |
|
120
|
1 |
|
->get_controller(); |
|
121
|
1 |
|
switch ($this->controller->handle($request)) { |
|
122
|
1 |
|
case 'save': |
|
123
|
|
|
// Reindex the object |
|
124
|
|
|
//$indexer = midcom::get()->indexer; |
|
125
|
|
|
//net_nemein_wiki_viewer::index($this->_request_data['controller']->datamanager, $indexer, $this->_topic); |
|
126
|
|
|
//Fall-through |
|
127
|
|
|
|
|
128
|
1 |
|
case 'cancel': |
|
129
|
|
|
return $this->_prepare_relocate($this->_object); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
1 |
|
$this->_prepare_request_data(); |
|
133
|
1 |
|
midgard_admin_asgard_plugin::bind_to_object($this->_object, $handler_id, $data); |
|
134
|
1 |
|
return $this->get_response('midgard_admin_asgard_object_edit'); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Object creating view |
|
139
|
|
|
*/ |
|
140
|
3 |
|
public function _handler_create(Request $request, string $handler_id, array $args, array &$data) |
|
141
|
|
|
{ |
|
142
|
3 |
|
$data['current_type'] = $args[0]; |
|
143
|
3 |
|
$create_type = midcom::get()->dbclassloader->get_midcom_class_name_for_mgdschema_object($data['current_type']); |
|
144
|
3 |
|
if (!$create_type) { |
|
145
|
|
|
throw new midcom_error_notfound('Failed to find type for the new object'); |
|
146
|
|
|
} |
|
147
|
3 |
|
$new_object = new $create_type(); |
|
148
|
|
|
|
|
149
|
3 |
|
if (in_array($handler_id, ['object_create_toplevel', 'object_create_chooser'])) { |
|
150
|
2 |
|
midcom::get()->auth->require_user_do('midgard:create', class: $create_type); |
|
151
|
|
|
|
|
152
|
2 |
|
$data['view_title'] = sprintf($this->_l10n_midcom->get('create %s'), midgard_admin_asgard_plugin::get_type_label($data['current_type'])); |
|
153
|
|
|
} else { |
|
154
|
1 |
|
$this->_object = midcom::get()->dbfactory->get_object_by_guid($args[1]); |
|
155
|
1 |
|
$this->_object->require_do('midgard:create'); |
|
156
|
1 |
|
midgard_admin_asgard_plugin::bind_to_object($this->_object, $handler_id, $data); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
3 |
|
$this->_load_schemadb($new_object); |
|
160
|
|
|
|
|
161
|
3 |
|
$this->controller = (new datamanager($this->schemadb)) |
|
162
|
3 |
|
->set_defaults($this->get_defaults($request, $create_type)) |
|
|
|
|
|
|
163
|
3 |
|
->set_storage($new_object, 'default') |
|
164
|
3 |
|
->get_controller(); |
|
165
|
|
|
|
|
166
|
3 |
|
if ($handler_id === 'object_create_chooser') { |
|
167
|
1 |
|
midcom::get()->head->set_pagetitle($data['view_title']); |
|
168
|
1 |
|
$workflow = $this->get_workflow('chooser', [ |
|
169
|
1 |
|
'controller' => $this->controller |
|
170
|
1 |
|
]); |
|
171
|
1 |
|
return $workflow->run($request); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
2 |
|
switch ($this->controller->handle($request)) { |
|
175
|
2 |
|
case 'save': |
|
176
|
|
|
// Reindex the object |
|
177
|
|
|
//$indexer = midcom::get()->indexer; |
|
178
|
|
|
//net_nemein_wiki_viewer::index($this->_request_data['controller']->datamanager, $indexer, $this->_topic); |
|
179
|
|
|
return $this->_prepare_relocate($new_object); |
|
180
|
|
|
|
|
181
|
2 |
|
case 'cancel': |
|
182
|
|
|
if ($this->_object) { |
|
183
|
|
|
return $this->_prepare_relocate($this->_object); |
|
184
|
|
|
} |
|
185
|
|
|
return new midcom_response_relocate($this->router->generate('type', ['type' => $args[0]])); |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
2 |
|
$this->_prepare_request_data(); |
|
189
|
2 |
|
return $this->get_response('midgard_admin_asgard_object_create'); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
3 |
|
private function get_defaults(Request $request, string $new_type) : array |
|
193
|
|
|
{ |
|
194
|
3 |
|
$defaults = []; |
|
195
|
3 |
|
if ($this->_object) { |
|
196
|
|
|
// Figure out the linking property |
|
197
|
1 |
|
$parent_property = midgard_object_class::get_property_parent($this->_request_data['current_type']); |
|
198
|
1 |
|
$new_type_reflector = midcom_helper_reflector::get($new_type); |
|
199
|
1 |
|
$link_properties = $new_type_reflector->get_link_properties(); |
|
200
|
1 |
|
foreach ($link_properties as $property => $link) { |
|
201
|
1 |
|
if ( ($link['class'] && midcom_helper_reflector::is_same_class($link['class'], $this->_object->__mgdschema_class_name__)) |
|
202
|
1 |
|
|| $link['type'] == MGD_TYPE_GUID) { |
|
203
|
1 |
|
$defaults[$property] = $this->_object->{$link['target']}; |
|
204
|
1 |
|
} elseif ( $property == $parent_property |
|
205
|
1 |
|
&& midcom_helper_reflector::is_same_class($new_type, $this->_object->__mgdschema_class_name__)) { |
|
206
|
|
|
$defaults[$property] = $this->_object->$parent_property; |
|
207
|
|
|
} |
|
208
|
|
|
} |
|
209
|
1 |
|
if (empty($defaults)) { |
|
210
|
|
|
throw new midcom_error("Could not establish link between {$new_type} and " . $this->_object::class); |
|
211
|
|
|
} |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
// Allow setting defaults from query string, useful for things like "create event for today" and chooser |
|
215
|
3 |
|
if ($request->query->has('defaults')) { |
|
216
|
|
|
$get_defaults = array_intersect_key($request->query->all('defaults'), $this->schemadb->get_first()->get('fields')); |
|
217
|
|
|
$defaults = array_merge($defaults, array_map(trim(...), $get_defaults)); |
|
|
|
|
|
|
218
|
|
|
} |
|
219
|
3 |
|
return $defaults; |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
1 |
|
private function _prepare_relocate(midcom_core_dbaobject $object, string $mode = 'default') : midcom_response_relocate |
|
223
|
|
|
{ |
|
224
|
|
|
// Redirect parameters to overview |
|
225
|
1 |
|
if ($object instanceof midcom_db_parameter) { |
|
226
|
|
|
return new midcom_response_relocate($this->router->generate('object_parameters', ['guid' => $object->parentguid])); |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
1 |
|
if ($mode == 'delete') { |
|
230
|
|
|
// Redirect person deletion to user management |
|
231
|
|
|
if ($object instanceof midcom_db_person) { |
|
232
|
|
|
return new midcom_response_relocate("__mfa/asgard_midgard.admin.user/"); |
|
233
|
|
|
} |
|
234
|
|
|
if ($parent = $object->get_parent()) { |
|
235
|
|
|
return $this->_prepare_relocate($parent); |
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
|
|
$url = $this->router->generate('type', ['type' => $object->__mgdschema_class_name__]); |
|
239
|
|
|
} else { |
|
240
|
|
|
// Redirect persons to user management |
|
241
|
1 |
|
if ($object instanceof midcom_db_person) { |
|
242
|
|
|
return new midcom_response_relocate("__mfa/asgard_midgard.admin.user/edit/{$object->guid}/"); |
|
243
|
|
|
} |
|
244
|
|
|
// Redirect to default object mode page. |
|
245
|
1 |
|
$url = $this->router->generate('object_' . $this->_request_data['default_mode'], ['guid' => $object->guid]); |
|
246
|
|
|
} |
|
247
|
1 |
|
return new midcom_response_relocate($url); |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
/** |
|
251
|
|
|
* Object deletion |
|
252
|
|
|
*/ |
|
253
|
1 |
|
public function _handler_delete(Request $request, string $handler_id, string $guid, array &$data) |
|
254
|
|
|
{ |
|
255
|
1 |
|
$this->_load_object($guid); |
|
256
|
1 |
|
$this->_object->require_do('midgard:delete'); |
|
257
|
1 |
|
$this->_load_schemadb(); |
|
258
|
|
|
|
|
259
|
1 |
|
$this->datamanager = (new datamanager($this->schemadb)) |
|
260
|
1 |
|
->set_storage($this->_object, 'default'); |
|
261
|
|
|
|
|
262
|
1 |
|
if ($request->request->has('midgard_admin_asgard_deleteok')) { |
|
263
|
|
|
// Deletion confirmed. |
|
264
|
|
|
$this->_object->_use_rcs = !$request->request->getBoolean('midgard_admin_asgard_disablercs'); |
|
265
|
|
|
|
|
266
|
|
|
if (!$this->_object->delete_tree()) { |
|
267
|
|
|
throw new midcom_error("Failed to delete object {$guid}, last Midgard error was: " . midcom_connection::get_error_string()); |
|
268
|
|
|
} |
|
269
|
|
|
|
|
270
|
|
|
return $this->_prepare_relocate($this->_object, 'delete'); |
|
271
|
|
|
} |
|
272
|
|
|
|
|
273
|
1 |
|
if ($request->request->has('midgard_admin_asgard_deletecancel')) { |
|
274
|
|
|
return $this->_prepare_relocate($this->_object); |
|
275
|
|
|
} |
|
276
|
|
|
|
|
277
|
1 |
|
midgard_admin_asgard_plugin::bind_to_object($this->_object, $handler_id, $data); |
|
278
|
1 |
|
$this->_prepare_request_data(); |
|
279
|
1 |
|
$this->_add_jscripts(); |
|
280
|
|
|
|
|
281
|
|
|
// Initialize the tree |
|
282
|
1 |
|
$data['tree'] = new midgard_admin_asgard_copytree($this->_object, $data); |
|
283
|
1 |
|
$data['tree']->copy_tree = false; |
|
284
|
1 |
|
$data['tree']->inputs = false; |
|
285
|
|
|
|
|
286
|
1 |
|
return $this->get_response('midgard_admin_asgard_object_delete'); |
|
287
|
|
|
} |
|
288
|
|
|
|
|
289
|
|
|
/** |
|
290
|
|
|
* Copy handler |
|
291
|
|
|
*/ |
|
292
|
1 |
|
public function _handler_copy(Request $request, string $handler_id, string $guid, array &$data) |
|
293
|
|
|
{ |
|
294
|
1 |
|
$this->_load_object($guid); |
|
295
|
|
|
|
|
296
|
1 |
|
$parent = midcom_helper_reflector_copy::get_parent_property($this->_object); |
|
297
|
1 |
|
$this->_load_schemadb($this->_object, [$parent], true); |
|
298
|
1 |
|
if ($parent) { |
|
299
|
|
|
// Change the name for the parent field |
|
300
|
1 |
|
$this->schemadb->get_first()->get_field($parent)['title'] = $this->_l10n->get('choose the target'); |
|
301
|
|
|
} |
|
302
|
|
|
|
|
303
|
1 |
|
$this->controller = (new datamanager($this->schemadb))->get_controller(); |
|
304
|
|
|
|
|
305
|
1 |
|
$this->_prepare_request_data(); |
|
306
|
1 |
|
$reflector = new midcom_helper_reflector($this->_object); |
|
|
|
|
|
|
307
|
|
|
|
|
308
|
|
|
// Process the form |
|
309
|
1 |
|
switch ($this->controller->handle($request)) { |
|
310
|
1 |
|
case 'save': |
|
311
|
1 |
|
$new_object = $this->_process_copy($request, $parent, $reflector); |
|
312
|
|
|
// Relocate to the newly created object |
|
313
|
1 |
|
return $this->_prepare_relocate($new_object); |
|
314
|
|
|
|
|
315
|
1 |
|
case 'cancel': |
|
316
|
|
|
return $this->_prepare_relocate($this->_object); |
|
317
|
|
|
} |
|
318
|
|
|
|
|
319
|
1 |
|
$this->_add_jscripts(); |
|
320
|
|
|
|
|
321
|
|
|
// Common hooks for Asgard |
|
322
|
1 |
|
midgard_admin_asgard_plugin::bind_to_object($this->_object, $handler_id, $data); |
|
323
|
|
|
|
|
324
|
|
|
// Set the page title |
|
325
|
1 |
|
$label = $reflector->get_object_label($this->_object); |
|
326
|
1 |
|
$data['page_title'] = sprintf($this->_l10n->get('copy %s'), $label); |
|
327
|
|
|
|
|
328
|
1 |
|
return $this->get_response(); |
|
329
|
|
|
} |
|
330
|
|
|
|
|
331
|
|
|
/** |
|
332
|
|
|
* Add the necessary static files for copy/delete operations |
|
333
|
|
|
*/ |
|
334
|
2 |
|
private function _add_jscripts() |
|
335
|
|
|
{ |
|
336
|
|
|
// Add Colorbox |
|
337
|
2 |
|
midcom::get()->head->add_jsfile(MIDCOM_STATIC_URL . '/jQuery/colorbox/jquery.colorbox-min.js'); |
|
338
|
2 |
|
$this->add_stylesheet(MIDCOM_STATIC_URL . '/jQuery/colorbox/colorbox.css', 'screen'); |
|
339
|
2 |
|
midcom::get()->head->add_jsfile(MIDCOM_STATIC_URL . '/midgard.admin.asgard/object_browser.js'); |
|
340
|
|
|
|
|
341
|
|
|
// Add jQuery file for the checkbox operations |
|
342
|
2 |
|
midcom::get()->head->add_jsfile(MIDCOM_STATIC_URL . '/midgard.admin.asgard/jquery-copytree.js'); |
|
343
|
|
|
} |
|
344
|
|
|
|
|
345
|
1 |
|
private function _process_copy(Request $request, ?string $parent, midcom_helper_reflector $reflector) : midcom_core_dbaobject |
|
346
|
|
|
{ |
|
347
|
1 |
|
$formdata = $this->controller->get_datamanager()->get_content_raw(); |
|
348
|
1 |
|
$copy = new midcom_helper_reflector_copy(); |
|
349
|
|
|
|
|
350
|
|
|
// Copying of parameters, metadata and such |
|
351
|
1 |
|
$copy->parameters = $formdata['parameters']; |
|
352
|
1 |
|
$copy->metadata = $formdata['metadata']; |
|
353
|
1 |
|
$copy->attachments = $formdata['attachments']; |
|
354
|
1 |
|
$copy->privileges = $formdata['privileges']; |
|
355
|
|
|
|
|
356
|
|
|
// Set the target - if available |
|
357
|
1 |
|
if (!empty($formdata[$parent])) { |
|
358
|
|
|
$link_properties = $reflector->get_link_properties(); |
|
359
|
|
|
|
|
360
|
|
|
if (empty($link_properties[$parent])) { |
|
361
|
|
|
throw new midcom_error('Failed to construct the target class object'); |
|
362
|
|
|
} |
|
363
|
|
|
$class_name = midcom::get()->dbclassloader->get_midcom_class_name_for_mgdschema_object($link_properties[$parent]['class']); |
|
364
|
|
|
$copy->target = new $class_name($formdata[$parent]); |
|
365
|
|
|
} |
|
366
|
1 |
|
$copy->exclude = array_diff($request->request->all('all_objects'), $request->request->all('selected')); |
|
367
|
1 |
|
$new_object = $copy->execute($this->_object); |
|
|
|
|
|
|
368
|
|
|
|
|
369
|
1 |
|
if ($new_object === null) { |
|
370
|
|
|
debug_print_r('Copying failed with the following errors', $copy->errors, MIDCOM_LOG_ERROR); |
|
371
|
|
|
throw new midcom_error('Failed to successfully copy the object. Details in error level log'); |
|
372
|
|
|
} |
|
373
|
|
|
|
|
374
|
1 |
|
midcom::get()->uimessages->add($this->_l10n->get($this->_component), $this->_l10n->get('copy successful, you have been relocated to the new object')); |
|
375
|
1 |
|
return $new_object; |
|
376
|
|
|
} |
|
377
|
|
|
|
|
378
|
|
|
/** |
|
379
|
|
|
* Show copy style |
|
380
|
|
|
*/ |
|
381
|
1 |
|
public function _show_copy(string $handler_id, array &$data) |
|
382
|
|
|
{ |
|
383
|
|
|
// Show the tree hierarchy |
|
384
|
1 |
|
$data['tree'] = new midgard_admin_asgard_copytree($this->_object, $data); |
|
|
|
|
|
|
385
|
1 |
|
$data['tree']->inputs = true; |
|
386
|
1 |
|
$data['tree']->copy_tree = true; |
|
387
|
1 |
|
midcom_show_style('midgard_admin_asgard_object_copytree'); |
|
388
|
|
|
|
|
389
|
1 |
|
midcom_show_style('midgard_admin_asgard_object_copy'); |
|
390
|
|
|
} |
|
391
|
|
|
} |
|
392
|
|
|
|