-
Notifications
You must be signed in to change notification settings - Fork 2
/
post.php
139 lines (111 loc) · 4.31 KB
/
post.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
130
131
132
133
134
135
136
137
138
<?php
require_once 'helpers.php';
require_once 'functions.php';
require_once 'dbconn.php';
// Проверка существования параметра запроса с ID поста
$post_id = filter_input(INPUT_GET, 'post_id', FILTER_SANITIZE_NUMBER_INT);
$post_id = (int) $post_id;
if ($post_id === 0) {
exit('Пост не существует');
}
// Проверка существования параметра show_all_comments и скрытых комментариев
$show_all_comments = isset($_GET['show_all_comments']);
$query = '
SELECT
p.*,
u.login,
u.avatar,
u.dt_add AS dt_user_registration,
u.id AS user_id,
c.category
FROM post AS p
INNER JOIN user AS u
ON p.user_id = u.id
INNER JOIN category AS c
ON p.category_id = c.id
WHERE p.id = ' . $post_id;
$post = get_result($db_link, $query, 3);
if (!$post) {
exit('Пост не существует');
}
// число лайков поста
$arr_num['likes_count'] = get_number($db_link, 'likeit', 'post_id =' . $post_id);
// число комментариев к посту
$arr_num['comments_count'] = get_number($db_link, 'comment', 'post_id =' . $post_id);
// число подписчиков автора поста
$arr_num['followers_count'] = get_number($db_link, 'subscription', 'target_id =' . $post['user_id']);
$followers_word = 'подписчик' . get_noun_plural_form($arr_num['followers_count'], '', 'а', 'ов');
// число постов автора поста
$arr_num['posts_count'] = get_number($db_link, 'post', 'user_id =' . $post['user_id']);
$posts_word = 'публикаци' . get_noun_plural_form($arr_num['posts_count'], 'я', 'и', 'й');
$view_word = 'просмотр' . get_noun_plural_form($post['view_count'], '', 'а', 'ов');
// генерация дат
$post['date_user_interval'] = get_interval ($post['dt_user_registration']);
$post['date_user_title'] = date(DATE_FORMAT, strtotime($post['dt_user_registration']));
// получение хештегов
$query = '
SELECT h_name
FROM hashtag
INNER JOIN post_hashtag_rel
ON post_hashtag_rel.hashtag_id = hashtag.id
WHERE post_hashtag_rel.post_id = ' . $post_id;
$hashtags = get_result($db_link, $query, 4);
if ($hashtags) {
$hashtags = explode(' ', $hashtags[0]);
}
// получение комментариев
$query = '
SELECT
c.id,
c.dt_add,
c_content,
post_id,
u.login,
u.avatar
FROM comment AS c
INNER JOIN user AS u
ON u.id = c.user_id
WHERE c.post_id = ' . $post_id . '
ORDER BY c.dt_add DESC';
if (!$show_all_comments) {
$query .= ' LIMIT 2'; //количество видимых комментариев
}
$comments = get_result($db_link, $query);
// генерация дат комментариев, запись id и ссылки последнего комментария
if ($comments) {
foreach ($comments as $key => $comment) {
$comments[$key]['comment_interval'] = get_interval(date(DATE_FORMAT, strtotime($comment['dt_add'])), true);
$comments[$key]['comment_date_title'] = $comment['dt_add'];
}
$last_comment_id = $comments[array_key_first($comments)]['id'];
$last_comment_href = 'post.php?post_id=' . $post['id'] . '&show_all_comments#last_comment_id_' . $last_comment_id;
}
else {
$last_comment_id = 0;
$last_comment_href = '#';
}
// выбор подшаблона поста
$post_type = 'templates/post-' . $post['category'] . '.php';
$is_auth = rand(0, 1);
$user_name = 'Никитина Виктория';
// Подготовка и вывод страницы
$main_content = include_template('posting.php', [
'post' => $post,
'post_type' => $post_type,
'arr_num' => $arr_num,
'last_comment_href' => $last_comment_href,
'view_word' => $view_word,
'hashtags' => $hashtags,
'comments' => $comments,
'last_comment_id' => $last_comment_id,
'show_all_comments' => $show_all_comments,
'followers_word' => $followers_word,
'posts_word' => $posts_word,
]);
$layout_content = include_template('layout.php', [
'is_auth' => $is_auth,
'user_name' => $user_name,
'main_content' => $main_content,
'page_title' => $post['p_title'],
]);
print($layout_content);