SearchState.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. namespace dokuwiki\Ui;
  3. class SearchState
  4. {
  5. /**
  6. * @var array
  7. */
  8. protected $parsedQuery = [];
  9. /**
  10. * SearchState constructor.
  11. *
  12. * @param array $parsedQuery
  13. */
  14. public function __construct(array $parsedQuery)
  15. {
  16. global $INPUT;
  17. $this->parsedQuery = $parsedQuery;
  18. if (!isset($parsedQuery['after'])) {
  19. $this->parsedQuery['after'] = $INPUT->str('min');
  20. }
  21. if (!isset($parsedQuery['before'])) {
  22. $this->parsedQuery['before'] = $INPUT->str('max');
  23. }
  24. if (!isset($parsedQuery['sort'])) {
  25. $this->parsedQuery['sort'] = $INPUT->str('srt');
  26. }
  27. }
  28. /**
  29. * Get a search state for the current search limited to a new namespace
  30. *
  31. * @param string $ns the namespace to which to limit the search, falsy to remove the limitation
  32. * @param array $notns
  33. *
  34. * @return SearchState
  35. */
  36. public function withNamespace($ns, array $notns = [])
  37. {
  38. $parsedQuery = $this->parsedQuery;
  39. $parsedQuery['ns'] = $ns ? [$ns] : [];
  40. $parsedQuery['notns'] = $notns;
  41. return new SearchState($parsedQuery);
  42. }
  43. /**
  44. * Get a search state for the current search with new search fragments and optionally phrases
  45. *
  46. * @param array $and
  47. * @param array $not
  48. * @param array $phrases
  49. *
  50. * @return SearchState
  51. */
  52. public function withFragments(array $and, array $not, array $phrases = [])
  53. {
  54. $parsedQuery = $this->parsedQuery;
  55. $parsedQuery['and'] = $and;
  56. $parsedQuery['not'] = $not;
  57. $parsedQuery['phrases'] = $phrases;
  58. return new SearchState($parsedQuery);
  59. }
  60. /**
  61. * Get a search state for the current search with with adjusted time limitations
  62. *
  63. * @param $after
  64. * @param $before
  65. *
  66. * @return SearchState
  67. */
  68. public function withTimeLimitations($after, $before)
  69. {
  70. $parsedQuery = $this->parsedQuery;
  71. $parsedQuery['after'] = $after;
  72. $parsedQuery['before'] = $before;
  73. return new SearchState($parsedQuery);
  74. }
  75. /**
  76. * Get a search state for the current search with adjusted sort preference
  77. *
  78. * @param $sort
  79. *
  80. * @return SearchState
  81. */
  82. public function withSorting($sort)
  83. {
  84. $parsedQuery = $this->parsedQuery;
  85. $parsedQuery['sort'] = $sort;
  86. return new SearchState($parsedQuery);
  87. }
  88. /**
  89. * Get a link that represents the current search state
  90. *
  91. * Note that this represents only a simplified version of the search state.
  92. * Grouping with braces and "OR" conditions are not supported.
  93. *
  94. * @param $label
  95. *
  96. * @return string
  97. */
  98. public function getSearchLink($label)
  99. {
  100. global $ID, $conf;
  101. $parsedQuery = $this->parsedQuery;
  102. $tagAttributes = [
  103. 'target' => $conf['target']['wiki'],
  104. ];
  105. $newQuery = ft_queryUnparser_simple(
  106. $parsedQuery['and'],
  107. $parsedQuery['not'],
  108. $parsedQuery['phrases'],
  109. $parsedQuery['ns'],
  110. $parsedQuery['notns']
  111. );
  112. $hrefAttributes = ['do' => 'search', 'sf' => '1', 'q' => $newQuery];
  113. if ($parsedQuery['after']) {
  114. $hrefAttributes['min'] = $parsedQuery['after'];
  115. }
  116. if ($parsedQuery['before']) {
  117. $hrefAttributes['max'] = $parsedQuery['before'];
  118. }
  119. if ($parsedQuery['sort']) {
  120. $hrefAttributes['srt'] = $parsedQuery['sort'];
  121. }
  122. $href = wl($ID, $hrefAttributes, false, '&');
  123. return "<a href='$href' " . buildAttributes($tagAttributes, true) . ">$label</a>";
  124. }
  125. }