private function _search($term) { $dummy_objects = array(); $type_class = $this->type; $dummy_type_object = new $type_class(); $dummy_objects[] =& $dummy_type_object; $resolver = new midcom_helper_reflector_tree($dummy_type_object); $child_classes = $resolver->get_child_classes(); foreach ($child_classes as $child_class) { if ($child_class != $type_class) { $dummy_objects[] = new $child_class(); } } $search_results = array(); foreach ($dummy_objects as $dummy_object) { $results = $this->_search_type_qb($dummy_object, $term); $search_results = array_merge($search_results, $results); } return $search_results; }
/** * Get children of given object * * @param midgard_object &$object object to get children for * @param boolean $deleted whether to get (only) deleted or not-deleted objects * @return array multidimensional array (keyed by classname) of objects or false on failure */ public static function get_child_objects(&$object, $deleted = false) { // PONDER: Check for some generic user privilege instead ?? if ($deleted && !midcom_connection::is_admin()) { debug_add('Non-admins are not allowed to list deleted objects', MIDCOM_LOG_ERROR); return false; } $resolver = new midcom_helper_reflector_tree($object); $child_classes = $resolver->get_child_classes(); if (!$child_classes) { if ($child_classes === false) { debug_add('resolver returned false (critical failure) from get_child_classes()', MIDCOM_LOG_ERROR); } return false; } //make sure children of the same type come out on top $i = 0; foreach ($child_classes as $child_class) { if (midcom::get('dbfactory')->is_a($object, $child_class)) { unset($child_classes[$i]); array_unshift($child_classes, $child_class); break; } $i++; } $child_objects = array(); foreach ($child_classes as $schema_type) { $type_children = $resolver->_get_child_objects_type($schema_type, $object, $deleted); // PONDER: check for boolean false as result ?? if (empty($type_children)) { unset($type_children); continue; } $child_objects[$schema_type] = $type_children; unset($type_children); } return $child_objects; }
/** * 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); }