noticelist.php 5.12 KB
Newer Older
1
<?php
2
/**
3
 * StatusNet, the distributed open-source microblogging tool
4
 *
5
 * widget for displaying a list of notices
6 7 8 9
 *
 * PHP version 5
 *
 * LICENCE: This program is free software: you can redistribute it and/or modify
10 11 12 13 14 15
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 18 19
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
20 21 22
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @category  UI
23
 * @package   StatusNet
24 25
 * @author    Evan Prodromou <evan@status.net>
 * @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 35 36 37 38 39 40 41
/**
 * widget for displaying a list of notices
 *
 * There are a number of actions that display a list of notices, in
 * reverse chronological order. This widget abstracts out most of the
 * code for UI for notice lists. It's overridden to hide some
 * data for e.g. the profile page.
 *
 * @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 47 48 49
 * @see      Notice
 * @see      NoticeListItem
 * @see      ProfileNoticeList
 */
50
class NoticeList extends Widget
51 52
{
    /** the current stream of notices being displayed. */
53

54
    var $notice = null;
55

56 57
    protected $addressees = true;
    protected $attachments = true;
58
    protected $id_prefix = null;
59 60 61 62
    protected $maxchars = 0;
    protected $options = true;
    protected $show_n = NOTICES_PER_PAGE;

63 64 65 66 67
    /**
     * constructor
     *
     * @param Notice $notice stream of notices from DB_DataObject
     */
68
    function __construct(Notice $notice, $out=null, array $prefs=array())
69
    {
70
        parent::__construct($out);
71
        $this->notice = $notice;
72 73 74 75 76 77 78 79 80 81 82 83 84

        // integer preferences
        foreach(array('show_n', 'maxchars') as $key) {
            if (array_key_exists($key, $prefs)) {
                $this->$key = (int)$prefs[$key];
            }
        }
        // boolean preferences
        foreach(array('addressees', 'attachments', 'options') as $key) {
            if (array_key_exists($key, $prefs)) {
                $this->$key = (bool)$prefs[$key];
            }
        }
85 86 87 88 89 90
        // string preferences
        foreach(array('id_prefix') as $key) {
            if (array_key_exists($key, $prefs)) {
                $this->$key = $prefs[$key];
            }
        }
91 92
    }

93 94 95
    /**
     * show the list of notices
     *
96
     * "Uses up" the stream by looping through it. So, probably can't
97 98
     * be called twice on the same list.
     *
99 100 101
     * @param integer $n    The amount of notices to show.
     *
     * @return int  Total amount of notices actually available.
102
     */
103
    public function show()
104
    {
105
        $this->out->elementStart('ol', array('class' => 'notices xoxo'));
106

107
		$notices = $this->notice->fetchAll();
108
		$total   = count($notices);
109
		$notices = array_slice($notices, 0, $this->show_n);
110
		
111
    	self::prefill($notices);
112 113
    	
    	foreach ($notices as $notice) {
114

115
            try {
116
                $item = $this->newListItem($notice);
117 118 119 120 121 122
                $item->show();
            } catch (Exception $e) {
                // we log exceptions and continue
                common_log(LOG_ERR, $e->getMessage());
                continue;
            }
123
        }
124

125
        $this->out->elementEnd('ol');
126
        return $total;
127
    }
128

129 130 131 132 133 134 135 136 137 138
    /**
     * returns a new list item for the current notice
     *
     * Recipe (factory?) method; overridden by sub-classes to give
     * a different list item class.
     *
     * @param Notice $notice the current notice
     *
     * @return NoticeListItem a list item for displaying the notice
     */
139
    function newListItem(Notice $notice)
140
    {
141 142
        $prefs = array('addressees' => $this->addressees,
                       'attachments' => $this->attachments,
143
                       'id_prefix' => $this->id_prefix,
144 145 146
                       'maxchars' => $this->maxchars,
                       'options' => $this->options);
        return new NoticeListItem($notice, $this->out, $prefs);
147
    }
148
    
149
    static function prefill(array &$notices)
150
    {
151 152 153 154
        $scoped = Profile::current();
        $notice_ids = Notice::_idsOf($notices);

        if (Event::handle('StartNoticeListPrefill', array(&$notices, $notice_ids, $scoped))) {
155 156 157 158 159

            // Prefill attachments
            Notice::fillAttachments($notices);
            // Prefill the profiles
            $profiles = Notice::fillProfiles($notices);
160

161
            Event::handle('EndNoticeListPrefill', array(&$notices, &$profiles, $notice_ids, $scoped));
162
        }
163
    }
164
}