forked from M2rk13/PHP-Professional-Web-Development
-
Notifications
You must be signed in to change notification settings - Fork 0
/
messages.php
65 lines (57 loc) · 1.81 KB
/
messages.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
<?php
require_once __DIR__ . '/libs/base.php';
$validation_rules = [
'receiver-id' => 'exists:users,id,not',
'message' => 'filled|long:4',
];
$user = get_user($connection);
if ($user === null) {
header("Location: index.php");
exit();
}
$title = $site_name . ': Сообщения';
$add_post_button = true;
$form['errors'] = [];
if (count($_POST) > 0 && isset($_POST['receiver-id']) && ($_POST['receiver-id'] !== $user['id'])) {
$receiver_id = (int) $_POST['receiver-id'];
$form['values'] = $_POST;
$form['errors'] = validate($form['values'], $validation_rules, $connection);
$form['errors'] = array_filter($form['errors']);
if (empty($form['errors'])) {
$message = add_message($connection, $user['id'], $receiver_id, $_POST['message']);
header("Location: " . $_SERVER['PHP_SELF']);
} else {
$errors = $form['errors'];
}
}
$dialogs = get_dialogs($connection, $user['id']);
if (($dialogs !== null) || ($GET['id'] !== null)) {
$active_dialog_id = (int) ($_GET['id'] ?? array_key_first($dialogs));
read_messages($connection, $active_dialog_id, $user['id']);
}
$messages = get_messages($connection, $user['id']);
foreach ($messages as $message) {
array_push($dialogs[$message['dialog']]['messages'], $message);
}
$page_content = include_template(
'messages-template.php',
[
'user' => $user,
'dialogs' => $dialogs,
'messages' => $messages,
'message_errors' => $errors,
'active_dialog_id' => $active_dialog_id,
'now_time' => $now_time,
]
);
$layout_content = include_template(
'layout.php',
[
'title' => $title,
'user' => $user,
'content' => $page_content,
'active_section' => 'messages',
'add_post_button' => $add_post_button,
]
);
print($layout_content);