You are here

function panels_ajax_context_item_add in Panels 6.2

Ajax entry point to add an context

1 string reference to 'panels_ajax_context_item_add'
panels_menu in ./panels.module
Implementation of hook_menu

File

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

Code

function panels_ajax_context_item_add($module = NULL, $type = NULL, $panel_name = NULL, $name = NULL) {
  panels_load_include('plugins');
  panels_load_include('ajax');
  if (!$name) {
    return panels_ajax_render();
  }

  // Load stored object from cache.
  if (!($object = panels_cache_get("panel_object:{$module}", $panel_name))) {
    panels_ajax_render(t('Invalid panel name.'));
  }

  // Get info about what we're adding.
  $info = panels_common_context_data($type, $name);
  if (empty($info)) {
    panels_ajax_render();
  }
  $type_info = panels_common_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 = panels_common_get_arg_id($ref, $name) + 1;

  // 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' => panels_common_get_keyword($object, $info['keyword']),
    'id' => $id,
    'name' => $name,
  );
  $base_contexts = isset($object->base_contexts) ? $object->base_contexts : array();
  $contexts = panels_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 = panels_ajax_form_wrapper($type_info['form id'], $form_state);
  if (empty($output)) {

    // successful submit
    panels_cache_set("panel_object:{$module}", $panel_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,
    );
    panels_common_add_item_to_form($module, $type, $panel_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' => 'panels_common_context_item_form',
      '#panel_name' => $panel_name,
      '#panels_context_type' => 'relationship',
      '#panels_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 = panels_context_load_contexts($object, TRUE, $rel_contexts);
    $available_relationships = panels_get_relevant_relationships($all_contexts);
    $output = new stdClass();
    if (!empty($available_relationships)) {
      panels_common_add_item_table_buttons('relationship', $module, $rel_form, $available_relationships);
      $rel_form = form_builder('dummy_form_id', $rel_form, $arg_form_state);
      $output->replace = array(
        'div#panels-relationships-table div.buttons' => drupal_render($rel_form),
      );
    }
    $output->type = 'add';
    $output->output = theme('panels_common_context_item_row', $type, $arg_form[$type][$position], $position, $position);
    $output->region = '#' . $type . '-table tbody';
    $output->id = '#' . $type . '-row-' . $position;
  }
  panels_ajax_render($output);
}