You are here

function ctools_context_ajax_item_edit in Chaos Tool Suite (ctools) 7

Same name and namespace in other branches
  1. 6 includes/context-admin.inc \ctools_context_ajax_item_edit()

Ajax entry point to edit an item

1 string reference to 'ctools_context_ajax_item_edit'
ctools_context_menu in includes/context.menu.inc
@file Contains menu item registration for the context tool.

File

includes/context-admin.inc, line 497
includes/common-context.inc Provide API for adding contexts for modules that embed displays.

Code

function ctools_context_ajax_item_edit($mechanism = NULL, $type = NULL, $cache_key = NULL, $position = NULL, $step = NULL) {
  ctools_include('ajax');
  ctools_include('modal');
  ctools_include('context');
  ctools_include('cache');
  ctools_include('plugins-admin');
  if (!isset($position)) {
    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.'));
  }
  $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']};
  if (empty($step) || empty($object->temporary)) {

    // Create the basis for our new context.
    $conf = $object->{$type_info['key']}[$position];
    $object->temporary =& $conf;
  }
  else {
    $conf =& $object->temporary;
  }
  $name = $ref[$position]['name'];
  if (empty($name)) {
    ctools_ajax_render_error();
  }

  // load the plugin definition
  $plugin_definition = ctools_context_get_plugin($type, $name);
  if (empty($plugin_definition)) {
    ctools_ajax_render_error(t('Invalid context type'));
  }

  // Load the contexts
  $base_contexts = isset($object->base_contexts) ? $object->base_contexts : array();
  $contexts = ctools_context_load_contexts($object, TRUE, $base_contexts);
  $form_state = array(
    'ajax' => TRUE,
    'modal' => TRUE,
    'modal return' => TRUE,
    'object' => &$object,
    'conf' => &$conf,
    'position' => $position,
    'plugin' => $plugin_definition,
    'type' => $type,
    'contexts' => $contexts,
    'title' => t('Edit @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/configure/{$mechanism}/{$type}/{$cache_key}/{$position}/%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
    $ref[$position] = $conf;
    if (isset($object->temporary)) {
      unset($object->temporary);
    }
    ctools_cache_operation($mechanism, $cache_key, 'finalize', $object);
    $output = array();
    $output[] = ctools_modal_command_dismiss();
    $arg_form_state = form_state_defaults() + array(
      'values' => array(),
      'process_input' => FALSE,
      'complete form' => array(),
    );
    $arg_form = array(
      '#post' => array(),
      '#parents' => array(),
      '#array_parents' => 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($mechanism, $type, $cache_key, $arg_form[$type][$position], $position, $ref[$position]);
    $arg_form = form_builder('ctools_context_form', $arg_form, $arg_form_state);
    $theme_vars = array();
    $theme_vars['type'] = $type;
    $theme_vars['form'] = $arg_form[$type][$position];
    $theme_vars['position'] = $position;
    $theme_vars['count'] = $position;
    $output[] = ajax_command_replace('#' . $type . '-row-' . $position, theme('ctools_context_item_row', $theme_vars));
    $output[] = ajax_command_changed('#' . $type . '-row-' . $position, '.title');
  }
  else {
    $output = ctools_modal_form_render($form_state, $output);
  }
  print ajax_render($output);
  exit;
}