xref: /dokuwiki/inc/Ui/SearchState.php (revision 01c23ba89c0c183d8e89de0774d31d82f40a7be9)
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        $this->parsedQuery['sort'] = $INPUT->str('sort');
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     */
46    public function addSearchLinkFragment(Form $searchForm, $label, array $and)
47    {
48        $parsedQuery = $this->parsedQuery;
49        $parsedQuery['and'] = $and;
50        $this->addSearchLink($searchForm, $label, $parsedQuery);
51    }
52
53    /**
54     * Add a link to the form which modifies the current search's time limitations
55     *
56     * @param Form        $searchForm
57     * @param string      $label
58     * @param string      $after
59     * @param null|string $before
60     */
61    public function addSearchLinkTime(Form $searchForm, $label, $after, $before = null)
62    {
63        $parsedQuery = $this->parsedQuery;
64        $parsedQuery['after'] = $after;
65        $parsedQuery['before'] = $before;
66
67        $this->addSearchLink($searchForm, $label, $parsedQuery);
68    }
69
70    /**
71     * Add a link to the form which sets the sort preference for the current search
72     *
73     * @param Form $searchForm
74     * @param string $label
75     * @param string $sort
76     */
77    public function addSearchLinkSort(Form $searchForm, $label, $sort)
78    {
79        $parsedQuery = $this->parsedQuery;
80        $parsedQuery['sort'] = $sort;
81
82        $this->addSearchLink($searchForm, $label, $parsedQuery);
83    }
84
85    protected function addSearchLink(
86        Form $searchForm,
87        $label,
88        $parsedQuery
89    ) {
90        global $ID;
91
92        $newQuery = ft_queryUnparser_simple(
93            $parsedQuery['and'],
94            $parsedQuery['not'],
95            $parsedQuery['phrases'],
96            $parsedQuery['ns'],
97            $parsedQuery['notns']
98        );
99        $hrefAttributes = ['do' => 'search', 'searchPageForm' => '1', 'q' => $newQuery];
100        if ($parsedQuery['after']) {
101            $hrefAttributes['after'] = $parsedQuery['after'];
102        }
103        if ($parsedQuery['before']) {
104            $hrefAttributes['before'] = $parsedQuery['before'];
105        }
106        if ($parsedQuery['sort']) {
107            $hrefAttributes['sort'] = $parsedQuery['sort'];
108        }
109        $searchForm->addTagOpen('a')
110            ->attrs([
111                'href' => wl($ID, $hrefAttributes, false, '&')
112            ]);
113        $searchForm->addHTML($label);
114        $searchForm->addTagClose('a');
115    }
116}
117