コード例 #1
0
ファイル: schemadb.php プロジェクト: nemein/openpsa
 /**
  * Generates, loads and prepares the schema database.
  *
  * The operations are done on all available schemas within the DB.
  */
 public function create($type, $include_fields)
 {
     if ($type != null) {
         $dba_type = $type;
         if (!midcom::get('dbclassloader')->is_midcom_db_object($type)) {
             $dba_type = midcom::get('dbclassloader')->get_midcom_class_name_for_mgdschema_object($type);
         }
         $dummy_object = new $dba_type();
         $type_fields = $dummy_object->get_properties();
     } else {
         $type = get_class($this->_object);
         if (!midcom::get('dbclassloader')->is_midcom_db_object($type)) {
             $this->_object = midcom::get('dbfactory')->convert_midgard_to_midcom($this->_object);
         }
         $type_fields = $this->_object->get_properties();
     }
     if (empty($include_fields)) {
         $include_fields = null;
     } else {
         if (is_string($include_fields)) {
             $include_fields = array($include_fields);
         }
     }
     //This is an ugly little workaround for unittesting
     $template = midcom_helper_datamanager2_schema::load_database('file:/midgard/admin/asgard/config/schemadb_default.inc');
     $empty_db = clone $template['object'];
     $this->_schemadb = array('object' => $empty_db);
     //workaround end
     $this->_reflector = new midgard_reflection_property(midcom_helper_reflector::resolve_baseclass($type));
     // Iterate through object properties
     unset($type_fields['metadata']);
     if (!extension_loaded('midgard2')) {
         // Midgard1 returns properties is random order so we need to sort them heuristically
         usort($type_fields, array($this, 'sort_schema_fields'));
     }
     foreach ($type_fields as $key) {
         if (in_array($key, $this->_config->get('object_skip_fields'))) {
             continue;
         }
         // Skip the fields that aren't requested, if inclusion list has been defined
         if ($include_fields && !in_array($key, $include_fields)) {
             continue;
         }
         // Only hosts have lang field that we will actually display
         if ($key == 'lang' && !is_a($this->_object, 'midcom_db_host')) {
             continue;
         }
         // Skip topic symlink field because it is a special field not meant to be touched directly
         if ($key == 'symlink' && is_a($this->_object, 'midcom_db_topic')) {
             continue;
         }
         // Linked fields should use chooser
         if ($this->_reflector->is_link($key)) {
             $this->_add_linked_field($key);
             // Skip rest of processing
             continue;
         }
         $field_type = $this->_reflector->get_midgard_type($key);
         switch ($field_type) {
             case MGD_TYPE_GUID:
             case MGD_TYPE_STRING:
                 if ($key == 'component' && is_a($this->_object, 'midcom_db_topic')) {
                     $this->_add_component_dropdown($key);
                     break;
                 }
                 // Special name handling, start by checking if given type is same as $this->_object and if not making a dummy copy (we're probably in creation mode then)
                 if (midcom::get('dbfactory')->is_a($this->_object, $type)) {
                     $name_obj = $this->_object;
                 } else {
                     $name_obj = new $type();
                 }
                 if ($key === midcom_helper_reflector::get_name_property($name_obj)) {
                     $this->_add_name_field($key, $name_obj);
                     break;
                 }
                 unset($name_obj);
                 // Special page treatment
                 if ($key === 'info' && $type === 'midcom_db_page') {
                     $this->_add_info_field_for_page($key);
                     break;
                 }
                 if ($key === 'info' && $type === 'midcom_db_pageelement') {
                     $this->_schemadb['object']->append_field($key, array('title' => $key, 'storage' => $key, 'type' => 'select', 'type_config' => array('options' => array('' => 'not inherited', 'inherit' => 'inherited')), 'widget' => 'select'));
                     break;
                 }
                 $this->_schemadb['object']->append_field($key, array('title' => $key, 'storage' => $key, 'type' => 'text', 'widget' => 'text'));
                 break;
             case MGD_TYPE_LONGTEXT:
                 $this->_add_longtext_field($key, $type);
                 break;
             case MGD_TYPE_INT:
             case MGD_TYPE_UINT:
                 $this->_add_int_field($key);
                 break;
             case MGD_TYPE_FLOAT:
                 $this->_schemadb['object']->append_field($key, array('title' => $key, 'storage' => $key, 'type' => 'number', 'widget' => 'text'));
                 break;
             case MGD_TYPE_BOOLEAN:
                 $this->_schemadb['object']->append_field($key, array('title' => $key, 'storage' => $key, 'type' => 'boolean', 'widget' => 'checkbox'));
                 break;
             case MGD_TYPE_TIMESTAMP:
                 $this->_schemadb['object']->append_field($key, array('title' => $key, 'storage' => $key, 'type' => 'date', 'widget' => 'jsdate'));
                 break;
         }
     }
     $this->_add_rcs_field();
     if ($this->add_copy_fields) {
         $this->_add_copy_fields();
     }
     return $this->_schemadb;
 }