-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathadmin-comment-edit.php
More file actions
363 lines (332 loc) · 15.6 KB
/
Copy pathadmin-comment-edit.php
File metadata and controls
363 lines (332 loc) · 15.6 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
<?php
declare(strict_types=1);
require_once __DIR__ . '/includes/db.php';
require_once __DIR__ . '/includes/admin_auth.php';
require_once __DIR__ . '/includes/admin_notifications.php';
require_once __DIR__ . '/includes/user_notifications.php';
$adminUser = requireAdminUser($pdo);
$csrfToken = adminCsrfToken('admin_comment_edit');
function adminCommentRedirectToList(): void
{
header('Location: admin.php#comments');
exit;
}
function adminCommentRedirectToComment(int $commentId): void
{
header('Location: admin-comment-edit.php?id=' . $commentId);
exit;
}
function adminCommentDate(?string $dateValue): string
{
if ($dateValue === null || trim($dateValue) === '') {
return '-';
}
$timestamp = strtotime($dateValue);
if ($timestamp === false) {
return '-';
}
return date('d.m.Y H:i', $timestamp);
}
$commentId = $_SERVER['REQUEST_METHOD'] === 'POST'
? (int)($_POST['id'] ?? 0)
: (int)($_GET['id'] ?? 0);
if ($commentId <= 0) {
adminSetFlash('danger', 'Düzenlenecek yorum bulunamadı.');
adminCommentRedirectToList();
}
$localFlash = null;
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$action = (string)($_POST['action'] ?? 'update_comment');
$requestToken = (string)($_POST['csrf_token'] ?? '');
if (!adminValidateCsrfToken('admin_comment_edit', $requestToken)) {
$localFlash = [
'type' => 'danger',
'message' => 'Güvenlik doğrulaması başarısız oldu. Sayfayı yenileyip tekrar deneyin.',
];
} elseif ($action === 'update_comment') {
$rating = (int)($_POST['rating'] ?? 0);
$commentText = trim((string)($_POST['comment'] ?? ''));
$errors = [];
if ($rating < 1 || $rating > 5) {
$errors[] = 'Puan 1 ile 5 arasında olmalıdır.';
}
if ($commentText === '') {
$errors[] = 'Yorum metni boş olamaz.';
} elseif (mb_strlen($commentText) > 5000) {
$errors[] = 'Yorum metni 5000 karakteri geçemez.';
}
if (empty($errors)) {
try {
$beforeStmt = $pdo->prepare("
SELECT
nc.id,
nc.note_id,
nc.user_id,
nc.rating,
nc.comment,
nc.created_at,
n.title AS note_title,
n.course AS note_course,
n.topic AS note_topic,
n.deleted_at AS note_deleted_at,
u.first_name,
u.last_name,
u.email
FROM note_comments nc
JOIN notes n ON n.id = nc.note_id
JOIN users u ON u.id = nc.user_id
WHERE nc.id = :id
LIMIT 1
");
$beforeStmt->execute(['id' => $commentId]);
$commentBefore = $beforeStmt->fetch();
if (!$commentBefore) {
adminSetFlash('danger', 'Düzenlenecek yorum bulunamadı.');
adminCommentRedirectToList();
}
$updateStmt = $pdo->prepare("
UPDATE note_comments
SET rating = :rating,
comment = :comment
WHERE id = :id
LIMIT 1
");
$updateStmt->execute([
'rating' => $rating,
'comment' => $commentText,
'id' => $commentId,
]);
if ($updateStmt->rowCount() < 1) {
adminSetFlash('warning', 'Yorumda değişiklik yapılmadı veya yorum bulunamadı.');
} else {
sendAdminNotification($pdo, 'Yorum düzenlendi', 'Admin panelinden bir yorum güncellendi.', [
'İşlem yapan admin' => adminNotificationAdminLabel($adminUser),
'Yorum' => '#' . (int)$commentBefore['id'],
'Not' => (string)$commentBefore['note_title'] . ' (#' . (int)$commentBefore['note_id'] . ')',
'Yazan' => adminNotificationUserLabel($commentBefore) . ' (#' . (int)$commentBefore['user_id'] . ')',
'Puan değişimi' => (int)$commentBefore['rating'] . '/5' . "\n=> " . $rating . '/5',
'Yorum değişimi' => (string)$commentBefore['comment'] . "\n=> " . $commentText,
], [
'Yorumu Düzenle' => adminNotificationUrl('admin-comment-edit.php?id=' . $commentId),
'Yorum Yönetimi' => adminNotificationUrl('admin.php#comments'),
]);
sendAdminActionUserNotification($adminUser, $commentBefore, 'Yorumunuz admin tarafından güncellendi', 'Bir nota yaptığınız yorum admin tarafından güncellendi.', [
'Not' => (string)$commentBefore['note_title'],
'Puan değişimi' => (int)$commentBefore['rating'] . '/5' . "\n=> " . $rating . '/5',
'Yorum değişimi' => (string)$commentBefore['comment'] . "\n=> " . $commentText,
], [
'Profilim' => userNotificationUrl('profile.php#comments'),
]);
adminSetFlash('success', 'Yorum güncellendi.');
}
adminCommentRedirectToComment($commentId);
} catch (Throwable $e) {
error_log('admin comment edit update error: ' . $e->getMessage());
$localFlash = [
'type' => 'danger',
'message' => 'Yorum güncellenirken beklenmeyen bir hata oluştu.',
];
}
} else {
$localFlash = [
'type' => 'danger',
'message' => implode(' ', $errors),
];
}
} elseif ($action === 'delete_comment') {
try {
$beforeStmt = $pdo->prepare("
SELECT
nc.id,
nc.note_id,
nc.user_id,
nc.rating,
nc.comment,
nc.created_at,
n.title AS note_title,
n.course AS note_course,
n.topic AS note_topic,
n.deleted_at AS note_deleted_at,
u.first_name,
u.last_name,
u.email
FROM note_comments nc
JOIN notes n ON n.id = nc.note_id
JOIN users u ON u.id = nc.user_id
WHERE nc.id = :id
LIMIT 1
");
$beforeStmt->execute(['id' => $commentId]);
$commentBefore = $beforeStmt->fetch();
if (!$commentBefore) {
adminSetFlash('danger', 'Silinecek yorum bulunamadı.');
adminCommentRedirectToList();
}
$deleteStmt = $pdo->prepare("DELETE FROM note_comments WHERE id = :id LIMIT 1");
$deleteStmt->execute(['id' => $commentId]);
if ($deleteStmt->rowCount() < 1) {
adminSetFlash('danger', 'Silinecek yorum bulunamadı.');
} else {
sendAdminNotification($pdo, 'Yorum silindi', 'Admin yorum düzenleme sayfasından bir yorum kalıcı olarak silindi.', [
'İşlem yapan admin' => adminNotificationAdminLabel($adminUser),
'Yorum' => '#' . (int)$commentBefore['id'],
'Not' => (string)$commentBefore['note_title'] . ' (#' . (int)$commentBefore['note_id'] . ')',
'Yazan' => adminNotificationUserLabel($commentBefore) . ' (#' . (int)$commentBefore['user_id'] . ')',
'Puan' => (int)$commentBefore['rating'] . '/5',
'Yorum metni' => (string)$commentBefore['comment'],
], [
'Yorum Yönetimi' => adminNotificationUrl('admin.php#comments'),
]);
sendAdminActionUserNotification($adminUser, $commentBefore, 'Yorumunuz silindi', 'Bir nota yaptığınız yorum admin tarafından kalıcı olarak silindi.', [
'Not' => (string)$commentBefore['note_title'],
'Puan' => (int)$commentBefore['rating'] . '/5',
'Yorum metni' => (string)$commentBefore['comment'],
], [
'Profilim' => userNotificationUrl('profile.php#comments'),
]);
adminSetFlash('success', 'Yorum kalıcı olarak silindi.');
}
adminCommentRedirectToList();
} catch (Throwable $e) {
error_log('admin comment edit delete error: ' . $e->getMessage());
$localFlash = [
'type' => 'danger',
'message' => 'Yorum silinirken beklenmeyen bir hata oluştu.',
];
}
} else {
$localFlash = [
'type' => 'danger',
'message' => 'Geçersiz yorum işlemi.',
];
}
}
$commentStmt = $pdo->prepare("
SELECT
nc.id,
nc.note_id,
nc.user_id,
nc.rating,
nc.comment,
nc.created_at,
n.title AS note_title,
n.course AS note_course,
n.topic AS note_topic,
n.deleted_at AS note_deleted_at,
u.first_name,
u.last_name,
u.email
FROM note_comments nc
JOIN notes n ON n.id = nc.note_id
JOIN users u ON u.id = nc.user_id
WHERE nc.id = :id
LIMIT 1
");
$commentStmt->execute(['id' => $commentId]);
$comment = $commentStmt->fetch();
if (!$comment) {
adminSetFlash('danger', 'Düzenlenecek yorum bulunamadı.');
adminCommentRedirectToList();
}
$flash = $localFlash ?? adminGetFlash();
$pageTitle = 'Not Bul | Yorum Düzenle';
$pageKey = 'admin';
require __DIR__ . '/includes/header.php';
?>
<main class="page-shell">
<section class="container section-block">
<div class="d-flex justify-content-between align-items-start flex-wrap gap-3 mb-3">
<div>
<h1 class="section-title mb-1">Yorum Düzenle</h1>
<p class="mb-0 text-secondary">Yorum #<?= (int)$comment['id'] ?> / Not #<?= (int)$comment['note_id'] ?></p>
</div>
<div class="d-flex flex-wrap gap-2">
<a class="btn btn-sm btn-outline-secondary" href="admin.php#comments"><i class="fa-solid fa-arrow-left me-1"></i>Yorum Yönetimi</a>
<?php if (empty($comment['note_deleted_at'])): ?>
<a class="btn btn-sm btn-outline-primary" href="note-detail.php?id=<?= (int)$comment['note_id'] ?>">Notu Gör</a>
<?php endif; ?>
</div>
</div>
<?php if ($flash): ?>
<div class="alert alert-<?= htmlspecialchars((string)$flash['type'], ENT_QUOTES, 'UTF-8') ?>" role="alert">
<?= htmlspecialchars((string)$flash['message'], ENT_QUOTES, 'UTF-8') ?>
</div>
<?php endif; ?>
<div class="row g-4 align-items-start">
<div class="col-lg-8">
<div class="panel-card">
<h2 class="h4 mb-3">Yorum İçeriği</h2>
<form method="POST" action="admin-comment-edit.php?id=<?= (int)$comment['id'] ?>">
<input type="hidden" name="action" value="update_comment">
<input type="hidden" name="id" value="<?= (int)$comment['id'] ?>">
<input type="hidden" name="csrf_token" value="<?= htmlspecialchars($csrfToken, ENT_QUOTES, 'UTF-8') ?>">
<div class="mb-3">
<label class="form-label" for="commentRating">Puan</label>
<select class="form-select" id="commentRating" name="rating" required>
<?php for ($rating = 1; $rating <= 5; $rating += 1): ?>
<option value="<?= $rating ?>" <?= (int)$comment['rating'] === $rating ? 'selected' : '' ?>>
<?= $rating ?>/5
</option>
<?php endfor; ?>
</select>
</div>
<div class="mb-4">
<label class="form-label" for="commentText">Yorum</label>
<textarea class="form-control" id="commentText" name="comment" rows="8" maxlength="5000" required><?= htmlspecialchars((string)$comment['comment'], ENT_QUOTES, 'UTF-8') ?></textarea>
</div>
<div class="d-grid gap-2 d-md-flex">
<button class="btn btn-primary" type="submit"><i class="fa-solid fa-save me-1"></i>Yorumu Kaydet</button>
</div>
</form>
</div>
</div>
<div class="col-lg-4">
<div class="panel-card">
<h2 class="h4 mb-3">Detaylar</h2>
<dl class="admin-meta-list">
<dt>Yorum ID</dt>
<dd>#<?= (int)$comment['id'] ?></dd>
<dt>Not</dt>
<dd>
#<?= (int)$comment['note_id'] ?> /
<?= htmlspecialchars((string)$comment['note_title'], ENT_QUOTES, 'UTF-8') ?>
<?php if (!empty($comment['note_deleted_at'])): ?>
<span class="badge bg-secondary ms-1">Arşivde</span>
<?php endif; ?>
</dd>
<dt>Ders / Konu</dt>
<dd>
<?= htmlspecialchars((string)($comment['note_course'] ?? '-'), ENT_QUOTES, 'UTF-8') ?>
<?php if (!empty($comment['note_topic'])): ?>
/ <?= htmlspecialchars((string)$comment['note_topic'], ENT_QUOTES, 'UTF-8') ?>
<?php endif; ?>
</dd>
<dt>Yazan</dt>
<dd>
#<?= (int)$comment['user_id'] ?> /
<?= htmlspecialchars(trim((string)$comment['first_name'] . ' ' . (string)$comment['last_name']), ENT_QUOTES, 'UTF-8') ?>
</dd>
<dt>E-posta</dt>
<dd><?= htmlspecialchars((string)$comment['email'], ENT_QUOTES, 'UTF-8') ?></dd>
<dt>Oluşturulma</dt>
<dd><?= htmlspecialchars(adminCommentDate((string)$comment['created_at']), ENT_QUOTES, 'UTF-8') ?></dd>
</dl>
<hr>
<form
method="POST"
action="admin-comment-edit.php?id=<?= (int)$comment['id'] ?>"
onsubmit="return confirm('Bu yorum kalıcı olarak silinecek. Devam edilsin mi?');"
>
<input type="hidden" name="action" value="delete_comment">
<input type="hidden" name="id" value="<?= (int)$comment['id'] ?>">
<input type="hidden" name="csrf_token" value="<?= htmlspecialchars($csrfToken, ENT_QUOTES, 'UTF-8') ?>">
<button class="btn btn-outline-danger w-100" type="submit">
<i class="fa-solid fa-trash me-1"></i>Yorumu Sil
</button>
</form>
</div>
</div>
</div>
</section>
</main>
<?php require __DIR__ . '/includes/footer.php'; ?>