/application/src/Application/Model/Search/FacetGroup.php

https://github.com/dswalker/xerxes · PHP · 239 lines · 125 code · 57 blank · 57 comment · 13 complexity · e08183e4b45e02cce824fe9214a0f4e3 MD5 · raw file

  1. <?php
  2. /*
  3. * This file is part of Xerxes.
  4. *
  5. * (c) California State University <library@calstate.edu>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Application\Model\Search;
  11. use Xerxes\Utility\Parser;
  12. /**
  13. * Search Facet Group
  14. *
  15. * @author David Walker <dwalker@calstate.edu>
  16. */
  17. class FacetGroup
  18. {
  19. public $name; // internal name
  20. public $public; // public facing name
  21. public $display; // to display
  22. public $facets = array();
  23. /**
  24. * Add a facet
  25. *
  26. * @param Facet $facets
  27. */
  28. public function addFacet(Facet $facet)
  29. {
  30. $facet->count_display = Parser::number_format($facet->count);
  31. array_push($this->facets, $facet);
  32. }
  33. /**
  34. * Add a facet to the top of the group
  35. *
  36. * @param Facet $facets
  37. */
  38. public function prependFacet(Facet $facet)
  39. {
  40. $facet->count_display = Parser::number_format($facet->count);
  41. array_unshift($this->facets, $facet);
  42. }
  43. /**
  44. * Get the facets
  45. *
  46. * @return Facet[]
  47. */
  48. public function getFacets()
  49. {
  50. return $this->facets;
  51. }
  52. /**
  53. * Sort facets by name
  54. *
  55. * @param string $order 'desc' or 'asc'
  56. */
  57. public function sortByName($order)
  58. {
  59. $names = array();
  60. // extract the names
  61. foreach ( $this->facets as $facet )
  62. {
  63. array_push($names, strtoupper((string) $facet->name));
  64. }
  65. // now sort them, keeping the key associations
  66. if ( $order == "desc")
  67. {
  68. arsort($names);
  69. }
  70. elseif ( $order == "asc")
  71. {
  72. asort($names);
  73. }
  74. else
  75. {
  76. throw new \Exception("sort order must be 'desc' or 'asc'");
  77. }
  78. // now unset and re-add the facets based on those keys
  79. $facets = $this->facets;
  80. $this->facets = array();
  81. foreach ( array_keys($names) as $key )
  82. {
  83. array_push($this->facets, $facets[$key]);
  84. }
  85. }
  86. /**
  87. * Convert date facets based on Lucene types into decade groupings
  88. *
  89. * @param array $facet_array array of facets
  90. * @param int $bottom_decade default is 1900
  91. * @return array associative array of facets and display info
  92. */
  93. public function luceneDateToDecade($facet_array, $bottom_decade = 1900)
  94. {
  95. // ksort($facet_array); print_r($facet_array);
  96. $bottom_year = $bottom_decade - 1; // the year before the bottom decade
  97. $decades = array();
  98. $decade_display = array();
  99. $top = date("Y"); // keep track of top most year
  100. $bottom = $bottom_year; // and the bottom most year
  101. $top_of_bottom = 0; // the top most of the bottom group
  102. foreach ( $facet_array as $year => $value)
  103. {
  104. // set a new top
  105. if ( $year > $top )
  106. {
  107. $top = $year;
  108. }
  109. // strip the end year, getting just century and decade
  110. $dec = substr($year,0,3);
  111. // if the end date in this decade is beyond the current year, then
  112. // we are in the current decade
  113. $dec_end = (int) $dec . "9";
  114. if ( $dec_end > date("Y") )
  115. {
  116. $display = $dec . "0-present";
  117. }
  118. else
  119. {
  120. // otherwise we're going DDD0-D9
  121. $display = $dec . "0-" . substr($dec,2,1) . "9";
  122. }
  123. // but the actual query is the dates themselves
  124. $query = "[" . $dec . "0 TO " . $dec . "9]";
  125. // for the old stuff, just group it together
  126. $bottom_decade = $bottom_decade - 1;
  127. if ( $year <= $bottom_year )
  128. {
  129. // set a new bottom for display purposes
  130. if ( $year < $bottom )
  131. {
  132. $bottom = $year;
  133. }
  134. // and the top of the bottom group
  135. if ( $year > $top_of_bottom )
  136. {
  137. $top_of_bottom = $year;
  138. }
  139. $query = "[-999999999 TO $bottom_year]";
  140. $display = "before-$bottom_year";
  141. }
  142. $decade = array();
  143. $decade["display"] = $display;
  144. $decade["query"] = $query;
  145. $query = $decade["query"];
  146. $decade_display[$query] = $decade["display"];
  147. if ( array_key_exists($query, $decades) )
  148. {
  149. $decades[$query] += (int) $value;
  150. }
  151. else
  152. {
  153. $decades[$query] = (int) $value;
  154. }
  155. }
  156. // now replace the 'present' and 'bottom' place holders
  157. // with actual top and bottom year values
  158. foreach ( $decade_display as $key => $value )
  159. {
  160. if ( strstr($value,"present") )
  161. {
  162. $decade_display[$key] = str_replace("present", $top, $value);
  163. }
  164. if ( strstr($value,"before") )
  165. {
  166. $decade_display[$key] = str_replace("before", $bottom, $value);
  167. }
  168. // now eliminate same year scenario
  169. $date = explode("-", $decade_display[$key]);
  170. if ( $date[0] == $date[1] )
  171. {
  172. $decade_display[$key] = $date[0];
  173. }
  174. }
  175. // sort em in date order
  176. krsort($decades);
  177. $final = array();
  178. $final["decades"] = $decades;
  179. $final["display"] = $decade_display;
  180. return $final;
  181. }
  182. }