Skip to content
Snippets Groups Projects
Commit 25f97283 authored by Earl Miles's avatar Earl Miles
Browse files

Implement the whole export/import workflow.

parent 0b229c0d
No related branches found
No related tags found
No related merge requests found
...@@ -33,3 +33,23 @@ function ctools_get_plugins($module, $type, $id = NULL) { ...@@ -33,3 +33,23 @@ function ctools_get_plugins($module, $type, $id = NULL) {
ctools_include('plugins'); ctools_include('plugins');
return _ctools_get_plugins($module, $type, $id); return _ctools_get_plugins($module, $type, $id);
} }
/**
* Provide a form for displaying an export.
*
* This is a simple form that should be invoked like this:
* @code
* $output = drupal_get_form('ctools_export_form', $code, $object_title);
* @endcode
*/
function ctools_export_form(&$form_state, $code, $title = '') {
$lines = substr_count($code, "\n");
$form['code'] = array(
'#type' => 'textarea',
'#title' => $title,
'#default_value' => $code,
'#rows' => $lines,
);
return $form;
}
\ No newline at end of file
...@@ -17,3 +17,16 @@ ...@@ -17,3 +17,16 @@
border: 1px solid red; border: 1px solid red;
padding: 1em; padding: 1em;
} }
.delegator-operations div {
display: inline;
}
.delegator-disabled {
color: #ccc;
}
.delegator-operations select {
width: 10em;
}
This diff is collapsed.
...@@ -26,6 +26,7 @@ function delegator_schema_1() { ...@@ -26,6 +26,7 @@ function delegator_schema_1() {
'type' => 'serial', 'type' => 'serial',
'not null' => TRUE, 'not null' => TRUE,
'description' => t('Primary ID field for the table. Not used for anything except internal lookups.'), 'description' => t('Primary ID field for the table. Not used for anything except internal lookups.'),
'no export' => TRUE,
), ),
'name' => array( 'name' => array(
'type' => 'varchar', 'type' => 'varchar',
......
...@@ -60,6 +60,22 @@ function delegator_menu() { ...@@ -60,6 +60,22 @@ function delegator_menu() {
'file path' => drupal_get_path('module', 'system'), 'file path' => drupal_get_path('module', 'system'),
); );
$items['admin/build/delegator/list'] = array(
'title' => 'Tasks',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10,
);
// Set up our own menu items here.
$items['admin/build/delegator/import'] = array(
'title' => 'Import',
'page callback' => 'drupal_get_form',
'page arguments' => array('delegator_admin_import_task_handler'),
'access arguments' => array('administer delegator'),
'file' => 'delegator.admin.inc',
'type' => MENU_LOCAL_TASK,
);
$tasks = delegator_get_tasks(); $tasks = delegator_get_tasks();
foreach ($tasks as $id => $task) { foreach ($tasks as $id => $task) {
...@@ -95,6 +111,15 @@ function delegator_menu() { ...@@ -95,6 +111,15 @@ function delegator_menu() {
'file' => 'delegator.admin.inc', 'file' => 'delegator.admin.inc',
); );
// Form to add export a handler
$items['admin/build/delegator/' . $id . '/export/%'] = array(
'page callback' => 'delegator_administer_task_handler_export',
'page arguments' => array($id, 5),
'access callback' => $access_callback,
'access arguments' => $access_arguments,
'file' => 'delegator.admin.inc',
);
// Form to break a lock on this task. // Form to break a lock on this task.
$items['admin/build/delegator/' . $id . '/break-lock'] = array( $items['admin/build/delegator/' . $id . '/break-lock'] = array(
'title' => 'Break lock', 'title' => 'Break lock',
...@@ -277,6 +302,22 @@ function delegator_delete_task_handler($handler) { ...@@ -277,6 +302,22 @@ function delegator_delete_task_handler($handler) {
db_query("DELETE FROM {delegator_weights} WHERE name = '%s'", $handler->name); db_query("DELETE FROM {delegator_weights} WHERE name = '%s'", $handler->name);
} }
function delegator_export_task_handler($handler, $indent = '') {
ctools_include('export');
ctools_include('plugins');
$handler = drupal_clone($handler);
$append = '';
if ($function = ctools_plugin_load_function('delegator', 'task_handlers', $handler->handler, 'export')) {
$append = $function($handler, $indent);
}
$output = ctools_export_object('delegator_handlers', $handler, 'handler', $indent);
$output .= $append;
return $output;
}
/** /**
* Set an overidden weight for a task handler. * Set an overidden weight for a task handler.
* *
......
...@@ -9,6 +9,9 @@ task handler definition: ...@@ -9,6 +9,9 @@ task handler definition:
default conf -- either an array() of default conf data or a callback that returns an array. default conf -- either an array() of default conf data or a callback that returns an array.
params: $handler, $task, $subtask_id params: $handler, $task, $subtask_id
save -- callback to call just prior to the task handler being saved so it can adjust its data. save -- callback to call just prior to the task handler being saved so it can adjust its data.
params: &$handler, $update (as drupal_write_record would receive)
export -- callback to call just prior to the task being exported. It should return text to append to the export if necessary.
params: &$handler, $indent
forms => array( forms => array(
'id' => array( 'id' => array(
......
...@@ -18,4 +18,24 @@ Drupal.behaviors.zzGoLastDelegatorTaskList = function(context) { ...@@ -18,4 +18,24 @@ Drupal.behaviors.zzGoLastDelegatorTaskList = function(context) {
Drupal.tableDrag[id].oldRowElement = $row; Drupal.tableDrag[id].oldRowElement = $row;
} }
} }
$('.delegator-operations select:not(.delegator-processed)', context).each(function() {
var $next = $(this).parent().next('input');
$next.hide();
$(this).change(function() {
var val = $(this).val();
// ignore empty
if (!val) {
return;
}
// force confirm on delete
if ($(this).val() == 'delete' && !confirm(Drupal.t('Remove this task?'))) {
$(this).val('');
return;
}
$next.trigger('click');
});
});
} }
\ No newline at end of file
...@@ -152,30 +152,28 @@ function ctools_export_load_object($table, $type = 'all', $args = array()) { ...@@ -152,30 +152,28 @@ function ctools_export_load_object($table, $type = 'all', $args = array()) {
} }
} }
else if ($type == 'names') { else if ($type == 'names') {
if (!in_array($names, $object->{$export['key']})) { if (!in_array($object->{$export['key']}, $args)) {
continue; continue;
} }
} }
// Determine if default panel is enabled or disabled. // Determine if default object is enabled or disabled.
if (isset($status[$object->name])) { if (isset($status[$object->name])) {
$object->disabled = $status[$object->name]; $object->disabled = $status[$object->name];
} }
if (!empty($cache[$table][$object->name])) { if (!empty($cache[$table][$object->name])) {
$cache[$table][$object->name]->type = t('Overridden'); $cache[$table][$object->name]->type = t('Overridden');
$cache[$table][$object->name]->export_status |= EXPORT_IN_CODE; $cache[$table][$object->name]->export_type |= EXPORT_IN_CODE;
if ($type == 'conditions') { if ($type == 'conditions') {
$return[$object->name] = $cache[$table][$object->name]->type; $return[$object->name] = $cache[$table][$object->name];
} }
} }
else { else {
$object->type = t('Default'); $object->type = t('Default');
$object->export_status = EXPORT_IN_CODE; $object->export_type = EXPORT_IN_CODE;
$object->in_code_only = TRUE; $object->in_code_only = TRUE;
// move the 'display' to the new 'primary' location.
$object->primary = $object->display;
unset($object->display);
$cache[$table][$object->name] = $object; $cache[$table][$object->name] = $object;
if ($type == 'conditions') { if ($type == 'conditions') {
$return[$object->name] = $object; $return[$object->name] = $object;
...@@ -212,6 +210,45 @@ function ctools_export_load_object($table, $type = 'all', $args = array()) { ...@@ -212,6 +210,45 @@ function ctools_export_load_object($table, $type = 'all', $args = array()) {
return $return; return $return;
} }
/**
* Get the default version of an object, if it exists.
*
* This function doesn't care if an object is in the database or not and
* does not check. This means that export_type could appear to be incorrect,
* because a version could exist in the database. However, it's not
* incorrect for this function as it is *only* used for the default
* in code version.
*/
function ctools_get_default_object($table, $name) {
$schema = ctools_export_get_schema($table);
$export = $schema['export'];
if (!$export['default hook']) {
return;
}
// @todo add a method to load .inc files for this.
$defaults = module_invoke_all($export['default hook']);
$status = variable_get($export['status'], array());
if (!isset($defaults[$name])) {
return;
}
$object = $defaults[$name];
// Determine if default object is enabled or disabled.
if (isset($status[$object->name])) {
$object->disabled = $status[$object->name];
}
$object->type = t('Default');
$object->export_type = EXPORT_IN_CODE;
$object->in_code_only = TRUE;
return $object;
}
/** /**
* Unpack data loaded from the database onto an object. * Unpack data loaded from the database onto an object.
* *
...@@ -276,6 +313,51 @@ function ctools_var_export($var, $prefix = '') { ...@@ -276,6 +313,51 @@ function ctools_var_export($var, $prefix = '') {
return $output; return $output;
} }
/**
* Export an object into code.
*/
function ctools_export_object($table, $object, $identifier, $indent = '', $additions = array(), $additions2 = array()) {
$schema = drupal_get_schema($table);
$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";
// Put top additions here:
foreach ($additions as $field => $value) {
$output .= $indent . '$' . $identifier . '->' . $field . ' = ' . ctools_var_export($value, $indent) . ";\n";
}
// Go through our schema and build correlations.
foreach ($schema['fields'] as $field => $info) {
if (!empty($info['no export'])) {
continue;
}
if (!isset($object->$field)) {
if (isset($info['default'])) {
$object->$field = $info['default'];
}
else {
$object->$field = '';
}
}
$value = $object->$field;
if ($info['type'] == 'int') {
$value = (isset($info['size']) && $info['size'] == 'tiny') ? (bool) $value : (int) $value;
}
$output .= $indent . '$' . $identifier . '->' . $field . ' = ' . ctools_var_export($value, $indent) . ";\n";
}
// And bottom additions here
foreach ($additions2 as $field => $value) {
$output .= $indent . '$' . $identifier . '->' . $field . ' = ' . ctools_var_export($value, $indent) . ";\n";
}
return $output;
}
/** /**
* Get the schema for a given table. * Get the schema for a given table.
* *
...@@ -303,7 +385,7 @@ function ctools_export_get_schema($table) { ...@@ -303,7 +385,7 @@ function ctools_export_get_schema($table) {
/** /**
* Set the status of a default $object as a variable. * Set the status of a default $object as a variable.
* *
* The status, in this case, is whether or not it is 'enabled' or 'disabled' * The status, in this case, is whether or not it is 'disabled'
* and is only valid for in-code objects that do not have a database * and is only valid for in-code objects that do not have a database
* equivalent. This function does not check to make sure $object actually * equivalent. This function does not check to make sure $object actually
* exists. * exists.
...@@ -311,6 +393,7 @@ function ctools_export_get_schema($table) { ...@@ -311,6 +393,7 @@ function ctools_export_get_schema($table) {
function ctools_export_set_status($table, $name, $new_status = TRUE) { function ctools_export_set_status($table, $name, $new_status = TRUE) {
$schema = ctools_export_get_schema($table); $schema = ctools_export_get_schema($table);
$status = variable_get($schema['export']['status'], array()); $status = variable_get($schema['export']['status'], array());
$status[$name] = $new_status; $status[$name] = $new_status;
variable_set($schema['export']['status'], $status); variable_set($schema['export']['status'], $status);
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment