İçeriğe geç
Tüm kodlar

ER3NSHOP

Kurulum sihirbazlı online mağaza sistemi

90 satır · 3,9 KBHamİndir
<?php
$id = get_int('id');
$coupon = $id ? db_find('coupons', $id) : null;
$form = $coupon ?: ['code' => '', 'type' => 'percent', 'value' => '', 'min_total' => 0, 'usage_limit' => 0, 'starts_at' => null, 'ends_at' => null, 'status' => 1];
$error = null;

if (is_post()) {
    csrf_check();

    $form = array_merge($form, [
        'code'        => strtoupper(preg_replace('/[^A-Za-z0-9_-]/', '', post('code', ''))),
        'type'        => post('type') === 'amount' ? 'amount' : 'percent',
        'value'       => to_decimal(post('value')),
        'min_total'   => to_decimal(post('min_total')) ?? 0,
        'usage_limit' => max(0, post_int('usage_limit')),
        'starts_at'   => post('starts_at') ?: null,
        'ends_at'     => post('ends_at') ?: null,
        'status'      => checked('status'),
    ]);

    if ($form['code'] === '') {
        $error = 'Kupon kodu zorunlu (harf, rakam, - ve _).';
    } elseif (db_value('SELECT COUNT(*) FROM coupons WHERE code = ? AND id != ?', [$form['code'], $id])) {
        $error = 'Bu kod başka bir kuponda kullanılıyor.';
    } elseif (!$form['value'] || $form['value'] <= 0 || ($form['type'] === 'percent' && $form['value'] > 100)) {
        $error = 'Geçerli bir indirim değeri girin.';
    } else {
        $data = $form;
        unset($data['id'], $data['used'], $data['created_at']);
        $id ? db_update('coupons', $data, $id) : db_insert('coupons', $data);
        flash('Kupon kaydedildi.');
        go(admin_url('kuponlar'));
    }
}

admin_header($coupon ? 'Kuponu düzenle' : 'Yeni kupon', '<a href="' . admin_url('kuponlar') . '" class="btn-light btn-sm">' . icon('left', 'size-4', '2') . 'Kuponlar</a>');
?>

<?= form_error($error) ?>

<form method="post" class="card max-w-3xl space-y-6 p-6">
    <?= csrf_field() ?>

    <div class="grid gap-5 sm:grid-cols-2">
        <div>
            <label class="label">Kupon kodu</label>
            <input name="code" value="<?= e($form['code']) ?>" class="input font-mono uppercase" placeholder="YAZ20" required>
        </div>
        <div>
            <label class="label">İndirim</label>
            <div class="flex gap-2">
                <input name="value" value="<?= e($form['value']) ?>" inputmode="decimal" class="input" required>
                <select name="type" class="input w-36">
                    <option value="percent" <?= $form['type'] === 'percent' ? 'selected' : '' ?>>% yüzde</option>
                    <option value="amount" <?= $form['type'] === 'amount' ? 'selected' : '' ?>>₺ tutar</option>
                </select>
            </div>
        </div>
        <div>
            <label class="label">En az sepet tutarı (₺)</label>
            <input name="min_total" value="<?= e($form['min_total']) ?>" inputmode="decimal" class="input">
        </div>
        <div>
            <label class="label">Kullanım limiti <span class="font-normal text-zinc-400">(0 = sınırsız)</span></label>
            <input name="usage_limit" type="number" min="0" value="<?= (int) $form['usage_limit'] ?>" class="input">
        </div>
        <div>
            <label class="label">Başlangıç</label>
            <input name="starts_at" type="date" value="<?= e($form['starts_at']) ?>" class="input">
        </div>
        <div>
            <label class="label">Bitiş</label>
            <input name="ends_at" type="date" value="<?= e($form['ends_at']) ?>" class="input">
        </div>
    </div>

    <label class="flex items-center gap-3 text-sm font-medium">
        <input type="checkbox" name="status" value="1" class="size-4 rounded accent-ink" <?= $form['status'] ? 'checked' : '' ?>> Aktif
    </label>

    <?php if ($coupon): ?>
        <p class="text-sm text-zinc-500">Bu kupon şimdiye kadar <strong class="text-ink"><?= (int) $coupon['used'] ?></strong> kez kullanıldı.</p>
    <?php endif ?>

    <div class="border-t border-zinc-100 pt-5">
        <button class="btn-primary">Kaydet</button>
    </div>
</form>

<?php admin_footer() ?>

Lisans bilgisi projenin LICENSE dosyasında.