-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalendar.php
More file actions
392 lines (337 loc) · 14.5 KB
/
Copy pathcalendar.php
File metadata and controls
392 lines (337 loc) · 14.5 KB
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
<?php
$page_title = "My Calendar";
require_once __DIR__ . '/autoload.php';
use App\Core\Bootstrap;
use App\Helpers\SecurityHelper;
use App\Helpers\Layout;
Bootstrap::init();
// 2. Handle Actions (Logic BEFORE Header)
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
SecurityHelper::verifyCsrfToken($_POST['csrf_token'] ?? '');
// Permission Check
if (($_SESSION['permission'] ?? 'edit') === 'read_only') {
header("Location: calendar.php?error=Unauthorized: Read-only access");
exit();
}
if ($_POST['action'] == 'add_reminder') {
$title = $_POST['title'];
$date = $_POST['alert_date'];
$recur_type = $_POST['recurrence_type'] ?? 'none';
$color = $_POST['color'] ?? 'primary';
$stmt = $pdo->prepare("INSERT INTO reminders (user_id, tenant_id, title, alert_date, recurrence_type, color) VALUES (?, ?, ?, ?, ?, ?)");
$stmt->execute([$_SESSION['user_id'], $_SESSION['tenant_id'], $title, $date, $recur_type, $color]);
header("Location: calendar.php?success=Reminder Added");
exit;
} elseif ($_POST['action'] == 'delete_reminder') {
$id = filter_input(INPUT_POST, 'id', FILTER_VALIDATE_INT);
if ($id) {
$stmt = $pdo->prepare("DELETE FROM reminders WHERE id = ? AND tenant_id = ?");
$stmt->execute([$id, $_SESSION['tenant_id']]);
}
header("Location: calendar.php?deleted=1");
exit;
}
}
Layout::header();
Layout::sidebar();
$year = filter_input(INPUT_GET, 'year', FILTER_VALIDATE_INT) ?? date('Y');
$month = filter_input(INPUT_GET, 'month', FILTER_VALIDATE_INT) ?? date('n');
// Navigation
$prev_month = $month - 1;
$prev_year = $year;
if ($prev_month < 1) {
$prev_month = 12;
$prev_year--;
}
$next_month = $month + 1;
$next_year = $year;
if ($next_month > 12) {
$next_month = 1;
$next_year++;
}
$month_name = date('F', mktime(0, 0, 0, $month, 10));
$days_in_month = cal_days_in_month(CAL_GREGORIAN, $month, $year);
$first_day_of_week = date('N', strtotime("$year-$month-01")); // 1 (Mon) - 7 (Sun)
// 1. Fetch Events
$events = [];
// A. Subscriptions
$stmt = $pdo->prepare("SELECT id, description, amount, expense_date FROM expenses WHERE tenant_id = ? AND is_subscription = 1");
$stmt->execute([$_SESSION['tenant_id']]);
$subs = $stmt->fetchAll();
foreach ($subs as $sub) {
$day = date('d', strtotime($sub['expense_date']));
if ($day <= $days_in_month) {
$events[(int) $day][] = [
'type' => 'sub',
'title' => $sub['description'],
'amount' => number_format($sub['amount'], 2),
'color' => 'warning',
'icon' => 'fa-repeat'
];
}
}
// B. Card Bills
$stmt = $pdo->prepare("SELECT id, card_name, bank_name, bill_day FROM cards WHERE tenant_id = ? AND bill_day IS NOT NULL");
$stmt->execute([$_SESSION['tenant_id']]);
$cards = $stmt->fetchAll();
foreach ($cards as $card) {
$day = $card['bill_day'];
if ($day <= $days_in_month) {
$events[(int) $day][] = [
'type' => 'bill',
'title' => $card['bank_name'] . ' Bill',
'amount' => 'Due',
'color' => 'danger',
'icon' => 'fa-file-invoice-dollar'
];
}
}
// C. Custom Reminders
$stmt = $pdo->prepare("SELECT id, title, alert_date, recurrence_type, color FROM reminders WHERE tenant_id = ?");
$stmt->execute([$_SESSION['tenant_id']]);
$reminders = $stmt->fetchAll();
foreach ($reminders as $rem) {
$rDay = date('d', strtotime($rem['alert_date']));
$rMonth = date('n', strtotime($rem['alert_date']));
$rYear = date('Y', strtotime($rem['alert_date']));
$recur_type = $rem['recurrence_type'] ?? ($rem['is_recurring'] ? 'monthly' : 'none'); // Fallback for old rows
$should_show = false;
if ($recur_type == 'monthly') {
// Show every month on this day
if ($rDay <= $days_in_month) {
$should_show = true;
}
} elseif ($recur_type == 'yearly') {
// Show every year on this month & day
if ($rMonth == $month && $rDay <= $days_in_month) {
$should_show = true;
}
} else {
// One-time: Specific Date
if ($rMonth == $month && $rYear == $year) {
$should_show = true;
}
}
if ($should_show) {
$events[(int) $rDay][] = [
'type' => 'reminder',
'id' => $rem['id'],
'title' => $rem['title'],
'amount' => '',
'color' => $rem['color'] ?? 'primary',
'icon' => 'fa-bell'
];
}
}
?>
<div class="d-flex justify-content-between align-items-center mb-4">
<h1 class="h3 fw-bold mb-0">My Calendar</h1>
<div class="d-flex gap-2">
<?php if (($_SESSION['permission'] ?? 'edit') !== 'read_only'): ?>
<button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#addReminderModal">
<i class="fa-solid fa-plus me-2"></i> Add Reminder
</button>
<?php else: ?>
<div class="bg-light text-muted px-3 py-1 rounded-pill shadow-sm d-flex align-items-center small">
<i class="fa-solid fa-lock me-2"></i> Read Only
</div>
<?php endif; ?>
<div class="btn-group">
<a href="?month=<?php echo htmlspecialchars($prev_month); ?>&year=<?php echo htmlspecialchars($prev_year); ?>"
class="btn btn-light"><i class="fa-solid fa-chevron-left"></i></a>
<span class="btn btn-light fw-bold px-3" style="cursor: default; min-width: 140px;">
<?php echo htmlspecialchars($month_name . ' ' . $year); ?>
</span>
<a href="?month=<?php echo htmlspecialchars($next_month); ?>&year=<?php echo htmlspecialchars($next_year); ?>"
class="btn btn-light"><i class="fa-solid fa-chevron-right"></i></a>
</div>
</div>
</div>
<div class="glass-panel p-4">
<div class="calendar-wrapper">
<!-- Days Header -->
<div style="display:grid;grid-template-columns:repeat(7,minmax(0,1fr));gap:8px;min-width:560px;" class="text-center fw-bold text-muted mb-2 d-none d-md-grid">
<div>Mon</div>
<div>Tue</div>
<div>Wed</div>
<div>Thu</div>
<div>Fri</div>
<div>Sat</div>
<div>Sun</div>
</div>
<div class="calendar-grid">
<?php
// Empty slots before 1st day
for ($i = 1; $i < $first_day_of_week; $i++) {
echo '<div class="calendar-day empty d-none d-md-block"></div>';
}
// Days
for ($d = 1; $d <= $days_in_month; $d++) {
$is_today = ($d == date('j') && $month == date('n') && $year == date('Y'));
$day_events = $events[$d] ?? [];
$bg_class = $is_today ? 'bg-primary-subtle border-primary' : '';
echo "<div class='calendar-day $bg_class'>";
echo "<div class='day-number " . ($is_today ? 'text-primary fw-bold' : 'text-muted') . "'>$d</div>";
echo "<div class='d-flex flex-column gap-1'>";
foreach ($day_events as $evt) {
// If it's a reminder, we allow delete
$is_rem = ($evt['type'] === 'reminder');
echo "<div class='event-badge bg-{$evt['color']}-subtle text-{$evt['color']} small d-flex justify-content-between align-items-center' title='" . htmlspecialchars($evt['title']) . "'>";
echo "<span><i class='fa-solid {$evt['icon']} me-1'></i>" . htmlspecialchars($evt['title']) . "</span>";
if ($is_rem) {
if (($_SESSION['permission'] ?? 'edit') !== 'read_only') {
echo "<button type='button' class='btn btn-link p-0 text-danger ms-1'" .
" style='font-size: 0.9em; line-height: 1;'" .
" onclick=\"confirmDeleteReminder({$evt['id']}, '" . addslashes(htmlspecialchars($evt['title'])) . "')\">" .
" <i class='fa-solid fa-times'></i>" .
"</button>";
} else {
echo "<i class='fa-solid fa-lock text-muted x-small ms-1'></i>";
}
}
echo "</div>";
}
echo "</div>"; // End flex column
echo "</div>";
}
?>
</div>
</div><!-- end calendar-wrapper -->
</div>
<style>
.calendar-wrapper {
overflow-x: auto;
-webkit-overflow-scrolling: touch;
}
.calendar-grid {
display: grid;
grid-template-columns: repeat(7, minmax(0, 1fr));
gap: 8px;
min-width: 560px;
}
.calendar-day {
min-height: 100px;
min-width: 0;
overflow: hidden;
background: rgba(255, 255, 255, 0.5);
border-radius: 10px;
padding: 8px;
border: 1px solid rgba(255, 255, 255, 0.2);
}
.calendar-day.empty {
background: none;
border: none;
}
.day-number {
font-size: 1em;
margin-bottom: 4px;
}
.event-badge {
font-size: 0.72em;
padding: 2px 5px;
border-radius: 4px;
margin-bottom: 2px;
min-width: 0;
overflow: hidden;
}
.event-badge span {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
min-width: 0;
flex: 1;
}
@media (max-width: 768px) {
.calendar-grid {
display: flex;
flex-direction: column;
min-width: unset;
}
.calendar-day {
min-height: auto;
}
.calendar-day.empty {
display: none;
}
}
</style>
<!-- Add Reminder Modal -->
<div class="modal fade" id="addReminderModal" tabindex="-1">
<div class="modal-dialog modal-dialog-centered">
<div class="modal-content glass-panel border-0">
<div class="modal-header border-0">
<h5 class="modal-title fw-bold">Add New Reminder</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
</div>
<div class="modal-body">
<form method="POST">
<input type="hidden" name="csrf_token" value="<?php echo SecurityHelper::generateCsrfToken(); ?>">
<input type="hidden" name="action" value="add_reminder">
<div class="mb-3">
<label for="reminder_title" class="form-label">Title <span class="text-danger">*</span></label>
<input type="text" name="title" id="reminder_title" class="form-control"
placeholder="e.g. Passport Expiry" required>
</div>
<div class="mb-3">
<label for="alert_date" class="form-label">Date <span class="text-danger">*</span></label>
<input type="date" name="alert_date" id="alert_date" class="form-control" required>
</div>
<div class="mb-3">
<label for="recurrence_type" class="form-label">Recurrence</label>
<select name="recurrence_type" id="recurrence_type" class="form-select">
<option value="none">One-time (No Repeat)</option>
<option value="monthly">Monthly (Every Month on this day)</option>
<option value="yearly">Yearly (Every Year on this date)</option>
</select>
<div class="form-text small">Great for subscriptions, birthdays, or annual fees.</div>
</div>
<fieldset class="mb-4">
<legend class="form-label fs-6">Color Tag</legend>
<div class="d-flex gap-2">
<input type="radio" class="btn-check" name="color" id="c_primary" value="primary" checked>
<label class="btn btn-outline-primary btn-sm rounded-pill" for="c_primary">Blue</label>
<input type="radio" class="btn-check" name="color" id="c_danger" value="danger">
<label class="btn btn-outline-danger btn-sm rounded-pill" for="c_danger">Red</label>
<input type="radio" class="btn-check" name="color" id="c_success" value="success">
<label class="btn btn-outline-success btn-sm rounded-pill" for="c_success">Green</label>
<input type="radio" class="btn-check" name="color" id="c_warning" value="warning">
<label class="btn btn-outline-warning btn-sm rounded-pill" for="c_warning">Yellow</label>
</div>
</fieldset>
<div class="d-grid">
<button type="submit" class="btn btn-primary fw-bold">Save Reminder</button>
</div>
</form>
</div>
</div>
</div>
</div>
<!-- Delete Confirmation Modal -->
<div class="modal fade" id="deleteReminderModal" tabindex="-1">
<div class="modal-dialog modal-dialog-centered modal-sm">
<div class="modal-content glass-panel border-0">
<div class="modal-body text-center py-4">
<i class="fa-solid fa-trash-can text-danger fa-3x mb-3"></i>
<h5 class="fw-bold">Delete Reminder?</h5>
<p id="deleteReminderMsg" class="text-muted small">This cannot be undone.</p>
<form method="POST">
<input type="hidden" name="csrf_token" value="<?php echo SecurityHelper::generateCsrfToken(); ?>">
<input type="hidden" name="action" value="delete_reminder">
<input type="hidden" name="id" id="deleteReminderId">
<div class="d-flex justify-content-center gap-2 mt-3">
<button type="button" class="btn btn-light" data-bs-dismiss="modal">Cancel</button>
<button type="submit" class="btn btn-danger">Delete</button>
</div>
</form>
</div>
</div>
</div>
</div>
<script nonce="<?php echo $GLOBALS['csp_nonce'] ?? ''; ?>">
function confirmDeleteReminder(id, title) {
document.getElementById('deleteReminderId').value = id;
document.getElementById('deleteReminderMsg').innerHTML = `Delete reminder: <strong>${title}</strong>? <br><span class="text-danger small">This cannot be undone.</span>`;
new bootstrap.Modal(document.getElementById('deleteReminderModal')).show();
}
</script>
<?php Layout::footer(); ?>