string_equal.inc 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. /**
  3. * @file
  4. * Plugin to provide access control/visibility based on specified context string matching user-specified string
  5. */
  6. $plugin = array(
  7. 'title' => t("String: comparison"),
  8. 'description' => t('Control access by string match.'),
  9. 'callback' => 'ctools_string_equal_ctools_access_check',
  10. 'settings form' => 'ctools_string_equal_ctools_access_settings',
  11. 'summary' => 'ctools_string_equal_ctools_access_summary',
  12. 'required context' => new ctools_context_required(t('String'), 'string'),
  13. 'defaults' => array('operator' => '=', 'value' => '', 'case' => FALSE),
  14. );
  15. /**
  16. * Settings form
  17. */
  18. function ctools_string_equal_ctools_access_settings($form, &$form_state, $conf) {
  19. $form['settings']['operator'] = array(
  20. '#type' => 'radios',
  21. '#title' => t('Operator'),
  22. '#options' => array(
  23. '=' => t('Equal'),
  24. '!=' => t('Not equal'),
  25. 'regex' => t('Regular expression'),
  26. '!regex' => t('Not equal to regular expression'),
  27. ),
  28. '#default_value' => $conf['operator'],
  29. '#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.'),
  30. );
  31. $form['settings']['value'] = array(
  32. '#type' => 'textfield',
  33. '#title' => t('String'),
  34. '#default_value' => $conf['value'],
  35. );
  36. $form['settings']['case'] = array(
  37. '#type' => 'checkbox',
  38. '#title' => t('Case sensitive'),
  39. '#default_value' => $conf['case'],
  40. );
  41. return $form;
  42. }
  43. /**
  44. * Check for access
  45. */
  46. function ctools_string_equal_ctools_access_check($conf, $context) {
  47. if (empty($context) || empty($context->data)) {
  48. $string = '';
  49. }
  50. else {
  51. $string = $context->data;
  52. }
  53. $value = $conf['value'];
  54. if (empty($conf['case'])) {
  55. $string = drupal_strtolower($string);
  56. $value = drupal_strtolower($value);
  57. }
  58. switch ($conf['operator']) {
  59. case '=':
  60. return $string === $value;
  61. case '!=':
  62. return $string !== $value;
  63. case 'regex':
  64. return preg_match($value, $string);
  65. case '!regex':
  66. return !preg_match($value, $string);
  67. }
  68. }
  69. /**
  70. * Provide a summary description based upon the specified context
  71. */
  72. function ctools_string_equal_ctools_access_summary($conf, $context) {
  73. $values = array('@identifier' => $context->identifier, '@value' => $conf['value']);
  74. switch ($conf['operator']) {
  75. case '=':
  76. return t('@identifier is "@value"', $values);
  77. case '!=':
  78. return t('@identifier is not "@value"', $values);
  79. case 'regex':
  80. return t('@identifier matches "@value"', $values);
  81. case '!regex':
  82. return t('@identifier does not match "@value"', $values);
  83. }
  84. }