xref: /dokuwiki/inc/Ui/SearchState.php (revision 18856c5d101ff874df730bc3970d0b597a6ec390)
1*18856c5dSMichael Große<?php
2*18856c5dSMichael Große
3*18856c5dSMichael Großenamespace dokuwiki\Ui;
4*18856c5dSMichael Große
5*18856c5dSMichael Großeuse dokuwiki\Form\Form;
6*18856c5dSMichael Große
7*18856c5dSMichael Großeclass SearchState
8*18856c5dSMichael Große{
9*18856c5dSMichael Große    /**
10*18856c5dSMichael Große     * @var array
11*18856c5dSMichael Große     */
12*18856c5dSMichael Große    protected $parsedQuery = [];
13*18856c5dSMichael Große
14*18856c5dSMichael Große    public function __construct(array $parsedQuery)
15*18856c5dSMichael Große    {
16*18856c5dSMichael Große        global $INPUT;
17*18856c5dSMichael Große
18*18856c5dSMichael Große        $this->parsedQuery = $parsedQuery;
19*18856c5dSMichael Große        $this->parsedQuery['after'] = $INPUT->str('after');
20*18856c5dSMichael Große        $this->parsedQuery['before'] = $INPUT->str('before');
21*18856c5dSMichael Große    }
22*18856c5dSMichael Große
23*18856c5dSMichael Große    /**
24*18856c5dSMichael Große     * Add a link to the form which limits the search to the provided namespace
25*18856c5dSMichael Große     *
26*18856c5dSMichael Große     * @param Form   $searchForm
27*18856c5dSMichael Große     * @param string $label
28*18856c5dSMichael Große     * @param string $ns namespace to which to limit the search, empty string to remove namespace limitation
29*18856c5dSMichael Große     */
30*18856c5dSMichael Große    public function addSeachLinkNS(Form $searchForm, $label, $ns)
31*18856c5dSMichael Große    {
32*18856c5dSMichael Große        $parsedQuery = $this->parsedQuery;
33*18856c5dSMichael Große        $parsedQuery['notns'] = [];
34*18856c5dSMichael Große        $parsedQuery['ns'] = $ns ? [$ns] : [];
35*18856c5dSMichael Große        $this->addSearchLink($searchForm, $label, $parsedQuery);
36*18856c5dSMichael Große    }
37*18856c5dSMichael Große
38*18856c5dSMichael Große    /**
39*18856c5dSMichael Große     * Add a link to the form which searches only for the provided words, but keeps the namespace and time limitations
40*18856c5dSMichael Große     *
41*18856c5dSMichael Große     * @param Form   $searchForm
42*18856c5dSMichael Große     * @param string $label
43*18856c5dSMichael Große     * @param array  $and
44*18856c5dSMichael Große     */
45*18856c5dSMichael Große    public function addSearchLinkFragment(Form $searchForm, $label, array $and)
46*18856c5dSMichael Große    {
47*18856c5dSMichael Große        $parsedQuery = $this->parsedQuery;
48*18856c5dSMichael Große        $parsedQuery['and'] = $and;
49*18856c5dSMichael Große        $this->addSearchLink($searchForm, $label, $parsedQuery);
50*18856c5dSMichael Große    }
51*18856c5dSMichael Große
52*18856c5dSMichael Große    /**
53*18856c5dSMichael Große     * Add a link to the form which modifies the current search's time limitations
54*18856c5dSMichael Große     *
55*18856c5dSMichael Große     * @param Form        $searchForm
56*18856c5dSMichael Große     * @param string      $label
57*18856c5dSMichael Große     * @param string      $after
58*18856c5dSMichael Große     * @param null|string $before
59*18856c5dSMichael Große     */
60*18856c5dSMichael Große    public function addSearchLinkTime(Form $searchForm, $label, $after, $before = null)
61*18856c5dSMichael Große    {
62*18856c5dSMichael Große        $parsedQuery = $this->parsedQuery;
63*18856c5dSMichael Große        $parsedQuery['after'] = $after;
64*18856c5dSMichael Große        $parsedQuery['before'] = $before;
65*18856c5dSMichael Große
66*18856c5dSMichael Große        $this->addSearchLink($searchForm, $label, $parsedQuery);
67*18856c5dSMichael Große    }
68*18856c5dSMichael Große
69*18856c5dSMichael Große    protected function addSearchLink(
70*18856c5dSMichael Große        Form $searchForm,
71*18856c5dSMichael Große        $label,
72*18856c5dSMichael Große        $parsedQuery
73*18856c5dSMichael Große    ) {
74*18856c5dSMichael Große        global $ID;
75*18856c5dSMichael Große
76*18856c5dSMichael Große        $newQuery = ft_queryUnparser_simple(
77*18856c5dSMichael Große            $parsedQuery['and'],
78*18856c5dSMichael Große            $parsedQuery['not'],
79*18856c5dSMichael Große            $parsedQuery['phrases'],
80*18856c5dSMichael Große            $parsedQuery['ns'],
81*18856c5dSMichael Große            $parsedQuery['notns']
82*18856c5dSMichael Große        );
83*18856c5dSMichael Große        $hrefAttributes = ['do' => 'search', 'searchPageForm' => '1', 'q' => $newQuery];
84*18856c5dSMichael Große        if ($parsedQuery['after']) {
85*18856c5dSMichael Große            $hrefAttributes['after'] = $parsedQuery['after'];
86*18856c5dSMichael Große        }
87*18856c5dSMichael Große        if ($parsedQuery['before']) {
88*18856c5dSMichael Große            $hrefAttributes['before'] = $parsedQuery['before'];
89*18856c5dSMichael Große        }
90*18856c5dSMichael Große        $searchForm->addTagOpen('a')
91*18856c5dSMichael Große            ->attrs([
92*18856c5dSMichael Große                'href' => wl($ID, $hrefAttributes, false, '&')
93*18856c5dSMichael Große            ]);
94*18856c5dSMichael Große        $searchForm->addHTML($label);
95*18856c5dSMichael Große        $searchForm->addTagClose('a');
96*18856c5dSMichael Große    }
97*18856c5dSMichael Große}
98