1
- <?php
1
+ <?php
2
+ // if post
3
+ if (isset ($ _POST ['title ' ])){
4
+ // weekday array
5
+ $ postDates = array (
6
+ array ('M ' ,1 ,'monday ' ),
7
+ array ('T ' ,2 ,'tuesday ' ),
8
+ array ('W ' ,3 ,'wednesday ' ),
9
+ array ('R ' ,4 ,'thursday ' ),
10
+ array ('F ' ,5 ,'friday ' ),
11
+ array ('S ' ,6 ,'saturday ' ),
12
+ array ('U ' ,7 ,'sunday ' ),
13
+ );
14
+ // compute days in range
15
+ function getDatesInRange ($ dateFromString , $ dateToString , $ dow , $ dowNum ) {
16
+ $ dateFrom = new \DateTime ($ dateFromString );
17
+ $ dateTo = new \DateTime ($ dateToString );
18
+ // array of dates
19
+ $ dates = [];
20
+ // return empty if dateFrom > dateTo
21
+ if ($ dateFrom > $ dateTo ) {
22
+ return $ dates ;
23
+ }
24
+ // get next closest dow if dowNUM != dateFrom
25
+ if ($ dowNum != $ dateFrom ->format ('N ' )) {
26
+ $ dateFrom ->modify ('next ' . $ dow );
27
+ }
28
+ // if dateFrom <= dateTo, modify by 1 week
29
+ while ($ dateFrom <= $ dateTo ) {
30
+ $ dates [] = $ dateFrom ->format ('Y-m-d ' );
31
+ $ dateFrom ->modify ('+1 week ' );
32
+ }
33
+ return $ dates ;
34
+ }
2
35
3
- //echo $_POST['title'];
4
- if (isset ($ _POST ['title ' ]) && isset ($ _POST ['description ' ]) && isset ($ _POST ['start ' ]) && isset ($ _POST ['end ' ]) && isset ($ _POST ['color ' ])){
5
-
6
- $ jsonString = file_get_contents ('json/events.json ' );
7
- $ data = json_decode ($ jsonString , true );
8
-
9
- $ last_item = end ($ data );
10
- $ last_item_id = $ last_item ['id ' ];
11
-
12
- $ start = explode (" " , $ _POST ['start ' ]);
13
- $ end = explode (" " , $ _POST ['end ' ]);
14
- if ($ start [1 ] == '00:00:00 ' ){
15
- $ _POST ['start ' ] = $ start [0 ];
16
- }
17
- if ($ end [1 ] == '00:00:00 ' ){
18
- $ _POST ['end ' ] = $ end [0 ];
19
- }
20
-
21
-
22
- $ extra = array (
23
- 'id ' => ++$ last_item_id ,
24
- 'title ' => $ _POST ['title ' ],
25
- 'description ' => $ _POST ['description ' ],
26
- 'start ' => $ _POST ['start ' ],
27
- 'end ' => $ _POST ['end ' ],
28
- 'color ' => $ _POST ['color ' ],
29
- );
30
-
31
- $ data [] = $ extra ;
32
-
33
- $ newJsonString = json_encode ($ data );
34
- file_put_contents ('json/events.json ' , $ newJsonString );
36
+ // get date info
37
+ $ jsonString = file_get_contents ('json/events.json ' );
38
+ $ data = json_decode ($ jsonString , true );
39
+ // get, set id, rid
40
+ $ last_item = end ($ data );
41
+ $ last_item_id = $ last_item ['id ' ];
42
+ $ last_item_rid = $ last_item ['rid ' ];
43
+ // split start, end into date, time
44
+ $ start = explode (" " , $ _POST ['start ' ]);
45
+ $ end = explode (" " , $ _POST ['end ' ]);
35
46
47
+ // if event is recurrence
48
+ if ($ _POST ['repeat ' ] == 'yes ' ) {
49
+ // initialize vars
50
+ $ dateFromString = $ start [0 ];
51
+ $ endDate = $ _POST ['endDate ' ];
52
+ $ dateToString = $ endDate ;
53
+ // for each dow
54
+ foreach ($ _POST ['dowID ' ] as $ key => $ value ) {
55
+ // initialize vars
56
+ $ dowNum = $ postDates [$ value ][1 ];
57
+ $ dow = $ postDates [$ value ][2 ];
58
+ // call days in range function
59
+ $ dates = getDatesInRange ($ dateFromString , $ dateToString , $ dow , $ dowNum );
60
+ // loop each day for dow
61
+ $ counter = count ($ dates );
62
+ for ($ x = 0 ; $ x < $ counter ; $ x ++) {
63
+ // date from array dates
64
+ $ date = $ dates [$ x ];
65
+ // set recurrence date, time for each date
66
+ $ _POST ['start ' ] = $ date . " " . $ start [1 ];
67
+ $ _POST ['end ' ] = $ date . " " . $ end [1 ];
68
+ // if all day event, set the date only
69
+ if ($ start [1 ] == '00:00:00 ' ) {
70
+ $ _POST ['start ' ] = $ date ;
71
+ }
72
+ if ($ end [1 ] == '00:00:00 ' ) {
73
+ $ _POST ['end ' ] = $ date ;
74
+ }
75
+ // add date to array
76
+ $ extra = array (
77
+ 'id ' => ++$ last_item_id ,
78
+ 'rid ' => $ last_item_rid +1 ,
79
+ 'repeat ' => $ _POST ['repeat ' ],
80
+ 'title ' => $ _POST ['title ' ],
81
+ 'description ' => $ _POST ['description ' ],
82
+ 'start ' => $ _POST ['start ' ],
83
+ 'end ' => $ _POST ['end ' ],
84
+ 'color ' => $ _POST ['color ' ],
85
+ );
86
+ // save date info
87
+ $ data [] = $ extra ;
88
+ $ newJsonString = json_encode ($ data );
89
+ file_put_contents ('json/events.json ' , $ newJsonString );
90
+ }
91
+ }
92
+ // if single event
93
+ } else {
94
+ // if all day event, set the date only
95
+ if ($ start [1 ] == '00:00:00 ' ) {
96
+ $ _POST ['start ' ] = $ start [0 ];
97
+ }
98
+ if ($ end [1 ] == '00:00:00 ' ) {
99
+ $ _POST ['end ' ] = $ end [0 ];
100
+ }
101
+ // add date to array
102
+ $ extra = array (
103
+ 'id ' => ++$ last_item_id ,
104
+ 'rid ' => $ last_item_rid +1 ,
105
+ 'repeat ' => $ _POST ['repeat ' ],
106
+ 'title ' => $ _POST ['title ' ],
107
+ 'description ' => $ _POST ['description ' ],
108
+ 'start ' => $ _POST ['start ' ],
109
+ 'end ' => $ _POST ['end ' ],
110
+ 'color ' => $ _POST ['color ' ],
111
+ );
112
+ // save date info
113
+ $ data [] = $ extra ;
114
+ $ newJsonString = json_encode ($ data );
115
+ file_put_contents ('json/events.json ' , $ newJsonString );
116
+ }
36
117
}
37
- header ('Location: ' .$ _SERVER ['HTTP_REFERER ' ]);
38
-
39
- ?>
40
-
118
+ // back to fullCalendar
119
+ header ('Location: ' .$ _SERVER ['HTTP_REFERER ' ]);
120
+ ?>
0 commit comments