İçeriğe geç
Tüm kodlar

ER3NSHOP

Kurulum sihirbazlı online mağaza sistemi

200 satır · 10,1 KBHamİndir
<?php
$locations = menu_locations();
$location = array_key_exists(get('konum', 'header'), $locations) ? get('konum', 'header') : 'header';
$editing = get_int('duzenle') ? db_one('SELECT * FROM menus WHERE id = ? AND location = ?', [get_int('duzenle'), $location]) : null;

if (is_post()) {
    csrf_check();
    $action = post('action');

    if ($action === 'sort') {
        foreach (array_values((array) post('ids', [])) as $sort => $id) {
            db_query('UPDATE menus SET sort = ? WHERE id = ?', [$sort, (int) $id]);
        }
        reply(true, 'Menü sırası kaydedildi.');
    }

    if ($action === 'title') {
        setting_save(['menu_title_' . $location => mb_substr(post('title', ''), 0, 60)]);
        finish('Sütun başlığı kaydedildi.', admin_url('menuler?konum=' . $location));
    }

    if ($action === 'delete') {
        $id = post_int('id');
        db_query('DELETE FROM menus WHERE parent_id = ?', [$id]);
        db_delete('menus', $id);
        log_action('Menü öğesi sildi', $locations[$location]);
        finish('Menü öğesi silindi.', admin_url('menuler?konum=' . $location));
    }

    if ($action === 'seed') {
        db_query('DELETE FROM menus WHERE location = ?', [$location]);
        $sort = 0;
        foreach ($location === 'header' ? header_menu() : footer_menu($location) as $item) {
            $parent = db_insert('menus', ['location' => $location, 'label' => $item['label'], 'type' => 'link', 'url' => str_replace(URL, '', $item['href']) ?: '/', 'sort' => $sort++]);
            foreach ($item['children'] ?? [] as $i => $child) {
                db_insert('menus', ['location' => $location, 'parent_id' => $parent, 'label' => $child['label'], 'type' => 'link', 'url' => str_replace(URL, '', $child['href']), 'sort' => $i]);
            }
        }
        finish('Varsayılan menü oluşturuldu, artık düzenleyebilirsiniz.', admin_url('menuler?konum=' . $location));
    }

    if ($action === 'save') {
        $type = array_key_exists(post('type'), menu_types()) ? post('type') : 'link';
        $target = $type === 'link' ? null : post_int('target_' . $type);
        $label = trim(post('label', ''));
        if ($label === '' && $target) {
            $table = ['category' => 'categories', 'page' => 'pages', 'product' => 'products'][$type];
            $row = db_find($table, $target);
            $label = $row['name'] ?? $row['title'] ?? '';
        }
        if ($label === '') {
            reply(false, 'Menü yazısı zorunlu.');
        }
        if ($type === 'link' && trim(post('url', '')) === '') {
            reply(false, 'Bağlantı adresi girin.');
        }
        $parent = $location === 'header' ? (post_int('parent_id') ?: null) : null;
        $data = [
            'location'  => $location,
            'parent_id' => $parent && $parent !== post_int('id') ? $parent : null,
            'label'     => mb_substr($label, 0, 150),
            'type'      => $type,
            'target_id' => $target,
            'url'       => $type === 'link' ? trim(post('url', '')) : null,
        ];
        if (post_int('id')) {
            db_update('menus', $data, post_int('id'));
        } else {
            $data['sort'] = (int) db_value('SELECT COALESCE(MAX(sort), 0) + 1 FROM menus WHERE location = ?', [$location]);
            db_insert('menus', $data);
        }
        log_action('Menüyü güncelledi', $locations[$location]);
        finish('Menü kaydedildi.', admin_url('menuler?konum=' . $location));
    }
}

$rows = db_all('SELECT * FROM menus WHERE location = ? ORDER BY sort, id', [$location]);
$top = array_filter($rows, fn($row) => !$row['parent_id']);
$children = [];
foreach ($rows as $row) {
    if ($row['parent_id']) {
        $children[$row['parent_id']][] = $row;
    }
}
$form = $editing ?: ['id' => '', 'label' => '', 'type' => 'category', 'target_id' => null, 'url' => '', 'parent_id' => null];
$sort_url = admin_url('menuler?konum=' . $location);
$targets = [
    'category' => category_tree(),
    'page'     => db_all('SELECT id, title AS name FROM pages ORDER BY title'),
    'product'  => db_all('SELECT id, name FROM products ORDER BY name LIMIT 1000'),
];

admin_header('Menüler');
?>

<div class="mb-6 flex flex-wrap gap-1.5">
    <?php foreach ($locations as $key => $label): ?>
        <a href="<?= admin_url('menuler?konum=' . $key) ?>" class="rounded-full px-3.5 py-1.5 text-[13px] font-medium transition <?= $location === $key ? 'bg-ink text-white' : 'bg-white text-zinc-600 ring-1 ring-zinc-200 hover:text-ink' ?>"><?= e($label) ?></a>
    <?php endforeach ?>
</div>

<div class="grid gap-6 xl:grid-cols-[1fr_420px]">
    <div class="space-y-4">
        <?php if ($location !== 'header'): ?>
            <form method="post" class="card flex flex-wrap items-end gap-3 p-5">
                <?= csrf_field() ?>
                <div class="min-w-60 flex-1">
                    <label class="label">Sütun başlığı</label>
                    <input name="title" value="<?= e(menu_column_title($location)) ?>" class="input">
                </div>
                <button name="action" value="title" class="btn-light">Kaydet</button>
            </form>
        <?php endif ?>

        <div class="card p-5">
            <div class="flex flex-wrap items-center justify-between gap-3">
                <div>
                    <p class="font-semibold"><?= e($locations[$location]) ?></p>
                    <p class="mt-0.5 text-xs text-zinc-500">Sürükleyerek sıralayın<?= $location === 'header' ? ', alt menü için düzenleyip üst öğe seçin' : '' ?>.</p>
                </div>
                <form method="post" data-confirm="Bu konumdaki menü silinip varsayılan içerikle yeniden oluşturulsun mu?">
                    <?= csrf_field() ?>
                    <button name="action" value="seed" class="btn-light btn-sm"><?= icon('refresh', 'size-4') ?>Varsayılandan oluştur</button>
                </form>
            </div>

            <?php if ($top): ?>
                <div data-sortable="<?= e($sort_url) ?>" class="mt-5 space-y-2">
                    <?php foreach ($top as $item): ?>
                        <div data-id="<?= $item['id'] ?>" draggable="true" class="rounded-xl border border-zinc-200 bg-white <?= $editing && $editing['id'] == $item['id'] ? 'ring-2 ring-ink' : '' ?>">
                            <?php $row = $item; include PARTS . '/admin-menu-row.php' ?>
                            <?php if (!empty($children[$item['id']])): ?>
                                <div data-sortable="<?= e($sort_url) ?>" class="space-y-1 border-t border-zinc-100 bg-zinc-50/60 p-2 pl-10">
                                    <?php foreach ($children[$item['id']] as $row): ?>
                                        <div data-id="<?= $row['id'] ?>" draggable="true" class="rounded-lg border border-zinc-200 bg-white"><?php include PARTS . '/admin-menu-row.php' ?></div>
                                    <?php endforeach ?>
                                </div>
                            <?php endif ?>
                        </div>
                    <?php endforeach ?>
                </div>
            <?php else: ?>
                <div class="mt-5 rounded-xl border border-dashed border-zinc-200 px-5 py-10 text-center text-sm text-zinc-500">
                    Bu konumda menü kurulmamış, vitrinde otomatik içerik gösteriliyor.<br>Sağdan öğe ekleyin ya da varsayılandan oluşturun.
                </div>
            <?php endif ?>
        </div>
    </div>

    <form method="post" class="card h-fit space-y-4 p-5 xl:sticky xl:top-24">
        <?= csrf_field() ?>
        <input type="hidden" name="action" value="save">
        <input type="hidden" name="id" value="<?= e($form['id']) ?>">
        <div class="flex items-center justify-between">
            <p class="font-semibold"><?= $editing ? 'Öğeyi düzenle' : 'Yeni öğe ekle' ?></p>
            <?php if ($editing): ?><a href="<?= admin_url('menuler?konum=' . $location) ?>" class="text-xs font-medium text-zinc-500 hover:text-ink">Vazgeç</a><?php endif ?>
        </div>
        <div>
            <label class="label">Bağlantı türü</label>
            <select name="type" data-menu-type class="input">
                <?php foreach (menu_types() as $key => $label): ?>
                    <option value="<?= $key ?>" <?= $form['type'] === $key ? 'selected' : '' ?>><?= $label ?></option>
                <?php endforeach ?>
            </select>
        </div>
        <?php foreach ($targets as $key => $items): ?>
            <div data-menu-target="<?= $key ?>">
                <label class="label"><?= menu_types()[$key] ?></label>
                <select name="target_<?= $key ?>" class="input">
                    <?php foreach ($items as $item): ?>
                        <option value="<?= $item['id'] ?>" <?= $form['type'] === $key && $form['target_id'] == $item['id'] ? 'selected' : '' ?>><?= str_repeat('— ', $item['depth'] ?? 0) . e($item['name']) ?></option>
                    <?php endforeach ?>
                </select>
            </div>
        <?php endforeach ?>
        <div data-menu-target="link">
            <label class="label">Adres</label>
            <input name="url" value="<?= e($form['url']) ?>" class="input" placeholder="/urunler?indirim=1 ya da https://...">
        </div>
        <div>
            <label class="label">Menüde görünecek yazı</label>
            <input name="label" value="<?= e($form['label']) ?>" class="input" placeholder="Boş bırakılırsa seçilen öğenin adı">
        </div>
        <?php if ($location === 'header'): ?>
            <div>
                <label class="label">Üst öğe</label>
                <select name="parent_id" class="input">
                    <option value="">Yok (ana menü)</option>
                    <?php foreach ($top as $item): ?>
                        <?php if ($editing && $editing['id'] == $item['id']) continue ?>
                        <option value="<?= $item['id'] ?>" <?= $form['parent_id'] == $item['id'] ? 'selected' : '' ?>><?= e($item['label']) ?></option>
                    <?php endforeach ?>
                </select>
            </div>
        <?php endif ?>
        <button class="btn-primary w-full"><?= $editing ? 'Kaydet' : 'Menüye ekle' ?></button>
    </form>
</div>

<?php admin_footer() ?>

Lisans bilgisi projenin LICENSE dosyasında.