xref: /dokuwiki/inc/Ui/SearchState.php (revision 5d021066fe401ca182d989f7f9d0353a1bf74bb6)
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
1252d4cd42SMichael Große    /**
1352d4cd42SMichael Große     * SearchState constructor.
1452d4cd42SMichael Große     *
1552d4cd42SMichael Große     * @param array $parsedQuery
1652d4cd42SMichael 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;
2252d4cd42SMichael Große        if (!isset($parsedQuery['after'])) {
23422bbbc6SMichael Große            $this->parsedQuery['after'] = $INPUT->str('min');
2452d4cd42SMichael Große        }
2552d4cd42SMichael Große        if (!isset($parsedQuery['before'])) {
26422bbbc6SMichael Große            $this->parsedQuery['before'] = $INPUT->str('max');
2752d4cd42SMichael Große        }
2852d4cd42SMichael 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    /**
3452d4cd42SMichael Große     * Get a search state for the current search limited to a new namespace
3518856c5dSMichael Große     *
3652d4cd42SMichael Große     * @param string $ns the namespace to which to limit the search, falsy to remove the limitation
3752d4cd42SMichael Große     * @param array  $notns
3852d4cd42SMichael Große     *
3952d4cd42SMichael Große     * @return SearchState
4052d4cd42SMichael Große     */
4152d4cd42SMichael Große    public function withNamespace($ns, array $notns = [])
4252d4cd42SMichael Große    {
4352d4cd42SMichael Große        $parsedQuery = $this->parsedQuery;
4452d4cd42SMichael Große        $parsedQuery['ns'] = $ns ? [$ns] : [];
4552d4cd42SMichael Große        $parsedQuery['notns'] = $notns;
4652d4cd42SMichael Große
4752d4cd42SMichael Große        return new SearchState($parsedQuery);
4852d4cd42SMichael Große    }
4952d4cd42SMichael Große
5052d4cd42SMichael Große    /**
5152d4cd42SMichael Große     * Get a search state for the current search with new search fragments and optionally phrases
5252d4cd42SMichael Große     *
5318856c5dSMichael Große     * @param array $and
54df977249SMichael Große     * @param array $not
5552d4cd42SMichael Große     * @param array $phrases
5652d4cd42SMichael Große     *
5752d4cd42SMichael Große     * @return SearchState
5818856c5dSMichael Große     */
5952d4cd42SMichael 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;
6452d4cd42SMichael Große        $parsedQuery['phrases'] = $phrases;
6552d4cd42SMichael Große
6652d4cd42SMichael Große        return new SearchState($parsedQuery);
6718856c5dSMichael Große    }
6818856c5dSMichael Große
6918856c5dSMichael Große    /**
7052d4cd42SMichael Große     * Get a search state for the current search with with adjusted time limitations
7118856c5dSMichael Große     *
7252d4cd42SMichael Große     * @param $after
7352d4cd42SMichael Große     * @param $before
7452d4cd42SMichael Große     *
7552d4cd42SMichael Große     * @return SearchState
7618856c5dSMichael Große     */
7752d4cd42SMichael 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
8352d4cd42SMichael Große        return new SearchState($parsedQuery);
8418856c5dSMichael Große    }
8518856c5dSMichael Große
868d0e286aSMichael Große    /**
8752d4cd42SMichael Große     * Get a search state for the current search with adjusted sort preference
888d0e286aSMichael Große     *
8952d4cd42SMichael Große     * @param $sort
9052d4cd42SMichael Große     *
9152d4cd42SMichael Große     * @return SearchState
928d0e286aSMichael Große     */
9352d4cd42SMichael Große    public function withSorting($sort)
948d0e286aSMichael Große    {
958d0e286aSMichael Große        $parsedQuery = $this->parsedQuery;
968d0e286aSMichael Große        $parsedQuery['sort'] = $sort;
978d0e286aSMichael Große
9852d4cd42SMichael Große        return new SearchState($parsedQuery);
998d0e286aSMichael Große    }
1008d0e286aSMichael Große
10152d4cd42SMichael Große    /**
10252d4cd42SMichael Große     * Get a link that represents the current search state
10352d4cd42SMichael Große     *
10452d4cd42SMichael Große     * Note that this represents only a simplified version of the search state.
10552d4cd42SMichael Große     * Grouping with braces and "OR" conditions are not supported.
10652d4cd42SMichael Große     *
10752d4cd42SMichael Große     * @param $label
10852d4cd42SMichael Große     *
10952d4cd42SMichael Große     * @return string
11052d4cd42SMichael Große     */
11152d4cd42SMichael Große    public function getSearchLink($label)
11252d4cd42SMichael Große    {
11352d4cd42SMichael Große        global $ID, $conf;
11452d4cd42SMichael Große        $parsedQuery = $this->parsedQuery;
11552d4cd42SMichael Große
11652d4cd42SMichael Große        $tagAttributes = [
11752d4cd42SMichael Große            'target' => $conf['target']['wiki'],
11852d4cd42SMichael 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']) {
129422bbbc6SMichael Große            $hrefAttributes['min'] = $parsedQuery['after'];
13018856c5dSMichael Große        }
13118856c5dSMichael Große        if ($parsedQuery['before']) {
132422bbbc6SMichael Große            $hrefAttributes['max'] = $parsedQuery['before'];
13318856c5dSMichael Große        }
1348d0e286aSMichael Große        if ($parsedQuery['sort']) {
1351265b193SMichael Große            $hrefAttributes['srt'] = $parsedQuery['sort'];
1368d0e286aSMichael Große        }
13752d4cd42SMichael Große
13852d4cd42SMichael Große        $href = wl($ID, $hrefAttributes, false, '&');
139*5d021066SAnika Henke        return "<a href='$href' " . buildAttributes($tagAttributes, true) . ">$label</a>";
14018856c5dSMichael Große    }
14118856c5dSMichael Große}
142