3 * StatusNet, the distributed open-source microblogging tool
5 * widget for displaying a list of notices
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.
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.
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/>.
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/
31 if (!defined('GNUSOCIAL') && !defined('STATUSNET')) { exit(1); }
34 * widget for displaying a list of notices
36 * There are a number of actions that display a list of notices, in
37 * reverse chronological order. This widget abstracts out most of the
38 * code for UI for notice lists. It's overridden to hide some
39 * data for e.g. the profile page.
43 * @author Evan Prodromou <evan@status.net>
44 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
45 * @link http://status.net/
48 * @see ProfileNoticeList
50 class NoticeList extends Widget
52 /** the current stream of notices being displayed. */
56 protected $addressees = true;
57 protected $attachments = true;
58 protected $id_prefix = null;
59 protected $maxchars = 0;
60 protected $options = true;
61 protected $show_n = NOTICES_PER_PAGE;
66 * @param Notice $notice stream of notices from DB_DataObject
68 function __construct(Notice $notice, $out=null, array $prefs=array())
70 parent::__construct($out);
71 $this->notice = $notice;
73 // integer preferences
74 foreach(array('show_n', 'maxchars') as $key) {
75 if (array_key_exists($key, $prefs)) {
76 $this->$key = (int)$prefs[$key];
79 // boolean preferences
80 foreach(array('addressees', 'attachments', 'options') as $key) {
81 if (array_key_exists($key, $prefs)) {
82 $this->$key = (bool)$prefs[$key];
86 foreach(array('id_prefix') as $key) {
87 if (array_key_exists($key, $prefs)) {
88 $this->$key = $prefs[$key];
94 * show the list of notices
96 * "Uses up" the stream by looping through it. So, probably can't
97 * be called twice on the same list.
99 * @param integer $n The amount of notices to show.
101 * @return int Total amount of notices actually available.
103 public function show()
105 $this->out->elementStart('ol', array('class' => 'notices xoxo'));
107 $notices = $this->notice->fetchAll();
108 $total = count($notices);
109 $notices = array_slice($notices, 0, $this->show_n);
111 self::prefill($notices);
113 foreach ($notices as $notice) {
116 $item = $this->newListItem($notice);
118 } catch (Exception $e) {
119 // we log exceptions and continue
120 common_log(LOG_ERR, $e->getMessage());
125 $this->out->elementEnd('ol');
130 * returns a new list item for the current notice
132 * Recipe (factory?) method; overridden by sub-classes to give
133 * a different list item class.
135 * @param Notice $notice the current notice
137 * @return NoticeListItem a list item for displaying the notice
139 function newListItem(Notice $notice)
141 $prefs = array('addressees' => $this->addressees,
142 'attachments' => $this->attachments,
143 'id_prefix' => $this->id_prefix,
144 'maxchars' => $this->maxchars,
145 'options' => $this->options);
146 return new NoticeListItem($notice, $this->out, $prefs);
149 static function prefill(array &$notices)
151 $scoped = Profile::current();
152 $notice_ids = Notice::_idsOf($notices);
154 if (Event::handle('StartNoticeListPrefill', array(&$notices, $notice_ids, $scoped))) {
156 // Prefill attachments
157 Notice::fillAttachments($notices);
158 // Prefill the profiles
159 $profiles = Notice::fillProfiles($notices);
161 Event::handle('EndNoticeListPrefill', array(&$notices, &$profiles, $notice_ids, $scoped));