forked from htmlacademy-php/1415187-readme-12
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathadd.php
113 lines (97 loc) · 3.22 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
<?php
require_once __DIR__ . '/libs/base.php';
$title = $site_name . ': Добавление публикации';
$validation_rules = [
'text' => [
'heading' => 'filled|length:10,50',
'content' => 'filled|length:50,500',
],
'photo' => [
'heading' => 'filled|length:10,50',
'photo-url' => 'filled|correct_url|image_url_content',
'photo-file' => 'img_loaded',
],
'link' => [
'heading' => 'filled|length:10,50',
'link-url' => 'filled|correct_url',
],
'quote' => [
'heading' => 'filled|length:10,50',
'content' => 'filled',
'quote-author' => 'filled',
],
'video' => [
'heading' => 'filled|length:10,50',
'video-url' => 'filled|correct_url|youtube_url',
],
];
$field_error_codes = [
'heading' => 'Заголовок',
'content' => 'Текст поста',
'link-url' => 'Ссылка',
'photo-url' => 'Ссылка на изображение',
'video-url' => 'Ссылка YouTube',
'photo-file' => 'Файл фото',
'quote-author' => 'Автор',
];
$img_folder = __DIR__ . DIRECTORY_SEPARATOR . 'img' . DIRECTORY_SEPARATOR;
$user = get_user();
if ($user === null) {
header("Location: index.php");
exit();
}
$content_types = get_content_types($connection);
$form = [
'values' => [],
'errors' => [],
];
$form_type = $_GET['tab'] ?? 'photo';
$post_types = array_column($content_types, 'id', 'type_class');
if (count($_POST) > 0 && isset($_POST['form-type'])) {
$form_type = $_POST['form-type'];
$form['values'] = $_POST;
$form['values']['photo-file'] = $_FILES['photo-file'] ?? null;
$form['errors'] = validate($form['values'], $validation_rules[$_POST['form-type']], $connection);
if ((empty($form['errors']['photo-file'])) && (!empty($form['errors']['photo-url']))) {
$form = ignore_field($form, 'photo-url');
} elseif ((!empty($form['errors']['photo-file'])) && (empty($form['errors']['photo-url']))) {
$form = ignore_field($form, 'photo-file');
}
$form['errors'] = array_filter($form['errors']);
if (empty($form['errors'])) {
$file_url = ($form_type === 'photo') ? upload_file($form, $img_folder) : null;
$post_id = save_post($connection, $form['values'], $post_types, $user, $file_url);
add_tags($_POST['tags'], $post_id, $connection);
$URL = '/post.php?id=' . $post_id;
$followers = get_user_followers($connection, $user['id']);
new_post_notification(
$mail_settings['sender'],
$user,
$followers,
$form['values']['heading'],
$post_id,
$mailer
);
header("Location: $URL");
}
}
$page_content = include_template(
'add-template.php',
[
'content_types' => $content_types,
'form_values' => $form['values'] ?? [],
'form_errors' => $form['errors'] ?? [],
'field_error_codes' => $field_error_codes,
'form_type' => $form_type,
]
);
$layout_content = include_template(
'layout.php',
[
'title' => $title,
'active_section' => $active_section,
'user' => $user,
'content' => $page_content,
]
);
print($layout_content);