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