forked from htmlacademy-php1/2074903-doingsdone-14
-
Notifications
You must be signed in to change notification settings - Fork 0
/
new-project.php
106 lines (94 loc) · 2.93 KB
/
new-project.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php
require_once 'init.php';
require_once 'model.php';
require_once 'helpers.php';
require_once 'myfunction.php';
if (empty($_SESSION['user'])) {
$page_content = include_template('guest.php');
$project_id = null;
$projects = [];
$navigation_content = include_template(
'navigation.php',
[
'user' => $_SESSION['user'],
'projects' => $projects,
'project_id' => $project_id,
'content' => $page_content
]
);
$layout_content = include_template(
'layout.php',
[
'user' => $_SESSION['user'],
'navigation' => $navigation_content,
'title' => 'Дела в порядке'
]
);
} else {
$project_id = filter_input(INPUT_GET, 'project_id', FILTER_SANITIZE_NUMBER_INT);
$projects = get_projects($con, $user_id);
$projects_ids = array_column($projects, 'id');
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$required = ['name'];
$errors = [];
$rules = [
'name' => function ($value) {
return validate_name($value);
}
];
$project_form = filter_input_array(
INPUT_POST,
[
'name' => FILTER_DEFAULT
],
true
);
foreach ($project_form as $key => $value) {
if (!empty($rules[$key])) {
$rule = $rules[$key];
$errors[$key] = $rule($value);
}
if (in_array($key, $required) && empty($value)) {
$errors[$key] = "Поле $key надо заполнить";
}
}
$errors = array_filter($errors);
if (!count($errors) and check_name_project($projects, $project_form)) {
$errors['name'] = 'У вас уже есть такой проект';
} elseif (!count($errors) and !check_name_project($projects, $project_form)) {
$result = add_project($con, $project_form, $user_id);
if ($result) {
$project_id = mysqli_insert_id($con);
header("Location: index.php");
}
}
if (count($errors)) {
$page_content = include_template(
'add-project.php',
[
'errors' => $errors
]
);
}
} else {
$page_content = include_template('add-project.php');
}
$navigation_content = include_template(
'navigation.php',
[
'user' => $_SESSION['user'],
'projects' => $projects,
'project_id' => $project_id,
'content' => $page_content
]
);
$layout_content = include_template(
'layout.php',
[
'user' => $_SESSION['user'],
'navigation' => $navigation_content,
'title' => 'Дела в порядке'
]
);
}
print($layout_content);