function ctools_context_ajax_item_add
Ajax entry point to add an context
1 string reference to 'ctools_context_ajax_item_add'
- ctools_context_menu in includes/
context.menu.inc - @file Contains menu item registration for the context tool.
File
-
includes/
context-admin.inc, line 323
Code
function ctools_context_ajax_item_add($mechanism = NULL, $type = NULL, $cache_key = NULL, $name = NULL, $step = NULL) {
ctools_include('ajax');
ctools_include('modal');
ctools_include('context');
ctools_include('cache');
ctools_include('plugins-admin');
if (!$name) {
return ctools_ajax_render_error();
}
// Load stored object from cache.
if (!($object = ctools_cache_get($mechanism, $cache_key))) {
ctools_ajax_render_error(t('Invalid object name.'));
}
// Get info about what we're adding, i.e, relationship, context, argument, etc.
$plugin_definition = ctools_context_get_plugin($type, $name);
if (empty($plugin_definition)) {
ctools_ajax_render_error(t('Invalid context type'));
}
// Set up the $conf array for this plugin
if (empty($step) || empty($object->temporary)) {
// Create the basis for our new context.
$conf = ctools_context_get_defaults($plugin_definition, $object, $type);
$object->temporary =& $conf;
}
else {
$conf =& $object->temporary;
}
// Load the contexts that may be used.
$base_contexts = isset($object->base_contexts) ? $object->base_contexts : array();
$contexts = ctools_context_load_contexts($object, TRUE, $base_contexts);
$type_info = ctools_context_info($type);
$form_state = array(
'ajax' => TRUE,
'modal' => TRUE,
'modal return' => TRUE,
'object' => &$object,
'conf' => &$conf,
'plugin' => $plugin_definition,
'type' => $type,
'contexts' => $contexts,
'title' => t('Add @type "@context"', array(
'@type' => $type_info['singular title'],
'@context' => $plugin_definition['title'],
)),
'type info' => $type_info,
'op' => 'add',
'step' => $step,
);
$form_info = array(
'path' => "ctools/context/ajax/add/{$mechanism}/{$type}/{$cache_key}/{$name}/%step",
'show cancel' => TRUE,
'default form' => 'ctools_edit_context_form_defaults',
'auto cache' => TRUE,
'cache mechanism' => $mechanism,
'cache key' => $cache_key,
// This is stating what the cache will be referred to in $form_state
'cache location' => 'object',
);
if ($type == 'requiredcontext') {
$form_info += array(
'add form name' => 'required context add form',
'edit form name' => 'required context edit form',
);
}
$output = ctools_plugin_configure_form($form_info, $form_state);
if (!empty($form_state['cancel'])) {
$output = array(
ctools_modal_command_dismiss(),
);
}
elseif (!empty($form_state['complete'])) {
// Successful submit -- move temporary data to location.
// Create a reference to the place our context lives. Since this is fairly
// generic, this is the easiest way to get right to the place of the
// object without knowing precisely what data we're poking at.
$ref =& $object->{$type_info['key']};
// Figure out the position for our new context.
$position = empty($ref) ? 0 : max(array_keys($ref)) + 1;
$conf['id'] = ctools_context_next_id($ref, $name);
$ref[$position] = $conf;
if (isset($object->temporary)) {
unset($object->temporary);
}
ctools_cache_operation($mechanism, $cache_key, 'finalize', $object);
// Very irritating way to update the form for our contexts.
$arg_form_state = form_state_defaults() + array(
'values' => array(),
'process_input' => FALSE,
'complete form' => array(),
);
$rel_form_state = $arg_form_state;
$arg_form = array(
'#post' => array(),
'#programmed' => FALSE,
'#tree' => FALSE,
'#parents' => array(),
'#array_parents' => array(),
);
// Build a chunk of the form to merge into the displayed form
$arg_form[$type] = array(
'#tree' => TRUE,
);
$arg_form[$type][$position] = array(
'#tree' => TRUE,
);
ctools_context_add_item_to_form($mechanism, $type, $cache_key, $arg_form[$type][$position], $position, $ref[$position]);
$arg_form = form_builder('ctools_context_form', $arg_form, $arg_form_state);
// Build the relationships table so we can ajax it in.
// This is an additional thing that goes in here.
$rel_form = array(
'#theme' => 'ctools_context_item_form',
'#cache_key' => $cache_key,
'#ctools_context_type' => 'relationship',
'#ctools_context_module' => $mechanism,
'#only_buttons' => TRUE,
'#post' => array(),
'#programmed' => FALSE,
'#tree' => FALSE,
'#parents' => array(),
'#array_parents' => array(),
);
$rel_form['relationship'] = array(
'#tree' => TRUE,
);
// Allow an object to set some 'base' contexts that come from elsewhere.
$rel_contexts = isset($object->base_contexts) ? $object->base_contexts : array();
$all_contexts = ctools_context_load_contexts($object, TRUE, $rel_contexts);
$available_relationships = ctools_context_get_relevant_relationships($all_contexts);
$output = array();
if (!empty($available_relationships)) {
ctools_context_add_item_table_buttons('relationship', $mechanism, $rel_form, $available_relationships);
$rel_form = form_builder('dummy_form_id', $rel_form, $rel_form_state);
$output[] = ajax_command_replace('div#ctools-relationships-table div.buttons', drupal_render($rel_form));
}
$theme_vars = array();
$theme_vars['type'] = $type;
$theme_vars['form'] = $arg_form[$type][$position];
$theme_vars['position'] = $position;
$theme_vars['count'] = $position;
$text = theme('ctools_context_item_row', $theme_vars);
$output[] = ajax_command_append('#' . $type . '-table tbody', $text);
$output[] = ajax_command_changed('#' . $type . '-row-' . $position, '.title');
$output[] = ctools_modal_command_dismiss();
}
else {
$output = ctools_modal_form_render($form_state, $output);
}
print ajax_render($output);
exit;
}