xref: /dokuwiki/inc/Ui/SearchState.php (revision 8d0e286a89e3ad07963562359bfa5f9e974fd1fb)
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;
1918856c5dSMichael Große        $this->parsedQuery['after'] = $INPUT->str('after');
2018856c5dSMichael Große        $this->parsedQuery['before'] = $INPUT->str('before');
21*8d0e286aSMichael Große        $this->parsedQuery['sort'] = $INPUT->str('sort');
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
4518856c5dSMichael Große     */
4618856c5dSMichael Große    public function addSearchLinkFragment(Form $searchForm, $label, array $and)
4718856c5dSMichael Große    {
4818856c5dSMichael Große        $parsedQuery = $this->parsedQuery;
4918856c5dSMichael Große        $parsedQuery['and'] = $and;
5018856c5dSMichael Große        $this->addSearchLink($searchForm, $label, $parsedQuery);
5118856c5dSMichael Große    }
5218856c5dSMichael Große
5318856c5dSMichael Große    /**
5418856c5dSMichael Große     * Add a link to the form which modifies the current search's time limitations
5518856c5dSMichael Große     *
5618856c5dSMichael Große     * @param Form        $searchForm
5718856c5dSMichael Große     * @param string      $label
5818856c5dSMichael Große     * @param string      $after
5918856c5dSMichael Große     * @param null|string $before
6018856c5dSMichael Große     */
6118856c5dSMichael Große    public function addSearchLinkTime(Form $searchForm, $label, $after, $before = null)
6218856c5dSMichael Große    {
6318856c5dSMichael Große        $parsedQuery = $this->parsedQuery;
6418856c5dSMichael Große        $parsedQuery['after'] = $after;
6518856c5dSMichael Große        $parsedQuery['before'] = $before;
6618856c5dSMichael Große
6718856c5dSMichael Große        $this->addSearchLink($searchForm, $label, $parsedQuery);
6818856c5dSMichael Große    }
6918856c5dSMichael Große
70*8d0e286aSMichael Große    /**
71*8d0e286aSMichael Große     * Add a link to the form which sets the sort preference for the current search
72*8d0e286aSMichael Große     *
73*8d0e286aSMichael Große     * @param Form $searchForm
74*8d0e286aSMichael Große     * @param string $label
75*8d0e286aSMichael Große     * @param string $sort
76*8d0e286aSMichael Große     */
77*8d0e286aSMichael Große    public function addSearchLinkSort(Form $searchForm, $label, $sort)
78*8d0e286aSMichael Große    {
79*8d0e286aSMichael Große        $parsedQuery = $this->parsedQuery;
80*8d0e286aSMichael Große        $parsedQuery['sort'] = $sort;
81*8d0e286aSMichael Große
82*8d0e286aSMichael Große        $this->addSearchLink($searchForm, $label, $parsedQuery);
83*8d0e286aSMichael Große    }
84*8d0e286aSMichael Große
8518856c5dSMichael Große    protected function addSearchLink(
8618856c5dSMichael Große        Form $searchForm,
8718856c5dSMichael Große        $label,
8818856c5dSMichael Große        $parsedQuery
8918856c5dSMichael Große    ) {
9018856c5dSMichael Große        global $ID;
9118856c5dSMichael Große
9218856c5dSMichael Große        $newQuery = ft_queryUnparser_simple(
9318856c5dSMichael Große            $parsedQuery['and'],
9418856c5dSMichael Große            $parsedQuery['not'],
9518856c5dSMichael Große            $parsedQuery['phrases'],
9618856c5dSMichael Große            $parsedQuery['ns'],
9718856c5dSMichael Große            $parsedQuery['notns']
9818856c5dSMichael Große        );
9918856c5dSMichael Große        $hrefAttributes = ['do' => 'search', 'searchPageForm' => '1', 'q' => $newQuery];
10018856c5dSMichael Große        if ($parsedQuery['after']) {
10118856c5dSMichael Große            $hrefAttributes['after'] = $parsedQuery['after'];
10218856c5dSMichael Große        }
10318856c5dSMichael Große        if ($parsedQuery['before']) {
10418856c5dSMichael Große            $hrefAttributes['before'] = $parsedQuery['before'];
10518856c5dSMichael Große        }
106*8d0e286aSMichael Große        if ($parsedQuery['sort']) {
107*8d0e286aSMichael Große            $hrefAttributes['sort'] = $parsedQuery['sort'];
108*8d0e286aSMichael Große        }
10918856c5dSMichael Große        $searchForm->addTagOpen('a')
11018856c5dSMichael Große            ->attrs([
11118856c5dSMichael Große                'href' => wl($ID, $hrefAttributes, false, '&')
11218856c5dSMichael Große            ]);
11318856c5dSMichael Große        $searchForm->addHTML($label);
11418856c5dSMichael Große        $searchForm->addTagClose('a');
11518856c5dSMichael Große    }
11618856c5dSMichael Große}
117