TypeAhead.php 3.42 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
<?php
/**
 * Created by PhpStorm.
 * User: crose
 * Date: 3/13/17
 * Time: 9:29 PM
 */

namespace qfq;

11
//use TYPO3\CMS\Core\FormProtection\Exception;
12

13
14
15
require_once(__DIR__ . '/../store/Sip.php');
require_once(__DIR__ . '/../store/Session.php');
require_once(__DIR__ . '/../Constants.php');
16
require_once(__DIR__ . '/../helper/Ldap.php');
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
require_once(__DIR__ . '/../Database.php');


class TypeAhead {

    /**
     * @var Database instantiated class
     */
    protected $db = null;

    /**
     * @var array
     */
    protected $vars = array();

    /**
     *
     */
    public function __construct($phpUnit = false) {

37
38
        $this->vars[TYPEAHEAD_API_QUERY] = isset($_GET[TYPEAHEAD_API_QUERY]) ? $_GET[TYPEAHEAD_API_QUERY] : '';
        $this->vars[TYPEAHEAD_API_PREFETCH] = isset($_GET[TYPEAHEAD_API_PREFETCH]) ? $_GET[TYPEAHEAD_API_PREFETCH] : '';
39
40
41
42
43
44
45
46
47
        $this->vars[TYPEAHEAD_API_SIP] = isset($_GET[TYPEAHEAD_API_SIP]) ? $_GET[TYPEAHEAD_API_SIP] : '';

        if ($this->vars[TYPEAHEAD_API_SIP] == '') {
            throw new CodeException('Missing GET parameter "' . TYPEAHEAD_API_SIP);
        }

        if ($this->vars[TYPEAHEAD_API_QUERY] == '' && $this->vars[TYPEAHEAD_API_PREFETCH] == '') {
            throw new CodeException('Missing GET parameter "' . TYPEAHEAD_API_QUERY . '" or "' . TYPEAHEAD_API_PREFETCH . '"');
        }
48
49
50
51
52
53
54
55
56
57
58
59
60
61

        $session = Session::getInstance($phpUnit);

        $this->db = new Database();

    }

    /**
     * @return array|int
     * @throws CodeException
     * @throws DbException
     * @throws UserFormException
     */
    public function process() {
62

63
64
65
66
67
        $arr = array();
        $values = array();

        $sipClass = new Sip();

68
        $sipVars = $sipClass->getVarsFromSip($this->vars[TYPEAHEAD_API_SIP]);
69

70
        if (isset($sipVars[FE_TYPEAHEAD_SQL])) {
Carsten  Rose's avatar
Carsten Rose committed
71
            $arr = $this->typeAheadSql($sipVars, $this->vars[TYPEAHEAD_API_QUERY]);
72
        } elseif (isset($sipVars[FE_LDAP_SERVER])) {
73
            $ldap = new Ldap();
74
75
            $mode = ($this->vars[TYPEAHEAD_API_PREFETCH] == '') ? MODE_LDAP_MULTI : MODE_LDAP_PREFETCH;
            $arr = $ldap->process($sipVars, $this->vars[TYPEAHEAD_API_QUERY], $mode);
76
77
        }

78
79
80
81
        return $arr;
    }

    /**
82
83
84
85
86
     * Do a wildcard serach on the prepared statement $config[FE_TYPEAHEAD_SQL].
     * All '?' will be replaced by '%$value%'.
     * If there is no 'LIMIT x' defined, append it.
     * Returns an dict array [ API_TYPEAHEAD_KEY => key, API_TYPEAHEAD_VALUE => value ]
     *
87
     * @param array $config
88
89
     * @param string $value
     * @return array
90
91
     * @throws CodeException
     * @throws DbException
92
     * @throws UserFormException
93
     */
94
    private function typeAheadSql(array $config, $value) {
95
96
        $values = array();

97
98
99
100
101
102
103
104
        $sql = $config[FE_TYPEAHEAD_SQL];
        $value = '%' . $value . '%';
        $cnt = substr_count($sql, '?');

        if ($cnt == 0) {
            throw new UserFormException("Missing at least one '?' in " . FE_TYPEAHEAD_SQL);
        }

105
        for ($ii = 0; $ii < $cnt; $ii++) {
106
            $values[] = $value;
107
108
        }

109
110
111
112
113
114
115
116
117
        if (!$this->db->hasLimit($sql)) {
            $sql .= ' LIMIT ' . $config[FE_TYPEAHEAD_LIMIT];
        }

        $arr = $this->db->sql($sql, ROW_REGULAR, $values);
        if ($arr == false || count($arr) == 0) {
            return array();
        }

118
        return $this->db->makeArrayDict($arr, TYPEAHEAD_SQL_KEY_NAME, API_TYPEAHEAD_VALUE, API_TYPEAHEAD_KEY, API_TYPEAHEAD_VALUE);
119
120
    }

121

122
}