string_length.inc 2.3 KB

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