arg_length.inc 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. /**
  3. * @file
  4. * Plugin to provide access control/visibility based on length of
  5. * simplecontext argument (in URL).
  6. */
  7. /**
  8. * Plugins are described by creating a $plugin array which will be used
  9. * by the system that includes this file.
  10. */
  11. $plugin = array(
  12. 'title' => t("Arg length"),
  13. 'description' => t('Control access by length of simplecontext argument.'),
  14. 'callback' => 'ctools_plugin_example_arg_length_ctools_access_check',
  15. 'settings form' => 'ctools_plugin_example_arg_length_ctools_access_settings',
  16. 'summary' => 'ctools_plugin_example_arg_length_ctools_access_summary',
  17. 'required context' => new ctools_context_required(t('Simplecontext'), 'simplecontext'),
  18. );
  19. /**
  20. * Settings form for the 'by role' access plugin.
  21. */
  22. function ctools_plugin_example_arg_length_ctools_access_settings(&$form, &$form_state, $conf) {
  23. $form['settings']['greater_than'] = array(
  24. '#type' => 'radios',
  25. '#title' => t('Grant access if simplecontext argument length is'),
  26. '#options' => array(1 => t('Greater than'), 0 => t('Less than or equal to')),
  27. '#default_value' => $conf['greater_than'],
  28. );
  29. $form['settings']['arg_length'] = array(
  30. '#type' => 'textfield',
  31. '#title' => t('Length of simplecontext argument'),
  32. '#size' => 3,
  33. '#default_value' => $conf['arg_length'],
  34. '#description' => t('Access/visibility will be granted based on arg length.'),
  35. );
  36. }
  37. /**
  38. * Check for access.
  39. */
  40. function ctools_plugin_example_arg_length_ctools_access_check($conf, $context) {
  41. // As far as I know there should always be a context at this point, but this
  42. // is safe.
  43. if (empty($context) || empty($context->data)) {
  44. return FALSE;
  45. }
  46. $compare = ($context->arg_length > $conf['arg_length']);
  47. if (($compare && $conf['greater_than']) || (!$compare && !$conf['greater_than'])) {
  48. return TRUE;
  49. }
  50. return FALSE;
  51. }
  52. /**
  53. * Provide a summary description based upon the checked roles.
  54. */
  55. function ctools_plugin_example_arg_length_ctools_access_summary($conf, $context) {
  56. return t('Simpletext argument must be !comp @length characters',
  57. array(
  58. '!comp' => $conf['greater_than'] ? 'greater than' : 'less than or equal to',
  59. '@length' => $conf['arg_length'],
  60. ));
  61. }