[ Index ]

PHP Cross Reference of Drupal 6 (gatewave)

title

Body

[close]

/sites/all/modules/ctools/plugins/access/ -> string_equal.inc (source)

   1  <?php
   2  // $Id: string_equal.inc,v 1.1.2.3 2009/12/29 05:44:04 merlinofchaos Exp $
   3  
   4  /**
   5   * @file
   6   * Plugin to provide access control/visibility based on specified context string matching user-specified string
   7   */
   8  
   9  $plugin = array(
  10    'title' => t("String: comparison"),
  11    'description' => t('Control access by string match.'),
  12    'callback' => 'ctools_string_equal_ctools_access_check',
  13    'settings form' => 'ctools_string_equal_ctools_access_settings',
  14    'summary' => 'ctools_string_equal_ctools_access_summary',
  15    'required context' => new ctools_context_required(t('String'), 'string'),
  16    'defaults' => array('operator' => '=', 'value' => '', 'case' => FALSE),
  17  );
  18  
  19  /**
  20   * Settings form
  21   */
  22  function ctools_string_equal_ctools_access_settings(&$form, &$form_state, $conf) {
  23    $form['settings']['operator'] = array(
  24      '#type' => 'radios',
  25      '#title' => t('Operator'),
  26      '#options' => array(
  27        '=' => t('Equal'),
  28        '!=' => t('Not equal'),
  29        'regex' => t('Regular expression'),
  30        '!regex' => t('Not equal to regular expression'),
  31      ),
  32      '#default_value' => $conf['operator'],
  33      '#description' => t('If using a regular expression, you should enclose the pattern in slashes like so: <em>/foo/</em>. If you need to compare against slashes you can use another character to enclose the pattern, such as @. See <a href="http://www.php.net/manual/en/reference.pcre.pattern.syntax.php">PHP regex documentation</a> for more.'),
  34    );
  35  
  36    $form['settings']['value'] = array(
  37      '#type' => 'textfield',
  38      '#title' => t('String'),
  39      '#default_value' => $conf['value'],
  40    );
  41  
  42    $form['settings']['case'] = array(
  43      '#type' => 'checkbox',
  44      '#title' => t('Case sensitive'),
  45      '#default_value' => $conf['case'],
  46    );
  47  }
  48  
  49  /**
  50   * Check for access
  51   */
  52  function ctools_string_equal_ctools_access_check($conf, $context) {
  53    if (empty($context) || empty($context->data)) {
  54      $string = '';
  55    }
  56    else {
  57      $string = $context->data;
  58    }
  59  
  60    $value = $conf['value'];
  61    if (empty($conf['case'])) {
  62      $string = drupal_strtolower($string);
  63      $value = drupal_strtolower($value);
  64    }
  65  
  66    switch ($conf['operator']) {
  67      case '=':
  68        return $string === $value;
  69      case '!=':
  70        return $string !== $value;
  71      case 'regex':
  72        return preg_match($value, $string);
  73      case '!regex':
  74        return !preg_match($value, $string);
  75    }
  76  }
  77  
  78  /**
  79   * Provide a summary description based upon the specified context
  80   */
  81  function ctools_string_equal_ctools_access_summary($conf, $context) {
  82    $values = array('@identifier' => $context->identifier, '@value' => $conf['value']);
  83    switch ($conf['operator']) {
  84      case '=':
  85        return t('@identifier is "@value"', $values);
  86      case '!=':
  87        return t('@identifier is not "@value"', $values);
  88      case 'regex':
  89        return t('@identifier matches "@value"', $values);
  90      case '!regex':
  91        return t('@identifier does not match "@value"', $values);
  92    }
  93  }
  94  


Generated: Thu Mar 24 11:18:33 2011 Cross-referenced by PHPXref 0.7