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