-
Notifications
You must be signed in to change notification settings - Fork 4
/
logic.php
290 lines (215 loc) · 9.38 KB
/
logic.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
<?php
require_once('markdown.php');
class DBObject {
}
class Category extends DBObject {
var $id;
var $name;
public static $table_name = 'q_category';
public function delete() {
$query = "DELETE FROM ".Category::$table_name." WHERE id = ".$this->id." LIMIT 1";
$success = null;
$con = $GLOBALS["con"];
$result = mysqli_query($con, $query, $success) or die("mysql_error in delete: <b>".$query."</b> ". mysql_error());
return 1;
}
public static function Create($name_param=null) {
// TODO: handle duplicates
if($name_param == null) return;
$con = $GLOBALS["con"];
$name_param_safe = mysqli_real_escape_string($con, stripslashes($name_param));
$query = "INSERT INTO ".Category::$table_name." (name) VALUES ('".$name_param_safe."')";
$result = mysqli_query($con, $query, $success) or die("mysql_error in insert: ". mysql_error());
return mysqli_insert_id($con);
}
public function update($req_name) {
$safe_req_name = mysql_real_escape_string(stripslashes($req_name));
$query = "UPDATE ".Category::$table_name." SET name = '".$safe_req_name."' WHERE id = ".$this->id." LIMIT 1";
if($req_name == null) {
die($query);
}
$con = $GLOBALS["con"];
$success = null;
$result = mysqli_query($con, $query, $success) or die("mysql_error in update: <b>".$query."</b> ". mysql_error());
}
public static function AllObjects($where_clause=null) {
$query = "SELECT C.id, C.name FROM ".Category::$table_name." AS C";
if($where_clause) {
$query .= " WHERE (".$where_clause.")";
}
$query .= " ORDER BY C.name";
$con = $GLOBALS["con"];
$success = null;
$result = mysqli_query($con, $query, $success) or die("Error in query: ". mysql_error());
$a = array();
while (list($cat_id, $cat_name) = mysqli_fetch_row($result)) {
$c = new Category();
$c->id = $cat_id;
$c->name = $cat_name;
array_push($a, $c);
}
return $a;
}
public static function CategoriesWithId($id) {
$id_int = (int)$id;
$where_clause = "C.id = ".$id_int;
return Category::AllObjects($where_clause);
}
public static function CategoryWithId($id) {
$cs = Category::CategoriesWithId($id);
return $cs[0];
}
}
class Note extends DBObject {
var $id;
var $title;
var $text;
var $timestamp;
var $category_id;
var $category_name;
public static $table_name = 'q_note';
public function delete() {
$query = "DELETE FROM ".Note::$table_name." WHERE id = ".$this->id." LIMIT 1";
$success = null;
$con = $GLOBALS["con"];
$result = mysqli_query($con, $query, $success) or die("mysql_error in delete: <b>".$query."</b> ". mysql_error());
return 1;
}
public function update($req_cat_id, $req_note_title, $req_note_text) {
$con = $GLOBALS["con"];
$safe_cat_id = mysqli_real_escape_string($con, (int)$req_cat_id);
$safe_note_title = mysqli_real_escape_string($con, stripslashes($req_note_title));
$safe_note_text = mysqli_real_escape_string($con, stripslashes($req_note_text));
$query = "UPDATE ".Note::$table_name." SET title = '".$safe_note_title."', text = '".$safe_note_text."', category_id = '".$safe_cat_id."' WHERE id = ".$this->id." LIMIT 1";
if($req_cat_id == 0 || $req_note_title == null || $req_note_text == null) {
die($query);
}
$success = null;
$result = mysqli_query($con, $query, $success) or die("mysql_error in update: <b>".$query."</b> ". mysql_error());
}
public static function AllObjectsCount() {
$query = "SELECT COUNT(*) FROM ".Note::$table_name;
$con = $GLOBALS["con"];
$success = null;
$result = mysqli_query($con, $query, $success) or die("Error in query: ". mysql_error());
while (list($count) = mysqli_fetch_row($result)) {
return $count;
}
return 0;
}
public static function Create($req_cat_id, $req_title, $req_text) {
if($req_cat_id == null || $req_title == null || $req_text == null) return;
$con = $GLOBALS["con"];
$safe_cat_id = mysqli_real_escape_string($con, (int)$req_cat_id);
$safe_title = mysqli_real_escape_string($con, stripslashes($req_title));
$safe_text = mysqli_real_escape_string($con, stripslashes($req_text));
$query = "INSERT INTO ".Note::$table_name." (category_id, title, text) VALUES ('".$safe_cat_id."', '".$safe_title."', '".$safe_text."')";
$success = null;
$result = mysqli_query($con, $query, $success) or die("mysql_error in insert: ". mysql_error());
return mysqli_insert_id($con);
}
public static function AllObjects($where_clause=null, $order_clause=null, $limit=0) {
$query = "SELECT N.id, N.title, N.text, N.timestamp, N.category_id, C.id, C.name FROM ".Note::$table_name." AS N, ".Category::$table_name." AS C WHERE C.id = N.category_id";
if($where_clause) {
$query .= " AND (".$where_clause.")";
}
$query .= " ORDER BY ".$order_clause." C.name, N.title";
if($limit != 0) {
$query .= " LIMIT ".(int)$limit;
}
$con = $GLOBALS["con"];
$success = null;
$result = mysqli_query($con, $query, $success) or die("Error in query: ". mysql_error());
$a = array();
while (list($note_id, $note_title, $note_text, $note_timestamp, $note_category_id, $category_id, $category_name) = mysqli_fetch_row($result)) {
$n = new Note();
$n->id = $note_id;
$n->title = $note_title;
$n->text = $note_text;
$n->timestamp = $note_timestamp;
$n->category_id = $note_category_id;
$n->category_name = $category_name;
array_push($a, $n);
}
return $a;
}
public static function LatestNotes() {
return Note::AllObjects(null, "N.timestamp DESC,", 15);
}
public static function AllNotesIncludingOnesWithoutCategory() {
$a1 = Note::NotesWithMissingCategory();
$a2 = Note::AllObjects();
foreach($a2 as $n) {
array_push($a1, $n);
}
return $a1;
}
public static function NotesWithMissingCategory() {
$query = "SELECT N.id, N.title, N.text, N.timestamp, N.category_id FROM ".Note::$table_name." AS N WHERE category_id NOT IN (SELECT id FROM ".Category::$table_name.");";
$con = $GLOBALS["con"];
$success = null;
$result = mysqli_query($con, $query, $success) or die("Error in query: ". mysql_error());
$a = array();
while (list($note_id, $note_title, $note_text, $note_timestamp, $note_category_id, $category_id) = mysqli_fetch_row($result)) {
$n = new Note();
$n->id = $note_id;
$n->title = $note_title;
$n->text = $note_text;
$n->timestamp = $note_timestamp;
$n->category_id = 0;
$n->category_name = "** no category **";
array_push($a, $n);
}
return $a;
}
public static function NotesWithId($id) {
$id_int = (int)$id;
$where_clause = "N.id = ".$id_int;
return Note::AllObjects($where_clause);
}
public static function NotesWithCatId($id) {
$id_int = (int)$id;
$where_clause = "C.id = ".$id_int;
return Note::AllObjects($where_clause);
}
public static function NoteWithId($id) {
$notes = Note::NotesWithId($id);
$n = $notes[0];
if($n) return $n;
return Note::NoteWithIdNoCategory($id);
}
public static function NotesWithIdNoCategory($id) {
$id_int = (int)$id;
$query = "SELECT id, title, text, timestamp, category_id FROM `".Note::$table_name."` WHERE id = ".$id_int.";";
//echo $query;
$con = $GLOBALS["con"];
$success = null;
$result = mysqli_query($con, $query) or die("Error in query: ". mysql_error());
$a = array();
while (list($note_id, $note_title, $note_text, $note_timestamp, $note_category_id) = mysqli_fetch_row($result)) {
$n = new Note();
$n->id = $note_id;
$n->title = $note_title;
$n->text = $note_text;
$n->timestamp = $note_timestamp;
$n->category_id = $note_category_id;
array_push($a, $n);
}
return $a;
}
public static function NoteWithIdNoCategory($id) {
$a = Note::NotesWithIdNoCategory($id);
if(count($a) > 0) {
return $a[0];
}
return null;
}
public function NotesWithSearchString($s) {
$con = $GLOBALS["con"];
$ss = mysqli_real_escape_string($con, $s);
//echo $s;
$where_clause = "N.title LIKE '%$ss%' OR N.text LIKE '%$ss%'";
return Note::AllObjects($where_clause);
}
}
?>