summaryrefslogtreecommitdiff
path: root/sites/all/modules/ctools/plugins/access/string_length.inc
blob: 91abf2276fadfd202f3b9f423ac7c67265819e93 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?php

/**
 * @file
 * Plugin to provide access control/visibility based on length of
 * a string context.
 */

$plugin = array(
  'title' => t("String: length"),
  'description' => t('Control access by length of string context.'),
  'callback' => 'ctools_string_length_ctools_access_check',
  'settings form' => 'ctools_string_length_ctools_access_settings',
  'summary' => 'ctools_string_length_ctools_access_summary',
  'required context' => new ctools_context_required(t('String'), 'string'),
  'defaults' => array('operator' => '=', 'length' => 0),
);

/**
 * Settings form for the 'by role' access plugin.
 */
function ctools_string_length_ctools_access_settings($form, &$form_state, $conf) {
  $form['settings']['operator'] = array(
    '#type' => 'radios',
    '#title' => t('Operator'),
    '#options' => array(
      '>' => t('Greater than'),
      '>=' => t('Greater than or equal to'),
      '=' => t('Equal to'),
      '!=' => t('Not equal to'),
      '<' => t('Less than'),
      '<=' => t('Less than or equal to'),
    ),
    '#default_value' => $conf['operator'],
  );
  $form['settings']['length'] = array(
    '#type' => 'textfield',
    '#title' => t('Length of string'),
    '#size' => 3,
    '#default_value' => $conf['length'],
    '#description' => t('Access/visibility will be granted based on string context length.'),
  );
  return $form;
}

/**
 * Check for access.
 */
function ctools_string_length_ctools_access_check($conf, $context) {
  if (empty($context) || empty($context->data)) {
    $length = 0;
  }
  else {
    $length = drupal_strlen($context->data);
  }

  switch ($conf['operator']) {
    case '<':
      return $length < $conf['length'];
    case '<=':
      return $length <= $conf['length'];
    case '=':
      return $length == $conf['length'];
    case '!=':
      return $length != $conf['length'];
    case '>':
      return $length > $conf['length'];
    case '>=':
      return $length >= $conf['length'];
  }
  // Invalid Operator sent, return FALSE.
  return FALSE;
}

/**
 * Provide a summary description based upon the checked roles.
 */
function ctools_string_length_ctools_access_summary($conf, $context) {
  return t('@identifier must be @comp @length characters', array('@identifier' => $context->identifier, '@comp' => $conf['operator'], '@length' => $conf['length']));
}