-
Notifications
You must be signed in to change notification settings - Fork 1
/
add.php
81 lines (72 loc) · 2.29 KB
/
add.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php
require_once('helpers.php');
require_once('init.php');
if (!isset($user_id)) {
header('Location: index.php');
exit();
}
$project_id = filter_input(INPUT_GET, 'id');
$projects_ids = [];
$projects = projects_db($connect, $user_id);
$projects_ids = array_column($projects, 'id');
$page_content = include_template('add.php', ['projects' => $projects]);
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$required = ['name', 'project_id'];
$errors = [];
$rules = [
'name' => function ($value) {
return required($value);
},
'project_id' => function ($value) use ($projects_ids) {
return valid_projects($value, $projects_ids);
},
'deadline_at' => function ($value) {
return valid_date($value);
},
];
$task = filter_input_array(INPUT_POST, ['name' => FILTER_DEFAULT, 'project_id' => FILTER_DEFAULT, 'deadline_at' => FILTER_DEFAULT], true);
if (empty($task['deadline_at'])) {
$task['deadline_at'] = null;
}
foreach ($task as $key => $value) {
if (isset($rules[$key])) {
$rule = $rules[$key];
$errors[$key] = $rule($value);
}
}
$errors = array_filter($errors);
$task['user_id'] = $user_id;
if (!empty($_FILES['file']['name'])) {
$file_name = $_FILES['file']['name'];
$file_path = __DIR__ . '/uploads/';
$file_url = '/uploads/' . $file_name;
$task['file'] = $file_name;
move_uploaded_file($_FILES['file']['tmp_name'], $file_path . $file_name);
} else {
$task['file'] = null;
}
if (count($errors)) {
$page_content = include_template(
'add.php',
[
'projects' => $projects,
'project_id' => $project_id,
'task' => $task,
'errors' => $errors,
]
);
} else {
$sql = 'INSERT INTO tasks (created_at, name, project_id, deadline_at, user_id, file) VALUES (NOW(), ?, ?, ?, ?, ?)';
execute_or_error($connect, $sql, $task);
header('Location: index.php');
}
}
$layout_content = include_template(
'layout.php',
[
'content' => $page_content,
'title' => 'Дела в порядке',
'user' => $user_name,
]
);
print($layout_content);