comparison sites/all/modules/ctools/plugins/access/string_length.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 length of
6 * a string context.
7 */
8
9 $plugin = array(
10 'title' => t("String: length"),
11 'description' => t('Control access by length of string context.'),
12 'callback' => 'ctools_string_length_ctools_access_check',
13 'settings form' => 'ctools_string_length_ctools_access_settings',
14 'summary' => 'ctools_string_length_ctools_access_summary',
15 'required context' => new ctools_context_required(t('String'), 'string'),
16 'defaults' => array('operator' => '=', 'length' => 0),
17 );
18
19 /**
20 * Settings form for the 'by role' access plugin.
21 */
22 function ctools_string_length_ctools_access_settings($form, &$form_state, $conf) {
23 $form['settings']['operator'] = array(
24 '#type' => 'radios',
25 '#title' => t('Operator'),
26 '#options' => array(
27 '>' => t('Greater than'),
28 '>=' => t('Greater than or equal to'),
29 '=' => t('Equal to'),
30 '!=' => t('Not equal to'),
31 '<' => t('Less than'),
32 '<=' => t('Less than or equal to'),
33 ),
34 '#default_value' => $conf['operator'],
35 );
36 $form['settings']['length'] = array(
37 '#type' => 'textfield',
38 '#title' => t('Length of string'),
39 '#size' => 3,
40 '#default_value' => $conf['length'],
41 '#description' => t('Access/visibility will be granted based on string context length.'),
42 );
43 return $form;
44 }
45
46 /**
47 * Check for access.
48 */
49 function ctools_string_length_ctools_access_check($conf, $context) {
50 if (empty($context) || empty($context->data)) {
51 $length = 0;
52 }
53 else {
54 $length = drupal_strlen($context->data);
55 }
56
57 switch ($conf['operator']) {
58 case '<':
59 return $length < $conf['length'];
60 case '<=':
61 return $length <= $conf['length'];
62 case '==':
63 return $length == $conf['length'];
64 case '!=':
65 return $length != $conf['length'];
66 case '>':
67 return $length > $conf['length'];
68 case '>=':
69 return $length >= $conf['length'];
70 }
71 }
72
73 /**
74 * Provide a summary description based upon the checked roles.
75 */
76 function ctools_string_length_ctools_access_summary($conf, $context) {
77 return t('@identifier must be @comp @length characters', array('@identifier' => $context->identifier, '@comp' => $conf['operator'], '@length' => $conf['length']));
78 }