You are here

function ctools_access_ajax_add in Chaos Tool Suite (ctools) 7

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

AJAX callback to add a new access test to the list.

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

File

includes/context-access-admin.inc, line 252
Contains administrative screens for the access control plugins.

Code

function ctools_access_ajax_add($fragment = NULL, $name = NULL) {
  ctools_include('ajax');
  ctools_include('modal');
  ctools_include('context');
  if (empty($fragment) || empty($name)) {
    ctools_ajax_render_error();
  }
  $plugin = ctools_get_access_plugin($name);
  if (empty($plugin)) {
    ctools_ajax_render_error();
  }

  // Separate the fragment into 'module' and 'argument'.
  if (strpos($fragment, '-') === FALSE) {
    $module = $fragment;
    $argument = NULL;
  }
  else {
    list($module, $argument) = explode('-', $fragment, 2);
  }
  $function = $module . '_ctools_access_get';
  if (!function_exists($function)) {
    ctools_ajax_render_error(t('Missing callback hooks.'));
  }
  list($access, $contexts) = $function($argument);

  // Make sure we have the logged in user context.
  if (!isset($contexts['logged-in-user'])) {
    $contexts['logged-in-user'] = ctools_access_get_loggedin_context();
  }
  if (empty($access['plugins'])) {
    $access['plugins'] = array();
  }
  $test = ctools_access_new_test($plugin);
  $id = $access['plugins'] ? max(array_keys($access['plugins'])) + 1 : 0;
  $access['plugins'][$id] = $test;
  $form_state = array(
    'plugin' => $plugin,
    'id' => $id,
    'test' => &$access['plugins'][$id],
    'access' => &$access,
    'contexts' => $contexts,
    'title' => t('Add criteria'),
    'ajax' => TRUE,
    'modal' => TRUE,
    'modal return' => TRUE,
  );
  $output = ctools_modal_form_wrapper('ctools_access_ajax_edit_item', $form_state);
  $access = $form_state['access'];
  $access['plugins'][$id] = $form_state['test'];
  if (!isset($output[0])) {
    $function = $module . '_ctools_access_set';
    if (function_exists($function)) {
      $function($argument, $access);
    }
    $table = ctools_access_admin_render_table($access, $fragment, $contexts);
    $output = array();
    $output[] = ajax_command_replace('table#ctools-access-table', $table);
    $output[] = ctools_modal_command_dismiss();
  }
  print ajax_render($output);
}