context_reaction.inc 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. /**
  3. * Base class for a context reaction.
  4. */
  5. class context_reaction {
  6. var $plugin;
  7. var $title;
  8. var $description;
  9. /**
  10. * Clone our references when we're being cloned.
  11. *
  12. * PHP 5.3 performs 'shallow' clones when clone()'ing objects, meaning that
  13. * any objects or arrays referenced by this object will not be copied, the
  14. * cloned object will just reference our objects/arrays instead. By iterating
  15. * over our properties and serializing and unserializing them, we force PHP to
  16. * copy them.
  17. */
  18. function __clone() {
  19. foreach ($this as $key => $val) {
  20. if (is_object($val) || (is_array($val))) {
  21. $this->{$key} = unserialize(serialize($val));
  22. }
  23. }
  24. }
  25. /**
  26. * Constructor. Do not override.
  27. */
  28. function __construct($plugin, $info) {
  29. $this->plugin = $plugin;
  30. $this->title = isset($info['title']) ? $info['title'] : $plugin;
  31. $this->description = isset($info['description']) ? $info['description'] : '';
  32. }
  33. function options_form($context) {
  34. return array();
  35. }
  36. /**
  37. * Options form submit handler.
  38. */
  39. function options_form_submit($values) {
  40. return $values;
  41. }
  42. /**
  43. * Settings form. Provide variable settings for your reaction.
  44. */
  45. function settings_form() {
  46. return array();
  47. }
  48. /**
  49. * Public method that is called from hooks or other integration points with
  50. * Drupal. Note that it is not implemented in the base class, allowing
  51. * extending classes to change the function signature if necessary.
  52. *
  53. * function execute($value) {
  54. * foreach ($this->get_contexts($value) as $context) {
  55. * $this->condition_met($context, $value);
  56. * }
  57. * }
  58. */
  59. /**
  60. * Retrieve active contexts that have values for this reaction.
  61. */
  62. function get_contexts() {
  63. $contexts = array();
  64. foreach (context_active_contexts() as $context) {
  65. if ($this->fetch_from_context($context)) {
  66. $contexts[$context->name] = $context;
  67. }
  68. }
  69. return $contexts;
  70. }
  71. /**
  72. * Retrieve options from the context provided.
  73. */
  74. function fetch_from_context($context) {
  75. return isset($context->reactions[$this->plugin]) ? $context->reactions[$this->plugin] : array();
  76. }
  77. }