74 lines
3.3 KiB
PHP
74 lines
3.3 KiB
PHP
<?php
|
|
require __DIR__ . '/../../../config/db.php';
|
|
require __DIR__ . '/../../../config/functions.php';
|
|
$user = require_admin();
|
|
|
|
$errors = [];
|
|
$data = events_defaults();
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
csrf_check();
|
|
$data = [
|
|
'title' => trim($_POST['title'] ?? ''),
|
|
'slug' => trim($_POST['slug'] ?? ''),
|
|
'subtitle' => trim($_POST['subtitle'] ?? ''),
|
|
'summary' => trim($_POST['summary'] ?? ''),
|
|
'description' => trim($_POST['description'] ?? ''),
|
|
'cor' => trim($_POST['cor'] ?? '#C2D500'),
|
|
'location' => trim($_POST['location'] ?? ''),
|
|
'event_starts_at' => trim($_POST['event_starts_at'] ?? ''),
|
|
'event_ends_at' => trim($_POST['event_ends_at'] ?? ''),
|
|
'publish_from' => trim($_POST['publish_from'] ?? ''),
|
|
'publish_until' => trim($_POST['publish_until'] ?? ''),
|
|
'is_active' => isset($_POST['is_active']) ? 1 : 0,
|
|
'cta_label' => trim($_POST['cta_label'] ?? ''),
|
|
'cta_url' => trim($_POST['cta_url'] ?? ''),
|
|
'show_lead_form' => isset($_POST['show_lead_form']) ? 1 : 0,
|
|
'hero_image' => '',
|
|
];
|
|
|
|
if ($data['title'] === '') $errors[] = 'O título é obrigatório.';
|
|
if ($data['publish_from'] === '') $errors[] = 'A data de início da publicação é obrigatória.';
|
|
if ($data['publish_until'] === '') $errors[] = 'A data de fim da publicação é obrigatória.';
|
|
if ($data['publish_from'] !== '' && $data['publish_until'] !== ''
|
|
&& strtotime($data['publish_until']) < strtotime($data['publish_from'])) {
|
|
$errors[] = 'A data de fim da publicação não pode ser anterior à de início.';
|
|
}
|
|
|
|
if (!$errors) {
|
|
$hero_err = null;
|
|
$uploaded = events_image_upload($_FILES['hero_file'] ?? [], $hero_err);
|
|
if ($hero_err) $errors[] = $hero_err;
|
|
elseif ($uploaded) $data['hero_image'] = $uploaded;
|
|
}
|
|
|
|
if (!$errors) {
|
|
$data['slug'] = events_make_slug($pdo, $data['slug'] !== '' ? $data['slug'] : $data['title']);
|
|
$id = db_events_create($pdo, $data);
|
|
session()->setFlashdata('success', 'Evento criado.');
|
|
header('Location: ' . base_url('views/admin/events/edit.php?id=' . $id));
|
|
exit;
|
|
}
|
|
}
|
|
|
|
$page_title = 'Adicionar Evento';
|
|
include __DIR__ . '/../../../includes/header.php';
|
|
?>
|
|
<div class="container" style="max-width:820px;">
|
|
<div class="d-flex justify-content-between align-items-center mb-4">
|
|
<h1 class="h3 mb-0">Adicionar Evento</h1>
|
|
<a class="btn btn-outline-secondary btn-sm" href="index.php"><i class="bi bi-arrow-left me-1"></i>Voltar</a>
|
|
</div>
|
|
<?php foreach ($errors as $e): ?><div class="alert alert-danger"><?= htmlspecialchars($e) ?></div><?php endforeach; ?>
|
|
|
|
<form method="POST" enctype="multipart/form-data" class="card shadow-sm"><div class="card-body">
|
|
<?= csrf_field() ?>
|
|
<?php include __DIR__ . '/_form.php'; ?>
|
|
<div class="mt-4 d-flex gap-2">
|
|
<button type="submit" class="btn btn-primary">Guardar</button>
|
|
<a class="btn btn-outline-secondary" href="index.php">Cancelar</a>
|
|
</div>
|
|
</div></form>
|
|
</div>
|
|
<?php include __DIR__ . '/../../../includes/footer.php'; ?>
|