Project

General

Profile

Paste
Download (2.79 KB) Statistics
| Branch: | Revision:

root / drupal7 / sites / all / modules / ctools / plugins / access / string_equal.inc @ 6e3ce7c2

1
<?php
2

    
3
/**
4
 * @file
5
 * Plugin to provide access control/visibility based on specified context string matching user-specified string.
6
 */
7

    
8
$plugin = array(
9
  'title' => t("String: comparison"),
10
  'description' => t('Control access by string match.'),
11
  'callback' => 'ctools_string_equal_ctools_access_check',
12
  'settings form' => 'ctools_string_equal_ctools_access_settings',
13
  'summary' => 'ctools_string_equal_ctools_access_summary',
14
  'required context' => new ctools_context_required(t('String'), 'string'),
15
  'defaults' => array('operator' => '=', 'value' => '', 'case' => FALSE),
16
);
17

    
18
/**
19
 * Settings form.
20
 */
21
function ctools_string_equal_ctools_access_settings($form, &$form_state, $conf) {
22
  $form['settings']['operator'] = array(
23
    '#type' => 'radios',
24
    '#title' => t('Operator'),
25
    '#options' => array(
26
      '=' => t('Equal'),
27
      '!=' => t('Not equal'),
28
      'regex' => t('Regular expression'),
29
      '!regex' => t('Not equal to regular expression'),
30
    ),
31
    '#default_value' => $conf['operator'],
32
    '#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.'),
33
  );
34

    
35
  $form['settings']['value'] = array(
36
    '#type' => 'textfield',
37
    '#title' => t('String'),
38
    '#default_value' => $conf['value'],
39
  );
40

    
41
  $form['settings']['case'] = array(
42
    '#type' => 'checkbox',
43
    '#title' => t('Case sensitive'),
44
    '#default_value' => $conf['case'],
45
  );
46
  return $form;
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

    
70
    case '!=':
71
      return $string !== $value;
72

    
73
    case 'regex':
74
      return preg_match($value, $string);
75

    
76
    case '!regex':
77
      return !preg_match($value, $string);
78
  }
79
}
80

    
81
/**
82
 * Provide a summary description based upon the specified context.
83
 */
84
function ctools_string_equal_ctools_access_summary($conf, $context) {
85
  $values = array('@identifier' => $context->identifier, '@value' => $conf['value']);
86
  switch ($conf['operator']) {
87
    case '=':
88
      return t('@identifier is "@value"', $values);
89

    
90
    case '!=':
91
      return t('@identifier is not "@value"', $values);
92

    
93
    case 'regex':
94
      return t('@identifier matches "@value"', $values);
95

    
96
    case '!regex':
97
      return t('@identifier does not match "@value"', $values);
98
  }
99
}