xref: /dokuwiki/inc/Ui/SearchState.php (revision 1265b1930c74888a3474df6c6cf1e1c1c01f9c9e)
118856c5dSMichael Große<?php
218856c5dSMichael Große
318856c5dSMichael Großenamespace dokuwiki\Ui;
418856c5dSMichael Große
518856c5dSMichael Großeuse dokuwiki\Form\Form;
618856c5dSMichael Große
718856c5dSMichael Großeclass SearchState
818856c5dSMichael Große{
918856c5dSMichael Große    /**
1018856c5dSMichael Große     * @var array
1118856c5dSMichael Große     */
1218856c5dSMichael Große    protected $parsedQuery = [];
1318856c5dSMichael Große
1418856c5dSMichael Große    public function __construct(array $parsedQuery)
1518856c5dSMichael Große    {
1618856c5dSMichael Große        global $INPUT;
1718856c5dSMichael Große
1818856c5dSMichael Große        $this->parsedQuery = $parsedQuery;
19*1265b193SMichael Große        $this->parsedQuery['after'] = $INPUT->str('dta');
20*1265b193SMichael Große        $this->parsedQuery['before'] = $INPUT->str('dtb');
21*1265b193SMichael Große        $this->parsedQuery['sort'] = $INPUT->str('srt');
2218856c5dSMichael Große    }
2318856c5dSMichael Große
2418856c5dSMichael Große    /**
2518856c5dSMichael Große     * Add a link to the form which limits the search to the provided namespace
2618856c5dSMichael Große     *
2718856c5dSMichael Große     * @param Form   $searchForm
2818856c5dSMichael Große     * @param string $label
2918856c5dSMichael Große     * @param string $ns namespace to which to limit the search, empty string to remove namespace limitation
3018856c5dSMichael Große     */
3118856c5dSMichael Große    public function addSeachLinkNS(Form $searchForm, $label, $ns)
3218856c5dSMichael Große    {
3318856c5dSMichael Große        $parsedQuery = $this->parsedQuery;
3418856c5dSMichael Große        $parsedQuery['notns'] = [];
3518856c5dSMichael Große        $parsedQuery['ns'] = $ns ? [$ns] : [];
3618856c5dSMichael Große        $this->addSearchLink($searchForm, $label, $parsedQuery);
3718856c5dSMichael Große    }
3818856c5dSMichael Große
3918856c5dSMichael Große    /**
4018856c5dSMichael Große     * Add a link to the form which searches only for the provided words, but keeps the namespace and time limitations
4118856c5dSMichael Große     *
4218856c5dSMichael Große     * @param Form   $searchForm
4318856c5dSMichael Große     * @param string $label
4418856c5dSMichael Große     * @param array  $and
45df977249SMichael Große     * @param array  $not
4618856c5dSMichael Große     */
47df977249SMichael Große    public function addSearchLinkFragment(Form $searchForm, $label, array $and, array $not)
4818856c5dSMichael Große    {
4918856c5dSMichael Große        $parsedQuery = $this->parsedQuery;
5018856c5dSMichael Große        $parsedQuery['and'] = $and;
51df977249SMichael Große        $parsedQuery['not'] = $not;
5218856c5dSMichael Große        $this->addSearchLink($searchForm, $label, $parsedQuery);
5318856c5dSMichael Große    }
5418856c5dSMichael Große
5518856c5dSMichael Große    /**
5618856c5dSMichael Große     * Add a link to the form which modifies the current search's time limitations
5718856c5dSMichael Große     *
5818856c5dSMichael Große     * @param Form        $searchForm
5918856c5dSMichael Große     * @param string      $label
6018856c5dSMichael Große     * @param string      $after
6118856c5dSMichael Große     * @param null|string $before
6218856c5dSMichael Große     */
6318856c5dSMichael Große    public function addSearchLinkTime(Form $searchForm, $label, $after, $before = null)
6418856c5dSMichael Große    {
6518856c5dSMichael Große        $parsedQuery = $this->parsedQuery;
6618856c5dSMichael Große        $parsedQuery['after'] = $after;
6718856c5dSMichael Große        $parsedQuery['before'] = $before;
6818856c5dSMichael Große
6918856c5dSMichael Große        $this->addSearchLink($searchForm, $label, $parsedQuery);
7018856c5dSMichael Große    }
7118856c5dSMichael Große
728d0e286aSMichael Große    /**
738d0e286aSMichael Große     * Add a link to the form which sets the sort preference for the current search
748d0e286aSMichael Große     *
758d0e286aSMichael Große     * @param Form $searchForm
768d0e286aSMichael Große     * @param string $label
778d0e286aSMichael Große     * @param string $sort
788d0e286aSMichael Große     */
798d0e286aSMichael Große    public function addSearchLinkSort(Form $searchForm, $label, $sort)
808d0e286aSMichael Große    {
818d0e286aSMichael Große        $parsedQuery = $this->parsedQuery;
828d0e286aSMichael Große        $parsedQuery['sort'] = $sort;
838d0e286aSMichael Große
848d0e286aSMichael Große        $this->addSearchLink($searchForm, $label, $parsedQuery);
858d0e286aSMichael Große    }
868d0e286aSMichael Große
8718856c5dSMichael Große    protected function addSearchLink(
8818856c5dSMichael Große        Form $searchForm,
8918856c5dSMichael Große        $label,
9018856c5dSMichael Große        $parsedQuery
9118856c5dSMichael Große    ) {
9218856c5dSMichael Große        global $ID;
9318856c5dSMichael Große
9418856c5dSMichael Große        $newQuery = ft_queryUnparser_simple(
9518856c5dSMichael Große            $parsedQuery['and'],
9618856c5dSMichael Große            $parsedQuery['not'],
9718856c5dSMichael Große            $parsedQuery['phrases'],
9818856c5dSMichael Große            $parsedQuery['ns'],
9918856c5dSMichael Große            $parsedQuery['notns']
10018856c5dSMichael Große        );
101*1265b193SMichael Große        $hrefAttributes = ['do' => 'search', 'sf' => '1', 'q' => $newQuery];
10218856c5dSMichael Große        if ($parsedQuery['after']) {
103*1265b193SMichael Große            $hrefAttributes['dta'] = $parsedQuery['after'];
10418856c5dSMichael Große        }
10518856c5dSMichael Große        if ($parsedQuery['before']) {
106*1265b193SMichael Große            $hrefAttributes['dtb'] = $parsedQuery['before'];
10718856c5dSMichael Große        }
1088d0e286aSMichael Große        if ($parsedQuery['sort']) {
109*1265b193SMichael Große            $hrefAttributes['srt'] = $parsedQuery['sort'];
1108d0e286aSMichael Große        }
11118856c5dSMichael Große        $searchForm->addTagOpen('a')
11218856c5dSMichael Große            ->attrs([
11318856c5dSMichael Große                'href' => wl($ID, $hrefAttributes, false, '&')
11418856c5dSMichael Große            ]);
11518856c5dSMichael Große        $searchForm->addHTML($label);
11618856c5dSMichael Große        $searchForm->addTagClose('a');
11718856c5dSMichael Große    }
11818856c5dSMichael Große}
119