1<?php 2 3namespace dokuwiki\Ui; 4 5use dokuwiki\Form\Form; 6 7class SearchState 8{ 9 /** 10 * @var array 11 */ 12 protected $parsedQuery = []; 13 14 public function __construct(array $parsedQuery) 15 { 16 global $INPUT; 17 18 $this->parsedQuery = $parsedQuery; 19 $this->parsedQuery['after'] = $INPUT->str('after'); 20 $this->parsedQuery['before'] = $INPUT->str('before'); 21 } 22 23 /** 24 * Add a link to the form which limits the search to the provided namespace 25 * 26 * @param Form $searchForm 27 * @param string $label 28 * @param string $ns namespace to which to limit the search, empty string to remove namespace limitation 29 */ 30 public function addSeachLinkNS(Form $searchForm, $label, $ns) 31 { 32 $parsedQuery = $this->parsedQuery; 33 $parsedQuery['notns'] = []; 34 $parsedQuery['ns'] = $ns ? [$ns] : []; 35 $this->addSearchLink($searchForm, $label, $parsedQuery); 36 } 37 38 /** 39 * Add a link to the form which searches only for the provided words, but keeps the namespace and time limitations 40 * 41 * @param Form $searchForm 42 * @param string $label 43 * @param array $and 44 */ 45 public function addSearchLinkFragment(Form $searchForm, $label, array $and) 46 { 47 $parsedQuery = $this->parsedQuery; 48 $parsedQuery['and'] = $and; 49 $this->addSearchLink($searchForm, $label, $parsedQuery); 50 } 51 52 /** 53 * Add a link to the form which modifies the current search's time limitations 54 * 55 * @param Form $searchForm 56 * @param string $label 57 * @param string $after 58 * @param null|string $before 59 */ 60 public function addSearchLinkTime(Form $searchForm, $label, $after, $before = null) 61 { 62 $parsedQuery = $this->parsedQuery; 63 $parsedQuery['after'] = $after; 64 $parsedQuery['before'] = $before; 65 66 $this->addSearchLink($searchForm, $label, $parsedQuery); 67 } 68 69 protected function addSearchLink( 70 Form $searchForm, 71 $label, 72 $parsedQuery 73 ) { 74 global $ID; 75 76 $newQuery = ft_queryUnparser_simple( 77 $parsedQuery['and'], 78 $parsedQuery['not'], 79 $parsedQuery['phrases'], 80 $parsedQuery['ns'], 81 $parsedQuery['notns'] 82 ); 83 $hrefAttributes = ['do' => 'search', 'searchPageForm' => '1', 'q' => $newQuery]; 84 if ($parsedQuery['after']) { 85 $hrefAttributes['after'] = $parsedQuery['after']; 86 } 87 if ($parsedQuery['before']) { 88 $hrefAttributes['before'] = $parsedQuery['before']; 89 } 90 $searchForm->addTagOpen('a') 91 ->attrs([ 92 'href' => wl($ID, $hrefAttributes, false, '&') 93 ]); 94 $searchForm->addHTML($label); 95 $searchForm->addTagClose('a'); 96 } 97} 98