Skip to content
This repository was archived by the owner on Oct 17, 2021. It is now read-only.

Commit cc83bf8

Browse files
committed
weekday repeating events
1 parent b9733ab commit cc83bf8

File tree

4 files changed

+141
-51
lines changed

4 files changed

+141
-51
lines changed

addEvent-json.php

Lines changed: 107 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,120 @@
1-
<?php
2-
3-
//echo $_POST['title'];
4-
if (isset($_POST['title']) && isset($_POST['description']) && isset($_POST['start']) && isset($_POST['end']) && isset($_POST['color'])){
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+
}
535

36+
// get date info
637
$jsonString = file_get_contents('json/events.json');
738
$data = json_decode($jsonString, true);
8-
9-
$last_item = end($data);
39+
// get, set id, rid
40+
$last_item = end($data);
1041
$last_item_id = $last_item['id'];
1142
$last_item_rid = $last_item['rid'];
12-
43+
// split start, end into date, time
1344
$start = explode(" ", $_POST['start']);
1445
$end = explode(" ", $_POST['end']);
15-
if($start[1] == '00:00:00') {
16-
$_POST['start'] = $start[0];
17-
}
18-
if($end[1] == '00:00:00') {
19-
$_POST['end'] = $end[0];
20-
}
21-
22-
for ($x = 0; $x < $_POST['count']; $x++) {
23-
if ($x > 0) {
24-
$start[0] = date('Y-m-d', strtotime($start[0] . " + 7 day"));
25-
$_POST['start'] = $start[0] . " " . $start[1];
26-
$end[0] = date('Y-m-d', strtotime($end[0] . " + 7 day"));
27-
$_POST['end'] = $end[0] . " " . $end[1];
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
2869
if($start[1] == '00:00:00') {
29-
$_POST['start'] = $start[0];
70+
$_POST['start'] = $date;
3071
}
3172
if($end[1] == '00:00:00') {
32-
$_POST['end'] = $end[0];
33-
}
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);
3490
}
35-
$extra = array(
36-
'id' => ++$last_item_id,
37-
'rid' => $last_item_rid+1,
38-
'repeat' => $_POST['repeat'],
39-
'title' => $_POST['title'],
40-
'description' => $_POST['description'],
41-
'start' => $_POST['start'],
42-
'end' => $_POST['end'],
43-
'color' => $_POST['color'],
44-
);
45-
$data[] = $extra;
46-
$newJsonString = json_encode($data);
47-
file_put_contents('json/events.json', $newJsonString);
4891
}
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+
}
49117
}
50-
header('Location: '.$_SERVER['HTTP_REFERER']);
51-
52-
?>
53-
118+
// back to fullCalendar
119+
header('Location: '.$_SERVER['HTTP_REFERER']);
120+
?>

index-json.php

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@
105105
</div>
106106
<!-- /.row -->
107107
<!-- Modal -->
108-
<div class="modal fade" id="ModalAdd" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
108+
<div class="modal fade" id="ModalAdd" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
109109
<div class="modal-dialog" role="document">
110110
<div class="modal-content">
111111
<form class="form-horizontal" method="POST" action="addEvent-json.php">
@@ -152,15 +152,38 @@
152152
</select>
153153
</div>
154154
</div>
155-
<div class="form-group" id="repeat-form">
156-
<label for="count" class="col-sm-4 control-label"># of Repeats</label>
155+
<div class="form-check repeat-form">
156+
<input name="dowID[]" class="form-check-input" type="checkbox" value="0" id="monday">
157+
<label class="form-check-label" for="defaultCheck1">Monday</label>
158+
</div>
159+
<div class="form-check repeat-form">
160+
<input name="dowID[]" class="form-check-input" type="checkbox" value="1" id="tuesday">
161+
<label class="form-check-label" for="defaultCheck1">Tuesday</label>
162+
</div>
163+
<div class="form-check repeat-form">
164+
<input name="dowID[]" class="form-check-input" type="checkbox" value="2" id="wednesday">
165+
<label class="form-check-label" for="defaultCheck1">Wednesday</label>
166+
</div>
167+
<div class="form-check repeat-form">
168+
<input name="dowID[]" class="form-check-input" type="checkbox" value="3" id="thursday">
169+
<label class="form-check-label" for="defaultCheck1">Thursday</label>
170+
</div>
171+
<div class="form-check repeat-form">
172+
<input name="dowID[]" class="form-check-input" type="checkbox" value="4" id="friday">
173+
<label class="form-check-label" for="defaultCheck1">Friday</label>
174+
</div>
175+
<div class="form-check repeat-form">
176+
<input name="dowID[]" class="form-check-input" type="checkbox" value="5" id="saturday">
177+
<label class="form-check-label" for="defaultCheck1">Saturday</label>
178+
</div>
179+
<div class="form-check repeat-form">
180+
<input name="dowID[]" class="form-check-input" type="checkbox" value="6" id="sunday">
181+
<label class="form-check-label" for="defaultCheck1">Sunday</label>
182+
</div>
183+
<div class="form-group">
184+
<label for="endDate" class="col-sm-6 control-label">End Date</label>
157185
<div class="col-sm-10">
158-
<select name="count" class="form-control" id="count">
159-
<option value="1">1</option>
160-
<option value="2">2</option>
161-
<option value="3">3</option>
162-
<option value="4">4</option>
163-
</select>
186+
<input type="text" name="endDate" class="form-control" id="endDate" value="2019-01-31" placeholder="2019-01-31">
164187
</div>
165188
</div>
166189
<script>

json/events.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"0":{"id":1,"rid":1,"repeat":"no","title":"All Day Event","description":"some text for all day event","start":"2019-01-01","end":"2019-01-02","color":"#40E0D0"},"1":{"id":2,"rid":2,"repeat":"no","title":"Long Event","description":"some text for long event","start":"2019-01-07","end":"2019-01-10","color":"#FF0000"},"2":{"id":3,"rid":3,"repeat":"no","title":"Conference","description":"some text for conference","start":"2019-01-10","end":"2019-01-12","color":"#40E0D0"},"3":{"id":4,"rid":4,"repeat":"no","title":"Meeting","description":"some text for meeting","start":"2019-01-11 10:30:00","end":"2019-01-11 12:30:00","color":"#000"},"4":{"id":5,"rid":5,"repeat":"no","title":"Lunch","description":"some text for lunch","start":"2019-01-11 12:00:00","end":"2019-01-11 14:00:00","color":"#0071c5"},"5":{"id":6,"rid":6,"repeat":"no","title":"Short Event","description":"some text for short event","start":"2019-01-09 16:00:00","end":"2019-01-09 16:30:00","color":"#0071c5"},"6":{"id":7,"rid":7,"repeat":"no","title":"Dinner","description":"some text for dinner","start":"2019-01-11 16:00:00","end":"2019-01-11 17:30:00","color":"#0071c5"},"7":{"id":8,"rid":8,"repeat":"no","title":"Happy Hour","description":"some text for happy hour","start":"2019-01-11 17:30:00","end":"2019-01-11 19:30:00","color":"#0071c5"},"8":{"id":9,"rid":9,"repeat":"no","title":"Birthday Party","description":"some text for birthday party","start":"2019-01-13 09:00:00","end":"2019-01-13 12:00:00","color":"#FFD700"},"9":{"id":"10","rid":10,"repeat":"no","title":"Vacation","description":"some text for vacation","start":"2019-01-25","end":"2019-01-28","color":"#0071c5"},"10":{"id":11,"rid":11,"repeat":"no","title":"Double click to change","description":"some text for double click","start":"2019-01-22","end":"2019-01-23","color":"#000"},"11":{"id":12,"rid":12,"repeat":"no","title":"Shopping","description":"some text for shopping","start":"2019-01-31 17:30:00","end":"2019-01-31 18:30:00","color":"#FF8C00"},"12":{"id":13,"rid":13,"repeat":"yes","title":"Repeating Event","description":"You can change a series occurrence","start":"2019-01-03 20:00:00","end":"2019-01-03 20:30:00","color":"#000"},"13":{"id":14,"rid":13,"repeat":"yes","title":"Repeating Event","description":"This is a repeating event","start":"2019-01-10 20:00:00","end":"2019-01-10 20:30:00","color":"#008000"},"14":{"id":15,"rid":13,"repeat":"yes","title":"Repeating Event","description":"This is a repeating event","start":"2019-01-17 20:00:00","end":"2019-01-17 20:30:00","color":"#008000"},"16":{"id":17,"rid":14,"repeat":"yes","title":"All Day Repeating Event","description":"all day repeating event","start":"2019-01-14","end":"2019-01-15","color":"#FFD700"},"17":{"id":18,"rid":14,"repeat":"yes","title":"All Day Repeating Event","description":"all day repeating event","start":"2019-01-21","end":"2019-01-22","color":"#FFD700"}}
1+
[{"id":1,"rid":1,"repeat":"no","title":"All Day Event","description":"some text for all day event","start":"2019-01-01","end":"2019-01-02","color":"#40E0D0"},{"id":2,"rid":2,"repeat":"no","title":"Long Event","description":"some text for long event","start":"2019-01-07","end":"2019-01-10","color":"#FF0000"},{"id":3,"rid":3,"repeat":"no","title":"Conference","description":"some text for conference","start":"2019-01-10","end":"2019-01-12","color":"#40E0D0"},{"id":4,"rid":4,"repeat":"no","title":"Meeting","description":"some text for meeting","start":"2019-01-11 10:30:00","end":"2019-01-11 12:30:00","color":"#000"},{"id":5,"rid":5,"repeat":"no","title":"Lunch","description":"some text for lunch","start":"2019-01-11 12:00:00","end":"2019-01-11 14:00:00","color":"#0071c5"},{"id":6,"rid":6,"repeat":"no","title":"Short Event","description":"some text for short event","start":"2019-01-09 16:00:00","end":"2019-01-09 16:30:00","color":"#0071c5"},{"id":7,"rid":7,"repeat":"no","title":"Dinner","description":"some text for dinner","start":"2019-01-11 16:00:00","end":"2019-01-11 17:30:00","color":"#0071c5"},{"id":8,"rid":8,"repeat":"no","title":"Happy Hour","description":"some text for happy hour","start":"2019-01-11 17:30:00","end":"2019-01-11 19:30:00","color":"#0071c5"},{"id":9,"rid":9,"repeat":"no","title":"Birthday Party","description":"some text for birthday party","start":"2019-01-13 09:00:00","end":"2019-01-13 12:00:00","color":"#FFD700"},{"id":10,"rid":10,"repeat":"no","title":"Vacation","description":"some text for vacation","start":"2019-01-18","end":"2019-01-21","color":"#008000"},{"id":11,"rid":11,"repeat":"no","title":"Double click to change","description":"some text for double click","start":"2019-01-22","end":"2019-01-23","color":"#000"},{"id":12,"rid":12,"repeat":"no","title":"Shopping","description":"some text for shopping","start":"2019-01-31 17:30:00","end":"2019-01-31 18:30:00","color":"#FF8C00"},{"id":13,"rid":13,"repeat":"yes","title":"Repeating Event","description":"This is a repeating event","start":"2019-01-03 14:30:00","end":"2019-01-03 15:00:00","color":"#0071c5"},{"id":14,"rid":13,"repeat":"yes","title":"Repeating Event","description":"This is a repeating event","start":"2019-01-10 14:30:00","end":"2019-01-10 15:00:00","color":"#0071c5"},{"id":15,"rid":13,"repeat":"yes","title":"Repeating Event","description":"This repeating event occurrence is different","start":"2019-01-17 14:30:00","end":"2019-01-17 15:00:00","color":"#FF0000"},{"id":16,"rid":13,"repeat":"yes","title":"Repeating Event","description":"This is a repeating event","start":"2019-01-24 14:30:00","end":"2019-01-24 15:00:00","color":"#0071c5"},{"id":17,"rid":13,"repeat":"yes","title":"Repeating Event","description":"This is a repeating event","start":"2019-01-05 14:30:00","end":"2019-01-05 15:00:00","color":"#0071c5"},{"id":18,"rid":13,"repeat":"yes","title":"Repeating Event","description":"This is a repeating event","start":"2019-01-12 14:30:00","end":"2019-01-12 15:00:00","color":"#0071c5"},{"id":19,"rid":13,"repeat":"yes","title":"Repeating Event","description":"This is a repeating event","start":"2019-01-19 14:30:00","end":"2019-01-19 15:00:00","color":"#0071c5"},{"id":20,"rid":14,"repeat":"yes","title":"Repeating All Day Event","description":"Repeating event","start":"2019-01-14","end":"2019-01-14","color":"#FF8C00"},{"id":21,"rid":14,"repeat":"yes","title":"Repeating All Day Event","description":"Repeating event","start":"2019-01-21","end":"2019-01-21","color":"#FF8C00"}]

test.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ function getDatesInRange($dateFromString, $dateToString, $dow, $dowNum) {
3838
);
3939

4040
$num = 0;
41-
$post=array('1'=>'0','2'=>'2','3'=>'4');
41+
$post=array('1'=>'0','2'=>'2');
4242
foreach ($post as $p) {
4343
$dowNum = $postDates[$p][1];
4444
$dow = $postDates[$p][2];

0 commit comments

Comments
 (0)