xref: /dokuwiki/inc/Ui/SearchState.php (revision 52d4cd42c6b38524d18e8ad46a664bff15c67d5d)
118856c5dSMichael Große<?php
218856c5dSMichael Große
318856c5dSMichael Großenamespace dokuwiki\Ui;
418856c5dSMichael Große
518856c5dSMichael Großeclass SearchState
618856c5dSMichael Große{
718856c5dSMichael Große    /**
818856c5dSMichael Große     * @var array
918856c5dSMichael Große     */
1018856c5dSMichael Große    protected $parsedQuery = [];
1118856c5dSMichael Große
12*52d4cd42SMichael Große    /**
13*52d4cd42SMichael Große     * SearchState constructor.
14*52d4cd42SMichael Große     *
15*52d4cd42SMichael Große     * @param array $parsedQuery
16*52d4cd42SMichael Große     */
1718856c5dSMichael Große    public function __construct(array $parsedQuery)
1818856c5dSMichael Große    {
1918856c5dSMichael Große        global $INPUT;
2018856c5dSMichael Große
2118856c5dSMichael Große        $this->parsedQuery = $parsedQuery;
22*52d4cd42SMichael Große        if (!isset($parsedQuery['after'])) {
231265b193SMichael Große            $this->parsedQuery['after'] = $INPUT->str('dta');
24*52d4cd42SMichael Große        }
25*52d4cd42SMichael Große        if (!isset($parsedQuery['before'])) {
261265b193SMichael Große            $this->parsedQuery['before'] = $INPUT->str('dtb');
27*52d4cd42SMichael Große        }
28*52d4cd42SMichael Große        if (!isset($parsedQuery['sort'])) {
291265b193SMichael Große            $this->parsedQuery['sort'] = $INPUT->str('srt');
3018856c5dSMichael Große        }
3118856c5dSMichael Große    }
3218856c5dSMichael Große
3318856c5dSMichael Große    /**
34*52d4cd42SMichael Große     * Get a search state for the current search limited to a new namespace
3518856c5dSMichael Große     *
36*52d4cd42SMichael Große     * @param string $ns the namespace to which to limit the search, falsy to remove the limitation
37*52d4cd42SMichael Große     * @param array  $notns
38*52d4cd42SMichael Große     *
39*52d4cd42SMichael Große     * @return SearchState
40*52d4cd42SMichael Große     */
41*52d4cd42SMichael Große    public function withNamespace($ns, array $notns = [])
42*52d4cd42SMichael Große    {
43*52d4cd42SMichael Große        $parsedQuery = $this->parsedQuery;
44*52d4cd42SMichael Große        $parsedQuery['ns'] = $ns ? [$ns] : [];
45*52d4cd42SMichael Große        $parsedQuery['notns'] = $notns;
46*52d4cd42SMichael Große
47*52d4cd42SMichael Große        return new SearchState($parsedQuery);
48*52d4cd42SMichael Große    }
49*52d4cd42SMichael Große
50*52d4cd42SMichael Große    /**
51*52d4cd42SMichael Große     * Get a search state for the current search with new search fragments and optionally phrases
52*52d4cd42SMichael Große     *
5318856c5dSMichael Große     * @param array $and
54df977249SMichael Große     * @param array $not
55*52d4cd42SMichael Große     * @param array $phrases
56*52d4cd42SMichael Große     *
57*52d4cd42SMichael Große     * @return SearchState
5818856c5dSMichael Große     */
59*52d4cd42SMichael Große    public function withFragments(array $and, array $not, array $phrases = [])
6018856c5dSMichael Große    {
6118856c5dSMichael Große        $parsedQuery = $this->parsedQuery;
6218856c5dSMichael Große        $parsedQuery['and'] = $and;
63df977249SMichael Große        $parsedQuery['not'] = $not;
64*52d4cd42SMichael Große        $parsedQuery['phrases'] = $phrases;
65*52d4cd42SMichael Große
66*52d4cd42SMichael Große        return new SearchState($parsedQuery);
6718856c5dSMichael Große    }
6818856c5dSMichael Große
6918856c5dSMichael Große    /**
70*52d4cd42SMichael Große     * Get a search state for the current search with with adjusted time limitations
7118856c5dSMichael Große     *
72*52d4cd42SMichael Große     * @param $after
73*52d4cd42SMichael Große     * @param $before
74*52d4cd42SMichael Große     *
75*52d4cd42SMichael Große     * @return SearchState
7618856c5dSMichael Große     */
77*52d4cd42SMichael Große    public function withTimeLimitations($after, $before)
7818856c5dSMichael Große    {
7918856c5dSMichael Große        $parsedQuery = $this->parsedQuery;
8018856c5dSMichael Große        $parsedQuery['after'] = $after;
8118856c5dSMichael Große        $parsedQuery['before'] = $before;
8218856c5dSMichael Große
83*52d4cd42SMichael Große        return new SearchState($parsedQuery);
8418856c5dSMichael Große    }
8518856c5dSMichael Große
868d0e286aSMichael Große    /**
87*52d4cd42SMichael Große     * Get a search state for the current search with adjusted sort preference
888d0e286aSMichael Große     *
89*52d4cd42SMichael Große     * @param $sort
90*52d4cd42SMichael Große     *
91*52d4cd42SMichael Große     * @return SearchState
928d0e286aSMichael Große     */
93*52d4cd42SMichael Große    public function withSorting($sort)
948d0e286aSMichael Große    {
958d0e286aSMichael Große        $parsedQuery = $this->parsedQuery;
968d0e286aSMichael Große        $parsedQuery['sort'] = $sort;
978d0e286aSMichael Große
98*52d4cd42SMichael Große        return new SearchState($parsedQuery);
998d0e286aSMichael Große    }
1008d0e286aSMichael Große
101*52d4cd42SMichael Große    /**
102*52d4cd42SMichael Große     * Get a link that represents the current search state
103*52d4cd42SMichael Große     *
104*52d4cd42SMichael Große     * Note that this represents only a simplified version of the search state.
105*52d4cd42SMichael Große     * Grouping with braces and "OR" conditions are not supported.
106*52d4cd42SMichael Große     *
107*52d4cd42SMichael Große     * @param $label
108*52d4cd42SMichael Große     *
109*52d4cd42SMichael Große     * @return string
110*52d4cd42SMichael Große     */
111*52d4cd42SMichael Große    public function getSearchLink($label)
112*52d4cd42SMichael Große    {
113*52d4cd42SMichael Große        global $ID, $conf;
114*52d4cd42SMichael Große        $parsedQuery = $this->parsedQuery;
115*52d4cd42SMichael Große
116*52d4cd42SMichael Große        $tagAttributes = [
117*52d4cd42SMichael Große            'target' => $conf['target']['wiki'],
118*52d4cd42SMichael Große        ];
11918856c5dSMichael Große
12018856c5dSMichael Große        $newQuery = ft_queryUnparser_simple(
12118856c5dSMichael Große            $parsedQuery['and'],
12218856c5dSMichael Große            $parsedQuery['not'],
12318856c5dSMichael Große            $parsedQuery['phrases'],
12418856c5dSMichael Große            $parsedQuery['ns'],
12518856c5dSMichael Große            $parsedQuery['notns']
12618856c5dSMichael Große        );
1271265b193SMichael Große        $hrefAttributes = ['do' => 'search', 'sf' => '1', 'q' => $newQuery];
12818856c5dSMichael Große        if ($parsedQuery['after']) {
1291265b193SMichael Große            $hrefAttributes['dta'] = $parsedQuery['after'];
13018856c5dSMichael Große        }
13118856c5dSMichael Große        if ($parsedQuery['before']) {
1321265b193SMichael Große            $hrefAttributes['dtb'] = $parsedQuery['before'];
13318856c5dSMichael Große        }
1348d0e286aSMichael Große        if ($parsedQuery['sort']) {
1351265b193SMichael Große            $hrefAttributes['srt'] = $parsedQuery['sort'];
1368d0e286aSMichael Große        }
137*52d4cd42SMichael Große
138*52d4cd42SMichael Große        $href = wl($ID, $hrefAttributes, false, '&');
139*52d4cd42SMichael Große        return "<a href='$href' " . buildAttributes($tagAttributes) . ">$label</a>";
14018856c5dSMichael Große    }
14118856c5dSMichael Große}
142