t("Context matches string"), 'description' => t('Control access by context string match.'), 'callback' => 'ctools_context_matches_string_ctools_access_check', 'settings form' => 'ctools_context_matches_string_ctools_access_settings', 'settings form submit' => 'ctools_context_matches_string_ctools_access_settings_submit', 'summary' => 'ctools_context_matches_string_ctools_access_summary', 'required context' => new ctools_context_required(t('String'), 'string'), ); } /** * Settings form */ function ctools_context_matches_string_ctools_access_settings(&$form, &$form_state, $conf) { $form['settings']['context_matches_string_rule'] = array( '#type' => 'radios', '#title' => t('Matches or doesn\'t match?'), '#options' => array('1' => t('Matches'), '0' => t('Doesn\'t match')), '#default_value' => is_null($conf['context_matches_string_rule']) ? TRUE : $conf['context_matches_string_rule'], // default true if none ); $form['settings']['context_matches_string'] = array( '#type' => 'textfield', '#title' => t('String'), '#default_value' => $conf['context_matches_string'], '#description' => 'Not case-sensitive. Blanks will be ignored / always FALSE.', // TODO: make this regex-able? ); } function ctools_context_matches_string_ctools_access_settings_submit(&$form, &$form_state) { $context = $form_state['contexts'][$form_state['values']['settings']['which_context']]; $form_state['values']['settings']['context_readable'] = $context->keyword . ' (' . $context->identifier . ')'; } /** * Check for access */ function ctools_context_matches_string_ctools_access_check($conf, $contexts) { // match also checks for not-blank -- blank is treated as non-match always! $match = (isset($conf['context_matches_string']) and isset($contexts->data) // is there any case where !empty() does not imply isset()? && !empty($conf['context_matches_string']) and !empty($contexts->data) && strcasecmp($conf['context_matches_string'], $contexts->data) === 0); // case insensitive $should_match = $conf['context_matches_string_rule']; // a string ('0' or '1') if ($should_match === '1' && $match === TRUE) { return TRUE; } // doesn't match? else if ($should_match === '0' && $match === FALSE) { return TRUE; } return FALSE; } /** * Provide a summary description based upon the specified context */ function ctools_context_matches_string_ctools_access_summary($conf, $contexts) { return t('Context \'!context\' !matches_rule string \'!string\'', array('!context' => $contexts->identifier, '!matches_rule' => $conf['context_matches_string_rule'] ? 'matches' : 'doesn\'t match', '!string' => $conf['context_matches_string']) ); }