elkarte /
Elkarte
| 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): array |
||
| 43 | 2 | { |
|
| 44 | global $modSettings, $options, $txt, $settings, $url_format; |
||
| 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']), ['<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']), ['<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 | |||
| 121 | if ($url_format === 'queryless') |
||
| 122 | 2 | { |
|
| 123 | $pages = constructPageIndex('{scripturl}?topic,' . $row['id_topic'] . '.%1$d.html' . $topicseen, $start, $topic_length, $messages_per_page, true, ['prev_next' => false, 'all' => $show_all]); |
||
| 124 | } |
||
| 125 | 2 | else |
|
| 126 | 2 | { |
|
| 127 | 2 | $base_url = getUrl('topic', ['topic' => $row['id_topic'], 'subject' => $row['first_subject']]); |
|
| 128 | 2 | $base_url = str_replace('%', '%%', $base_url); |
|
| 129 | $pages = constructPageIndex($base_url . '.%1$d' . $topicseen, $start, $topic_length, $messages_per_page, true, ['prev_next' => false, 'all' => $show_all]); |
||
| 130 | 2 | } |
|
| 131 | } |
||
| 132 | |||
| 133 | $row['new_from'] = $row['new_from'] ?? 0; |
||
| 134 | $first_poster_href = getUrl('profile', ['action' => 'profile', 'u' => $row['first_id_member'], 'name' => $row['first_display_name']]); |
||
| 135 | $first_topic_href = getUrl('topic', ['topic' => $row['id_topic'], 'start' => '0', $topicseen, 'subject' => $row['first_subject']]); |
||
| 136 | 2 | $last_poster_href = getUrl('profile', ['action' => 'profile', 'u' => $row['last_id_member'], 'name' => $row['last_display_name']]); |
|
| 137 | |||
| 138 | 2 | if (User::$info->is_guest) |
|
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 139 | { |
||
| 140 | $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']; |
||
| 141 | 2 | } |
|
| 142 | 2 | else |
|
| 143 | { |
||
| 144 | 2 | $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'; |
|
| 145 | } |
||
| 146 | 2 | ||
| 147 | 2 | $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'; |
|
| 148 | 2 | ||
| 149 | 2 | // And build the array. |
|
| 150 | 2 | $topics[$row['id_topic']] = [ |
|
| 151 | 'id' => $row['id_topic'], |
||
| 152 | 2 | 'first_post' => [ |
|
| 153 | 2 | 'id' => $row['id_first_msg'], |
|
| 154 | 2 | 'member' => [ |
|
| 155 | 2 | 'username' => $row['first_member_name'], |
|
| 156 | 2 | 'name' => $row['first_display_name'], |
|
| 157 | 2 | 'id' => $row['first_id_member'], |
|
| 158 | 2 | 'href' => empty($row['first_id_member']) ? '' : $first_poster_href, |
|
| 159 | 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>' |
|
| 160 | 2 | ], |
|
| 161 | 'time' => standardTime($row['first_poster_time']), |
||
| 162 | 'html_time' => htmlTime($row['first_poster_time']), |
||
| 163 | 2 | 'timestamp' => forum_time(true, $row['first_poster_time']), |
|
| 164 | 'subject' => $row['first_subject'], |
||
| 165 | 2 | 'preview' => isset($row['first_body']) ? trim($row['first_body']) : '', |
|
| 166 | 2 | 'icon' => $icon_sources->getIconValue($row['first_icon']), |
|
| 167 | 2 | 'icon_url' => $icon_sources->getIconURL($row['first_icon']), |
|
| 168 | 2 | 'href' => $first_topic_href, |
|
| 169 | 2 | 'link' => '<a href="' . $first_topic_href . '">' . $row['first_subject'] . '</a>' |
|
| 170 | ], |
||
| 171 | 2 | 'last_post' => [ |
|
| 172 | 2 | 'id' => $row['id_last_msg'], |
|
| 173 | 2 | 'member' => [ |
|
| 174 | 2 | 'username' => $row['last_member_name'], |
|
| 175 | 2 | 'name' => $row['last_display_name'], |
|
| 176 | 2 | 'id' => $row['last_id_member'], |
|
| 177 | 2 | 'href' => empty($row['last_id_member']) ? '' : $last_poster_href, |
|
| 178 | 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>' |
|
| 179 | 2 | ], |
|
| 180 | 'time' => standardTime($row['last_poster_time']), |
||
| 181 | 2 | 'html_time' => htmlTime($row['last_poster_time']), |
|
| 182 | 2 | 'timestamp' => forum_time(true, $row['last_poster_time']), |
|
| 183 | 2 | 'subject' => $row['last_subject'], |
|
| 184 | 2 | 'preview' => isset($row['last_body']) ? trim($row['last_body']) : '', |
|
| 185 | 2 | 'icon' => $icon_sources->getIconValue($row['last_icon']), |
|
| 186 | 2 | 'icon_url' => $icon_sources->getIconURL($row['last_icon']), |
|
| 187 | 'href' => $topic_href, |
||
| 188 | 2 | 'link' => '<a href="' . $topic_href . '" ' . ($row['num_replies'] == 0 ? '' : 'rel="nofollow"') . '>' . $row['last_subject'] . '</a>', |
|
| 189 | 2 | ], |
|
| 190 | 2 | 'default_preview' => trim($row[!empty($modSettings['message_index_preview']) && $modSettings['message_index_preview'] == 2 ? 'last_body' : 'first_body']), |
|
| 191 | 2 | 'is_sticky' => !empty($row['is_sticky']), |
|
| 192 | 2 | 'is_locked' => !empty($row['locked']), |
|
| 193 | 2 | 'is_poll' => !empty($modSettings['pollMode']) && $row['id_poll'] > 0, |
|
| 194 | 2 | 'is_hot' => empty($modSettings['useLikesNotViews']) ? $row['num_replies'] >= $modSettings['hotTopicPosts'] : $row['num_likes'] >= $modSettings['hotTopicPosts'], |
|
| 195 | 2 | 'is_very_hot' => empty($modSettings['useLikesNotViews']) ? $row['num_replies'] >= $modSettings['hotTopicVeryPosts'] : $row['num_likes'] >= $modSettings['hotTopicVeryPosts'], |
|
| 196 | 2 | 'is_posted_in' => false, |
|
| 197 | 2 | 'icon' => $icon_sources->getIconValue($row['first_icon']), |
|
| 198 | 2 | 'icon_url' => $icon_sources->getIconURL($row['first_icon']), |
|
| 199 | 2 | 'subject' => $row['first_subject'], |
|
| 200 | 2 | 'new' => !empty($row['id_msg_modified']) && $row['new_from'] <= $row['id_msg_modified'], |
|
| 201 | 2 | 'new_from' => $row['new_from'], |
|
| 202 | 2 | 'newtime' => $row['new_from'], |
|
| 203 | 2 | 'new_href' => getUrl('topic', ['topic' => $row['id_topic'], 'start' => 'msg' . $row['new_from'], 'subject' => $row['first_subject'], $topicseen]) . '#new', |
|
| 204 | 'href' => $href, |
||
| 205 | 'link' => '<a href="' . $href . '" rel="nofollow">' . $row['first_subject'] . '</a>', |
||
| 206 | 'redir_href' => empty($row['id_redirect_topic']) ? '' : getUrl('topic', ['topic' => $row['id_topic'], 'start' => '0', 'subject' => $row['first_subject'], 'noredir']), |
||
| 207 | 2 | 'pages' => $pages, |
|
| 208 | 'replies' => comma_format($row['num_replies']), |
||
| 209 | 'views' => comma_format($row['num_views']), |
||
| 210 | 'likes' => comma_format($row['num_likes']), |
||
| 211 | 'approved' => $row['approved'] ?? 1, |
||
| 212 | 'unapproved_posts' => empty($row['unapproved_posts']) ? 0 : $row['unapproved_posts'], |
||
| 213 | 'classes' => [], |
||
| 214 | ]; |
||
| 215 | |||
| 216 | if (!empty($row['id_board'])) |
||
| 217 | { |
||
| 218 | 2 | $board_href = getUrl('board', ['board' => $row['id_board'], 'start' => '0', 'name' => $row['bname']]); |
|
| 219 | $topics[$row['id_topic']]['board'] = [ |
||
| 220 | 'id' => $row['id_board'], |
||
| 221 | 'name' => $row['bname'], |
||
| 222 | 2 | 'href' => $board_href, |
|
| 223 | 'link' => '<a href="' . $board_href . '.0">' . $row['bname'] . '</a>' |
||
| 224 | ]; |
||
| 225 | } |
||
| 226 | |||
| 227 | if (isset($row['avatar']) || !empty($row['id_attach'])) |
||
| 228 | { |
||
| 229 | $topics[$row['id_topic']]['last_post']['member']['avatar'] = determineAvatar($row); |
||
| 230 | } |
||
| 231 | |||
| 232 | if (!empty($row['avatar_first']) || !empty($row['id_attach_first'])) |
||
| 233 | { |
||
| 234 | 2 | $first_avatar = [ |
|
| 235 | 'avatar' => $row['avatar_first'], |
||
| 236 | 'id_attach' => $row['id_attach_first'], |
||
| 237 | 2 | 'attachment_type' => $row['attachment_type_first'], |
|
| 238 | 'filename' => $row['filename_first'], |
||
| 239 | 'email_address' => $row['email_address_first'], |
||
| 240 | ]; |
||
| 241 | $topics[$row['id_topic']]['first_post']['member']['avatar'] = determineAvatar($first_avatar); |
||
| 242 | } |
||
| 243 | |||
| 244 | determineTopicClass($topics[$row['id_topic']]); |
||
| 245 | } |
||
| 246 | |||
| 247 | return $topics; |
||
| 248 | } |
||
| 249 | } |
||
| 250 |