You are here

function ctools_context_ajax_item_edit in Chaos Tool Suite (ctools) 6

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

Code

function ctools_context_ajax_item_edit($module = NULL, $type = NULL, $object_name = NULL, $position = NULL) {
  ctools_include('ajax');
  ctools_include('modal');
  ctools_include('context');
  ctools_include('object-cache');
  if (!isset($position)) {
    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.'));
  }
  $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']};
  $name = $ref[$position]['name'];
  if (empty($name)) {
    ctools_ajax_render_error();
  }

  // load the context
  $info = ctools_context_data($type, $name);
  if (empty($info)) {
    ctools_ajax_render_error(t('Invalid context type'));
  }
  $base_contexts = isset($object->base_contexts) ? $object->base_contexts : array();
  $contexts = ctools_context_load_contexts($object, TRUE, $base_contexts);

  // Remove this context, because we can't really allow circular contexts.
  unset($contexts[ctools_context_id($ref[$position])]);
  $form_state = array(
    'object' => &$object,
    'module' => $module,
    'type' => $type,
    'name' => $name,
    'ajax' => TRUE,
    'info' => $info,
    'position' => $position,
    'contexts' => $contexts,
    'ref' => &$ref,
    'title' => t('Edit @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);
    $output = array();
    $output[] = ctools_modal_command_dismiss();
    $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);
    $output[] = ctools_ajax_command_replace('#' . $type . '-row-' . $position, theme('ctools_context_item_row', $type, $arg_form[$type][$position], $position, $position));
    $output[] = ctools_ajax_command_changed('#' . $type . '-row-' . $position, '.title');
  }
  ctools_ajax_render($output);
}