/** * Copy an object tree. Both source and parent may be liberally filled. Source can be either * MgdSchema or MidCOM db object or GUID of the object and parent can be * * - MgdSchema object * - MidCOM db object * - predefined target array (@see get_target_properties()) * - ID or GUID of the object * - left empty to copy as a parentless object * * This method is self-aware and will refuse to perform any infinite loops (e.g. to copy * itself to its descendant, copying itself again and again and again). * * Eventually this method will return the first root object that was created, i.e. the root * of the new tree. * * @param mixed $source GUID or MgdSchema object that will be copied * @param mixed $parent MgdSchema or MidCOM db object, predefined array or ID of the parent object * @param array $exclude IDs that will be excluded from the copying * @param boolean $parameters Switch to determine if the parameters should be copied * @param boolean $metadata Switch to determine if the metadata should be copied (excluding created and published) * @param boolean $attachments Switch to determine if the attachments should be copied (creates only a new link, doesn't duplicate the content) * @return mixed False on failure, newly created MgdSchema root object on success */ public static function copy_object_tree($source, $parent, $exclude = array(), $parameters = true, $metadata = true, $attachments = true) { $copy = new midcom_helper_reflector_copy(); $copy->source =& $source; $copy->target =& $parent; $copy->parameters = $parameters; $copy->metadata = $metadata; $copy->attachments = $attachments; if (!$copy->execute()) { return false; } return $copy->get_object(); }
private function _process_copy($target) { // Get the target information of the form $target['id'] = $this->_controller->datamanager->types[$target['parent']]->convert_to_storage(); $this->_controller->datamanager->types['metadata']->convert_to_storage(); $this->_controller->datamanager->types['attachments']->convert_to_storage(); $this->_controller->datamanager->types['privileges']->convert_to_storage(); $copy = new midcom_helper_reflector_copy(); $copy->source = $this->_object; // Set the target - if available if (isset($target['id']) && $target['id']) { $link_properties = $target['reflector']->get_link_properties(); $parent = $target['parent']; if (!$link_properties || !isset($link_properties[$parent])) { throw new midcom_error('Failed to construct the target class object'); } $class_name = $link_properties[$parent]['class']; $target_object = new $class_name($target['id']); if ($target_object && $target_object->guid) { $copy->target = $target_object; } else { throw new midcom_error('Failed to get the target object'); } } // Copying of parameters, metadata and such $copy->parameters = $this->_controller->datamanager->types['parameters']->convert_to_storage(); $copy->metadata = $this->_controller->datamanager->types['metadata']->convert_to_storage(); $copy->attachments = $this->_controller->datamanager->types['attachments']->convert_to_storage(); $copy->privileges = $this->_controller->datamanager->types['privileges']->convert_to_storage(); if ($this->_request_data['handler_id'] === '____mfa-asgard-object_copy_tree') { foreach ($_POST['all_objects'] as $guid) { if (!in_array($guid, $_POST['selected'])) { $copy->exclude[] = $guid; } } } else { $copy->copy_tree = false; } if (!$copy->execute()) { debug_print_r('Copying failed with the following errors', $copy->errors, MIDCOM_LOG_ERROR); throw new midcom_error('Failed to successfully copy the object. Details in error level log'); } $new_object = $copy->get_object(); if (!$new_object || !$new_object->guid) { throw new midcom_error('Failed to copy the object'); } if ($this->_request_data['handler_id'] === '____mfa-asgard-object_copy_tree') { midcom::get('uimessages')->add($this->_l10n->get('midgard.admin.asgard'), $this->_l10n->get('copy successful, you have been relocated to the root of the new object tree')); } else { midcom::get('uimessages')->add($this->_l10n->get('midgard.admin.asgard'), $this->_l10n->get('copy successful, you have been relocated to the new object')); } return $new_object; }