-
Notifications
You must be signed in to change notification settings - Fork 0
/
classes.php
1130 lines (1045 loc) · 36.1 KB
/
classes.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
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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
require_once 'config.php';
/**
* Function is_admin_logged_in
*
* Checks if an admin is logged in
*
* @return bool
*/
function is_admin_logged_in() {
if (session_status() == PHP_SESSION_NONE) {
session_start();
}
if (isset($_SESSION['admin_logged_in']) &&
$_SESSION['admin_logged_in'] == 1) {
// Logged in
return true;
} else {
// Not logged in
return false;
}
}
/**
* Function security_check_admin
*
* Checks if an admin is logged in and if not head to index
*
* @return bool|exit
*/
function security_check_admin() {
if (session_status() == PHP_SESSION_NONE) {
session_start();
}
if (isset($_SESSION['admin_logged_in']) &&
$_SESSION['admin_logged_in'] == 1) {
// Logged in
return true;
} else {
// Not logged in
header("location: products.php");
exit();
}
}
/**
* Function is_customer_logged_in
*
* Checks if an customer is logged in
*
* @return bool
*/
function is_customer_logged_in() {
if (session_status() == PHP_SESSION_NONE) {
session_start();
}
if (isset($_SESSION['customer_logged_in']) &&
$_SESSION['customer_logged_in'] == 1) {
// Logged in
return true;
} else {
// Not logged in
return false;
}
}
/**
* Function security_check_customer
*
* Checks if an customer is logged in and if not head to index
*
* @return bool
*/
function security_check_customer() {
if (session_status() == PHP_SESSION_NONE) {
session_start();
}
if (isset($_SESSION['customer_logged_in']) &&
$_SESSION['customer_logged_in'] == 1) {
// Logged in
return true;
} else {
// Not logged in
header("location: index.php");
exit();
}
}
/**
* Class ProductType
*
* Handles the product categories
*
*/
class ProductType {
public $id;
public $name;
function __construct($id = null) {
if ($id) {
global $db;
$query = $db->prepare("SELECT * FROM ProductTypes WHERE id = :id");
$query->bindParam(':id', $id, PDO::PARAM_STR);
$query->setFetchMode(PDO::FETCH_INTO, $this);
$query->execute();
$query->fetch();
}
}
/**
* Function getAllProductTypes
*
* Gets all productTypes from the database and fetches it all into 1 big
* object with subobject of the class "productType"
*
* @return object with subobjects as ProductType
*/
static function getAllProductTypes() {
global $db;
$query = $db->prepare("SELECT * FROM ProductTypes ORDER BY name");
$query->execute();
$result = $query->fetchAll(PDO::FETCH_CLASS, "ProductType");
return $result;
}
/**
* Function create
*
* Inserts a new producttype in the database
*
* @param string $name Name of the new productype
*/
static function create($name) {
global $db;
$query = $db->prepare("INSERT INTO ProductTypes (name) VALUES (:name)");
$query->bindParam(':name', $name, PDO::PARAM_STR);
$query->execute();
}
/**
* Function create
*
* Outputs the the productype edit form to the user
*
* @return file Contents of the edit form view
*/
function displayEditForm() {
$output = include 'views/ProductType_editForm.php';
return $output;
}
/**
* Function edit
*
* Updates the productype name in the database
*/
function edit() {
global $db;
$query = $db->prepare("UPDATE ProductTypes SET name = :name "
. "WHERE id = :id");
$query->bindParam(':name', $this->name, PDO::PARAM_STR);
$query->bindParam(':id', $this->id, PDO::PARAM_INT);
$status = $query->execute();
if ($status) {
return true;
} else {
return false;
}
}
/**
* Function delete
*
* Deletes a producttype from the database
*/
function delete() {
global $db;
$query = $db->prepare("DELETE FROM ProductTypes WHERE id = :id");
$query->bindParam(':id', $this->id, PDO::PARAM_STR);
$query->execute();
}
}
/**
* Class Product
*
* Handles the seperate products
*/
class Product {
public $id;
public $typeid;
public $type;
public $name;
public $description;
public $image;
public $stock;
public $price;
public $quantity;
function __construct($id = null) {
if ($id) {
global $db;
$query = $db->prepare("SELECT * FROM Products WHERE id = :id");
$query->bindParam(':id', $id, PDO::PARAM_STR);
$query->setFetchMode(PDO::FETCH_INTO, $this);
$query->execute();
$status = $query->fetch();
if ($status == false) {
header('Location: 404page.php');
}
}
$this->type = new ProductType($this->typeid);
}
/**
* Function getAllProductTypes
*
* Displays one productbox to the user
*
* @return file
*/
function displayBox() {
$output = include 'views/Product_displayBox.php';
return $output;
}
/**
* Function countProducts
*
* Counts rows of products
*
* @param int $type specifies if only a specific type should be counted
* @return file
*/
static function countProducts($type = null) {
global $db;
if ($type) {
$query = $db->prepare("SELECT * FROM Products "
. "WHERE typeid = :typeid");
$query->bindParam(':typeid', $type, PDO::PARAM_INT);
} else {
$query = $db->prepare("SELECT * FROM Products");
}
$query->execute();
$result = $query->rowCount();
return $result;
}
/**
* Function getAllProducts
*
* Gets all products from the database and fetches it all into 1 big
* object with subobject of the class "products"
*
* @param int $type producttype (categorie)
* @param string $sortorder specifies sorting method
* @param int $sortorder specifies sorting method
* @param int $startamount specifies lowerlimit
* @param int $endamount amount of products per page upperlimit
* @param int(bool) $special sets if products should marked as special
* (frontpage)
* @return object with subobjects as products
*/
static function getAllProducts($type = null,
$sortorder = "name ASC",
$startamount = 0,
$endamount = 8,
$special = 0) {
global $db;
if ($type) {
$query = $db->prepare("SELECT * FROM Products "
. "WHERE typeid = :typeid ORDER BY $sortorder "
. "LIMIT :startamount, :endamount");
$query->bindParam(':startamount', $startamount, PDO::PARAM_INT);
$query->bindParam(':endamount', $endamount, PDO::PARAM_INT);
$query->bindParam(':typeid', $type, PDO::PARAM_INT);
} else if ($special == 1) {
$query = $db->prepare("SELECT * FROM Products WHERE special = 1");
} else {
$query = $db->prepare("SELECT * FROM Products "
. "ORDER BY $sortorder LIMIT :startamount, :endamount");
$query->bindParam(':startamount', $startamount, PDO::PARAM_INT);
$query->bindParam(':endamount', $endamount, PDO::PARAM_INT);
}
$query->execute();
$result = $query->fetchAll(PDO::FETCH_CLASS, "Product");
return $result;
}
/**
* Function search
*
* Searches the product table for the input query with on two sides of the query a wildcard
*
* @param int the query word
* @return bool
*/
static function search($word) {
global $db;
$query = $db->prepare("SELECT * FROM Products WHERE name "
. "LIKE :word ORDER BY name");
$str = '%' . $word . '%';
$query->bindParam(':word', $str);
$query->execute();
$result = $query->fetchAll(PDO::FETCH_CLASS, "Product");
return $result;
}
/**
* Function create
*
* Creates the product details
*
* @return bool
*/
static function create($typeid,
$name,
$description,
$stock,
$price,
$special,
$image = null) {
global $db;
$query = $db->prepare("INSERT INTO Products (typeid, name, description, "
. "stock, price, special, image) "
. "VALUES (:typeid, :name, :description, :stock, :price, "
. ":special, :image)");
$query->bindParam(':typeid', $typeid, PDO::PARAM_INT);
$query->bindParam(':name', $name, PDO::PARAM_STR);
$query->bindParam(':description', $description, PDO::PARAM_STR);
$query->bindParam(':stock', $stock, PDO::PARAM_STR);
$query->bindParam(':price', $price, PDO::PARAM_STR);
$query->bindParam(':special', $special, PDO::PARAM_INT);
$query->bindParam(':image', $image, PDO::PARAM_LOB);
return $query->execute();
}
/**
* Function edit
*
* Updates the product details
*
* @return bool
*/
function edit() {
global $db;
$query = $db->prepare("UPDATE Products SET typeid = :typeid, "
. "name = :name, description = :description, stock = :stock, "
. "price = :price, special = :special, image = :image "
. "WHERE id = :id");
$query->bindParam(':image', $this->image, PDO::PARAM_LOB);
$query->bindParam(':typeid', $this->typeid, PDO::PARAM_INT);
$query->bindParam(':name', $this->name, PDO::PARAM_STR);
$query->bindParam(':description', $this->description, PDO::PARAM_STR);
$query->bindParam(':stock', $this->stock, PDO::PARAM_STR);
$query->bindParam(':price', $this->price, PDO::PARAM_STR);
$query->bindParam(':special', $this->special, PDO::PARAM_INT);
$query->bindParam(':id', $this->id, PDO::PARAM_INT);
return $query->execute();
}
/**
* Function resizeImage
*
* Resizes an image to specific aspect ratio. It does not crop so the image
* will be stretched. *Uses imagick module.*
*
* NOTE: This function is not used by default. It needs to be uncomemnted in
* admin_edit_product and
* admin_add_product to enable it.
*
* @param image file
* @param int dowidth width of new image
* @param int doheight heightof new image
* @return image or false
*/
static function resizeImage($image, $doheight, $dowidth) {
list($width) = getimagesize($image);
$height = round($width / ($doheight / $dowidth));
$resizedimage = new Imagick($image);
$status = $resizedimage->scaleImage($height, $width);
if ($status) {
return $resizedimage;
} else {
return false;
}
}
function displayEditForm() {
$output = include 'views/Product_editForm.php';
return $output;
}
/**
* Function delete
*
* Delete a whole product row
*
* @return bool
*/
function delete() {
global $db;
$query = $db->prepare("DELETE FROM Products WHERE id = :id");
$query->bindParam(':id', $id, PDO::PARAM_STR);
$query->bindParam(':id', $this->id, PDO::PARAM_STR);
$status = $query->execute();
return $status;
}
}
/**
* Class Customer
*
* Handles the customer actions
*/
class Customer {
public $id;
public $firstname;
public $lastname;
public $streetaddress;
public $streetnumber;
public $zip;
public $email;
public $gender;
function __construct($id = null) {
if ($id) {
global $db;
$query = $db->prepare("SELECT * FROM Customers WHERE id = :id");
$query->bindParam(':id', $id, PDO::PARAM_INT);
$query->setFetchMode(PDO::FETCH_INTO, $this);
$query->execute();
$query->fetch();
}
}
function displayBox() {
$output = include 'views/Customer_displayBox.php';
return $output;
}
function displayCustomerDetails() {
$output = include 'views/Customer_detailsTable.php';
return $output;
}
function displayEditForm() {
$output = include 'views/Customer_editForm.php';
return $output;
}
/**
* Function login
*
* Checks if the username and hashed password can be be found in
* the database. If it can be found set session customer logged in and set a
* session customer id. If it can not be found point head to function
* credentialsfalse
*
* @param string $email Email of the user
* @param string $password Plain password of the user
*
*/
static function login($email, $password) {
global $db;
global $passwordsalt;
$password = hash("sha256", $password . $passwordsalt);
$query = $db->prepare("SELECT * FROM Customers "
. "WHERE email = :email AND password = :password");
$query->bindParam(':email', $email, PDO::PARAM_STR);
$query->bindParam(':password', $password, PDO::PARAM_STR);
$query->execute();
$result = $query->fetchAll(PDO::FETCH_CLASS, "Customer");
if ($result) {
if (session_status() == PHP_SESSION_NONE) {
session_start();
}
$_SESSION['customer_logged_in'] = 1;
$_SESSION['customer_id'] = $result[0]->id;
if (isset($_SESSION['loginFalse']) &&
$_SESSION['loginFalse'] == 1) {
$_SESSION['loginFalse'] == 0;
header('Location: checkout.php');
} else {
header('Location: products.php');
}
exit();
} else {
header('Location: customer_login.php?fn=credentialsfalse');
exit();
}
}
/**
* Function create
*
* Create a new customer. Before inserting the variable in the database the
* password + salt is hashed with sha256.
*
* @param string $email Email of the user
* @param string $password Plain password of the user
* @param string $streetaddress Streetname
* @param string $streetnumber Integer in street + optional letter(s)
* @param string $zip Zip code
* @param string $firstname First name of user
* @param string $lastname Last name of user
* @param bool $gender 1 for male and 0 for female
*
*/
static function create($email,
$password,
$streetaddress,
$streetnumber,
$zip,
$firstname,
$lastname,
$gender) {
global $db;
global $passwordsalt;
$password = hash("sha256", $password . $passwordsalt);
try {
$query = $db->prepare("INSERT INTO Customers (email, password, "
. "streetaddress, streetnumber, zip, firstname, lastname, "
. "gender) VALUES (:email, :password, :streetaddress, "
. ":streetnumber, :zip, :firstname, :lastname, :gender)");
$query->bindParam(':email', $email, PDO::PARAM_STR);
$query->bindParam(':password', $password, PDO::PARAM_STR);
$query->bindParam(':streetaddress', $streetaddress, PDO::PARAM_STR);
$query->bindParam(':streetnumber', $streetnumber, PDO::PARAM_STR);
$query->bindParam(':zip', $zip, PDO::PARAM_STR);
$query->bindParam(':firstname', $firstname, PDO::PARAM_STR);
$query->bindParam(':lastname', $lastname, PDO::PARAM_STR);
$query->bindParam(':gender', $gender, PDO::PARAM_BOOL);
$query->execute();
if(isset($_SESSION['loginFalse']) && $_SESSION['loginFalse'] == 1) {
Customer::login($email, $password);
}
return true;
} catch (PDOException $e) {
return false;
}
}
static function checkMailOccurrance($email) {
global $db;
$query = $db->prepare("SELECT * FROM Customers WHERE email = :email");
$query->bindParam(':email', $email, PDO::PARAM_STR);
$query->execute();
if ($query->rowCount() > 0) {
return true;
} else {
return false;
}
}
function edit() {
global $db;
$query = $db->prepare("UPDATE Customers SET email = :email, "
. "zip = :zip, gender = :gender, "
. "streetaddress = :streetaddress, "
. "streetnumber = :streetnumber, "
. "firstname = :firstname, "
. "lastname = :lastname WHERE id = :id");
$query->bindParam(':email', $this->email, PDO::PARAM_STR);
$query->bindParam(':streetaddress', $this->streetaddress,
PDO::PARAM_STR);
$query->bindParam(':streetnumber', $this->streetnumber, PDO::PARAM_STR);
$query->bindParam(':zip', $this->zip, PDO::PARAM_STR);
$query->bindParam(':firstname', $this->firstname, PDO::PARAM_STR);
$query->bindParam(':lastname', $this->lastname, PDO::PARAM_STR);
$query->bindParam(':gender', $this->gender, PDO::PARAM_BOOL);
$query->bindParam(':id', $this->id, PDO::PARAM_INT);
$query->execute();
return true;
}
/**
* Function delete
*
* Deletes a customer from the database
*/
function delete() {
global $db;
$query = $db->prepare("DELETE FROM Customers WHERE id = :id");
$query->bindParam(':id', $this->id, PDO::PARAM_STR);
$query->execute();
}
function changePassword($oldpassword, $newpassword, $newpassword2) {
global $db;
global $passwordsalt;
if ($newpassword != "" && $newpassword == $newpassword2) {
$oldpassword = hash("sha256", $oldpassword . $passwordsalt);
$newpassword = hash("sha256", $newpassword . $passwordsalt);
try {
$query = $db->prepare("UPDATE Customers "
. "SET password=:newpassword "
. "WHERE id = :id AND password = :oldpassword");
$query->bindParam(':newpassword', $newpassword, PDO::PARAM_STR);
$query->bindParam(':oldpassword', $oldpassword, PDO::PARAM_STR);
$query->bindParam(':id', $this->id, PDO::PARAM_INT);
$query->execute();
return true;
} catch (PDOException $e) {
return false;
}
}
}
static function getAllCustomers() {
global $db;
$query = $db->prepare("SELECT * FROM Customers");
$query->execute();
$result = $query->fetchAll(PDO::FETCH_CLASS, "Customer");
return $result;
}
/**
* Function logout
*
* Sets session to false and destroy
*/
static function logout() {
if (session_status() == PHP_SESSION_NONE) {
session_start();
}
$_SESSION['customer_logged_in'] = 0;
session_destroy();
}
}
/**
* Class Admin
*
* Handles the admin actions
*/
class Admin {
public $id;
public $username;
public $password;
public $description;
function __construct($id = null) {
if ($id) {
global $db;
$query = $db->prepare("SELECT * FROM Admins WHERE id = :id");
$query->bindParam(':id', $id, PDO::PARAM_INT);
$query->execute();
$query->fetch();
}
}
function displayBox() {
$output = include 'views/Customer_displayBox.php';
return $output;
}
function changePassword($oldpassword, $newpassword) {
global $db;
global $passwordsalt;
$oldpassword = hash("sha256", $oldpassword . $passwordsalt);
$newpassword = hash("sha256", $newpassword . $passwordsalt);
try {
$query = $db->prepare("UPDATE Admins SET password=:newpassword "
. "WHERE id = :id AND password=:oldpassword");
$query->bindParam(':newpassword', $newpassword, PDO::PARAM_STR);
$query->bindParam(':id', $this->id, PDO::PARAM_INT);
$query->bindParam(':oldpassword', $oldpassword, PDO::PARAM_STR);
$query->execute();
return true;
} catch (PDOException $e) {
return false;
}
}
static function create($username, $password) {
global $db;
global $passwordsalt;
$password = hash("sha256", $password . $passwordsalt);
try {
$query = $db->prepare("INSERT INTO Admins (username, password) "
. "VALUES (:username, :password)");
$query->bindParam(':username', $username, PDO::PARAM_STR);
$query->bindParam(':password', $password, PDO::PARAM_STR);
$query->execute();
return true;
} catch (PDOException $e) {
return false;
}
}
static function login($username, $password) {
global $db;
global $passwordsalt;
$password = hash("sha256", $password . $passwordsalt);
$query = $db->prepare("SELECT * FROM Admins "
. "WHERE username = :username AND password = :password");
$query->bindParam(':username', $username, PDO::PARAM_STR);
$query->bindParam(':password', $password, PDO::PARAM_STR);
$query->execute();
$result = $query->fetchAll(PDO::FETCH_CLASS, "Admin");
if ($result) {
session_start();
$_SESSION['admin_logged_in'] = 1;
header('Location: index.php');
exit();
} else {
header('Location: admin_login.php?fn=credentialsfalse');
exit();
}
}
static function logout() {
if (session_status() == PHP_SESSION_NONE) {
session_start();
}
$_SESSION['admin_logged_in'] = 0;
session_destroy();
}
// show_order_list() prints a table to the screen which will contain all the
// orders which are stored in the database. It stores each order in a
// multidimensional array called 'orders' which is stored in the session array.
static function show_order_list() {
global $db;
$i = 0;
$_SESSION['orders'] = Array();
$query = $db->query("SELECT id, customerid, date "
. "FROM Orders ORDER BY id DESC");
$query->setFetchMode(PDO::FETCH_ASSOC);
while (true) {
if ($row = $query->fetch()) {
$_SESSION['orders'][$i]['orderNumber'] = $row['id'];
$_SESSION['orders'][$i]['customerID'] = $row['customerid'];
$_SESSION['orders'][$i]['date'] = $row['date'];
} else {
break;
}
$i += 1;
}
echo "<table>";
echo "<th>Ordernummer</th>";
echo "<th>Klantnummer</th>";
echo "<th>Datum</th>";
foreach ($_SESSION['orders'] as $order) {
echo "<tr>";
echo "<td>";
echo "<form method='post' action='admin_orders.php'>"
. "<input type='submit' name='order_number' value="
. $order['orderNumber'] . ">"
. "</form>";
echo "</td>";
echo "<td>" . $order['customerID'] . "</td>";
echo "<td>" . $order['date'] . "</td>";
echo "</tr>";
}
echo "</table>";
}
// when given the orderID as parameter, show_order prints all the information
// about that order to the screen.
static function show_order($orderID) {
global $db;
global $customer;
$query = $db->query("SELECT customerid FROM Orders WHERE id=$orderID");
$row = $query->fetch();
$customerID = $row['customerid'];
if((isset($_SESSION['admin_logged_in']) &&
$_SESSION['admin_logged_in'] !== 1) || is_customer_logged_in()) {
$loggedInCustomer = $customer->id;
if($customerID !== $loggedInCustomer) {
echo "ERROR";
include 'views/footer.php';
exit();
}
}
$date = Order::show_date($orderID);
echo "<table class='order'>";
echo "<tr>";
echo "<td> factuurnummer: $orderID<br>datum: $date</td>";
echo "</tr>";
Order::show_company_Info();
Admin::show_customer_info($orderID);
echo "</table>";
echo "<table>";
Order::show_order_table($orderID);
echo "</table>";
echo "<a href='admin_view_customer.php?id=$customerID' class='button'>"
. "<span></span>klant bekijken</a>";
}
// when given the orderID, show_customer_info prints all the information about
// the customer who made that order to the screen.
static function show_customer_info($orderID) {
global $db;
$sqlQuery = "SELECT customerid FROM Orders WHERE id=$orderID";
$query = $db->query($sqlQuery);
$query->setFetchMode(PDO::FETCH_ASSOC);
$row = $query->fetch();
$customerID = $row['customerid'];
$sqlQuery = "SELECT * FROM Customers WHERE id=$customerID";
$query = $db->query($sqlQuery);
$row = $query->fetch();
echo "<td>";
echo "<h3>Klantgegevens:</h3> <br>";
echo "klantnummer: " . $customerID . "<br>";
echo "voornaam: " . $row[6] . "<br>";
echo "achternaam: " . $row[7] . "<br>";
echo "adres: " . $row[8] . " ";
echo $row[5] . "<br>";
echo "postcode: " . $row[3] . "<br>";
echo "email: " . $row[1] . "<br>";
echo "</td>";
}
}
class showMessage {
public $messages = array();
public function addMessage($type, $message) {
$this->messages[] = array($type, $message);
}
public function showMessages() {
foreach ($this->messages as $message) {
if ($message[0] == "error") {
echo "<span class=\"message\">"
. "<span id=\"error\">ERROR: </span> "
. $message[1] . "</span>";
} elseif ($message[0] == "success") {
echo "<span class=\"message\">"
. "<span id=\"success\">GELUKT: </span> "
. $message[1] . "</span>";
} elseif ($message[0] == "notice") {
echo "<span class=\"message\">"
. "<span id=\"success\">NOTICE: </span> "
. $message[1] . "</span>";
}
}
return true;
}
}
// Initialize messages
$display = new showMessage();
/**
* Class Order
*
* Handles the orders and billing
*/
class Order {
public $id;
public $customerid;
public $customer;
public $products;
public $date;
// tryOrder tries to remove the the items of an order from the database.
// If tryOrder fails the user will be informed.
static function tryOrder($products, $quantities, $names) {
global $db;
$updateQuery = "UPDATE Products SET stock=:value WHERE id=:id";
$_SESSION['errorProducts'] = Array();
for ($i = 0; $i < sizeof($products); $i++) {
$query = $db->query("SELECT stock FROM Products "
. "WHERE id=$products[$i]");
$query->setFetchMode(PDO::FETCH_ASSOC);
$row = $query->fetch();
$stock = $row['stock'];
$prodID = $products[$i];
$ProdQuantity = $stock - $quantities[$i];
$query = $db->prepare($updateQuery);
$query->bindParam(':value', $ProdQuantity);
$query->bindParam(':id', $prodID);
if (!(($stock - $quantities[$i]) >= 0) || !$query->execute()) {
array_push($_SESSION['errorProducts'], $names[$i]);
$_SESSION['dbPullSuccess'] = false;
}
if ($i == (sizeof($products) - 1) &&
!isset($_SESSION['dbPullSuccess'])) {
$_SESSION['dbPullSuccess'] = true;
}
}
}
// executeOrder inserts the order into the database
static function executeOrder($userID,
$productIDs,
$quantities,
$prices,
$names,
$payment_method) {
global $db;
$query = $db->prepare("INSERT INTO Orders (customerid, payment_method) "
. "VALUES (:userID, :payment_method)");
$query->bindParam(':userID', $userID, PDO::PARAM_INT);
$query->bindParam(':payment_method', $payment_method, PDO::PARAM_STR);
$query->execute();
$result = $db->query("SELECT MAX(id) FROM Orders WHERE "
. "customerid=$userID");
$result->setFetchMode(PDO::FETCH_ASSOC);
$resultArray = $result->fetch();
$inserted_id = end($resultArray);
foreach ($productIDs as $index => $order_product) {
$query = $db->prepare("INSERT INTO Orders_Products "
. "(OrderID, productID, quantity,product_name,price) "
. "VALUES (:OrderID, :productID,:quantity, "
. ":product_name,:price)");
$query->bindParam(":OrderID", $inserted_id, PDO::PARAM_INT);
$query->bindParam(":productID", $order_product, PDO::PARAM_INT);
$query->bindParam(":quantity", $quantities[$index], PDO::PARAM_STR);
$query->bindParam(":price", $prices[$index], PDO::PARAM_INT);
$query->bindParam(":product_name", $names[$index], PDO::PARAM_STR);
$query->execute();
}
}
// when given an array of productID's getProductNames returns an array
// containing the product names of those id's
static function getProductNames($IDarray) {
global $db;
$namesArray = Array();
foreach ($IDarray as $ProductID) {
$query = $db->query("SELECT name FROM Products "
. "WHERE id=$ProductID LIMIT 1");
$query->setFetchMode(PDO::FETCH_NUM);
$resultArray = $query->fetch();
$productName = end($resultArray);
array_push($namesArray, $productName);
}
return $namesArray;
}
// when given an array of productID's getProductPrices returns an array
// containing the product prices of those id's
static function getProductPrices($IDarray) {
global $db;
$pricesArray = Array();
foreach ($IDarray as $ProductID) {
$query = $db->query("SELECT price FROM Products "
. "WHERE id=$ProductID LIMIT 1");
$query->setFetchMode(PDO::FETCH_ASSOC);
$resultArray = $query->fetch();
$productPrice = end($resultArray);
array_push($pricesArray, $productPrice);
}
return $pricesArray;
}
// shows a list of all the orders that a customer has made
static function show_list_orders() {
global $db;
global $customer;
$query = $db->prepare("SELECT id FROM Orders WHERE "
. "customerid=:id ORDER BY id DESC");
$query->bindParam(':id', $customer->id, PDO::PARAM_INT);
$query->execute();
$ordersArray = $query->fetchAll();
for ($i = 0; $i < sizeof($ordersArray); $i++) {
echo "<form method='post' action='customer_orders.php'>"
. "<input type='submit' "
. "name='order_number' value=" . $ordersArray[$i][0] . ">"
. "</form>";
}
}
// shows a single order
static function show_order($orderID) {
global $db;
global $customer;
$query = $db->query("SELECT customerid FROM Orders WHERE id=$orderID");
$row = $query->fetch();
$customerID = $row['customerid'];