Guest User

Untitled

a guest
Nov 20th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.20 KB | None | 0 0
  1. <?php
  2.  
  3.  
  4. // Load news posts into $context['news_posts']
  5. function loadNewsPosts($start = null, $limit = null, $category = null, $length = null)
  6. {
  7.     global $context, $smcFunc, $modSettings, $settings, $txt, $scripturl, $boardurl;
  8.  
  9.     // A bit of basic validation
  10.     $start = (int) $start;
  11.     $limit = (int) $limit;
  12.     $category = (int) $category;
  13.     $length = (int) $length;
  14.     if (empty($limit))
  15.         $limit = 15;
  16.  
  17.     // Load all the boards along with category names
  18.     $request = $smcFunc['db_query']('', '
  19.        SELECT b.id_board, c.name, c.id_cat
  20.        FROM {db_prefix}boards AS b
  21.            LEFT JOIN {db_prefix}categories AS c ON (b.id_cat = c.id_cat)
  22.        WHERE' . (empty($category) ? '' : ' c.id_cat = {int:cat} AND') . ' b.id_board != {int:recycle}',
  23.         array(
  24.             'cat' => $category,
  25.             'recycle' => $modSettings['recycle_board']//$modSettings['recycle_enabled'] ? $modSettings['recycle_board'] : 0,
  26.         )
  27.     );
  28.  
  29.     $boards = array();
  30.     while ($row = $smcFunc['db_fetch_assoc']($request))
  31.     {
  32.         $boards[] = $row['id_board'];
  33.         $cats[$row['id_board']] = array($row['id_cat'], $row['name']);
  34.     }
  35.     $smcFunc['db_free_result']($request);
  36.  
  37.     // Now grab all the topic ids
  38.     $request = $smcFunc['db_query']('', '
  39.        SELECT id_first_msg
  40.        FROM {db_prefix}topics AS t
  41.            INNER JOIN {db_prefix}news AS n ON (t.id_topic = n.id_topic)
  42.        ' . (empty($boards) ? '' : 'WHERE
  43.            id_board IN ({array_int:boards})') . '
  44.        ORDER BY id_first_msg DESC
  45.        LIMIT ' . $start . ', ' . $limit,
  46.         array(
  47.             'boards' => $boards,
  48.         )
  49.     );
  50.  
  51.     $posts = array();
  52.     while ($row = $smcFunc['db_fetch_assoc']($request))
  53.         $posts[] = $row['id_first_msg'];
  54.     $smcFunc['db_free_result']($request);
  55.  
  56.     // Now query each post with newest first
  57.     $context['news_posts'] = array();
  58.     if (!empty($posts))
  59.     {
  60.         $request = $smcFunc['db_query']('', '
  61.            SELECT
  62.                m.icon, m.subject, m.body, IFNULL(mem.real_name, m.poster_name) AS poster_name, m.poster_time,
  63.                 t.num_replies, t.id_topic, m.id_member, m.smileys_enabled, m.id_msg, t.locked, m.id_board,
  64.                n.shortest, n.short, n.pic
  65.             FROM {db_prefix}topics AS t
  66.                 INNER JOIN {db_prefix}messages AS m ON (m.id_msg = t.id_first_msg)
  67.                INNER JOIN {db_prefix}news AS n ON (n.id_topic = t.id_topic)
  68.                 LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = m.id_member)
  69.             WHERE t.id_first_msg IN ({array_int:posts})
  70.             ORDER BY m.poster_time DESC',
  71.             array(
  72.                 'posts' => $posts,
  73.             )
  74.         );
  75.  
  76.  
  77.         while ($row = $smcFunc['db_fetch_assoc']($request))
  78.         {
  79.             // If we want to limit the length of the post by char.
  80.             unset($shortbody);
  81.             if (!empty($length) && $smcFunc['strlen']($row['body']) > $length)
  82.             {
  83.                 $shortbody = $smcFunc['substr']($row['body'], 0, $length);
  84.  
  85.                 // The first space or line break. (<br />, etc.)
  86.                 $cutoff = max(strrpos($shortbody, ' '), strrpos($shortbody, '<'));
  87.  
  88.                 if ($cutoff !== false)
  89.                     $shortbody = $smcFunc['substr']($shortbody, 0, $cutoff);
  90.                 $shortbody .= '...';
  91.             }
  92.  
  93.             $row['body'] = parse_bbc($row['body'], $row['smileys_enabled'], $row['id_msg']);
  94.             if (!empty($shortbody))
  95.                 $shortbody = parse_bbc($shortbody, $row['smileys_enabled'], $row['id_msg']);
  96.  
  97.             // Check that this message icon is there...
  98.             if (empty($modSettings['messageIconChecks_disable']) && !isset($icon_sources[$row['icon']]))
  99.                 $icon_sources[$row['icon']] = file_exists($settings['theme_dir'] . '/images/post/' . $row['icon'] . '.gif') ? 'images_url' : 'default_images_url';
  100.  
  101.             censorText($row['subject']);
  102.             censorText($row['body']);
  103.  
  104.             $context['news_posts'][] = array(
  105.                 'id' => $row['id_topic'],
  106.                 'id_cat' => $cats[$row['id_board']][0],
  107.                 'cat' => $cats[$row['id_board']][1],
  108.                 'message_id' => $row['id_msg'],
  109.                 'icon' => '<img class="icon" src="' . $settings[$icon_sources[$row['icon']]] . '/post/' . $row['icon'] . '.gif" align="middle" alt="' . $row['icon'] . '" border="0" />',
  110.                 'subject' => $row['subject'],
  111.                 'time' => timeformat($row['poster_time']),
  112.                 'timestamp' => forum_time(true, $row['poster_time']),
  113.                 'shortest' => $row['shortest'],
  114.                 'short' => $row['short'],
  115.                 'pic' => $boardurl . '/NewsImages/' . $row['pic'],
  116.                 'body' => $row['body'],
  117.                 'shortbody' => empty($shortbody) ? $row['body'] : $shortbody,
  118.                 'shortened' => !empty($shortbody),
  119.                 'href' => $scripturl . '?topic=' . $row['id_topic'] . '.0',
  120.                 'link' => '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.0">' . $row['num_replies'] . ' ' . ($row['num_replies'] == 1 ? 'Comment' : 'Comments') . '</a>',
  121.                 'replies' => $row['num_replies'],
  122.                 'comment_href' => !empty($row['locked']) ? '' : $scripturl . '?action=post;topic=' . $row['id_topic'] . '.' . $row['num_replies'] . ';num_replies=' . $row['num_replies'],
  123.                 'comment_link' => !empty($row['locked']) ? '' : '<a href="' . $scripturl . '?action=post;topic=' . $row['id_topic'] . '.' . $row['num_replies'] . ';num_replies=' . $row['num_replies'] . '">Write Comment</a>',
  124.                 'new_comment' => !empty($row['locked']) ? '' : '<a href="' . $scripturl . '?action=post;topic=' . $row['id_topic'] . '.' . $row['num_replies'] . '">Write Comment</a>',
  125.                 'poster' => array(
  126.                     'id' => $row['id_member'],
  127.                     'name' => $row['poster_name'],
  128.                     'href' => !empty($row['id_member']) ? $scripturl . '?action=profile;u=' . $row['id_member'] : '',
  129.                     'link' => !empty($row['id_member']) ? '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['poster_name'] . '</a>' : $row['poster_name']
  130.                 ),
  131.                 'locked' => !empty($row['locked']),
  132.                 'is_last' => false
  133.             );
  134.         }
  135.         $smcFunc['db_free_result']($request);
  136.         $context['news_posts'][count($context['news_posts']) - 1]['is_last'] = true;
  137.  
  138.     }
  139.  
  140.     return $context['news_posts'];
  141. }
  142.  
  143.  
  144. ?>
Add Comment
Please, Sign In to add comment