xref: /plugin/struct/helper/config.php (revision 2987727967455b4335cb8dba900196317113c849)
1*29877279SMichael Große<?php
2*29877279SMichael Große/**
3*29877279SMichael Große * DokuWiki Plugin struct (Helper Component)
4*29877279SMichael Große *
5*29877279SMichael Große * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6*29877279SMichael Große * @author  Andreas Gohr, Michael Große <dokuwiki@cosmocode.de>
7*29877279SMichael Große */
8*29877279SMichael Große
9*29877279SMichael Große// must be run within Dokuwiki
10*29877279SMichael Großeif(!defined('DOKU_INC')) die();
11*29877279SMichael Große
12*29877279SMichael Großeclass helper_plugin_struct_config extends DokuWiki_Plugin {
13*29877279SMichael Große
14*29877279SMichael Große    /**
15*29877279SMichael Große     * @param string $val
16*29877279SMichael Große     *
17*29877279SMichael Große     * @return array
18*29877279SMichael Große     */
19*29877279SMichael Große    public function parseSort($val) {
20*29877279SMichael Große        if(substr($val, 0, 1) == '^') {
21*29877279SMichael Große            return array(substr($val, 1), 'DESC',);
22*29877279SMichael Große        }
23*29877279SMichael Große        return array($val, 'ASC',);
24*29877279SMichael Große    }
25*29877279SMichael Große
26*29877279SMichael Große    /**
27*29877279SMichael Große     * @param $logic
28*29877279SMichael Große     * @param $val
29*29877279SMichael Große     *
30*29877279SMichael Große     * @return array|bool
31*29877279SMichael Große     */
32*29877279SMichael Große    public function parseFilterLine($logic, $val) {
33*29877279SMichael Große        $flt = $this->parseFilter($val);
34*29877279SMichael Große        if($flt) {
35*29877279SMichael Große            $flt[] = $logic;
36*29877279SMichael Große            return $flt;
37*29877279SMichael Große        }
38*29877279SMichael Große        return false;
39*29877279SMichael Große    }
40*29877279SMichael Große
41*29877279SMichael Große    /**
42*29877279SMichael Große     * Parse a filter
43*29877279SMichael Große     *
44*29877279SMichael Große     * @param string $val
45*29877279SMichael Große     *
46*29877279SMichael Große     * @return array ($col, $comp, $value)
47*29877279SMichael Große     * @throws plugin\struct\meta\StructException
48*29877279SMichael Große     */
49*29877279SMichael Große    protected function parseFilter($val) {
50*29877279SMichael Große
51*29877279SMichael Große        $comps = plugin\struct\meta\Search::$COMPARATORS;
52*29877279SMichael Große        $comps = array_map('preg_quote_cb', $comps);
53*29877279SMichael Große        $comps = join('|', $comps);
54*29877279SMichael Große
55*29877279SMichael Große        if(!preg_match('/^(.*?)('.$comps.')(.*)$/', $val, $match)) {
56*29877279SMichael Große            throw new plugin\struct\meta\StructException('Invalid search filter %s', hsc($val));
57*29877279SMichael Große        }
58*29877279SMichael Große        array_shift($match); // we don't need the zeroth match
59*29877279SMichael Große        $match[0] = trim($match[0]);
60*29877279SMichael Große        $match[2] = trim($match[2]);
61*29877279SMichael Große        return $match;
62*29877279SMichael Große    }
63*29877279SMichael Große}
64*29877279SMichael Große
65*29877279SMichael Große// vim:ts=4:sw=4:et:
66