<?php
$id = get_int('id');
$product = $id ? db_find('products', $id) : null;
if ($id && !$product) {
flash('Ürün bulunamadı.', 'error');
go(admin_url('urunler'));
}
if (is_post() && post('action') === 'delete' && $product) {
csrf_check();
product_delete($product);
log_action('Ürün sildi', $product['name']);
flash('"' . $product['name'] . '" silindi.');
go(admin_url('urunler'));
}
$form = $product ?: [
'name' => '', 'slug' => '', 'category_id' => null, 'description' => '', 'price' => '', 'old_price' => '',
'stock' => 0, 'variant_name' => '', 'image' => null, 'featured' => 0, 'status' => 1,
];
$variants = $id ? array_values(product_variants($id)) : [];
$gallery = $id ? db_all('SELECT * FROM product_images WHERE product_id = ? ORDER BY sort, id', [$id]) : [];
$error = null;
if (is_post()) {
csrf_check();
$form = array_merge($form, [
'name' => post('name', ''),
'slug' => post('slug', ''),
'category_id' => post_int('category_id') ?: null,
'description' => post('description', ''),
'price' => to_decimal(post('price')),
'old_price' => to_decimal(post('old_price')),
'stock' => max(0, post_int('stock')),
'variant_name' => post('variant_name', ''),
'featured' => checked('featured'),
'status' => checked('status'),
]);
$variants = array_values(array_filter(post('variants', []), fn($row) => trim($row['name'] ?? '') !== ''));
if ($form['name'] === '') {
$error = 'Ürün adı zorunlu.';
} elseif ($form['price'] === null || $form['price'] < 0) {
$error = 'Geçerli bir fiyat girin.';
} elseif ($form['old_price'] !== null && $form['old_price'] > 0 && $form['old_price'] <= $form['price']) {
$error = 'Eski fiyat, satış fiyatından yüksek olmalı.';
}
if (!$error) {
$form['image'] = upload_replace('image', $form['image'], 'remove_image', $error);
}
if (!$error) {
$data = [
'name' => $form['name'],
'slug' => unique_slug('products', $form['slug'] ?: $form['name'], $id),
'category_id' => $form['category_id'],
'description' => $form['description'],
'price' => $form['price'],
'old_price' => $form['old_price'] ?: null,
'stock' => $form['stock'],
'variant_name' => $variants ? ($form['variant_name'] ?: 'Seçenek') : null,
'image' => $form['image'],
'featured' => $form['featured'],
'status' => $form['status'],
'badge' => mb_substr(post('badge', ''), 0, 40) ?: null,
] + seo_input();
if ($id) {
db_update('products', $data, $id);
} else {
$id = db_insert('products', $data);
}
log_action($product ? 'Ürünü düzenledi' : 'Ürün ekledi', $data['name']);
product_save_variants($id, $variants);
foreach ((array) post('remove_gallery', []) as $image_id) {
$image = db_one('SELECT * FROM product_images WHERE id = ? AND product_id = ?', [(int) $image_id, $id]);
if ($image) {
upload_delete($image['file']);
db_delete('product_images', $image['id']);
}
}
$sort = (int) db_value('SELECT COALESCE(MAX(sort), 0) FROM product_images WHERE product_id = ?', [$id]);
foreach (upload_images('gallery', $error) as $file) {
db_insert('product_images', ['product_id' => $id, 'file' => $file, 'sort' => ++$sort]);
}
flash($error ? 'Ürün kaydedildi, ancak bazı görseller yüklenemedi: ' . $error : 'Ürün kaydedildi.', $error ? 'error' : 'success');
go(admin_url('urun?id=' . $id));
}
}
$actions = $product
? '<a href="' . product_url($product) . '" target="_blank" class="btn-light btn-sm">' . icon('external', 'size-4') . 'Mağazada gör</a>'
: '';
admin_header($product ? $product['name'] : 'Yeni ürün', $actions . '<a href="' . admin_url('urunler') . '" class="btn-light btn-sm">' . icon('left', 'size-4', '2') . 'Ürünler</a>');
?>
<?= form_error($error) ?>
<form method="post" enctype="multipart/form-data" class="grid gap-6 xl:grid-cols-3">
<?= csrf_field() ?>
<div class="space-y-6 xl:col-span-2">
<div class="card space-y-5 p-6">
<div>
<label class="label">Ürün adı</label>
<input name="name" value="<?= e($form['name']) ?>" class="input" data-slug-source="#slug" required>
</div>
<div>
<label class="label">Adres</label>
<div class="flex items-center overflow-hidden rounded-xl border border-zinc-200 bg-zinc-50 text-sm focus-within:border-ink focus-within:ring-4 focus-within:ring-zinc-900/5">
<span class="hidden shrink-0 pl-4 text-zinc-400 sm:block"><?= e(url('urun/')) ?></span>
<input id="slug" name="slug" value="<?= e($form['slug']) ?>" placeholder="otomatik" class="w-full bg-transparent px-2 py-3 focus:outline-none">
</div>
</div>
<div>
<label class="label">Açıklama</label>
<textarea name="description" rows="8" class="input"><?= e($form['description']) ?></textarea>
</div>
</div>
<div class="card p-6">
<h2 class="font-semibold">Fiyat ve stok</h2>
<div class="mt-5 grid gap-5 sm:grid-cols-3">
<div>
<label class="label">Satış fiyatı (₺)</label>
<input name="price" value="<?= e($form['price']) ?>" inputmode="decimal" placeholder="0,00" class="input" required>
</div>
<div>
<label class="label">Eski fiyat (₺)</label>
<input name="old_price" value="<?= e($form['old_price']) ?>" inputmode="decimal" placeholder="İndirim yoksa boş" class="input">
</div>
<div>
<label class="label">Stok</label>
<input name="stock" type="number" min="0" value="<?= (int) $form['stock'] ?>" class="input" <?= $variants ? 'disabled title="Varyantlı üründe stok varyantlardan hesaplanır"' : '' ?>>
</div>
</div>
</div>
<div class="card p-6">
<div class="flex flex-wrap items-center justify-between gap-3">
<div>
<h2 class="font-semibold">Varyantlar</h2>
<p class="mt-1 text-sm text-zinc-500">Beden, renk ya da numara gibi seçenekler. Fiyat boş kalırsa ürün fiyatı geçerli olur.</p>
</div>
<input name="variant_name" value="<?= e($form['variant_name']) ?>" placeholder="Seçenek adı (örn. Beden)" class="input w-52 py-2.5">
</div>
<div class="mt-5 hidden grid-cols-12 gap-2 px-1 text-xs font-medium text-zinc-500 sm:grid">
<span class="col-span-3">Seçenek</span><span class="col-span-3">Stok kodu</span><span class="col-span-3">Fiyat (₺)</span><span class="col-span-2">Stok</span>
</div>
<div data-variant-list class="mt-2 space-y-2">
<?php foreach ($variants as $index => $variant): ?>
<?php $variant += ['id' => '', 'sku' => '', 'price' => '', 'stock' => 0] ?>
<?php include PARTS . '/admin-variant-row.php' ?>
<?php endforeach ?>
</div>
<p data-variant-empty class="<?= $variants ? 'hidden' : '' ?> mt-2 rounded-xl border border-dashed border-zinc-200 px-4 py-6 text-center text-sm text-zinc-500">Bu ürünün varyantı yok.</p>
<button type="button" data-add-variant class="btn-light btn-sm mt-4"><?= icon('plus', 'size-4', '2') ?>Varyant ekle</button>
</div>
<div class="card p-6">
<h2 class="font-semibold">Galeri</h2>
<p class="mt-1 text-sm text-zinc-500">Ürün sayfasında ana görselin yanında gösterilir.</p>
<div class="mt-5 grid grid-cols-3 gap-3 sm:grid-cols-5">
<?php foreach ($gallery as $image): ?>
<label class="group relative aspect-square cursor-pointer overflow-hidden rounded-xl bg-zinc-100">
<img src="<?= e(upload_url($image['file'])) ?>" alt="" class="h-full w-full object-cover transition group-has-checked:opacity-30">
<input type="checkbox" name="remove_gallery[]" value="<?= $image['id'] ?>" class="peer sr-only">
<span class="absolute top-2 right-2 flex size-7 items-center justify-center rounded-full bg-white/90 text-zinc-600 shadow transition peer-checked:bg-rose-600 peer-checked:text-white"><?= icon('trash', 'size-3.5') ?></span>
</label>
<?php endforeach ?>
<label class="flex aspect-square cursor-pointer flex-col items-center justify-center gap-2 rounded-xl border-2 border-dashed border-zinc-200 text-zinc-400 transition hover:border-zinc-400 hover:text-zinc-600">
<?= icon('plus', 'size-6') ?>
<span class="text-xs font-medium">Görsel ekle</span>
<input type="file" name="gallery[]" multiple accept="image/jpeg,image/png,image/webp,image/gif" data-preview="#gallery-preview" class="sr-only">
</label>
</div>
<div id="gallery-preview" data-image-class="aspect-square w-full rounded-xl object-cover" class="mt-3 grid grid-cols-3 gap-3 empty:hidden sm:grid-cols-5"></div>
<?php if ($gallery): ?><p class="mt-3 text-xs text-zinc-500">Silmek istediğiniz görsellere tıklayıp kaydedin.</p><?php endif ?>
</div>
</div>
<div class="space-y-6">
<div class="card divide-y divide-zinc-100">
<label class="flex cursor-pointer items-center justify-between gap-3 p-5 text-sm font-medium">
<span>Yayında<span class="block text-xs font-normal text-zinc-500">Mağazada görünür</span></span>
<input type="checkbox" name="status" value="1" class="peer sr-only" <?= $form['status'] ? 'checked' : '' ?>>
<span class="relative h-6 w-11 shrink-0 rounded-full bg-zinc-200 transition after:absolute after:top-0.5 after:left-0.5 after:size-5 after:rounded-full after:bg-white after:shadow after:transition peer-checked:bg-ink peer-checked:after:translate-x-5"></span>
</label>
<label class="flex cursor-pointer items-center justify-between gap-3 p-5 text-sm font-medium">
<span>Öne çıkan<span class="block text-xs font-normal text-zinc-500">Anasayfada gösterilir</span></span>
<input type="checkbox" name="featured" value="1" class="peer sr-only" <?= $form['featured'] ? 'checked' : '' ?>>
<span class="relative h-6 w-11 shrink-0 rounded-full bg-zinc-200 transition after:absolute after:top-0.5 after:left-0.5 after:size-5 after:rounded-full after:bg-white after:shadow after:transition peer-checked:bg-ink peer-checked:after:translate-x-5"></span>
</label>
</div>
<div class="card space-y-4 p-5">
<div>
<label class="label">Kategori</label>
<select name="category_id" class="input">
<option value="">Kategorisiz</option>
<?php foreach (category_tree() as $category): ?>
<option value="<?= $category['id'] ?>" <?= $form['category_id'] == $category['id'] ? 'selected' : '' ?>><?= str_repeat('— ', $category['depth']) . e($category['name']) ?></option>
<?php endforeach ?>
</select>
</div>
<div>
<label class="label">Özel rozet</label>
<input name="badge" value="<?= e($form['badge'] ?? '') ?>" maxlength="40" class="input" placeholder="Örn. Sınırlı üretim">
<p class="mt-1.5 text-xs text-zinc-500">Yeni, çok satan ve son ürünler rozetleri Tema ayarlarından otomatik gelir.</p>
</div>
</div>
<?php seo_fields($form) ?>
<div class="card p-5">
<label class="label">Ana görsel</label>
<div id="image-preview" class="aspect-[4/5] overflow-hidden rounded-xl bg-zinc-100">
<?= product_image($form + ['name' => $form['name'] ?: 'Ürün']) ?>
</div>
<input type="file" name="image" accept="image/jpeg,image/png,image/webp,image/gif" data-preview="#image-preview" class="mt-4 block w-full text-sm text-zinc-600 file:mr-3 file:rounded-lg file:border-0 file:bg-ink file:px-4 file:py-2 file:text-sm file:font-semibold file:text-white hover:file:bg-zinc-800">
<?php if ($form['image']): ?>
<label class="mt-3 flex items-center gap-2 text-sm text-rose-600"><input type="checkbox" name="remove_image" value="1" class="accent-rose-600">Görseli kaldır</label>
<?php endif ?>
<p class="mt-2 text-xs text-zinc-400">JPG, PNG, WEBP ya da GIF, en fazla <?= MAX_UPLOAD_MB ?> MB.</p>
</div>
<?php if ($product): ?>
<div class="card grid grid-cols-3 divide-x divide-zinc-100 text-center">
<div class="p-4"><p class="font-display text-lg font-semibold"><?= (int) $product['sold'] ?></p><p class="text-xs text-zinc-500">Satış</p></div>
<div class="p-4"><p class="font-display text-lg font-semibold"><?= number_format($product['rating'], 1, ',', '') ?></p><p class="text-xs text-zinc-500">Puan</p></div>
<div class="p-4"><p class="font-display text-lg font-semibold"><?= (int) $product['review_count'] ?></p><p class="text-xs text-zinc-500">Yorum</p></div>
</div>
<?php endif ?>
<div class="sticky bottom-4 flex gap-3">
<button class="btn-primary flex-1 shadow-lg">Kaydet</button>
<?php if ($product): ?>
<button name="action" value="delete" class="btn-light text-rose-600" data-confirm="Bu ürün kalıcı olarak silinsin mi?" formnovalidate><?= icon('trash', 'size-4') ?></button>
<?php endif ?>
</div>
</div>
</form>
<?php admin_footer() ?>
Lisans bilgisi projenin LICENSE dosyasında.