1
+ <?php
2
+ session_start ();
3
+
4
+ if ($ _SESSION ['role ' ] != 'A ' ) {
5
+ header ('Location: / ' );
6
+ exit ();
7
+ }
8
+
9
+ require_once ('../../../helpers/config.php ' );
10
+ require_once ('../../../helpers/connection.php ' );
11
+ require_once ('../../../helpers/utils.php ' );
12
+
13
+ if ($ _SERVER ['REQUEST_METHOD ' ] === 'POST ' ) {
14
+ try {
15
+ if ($ _POST ['token ' ] != $ _SESSION ['editevent ' ]) {
16
+ unset($ _SESSION ['editevent ' ]);
17
+ header ('Location: ./index.php?error=Invalid Token ' );
18
+ exit ();
19
+ }
20
+
21
+ $ datetime = $ _POST ['date ' ].' ' .$ _POST ['time ' ];
22
+ if (strlen ($ _POST ['time ' ]) == 5 ) {
23
+ $ datetime = $ _POST ['date ' ].' ' .$ _POST ['time ' ].':00 ' ;
24
+ }
25
+
26
+ if (empty ($ _POST ['title ' ])) {
27
+ $ _GET ['error ' ] = "Title can not be empty " ;
28
+ } else if (empty ($ _POST ['date ' ])) {
29
+ $ _GET ['error ' ] = "Date can not be empty " ;
30
+ } else if (empty ($ _POST ['speaker ' ])) {
31
+ $ _GET ['error ' ] = "Speaker can not be empty " ;
32
+ } else if (empty ($ _POST ['number_of_participant ' ])) {
33
+ $ _GET ['error ' ] = "Number of Participant can not be empty " ;
34
+ } else if (!preg_match ("/^[-:a-zA-Z-' ]*$/ " ,$ _POST ['title ' ])) {
35
+ $ _GET ['error ' ] = "Title only letters and white space allowed " ;
36
+ } else if (!validateDate ($ datetime , 'Y-m-d H:i:s ' )) {
37
+ $ _GET ['error ' ] = "Please supply valid date " ;
38
+ } else if (!preg_match ("/^[a-zA-Z-' ]*$/ " ,$ _POST ['speaker ' ])) {
39
+ $ _GET ['error ' ] = "Speaker only letters and white space allowed " ;
40
+ } else if (!is_numeric ($ _POST ['number_of_participant ' ])) {
41
+ $ _GET ['error ' ] = "Number of participant must be numeric " ;
42
+ } else {
43
+ $ query = 'UPDATE events SET title=?, description=?, date=?, speaker=?, number_of_participant=?, updated_by=?, updated_at=NOW() WHERE id=UUID_TO_BIN(?) ' ;
44
+ $ stmt = $ db ->prepare ($ query );
45
+ $ stmt ->bind_param ("ssssdds " , $ _POST ['title ' ], $ _POST ['description ' ], $ datetime , $ _POST ['speaker ' ], $ _POST ['number_of_participant ' ], $ _SESSION ['userid ' ], $ _POST ['id ' ]);
46
+ $ stmt ->execute ();
47
+ $ stmt ->close ();
48
+ $ db -> close ();
49
+ unset($ _SESSION ['editevent ' ]);
50
+ header ('Location: ./index.php?message=Event berhasil diupdate ' );
51
+ exit ();
52
+ }
53
+
54
+ } catch (Exception $ e ) {
55
+ unset($ _SESSION ['editevent ' ]);
56
+ header ('Location: ./index.php?error=Event gagal diupdate: ' . $ e ->getMessage ());
57
+ exit ();
58
+ }
59
+ } else {
60
+ try {
61
+ $ query = 'SELECT BIN_TO_UUID(id) as id, title, date, description, speaker, number_of_participant FROM events WHERE id = UUID_TO_BIN(?) ' ;
62
+ $ stmt = $ db ->prepare ($ query );
63
+ $ stmt ->bind_param ("s " , $ _GET ['id ' ]);
64
+ $ stmt ->execute ();
65
+
66
+ $ result = $ stmt ->get_result ();
67
+ $ stmt ->close ();
68
+ $ data = $ result -> fetch_assoc ();
69
+ if (!$ data ) {
70
+ unset($ _SESSION ['editevent ' ]);
71
+ header ('Location: ./index.php?error=Invalid ID event ' );
72
+ exit ();
73
+ }
74
+ $ result -> free_result ();
75
+ $ db -> close ();
76
+
77
+ if (!empty ($ data ['date ' ])) {
78
+ $ dates = explode (' ' , $ data ['date ' ]);
79
+ $ data ['date ' ] = $ dates [0 ];
80
+ $ data ['time ' ] = $ dates [1 ];
81
+ }
82
+
83
+ } catch (Exception $ e ) {
84
+ unset($ _SESSION ['editevent ' ]);
85
+ header ('Location: ./index.php?error=Invalid ID Event ' );
86
+ exit ();
87
+ }
88
+ }
89
+
90
+ $ datetime = new DateTime ();
91
+ $ _SESSION ['editevent ' ] = $ datetime ->getTimestamp ();
92
+
93
+ include ('edit_view.php ' );
0 commit comments