facetapi.requirements.inc 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. /**
  3. * @file
  4. * Requirement checking callbacks.
  5. */
  6. /**
  7. * Checks equality of one or more realm properties.
  8. *
  9. * @param array $realm
  10. * The realm definition as returned by facetapi_realm_load().
  11. * @param array $facet
  12. * The facet definition as returned by facetapi_facet_load().
  13. * @param array $options
  14. * An array of values keyed by properties that are being compared.
  15. *
  16. * @return
  17. * TRUE if all property values are equal, FALSE otherwise.
  18. */
  19. function facetapi_requirement_realm_property(array $realm, array $facet, array $options, $operator = 'AND') {
  20. return facetapi_requirement_property($realm, $options, $operator);
  21. }
  22. /**
  23. * Checks equality of one or more facet properties.
  24. *
  25. * @param array $realm
  26. * The realm definition as returned by facetapi_realm_load().
  27. * @param array $facet
  28. * The facet definition as returned by facetapi_facet_load().
  29. * @param array $options
  30. * An array of values keyed by properties that are being compared.
  31. *
  32. * @return
  33. * TRUE if all property values are equal, FALSE otherwise.
  34. */
  35. function facetapi_requirement_facet_property(array $realm, array $facet, array $options, $operator = 'AND') {
  36. return facetapi_requirement_property($facet, $options, $operator);
  37. }
  38. /**
  39. * Checks the equality of one or more properties.
  40. *
  41. * @param array $definition
  42. * The facet or realm definition.
  43. * @param array $options
  44. * An array of values keyed by properties that are being compared.
  45. *
  46. * @return
  47. * TRUE if all property values are equal, FALSE otherwise.
  48. */
  49. function facetapi_requirement_property(array $definition, array $options, $operator) {
  50. $passed = TRUE;
  51. foreach ($options as $key => $requirement) {
  52. $condition = $definition[$key];
  53. // we always expect an array
  54. if (!is_array($requirement)) {
  55. $requirement = array($requirement);
  56. }
  57. // we always expect an array
  58. if (!is_array($condition)) {
  59. $condition = array($condition);
  60. }
  61. // if the keys are numeric, map assoc them so intersect works
  62. if (is_int(key($requirement))) {
  63. $requirement = drupal_map_assoc($requirement);
  64. }
  65. // if the keys are numeric, map assoc them so intersect works
  66. if (is_int(key($condition))) {
  67. $condition = drupal_map_assoc($condition);
  68. }
  69. // Check if it is either an AND or OR operation and act accordingly
  70. if ($operator == 'AND') {
  71. if (array_intersect_key($condition, $requirement) != $requirement) {
  72. $passed = FALSE;
  73. break;
  74. }
  75. }
  76. elseif ($operator == 'OR') {
  77. if (!array_intersect_key($condition, $requirement)) {
  78. $passed = FALSE;
  79. break;
  80. }
  81. }
  82. }
  83. return $passed;
  84. }
  85. /**
  86. * Checks whether one or more realm properties are set.
  87. *
  88. * @param array $realm
  89. * The realm definition as returned by facetapi_realm_load().
  90. * @param array $facet
  91. * The facet definition as returned by facetapi_facet_load().
  92. * @param array $options
  93. * An array of boolean statuses keyed by properties being checked.
  94. *
  95. * @return
  96. * TRUE if all properties match the passed statues, FALSE otherwise.
  97. */
  98. function facetapi_requirement_realm_property_set(array $realm, array $facet, array $options) {
  99. return facetapi_requirement_property_set($realm, $options);
  100. }
  101. /**
  102. * Checks whether one or more facet properties are set.
  103. *
  104. * @param array $realm
  105. * The realm definition as returned by facetapi_realm_load().
  106. * @param array $facet
  107. * The facet definition as returned by facetapi_facet_load().
  108. * @param array $options
  109. * An array of boolean statuses keyed by properties being checked.
  110. *
  111. * @return
  112. * TRUE if all properties match the passed statues, FALSE otherwise.
  113. */
  114. function facetapi_requirement_facet_property_set(array $realm, array $facet, array $options) {
  115. return facetapi_requirement_property_set($facet, $options);
  116. }
  117. /**
  118. * Checks whether one or more properties are set.
  119. *
  120. * @param array $definition
  121. * The facet or realm definition.
  122. * @param array $options
  123. * An array of boolean statuses keyed by properties being checked.
  124. *
  125. * @return
  126. * TRUE if all properties match the passed statues, FALSE otherwise.
  127. */
  128. function facetapi_requirement_property_set(array $definition, array $options) {
  129. $passed = TRUE;
  130. foreach ($options as $key => $requirement) {
  131. if (!($requirement ? !empty($definition[$key]) : empty($definition[$key]))) {
  132. $passed = FALSE;
  133. break;
  134. }
  135. }
  136. return $passed;
  137. }
  138. /**
  139. * Checks whether the facet is hierarchical.
  140. *
  141. * @param array $realm
  142. * The realm definition as returned by facetapi_realm_load().
  143. * @param array $facet
  144. * The facet definition as returned by facetapi_facet_load().
  145. * @param $option
  146. * TRUE if the facet should be hierarchical, FALSE if it should be flat.
  147. *
  148. * @return
  149. * TRUE if the hierarchical status matches $option, FALSE otherwise.
  150. */
  151. function facetapi_requirement_facet_hierarchical(array $realm, array $facet, $option) {
  152. $options = array('hierarchy callback' => (bool) $option);
  153. return facetapi_requirement_facet_property_set($realm, $facet, $options);
  154. }