threadednoticelist.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. /**
  3. * StatusNet, the distributed open-source microblogging tool
  4. *
  5. * widget for displaying a list of notices
  6. *
  7. * PHP version 5
  8. *
  9. * LICENCE: This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. * @category UI
  23. * @package StatusNet
  24. * @author Brion Vibber <brion@status.net>
  25. * @copyright 2011 StatusNet, Inc.
  26. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  27. * @link http://status.net/
  28. */
  29. if (!defined('GNUSOCIAL')) { exit(1); }
  30. /**
  31. * widget for displaying a list of notices
  32. *
  33. * There are a number of actions that display a list of notices, in
  34. * reverse chronological order. This widget abstracts out most of the
  35. * code for UI for notice lists. It's overridden to hide some
  36. * data for e.g. the profile page.
  37. *
  38. * @category UI
  39. * @package StatusNet
  40. * @author Evan Prodromou <evan@status.net>
  41. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  42. * @link http://status.net/
  43. * @see Notice
  44. * @see NoticeListItem
  45. * @see ProfileNoticeList
  46. */
  47. class ThreadedNoticeList extends NoticeList
  48. {
  49. public $widgetOpts;
  50. public $scoped;
  51. protected $userProfile;
  52. function __construct(Notice $notice, Action $out=null, $profile=-1)
  53. {
  54. parent::__construct($notice, $out);
  55. if (is_int($profile) && $profile == -1) {
  56. $profile = Profile::current();
  57. }
  58. $this->userProfile = $profile;
  59. }
  60. /**
  61. * show the list of notices
  62. *
  63. * "Uses up" the stream by looping through it. So, probably can't
  64. * be called twice on the same list.
  65. *
  66. * @return int count of notices listed.
  67. */
  68. function show()
  69. {
  70. $this->out->elementStart('div', array('id' =>'notices_primary'));
  71. // TRANS: Header for Notices section.
  72. $this->out->element('h2', null, _m('HEADER','Notices'));
  73. $this->out->elementStart('ol', array('class' => 'notices threaded-notices xoxo'));
  74. $notices = $this->notice->fetchAll();
  75. $total = count($notices);
  76. $notices = array_slice($notices, 0, NOTICES_PER_PAGE);
  77. $allnotices = self::_allNotices($notices);
  78. self::prefill($allnotices);
  79. $conversations = array();
  80. foreach ($notices as $notice) {
  81. // Collapse repeats into their originals...
  82. if ($notice->repeat_of) {
  83. $orig = Notice::getKV('id', $notice->repeat_of);
  84. if ($orig instanceof Notice) {
  85. $notice = $orig;
  86. }
  87. }
  88. $convo = $notice->conversation;
  89. if (!empty($conversations[$convo])) {
  90. // Seen this convo already -- skip!
  91. continue;
  92. }
  93. $conversations[$convo] = true;
  94. // Get the convo's root notice
  95. $root = $notice->conversationRoot($this->userProfile);
  96. if ($root instanceof Notice) {
  97. $notice = $root;
  98. }
  99. try {
  100. $item = $this->newListItem($notice);
  101. $item->show();
  102. } catch (Exception $e) {
  103. // we log exceptions and continue
  104. common_log(LOG_ERR, $e->getMessage());
  105. continue;
  106. }
  107. }
  108. $this->out->elementEnd('ol');
  109. $this->out->elementEnd('div');
  110. return $total;
  111. }
  112. function _allNotices($notices)
  113. {
  114. $convId = array();
  115. foreach ($notices as $notice) {
  116. $convId[] = $notice->conversation;
  117. }
  118. $convId = array_unique($convId);
  119. $allMap = Notice::listGet('conversation', $convId);
  120. $allArray = array();
  121. foreach ($allMap as $convId => $convNotices) {
  122. $allArray = array_merge($allArray, $convNotices);
  123. }
  124. return $allArray;
  125. }
  126. /**
  127. * returns a new list item for the current notice
  128. *
  129. * Recipe (factory?) method; overridden by sub-classes to give
  130. * a different list item class.
  131. *
  132. * @param Notice $notice the current notice
  133. *
  134. * @return NoticeListItem a list item for displaying the notice
  135. */
  136. function newListItem(Notice $notice)
  137. {
  138. return new ThreadedNoticeListItem($notice, $this->out, $this->userProfile);
  139. }
  140. }