comparison sites/all/modules/ctools/plugins/access/string_equal.inc @ 0:ff03f76ab3fe

initial version
author danieleb <danielebarchiesi@me.com>
date Wed, 21 Aug 2013 18:51:11 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:ff03f76ab3fe
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 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