forked from htmlacademy-php/264103-yeticave-12
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd.php
129 lines (121 loc) · 4.12 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
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php
require_once "init.php";
require_once "helpers.php";
require_once "validate_functions.php";
require_once "vendor/autoload.php";
if (!isset($_SESSION["user"])) {
http_response_code(403);
$content = include_template("error.php", [
"categories" => $categories,
"code_error" => "403",
"text_error" => "Страница доступна только зарегистрированным пользователям"
]);
} elseif ($_SERVER["REQUEST_METHOD"] === "POST") {
$errors = [];
$lot_name = post_value("lot-name");
$message = post_value("message");
$lot_rate = post_value("lot-rate");
$category = post_value("category");
$lot_step = post_value("lot-step");
$lot_date = post_value("lot-date");
$lot_img = get_file("lot-img", "");
$author_id = get_value_from_user_session("id");
$id_image = uniqid();
$errors = validate(
[
"lot-name" => $lot_name,
"message" => $message,
"lot-rate" => $lot_rate,
"category" => $category,
"lot-step" => $lot_step,
"lot-date" => $lot_date,
"lot-img" => $lot_img,
],
[
"lot-name" => [
not_empty("Введите название лота"),
str_length_gt(256),
],
"message" => [
not_empty("Введите описание лота"),
],
"lot-rate" => [
not_empty("Цена должна быть больше 0"),
it_is_number(),
check_price_greater_than_zero(),
str_length_gt(9),
],
"category" => [
not_empty("Укажите категорию лота"),
],
"lot-step" => [
not_empty("Цена должна быть больше 0"),
it_is_number(),
check_price_greater_than_zero(),
str_length_gt(9),
],
"lot-date" => [
not_empty("Введите дату окончания дейтсвия лота"),
checking_date_on_format_and_date_lot_end(),
],
"lot-img" => [
is_file_uploaded(),
checking_add_image(),
checking_type_image(),
]
]
);
if (!count($errors)) {
move_file_to_folder($id_image, $lot_img, PATH_UPLOADS_IMAGE);
resize_and_watermark_image_of_lot($id_image . $lot_img["name"]);
$file_url = PATH_UPLOADS_IMAGE . $id_image . $lot_img["name"];
$query_insert_database_lot = "INSERT INTO `lots` (
`name`,
`description`,
`link`,
`st_coast`,
`end_date`,
`step_bids`,
`author_id`,
`category_id`
)
VALUES(
?, ?, ?, ?, ?, ?, ?, ?)";
$stmt = db_get_prepare_stmt($con, $query_insert_database_lot, [
$lot_name,
$message,
$file_url,
$lot_rate,
$lot_date,
$lot_step,
$author_id,
$category
]);
$result = mysqli_stmt_execute($stmt);
$content = "";
if ($result) {
$last_id = mysqli_insert_id($con);
header("Location: lot.php?id=$last_id");
}
echo "Ошибка вставки " . mysqli_error($con);
} else {
$content = include_template("add-lot.php", [
"categories" => $categories,
"errors" => $errors,
"id_category" => post_value("category", null),
]);
}
} else {
$content = include_template("add-lot.php", [
"categories" => $categories,
"id_category" => null,
]);
}
$layout_content = include_template("layout.php", [
"content" => $content,
"title_page" => "Добавление нового лота",
"user_name" => get_value_from_user_session("name"),
"categories" => $categories,
"css_calendar" => "<link href=\"css/flatpickr.min.css\" rel=\"stylesheet\">"
]);
print($layout_content);