1 | <?php |
||
2 | |||
3 | /** |
||
4 | * This file contains functions for dealing with topics presentation. |
||
5 | * Middle-level functions, those that "converts" raw queries into data |
||
6 | * usable in the template or elsewhere. |
||
7 | * |
||
8 | * @package ElkArte Forum |
||
9 | * @copyright ElkArte Forum contributors |
||
10 | * @license BSD http://opensource.org/licenses/BSD-3-Clause (see accompanying LICENSE.txt file) |
||
11 | * |
||
12 | * This file contains code covered by: |
||
13 | * copyright: 2011 Simple Machines (http://www.simplemachines.org) |
||
14 | * |
||
15 | * @version 2.0 dev |
||
16 | * |
||
17 | */ |
||
18 | |||
19 | namespace ElkArte; |
||
20 | |||
21 | use BBC\ParserWrapper; |
||
22 | use ElkArte\Helper\Util; |
||
23 | |||
24 | /** |
||
25 | * Class TopicUtil |
||
26 | * |
||
27 | * Methods for dealing with topics presentation. |
||
28 | * Convert queries results into data usable in the templates. |
||
29 | */ |
||
30 | class TopicUtil |
||
31 | { |
||
32 | /** |
||
33 | * This function takes an array of data coming from the database and related |
||
34 | * to a list of topics and returns data useful in the template. |
||
35 | * |
||
36 | * @param array $topics_info - data coming from a query, for example |
||
37 | * generated by getUnreadTopics or messageIndexTopics |
||
38 | * @param bool $topic_seen - use the temp table or not |
||
39 | * @param int|null $preview_length - length of the preview |
||
40 | * @return array - array of data related to topics |
||
41 | 2 | */ |
|
42 | public static function prepareContext($topics_info, $topic_seen = false, $preview_length = null) |
||
43 | 2 | { |
|
44 | global $modSettings, $options, $txt, $settings; |
||
45 | 2 | ||
46 | 2 | $topics = []; |
|
47 | 2 | $preview_length = (int) $preview_length; |
|
48 | if (empty($preview_length)) |
||
49 | { |
||
50 | $preview_length = 128; |
||
51 | } |
||
52 | 2 | ||
53 | 2 | $messages_per_page = empty($modSettings['disableCustomPerPage']) && !empty($options['messages_per_page']) ? $options['messages_per_page'] : $modSettings['defaultMaxMessages']; |
|
54 | $topicseen = $topic_seen ? 'topicseen' : ''; |
||
55 | 2 | ||
56 | $icon_sources = new MessageTopicIcons(!empty($modSettings['messageIconChecks_enable']), $settings['theme_dir']); |
||
57 | 2 | ||
58 | $parser = ParserWrapper::instance(); |
||
59 | 2 | ||
60 | foreach ($topics_info as $row) |
||
61 | { |
||
62 | 2 | // Is there a body to preview? (If not the preview is disabled.) |
|
63 | if (isset($row['first_body'])) |
||
64 | { |
||
65 | // Limit them to $preview_length characters - do this FIRST because it's a lot of wasted censoring otherwise. |
||
66 | $row['first_body'] = strtr($parser->parseMessage($row['first_body'], $row['first_smileys']), array('<br />' => "\n", ' ' => ' ')); |
||
67 | $row['first_body'] = Util::htmlspecialchars(Util::shorten_html($row['first_body'], $preview_length)); |
||
68 | |||
69 | // No reply then they are the same, no need to process it again |
||
70 | if ($row['num_replies'] == 0) |
||
71 | { |
||
72 | $row['last_body'] = $row['first_body']; |
||
73 | } |
||
74 | else |
||
75 | { |
||
76 | $row['last_body'] = strtr($parser->parseMessage($row['last_body'], $row['last_smileys']), array('<br />' => "\n", ' ' => ' ')); |
||
77 | $row['last_body'] = Util::htmlspecialchars(Util::shorten_html($row['last_body'], $preview_length)); |
||
78 | } |
||
79 | |||
80 | // Censor the subject and message preview. |
||
81 | $row['first_subject'] = censor($row['first_subject']); |
||
82 | $row['first_body'] = censor($row['first_body']); |
||
83 | |||
84 | // Don't censor them twice! |
||
85 | if ($row['id_first_msg'] == $row['id_last_msg']) |
||
86 | { |
||
87 | $row['last_subject'] = $row['first_subject']; |
||
88 | $row['last_body'] = $row['first_body']; |
||
89 | } |
||
90 | else |
||
91 | { |
||
92 | $row['last_subject'] = censor($row['last_subject']); |
||
93 | $row['last_body'] = censor($row['last_body']); |
||
94 | } |
||
95 | } |
||
96 | else |
||
97 | 2 | { |
|
98 | 2 | $row['first_body'] = ''; |
|
99 | 2 | $row['last_body'] = ''; |
|
100 | $row['first_subject'] = censor($row['first_subject']); |
||
101 | 2 | ||
102 | if ($row['id_first_msg'] == $row['id_last_msg']) |
||
103 | 2 | { |
|
104 | $row['last_subject'] = $row['first_subject']; |
||
105 | } |
||
106 | else |
||
107 | { |
||
108 | $row['last_subject'] = censor($row['last_subject']); |
||
109 | } |
||
110 | } |
||
111 | |||
112 | 2 | // Decide how many pages the topic should have. |
|
113 | 2 | $topic_length = $row['num_replies'] + 1; |
|
114 | $pages = ''; |
||
115 | if ($topic_length > $messages_per_page) |
||
116 | { |
||
117 | // We can't pass start by reference. |
||
118 | $start = -1; |
||
119 | $show_all = !empty($modSettings['enableAllMessages']) && $topic_length < $modSettings['enableAllMessages']; |
||
120 | $pages = constructPageIndex('{scripturl}?topic=' . $row['id_topic'] . '.%1$d' . $topicseen, $start, $topic_length, $messages_per_page, true, array('prev_next' => false, 'all' => $show_all)); |
||
121 | } |
||
122 | 2 | ||
123 | $row['new_from'] = $row['new_from'] ?? 0; |
||
124 | $first_poster_href = getUrl('profile', ['action' => 'profile', 'u' => $row['first_id_member'], 'name' => $row['first_display_name']]); |
||
125 | 2 | $first_topic_href = getUrl('topic', ['topic' => $row['id_topic'], 'start' => '0', $topicseen, 'subject' => $row['first_subject']]); |
|
126 | 2 | $last_poster_href = getUrl('profile', ['action' => 'profile', 'u' => $row['last_id_member'], 'name' => $row['last_display_name']]); |
|
127 | 2 | ||
128 | 2 | if (User::$info->is_guest) |
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
129 | { |
||
130 | 2 | $topic_href = getUrl('topic', ['topic' => $row['id_topic'], 'start' => ((int) (($row['num_replies']) / $messages_per_page)) * $messages_per_page, 'subject' => $row['first_subject'], $topicseen]) . '#msg' . $row['id_last_msg']; |
|
131 | } |
||
132 | else |
||
133 | { |
||
134 | $topic_href = getUrl('topic', ['topic' => $row['id_topic'], 'start' => $row['num_replies'] == 0 ? '0' : ('msg' . $row['id_last_msg']), 'subject' => $row['first_subject'], $topicseen]) . '#new'; |
||
135 | } |
||
136 | 2 | ||
137 | $href = getUrl('topic', ['topic' => $row['id_topic'], 'start' => $row['num_replies'] == 0 ? '0' : ('msg' . $row['new_from']), 'subject' => $row['first_subject'], $topicseen]) . $row['num_replies'] == 0 ? '' : '#new'; |
||
138 | 2 | ||
139 | // And build the array. |
||
140 | $topics[$row['id_topic']] = array( |
||
141 | 2 | 'id' => $row['id_topic'], |
|
142 | 2 | 'first_post' => array( |
|
143 | 'id' => $row['id_first_msg'], |
||
144 | 2 | 'member' => array( |
|
145 | 'username' => $row['first_member_name'], |
||
146 | 2 | 'name' => $row['first_display_name'], |
|
147 | 2 | 'id' => $row['first_id_member'], |
|
148 | 2 | 'href' => empty($row['first_id_member']) ? '' : $first_poster_href, |
|
149 | 2 | 'link' => empty($row['first_id_member']) ? $row['first_display_name'] : '<a href="' . $first_poster_href . '" title="' . $txt['profile_of'] . ' ' . $row['first_display_name'] . '">' . $row['first_display_name'] . '</a>' |
|
150 | 2 | ), |
|
151 | 'time' => standardTime($row['first_poster_time']), |
||
152 | 2 | 'html_time' => htmlTime($row['first_poster_time']), |
|
153 | 2 | 'timestamp' => forum_time(true, $row['first_poster_time']), |
|
154 | 2 | 'subject' => $row['first_subject'], |
|
155 | 2 | 'preview' => isset($row['first_body']) ? trim($row['first_body']) : '', |
|
156 | 2 | 'icon' => $icon_sources->getIconValue($row['first_icon']), |
|
157 | 2 | 'icon_url' => $icon_sources->getIconURL($row['first_icon']), |
|
158 | 2 | 'href' => $first_topic_href, |
|
159 | 2 | 'link' => '<a href="' . $first_topic_href . '">' . $row['first_subject'] . '</a>' |
|
160 | 2 | ), |
|
161 | 'last_post' => array( |
||
162 | 'id' => $row['id_last_msg'], |
||
163 | 2 | 'member' => array( |
|
164 | 'username' => $row['last_member_name'], |
||
165 | 2 | 'name' => $row['last_display_name'], |
|
166 | 2 | 'id' => $row['last_id_member'], |
|
167 | 2 | 'href' => empty($row['last_id_member']) ? '' : $last_poster_href, |
|
168 | 2 | 'link' => empty($row['last_id_member']) ? $row['last_display_name'] : '<a href="' . $last_poster_href . '" title="' . $txt['profile_of'] . ' ' . $row['last_display_name'] . '">' . $row['last_display_name'] . '</a>' |
|
169 | 2 | ), |
|
170 | 'time' => standardTime($row['last_poster_time']), |
||
171 | 2 | 'html_time' => htmlTime($row['last_poster_time']), |
|
172 | 2 | 'timestamp' => forum_time(true, $row['last_poster_time']), |
|
173 | 2 | 'subject' => $row['last_subject'], |
|
174 | 2 | 'preview' => isset($row['last_body']) ? trim($row['last_body']) : '', |
|
175 | 2 | 'icon' => $icon_sources->getIconValue($row['last_icon']), |
|
176 | 2 | 'icon_url' => $icon_sources->getIconURL($row['last_icon']), |
|
177 | 2 | 'href' => $topic_href, |
|
178 | 2 | 'link' => '<a href="' . $topic_href . '" ' . ($row['num_replies'] == 0 ? '' : 'rel="nofollow"') . '>' . $row['last_subject'] . '</a>', |
|
179 | 2 | ), |
|
180 | 'default_preview' => trim($row[!empty($modSettings['message_index_preview']) && $modSettings['message_index_preview'] == 2 ? 'last_body' : 'first_body']), |
||
181 | 2 | 'is_sticky' => !empty($row['is_sticky']), |
|
182 | 2 | 'is_locked' => !empty($row['locked']), |
|
183 | 2 | 'is_poll' => !empty($modSettings['pollMode']) && $row['id_poll'] > 0, |
|
184 | 2 | 'is_hot' => empty($modSettings['useLikesNotViews']) ? $row['num_replies'] >= $modSettings['hotTopicPosts'] : $row['num_likes'] >= $modSettings['hotTopicPosts'], |
|
185 | 2 | 'is_very_hot' => empty($modSettings['useLikesNotViews']) ? $row['num_replies'] >= $modSettings['hotTopicVeryPosts'] : $row['num_likes'] >= $modSettings['hotTopicVeryPosts'], |
|
186 | 2 | 'is_posted_in' => false, |
|
187 | 'icon' => $icon_sources->getIconValue($row['first_icon']), |
||
188 | 2 | 'icon_url' => $icon_sources->getIconURL($row['first_icon']), |
|
189 | 2 | 'subject' => $row['first_subject'], |
|
190 | 2 | 'new' => !empty($row['id_msg_modified']) && $row['new_from'] <= $row['id_msg_modified'], |
|
191 | 2 | 'new_from' => $row['new_from'], |
|
192 | 2 | 'newtime' => $row['new_from'], |
|
193 | 2 | 'new_href' => getUrl('topic', ['topic' => $row['id_topic'], 'start' => 'msg' . $row['new_from'], 'subject' => $row['first_subject'], $topicseen]) . '#new', |
|
194 | 2 | 'href' => $href, |
|
195 | 2 | 'link' => '<a href="' . $href . '" rel="nofollow">' . $row['first_subject'] . '</a>', |
|
196 | 2 | 'redir_href' => empty($row['id_redirect_topic']) ? '' : getUrl('topic', ['topic' => $row['id_topic'], 'start' => '0', 'subject' => $row['first_subject'], 'noredir']), |
|
197 | 2 | 'pages' => $pages, |
|
198 | 2 | 'replies' => comma_format($row['num_replies']), |
|
199 | 2 | 'views' => comma_format($row['num_views']), |
|
200 | 2 | 'likes' => comma_format($row['num_likes']), |
|
201 | 2 | 'approved' => $row['approved'] ?? 1, |
|
202 | 2 | 'unapproved_posts' => empty($row['unapproved_posts']) ? 0 : $row['unapproved_posts'], |
|
203 | 2 | 'classes' => array(), |
|
204 | ); |
||
205 | |||
206 | if (!empty($row['id_board'])) |
||
207 | 2 | { |
|
208 | $board_href = getUrl('board', ['board' => $row['id_board'], 'start' => '0', 'name' => $row['bname']]); |
||
209 | $topics[$row['id_topic']]['board'] = array( |
||
210 | 'id' => $row['id_board'], |
||
211 | 'name' => $row['bname'], |
||
212 | 'href' => $board_href, |
||
213 | 'link' => '<a href="' . $board_href . '.0">' . $row['bname'] . '</a>' |
||
214 | ); |
||
215 | } |
||
216 | |||
217 | if (isset($row['avatar']) || !empty($row['id_attach'])) |
||
218 | 2 | { |
|
219 | $topics[$row['id_topic']]['last_post']['member']['avatar'] = determineAvatar($row); |
||
220 | } |
||
221 | |||
222 | 2 | if (!empty($row['avatar_first']) || !empty($row['id_attach_first'])) |
|
223 | { |
||
224 | $first_avatar = array( |
||
225 | 'avatar' => $row['avatar_first'], |
||
226 | 'id_attach' => $row['id_attach_first'], |
||
227 | 'attachment_type' => $row['attachment_type_first'], |
||
228 | 'filename' => $row['filename_first'], |
||
229 | 'email_address' => $row['email_address_first'], |
||
230 | ); |
||
231 | $topics[$row['id_topic']]['first_post']['member']['avatar'] = determineAvatar($first_avatar); |
||
232 | } |
||
233 | |||
234 | 2 | determineTopicClass($topics[$row['id_topic']]); |
|
235 | } |
||
236 | |||
237 | 2 | return $topics; |
|
238 | } |
||
239 | } |
||
240 |