File: /home/muratemr/theotto.tr/admin/customer_edit.php
<?php
declare(strict_types=1);
$page_title = 'Müşteri Düzenle';
$active_menu = 'customers';
require_once __DIR__ . '/../includes/db.php';
require_once __DIR__ . '/../includes/auth.php';
require_once __DIR__ . '/../includes/functions.php';
require_once __DIR__ . '/../includes/csrf.php';
require_permission('customers');
if (!is_admin()) { header('Location: customers.php'); exit; }
$id = get_int('id');
if (!$id) { header('Location: customers.php'); exit; }
$customer = db_row("SELECT c.*, ct.id AS tag_id FROM customers c LEFT JOIN customer_tags ct ON ct.id=c.tag_id WHERE c.id=?", [$id]);
if (!$customer) { header('Location: customers.php'); exit; }
$tags = db_rows("SELECT id, name FROM customer_tags ORDER BY name");
$msg = '';
$err = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (!hash_equals(csrf_token(), $_POST['csrf_token'] ?? '')) {
$err = 'Güvenlik hatası.';
} else {
$full_name = trim($_POST['full_name'] ?? '');
$phone = trim($_POST['phone'] ?? '');
$tag_id = (int)($_POST['tag_id'] ?? 0) ?: null;
if (!$full_name) { $err = 'Ad Soyad zorunludur.'; }
elseif (!$phone) { $err = 'Telefon zorunludur.'; }
else {
// Telefon çakışması kontrolü
$dup = db_value("SELECT COUNT(*) FROM customers WHERE phone=? AND id!=?", [$phone, $id]);
if ($dup) { $err = 'Bu telefon numarası başka müşteriye ait.'; }
else {
db_run("UPDATE customers SET full_name=?, phone=?, tag_id=? WHERE id=?",
[$full_name, $phone, $tag_id, $id]);
$customer['full_name'] = $full_name;
$customer['phone'] = $phone;
$customer['tag_id'] = $tag_id;
$msg = 'Müşteri bilgileri güncellendi.';
}
}
}
}
require_once __DIR__ . '/../includes/admin_layout.php';
?>
<div class="d-flex align-items-center gap-2 mb-4">
<a href="customers.php" class="btn btn-outline-secondary btn-sm">
<i class="bi bi-arrow-left me-1"></i>Müşteri Listesi
</a>
<a href="customer_detail.php?id=<?= $id ?>" class="btn btn-outline-info btn-sm">
<i class="bi bi-eye me-1"></i>Detay
</a>
<h5 class="mb-0 ms-2">
<i class="bi bi-person-gear text-warning me-2"></i>Müşteri Düzenle
</h5>
</div>
<?php if ($msg): ?>
<div class="alert alert-success alert-dismissible fade show">
<i class="bi bi-check-circle me-2"></i><?= e($msg) ?>
<button type="button" class="btn-close" data-bs-dismiss="alert"></button>
</div>
<?php endif; ?>
<?php if ($err): ?>
<div class="alert alert-danger alert-dismissible fade show">
<i class="bi bi-exclamation-triangle me-2"></i><?= e($err) ?>
<button type="button" class="btn-close" data-bs-dismiss="alert"></button>
</div>
<?php endif; ?>
<div class="card" style="max-width:500px">
<div class="card-header">
<i class="bi bi-person-badge me-2 text-warning"></i>
<strong><?= e($customer['full_name']) ?></strong>
</div>
<div class="card-body">
<form method="POST" action="customer_edit.php?id=<?= $id ?>">
<input type="hidden" name="csrf_token" value="<?= htmlspecialchars(csrf_token()) ?>">
<div class="mb-3">
<label class="form-label">Ad Soyad <span class="text-danger">*</span></label>
<input type="text" name="full_name" class="form-control"
value="<?= e($customer['full_name']) ?>" required>
</div>
<div class="mb-3">
<label class="form-label">Telefon <span class="text-danger">*</span></label>
<input type="text" name="phone" class="form-control"
value="<?= e($customer['phone']) ?>" required>
</div>
<div class="mb-4">
<label class="form-label">Etiket</label>
<select name="tag_id" class="form-select">
<option value="">— Etiketsiz —</option>
<?php foreach ($tags as $t): ?>
<option value="<?= $t['id'] ?>" <?= (int)$customer['tag_id'] === (int)$t['id'] ? 'selected' : '' ?>>
<?= e($t['name']) ?>
</option>
<?php endforeach; ?>
</select>
</div>
<div class="d-flex gap-2">
<button type="submit" class="btn btn-warning px-4">
<i class="bi bi-check-lg me-1"></i>Kaydet
</button>
<a href="customers.php" class="btn btn-secondary px-4">İptal</a>
</div>
</form>
</div>
</div>
<?php require_once __DIR__ . '/../includes/admin_layout_end.php'; ?>