123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485 |
- <?php
- function ctools_access_admin_form($form, &$form_state) {
- ctools_include('context');
- $argument = isset($form_state['callback argument']) ? $form_state['callback argument'] : '';
- $fragment = $form_state['module'];
- if ($argument) {
- $fragment .= '-' . $argument;
- }
- $contexts = isset($form_state['contexts']) ? $form_state['contexts'] : array();
- $form['access_table'] = array(
- '#markup' => ctools_access_admin_render_table($form_state['access'], $fragment, $contexts),
- );
- $form['add-button'] = array(
- '#theme' => 'ctools_access_admin_add',
- );
-
- $form['add-button']['add-url'] = array(
- '#attributes' => array('class' => array("ctools-access-add-url")),
- '#type' => 'hidden',
- '#value' => url("ctools/context/ajax/access/add/$fragment", array('absolute' => TRUE)),
- );
- $plugins = ctools_get_relevant_access_plugins($contexts);
- $options = array();
- foreach ($plugins as $id => $plugin) {
- $options[$id] = $plugin['title'];
- }
- asort($options);
- $form['add-button']['type'] = array(
-
- '#attributes' => array('class' => array("ctools-access-add-url")),
- '#type' => 'select',
- '#options' => $options,
- '#required' => FALSE,
- );
- $form['add-button']['add'] = array(
- '#type' => 'submit',
- '#attributes' => array('class' => array('ctools-use-modal')),
- '#id' => "ctools-access-add",
- '#value' => t('Add'),
- );
- $form['logic'] = array(
- '#type' => 'radios',
- '#options' => array(
- 'and' => t('All criteria must pass.'),
- 'or' => t('Only one criteria must pass.'),
- ),
- '#default_value' => isset($form_state['access']['logic']) ? $form_state['access']['logic'] : 'and',
- );
- if (empty($form_state['no buttons'])) {
- $form['buttons']['save'] = array(
- '#type' => 'submit',
- '#value' => t('Save'),
- '#submit' => array('ctools_access_admin_form_submit'),
- );
- }
- return $form;
- }
- function ctools_access_admin_render_table($access, $fragment, $contexts) {
- ctools_include('ajax');
- ctools_include('modal');
- $rows = array();
- if (empty($access['plugins'])) {
- $access['plugins'] = array();
- }
- foreach ($access['plugins'] as $id => $test) {
- $row = array();
- $plugin = ctools_get_access_plugin($test['name']);
- $title = isset($plugin['title']) ? $plugin['title'] : t('Broken/missing access plugin %plugin', array('%plugin' => $test['name']));
- $row[] = array('data' => $title, 'class' => array('ctools-access-title'));
- $description = ctools_access_summary($plugin, $contexts, $test);
- $row[] = array('data' => $description, 'class' => array('ctools-access-description'));
- $operations = ctools_modal_image_button(ctools_image_path('icon-configure.png'), "ctools/context/ajax/access/configure/$fragment/$id", t('Configure settings for this item.'));
- $operations .= ctools_ajax_image_button(ctools_image_path('icon-delete.png'), "ctools/context/ajax/access/delete/$fragment/$id", t('Remove this item.'));
- $row[] = array('data' => $operations, 'class' => array('ctools-access-operations'), 'align' => 'right');
- $rows[] = $row;
- }
- $header = array(
- array('data' => t('Title'), 'class' => array('ctools-access-title')),
- array('data' => t('Description'), 'class' => array('ctools-access-description')),
- array('data' => '', 'class' => array('ctools-access-operations'), 'align' => 'right'),
- );
- if (empty($rows)) {
- $rows[] = array(array('data' => t('No criteria selected, this test will pass.'), 'colspan' => count($header)));
- }
- ctools_modal_add_js();
- return theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => array('id' => 'ctools-access-table')));
- }
- function theme_ctools_access_admin_add($vars) {
- $rows = array(array(drupal_render_children($vars['form'])));
- $output = '<div class="container-inline">';
- $output .= theme('table', array('rows' => $rows));
- $output .= '</div>';
- return $output;
- }
- function ctools_access_admin_form_submit($form, &$form_state) {
- $form_state['access']['logic'] = $form_state['values']['logic'];
- $function = $form_state['module'] . '_ctools_access_clear';
- if (function_exists($function)) {
- $function($form_state['callback argument']);
- }
- }
- 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();
- }
-
- 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);
-
- 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);
- 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);
- }
- function ctools_access_ajax_edit($fragment = NULL, $id = NULL) {
- ctools_include('ajax');
- ctools_include('modal');
- ctools_include('context');
- if (empty($fragment) || !isset($id)) {
- ctools_ajax_render_error();
- }
-
- 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);
- if (empty($access['plugins'][$id])) {
- ctools_ajax_render_error();
- }
-
- if (!isset($contexts['logged-in-user'])) {
- $contexts['logged-in-user'] = ctools_access_get_loggedin_context();
- }
- $plugin = ctools_get_access_plugin($access['plugins'][$id]['name']);
- $form_state = array(
- 'plugin' => $plugin,
- 'id' => $id,
- 'test' => &$access['plugins'][$id],
- 'access' => &$access,
- 'contexts' => $contexts,
- 'title' => t('Edit criteria'),
- 'ajax' => TRUE,
- 'modal' => TRUE,
- 'modal return' => TRUE,
- );
- $output = ctools_modal_form_wrapper('ctools_access_ajax_edit_item', $form_state);
- 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);
- }
- function ctools_access_ajax_edit_item($form, &$form_state) {
- $test = &$form_state['test'];
- $plugin = &$form_state['plugin'];
- if (isset($plugin['required context'])) {
- $form['context'] = ctools_context_selector($form_state['contexts'], $plugin['required context'], $test['context']);
- }
- $form['settings'] = array('#tree' => TRUE);
- if ($function = ctools_plugin_get_function($plugin, 'settings form')) {
- $form = $function($form, $form_state, $test['settings']);
- }
- $form['not'] = array(
- '#type' => 'checkbox',
- '#title' => t('Reverse (NOT)'),
- '#default_value' => !empty($test['not']),
- );
- $form['save'] = array(
- '#type' => 'submit',
- '#value' => t('Save'),
- );
- return $form;
- }
- function ctools_access_ajax_edit_item_validate($form, &$form_state) {
- if ($function = ctools_plugin_get_function($form_state['plugin'], 'settings form validate')) {
- $function($form, $form_state);
- }
- }
- function ctools_access_ajax_edit_item_submit($form, &$form_state) {
- if ($function = ctools_plugin_get_function($form_state['plugin'], 'settings form submit')) {
- $function($form, $form_state);
- }
- $form_state['test']['settings'] = $form_state['values']['settings'];
- if (isset($form_state['values']['context'])) {
- $form_state['test']['context'] = $form_state['values']['context'];
- }
- $form_state['test']['not'] = !empty($form_state['values']['not']);
- }
- function ctools_access_ajax_delete($fragment = NULL, $id = NULL) {
- ctools_include('ajax');
- ctools_include('modal');
- ctools_include('context');
- if (empty($fragment) || !isset($id)) {
- ajax_render_error();
- }
-
- if (strpos($fragment, '-') === FALSE) {
- $module = $fragment;
- $argument = NULL;
- }
- else {
- list($module, $argument) = explode('-', $fragment, 2);
- }
- $function = $module . '_ctools_access_get';
- if (!function_exists($function)) {
- ajax_render_error(t('Missing callback hooks.'));
- }
- list($access, $contexts) = $function($argument);
- if (isset($access['plugins'][$id])) {
- unset($access['plugins'][$id]);
- }
-
- $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);
- print ajax_render($output);
- }
|