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
/
categories.php
184 lines (146 loc) · 7.49 KB
/
categories.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';
if (isset($_SESSION['auth'])) {
include_once 'functions.php';
########## category updating ##########
if (isset($_GET['form_sent'])) {
$ids = array();
$file_id = intval($_GET['file']);
$_GET['category2'] = preg_replace('/\s{2,}/', '', $_GET['category2']);
$_GET['category2'] = preg_replace('/^\s$/', '', $_GET['category2']);
$_GET['category2'] = array_filter($_GET['category2']);
####### record new category into categories, if not exists #########
if (!empty($_GET['category2'])) {
database_connect(IL_DATABASE_PATH, 'library');
$query = "INSERT INTO categories (category) VALUES (:category)";
$stmt = $dbHandle->prepare($query);
$stmt->bindParam(':category', $new_category, PDO::PARAM_STR);
$dbHandle->beginTransaction();
while (list($key, $new_category) = each($_GET['category2'])) {
$new_category_quoted = $dbHandle->quote($new_category);
$result = $dbHandle->query("SELECT categoryID FROM categories WHERE category=$new_category_quoted");
$exists = $result->fetchColumn();
$result = null;
$ids[] = $exists;
if (empty($exists)) {
$stmt->execute();
$last_id = $dbHandle->query("SELECT last_insert_rowid() FROM categories");
$ids[] = $last_id->fetchColumn();
$last_id = null;
}
}
$dbHandle->commit();
$stmt = null;
$dbHandle = null;
}
####### record new relations into filescategories #########
database_connect(IL_DATABASE_PATH, 'library');
$categories = array();
if (!empty($_GET['category']) || !empty($ids)) {
$categories = array_merge((array) $_GET['category'], (array) $ids);
$categories = array_filter(array_unique($categories));
}
$query = "INSERT OR IGNORE INTO filescategories (fileID,categoryID) VALUES (:fileid,:categoryid)";
$stmt = $dbHandle->prepare($query);
$stmt->bindParam(':fileid', $file_id);
$stmt->bindParam(':categoryid', $category_id);
$dbHandle->beginTransaction();
$dbHandle->exec("DELETE FROM filescategories WHERE fileID=" . $file_id);
while (list($key, $category_id) = each($categories)) {
if (!empty($file_id))
$stmt->execute();
}
$dbHandle->commit();
$stmt = null;
$dbHandle = null;
die('OK');
}
########## read reference data, categories ##########
database_connect(IL_DATABASE_PATH, 'library');
$file_query = $dbHandle->quote($_GET['file']);
$record = $dbHandle->query("SELECT title,abstract FROM library WHERE id=$file_query LIMIT 1");
$result = $dbHandle->query("SELECT categoryID,category FROM categories ORDER BY category COLLATE NOCASE ASC");
$record_categories = $dbHandle->query("SELECT filescategories.categoryID as categoryID
FROM filescategories INNER JOIN categories ON filescategories.categoryID=categories.categoryID
WHERE fileID=$file_query");
$dbHandle = null;
$paper = $record->fetch(PDO::FETCH_ASSOC);
$cat_all = array();
while ($categories = $result->fetch(PDO::FETCH_ASSOC)) {
$cat_all["$categories[categoryID]"] = $categories['category'];
}
$cat_paper = $record_categories->fetchAll(PDO::FETCH_COLUMN);
while (list($cat_key, $cat_name) = each($cat_all)) {
if (stristr("$paper[title] $paper[abstract]", $cat_name)) {
$checkbox = '<span data-catid="' . $cat_key . '" style="cursor: pointer">';
if (in_array($cat_key, $cat_paper)) {
$checkbox .= '<i class="fa fa-check-square"></i> ';
} else {
$checkbox .= '<i class="fa fa-square-o"></i> ';
}
$suggested_categories[] = $checkbox . htmlspecialchars($cat_name) . '</span><br>';
}
}
reset($cat_all);
?>
<form id="categoriesform" action="categories.php" method="GET">
<input type="hidden" name="form_sent">
<input type="hidden" name="file" value="<?php print htmlspecialchars($_GET['file']) ?>">
<table cellpadding="0" cellspacing="0" border="0" style="width: 100%;height:100%;margin-top: 0px">
<tr>
<td class="alternating_row" style="width: 190px;padding: 5px">
<input type="submit" value="Save" style="display:none">
<button id="newcatbutton" style="margin:0"><i class="fa fa-save"></i> Save</button><br>
<b>Add to new categories:</b><br>
<input type="text" size="25" name="category2[]" value="" style="width:190px"><br>
<input type="text" size="25" name="category2[]" value="" style="width:190px"><br>
<input type="text" size="25" name="category2[]" value="" style="width:190px"><br>
<input type="text" size="25" name="category2[]" value="" style="width:190px"><br>
<input type="text" size="25" name="category2[]" value="" style="width:190px"><br>
<br>
<b>Suggestions:</b><br>
<span id="suggestions">
<?php if (!empty($suggested_categories)) print implode('<div style="clear:both"></div>', $suggested_categories); ?>
</span>
</td>
<td>
<div class="alternating_row">
<input type="text" id="filtercategories" value="" placeholder="Filter categories" style="width:300px;margin:0.75em 0">
</div>
<table class="categorieslist" style="width:49%;float:left;padding:2px">
<?php
$i = 1;
$newdiv = null;
$totalcount = count($cat_all);
while (list($cat_key, $cat_name) = each($cat_all)) {
$cat_selected = null;
if ($i > (1 + $totalcount / 2) && !$newdiv) {
print '</table><table class="categorieslist" style="width:49%;float:right;padding:2px">';
$newdiv = true;
}
if (isset($cat_paper) && is_numeric(array_search($cat_key, $cat_paper)))
$cat_selected = true;
print PHP_EOL . '<tr><td data-catid="' . $cat_key . '" class="select_span';
if ($cat_selected)
print ' alternating_row';
print '">';
print '<input type="checkbox" name="category[]" value="' . htmlspecialchars($cat_key) . '"';
if ($cat_selected)
print ' checked';
print ' style="display:none">';
print ' <i class="fa fa-';
print ($cat_selected) ? 'check-square' : 'square-o';
print '"></i> ' . htmlspecialchars($cat_name) . '</td></tr>';
$i = $i + 1;
}
?>
</table>
</td>
</tr>
</table>
</form>
<?php
} else {
print 'Super User or User permissions required.';
}
?>