1+ <?php
2+ require_once ("config.php " );
3+
4+ function read ($ db , $ requestParams ){
5+ $ queryParams = [];
6+ $ queryText = "SELECT * FROM `recurring_events` " ;
7+ if (isset ($ requestParams ["from " ]) && isset ($ requestParams ["to " ])) {
8+ $ queryText .= " WHERE `end_date`>=? AND `start_date` < ?; " ;
9+ $ queryParams = [$ requestParams ["from " ], $ requestParams ["to " ]];
10+ }
11+ $ query = $ db ->prepare ($ queryText );
12+ $ query ->execute ($ queryParams );
13+ $ events = $ query ->fetchAll (PDO ::FETCH_ASSOC );
14+ foreach ($ events as $ index =>$ event ){
15+ $ events [$ index ]["text " ] = htmlentities ($ event ["text " ]);
16+ }
17+ return $ events ;
18+ }
19+
20+ function create ($ db , $ event ){
21+ $ queryText = "INSERT INTO `recurring_events` SET
22+ `start_date`=?,
23+ `end_date`=?,
24+ `text`=?,
25+
26+ `event_pid`=?,
27+ `event_length`=?,
28+ `rec_type`=? " ;
29+ $ queryParams = [
30+ $ event ["start_date " ],
31+ $ event ["end_date " ],
32+ $ event ["text " ],
33+ // recurring events columns
34+ $ event ["event_pid " ] ? $ event ["event_pid " ] : 0 ,
35+ $ event ["event_length " ] ? $ event ["event_length " ] : 0 ,
36+ $ event ["rec_type " ]
37+ ];
38+
39+ $ query = $ db ->prepare ($ queryText );
40+ $ query ->execute ($ queryParams );
41+
42+ return $ db ->lastInsertId ();
43+ }
44+
45+ function update ($ db , $ event , $ id ){
46+ $ queryText = "UPDATE `recurring_events` SET
47+ `start_date`=?,
48+ `end_date`=?,
49+ `text`=?,
50+ `event_pid`=?,
51+ `event_length`=?,
52+ `rec_type`=?
53+ WHERE `id`=? " ;
54+
55+ $ queryParams = [
56+ $ event ["start_date " ],
57+ $ event ["end_date " ],
58+ $ event ["text " ],
59+
60+ $ event ["event_pid " ] ? $ event ["event_pid " ] : 0 ,
61+ $ event ["event_length " ] ? $ event ["event_length " ] : 0 ,
62+ $ event ["rec_type " ],//!
63+ $ id
64+ ];
65+ if ($ event ["rec_type " ] && $ event ["rec_type " ] != "none " ) {
66+ //all modified occurrences must be deleted when you update recurring series
67+ //https://docs.dhtmlx.com/scheduler/server_integration.html#savingrecurringevents
68+ $ subQueryText = "DELETE FROM `recurring_events` WHERE `event_pid`=? ; " ;
69+ $ subQuery = $ db ->prepare ($ subQueryText );
70+ $ subQuery ->execute ([$ id ]);
71+ }
72+
73+ $ query = $ db ->prepare ($ queryText );
74+ $ query ->execute ($ queryParams );
75+ }
76+
77+ function delete ($ db , $ id ){
78+ // some logic specific to recurring events support
79+ // https://docs.dhtmlx.com/scheduler/server_integration.html#savingrecurringevents
80+ $ subQueryText = "SELECT * FROM `recurring_events` WHERE id=? LIMIT 1; " ;
81+ $ subQuery = $ db ->prepare ($ subQueryText );
82+ $ subQuery ->execute ([$ id ]);
83+ $ event = $ subQuery ->fetch ();
84+
85+ if ($ event ["event_pid " ]) {
86+ // deleting a modified occurrence from a recurring series
87+ // If an event with the event_pid value was deleted - it needs updating
88+ // with rec_type==none instead of deleting.
89+ $ subQueryText ="UPDATE `recurring_events` SET `rec_type`='none' WHERE `id`=?; " ;
90+ $ subQuery = $ db ->prepare ($ subQueryText );
91+ $ subQuery ->execute ([$ id ]);
92+
93+ }else {
94+ if ($ event ["rec_type " ] && $ event ["rec_type " ] != "none " ) {//!
95+ // if a recurring series deleted, delete all modified occurrences of the series
96+ $ subQueryText = "DELETE FROM `recurring_events` WHERE `event_pid`=? ; " ;
97+ $ subQuery = $ db ->prepare ($ subQueryText );
98+ $ subQuery ->execute ([$ id ]);
99+ }
100+
101+ /*
102+ end of recurring events data processing
103+ */
104+
105+ $ queryText = "DELETE FROM `recurring_events` WHERE `id`=? ; " ;
106+ $ query = $ db ->prepare ($ queryText );
107+ $ query ->execute ([$ id ]);
108+ }
109+ }
110+
111+ try {
112+ $ db = new PDO ($ dsn , $ username , $ password , $ options );
113+ switch ($ _SERVER ["REQUEST_METHOD " ]) {
114+ case "GET " :
115+ $ result = read ($ db , $ _GET );
116+ break ;
117+ case "POST " :
118+ $ requestPayload = json_decode (file_get_contents ("php://input " ));
119+ $ id = $ requestPayload ->id ;
120+ $ action = $ requestPayload ->action ;
121+ $ body = (array ) $ requestPayload ->data ;
122+
123+ $ result = [
124+ "action " => $ action
125+ ];
126+
127+ if ($ action == "inserted " ) {
128+ $ databaseId = create ($ db , $ body );
129+ $ result ["tid " ] = $ databaseId ;
130+ // delete a single occurrence from recurring series
131+ if ($ body ["rec_type " ] === "none " ) {
132+ $ result ["action " ] = "deleted " ;//!
133+ }
134+ } elseif ($ action == "updated " ) {
135+ update ($ db , $ body , $ id );
136+ } elseif ($ action == "deleted " ) {
137+ delete ($ db , $ id );
138+ }
139+ break ;
140+ default : throw new Exception ("Unexpected Method " ); break ;
141+ }
142+ } catch (Exception $ e ) {
143+ http_response_code (500 );
144+ $ result = [
145+ "action " => "error " ,
146+ "message " => $ e ->getMessage ()
147+ ];
148+ }
149+
150+ header ("Access-Control-Allow-Origin: * " );
151+ header ("Access-Control-Allow-Methods: * " );
152+ header ("Content-Type: application/json " );
153+ echo json_encode ($ result );
0 commit comments