You are here

function ctools_context_ajax_item_add in Chaos Tool Suite (ctools) 6

Same name and namespace in other branches
  1. 7 includes/context-admin.inc \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 368
includes/common-context.inc Provide API for adding contexts for modules that embed displays.

Code

function ctools_context_ajax_item_add($module = NULL, $type = NULL, $object_name = NULL, $name = NULL) {
  ctools_include('ajax');
  ctools_include('modal');
  ctools_include('context');
  ctools_include('object-cache');
  if (!$name) {
    return ctools_ajax_render_error();
  }

  // Load stored object from cache.
  if (!($object = ctools_context_cache_get($module, $object_name))) {
    ctools_ajax_render_error(t('Invalid object name.'));
  }

  // Get info about what we're adding.
  $info = ctools_context_data($type, $name);
  if (empty($info)) {
    return ctools_ajax_render_error();
  }
  $type_info = ctools_context_info($type);

  // 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']};

  // Give this item an id, which is really just the nth version
  // of this particular context.
  $id = ctools_context_next_id($ref, $name);

  // Figure out the position for our new context.
  $position = empty($ref) ? 0 : max(array_keys($ref)) + 1;

  // Create the basis for our new context.
  $ref[$position] = array(
    'identifier' => $info['title'] . ($id > 1 ? ' ' . $id : ''),
    'keyword' => ctools_get_keyword($object, $info['keyword']),
    'id' => $id,
    'name' => $name,
  );
  if (isset($type_info['settings'])) {
    $ref[$position][$type_info['settings']] = isset($info['defaults']) ? $info['defaults'] : array();
  }
  $base_contexts = isset($object->base_contexts) ? $object->base_contexts : array();
  $contexts = ctools_context_load_contexts($object, TRUE, $base_contexts);
  $form_state = array(
    'object' => &$object,
    'module' => $module,
    'type' => $type,
    'name' => $name,
    'ajax' => TRUE,
    'info' => $info,
    'position' => $position,
    'contexts' => $contexts,
    'ref' => &$ref,
    'title' => t('Add @type "@context"', array(
      '@type' => $type_info['singular title'],
      '@context' => $info['title'],
    )),
  );
  $output = ctools_modal_form_wrapper($type_info['form id'], $form_state);
  if (empty($output)) {

    // successful submit
    ctools_context_cache_set($module, $object_name, $object);
    $arg_form_state = array();
    $arg_form = array(
      '#post' => array(),
      '#programmed' => FALSE,
      '#tree' => FALSE,
    );

    // 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($module, $type, $object_name, $arg_form[$type][$position], $position, $ref[$position]);
    $arg_form = form_builder($type_info['form id'], $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',
      '#object_name' => $object_name,
      '#ctools_context_type' => 'relationship',
      '#ctools_context_module' => $module,
      '#only_buttons' => TRUE,
      '#post' => array(),
      '#programmed' => FALSE,
      '#tree' => FALSE,
    );
    $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', $module, $rel_form, $available_relationships);
      $rel_form = form_builder('dummy_form_id', $rel_form, $arg_form_state);
      $output[] = ctools_ajax_command_replace('div#ctools-relationships-table div.buttons', drupal_render($rel_form));
    }
    $text = theme('ctools_context_item_row', $type, $arg_form[$type][$position], $position, $position);
    $output[] = ctools_ajax_command_append('#' . $type . '-table tbody', $text);
    $output[] = ctools_ajax_command_changed('#' . $type . '-row-' . $position, '.title');
    $output[] = ctools_modal_command_dismiss();
  }
  ctools_ajax_render($output);
}