PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/sources/subs/Recent.subs.php

https://github.com/Arantor/Elkarte
PHP | 116 lines | 77 code | 8 blank | 31 comment | 4 complexity | 10e51e49f2ad30fea66c704401f1e648 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-3.0
  1. <?php
  2. /**
  3. * @name ElkArte Forum
  4. * @copyright ElkArte Forum contributors
  5. * @license BSD http://opensource.org/licenses/BSD-3-Clause
  6. *
  7. * This software is a derived product, based on:
  8. *
  9. * Simple Machines Forum (SMF)
  10. * copyright: 2011 Simple Machines (http://www.simplemachines.org)
  11. * license: BSD, See included LICENSE.TXT for terms and conditions.
  12. *
  13. * @version 1.0 Alpha
  14. *
  15. * This file contains a couple of functions for the latests posts on forum.
  16. *
  17. */
  18. if (!defined('ELKARTE'))
  19. die('No access...');
  20. /**
  21. * Get the latest posts of a forum.
  22. *
  23. * @param array $latestPostOptions
  24. * @return array
  25. */
  26. function getLastPosts($latestPostOptions)
  27. {
  28. global $scripturl, $txt, $user_info, $modSettings, $smcFunc, $context;
  29. // Find all the posts. Newer ones will have higher IDs. (assuming the last 20 * number are accessable...)
  30. // @todo SLOW This query is now slow, NEEDS to be fixed. Maybe break into two?
  31. $request = $smcFunc['db_query']('substring', '
  32. SELECT
  33. m.poster_time, m.subject, m.id_topic, m.id_member, m.id_msg,
  34. IFNULL(mem.real_name, m.poster_name) AS poster_name, t.id_board, b.name AS board_name,
  35. SUBSTRING(m.body, 1, 385) AS body, m.smileys_enabled
  36. FROM {db_prefix}messages AS m
  37. INNER JOIN {db_prefix}topics AS t ON (t.id_topic = m.id_topic)
  38. INNER JOIN {db_prefix}boards AS b ON (b.id_board = t.id_board)
  39. LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = m.id_member)
  40. WHERE m.id_msg >= {int:likely_max_msg}' .
  41. (!empty($modSettings['recycle_enable']) && $modSettings['recycle_board'] > 0 ? '
  42. AND b.id_board != {int:recycle_board}' : '') . '
  43. AND {query_wanna_see_board}' . ($modSettings['postmod_active'] ? '
  44. AND t.approved = {int:is_approved}
  45. AND m.approved = {int:is_approved}' : '') . '
  46. ORDER BY m.id_msg DESC
  47. LIMIT ' . $latestPostOptions['number_posts'],
  48. array(
  49. 'likely_max_msg' => max(0, $modSettings['maxMsgID'] - 50 * $latestPostOptions['number_posts']),
  50. 'recycle_board' => $modSettings['recycle_board'],
  51. 'is_approved' => 1,
  52. )
  53. );
  54. $posts = array();
  55. while ($row = $smcFunc['db_fetch_assoc']($request))
  56. {
  57. // Censor the subject and post for the preview ;).
  58. censorText($row['subject']);
  59. censorText($row['body']);
  60. $row['body'] = strip_tags(strtr(parse_bbc($row['body'], $row['smileys_enabled'], $row['id_msg']), array('<br />' => '&#10;')));
  61. if ($smcFunc['strlen']($row['body']) > 128)
  62. $row['body'] = $smcFunc['substr']($row['body'], 0, 128) . '...';
  63. // Build the array.
  64. $posts[] = array(
  65. 'board' => array(
  66. 'id' => $row['id_board'],
  67. 'name' => $row['board_name'],
  68. 'href' => $scripturl . '?board=' . $row['id_board'] . '.0',
  69. 'link' => '<a href="' . $scripturl . '?board=' . $row['id_board'] . '.0">' . $row['board_name'] . '</a>'
  70. ),
  71. 'topic' => $row['id_topic'],
  72. 'poster' => array(
  73. 'id' => $row['id_member'],
  74. 'name' => $row['poster_name'],
  75. 'href' => empty($row['id_member']) ? '' : $scripturl . '?action=profile;u=' . $row['id_member'],
  76. 'link' => empty($row['id_member']) ? $row['poster_name'] : '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['poster_name'] . '</a>'
  77. ),
  78. 'subject' => $row['subject'],
  79. 'short_subject' => shorten_subject($row['subject'], 24),
  80. 'preview' => $row['body'],
  81. 'time' => timeformat($row['poster_time']),
  82. 'timestamp' => forum_time(true, $row['poster_time']),
  83. 'raw_timestamp' => $row['poster_time'],
  84. 'href' => $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['id_msg'] . ';topicseen#msg' . $row['id_msg'],
  85. 'link' => '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['id_msg'] . ';topicseen#msg' . $row['id_msg'] . '" rel="nofollow">' . $row['subject'] . '</a>'
  86. );
  87. }
  88. $smcFunc['db_free_result']($request);
  89. return $posts;
  90. }
  91. /**
  92. * Callback-function for the cache for getLastPosts().
  93. *
  94. * @param array $latestPostOptions
  95. */
  96. function cache_getLastPosts($latestPostOptions)
  97. {
  98. return array(
  99. 'data' => getLastPosts($latestPostOptions),
  100. 'expires' => time() + 60,
  101. 'post_retri_eval' => '
  102. foreach ($cache_block[\'data\'] as $k => $post)
  103. {
  104. $cache_block[\'data\'][$k][\'time\'] = timeformat($post[\'raw_timestamp\']);
  105. $cache_block[\'data\'][$k][\'timestamp\'] = forum_time(true, $post[\'raw_timestamp\']);
  106. }',
  107. );
  108. }