From b724d08d26a9b855304242bf0a23516da44765fe Mon Sep 17 00:00:00 2001 From: Earl Miles <merlin@logrus.com> Date: Wed, 4 Feb 2009 00:26:44 +0000 Subject: [PATCH] Update export to make it more useful for Panels which does not always export in a straightforward manner. --- ctools.module | 97 -------------------------------- delegator/delegator.install | 6 ++ delegator/delegator.module | 2 +- delegator/plugins/tasks/page.inc | 2 +- includes/export.inc | 43 ++++++++++---- 5 files changed, 40 insertions(+), 110 deletions(-) diff --git a/ctools.module b/ctools.module index a7f7510f..f50e288e 100644 --- a/ctools.module +++ b/ctools.module @@ -158,100 +158,3 @@ function ctools_cron() { ctools_object_cache_clean(); } } - -/** - * Initialize an object from schema. - * - * The schema for the table will be queried and flags on the fields will - * be used to initialize the data. - * - * - $schema['class'] will be used to determine the class of the new object. - * stdClass will be used if this is not set. - * - $schema['fields'][$field]['serialize'] will be queried to see if the - * data is serialized. - * - $schema['fields'][$field']'object default'] will be queried to see if - * the object gets a default that the database does not. - * - * @param $table - * The name of the table which must be available via drupal_get_schema(). - */ -function ctools_object_init($table) { - $schema = drupal_get_schema($table); - - if (!$schema) { - return; - } - - $class = isset($schema['class']) && class_exists($schema['class']) ? $schema['class'] : 'stdClass'; - $object = new $class; - - - // Go through our schema and build correlations. - foreach ($schema['fields'] as $field => $info) { - if ($info['type'] == 'serial') { - $object->$field = NULL; - } - if (!isset($object->$field)) { - if (!empty($info['serialize']) && isset($info['serialized default'])) { - $object->$field = unserialize($info['serialized default']); - } - else if (isset($info['default'])) { - $object->$field = $info['default']; - } - // Some database types do not support defaults but we want to - // provide one in code. This one will only be used on the in code - // object, never in the database. - else if (isset($info['object default'])) { - $object->$field = $info['object default']; - } - else { - $object->$field = ''; - } - } - } - - return $object; -} - -/** - * Load an object with data, presumably retrieved via db_fetch_object. - * - * @param &$object - * The object to load data onto. This will be modified in place. - * @param $table - * The name of the table, which will be loaded from schema. - * @param $data - * The data to load via db_fetch_object(). - */ -function ctools_object_load(&$object, $table, $data) { - $schema = drupal_get_schema($table); - - if (!$schema) { - return; - } - - // Go through our schema and build correlations. - foreach ($schema['fields'] as $field => $info) { - if (isset($data->$field)) { - $object->$field = empty($info['serialize']) ? $data->$field : unserialize($data->$field); - } - } -} - -/** - * Create an object from data loaded from the database. - * - * @param $table - * The name of the table, which will be loaded from schema. - * @param $data - * The data to load via db_fetch_object(). - * - * @return - * The initialized object. - */ -function ctools_object_create($table, $data) { - $object = ctools_object_init($table); - ctools_object_load($object, $table, $data); - - return $object; -} \ No newline at end of file diff --git a/delegator/delegator.install b/delegator/delegator.install index 06e922a3..cb578ba9 100644 --- a/delegator/delegator.install +++ b/delegator/delegator.install @@ -22,6 +22,9 @@ function delegator_schema_1() { $schema = array(); $schema['delegator_handlers'] = array( + 'export' => array( + 'identifier' => 'handler', + ), 'fields' => array( 'did' => array( 'type' => 'serial', @@ -90,6 +93,9 @@ function delegator_schema_1() { $schema['delegator_pages'] = array( 'description' => t('Contains page subtasks for implementing pages with arbitrary tasks.'), + 'export' => array( + 'identifier' => 'page', + ), 'fields' => array( 'pid' => array( 'type' => 'serial', diff --git a/delegator/delegator.module b/delegator/delegator.module index 42e0f4a6..88001e78 100644 --- a/delegator/delegator.module +++ b/delegator/delegator.module @@ -446,7 +446,7 @@ function delegator_export_task_handler($handler, $indent = '') { $append = $function($handler, $indent); } - $output = ctools_export_object('delegator_handlers', $handler, 'handler', $indent); + $output = ctools_export_object('delegator_handlers', $handler, $indent); $output .= $append; return $output; diff --git a/delegator/plugins/tasks/page.inc b/delegator/plugins/tasks/page.inc index ede233d3..0bf9dbe9 100644 --- a/delegator/plugins/tasks/page.inc +++ b/delegator/plugins/tasks/page.inc @@ -409,7 +409,7 @@ function delegator_page_delete($subtask) { */ function delegator_page_export($subtask, $with_handlers = FALSE, $indent = '') { ctools_include('export'); - $output = ctools_export_object('delegator_pages', $subtask, 'page', $indent); + $output = ctools_export_object('delegator_pages', $subtask, $indent); if ($with_handlers) { $handlers = delegator_load_task_handlers(delegator_get_task('page'), $subtask->name); $output .= $indent . '$page->default_handlers = array();' . "\n"; diff --git a/includes/export.inc b/includes/export.inc index 8bf11e7b..233b232b 100644 --- a/includes/export.inc +++ b/includes/export.inc @@ -102,7 +102,7 @@ function ctools_export_load_object($table, $type = 'all', $args = array()) { // Unpack the results of the query onto objects and cache them. while ($data = db_fetch_object($result)) { - $object = ctools_export_unpack_object($schema, $data, $export['object']); + $object = _ctools_export_unpack_object($schema, $data, $export['object']); $object->type = t('Normal'); $object->export_type = EXPORT_IN_DATABASE; @@ -241,7 +241,7 @@ function ctools_get_default_object($table, $name) { * If an object, data will be unpacked onto it. If a string * an object of that type will be created. */ -function ctools_export_unpack_object($schema, $data, $object = 'stdClass') { +function _ctools_export_unpack_object($schema, $data, $object = 'stdClass') { if (is_string($object)) { if (class_exists($object)) { $object = new $object; @@ -259,6 +259,19 @@ function ctools_export_unpack_object($schema, $data, $object = 'stdClass') { return $object; } +/** + * Unpack data loaded from the database onto an object. + * + * @param $table + * The name of the table this object represents. + * @param $data + * The data as loaded by db_fetch_object(). + */ +function ctools_export_unpack_object($table, $data) { + $schema = ctools_export_get_schema($table); + return _ctools_export_unpack_object($schema, $data, $schema['export']['object']); +} + /** * Export a field. * @@ -297,12 +310,17 @@ function ctools_var_export($var, $prefix = '') { /** * Export an object into code. */ -function ctools_export_object($table, $object, $identifier, $indent = '', $additions = array(), $additions2 = array()) { - $schema = drupal_get_schema($table); +function ctools_export_object($table, $object, $indent = '', $identifier = NULL, $additions = array(), $additions2 = array()) { + $schema = ctools_export_get_schema($table); + if (!isset($identifier)) { + $identifier = $schema['export']['identifier']; + } $output = $indent . '$' . $identifier . ' = new ' . get_class($object) . ";\n"; - $output .= $indent . '$' . $identifier . '->disabled' . ' = FALSE; /* Edit this to true to make a default ' . $identifier . ' disabled initially */' . "\n"; + if ($schema['export']['can disable']) { + $output .= $indent . '$' . $identifier . '->disabled' . ' = FALSE; /* Edit this to true to make a default ' . $identifier . ' disabled initially */' . "\n"; + } // Put top additions here: foreach ($additions as $field => $value) { @@ -358,6 +376,8 @@ function ctools_export_get_schema($table) { 'object' => 'stdClass', 'status' => 'default_' . $table, 'default hook' => 'default_' . $table, + 'can disable' => TRUE, + 'identifier' => $table, ); return $schema; @@ -406,11 +426,11 @@ function ctools_export_form(&$form_state, $code, $title = '') { * use 'object default' to fill in default values if default is not set * That's a little safer to use as it won't cause weird database default situations. */ -function ctools_export_new_object($table) { +function ctools_export_new_object($table, $set_defaults = TRUE) { $schema = ctools_export_get_schema($table); $export = $schema['export']; - $object = new stdClass; + $object = new $export['object']; foreach ($schema['fields'] as $field => $info) { if (isset($info['object default'])) { $object->$field = $info['object default']; @@ -423,9 +443,10 @@ function ctools_export_new_object($table) { } } - // Set some defaults so this data always exists. - - $object->export_type = EXPORT_IN_DATABASE; - $object->type = t('Local'); + if ($set_defaults) { + // Set some defaults so this data always exists. + $object->export_type = EXPORT_IN_DATABASE; + $object->type = t('Local'); + } return $object; } -- GitLab