示例#1
0
 public function prepareObject(TypeInterface $controller, $parent = null)
 {
     $config = $controller->getConfig();
     $class = $config['storage'];
     $object = new $class();
     if (null !== $parent) {
         $baseclass = \midcom_helper_reflector::resolve_baseclass($class);
         $reflector = new \midgard_reflection_property($baseclass);
         $up_property = \midgard_object_class::get_property_up($baseclass);
         if (!empty($up_property)) {
             $target_property = $reflector->get_link_target($up_property);
             $target_class = $reflector->get_link_name($up_property);
             if (\midcom_helper_reflector::resolve_baseclass($parent) === $target_class) {
                 $object->{$up_property} = $parent->{$target_property};
             }
         }
         $parent_property = \midgard_object_class::get_property_parent($baseclass);
         if (!empty($parent_property)) {
             $target_property = $reflector->get_link_target($parent_property);
             $target_class = $reflector->get_link_name($parent_property);
             if (\midcom_helper_reflector::resolve_baseclass($parent) === $target_class) {
                 $object->{$parent_property} = $parent->{$target_property};
             } else {
                 $object->{$parent_property} = $parent->{$parent_property};
             }
         }
     }
     return $object;
 }
示例#2
0
 public static function get_property_documentation($class)
 {
     $property_docs = array();
     $props = array();
     $mgdschemas = midgardmvc_core::get_instance()->dispatcher->get_mgdschema_classes();
     if (in_array($class, $mgdschemas)) {
         $dummy = new $class();
         $props = get_object_vars($dummy);
     } elseif ($class == 'midgard_metadata') {
         $dummy = new midgard_person();
         $props = get_object_vars($dummy->metadata);
     }
     if (empty($props)) {
         // Non-MgdSchema class, reflect using PHP method
         $reflector = new midgard_reflection_class($class);
         $props = $reflector->getProperties();
         foreach ($props as $property) {
             if (!$property->isPublic()) {
                 // No sense to document private properties
                 continue;
             }
             $property_doc = array('name' => $property->name, 'type' => null, 'type_url' => null, 'link_url' => null, 'signature' => $property->name, 'documentation' => midgardmvc_core_helpers_documentation::render_docblock($property->getDocComment()));
             $property_docs[] = $property_doc;
         }
         return $property_docs;
     }
     $reflectionproperty = new midgard_reflection_property($class);
     foreach ($props as $property => $value) {
         if ($property == 'action') {
             continue;
         }
         $type = midgardmvc_core_helpers_documentation::get_midgard_type_signature($reflectionproperty->get_midgard_type($property));
         if (!$type && $property == 'metadata') {
             $type = 'midgard_metadata';
         }
         $property_doc = array('name' => $property, 'type' => $type, 'type_url' => null, 'link_url' => null, 'signature' => "{$type} {$property}", 'documentation' => $reflectionproperty->description($property));
         try {
             // Link to the class documentation is the property is of particular type
             if (strpos($type, '_') !== false && class_exists($type)) {
                 $property_doc['type_url'] = midgardmvc_core::get_instance()->dispatcher->generate_url('midcom_documentation_class', array('class' => $type));
             }
         } catch (Exception $e) {
         }
         if ($reflectionproperty->is_link($property)) {
             $property_doc['link_url'] = midgardmvc_core::get_instance()->dispatcher->generate_url('midcom_documentation_class', array('class' => $reflectionproperty->get_link_name($property)));
             $property_doc['link'] = $reflectionproperty->get_link_name($property) . '::' . $reflectionproperty->get_link_target($property);
         }
         $property_docs[] = $property_doc;
     }
     return $property_docs;
 }
示例#3
0
 private function _add_linked_field($key)
 {
     $linked_type = $this->_reflector->get_link_name($key);
     $linked_type_reflector = midcom_helper_reflector::get($linked_type);
     $field_type = $this->_reflector->get_midgard_type($key);
     if ($key == 'up') {
         $field_label = sprintf($this->_l10n->get('under %s'), midgard_admin_asgard_plugin::get_type_label($linked_type));
     } else {
         $type_label = midgard_admin_asgard_plugin::get_type_label($linked_type);
         if (substr($type_label, 0, strlen($key)) == $key) {
             // Handle abbreviations like "lang" for "language"
             $field_label = $type_label;
         } else {
             if ($key == $type_label) {
                 $field_label = $key;
             } else {
                 $ref = midcom_helper_reflector::get($this->_object);
                 $component_l10n = $ref->get_component_l10n();
                 $field_label = sprintf($this->_l10n->get('%s (%s)'), $component_l10n->get($key), $type_label);
             }
         }
     }
     // Get the chooser widgets
     switch ($field_type) {
         case MGD_TYPE_UINT:
         case MGD_TYPE_STRING:
         case MGD_TYPE_GUID:
             $class = midcom::get('dbclassloader')->get_midcom_class_name_for_mgdschema_object($linked_type);
             if (!$class) {
                 break;
             }
             $component = midcom::get('dbclassloader')->get_component_for_class($linked_type);
             $this->_schemadb['object']->append_field($key, array('title' => $field_label, 'storage' => $key, 'type' => 'select', 'type_config' => array('require_corresponding_option' => false, 'options' => array(), 'allow_other' => true, 'allow_multiple' => false), 'widget' => 'chooser', 'widget_config' => array('class' => $class, 'component' => $component, 'titlefield' => $linked_type_reflector->get_label_property(), 'id_field' => $this->_reflector->get_link_target($key), 'searchfields' => $linked_type_reflector->get_search_properties(), 'result_headers' => $this->_get_result_headers($linked_type_reflector), 'orders' => array(), 'creation_mode_enabled' => true, 'creation_handler' => midcom_connection::get_url('self') . "__mfa/asgard/object/create/chooser/{$linked_type}/", 'creation_default_key' => $linked_type_reflector->get_label_property(), 'generate_path_for' => midcom_helper_reflector::get_name_property($this->_object))));
             break;
     }
 }
示例#4
0
文件: tree.php 项目: nemein/openpsa
 /**
  * Creates a QB instance for _get_child_objects_type and _count_child_objects_type
  */
 public function &_child_objects_type_qb(&$schema_type, &$for_object, $deleted)
 {
     if (empty($schema_type)) {
         debug_add('Passed schema_type argument is empty, this is fatal', MIDCOM_LOG_ERROR);
         $x = false;
         return $x;
     }
     if (!is_object($for_object)) {
         debug_add('Passed for_object argument is not object, this is fatal', MIDCOM_LOG_ERROR);
         $x = false;
         return $x;
     }
     if ($deleted) {
         $qb = new midgard_query_builder($schema_type);
     } else {
         $qb = false;
         // Figure correct MidCOM DBA class to use and get midcom QB
         $midcom_dba_classname = midcom::get('dbclassloader')->get_midcom_class_name_for_mgdschema_object($schema_type);
         if (empty($midcom_dba_classname)) {
             debug_add("MidCOM DBA does not know how to handle {$schema_type}", MIDCOM_LOG_ERROR);
             return $qb;
         }
         if (!midcom::get('dbclassloader')->load_component_for_class($midcom_dba_classname)) {
             debug_add("Failed to load the handling component for {$midcom_dba_classname}, cannot continue.", MIDCOM_LOG_ERROR);
             return $qb;
         }
         $qb_callback = array($midcom_dba_classname, 'new_query_builder');
         if (!is_callable($qb_callback)) {
             debug_add("Static method {$midcom_dba_classname}::new_query_builder() is not callable", MIDCOM_LOG_ERROR);
             return $qb;
         }
         $qb = call_user_func($qb_callback);
     }
     // Sanity-check
     if (!$qb) {
         debug_add("Could not get QB for type '{$schema_type}'", MIDCOM_LOG_ERROR);
         $x = false;
         return $x;
     }
     // Deleted constraints
     if ($deleted) {
         $qb->include_deleted();
         $qb->add_constraint('metadata.deleted', '<>', 0);
     }
     // Figure out constraint(s) to use to get child objects
     $ref = new midgard_reflection_property($schema_type);
     $multiple_links = false;
     $linkfields = array();
     $linkfields['up'] = midgard_object_class::get_property_up($schema_type);
     $linkfields['parent'] = midgard_object_class::get_property_parent($schema_type);
     $object_baseclass = midcom_helper_reflector::resolve_baseclass(get_class($for_object));
     foreach ($linkfields as $link_type => $field) {
         if (empty($field)) {
             // No such field for the object
             unset($linkfields[$link_type]);
             continue;
         }
         $linked_class = $ref->get_link_name($field);
         if (empty($linked_class) && $ref->get_midgard_type($field) === MGD_TYPE_GUID) {
             // Guid link without class specification, valid for all classes
             continue;
         }
         if ($linked_class != $object_baseclass) {
             // This link points elsewhere
             unset($linkfields[$link_type]);
             continue;
         }
     }
     if (count($linkfields) === 0) {
         debug_add("Class '{$schema_type}' has no valid link properties pointing to class '" . get_class($for_object) . "', this should not happen here", MIDCOM_LOG_ERROR);
         $x = false;
         return $x;
     }
     if (count($linkfields) > 1) {
         $multiple_links = true;
         $qb->begin_group('OR');
     }
     foreach ($linkfields as $link_type => $field) {
         $field_type = $ref->get_midgard_type($field);
         $field_target = $ref->get_link_target($field);
         if (empty($field_target) && $field_type === MGD_TYPE_GUID) {
             $field_target = 'guid';
         }
         if (!$field_target || !isset($for_object->{$field_target})) {
             if ($multiple_links) {
                 $qb->end_group();
             }
             // Why return false ???
             $x = false;
             return $x;
         }
         switch ($field_type) {
             case MGD_TYPE_STRING:
             case MGD_TYPE_GUID:
                 $qb->add_constraint($field, '=', (string) $for_object->{$field_target});
                 break;
             case MGD_TYPE_INT:
             case MGD_TYPE_UINT:
                 if ($link_type == 'up') {
                     $qb->add_constraint($field, '=', (int) $for_object->{$field_target});
                 } else {
                     $qb->begin_group('AND');
                     $qb->add_constraint($field, '=', (int) $for_object->{$field_target});
                     // make sure we don't accidentally find other objects with the same id
                     $qb->add_constraint($field . '.guid', '=', (string) $for_object->guid);
                     $qb->end_group();
                 }
                 break;
             default:
                 debug_add("Do not know how to handle linked field '{$field}', has type {$field_type}", MIDCOM_LOG_INFO);
                 if ($multiple_links) {
                     $qb->end_group();
                 }
                 // Why return false ???
                 $x = false;
                 return $x;
         }
     }
     if ($multiple_links) {
         $qb->end_group();
     }
     return $qb;
 }
示例#5
0
 /**
  * Get the GUID of the object's parent. This is done by reading up or parent
  * property values, which will give us the parent's ID. Since the ID => GUID relation
  * won't change, the corresponding GUID is then stored in an in-request static cache
  */
 public static function get_parent_guid_uncached_static($object_guid, $class_name)
 {
     static $parent_mapping = array();
     $class_name = midcom::get('dbclassloader')->get_mgdschema_class_name_for_midcom_class($class_name);
     $reflector = new midgard_reflection_property($class_name);
     $up_property = midgard_object_class::get_property_up($class_name);
     if (!empty($up_property)) {
         $target_property = $reflector->get_link_target($up_property);
         // Up takes precedence over parent
         $mc = new midgard_collector($class_name, 'guid', $object_guid);
         $mc->set_key_property($up_property);
         $mc->execute();
         $link_values = $mc->list_keys();
         if (!empty($link_values)) {
             list($link_value, $dummy) = each($link_values);
             unset($mc, $link_values, $dummy);
             if (!empty($link_value)) {
                 if (!array_key_exists($class_name, $parent_mapping)) {
                     $parent_mapping[$class_name] = array();
                 }
                 if (array_key_exists($link_value, $parent_mapping[$class_name])) {
                     return $parent_mapping[$class_name][$link_value];
                 }
                 $mc2 = new midgard_collector($class_name, $target_property, $link_value);
                 $mc2->set_key_property('guid');
                 $mc2->execute();
                 $guids = $mc2->list_keys();
                 if (!is_array($guids)) {
                     unset($mc2, $guids, $link_value);
                     $parent_mapping[$class_name][$link_value] = null;
                     return $parent_mapping[$class_name][$link_value];
                 }
                 list($parent_guid, $dummy) = each($guids);
                 $parent_mapping[$class_name][$link_value] = $parent_guid;
                 unset($mc2, $guids, $link_value, $dummy);
                 return $parent_guid;
             }
         } else {
             unset($mc, $link_values);
         }
     }
     $parent_property = midgard_object_class::get_property_parent($class_name);
     if (!empty($parent_property) && $reflector->get_link_target($parent_property)) {
         $target_property = $reflector->get_link_target($parent_property);
         $target_class = $reflector->get_link_name($parent_property);
         $mc = new midgard_collector($class_name, 'guid', $object_guid);
         $mc->set_key_property($parent_property);
         $mc->execute();
         $link_values = $mc->list_keys();
         if (!empty($link_values)) {
             list($link_value, $dummy) = each($link_values);
             unset($mc, $link_values, $dummy);
             if (!empty($link_value)) {
                 if (!array_key_exists($target_class, $parent_mapping)) {
                     $parent_mapping[$target_class] = array();
                 }
                 if (array_key_exists($link_value, $parent_mapping[$target_class])) {
                     return $parent_mapping[$target_class][$link_value];
                 }
                 $mc2 = new midgard_collector($target_class, $target_property, $link_value);
                 $mc2->set_key_property('guid');
                 $mc2->execute();
                 $guids = $mc2->list_keys();
                 if (!is_array($guids)) {
                     unset($mc2, $guids, $link_value);
                     $parent_mapping[$target_class][$link_value] = null;
                     return $parent_mapping[$target_class][$link_value];
                 }
                 list($parent_guid, $dummy) = each($guids);
                 $parent_mapping[$target_class][$link_value] = $parent_guid;
                 unset($mc2, $guids, $link_value, $dummy);
                 return $parent_guid;
             }
         } else {
             unset($mc, $link_values);
         }
     }
     // FIXME: Handle GUID linking
     return null;
 }