İçeriğe geç
Tüm kodlar

ER3NSHOP

Kurulum sihirbazlı online mağaza sistemi

135 satır · 6,6 KBHamİndir
<?php
if (is_post()) {
    csrf_check();
    $ids = array_map('intval', (array) post('ids', [post_int('sil')]));
    $action = post('action', 'delete');

    foreach (array_filter($ids) as $id) {
        $product = db_find('products', $id);
        if (!$product) {
            continue;
        }
        if ($action === 'delete') {
            product_delete($product);
        } elseif ($action === 'activate' || $action === 'deactivate') {
            db_update('products', ['status' => $action === 'activate' ? 1 : 0], $id);
        }
    }
    flash('İşlem tamamlandı.');
    back();
}

$q = get('q', '');
$category_id = get_int('kategori');
$stock = get('stok', '');

$where = '1 = 1';
$params = [];
if ($q !== '') {
    $where .= ' AND p.name LIKE ?';
    $params[] = "%$q%";
}
if ($category_id) {
    $where .= ' AND p.category_id = ?';
    $params[] = $category_id;
}
if ($stock === 'yok') {
    $where .= ' AND p.stock < 1';
} elseif ($stock === 'az') {
    $where .= ' AND p.stock BETWEEN 1 AND 5';
}

$total = db_value("SELECT COUNT(*) FROM products p WHERE $where", $params);
$pager = paginate($total, ADMIN_PER_PAGE, get_int('sayfa', 1));
$products = db_all(
    "SELECT p.*, c.name AS category, (SELECT COUNT(*) FROM product_variants v WHERE v.product_id = p.id) AS variant_count
     FROM products p LEFT JOIN categories c ON c.id = p.category_id
     WHERE $where ORDER BY p.id DESC LIMIT {$pager['limit']} OFFSET {$pager['offset']}",
    $params
);

admin_header('Ürünler', '<a href="' . admin_url('urun') . '" class="btn-primary btn-sm">' . icon('plus', 'size-4', '2') . 'Yeni ürün</a>');
?>

<form class="flex flex-wrap gap-2">
    <input name="q" value="<?= e($q) ?>" placeholder="Ürün ara..." class="input w-60 py-2">
    <select name="kategori" class="input w-48 py-2">
        <option value="">Tüm kategoriler</option>
        <?php foreach (category_tree() as $category): ?>
            <option value="<?= $category['id'] ?>" <?= $category_id === (int) $category['id'] ? 'selected' : '' ?>><?= str_repeat('— ', $category['depth']) . e($category['name']) ?></option>
        <?php endforeach ?>
    </select>
    <select name="stok" class="input w-40 py-2">
        <option value="">Tüm stoklar</option>
        <option value="az" <?= $stock === 'az' ? 'selected' : '' ?>>Azalanlar (1-5)</option>
        <option value="yok" <?= $stock === 'yok' ? 'selected' : '' ?>>Tükenenler</option>
    </select>
    <button class="btn-light btn-sm">Filtrele</button>
</form>

<form method="post" id="bulk" data-confirm="Seçili ürünlere işlem uygulansın mı?">
    <?= csrf_field() ?>
</form>

<div class="card mt-6 overflow-x-auto">
    <table class="w-full min-w-[860px] text-left text-sm">
        <thead class="border-b border-zinc-200 text-xs text-zinc-500">
            <tr>
                <th class="w-10 py-3 pl-5"></th>
                <th class="px-3 py-3 font-medium">Ürün</th>
                <th class="px-5 py-3 font-medium">Kategori</th>
                <th class="px-5 py-3 text-right font-medium">Fiyat</th>
                <th class="px-5 py-3 text-right font-medium">Stok</th>
                <th class="px-5 py-3 text-right font-medium">Satış</th>
                <th class="px-5 py-3 font-medium">Durum</th>
                <th class="px-5 py-3"></th>
            </tr>
        </thead>
        <tbody class="divide-y divide-zinc-100">
            <?php foreach ($products as $product): ?>
                <tr class="hover:bg-zinc-50">
                    <td class="py-3 pl-5"><input type="checkbox" name="ids[]" value="<?= $product['id'] ?>" form="bulk" class="size-4 rounded accent-ink"></td>
                    <td class="px-3 py-3">
                        <a href="<?= admin_url('urun?id=' . $product['id']) ?>" class="flex items-center gap-3">
                            <span class="size-11 shrink-0 overflow-hidden rounded-lg bg-zinc-100"><?= product_image($product, 'h-full w-full object-cover', 'text-base') ?></span>
                            <span class="min-w-0">
                                <span class="block truncate font-medium hover:underline"><?= e($product['name']) ?></span>
                                <span class="mt-0.5 flex items-center gap-2 text-xs text-zinc-500">
                                    <?php if ($product['variant_count']): ?><?= (int) $product['variant_count'] ?> <?= e(mb_strtolower($product['variant_name'] ?: 'seçenek')) ?><?php endif ?>
                                    <?php if ($product['featured']): ?><span class="rounded bg-accent-50 px-1.5 py-0.5 font-semibold text-accent-700">Öne çıkan</span><?php endif ?>
                                </span>
                            </span>
                        </a>
                    </td>
                    <td class="px-5 py-3 text-zinc-600"><?= e($product['category'] ?? '—') ?></td>
                    <td class="px-5 py-3 text-right">
                        <span class="font-medium"><?= money($product['price']) ?></span>
                        <?php if (discount_rate($product)): ?><span class="block text-xs text-zinc-400 line-through"><?= money($product['old_price']) ?></span><?php endif ?>
                    </td>
                    <td class="px-5 py-3 text-right <?= $product['stock'] < 1 ? 'font-semibold text-rose-600' : ($product['stock'] <= 5 ? 'font-semibold text-amber-600' : 'text-zinc-700') ?>"><?= (int) $product['stock'] ?></td>
                    <td class="px-5 py-3 text-right text-zinc-600"><?= (int) $product['sold'] ?></td>
                    <td class="px-5 py-3"><?= status_badge($product['status'], 'Yayında', 'Pasif') ?></td>
                    <td class="px-5 py-3 text-right">
                        <a href="<?= admin_url('urun?id=' . $product['id']) ?>" class="btn-ghost btn-sm p-2" title="Düzenle"><?= icon('pencil', 'size-4') ?></a>
                    </td>
                </tr>
            <?php endforeach ?>
            <?php if (!$products): ?>
                <tr><td colspan="8" class="px-5 py-16 text-center text-zinc-500">Ürün bulunamadı.</td></tr>
            <?php endif ?>
        </tbody>
    </table>
</div>

<?php if ($products): ?>
    <div class="mt-4 flex flex-wrap items-center gap-2 text-sm">
        <span class="text-zinc-500">Seçilenler:</span>
        <button name="action" value="activate" form="bulk" class="btn-light btn-sm">Yayına al</button>
        <button name="action" value="deactivate" form="bulk" class="btn-light btn-sm">Pasif yap</button>
        <button name="action" value="delete" form="bulk" class="btn-light btn-sm text-rose-600" data-confirm="Seçili ürünler kalıcı olarak silinsin mi?">Sil</button>
    </div>
<?php endif ?>

<?php pagination($pager) ?>

<?php admin_footer() ?>

Lisans bilgisi projenin LICENSE dosyasında.