xref: /dokuwiki/inc/Ui/SearchState.php (revision 2ce8affc66a5825b8eb9f401e287a2315987d62a)
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('dta');
20        $this->parsedQuery['before'] = $INPUT->str('dtb');
21        $this->parsedQuery['sort'] = $INPUT->str('srt');
22    }
23
24    /**
25     * Add a link to the form which limits the search to the provided namespace
26     *
27     * @param Form   $searchForm
28     * @param string $label
29     * @param string $ns namespace to which to limit the search, empty string to remove namespace limitation
30     */
31    public function addSeachLinkNS(Form $searchForm, $label, $ns)
32    {
33        $parsedQuery = $this->parsedQuery;
34        $parsedQuery['notns'] = [];
35        $parsedQuery['ns'] = $ns ? [$ns] : [];
36        $this->addSearchLink($searchForm, $label, $parsedQuery);
37    }
38
39    /**
40     * Add a link to the form which searches only for the provided words, but keeps the namespace and time limitations
41     *
42     * @param Form   $searchForm
43     * @param string $label
44     * @param array  $and
45     * @param array  $not
46     */
47    public function addSearchLinkFragment(Form $searchForm, $label, array $and, array $not)
48    {
49        $parsedQuery = $this->parsedQuery;
50        $parsedQuery['and'] = $and;
51        $parsedQuery['not'] = $not;
52        $this->addSearchLink($searchForm, $label, $parsedQuery);
53    }
54
55    /**
56     * Add a link to the form which modifies the current search's time limitations
57     *
58     * @param Form        $searchForm
59     * @param string      $label
60     * @param string      $after
61     * @param null|string $before
62     */
63    public function addSearchLinkTime(Form $searchForm, $label, $after, $before = null)
64    {
65        $parsedQuery = $this->parsedQuery;
66        $parsedQuery['after'] = $after;
67        $parsedQuery['before'] = $before;
68
69        $this->addSearchLink($searchForm, $label, $parsedQuery);
70    }
71
72    /**
73     * Add a link to the form which sets the sort preference for the current search
74     *
75     * @param Form $searchForm
76     * @param string $label
77     * @param string $sort
78     */
79    public function addSearchLinkSort(Form $searchForm, $label, $sort)
80    {
81        $parsedQuery = $this->parsedQuery;
82        $parsedQuery['sort'] = $sort;
83
84        $this->addSearchLink($searchForm, $label, $parsedQuery);
85    }
86
87    protected function addSearchLink(
88        Form $searchForm,
89        $label,
90        $parsedQuery
91    ) {
92        global $ID;
93
94        $newQuery = ft_queryUnparser_simple(
95            $parsedQuery['and'],
96            $parsedQuery['not'],
97            $parsedQuery['phrases'],
98            $parsedQuery['ns'],
99            $parsedQuery['notns']
100        );
101        $hrefAttributes = ['do' => 'search', 'sf' => '1', 'q' => $newQuery];
102        if ($parsedQuery['after']) {
103            $hrefAttributes['dta'] = $parsedQuery['after'];
104        }
105        if ($parsedQuery['before']) {
106            $hrefAttributes['dtb'] = $parsedQuery['before'];
107        }
108        if ($parsedQuery['sort']) {
109            $hrefAttributes['srt'] = $parsedQuery['sort'];
110        }
111        $searchForm->addTagOpen('a')
112            ->attrs([
113                'href' => wl($ID, $hrefAttributes, false, '&')
114            ]);
115        $searchForm->addHTML($label);
116        $searchForm->addTagClose('a');
117    }
118}
119