function midgard_admin_asgard_trash_type_show($object, $indent = 0, $prefix = '', $enable_undelete = true) { static $persons = array(); static $shown = array(); static $url_prefix = ''; if (!$url_prefix) { $url_prefix = midcom_core_context::get()->get_key(MIDCOM_CONTEXT_ANCHORPREFIX); } if (isset($shown[$object->guid])) { return; } if (!isset($persons[$object->metadata->revisor])) { $persons[$object->metadata->revisor] = midcom::get('auth')->get_user($object->metadata->revisor); } $reflector = midcom_helper_reflector_tree::get($object); $icon = $reflector->get_object_icon($object); echo "{$prefix}<tr>\n"; $disabled = ''; if (!$enable_undelete) { $disabled = ' disabled="disabled"'; } $object_label = $reflector->get_object_label($object); if (empty($object_label)) { $object_label = $object->guid; } echo "{$prefix} <td class=\"checkbox\"><input type=\"checkbox\" name=\"undelete[]\"{$disabled} value=\"{$object->guid}\" id=\"guid_{$object->guid}\" /></td>\n"; echo "{$prefix} <td class=\"label\" style=\"padding-left: {$indent}px\"><label for=\"guid_{$object->guid}\">{$icon}" . $object_label . "</label></td>\n"; echo "{$prefix} <td class=\"nowrap\">" . strftime('%x %X', strtotime($object->metadata->revised)) . "</td>\n"; if (isset($persons[$object->metadata->revisor]->guid)) { echo "{$prefix} <td><a href=\"{$url_prefix}__mfa/asgard/object/view/{$persons[$object->metadata->revisor]->guid}/\">{$persons[$object->metadata->revisor]->name}</a></td>\n"; } else { echo "{$prefix} <td> </td>\n"; } echo "{$prefix} <td>" . midcom_helper_misc::filesize_to_string($object->metadata->size) . "</td>\n"; echo "{$prefix}</tr>\n"; $child_types = midcom_helper_reflector_tree::get_child_objects($object, true); if (is_array($child_types) && count($child_types) > 0) { $child_indent = $indent + 20; echo "{$prefix}<tbody class=\"children\">\n"; foreach ($child_types as $type => $children) { if (count($children) < 10 || isset($_GET['show_children'][$object->guid][$type])) { foreach ($children as $child) { midgard_admin_asgard_trash_type_show($child, $child_indent, "{$prefix} ", false); } } else { echo "{$prefix} <tr>\n"; echo "{$prefix} <td class=\"label\" style=\"padding-left: {$child_indent}px\" colspan=\"5\"><a href=\"?show_children[{$object->guid}][{$type}]=1\">" . sprintf(midcom::get('i18n')->get_string('show %s %s children', 'midgard.admin.asgard'), count($children), midgard_admin_asgard_plugin::get_type_label($type)) . "</a></td>\n"; echo "{$prefix} </tr>\n"; } } echo "{$prefix}</tbody>\n"; } $shown[$object->guid] = true; }
<?php /** * @package midcom.helper.reflector * @author The Midgard Project, http://www.midgard-project.org * @copyright The Midgard Project, http://www.midgard-project.org * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License */ if (!isset($_GET['guid']) || empty($_GET['guid'])) { throw new midcom_error_notfound('Specify $guid via GET for info'); } $object = midcom::get('dbfactory')->get_object_by_guid($_GET['guid']); $reflector =& midcom_helper_reflector::get($object); echo "Got " . $reflector->get_class_label() . ' "' . $reflector->get_object_label($object) . "\", dump<pre>\n"; var_dump($object); echo "</pre>\n"; if (midcom_helper_reflector_tree::has_children($object)) { echo "Object has children<br/>\n"; echo "Child counts <pre>\n"; $counts = midcom_helper_reflector_tree::count_child_objects($object); print_r($counts); echo "</pre>\n"; echo "Child objects dump<pre>\n"; $children = midcom_helper_reflector_tree::get_child_objects($object); var_dump($children); echo "</pre>\n"; }
/** * Get the MgdSchema root classes * * @return array containing class name and translated name */ public static function get_root_classes() { static $root_classes = array(); // Return cached results if (!empty($root_classes)) { return $root_classes; } // Initialize the returnable array $root_classes = array(); // Get the classes $classes = midcom_helper_reflector_tree::get_root_classes(); // Get the translated name foreach ($classes as $class) { $ref = new midcom_helper_reflector($class); $root_classes[$class] = $ref->get_class_label(); } asort($root_classes); return $root_classes; }
private function _prepare_toolbar(&$data) { if (midcom::get('auth')->can_user_do('midgard:create', null, $this->type)) { $data['asgard_toolbar']->add_item(array(MIDCOM_TOOLBAR_URL => "__mfa/asgard/object/create/{$this->type}/", MIDCOM_TOOLBAR_LABEL => sprintf(midcom::get('i18n')->get_string('create %s', 'midcom'), midgard_admin_asgard_plugin::get_type_label($this->type)), MIDCOM_TOOLBAR_ICON => 'stock-icons/16x16/' . midcom_helper_reflector_tree::get_create_icon($this->type))); } if (midcom::get('auth')->admin) { $qb = new midgard_query_builder($this->type); $qb->include_deleted(); $qb->add_constraint('metadata.deleted', '=', true); $deleted = $qb->count(); if ($deleted > 0) { $data['asgard_toolbar']->add_item(array(MIDCOM_TOOLBAR_URL => "__mfa/asgard/trash/{$this->type}/", MIDCOM_TOOLBAR_LABEL => sprintf(midcom::get('i18n')->get_string('%s deleted items', 'midgard.admin.asgard'), $deleted), MIDCOM_TOOLBAR_ICON => 'stock-icons/16x16/trash-full.png')); } else { $data['asgard_toolbar']->add_item(array(MIDCOM_TOOLBAR_URL => "__mfa/asgard/trash/{$this->type}/", MIDCOM_TOOLBAR_LABEL => midcom::get('i18n')->get_string('trash is empty', 'midgard.admin.asgard'), MIDCOM_TOOLBAR_ICON => 'stock-icons/16x16/trash.png')); } } if ($data['component'] != 'midgard') { $data['asgard_toolbar']->add_item(array(MIDCOM_TOOLBAR_URL => "__mfa/asgard/components/{$data['component']}/", MIDCOM_TOOLBAR_LABEL => midcom::get('i18n')->get_string($data['component'], $data['component']), MIDCOM_TOOLBAR_ICON => 'stock-icons/16x16/component.png')); } $data['asgard_toolbar']->add_item(array(MIDCOM_TOOLBAR_URL => "__ais/help/{$data['documentation_component']}/mgdschemas/#{$this->type}", MIDCOM_TOOLBAR_LABEL => midcom::get('i18n')->get_string('type documentation', 'midgard.admin.asgard'), MIDCOM_TOOLBAR_OPTIONS => array('target' => '_blank'), MIDCOM_TOOLBAR_ICON => 'stock-icons/16x16/stock_help-agent.png')); }
/** * 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 * @return mixed False on failure, newly created MgdSchema root object on success */ public function copy_tree(&$source, &$parent) { // Copy the root object $root = $this->copy_object($source, $parent); if (!$root || !$root->guid) { $this->errors[] = sprintf($this->_l10n->get('failed to copy object %s'), $source->guid); return false; } // Add the newly copied object to the exclusion list to prevent infinite loops $this->exclude[] = $this->source->guid; // Get the children $children = midcom_helper_reflector_tree::get_child_objects($source); if (!$children || count($children) === 0) { return $root; } // Loop through the children and copy them to their corresponding parents foreach ($children as $subchildren) { // Get the children of each type foreach ($subchildren as $child) { // Skip the excluded child if (in_array($child->guid, $this->exclude)) { continue; } $this->copy_tree($child, $root); } } // Return the newly created root object return $root; }
private function _get_product_group_tree($up) { $groups = array(); $qb = org_openpsa_products_product_group_dba::new_query_builder(); $qb->add_constraint('up', '=', $up); $qb->add_order('metadata.score'); $results = $qb->execute(); foreach ($results as $result) { $groups[$result->id] = midcom_helper_reflector_tree::resolve_path($result); $subgroups = $this->_get_product_group_tree($result->id); foreach ($subgroups as $k => $v) { $groups[$k] = $v; } } return $groups; }
/** * Helper method for purging objects * * @param Array $guids * @param string $type * @return boolean Indicating success */ public static function purge($guids, $type) { $purged_size = 0; foreach ($guids as $guid) { $object = midcom_helper_reflector::get_object($guid, $type); if (is_null($object)) { debug_add("Failed to get object {$type} {$guid}", MIDCOM_LOG_ERROR); // Something wrong continue; } // first kill your children $children_types = midcom_helper_reflector_tree::get_child_objects($object, true); if (is_array($children_types)) { foreach ($children_types as $child_type => $children) { $child_guids = array(); foreach ($children as $child) { if (!$child->metadata->deleted) { $child->delete(); } $child_guids[] = $child->guid; } self::purge($child_guids, $child_type); } } // then shoot your dogs $purged_size += self::purge_parameters($guid); $purged_size += self::purge_attachments($guid); // now shoot yourself if (!$object->purge()) { debug_add("Failed to purge object " . get_class($object) . " {$object->guid}", MIDCOM_LOG_INFO); } else { $purged_size += $object->metadata->size; } } return $purged_size; }
/** * Helper method to resolve path for an object */ public static function resolve_path(&$object, $title) { if (!class_exists('midcom_helper_reflector_tree')) { return $title; } return midcom_helper_reflector_tree::resolve_path($object); }
/** * Static method to add default ("title" and "name") sorts to a QB instance * * @param midgard_query_builder $qb reference to QB instance * @param string $schema_type valid mgdschema class name */ public static function add_schema_sorts_to_qb(&$qb, $schema_type) { // Sort by "title" and "name" if available $ref = midcom_helper_reflector_tree::get($schema_type); $dummy = new $schema_type(); $title_property = $ref->get_title_property($dummy); if (is_string($title_property) && midcom::get('dbfactory')->property_exists($schema_type, $title_property)) { $qb->add_order($title_property); } $name_property = $ref->get_name_property($dummy); if (is_string($name_property) && midcom::get('dbfactory')->property_exists($schema_type, $name_property)) { $qb->add_order($name_property); } unset($title_property, $name_property, $ref, $dummy); }
private static function _get_child_objects($object) { $children = midcom_helper_reflector_tree::get_child_objects($object); if ($children === false) { debug_add('Failed to query the children of object {$object->guid}: ' . midcom_connection::get_error_string(), MIDCOM_LOG_ERROR); } return $children; }
/** * List the child elements * * @param mixed $object MgdSchema object * @param string $prefix Indent for the output * @param int $level Level of depth */ public function _list_child_elements($object, $prefix = ' ', $level = 0) { if ($level > 25) { debug_add('Recursion level 25 exceeded, aborting', MIDCOM_LOG_ERROR); return; } $siblings = midcom_helper_reflector_tree::get_child_objects($object); if (is_array($siblings) && count($siblings) > 0) { echo "{$prefix}<ul>\n"; foreach ($siblings as $type => $children) { foreach ($children as $child) { if (isset($this->shown_objects[$child->guid])) { continue; } $ref =& $this->_get_reflector($child); $span_class = ''; $css_class = $type; $this->_common_css_classes($child, $ref, $css_class); $this->shown_objects[$child->guid] = true; echo "{$prefix} <li class=\"{$css_class}\">\n"; $label = htmlspecialchars($ref->get_object_label($child)); $icon = $ref->get_object_icon($child); if (empty($label)) { $label = "#{$child->id}"; } if ($this->copy_tree) { $checked = ' checked="checked"'; } else { $checked = ''; } if ($this->inputs) { // This value is used for compiling the exclusion list: if the object is found from this list, but not from the selection list, // it means that the selection did not include the object GUID echo "{$prefix} <input type=\"hidden\" name=\"all_objects[]\" value=\"{$child->guid}\" />\n"; echo "{$prefix} <label for=\"item_{$child->guid}\">\n"; echo "{$prefix} <input id=\"item_{$child->guid}\" type=\"{$this->input_type}\" name=\"{$this->input_name}\" value=\"{$child->guid}\"{$checked} />\n"; } echo "{$prefix} <span class=\"title{$span_class}\">{$icon}{$label}</span>\n"; // Show the link to the object if ($this->view_link) { echo "{$prefix} <a href=\"{$this->page_prefix}__mfa/asgard/object/view/{$child->guid}/\" class=\"thickbox\" target=\"_blank\" title=\"" . sprintf(midcom::get('i18n')->get_string('%s (%s)', 'midgard.admin.asgard'), $label, $ref->get_class_label()) . "\">\n"; echo "{$prefix} <img src=\"" . MIDCOM_STATIC_URL . "/stock-icons/16x16/view.png\" alt=\"" . midcom::get('i18n')->get_string('view object', 'midgard.admin.asgard') . "\" />\n"; echo "{$prefix} </a>\n"; } // Show the link to the object if ($this->edit_link) { echo "{$prefix} <a href=\"{$this->page_prefix}__mfa/asgard/object/edit/{$child->guid}/\" class='target_blank' title=\"" . sprintf(midcom::get('i18n')->get_string('%s (%s)', 'midgard.admin.asgard'), $label, $ref->get_class_label()) . "\">\n"; echo "{$prefix} <img src=\"" . MIDCOM_STATIC_URL . "/stock-icons/16x16/edit.png\" alt=\"" . midcom::get('i18n')->get_string('edit object', 'midgard.admin.asgard') . "\" />\n"; echo "{$prefix} </a>\n"; } if ($this->inputs) { echo "{$prefix} </label>\n"; } // List the child elements $this->_list_child_elements($child, "{$prefix} ", $level + 1); echo "{$prefix} </li>\n"; } } echo "{$prefix}</ul>\n"; } }
/** * Renders the given root objects to HTML and calls _list_child_elements() * * @param array &$root_objects reference to the array of root objects * @param midcom_helper_reflector_tree &$ref Reflector singleton */ private function _list_root_elements(&$root_objects, midcom_helper_reflector_tree &$ref) { echo "<ul class=\"midgard_admin_asgard_navigation\">\n"; $label_mapping = array(); foreach ($root_objects as $i => $object) { $label_mapping[$i] = htmlspecialchars($ref->get_object_label($object)); } asort($label_mapping); foreach ($label_mapping as $index => $label) { $object = $root_objects[$index]; $selected = $this->_is_selected($object); $css_class = get_class($object); $this->_common_css_classes($object, $ref, $css_class); $mode = $this->_request_data['default_mode']; if (strpos($css_class, 'readonly')) { $mode = 'view'; } $this->shown_objects[$object->guid] = true; echo " <li class=\"{$css_class}\">"; $icon = $ref->get_object_icon($object); if (empty($label)) { $label = "#oid_{$object->id}"; } echo "<a href=\"" . midcom_connection::get_url('self') . "__mfa/asgard/object/{$mode}/{$object->guid}/\" title=\"GUID: {$object->guid}, ID: {$object->id}\">{$icon}{$label}</a>\n"; // If there is exactly one root object, show its children, since this is what the user most likely wants to reach if ($selected || sizeof($root_objects) == 1) { $this->_list_child_elements($object); } echo " </li>\n"; } echo "</ul>\n"; }
/** * Populate the object toolbar * * @param mixed $object MgdSchema object for which the toolbar will be created * @param String $handler_id Initialized handler id * @param array $data Local request data */ public function bind_to_object($object, $handler_id, $data) { // Show view toolbar button, if the user hasn't configured to use straight the edit mode if ($data['default_mode'] === 'view') { $this->add_item(array(MIDCOM_TOOLBAR_URL => $this->_generate_url('view', $object), MIDCOM_TOOLBAR_LABEL => midcom::get('i18n')->get_string('view', 'midcom'), MIDCOM_TOOLBAR_ICON => 'stock-icons/16x16/view.png', MIDCOM_TOOLBAR_ACCESSKEY => 'v')); } if (!is_a($object, 'midcom_db_style') && !is_a($object, 'midcom_db_element') && !is_a($object, 'midcom_db_snippetdir') && !is_a($object, 'midcom_db_snippet') && !is_a($object, 'midcom_db_page') && !is_a($object, 'midcom_db_pageelement') && !is_a($object, 'midcom_db_parameter') && substr($object->__mgdschema_class_name__, 0, 23) != 'org_routamc_positioning' && substr($object->__mgdschema_class_name__, 0, 14) != 'net_nemein_tag') { $link = midcom::get('permalinks')->resolve_permalink($object->guid); if ($link) { $this->add_item(array(MIDCOM_TOOLBAR_URL => $link, MIDCOM_TOOLBAR_LABEL => midcom::get('i18n')->get_string('view on site', 'midgard.admin.asgard'), MIDCOM_TOOLBAR_ICON => 'stock-icons/16x16/stock_internet.png')); } } if ($object->can_do('midgard:update')) { $this->add_item(array(MIDCOM_TOOLBAR_URL => $this->_generate_url('edit', $object), MIDCOM_TOOLBAR_LABEL => midcom::get('i18n')->get_string('edit', 'midcom'), MIDCOM_TOOLBAR_ICON => 'stock-icons/16x16/edit.png', MIDCOM_TOOLBAR_ACCESSKEY => 'e')); } if ($object->can_do('midgard:create')) { if (midcom_helper_reflector_tree::get_child_objects($object)) { $this->add_item(array(MIDCOM_TOOLBAR_URL => $this->_generate_url('copy/tree', $object), MIDCOM_TOOLBAR_LABEL => midcom::get('i18n')->get_string('copy', 'midcom'), MIDCOM_TOOLBAR_ICON => 'stock-icons/16x16/editcopy.png')); } else { $this->add_item(array(MIDCOM_TOOLBAR_URL => $this->_generate_url('copy', $object), MIDCOM_TOOLBAR_LABEL => midcom::get('i18n')->get_string('copy', 'midcom'), MIDCOM_TOOLBAR_ICON => 'stock-icons/16x16/editcopy.png')); } } if ($object->can_do('midgard:update')) { $this->_add_toolbar_update_items($object); } if ($object->can_do('midgard:create')) { // Find out what types of children the object can have and show create buttons for them $child_types = $data['tree_reflector']->get_child_classes(); if (!is_array($child_types)) { debug_add("\$data['tree_reflector']->get_child_classes() failed critically, recasting \$child_types as array", MIDCOM_LOG_WARN); $child_types = array(); } foreach ($child_types as $type) { $display_button = true; if (is_a($object, 'midcom_db_topic')) { // With topics we should check for component before populating create buttons as so many types can be children of topics switch ($type) { case 'midgard_topic': case 'midgard_article': // Articles and topics can always be created break; default: $midcom_dba_classname = midcom::get('dbclassloader')->get_midcom_class_name_for_mgdschema_object($type); if (!$midcom_dba_classname) { $display_button = false; break; } $component = midcom::get('dbclassloader')->get_component_for_class($type); if ($component != $object->component) { $display_button = false; } break; } } elseif (is_a($object, 'midcom_db_article') && $object->topic) { $topic = new midcom_db_topic($object->topic); // With articles we should check for topic component before populating create buttons as so many types can be children of topics switch ($type) { case 'midgard_article': // Articles can always be created break; default: $component = midcom::get('dbclassloader')->get_component_for_class($type); if ($component != $topic->component) { $display_button = false; } break; } } if (!$display_button) { // Skip this type continue; } $this->add_item(array(MIDCOM_TOOLBAR_URL => $this->_generate_url('create/' . $type, $object), MIDCOM_TOOLBAR_LABEL => sprintf(midcom::get('i18n')->get_string('create %s', 'midcom'), midgard_admin_asgard_plugin::get_type_label($type)), MIDCOM_TOOLBAR_ICON => 'stock-icons/16x16/' . $data['tree_reflector']->get_create_icon($type))); } } if ($object->can_do('midgard:delete')) { $this->add_item(array(MIDCOM_TOOLBAR_URL => $this->_generate_url('delete', $object), MIDCOM_TOOLBAR_LABEL => midcom::get('i18n')->get_string('delete', 'midcom'), MIDCOM_TOOLBAR_ICON => 'stock-icons/16x16/trash.png', MIDCOM_TOOLBAR_ACCESSKEY => 'd')); } if ($GLOBALS['midcom_config']['midcom_services_rcs_enable'] && $object->can_do('midgard:update') && $object->_use_rcs) { $this->add_item(array(MIDCOM_TOOLBAR_URL => $this->_generate_url('rcs', $object), MIDCOM_TOOLBAR_LABEL => midcom::get('i18n')->get_string('show history', 'midgard.admin.asgard'), MIDCOM_TOOLBAR_ICON => 'stock-icons/16x16/history.png', MIDCOM_TOOLBAR_ENABLED => substr($handler_id, 0, 25) === '____mfa-asgard-object_rcs' ? false : true, MIDCOM_TOOLBAR_ACCESSKEY => 'h')); } $this->_disable_active_item($handler_id, $object, $data); }
/** * Helper to resolve the base value for the incrementing suffix and for the name. * * @see midcom_helper_reflector_nameresolver::generate_unique_name() * @param string $current_name the "current name" of the object (might not be the actual name value see the title logic in generate_unique_name()) * @param string $extension The file extension, when working with attachments * @return array first key is the resolved $i second is the $base_name, which is $current_name without numeric suffix */ private function _generate_unique_name_resolve_i($current_name, $extension) { if (preg_match('/(.*?)-([0-9]{3,})' . $extension . '$/', $current_name, $name_matches)) { // Name already has i and base parts, split them. $i = (int) $name_matches[2]; $base_name = (string) $name_matches[1]; unset($name_matches); } else { // Defaults $i = 1; $base_name = $current_name; } // Look for siblings with similar names and see if they have higher i. midcom::get('auth')->request_sudo('midcom.helper.reflector'); $parent = midcom_helper_reflector_tree::get_parent($this->_object); // TODO: Refactor to reduce duplicate code with _name_is_unique_check_siblings if ($parent && !empty($parent->guid)) { // We have parent, check siblings $parent_resolver = new midcom_helper_reflector_tree($parent); $sibling_classes = $parent_resolver->get_child_classes(); if (!in_array('midgard_attachment', $sibling_classes)) { $sibling_classes[] = 'midgard_attachment'; } foreach ($sibling_classes as $schema_type) { $dummy = new $schema_type(); $child_name_property = midcom_helper_reflector::get_name_property($dummy); unset($dummy); if (empty($child_name_property)) { // This sibling class does not use names continue; } $resolver = midcom_helper_reflector_tree::get($schema_type); $qb =& $resolver->_child_objects_type_qb($schema_type, $parent, false); if (!is_object($qb)) { continue; } $qb->add_constraint($child_name_property, 'LIKE', "{$base_name}-%" . $extension); // Do not include current object in results, this is the easiest way if (!empty($this->_object->guid)) { $qb->add_constraint('guid', '<>', $this->_object->guid); } $qb->add_order('name', 'DESC'); // One result should be enough $qb->set_limit(1); $siblings = $qb->execute(); if (empty($siblings)) { // we don't care about fatal qb errors here continue; } $sibling = $siblings[0]; $sibling_name = $sibling->{$child_name_property}; if (preg_match('/(.*?)-([0-9]{3,})' . $extension . '$/', $sibling_name, $name_matches)) { // Name already has i and base parts, split them. $sibling_i = (int) $name_matches[2]; if ($sibling_i >= $i) { $i = $sibling_i + 1; } unset($sibling_i, $name_matches); } } unset($parent, $parent_resolver, $sibling_classes, $schema_type, $child_name_property, $sibling, $sibling_name); } else { unset($parent); // No parent, we might be a root level class $is_root_class = false; $root_classes = midcom_helper_reflector_tree::get_root_classes(); foreach ($root_classes as $schema_type) { if (midcom::get('dbfactory')->is_a($this->_object, $schema_type)) { $is_root_class = true; } } if (!$is_root_class) { // This should not happen, logging error and returning true (even though it's potentially dangerous) midcom::get('auth')->drop_sudo(); debug_add("Object #{$this->_object->guid} has no valid parent but is not listed in the root classes, don't know what to do, letting higher level decide", MIDCOM_LOG_ERROR); unset($root_classes, $is_root_class); return array($i, $base_name); } else { // TODO: Refactor to reduce duplicate code with _name_is_unique_check_roots foreach ($root_classes as $schema_type) { $dummy = new $schema_type(); $child_name_property = midcom_helper_reflector::get_name_property($dummy); unset($dummy); if (empty($child_name_property)) { // This sibling class does not use names continue; } $resolver =& midcom_helper_reflector_tree::get($schema_type); $deleted = false; $qb =& $resolver->_root_objects_qb($deleted); if (!$qb) { continue; } unset($deleted); $qb->add_constraint($child_name_property, 'LIKE', "{$base_name}-%" . $extension); // Do not include current object in results, this is the easiest way if (!empty($this->_object->guid)) { $qb->add_constraint('guid', '<>', $this->_object->guid); } $qb->add_order($child_name_property, 'DESC'); // One result should be enough $qb->set_limit(1); $siblings = $qb->execute(); if (empty($siblings)) { // we dont' care about fatal qb errors here continue; } $sibling = $siblings[0]; $sibling_name = $sibling->{$child_name_property}; if (preg_match('/(.*?)-([0-9]{3,})' . $extension . '$/', $sibling_name, $name_matches)) { // Name already has i and base parts, split them. $sibling_i = (int) $name_matches[2]; if ($sibling_i >= $i) { $i = $sibling_i + 1; } unset($sibling_i, $name_matches); } } unset($root_classes, $schema_type, $child_name_property, $sibling, $sibling_name); } } midcom::get('auth')->drop_sudo(); return array($i, $base_name); }