]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/noticelist.php
Stronger typing for NoticeListItem and so
[quix0rs-gnu-social.git] / lib / noticelist.php
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
31 if (!defined('GNUSOCIAL') && !defined('STATUSNET')) { exit(1); }
32
33 /**
34  * widget for displaying a list of notices
35  *
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.
40  *
41  * @category UI
42  * @package  StatusNet
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/
46  * @see      Notice
47  * @see      NoticeListItem
48  * @see      ProfileNoticeList
49  */
50 class NoticeList extends Widget
51 {
52     /** the current stream of notices being displayed. */
53
54     var $notice = null;
55
56     /**
57      * constructor
58      *
59      * @param Notice $notice stream of notices from DB_DataObject
60      */
61     function __construct(Notice $notice, $out=null)
62     {
63         parent::__construct($out);
64         $this->notice = $notice;
65     }
66
67     /**
68      * show the list of notices
69      *
70      * "Uses up" the stream by looping through it. So, probably can't
71      * be called twice on the same list.
72      *
73      * @return int count of notices listed.
74      */
75     function show()
76     {
77         $this->out->elementStart('div', array('id' =>'notices_primary'));
78         $this->out->elementStart('ol', array('class' => 'notices xoxo'));
79
80                 $notices = $this->notice->fetchAll();
81                 $total   = count($notices);
82                 $notices = array_slice($notices, 0, NOTICES_PER_PAGE);
83                 
84         self::prefill($notices);
85         
86         foreach ($notices as $notice) {
87
88             try {
89                 $item = $this->newListItem($notice);
90                 $item->show();
91             } catch (Exception $e) {
92                 // we log exceptions and continue
93                 common_log(LOG_ERR, $e->getMessage());
94                 continue;
95             }
96         }
97
98         $this->out->elementEnd('ol');
99         $this->out->elementEnd('div');
100
101         return $total;
102     }
103
104     /**
105      * returns a new list item for the current notice
106      *
107      * Recipe (factory?) method; overridden by sub-classes to give
108      * a different list item class.
109      *
110      * @param Notice $notice the current notice
111      *
112      * @return NoticeListItem a list item for displaying the notice
113      */
114     function newListItem(Notice $notice)
115     {
116         return new NoticeListItem($notice, $this->out);
117     }
118     
119     static function prefill(&$notices, $avatarSize=AVATAR_STREAM_SIZE)
120     {
121         if (Event::handle('StartNoticeListPrefill', array(&$notices, $avatarSize))) {
122
123             // Prefill attachments
124             Notice::fillAttachments($notices);
125             // Prefill attachments
126             Notice::fillFaves($notices);
127             // Prefill repeat data
128             Notice::fillRepeats($notices);
129             // Prefill the profiles
130             $profiles = Notice::fillProfiles($notices);
131         
132             $p = Profile::current();
133             if ($p instanceof Profile) {
134
135                 $ids = array();
136         
137                 foreach ($notices as $notice) {
138                     $ids[] = $notice->id;
139                 }
140         
141                 Fave::pivotGet('notice_id', $ids, array('user_id' => $p->id));
142                 Notice::pivotGet('repeat_of', $ids, array('profile_id' => $p->id));
143             }
144
145             Event::handle('EndNoticeListPrefill', array(&$notices, &$profiles, $avatarSize));
146         }
147     }
148 }