258 lines
7.1 KiB
PHP
258 lines
7.1 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Controllers;
|
|
|
|
use App\Core\Auth;
|
|
use App\Core\DB;
|
|
use App\Core\View;
|
|
|
|
class AdminBoardFieldController
|
|
{
|
|
private function requireAdmin(): void
|
|
{
|
|
if (!Auth::check()) {
|
|
header('Location: /login');
|
|
exit;
|
|
}
|
|
|
|
$user = Auth::user();
|
|
|
|
if ((int)($user['is_admin'] ?? 0) !== 1) {
|
|
http_response_code(403);
|
|
echo 'Доступ запрещен';
|
|
exit;
|
|
}
|
|
}
|
|
|
|
public function index(): void
|
|
{
|
|
$this->requireAdmin();
|
|
|
|
$boardId = (int)($_GET['id'] ?? 0);
|
|
|
|
$pdo = DB::connection();
|
|
|
|
$stmt = $pdo->prepare("SELECT id, name, code FROM boards WHERE id = ? LIMIT 1");
|
|
$stmt->execute([$boardId]);
|
|
$board = $stmt->fetch();
|
|
|
|
if (!$board) {
|
|
http_response_code(404);
|
|
echo 'Доска не найдена';
|
|
return;
|
|
}
|
|
|
|
$stmt = $pdo->prepare("
|
|
SELECT *
|
|
FROM board_fields
|
|
WHERE board_id = ?
|
|
ORDER BY sort_order ASC, id ASC
|
|
");
|
|
$stmt->execute([$boardId]);
|
|
$fields = $stmt->fetchAll();
|
|
|
|
View::render('admin/boards/fields/index', [
|
|
'board' => $board,
|
|
'fields' => $fields,
|
|
'user' => Auth::user(),
|
|
]);
|
|
}
|
|
|
|
public function create(): void
|
|
{
|
|
$this->requireAdmin();
|
|
|
|
$boardId = (int)($_GET['board_id'] ?? 0);
|
|
|
|
$pdo = DB::connection();
|
|
$stmt = $pdo->prepare("SELECT id, name, code FROM boards WHERE id = ? LIMIT 1");
|
|
$stmt->execute([$boardId]);
|
|
$board = $stmt->fetch();
|
|
|
|
if (!$board) {
|
|
http_response_code(404);
|
|
echo 'Доска не найдена';
|
|
return;
|
|
}
|
|
|
|
View::render('admin/boards/fields/create', [
|
|
'board' => $board,
|
|
'user' => Auth::user(),
|
|
]);
|
|
}
|
|
|
|
public function store(): void
|
|
{
|
|
$this->requireAdmin();
|
|
|
|
$boardId = (int)($_POST['board_id'] ?? 0);
|
|
$code = trim((string)($_POST['code'] ?? ''));
|
|
$name = trim((string)($_POST['name'] ?? ''));
|
|
$fieldType = trim((string)($_POST['field_type'] ?? 'text'));
|
|
$isRequired = isset($_POST['is_required']) ? 1 : 0;
|
|
$isActive = isset($_POST['is_active']) ? 1 : 0;
|
|
$sortOrder = (int)($_POST['sort_order'] ?? 100);
|
|
|
|
$settings = null;
|
|
if ($fieldType === 'select') {
|
|
$rawOptions = trim((string)($_POST['select_options'] ?? ''));
|
|
$options = array_values(array_filter(array_map('trim', preg_split('/\r\n|\r|\n/', $rawOptions))));
|
|
$settings = json_encode(['options' => $options], JSON_UNESCAPED_UNICODE);
|
|
}
|
|
|
|
if ($boardId <= 0 || $code === '' || $name === '') {
|
|
$_SESSION['error'] = 'Заполни обязательные поля';
|
|
header('Location: /admin/boards/fields/create?board_id=' . $boardId);
|
|
exit;
|
|
}
|
|
|
|
$pdo = DB::connection();
|
|
|
|
$stmt = $pdo->prepare("
|
|
INSERT INTO board_fields (
|
|
board_id, code, name, field_type, is_required, sort_order, is_active, settings_json, created_at, updated_at
|
|
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, NOW(), NOW())
|
|
");
|
|
$stmt->execute([
|
|
$boardId,
|
|
$code,
|
|
$name,
|
|
$fieldType,
|
|
$isRequired,
|
|
$sortOrder,
|
|
$isActive,
|
|
$settings,
|
|
]);
|
|
|
|
header('Location: /admin/boards/fields?id=' . $boardId);
|
|
exit;
|
|
}
|
|
|
|
public function edit(): void
|
|
{
|
|
$this->requireAdmin();
|
|
|
|
$fieldId = (int)($_GET['id'] ?? 0);
|
|
|
|
$pdo = DB::connection();
|
|
$stmt = $pdo->prepare("
|
|
SELECT bf.*, b.name AS board_name, b.code AS board_code
|
|
FROM board_fields bf
|
|
INNER JOIN boards b ON b.id = bf.board_id
|
|
WHERE bf.id = ?
|
|
LIMIT 1
|
|
");
|
|
$stmt->execute([$fieldId]);
|
|
$field = $stmt->fetch();
|
|
|
|
if (!$field) {
|
|
http_response_code(404);
|
|
echo 'Поле не найдено';
|
|
return;
|
|
}
|
|
|
|
View::render('admin/boards/fields/edit', [
|
|
'field' => $field,
|
|
'user' => Auth::user(),
|
|
]);
|
|
}
|
|
|
|
public function update(): void
|
|
{
|
|
$this->requireAdmin();
|
|
|
|
$fieldId = (int)($_POST['id'] ?? 0);
|
|
$code = trim((string)($_POST['code'] ?? ''));
|
|
$name = trim((string)($_POST['name'] ?? ''));
|
|
$fieldType = trim((string)($_POST['field_type'] ?? 'text'));
|
|
$isRequired = isset($_POST['is_required']) ? 1 : 0;
|
|
$isActive = isset($_POST['is_active']) ? 1 : 0;
|
|
$sortOrder = (int)($_POST['sort_order'] ?? 100);
|
|
|
|
$settings = null;
|
|
if ($fieldType === 'select') {
|
|
$rawOptions = trim((string)($_POST['select_options'] ?? ''));
|
|
$options = array_values(array_filter(array_map('trim', preg_split('/\r\n|\r|\n/', $rawOptions))));
|
|
$settings = json_encode(['options' => $options], JSON_UNESCAPED_UNICODE);
|
|
}
|
|
|
|
if ($fieldId <= 0 || $code === '' || $name === '') {
|
|
$_SESSION['error'] = 'Заполни обязательные поля';
|
|
header('Location: /admin');
|
|
exit;
|
|
}
|
|
|
|
$pdo = DB::connection();
|
|
|
|
$stmt = $pdo->prepare("SELECT board_id FROM board_fields WHERE id = ? LIMIT 1");
|
|
$stmt->execute([$fieldId]);
|
|
$boardId = (int)$stmt->fetchColumn();
|
|
|
|
$stmt = $pdo->prepare("
|
|
UPDATE board_fields
|
|
SET
|
|
code = ?,
|
|
name = ?,
|
|
field_type = ?,
|
|
is_required = ?,
|
|
sort_order = ?,
|
|
is_active = ?,
|
|
settings_json = ?,
|
|
updated_at = NOW()
|
|
WHERE id = ?
|
|
");
|
|
$stmt->execute([
|
|
$code,
|
|
$name,
|
|
$fieldType,
|
|
$isRequired,
|
|
$sortOrder,
|
|
$isActive,
|
|
$settings,
|
|
$fieldId,
|
|
]);
|
|
|
|
header('Location: /admin/boards/fields?id=' . $boardId);
|
|
exit;
|
|
}
|
|
public function delete(): void
|
|
{
|
|
$this->requireAdmin();
|
|
|
|
$fieldId = (int)($_POST['id'] ?? 0);
|
|
|
|
if ($fieldId <= 0) {
|
|
$_SESSION['error'] = 'Поле не найдено';
|
|
header('Location: /admin/boards');
|
|
exit;
|
|
}
|
|
|
|
$pdo = DB::connection();
|
|
|
|
$stmt = $pdo->prepare("
|
|
SELECT id, board_id
|
|
FROM board_fields
|
|
WHERE id = ?
|
|
LIMIT 1
|
|
");
|
|
$stmt->execute([$fieldId]);
|
|
$field = $stmt->fetch();
|
|
|
|
if (!$field) {
|
|
$_SESSION['error'] = 'Поле не найдено';
|
|
header('Location: /admin/boards');
|
|
exit;
|
|
}
|
|
|
|
$boardId = (int)$field['board_id'];
|
|
|
|
$stmt = $pdo->prepare("DELETE FROM board_fields WHERE id = ?");
|
|
$stmt->execute([$fieldId]);
|
|
|
|
$_SESSION['success'] = 'Поле удалено';
|
|
header('Location: /admin/boards/fields?id=' . $boardId);
|
|
exit;
|
|
}
|
|
} |