| Commit | Line | Data |
|---|---|---|
| dfd5d58a | 1 | <?php |
| dfd5d58a SB |
2 | |
| 3 | /** | |
| 4 | * @file | |
| 5 | * Plugin to provide access control/visibility based on existence of a specified context | |
| 6 | */ | |
| 7 | ||
| 8 | $plugin = array( | |
| 9 | 'title' => t("Context exists"), | |
| 10 | 'description' => t('Control access by whether or not a context exists and contains data.'), | |
| 11 | 'callback' => 'ctools_context_exists_ctools_access_check', | |
| 12 | 'settings form' => 'ctools_context_exists_ctools_access_settings', | |
| 13 | 'summary' => 'ctools_context_exists_ctools_access_summary', | |
| 14 | 'required context' => new ctools_context_required(t('Context'), 'any'), | |
| 15 | 'defaults' => array('exists' => TRUE), | |
| 16 | ); | |
| 17 | ||
| 18 | /** | |
| 19 | * Settings form | |
| 20 | */ | |
| a40482ac | 21 | function ctools_context_exists_ctools_access_settings($form, &$form_state, $conf) { |
| dfd5d58a SB |
22 | $form['settings']['exists'] = array( |
| 23 | '#type' => 'radios', | |
| 24 | '#description' => t("Check to see if the context exists (contains data) or does not exist (contains no data). For example, if a context is optional and the path does not contain an argument for that context, it will not exist."), | |
| 25 | '#options' => array(TRUE => t('Exists'), FALSE => t("Doesn't exist")), | |
| 26 | '#default_value' => $conf['exists'], | |
| 27 | ); | |
| a40482ac | 28 | return $form; |
| dfd5d58a SB |
29 | } |
| 30 | ||
| 31 | /** | |
| 32 | * Check for access | |
| 33 | */ | |
| 34 | function ctools_context_exists_ctools_access_check($conf, $context) { | |
| 35 | // xor returns false if the two bools are the same, and true if they are not. | |
| 36 | // i.e, if we asked for context_exists and it does, return true. | |
| 37 | // If we asked for context does not exist and it does, return false. | |
| aeed3fef | 38 | return (empty($context->data) xor !empty($conf['exists'])); |
| dfd5d58a SB |
39 | } |
| 40 | ||
| 41 | /** | |
| 42 | * Provide a summary description based upon the specified context | |
| 43 | */ | |
| 44 | function ctools_context_exists_ctools_access_summary($conf, $context) { | |
| 45 | if (!empty($conf['exists'])) { | |
| 46 | return t('@identifier exists', array('@identifier' => $context->identifier)); | |
| 47 | } | |
| 48 | else { | |
| 49 | return t('@identifier does not exist', array('@identifier' => $context->identifier)); | |
| 50 | } | |
| 51 | } |