This repository has been archived by the owner on Jan 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 38
/
ajaxfilter.php
184 lines (137 loc) · 5.52 KB
/
ajaxfilter.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<?php
include_once 'data.php';
include_once 'functions.php';
session_write_close();
if (!isset($_GET['select']) || ($_GET['select'] != 'library' &&
$_GET['select'] != 'shelf' &&
$_GET['select'] != 'project' &&
$_GET['select'] != 'clipboard')) {
$_GET['select'] = 'library';
}
database_connect(IL_DATABASE_PATH, 'library');
$in = '';
if ($_GET['select'] == 'shelf') {
$in = "id IN (SELECT fileID FROM shelves WHERE fileID>0 AND userID=" . intval($_SESSION['user_id']) . ")";
}
if ($_GET['select'] == 'clipboard') {
attach_clipboard($dbHandle);
$in = "id IN (SELECT id FROM clipboard.files)";
}
empty($in) ? $and = '' : $and = 'AND';
if (isset($_GET['term']))
$_GET['filter'] = $_GET['term'];
if (isset($_GET['filter'])) {
$filter = $_GET['filter'];
$filter_query = $dbHandle->quote("%$filter%");
} else {
die();
}
######################################################################
if (isset($_GET['open']) && in_array("authors", $_GET['open'])) {
$filter_arr = array();
if (strstr($filter, ',') !== 0)
$filter_arr = explode(',', $filter);
if (!empty($filter_arr[0]))
$author_filter = $dbHandle->quote('%L:"' . trim($filter_arr[0]) . '%');
if (!empty($filter_arr[1]))
$author_filter = $dbHandle->quote('%L:"' . trim($filter_arr[0]) . '",F:"' . trim($filter_arr[1]) . '%');
$result = $dbHandle->query("SELECT authors || ';' || authors_ascii FROM library WHERE $in $and (authors LIKE $author_filter OR authors_ascii LIKE $author_filter)");
$authors = $result->fetchAll(PDO::FETCH_COLUMN);
$dbHandle = null;
$authors_string = '';
$authors_string = implode(";", $authors);
$authors = explode(";", $authors_string);
function filter_authors($var) {
global $filter_arr;
return stripos($var, 'L:"' . trim($filter_arr[0])) === 0;
}
$authors = array_filter($authors, 'filter_authors');
if (empty($authors)) {
if (isset($_GET['term'])) {
echo json_encode(array());
} else {
echo 'No such authors.';
}
die();
}
$authors_unique = array_unique($authors);
usort($authors_unique, "strnatcasecmp");
$json_authors = array();
while (list($key, $authors) = each($authors_unique)) {
$authors = str_replace('L:"', '', $authors);
$authors = str_replace('",F:"', ', ', $authors);
$authors = substr($authors, 0, -1);
$json_authors[] = $authors;
if (!isset($_GET['term'])) print PHP_EOL . '<span class="author" id="' . urlencode($authors) . '">' . htmlspecialchars($authors) . '</span><br>';
}
// autocomplete
if (isset($_GET['term']))
echo json_encode($json_authors);
}
######################################################################
if (isset($_GET['open']) && in_array("keywords", $_GET['open'])) {
$result = $dbHandle->query("SELECT keywords FROM library WHERE $in $and keywords LIKE $filter_query");
$keywords = $result->fetchAll(PDO::FETCH_COLUMN);
$dbHandle = null;
$keywords_string = '';
$keywords_string = implode("/", $keywords);
$keywords = explode("/", $keywords_string);
foreach ($keywords as $value) {
$trimmed_keywords[] = trim($value);
}
$trimmed_keywords = array_filter($trimmed_keywords);
if (empty($trimmed_keywords)) {
print 'No such keywords.';
die();
}
$keywords_array = array_unique($trimmed_keywords);
usort($keywords_array, "strnatcasecmp");
while (list($key, $keywords) = each($keywords_array)) {
if (!empty($keywords) && stripos($keywords, $filter) !== false) {
print '<span class="key" id="' . htmlspecialchars(urlencode($keywords)) . '">' . htmlspecialchars($keywords) . '</span><br>';
}
}
}
######################################################################
if (isset($_GET['open']) && in_array("editors", $_GET['open'])) {
$filter_arr = array();
if (strstr($filter, ',') !== 0)
$filter_arr = explode(',', $filter);
if (!empty($filter_arr[0]))
$author_filter = $dbHandle->quote('%L:"' . trim($filter_arr[0]) . '%');
if (!empty($filter_arr[1]))
$author_filter = $dbHandle->quote('%L:"' . trim($filter_arr[0]) . '",F:"' . trim($filter_arr[1]) . '%');
$result = $dbHandle->query("SELECT editor FROM library WHERE $in $and (editor LIKE $author_filter)");
$authors = $result->fetchAll(PDO::FETCH_COLUMN);
$dbHandle = null;
$authors_string = '';
$authors_string = implode(";", $authors);
$authors = explode(";", $authors_string);
function filter_authors($var) {
global $filter_arr;
return stripos($var, 'L:"' . trim($filter_arr[0])) === 0;
}
$authors = array_filter($authors, 'filter_authors');
if (empty($authors)) {
if (isset($_GET['term'])) {
echo json_encode(array());
} else {
echo 'No such editors.';
}
die();
}
$authors_unique = array_unique($authors);
usort($authors_unique, "strnatcasecmp");
$json_authors = array();
while (list($key, $authors) = each($authors_unique)) {
$authors = str_replace('L:"', '', $authors);
$authors = str_replace('",F:"', ', ', $authors);
$authors = substr($authors, 0, -1);
$json_authors[] = $authors;
if (!isset($_GET['term'])) print PHP_EOL . '<span class="author" id="' . urlencode($authors) . '">' . htmlspecialchars($authors) . '</span><br>';
}
// autocomplete
if (isset($_GET['term']))
echo json_encode($json_authors);
}
?>