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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
|
<?php
/**
* This file provides the BusinessAdmin class
*
* @author Camil Staps
*
* BusinessAdmin: administrative software for small companies
* Copyright (C) 2015 Camil Staps (ViviSoft)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* Provides basic functions like adding elements to the database
*/
class BusinessAdmin {
//------------------------------------------------------------------------------
// Getters and setters
//------------------------------------------------------------------------------
/**
* Get all user ids
*
* @see BusinessAdmin::getUsers() This funtion returns instances of the user class instead of just the ids
*
* @param PDO $pdo The PDO class for database connection
* @param string[] $where An array of WHERE clauses that will be AND-ed into a prepared statement
* @param mixed[] $variables An array of variables that should go into the prepared statement
*
* @throws PDOException Is something went wrong with the database
*
* @return int[] The ids
*/
public static function getUserIds($pdo, $where = [], $variables = []) {
$ids = [];
$users = $pdo->prepare("SELECT `id` FROM `".Constants::db_prefix."user`" . ((count($where) > 0) ? (" WHERE (" . implode(') AND (', $where) . ")") : ""));
$users->execute($variables);
$users = $users->fetchAll(PDO::FETCH_ASSOC);
foreach ($users as $user) {
$ids[] = $user['id'];
}
return $ids;
}
/**
* Get all users
*
* @see BusinessAdmin::getUserIds() This function returns just the ids of the users, and not instances of the user class
*
* @param PDO $pdo The PDO class for database connection
* @param string[] $where An array of WHERE clauses that will be AND-ed into a prepared statement
* @param mixed[] $variables An array of variables that should go into the prepared statement
*
* @throws PDOException If something went wrong with the database
*
* @return user[] An array indexed by id of instances of the user class
*/
public static function getUsers($pdo, $where = [], $variables = []) {
$ids = self::getUserIds($pdo, $where, $variables);
$users = [];
foreach ($ids as $id) {
$users[$id] = new User($pdo, $id);
}
return $users;
}
/**
* Get all file ids
*
* @see BusinessAdmin::getFiles() This funtion returns instances of the file class instead of just the ids
*
* @param PDO $pdo The PDO class for database connection
* @param string[] $where An array of WHERE clauses that will be AND-ed into a prepared statement
* @param mixed[] $variables An array of variables that should go into the prepared statement
*
* @throws PDOException Is something went wrong with the database
*
* @return int[] The ids
*/
public static function getFileIds($pdo, $where = [], $variables = []) {
$ids = [];
$files = $pdo->prepare("SELECT `id` FROM `".Constants::db_prefix."file`" . ((count($where) > 0) ? (" WHERE (" . implode(') AND (', $where) . ")") : ""));
$files->execute($variables);
$files = $files->fetchAll(PDO::FETCH_ASSOC);
foreach ($files as $file) {
$ids[] = $file['id'];
}
return $ids;
}
/**
* Get all files
*
* @see BusinessAdmin::getFileIds() This function returns just the ids of the files, and not instances of the file class
*
* @param PDO $pdo The PDO class for database connection
* @param string[] $where An array of WHERE clauses that will be AND-ed into a prepared statement
* @param mixed[] $variables An array of variables that should go into the prepared statement
*
* @throws PDOException If something went wrong with the database
*
* @return file[] An array indexed by id of instances of the file class
*/
public static function getFiles($pdo, $where = [], $variables = []) {
$ids = self::getFileIds($pdo, $where, $variables);
$files = [];
foreach ($ids as $id) {
$files[$id] = new File($pdo, $id);
}
return $files;
}
/**
* Get all client ids
*
* @see BusinessAdmin::getClients() This funtion returns instances of the client class instead of just the ids
*
* @param PDO $pdo The PDO class for database connection
*
* @throws PDOException If something went wrong with the database
*
* @return int[] The ids
*/
public static function getClientIds($pdo) {
$ids = array();
$clients = $pdo->query("SELECT `id` FROM `".Constants::db_prefix."client`")->fetchAll(PDO::FETCH_ASSOC);
foreach ($clients as $client) {
$ids[] = $client['id'];
}
return $ids;
}
/**
* Get all clients
*
* @see BusinessAdmin::getClientIds() This function returns just the ids of the clients, and not instances of the client class
*
* @param PDO $pdo The PDO class for database connection
*
* @throws PDOException If something went wrong with the database
*
* @return client[] An array indexed by id of instances of the client class
*/
public static function getClients($pdo) {
$ids = self::getClientIds($pdo);
$clients = array();
foreach ($ids as $id) {
$clients[$id] = new Client($pdo, $id);
}
return $clients;
}
/**
* Get all contact ids
*
* @see BusinessAdmin::getContacts() This funtion returns instances of the contact class instead of just the ids
*
* @param PDO $pdo The PDO class for database connection
*
* @throws PDOException Is something went wrong with the database
*
* @return int[] The ids
*/
public static function getContactIds($pdo) {
$ids = array();
$contacts = $pdo->query("SELECT `id` FROM `".Constants::db_prefix."contact`")->fetchAll(PDO::FETCH_ASSOC);
foreach ($contacts as $contact) {
$ids[] = $contact['id'];
}
return $ids;
}
/**
* Get all contacts
*
* @see BusinessAdmin::getContactIds() This function returns just the ids of the contacts, and not instances of the contact class
*
* @param PDO $pdo The PDO class for database connection
*
* @throws PDOException If something went wrong with the database
*
* @return contact[] An array indexed by id of instances of the contact class
*/
public static function getContacts($pdo) {
$ids = self::getContactIds($pdo);
$contacts = array();
foreach ($ids as $id) {
$contacts[$id] = new Contact($pdo, $id);
}
return $contacts;
}
/**
* Get all offer ids
*
* @see BusinessAdmin::getOffers() This funtion returns instances of the offer class instead of just the ids
*
* @param PDO $pdo The PDO class for database connection
* @param string[] $where An array of WHERE clauses that will be AND-ed
*
* @throws PDOException Is something went wrong with the database
*
* @return int[] The ids
*/
public static function getOfferIds($pdo, $where = array()) {
$ids = array();
$offers = $pdo->query("SELECT `id` FROM `".Constants::db_prefix."offer`" . ((count($where) > 0) ? (" WHERE (" . implode(') AND (', $where) . ")") : ""))->fetchAll(PDO::FETCH_ASSOC);
foreach ($offers as $offer) {
$ids[] = $offer['id'];
}
return $ids;
}
/**
* Get all offers
*
* @see BusinessAdmin::getOfferIds() This function returns just the ids of the offers, and not instances of the offer class
*
* @param PDO $pdo The PDO class for database connection
* @param string[] $where An array of WHERE clauses that will be AND-ed
*
* @throws PDOException If something went wrong with the database
*
* @return offer[] An array indexed by id of instances of the offer class
*/
public static function getOffers($pdo, $where = array()) {
$ids = self::getOfferIds($pdo, $where);
$offers = array();
foreach ($ids as $id) {
$offers[$id] = new Offer($pdo, $id);
}
return $offers;
}
/**
* Get all assignment ids
*
* @see BusinessAdmin::getAssignments() This funtion returns instances of the assignment class instead of just the ids
*
* @param PDO $pdo The PDO class for database connection
* @param string[] $where An array of WHERE clauses that will be AND-ed
*
* @throws PDOException Is something went wrong with the database
*
* @return int[] The ids
*/
public static function getAssignmentIds($pdo, $where = array()) {
$ids = array();
$assignments = $pdo->query("SELECT `id` FROM `".Constants::db_prefix."assignment`" . ((count($where) > 0) ? (" WHERE (" . implode(') AND (', $where) . ")") : ""))->fetchAll(PDO::FETCH_ASSOC);
foreach ($assignments as $assignment) {
$ids[] = $assignment['id'];
}
return $ids;
}
/**
* Get all assignments
*
* @see BusinessAdmin::getAssignmentIds() This function returns just the ids of the assignments, and not instances of the assignment class
*
* @param PDO $pdo The PDO class for database connection
* @param string[] $where An array of WHERE clauses that will be AND-ed
*
* @throws PDOException If something went wrong with the database
*
* @return assignment[] An array indexed by id of instances of the assignment class
*/
public static function getAssignments($pdo, $where = array()) {
$ids = self::getAssignmentIds($pdo, $where);
$assignments = array();
foreach ($ids as $id) {
$assignments[$id] = new Assignment($pdo, $id);
}
return $assignments;
}
/**
* Get all discount ids
*
* @see BusinessAdmin::getDiscounts() This funtion returns instances of the discount class instead of just the ids
*
* @param PDO $pdo The PDO class for database connection
* @param string[] $where An array of WHERE clauses that will be AND-ed
*
* @throws PDOException Is something went wrong with the database
*
* @return int[] The ids
*/
public static function getDiscountIds($pdo, $where = array()) {
$ids = array();
$discounts = $pdo->query("SELECT `id` FROM `".Constants::db_prefix."discount`" . ((count($where) > 0) ? (" WHERE (" . implode(') AND (', $where) . ")") : ""))->fetchAll(PDO::FETCH_ASSOC);
foreach ($discounts as $discount) {
$ids[] = $discount['id'];
}
return $ids;
}
/**
* Get all discounts
*
* @see BusinessAdmin::getDiscountIds() This function returns just the ids of the discounts, and not instances of the discount class
*
* @param PDO $pdo The PDO class for database connection
* @param string[] $where An array of WHERE clauses that will be AND-ed
*
* @throws PDOException If something went wrong with the database
*
* @return discount[] An array indexed by id of instances of the discount class
*/
public static function getDiscounts($pdo, $where = array()) {
$ids = self::getDiscountIds($pdo, $where);
$discounts = array();
foreach ($ids as $id) {
$discounts[$id] = new Discount($pdo, $id);
}
return $discounts;
}
//------------------------------------------------------------------------------
// Other functions
//------------------------------------------------------------------------------
/**
* Create a new client
*
* @param PDO $pdo The database connection
* @param string $name The name for the new client
*
* @throws PDOException If something went wrong with the database
*
* @return client|bool A new instance of the client object, or false on failure
*/
public static function createClient($pdo, $name) {
$stmt = $pdo->prepare("INSERT INTO `".Constants::db_prefix."client` (`name`) VALUES (?)");
$stmt->execute(array($name));
if ($stmt->rowCount() == 1) {
return new Client($pdo, $pdo->lastInsertId());
} else {
return false;
}
}
/**
* Create a new file
*
* @param PDO $pdo The database connection
* @param string $filename The desired filename
*
* @throws PDOException If something went wrong with the database
* @throws Exception If the file could not be created (due to permissions, file existed already, etc.), or the database record couldn't be added
*
* @return file A new instance of the file object
*/
public static function createFile($pdo, $filename) {
// Check for file existence
if (file_exists(Constants::files_folder . $filename)) {
throw new Exception("$filename already exists.");
}
// Try to create the file
if (file_put_contents(Constants::files_folder . $filename, '') === false) {
throw new Exception("$filename could not be created. Check the permissions.");
}
$stmt = $pdo->prepare("INSERT INTO `".Constants::db_prefix."file` (`filename`) VALUES (?)");
$stmt->execute(array($filename));
if ($stmt->rowCount() == 1) {
return new File($pdo, $pdo->lastInsertId());
} else {
unlink(Constants::files_folder . $filename);
throw new Exception("$filename could not be added to the database");
}
}
/**
* Create a new user
*
* @param PDO $pdo The database connection
* @param string $username The username for the new user
* @param string $password The password for the new user
*
* @throws PDOException If something went wrong with the database
*
* @return user|bool A new instance of the user object, or false on failure
*/
public static function createUser($pdo, $username, $password) {
$stmt = $pdo->prepare("INSERT INTO `".Constants::db_prefix."user` (`username`, `password`) VALUES (?,?)");
$stmt->execute([$username, user::hash($password)]);
if ($stmt->rowCount() == 1) {
return new User($pdo, $pdo->lastInsertId());
} else {
return false;
}
}
/**
* Format a date nicely
*
* @todo implement $relatively = true
*
* @param int $timestamp The UNIX timestamp to format
* @param bool $with_time If false, only the date is returned; if false, also the time
* @param bool $full_date If true, a year will be outputted even if the date is in the current year
* @param bool $relatively Whether or not to show the date relatively (e.g. '1 day ago') (NOT IMPLEMENTED YET)
*
* @return string The formatted date
*/
public static function formatDate($timestamp, $with_time = true, $full_date = false, $relatively = false) {
$output = '';
if (date('Y', $timestamp) == 1970) {
return 'never';
}
if (date('d/m/Y') == date('d/m/Y', $timestamp)) {
return 'today';
}
if (date('Y') != date('Y', $timestamp) || $full_date) {
$output .= date('Y-', $timestamp);
}
if (date('d/m/Y') != date('d/m/Y', $timestamp) || $full_date) {
$output .= date('m-d', $timestamp);
}
if ($with_time) {
$output .= date(' H:i', $timestamp);
}
return $output;
}
}
|