Projet

Général

Profil

Paste
Télécharger (2,28 ko) Statistiques
| Branche: | Révision:

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

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

    
61
    case '<=':
62
      return $length <= $conf['length'];
63

    
64
    case '=':
65
      return $length == $conf['length'];
66

    
67
    case '!=':
68
      return $length != $conf['length'];
69

    
70
    case '>':
71
      return $length > $conf['length'];
72

    
73
    case '>=':
74
      return $length >= $conf['length'];
75
  }
76
  // Invalid Operator sent, return FALSE.
77
  return FALSE;
78
}
79

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