forked from ellite/Wallos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
calendar.php
261 lines (241 loc) · 10.4 KB
/
calendar.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
<?php
require_once 'includes/header.php';
$currentMonth = date('m');
$currentYear = date('Y');
$sameAsCurrent = false;
if ($_SERVER['REQUEST_METHOD'] === 'GET' && isset($_GET['month']) && isset($_GET['year'])) {
// Don't allow viewing past months
$selectedMonth = str_pad($_GET['month'], 2, '0', STR_PAD_LEFT);
$selectedYear = $_GET['year'];
$selectedTimestamp = strtotime($selectedYear . '-' . $selectedMonth . '-01');
$currentTimestamp = strtotime($currentYear . '-' . $currentMonth . '-01');
if ($selectedTimestamp < $currentTimestamp) {
$calendarMonth = $currentMonth;
$calendarYear = $currentYear;
} else {
$calendarMonth = $selectedMonth;
$calendarYear = $selectedYear;
}
if ($calendarMonth == $currentMonth && $calendarYear == $currentYear) {
$sameAsCurrent = true;
}
} else {
$calendarMonth = $currentMonth;
$calendarYear = $currentYear;
$sameAsCurrent = true;
}
$query = "SELECT * FROM subscriptions WHERE user_id = :user_id AND inactive = 0";
$stmt = $db->prepare($query);
$stmt->bindValue(':user_id', $userId, SQLITE3_INTEGER);
$result = $stmt->execute();
$subscriptions = [];
while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
$subscriptions[] = $row;
}
$yearsToLoad = $calendarYear - $currentYear + 1;
?>
<section class="contain">
<div class="split-header">
<h2>Calendar</h2>
<div class="calendar-nav">
<?php
if (!$sameAsCurrent) {
?>
<button class="button secondary-button tiny" onClick="currentMoth()" title="<?= translate('reset', $i18n) ?>"><i
class="fa-solid fa-calendar-day"></i></button>
<button class="button tiny" id="prev" onclick="prevMonth(<?= $calendarMonth ?>, <?= $calendarYear ?>)"><i
class="fa-solid fa-chevron-left"></i></button>
<?php
}
?>
<span id="month"><?= translate('month-' . $calendarMonth, $i18n) ?> <?= $calendarYear ?></span>
<button class="button tiny" id="next" onclick="nextMonth(<?= $calendarMonth ?>, <?= $calendarYear ?>)"><i
class="fa-solid fa-chevron-right"></i></button>
</div>
</div>
<div>
<?php
$daysInMonth = cal_days_in_month(CAL_GREGORIAN, $calendarMonth, $calendarYear);
$firstDay = mktime(0, 0, 0, $calendarMonth, 1, $calendarYear);
$firstDayOfWeek = date('N', $firstDay) - 1; // Adjusted to make Monday (1) the first day
$dayOfWeek = 0;
$day = 1;
$days = 1;
$week = 1;
$today = date('Y-m-d');
$today = explode('-', $today);
$todayYear = $today[0];
$todayMonth = $today[1];
$todayDay = $today[2];
$today = $todayYear . '-' . $todayMonth . '-' . $todayDay;
$today = strtotime($today);
?>
<div class="calendar">
<div class="calendar-header">
<div class="calendar-cell"><?= translate('mon', $i18n) ?></div>
<div class="calendar-cell"><?= translate('tue', $i18n) ?></div>
<div class="calendar-cell"><?= translate('wed', $i18n) ?></div>
<div class="calendar-cell"><?= translate('thu', $i18n) ?></div>
<div class="calendar-cell"><?= translate('fri', $i18n) ?></div>
<div class="calendar-cell"><?= translate('sat', $i18n) ?></div>
<div class="calendar-cell"><?= translate('sun', $i18n) ?></div>
</div>
<div class="calendar-body">
<div class="week calendar-row">
<?php
for ($i = 0; $i < $firstDayOfWeek; $i++) { // Fill empty cells if month doesn't start on Monday
?>
<div class="calendar-cell empty">
<div class="calendar-cell-header">
<span class="day"> </span>
</div>
<div class="calendar-cell-content"></div>
</div>
<?php
}
for ($i = $firstDayOfWeek; $i < 7; $i++) {
if ($day <= $daysInMonth) {
$dayClass = ($day == $todayDay && $calendarMonth == $todayMonth && $calendarYear == $todayYear) ? "today" : "";
?>
<div class="calendar-cell <?= $dayClass ?>">
<div class="calendar-cell-header">
<span class="day"><?= $day ?></span>
</div>
<div class="calendar-cell-content">
<?php
foreach ($subscriptions as $subscription) {
$nextPaymentDate = strtotime($subscription['next_payment']);
$cycle = $subscription['cycle']; // Integer from 1 to 4
$frequency = $subscription['frequency'];
$endDate = strtotime("+" . $yearsToLoad . " years", $nextPaymentDate);
// Determine the strtotime increment string based on cycle
switch ($cycle) {
case 1: // Days
$incrementString = "+{$frequency} days";
break;
case 2: // Weeks
$incrementString = "+{$frequency} weeks";
break;
case 3: // Months
$incrementString = "+{$frequency} months";
break;
case 4: // Years
$incrementString = "+{$frequency} years";
break;
default:
$incrementString = "+{$frequency} months"; // Default case, if needed
}
// Calculate the start of the month
$startOfMonth = strtotime($calendarYear . '-' . str_pad($calendarMonth, 2, '0', STR_PAD_LEFT) . '-01');
// Find the first payment date of the month by moving backwards
$startDate = $nextPaymentDate;
while ($startDate > $startOfMonth) {
$startDate = strtotime("-" . $incrementString, $startDate);
}
for ($date = $startDate; $date <= $endDate; $date = strtotime($incrementString, $date)) {
if (date('Y-m', $date) == $calendarYear . '-' . str_pad($calendarMonth, 2, '0', STR_PAD_LEFT)) {
if (date('d', $date) == $day) {
?>
<div class="calendar-subscription-title" onClick="openSubscriptionModal(<?= $subscription['id'] ?>)">
<?= htmlspecialchars($subscription['name']) ?>
</div>
<?php
}
}
}
}
?>
</div>
</div>
<?php
$day++;
}
}
while ($day <= $daysInMonth) {
if ($dayOfWeek % 7 == 0) {
?>
</div>
<div class="week calendar-row">
<?php
}
$dayClass = ($day == $todayDay && $calendarMonth == $todayMonth && $calendarYear == $todayYear) ? "today" : "";
?>
<div class="calendar-cell <?= $dayClass ?>">
<div class="calendar-cell-header">
<span class="day"><?= $day ?></span>
</div>
<div class="calendar-cell-content">
<?php
foreach ($subscriptions as $subscription) {
$nextPaymentDate = strtotime($subscription['next_payment']);
$cycle = $subscription['cycle']; // Integer from 1 to 4
$frequency = $subscription['frequency'];
$endDate = strtotime("+" . $yearsToLoad . " years", $nextPaymentDate);
// Determine the strtotime increment string based on cycle
switch ($cycle) {
case 1: // Days
$incrementString = "+{$frequency} days";
break;
case 2: // Weeks
$incrementString = "+{$frequency} weeks";
break;
case 3: // Months
$incrementString = "+{$frequency} months";
break;
case 4: // Years
$incrementString = "+{$frequency} years";
break;
default:
$incrementString = "+{$frequency} months"; // Default case, if needed
}
// Calculate the start of the month
$startOfMonth = strtotime($calendarYear . '-' . str_pad($calendarMonth, 2, '0', STR_PAD_LEFT) . '-01');
// Find the first payment date of the month by moving backwards
$startDate = $nextPaymentDate;
while ($startDate > $startOfMonth) {
$startDate = strtotime("-" . $incrementString, $startDate);
}
for ($date = $startDate; $date <= $endDate; $date = strtotime($incrementString, $date)) {
if (date('Y-m', $date) == $calendarYear . '-' . str_pad($calendarMonth, 2, '0', STR_PAD_LEFT)) {
if (date('d', $date) == $day) {
?>
<div class="calendar-subscription-title" onClick="openSubscriptionModal(<?= $subscription['id'] ?>)">
<?= $subscription['name'] ?>
</div>
<?php
}
}
}
}
?>
</div>
</div>
<?php
$day++;
$dayOfWeek++;
}
while ($dayOfWeek % 7 != 0) { // Fill the rest of the week with empty cells
?>
<div class="calendar-cell empty">
<div class="calendar-cell-header">
<span class="day"> </span>
</div>
<div class="calendar-cell-content"></div>
</div>
<?php
$dayOfWeek++;
}
?>
</div>
</div>
</div>
</section>
<div id="subscriptionModal" class="subscription-modal">
<div class="modal-content">
<div id="subscriptionModalContent"></div>
</div>
</div>
<script src="scripts/calendar.js?<?= $version ?>"></script>
<?php
require_once 'includes/footer.php';
?>