forked from craigk5n/webcalendar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
autocomplete_ajax.php
129 lines (112 loc) · 3.71 KB
/
autocomplete_ajax.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php
/**
* Description
* Handler for AJAX requests for search suggestion (aka autocomplete)
* Note that we only search the current user's events for all time
* (no date range, category, etc.). This this is a simple search
* (not advanced).
*
* Autocomplete URL: https://github.com/xcash/bootstrap-autocomplete
* Autocomplete Doc URL: https://bootstrap-autocomplete.readthedocs.io/en/latest/
*
* Must return data in the following format:
* {
* "error":0,
* "status":"OK",
* "message":"",
* "matches":["Unnamed Event","TEst","test event \"2\" !","test recur","test recur OVERRIDE","Test Event"]
* }
*/
require_once 'includes/translate.php';
require_once 'includes/classes/WebCalendar.php';
$WebCalendar = new WebCalendar( __FILE__ );
require_once 'includes/config.php';
require_once 'includes/dbi4php.php';
require_once 'includes/formvars.php';
require_once 'includes/functions.php';
$WebCalendar->initializeFirstPhase();
require_once "includes/$user_inc";
require_once 'includes/access.php';
require_once 'includes/ajax.php';
require_once 'includes/validate.php';
$WebCalendar->initializeSecondPhase();
load_global_settings();
load_user_preferences();
$WebCalendar->setLanguage();
load_user_layers();
$action = getValue('action');
if (empty($action))
$action = 'search';
$query = getValue('q');
if (empty($query))
$query = getValue('query');
$sendPlainText = false;
$format = getValue('format');
if (
!empty($format) &&
($format == 'text' || $format == 'plain')
);
$sendPlainText = true;
if ($sendPlainText) {
Header('Content-Type: text/plain');
} else {
Header('Content-Type: text/json');
}
$error = '';
$matches = 0;
if ($action == 'search') {
// remove double quotes
$query = str_replace('"', '', $query);
$words = explode(' ', $query);
$eventTitles = $ret = [];
$word_cnt = count ( $words );
for ($i = 0; $i < $word_cnt; $i++) {
$sql_params = [];
// Note: we only search approved/waiting events (not deleted).
$sql = 'SELECT we.cal_id, we.cal_name, we.cal_date, weu.cal_login '
. (empty($extra_filter) ? '' : ', wse.cal_data ')
. 'FROM webcal_entry_user weu LEFT JOIN webcal_entry we '
. (empty($cat_filter) ? '' : ', webcal_entry_categories wec ')
. (empty($extra_filter) ? '' : ', webcal_site_extras wse ')
. 'ON weu.cal_id = we.cal_id WHERE weu.cal_status in ( \'A\',\'W\' )
AND weu.cal_login IN ( ?';
$sql_params[] = $login;
$sql .= ' ) ';
// We get an error using mssql trying to read text column as varchar.
// This workaround seems to fix it up ROJ
// but, will only search the first 1kb of the description.
$sql .= 'AND ( UPPER( we.cal_name ) LIKE UPPER( ? ) OR UPPER( '
. (strcmp($GLOBALS['db_type'], 'mssql') == 0
? 'CAST ( we.cal_description AS varchar (1024) )'
: 'we.cal_description')
. ' ) LIKE UPPER( ? ) ) ';
$sql_params[] = '%' . $words[$i] . '%';
$sql_params[] = '%' . $words[$i] . '%';
//echo "SQL:\n$sql\n\n";
$res = dbi_execute ( $sql . ' ORDER BY we.cal_date ' .
', we.cal_name', $sql_params );
if ($res) {
while ($row = dbi_fetch_row($res)) {
$utitle = str_replace(' ', '', strtoupper($row[1]));
if (empty($eventTitles[$utitle])) {
$ret[$matches]['id'] = $row[0];
$ret[$matches]['name'] = $row[1];
$ret[$matches]['text'] = $row[1] . ' (' . date_to_str($row[2]) . ')';
$eventTitles[$utitle] = 1;
$matches++;
//echo "utitle = \"$utitle\" \n";
}
}
}
dbi_free_result ( $res );
}
$data = $sug = [];
foreach ( $ret as $i ) {
$sug[] = $i['name'];
$data[] = $i['text'];
}
ajax_send_object('matches', $sug, $sendPlainText);
} else {
ajax_send_error(translate("Error"));
}
exit;