context_condition_context.inc 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. /**
  3. * Expose active contexts as a context condition.
  4. */
  5. class context_condition_context extends context_condition_path {
  6. function execute() {
  7. if ($this->condition_used()) {
  8. $active_contexts = array_keys(context_active_contexts());
  9. foreach ($this->get_contexts() as $context) {
  10. if (!in_array($context->name, $active_contexts, TRUE) && $values = $this->fetch_from_context($context, 'values')) {
  11. // Always check against the active contexts.
  12. if ($this->match(array_keys(context_active_contexts()), $values)) {
  13. $this->condition_met($context);
  14. }
  15. }
  16. }
  17. // If the list of active contexts has changed, we need to recurse.
  18. if ($active_contexts != array_keys(context_active_contexts())) {
  19. $this->execute();
  20. }
  21. }
  22. }
  23. /**
  24. * Retrieve all context conditions.
  25. *
  26. * This method is slightly adapted to context_condition::get_contexts() in
  27. * order to ensure that a context that is used as condition in another context
  28. * gets handled before.
  29. */
  30. function get_contexts($value = NULL) {
  31. $map = context_condition_map();
  32. $map = isset($map[$this->plugin]) ? $map[$this->plugin] : array();
  33. $contexts = array();
  34. // Add the contexts that are needed for conditions in the other contexts
  35. // first. Start with the negated ones first, as we can not unset a met
  36. // condition afterwards.
  37. krsort($map);
  38. foreach ($map as $key => $submap) {
  39. // Negated context conditions start with a "~".
  40. if (substr($key, 0, 1) == "~") {
  41. $key = substr($key, 1);
  42. }
  43. if (!isset($contexts[$key])) {
  44. $context = context_load($key);
  45. // Check if context exists. This will fail for wildcards.
  46. if ($context) {
  47. $contexts[$context->name] = $context;
  48. }
  49. }
  50. }
  51. foreach ($map as $key => $submap) {
  52. foreach ($submap as $name) {
  53. if (!isset($contexts[$name])) {
  54. $context = context_load($name);
  55. $contexts[$context->name] = $context;
  56. }
  57. }
  58. }
  59. return $contexts;
  60. }
  61. }