Esempio n. 1
0
 /**
  * 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);
 }