facetapi.callbacks.inc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. /**
  3. * @file
  4. * Callbacks referenced in hook implementations.
  5. */
  6. /**
  7. * Maps bundle machine names to their human friendly label.
  8. *
  9. * @param array $values
  10. * An array of indexed values being mapped.
  11. * @param array $options
  12. * An associative array of map options containing:
  13. * - entities: An array of entities that $values are bundles for.
  14. *
  15. * @return array
  16. * An array mapping the indexed values to human readable values.
  17. */
  18. function facetapi_map_bundle(array $values, array $options) {
  19. if (empty($options['entities'])) {
  20. $options['entities'] = array('node');
  21. }
  22. foreach ($options['entities'] as $entity_type) {
  23. if ($info = entity_get_info($entity_type)) {
  24. foreach ($info['bundles'] as $bundle_name => $bundle_info) {
  25. $names[$bundle_name] = $bundle_info['label'];
  26. }
  27. }
  28. }
  29. return array_intersect_key($names, array_flip($values));
  30. }
  31. /**
  32. * Map callback for node authors.
  33. */
  34. function facetapi_map_author(array $values) {
  35. $map = array();
  36. $users = user_load_multiple($values);
  37. foreach ($users as $user) {
  38. $map[$user->uid] = format_username($user);
  39. }
  40. if (isset($map[0])) {
  41. $map[0] = variable_get('anonymous', t('Anonymous'));
  42. }
  43. return $map;
  44. }
  45. /**
  46. * Map callback for languages.
  47. */
  48. function facetapi_map_language(array $values) {
  49. $map = array();
  50. $language_list = language_list();
  51. foreach ($values as $language) {
  52. if (isset($language_list[$language])) {
  53. $map[$language] = t($language_list[$language]->name);
  54. }
  55. else {
  56. $map[$language] = t('Language neutral');
  57. }
  58. }
  59. return $map;
  60. }
  61. /**
  62. * Maps date ranges to human readable dates.
  63. *
  64. * @param array $values
  65. * An array of indexed values being mapped.
  66. * @param array $options
  67. * An associative array of map options containing:
  68. * - format callback: The callback used to format the date, defaults to
  69. * "facetapi_format_timestamp".
  70. *
  71. * @return array
  72. * An array mapping the indexed values to human readable values.
  73. */
  74. function facetapi_map_date(array $values, array $options) {
  75. if (empty($options['format callback'])) {
  76. $options['format callback'] = 'facetapi_format_timestamp';
  77. }
  78. $map = array();
  79. foreach ($values as $value) {
  80. $range = explode(' TO ', trim($value, '{[]}'));
  81. if (isset($range[1])) {
  82. $gap = facetapi_get_date_gap($range[0], $range[1]);
  83. $map[$value] = facetapi_format_date($range[0], $gap, $options['format callback']);
  84. }
  85. }
  86. return $map;
  87. }
  88. /**
  89. * Callback that returns the minimum date in the node table.
  90. *
  91. * @param $facet
  92. * An array containing the facet definition.
  93. *
  94. * @return
  95. * The minimum time in the node table.
  96. *
  97. * @todo Cache this value.
  98. */
  99. function facetapi_get_min_date(array $facet) {
  100. $query = db_select('node', 'n')->condition('status', 1);
  101. $query->addExpression('MIN(' . $facet['name'] . ')', 'max');
  102. return $query->execute()->fetch()->max;
  103. }
  104. /**
  105. * Callback that returns the minimum value in the node table.
  106. *
  107. * @param $facet
  108. * An array containing the facet definition.
  109. *
  110. * @return
  111. * The minimum time in the node table.
  112. *
  113. * @todo Cache this value.
  114. */
  115. function facetapi_get_max_date(array $facet) {
  116. $query = db_select('node', 'n')->condition('status', 1);
  117. $query->addExpression('MAX(' . $facet['name'] . ')', 'max');
  118. return $query->execute()->fetch()->max;
  119. }
  120. /**
  121. * Map callback for taxonomy terms.
  122. */
  123. function facetapi_map_taxonomy_terms(array $values) {
  124. $map = array();
  125. $terms = taxonomy_term_load_multiple($values);
  126. foreach ($terms as $term) {
  127. $map[$term->tid] = entity_label('taxonomy_term', $term);
  128. }
  129. return $map;
  130. }
  131. /**
  132. * Gets parent information for taxonomy terms.
  133. *
  134. * @param array $values
  135. * An array containing the term ids.
  136. *
  137. * @return
  138. * An associative array keyed by term ID to parent ID.
  139. */
  140. function facetapi_get_taxonomy_hierarchy(array $values) {
  141. $result = db_select('taxonomy_term_hierarchy', 'th')
  142. ->fields('th', array('tid', 'parent'))
  143. ->condition('th.parent', '0', '>')
  144. ->condition(db_or()
  145. ->condition('th.tid', $values, 'IN')
  146. ->condition('th.parent', $values, 'IN')
  147. )
  148. ->execute();
  149. $parents = array();
  150. foreach ($result as $record) {
  151. $parents[$record->tid][] = $record->parent;
  152. }
  153. return $parents;
  154. }