threadednoticelist.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. protected $userProfile;
  50. function __construct(Notice $notice, Action $out=null, $profile=-1)
  51. {
  52. parent::__construct($notice, $out);
  53. if (is_int($profile) && $profile == -1) {
  54. $profile = Profile::current();
  55. }
  56. $this->userProfile = $profile;
  57. }
  58. /**
  59. * show the list of notices
  60. *
  61. * "Uses up" the stream by looping through it. So, probably can't
  62. * be called twice on the same list.
  63. *
  64. * @return int count of notices listed.
  65. */
  66. function show()
  67. {
  68. $this->out->elementStart('div', array('id' =>'notices_primary'));
  69. // TRANS: Header for Notices section.
  70. $this->out->element('h2', null, _m('HEADER','Notices'));
  71. $this->out->elementStart('ol', array('class' => 'notices threaded-notices xoxo'));
  72. $notices = $this->notice->fetchAll();
  73. $total = count($notices);
  74. $notices = array_slice($notices, 0, NOTICES_PER_PAGE);
  75. $allnotices = self::_allNotices($notices);
  76. self::prefill($allnotices);
  77. $conversations = array();
  78. foreach ($notices as $notice) {
  79. // Collapse repeats into their originals...
  80. if ($notice->repeat_of) {
  81. $orig = Notice::getKV('id', $notice->repeat_of);
  82. if ($orig instanceof Notice) {
  83. $notice = $orig;
  84. }
  85. }
  86. $convo = $notice->conversation;
  87. if (!empty($conversations[$convo])) {
  88. // Seen this convo already -- skip!
  89. continue;
  90. }
  91. $conversations[$convo] = true;
  92. // Get the convo's root notice
  93. $root = $notice->conversationRoot($this->userProfile);
  94. if ($root instanceof Notice) {
  95. $notice = $root;
  96. }
  97. try {
  98. $item = $this->newListItem($notice);
  99. $item->show();
  100. } catch (Exception $e) {
  101. // we log exceptions and continue
  102. common_log(LOG_ERR, $e->getMessage());
  103. continue;
  104. }
  105. }
  106. $this->out->elementEnd('ol');
  107. $this->out->elementEnd('div');
  108. return $total;
  109. }
  110. function _allNotices($notices)
  111. {
  112. $convId = array();
  113. foreach ($notices as $notice) {
  114. $convId[] = $notice->conversation;
  115. }
  116. $convId = array_unique($convId);
  117. $allMap = Notice::listGet('conversation', $convId);
  118. $allArray = array();
  119. foreach ($allMap as $convId => $convNotices) {
  120. $allArray = array_merge($allArray, $convNotices);
  121. }
  122. return $allArray;
  123. }
  124. /**
  125. * returns a new list item for the current notice
  126. *
  127. * Recipe (factory?) method; overridden by sub-classes to give
  128. * a different list item class.
  129. *
  130. * @param Notice $notice the current notice
  131. *
  132. * @return NoticeListItem a list item for displaying the notice
  133. */
  134. function newListItem(Notice $notice)
  135. {
  136. return new ThreadedNoticeListItem($notice, $this->out, $this->userProfile);
  137. }
  138. }