noticelist.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 Evan Prodromou <evan@status.net>
  25. * @author Sarven Capadisli <csarven@status.net>
  26. * @copyright 2008 StatusNet, Inc.
  27. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  28. * @link http://status.net/
  29. */
  30. if (!defined('GNUSOCIAL') && !defined('STATUSNET')) { exit(1); }
  31. /**
  32. * widget for displaying a list of notices
  33. *
  34. * There are a number of actions that display a list of notices, in
  35. * reverse chronological order. This widget abstracts out most of the
  36. * code for UI for notice lists. It's overridden to hide some
  37. * data for e.g. the profile page.
  38. *
  39. * @category UI
  40. * @package StatusNet
  41. * @author Evan Prodromou <evan@status.net>
  42. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  43. * @link http://status.net/
  44. * @see Notice
  45. * @see NoticeListItem
  46. * @see ProfileNoticeList
  47. */
  48. class NoticeList extends Widget
  49. {
  50. /** the current stream of notices being displayed. */
  51. var $notice = null;
  52. protected $addressees = true;
  53. protected $attachments = true;
  54. protected $id_prefix = null;
  55. protected $maxchars = 0;
  56. protected $options = true;
  57. protected $show_n = NOTICES_PER_PAGE;
  58. /**
  59. * constructor
  60. *
  61. * @param Notice $notice stream of notices from DB_DataObject
  62. */
  63. function __construct(Notice $notice, $out=null, array $prefs=array())
  64. {
  65. parent::__construct($out);
  66. $this->notice = $notice;
  67. // integer preferences
  68. foreach(array('show_n', 'maxchars') as $key) {
  69. if (array_key_exists($key, $prefs)) {
  70. $this->$key = (int)$prefs[$key];
  71. }
  72. }
  73. // boolean preferences
  74. foreach(array('addressees', 'attachments', 'options') as $key) {
  75. if (array_key_exists($key, $prefs)) {
  76. $this->$key = (bool)$prefs[$key];
  77. }
  78. }
  79. // string preferences
  80. foreach(array('id_prefix') as $key) {
  81. if (array_key_exists($key, $prefs)) {
  82. $this->$key = $prefs[$key];
  83. }
  84. }
  85. }
  86. /**
  87. * show the list of notices
  88. *
  89. * "Uses up" the stream by looping through it. So, probably can't
  90. * be called twice on the same list.
  91. *
  92. * @param integer $n The amount of notices to show.
  93. *
  94. * @return int Total amount of notices actually available.
  95. */
  96. public function show()
  97. {
  98. $this->out->elementStart('ol', array('class' => 'notices xoxo'));
  99. $notices = $this->notice->fetchAll();
  100. $total = count($notices);
  101. $notices = array_slice($notices, 0, $this->show_n);
  102. self::prefill($notices);
  103. foreach ($notices as $notice) {
  104. try {
  105. $item = $this->newListItem($notice);
  106. $item->show();
  107. } catch (Exception $e) {
  108. // we log exceptions and continue
  109. common_log(LOG_ERR, $e->getMessage());
  110. continue;
  111. }
  112. }
  113. $this->out->elementEnd('ol');
  114. return $total;
  115. }
  116. /**
  117. * returns a new list item for the current notice
  118. *
  119. * Recipe (factory?) method; overridden by sub-classes to give
  120. * a different list item class.
  121. *
  122. * @param Notice $notice the current notice
  123. *
  124. * @return NoticeListItem a list item for displaying the notice
  125. */
  126. function newListItem(Notice $notice)
  127. {
  128. $prefs = array('addressees' => $this->addressees,
  129. 'attachments' => $this->attachments,
  130. 'id_prefix' => $this->id_prefix,
  131. 'maxchars' => $this->maxchars,
  132. 'options' => $this->options);
  133. return new NoticeListItem($notice, $this->out, $prefs);
  134. }
  135. static function prefill(array &$notices)
  136. {
  137. $scoped = Profile::current();
  138. $notice_ids = Notice::_idsOf($notices);
  139. if (Event::handle('StartNoticeListPrefill', array(&$notices, $notice_ids, $scoped))) {
  140. // Prefill attachments
  141. Notice::fillAttachments($notices);
  142. // Prefill the profiles
  143. $profiles = Notice::fillProfiles($notices);
  144. Event::handle('EndNoticeListPrefill', array(&$notices, &$profiles, $notice_ids, $scoped));
  145. }
  146. }
  147. }