noticelist.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. public $widgetOpts;
  51. public $scoped;
  52. /** the current stream of notices being displayed. */
  53. var $notice = null;
  54. protected $addressees = true;
  55. protected $attachments = true;
  56. protected $id_prefix = null;
  57. protected $maxchars = 0;
  58. protected $options = true;
  59. protected $show_n = NOTICES_PER_PAGE;
  60. /**
  61. * constructor
  62. *
  63. * @param mixed $notice stream of notices from DB_DataObject (may be a Notice but can also be something like an ArrayWrapper)
  64. */
  65. function __construct($notice, $out = null, array $prefs = [])
  66. {
  67. parent::__construct($out);
  68. $this->notice = $notice;
  69. // integer preferences
  70. foreach(array('show_n', 'maxchars') as $key) {
  71. if (array_key_exists($key, $prefs)) {
  72. $this->$key = (int)$prefs[$key];
  73. }
  74. }
  75. // boolean preferences
  76. foreach(array('addressees', 'attachments', 'options') as $key) {
  77. if (array_key_exists($key, $prefs)) {
  78. $this->$key = (bool)$prefs[$key];
  79. }
  80. }
  81. // string preferences
  82. foreach(array('id_prefix') as $key) {
  83. if (array_key_exists($key, $prefs)) {
  84. $this->$key = $prefs[$key];
  85. }
  86. }
  87. }
  88. /**
  89. * show the list of notices
  90. *
  91. * "Uses up" the stream by looping through it. So, probably can't
  92. * be called twice on the same list.
  93. *
  94. * @param integer $n The amount of notices to show.
  95. *
  96. * @return int Total amount of notices actually available.
  97. */
  98. public function show()
  99. {
  100. $this->out->elementStart('ol', array('class' => 'notices xoxo'));
  101. $notices = $this->notice->fetchAll();
  102. $total = count($notices);
  103. $notices = array_slice($notices, 0, $this->show_n);
  104. self::prefill($notices);
  105. foreach ($notices as $notice) {
  106. try {
  107. $item = $this->newListItem($notice);
  108. $item->show();
  109. } catch (Exception $e) {
  110. // we log exceptions and continue
  111. common_log(LOG_ERR, $e->getMessage());
  112. continue;
  113. }
  114. }
  115. $this->out->elementEnd('ol');
  116. return $total;
  117. }
  118. /**
  119. * returns a new list item for the current notice
  120. *
  121. * Recipe (factory?) method; overridden by sub-classes to give
  122. * a different list item class.
  123. *
  124. * @param Notice $notice the current notice
  125. *
  126. * @return NoticeListItem a list item for displaying the notice
  127. */
  128. function newListItem(Notice $notice)
  129. {
  130. $prefs = array('addressees' => $this->addressees,
  131. 'attachments' => $this->attachments,
  132. 'id_prefix' => $this->id_prefix,
  133. 'maxchars' => $this->maxchars,
  134. 'options' => $this->options);
  135. return new NoticeListItem($notice, $this->out, $prefs);
  136. }
  137. static function prefill(array &$notices)
  138. {
  139. $scoped = Profile::current();
  140. $notice_ids = Notice::_idsOf($notices);
  141. if (Event::handle('StartNoticeListPrefill', array(&$notices, $notice_ids, $scoped))) {
  142. // Prefill attachments
  143. Notice::fillAttachments($notices);
  144. // Prefill the profiles
  145. $profiles = Notice::fillProfiles($notices);
  146. Event::handle('EndNoticeListPrefill', array(&$notices, &$profiles, $notice_ids, $scoped));
  147. }
  148. }
  149. }