-
-
Notifications
You must be signed in to change notification settings - Fork 142
/
calendar.php
371 lines (341 loc) · 14.6 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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
<?php
require_once 'includes/header.php';
function getPriceConverted($price, $currency, $database, $userId)
{
$query = "SELECT rate FROM currencies WHERE id = :currency AND user_id = :userId";
$stmt = $database->prepare($query);
$stmt->bindParam(':currency', $currency, SQLITE3_INTEGER);
$stmt->bindParam(':userId', $userId, SQLITE3_INTEGER);
$result = $stmt->execute();
$exchangeRate = $result->fetchArray(SQLITE3_ASSOC);
if ($exchangeRate === false) {
return $price;
} else {
$fromRate = $exchangeRate['rate'];
return $price / $fromRate;
}
}
$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;
}
$currenciesInUse = [];
$numberOfSubscriptionsToPayThisMonth = 0;
$totalCostThisMonth = 0;
$amountDueThisMonth = 0;
$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;
$currenciesInUse[] = $row['currency_id'];
}
$currenciesInUse = array_unique($currenciesInUse);
$usesMultipleCurrencies = count($currenciesInUse) > 1;
$showCantConverErrorMessage = false;
if ($usesMultipleCurrencies) {
$query = "SELECT api_key FROM fixer WHERE user_id = :userId";
$stmt = $db->prepare($query);
$stmt->bindValue(':userId', $userId, SQLITE3_INTEGER);
$result = $stmt->execute();
if ($result->fetchArray(SQLITE3_ASSOC) === false) {
$showCantConverErrorMessage = true;
}
}
// Get code of main currency to display on statistics
$query = "SELECT c.code
FROM currencies c
INNER JOIN user u ON c.id = u.main_currency
WHERE u.id = :userId";
$stmt = $db->prepare($query);
$stmt->bindValue(':userId', $userId, SQLITE3_INTEGER);
$result = $stmt->execute();
$row = $result->fetchArray(SQLITE3_ASSOC);
$code = $row['code'];
$yearsToLoad = $calendarYear - $currentYear + 1;
?>
<section class="contain">
<?php
if ($showCantConverErrorMessage) {
?>
<div class="error-box">
<div class="error-message">
<i class="fa-solid fa-exclamation-circle"></i>
<?= translate('cant_convert_currency', $i18n) ?>
</div>
</div>
<?php
}
?>
<div class="split-header">
<h2>
Calendar
<button class="button export-ical" onClick="showExportPopup()" title="<?= translate('export_icalendar', $i18n) ?>">
<?php require_once 'images/siteicons/svg/export_ical.php'; ?>
</button>
</h2>
<div id="subscriptions_calendar" class="subscription-modal">
<div class="modal-header">
<h3><?= translate('export_icalendar', $i18n) ?></h3>
<span class="fa-solid fa-xmark close-modal" onclick="closePopup()"></span>
</div>
<div class="form-group-inline">
<input id="iCalendarUrl" type="text" value="" readonly>
<input type="hidden" id="apiKey" value="<?= $userData['api_key'] ?>">
<button onclick="copyToClipboard()" class="button tiny"> <?= translate('copy_to_clipboard', $i18n) ?> </button>
</div>
</div>
<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) {
$totalCostThisMonth += getPriceConverted($subscription['price'], $subscription['currency_id'], $db, $userId);
$numberOfSubscriptionsToPayThisMonth++;
if ($date > $today) {
$amountDueThisMonth += getPriceConverted($subscription['price'], $subscription['currency_id'], $db, $userId);
}
?>
<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) {
$totalCostThisMonth += getPriceConverted($subscription['price'], $subscription['currency_id'], $db, $userId);
$numberOfSubscriptionsToPayThisMonth++;
if ($date > $today) {
$amountDueThisMonth += getPriceConverted($subscription['price'], $subscription['currency_id'], $db, $userId);
}
?>
<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>
<div class="calendar-monthly-stats">
<div class="calendar-monthly-stats-header">
<h3><?= translate("stats", $i18n) ?></h3>
</div>
<div class="statistics">
<div class="statistic">
<span>
<?= $numberOfSubscriptionsToPayThisMonth ?></span>
<div class="title"><?= translate("active_subscriptions", $i18n) ?></div>
</div>
<div class="statistic">
<span><?= CurrencyFormatter::format($totalCostThisMonth, $code) ?></span>
<div class="title">Total cost</div>
</div>
<div class="statistic">
<span><?= CurrencyFormatter::format($amountDueThisMonth, $code) ?></span>
<div class="title"><?= translate("amount_due", $i18n) ?></div>
</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';
?>