noticelist.php 4.28 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('STATUSNET') && !defined('LACONICA')) {
32 33 34
    exit(1);
}

35 36
require_once INSTALLDIR.'/lib/favorform.php';
require_once INSTALLDIR.'/lib/disfavorform.php';
37
require_once INSTALLDIR.'/lib/attachmentlist.php';
38

39 40 41 42 43 44 45 46 47
/**
 * 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
48
 * @package  StatusNet
49
 * @author   Evan Prodromou <evan@status.net>
50
 * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
51
 * @link     http://status.net/
52 53 54 55
 * @see      Notice
 * @see      NoticeListItem
 * @see      ProfileNoticeList
 */
56
class NoticeList extends Widget
57 58
{
    /** the current stream of notices being displayed. */
59

60
    var $notice = null;
61

62 63 64 65 66
    /**
     * constructor
     *
     * @param Notice $notice stream of notices from DB_DataObject
     */
67
    function __construct($notice, $out=null)
68
    {
Evan Prodromou's avatar
Evan Prodromou committed
69
        parent::__construct($out);
70 71 72
        $this->notice = $notice;
    }

73 74 75
    /**
     * show the list of notices
     *
76
     * "Uses up" the stream by looping through it. So, probably can't
77 78 79 80 81 82
     * be called twice on the same list.
     *
     * @return int count of notices listed.
     */
    function show()
    {
sarven's avatar
sarven committed
83
        $this->out->elementStart('div', array('id' =>'notices_primary'));
84
        $this->out->elementStart('ol', array('class' => 'notices xoxo'));
85

86
		$notices = $this->notice->fetchAll();
87
		$total   = count($notices);
88 89
		$notices = array_slice($notices, 0, NOTICES_PER_PAGE);
		
90
    	self::prefill($notices);
91 92
    	
    	foreach ($notices as $notice) {
93

94
            try {
95
                $item = $this->newListItem($notice);
96 97 98 99 100 101
                $item->show();
            } catch (Exception $e) {
                // we log exceptions and continue
                common_log(LOG_ERR, $e->getMessage());
                continue;
            }
102
        }
103

104
        $this->out->elementEnd('ol');
sarven's avatar
sarven committed
105
        $this->out->elementEnd('div');
106

107
        return $total;
108
    }
109

110 111 112 113 114 115 116 117 118 119 120 121
    /**
     * 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
     */
    function newListItem($notice)
    {
122
        return new NoticeListItem($notice, $this->out);
123
    }
124
    
125
    static function prefill(&$notices, $avatarSize=AVATAR_STREAM_SIZE)
126
    {
Evan Prodromou's avatar
Evan Prodromou committed
127 128
        // Prefill attachments
        Notice::fillAttachments($notices);
129 130
        // Prefill attachments
        Notice::fillFaves($notices);
131
    	// Prefill the profiles
132
    	$profiles = Notice::fillProfiles($notices);
133 134 135 136 137 138 139 140 141 142 143 144 145 146
    	// Prefill the avatars
    	Profile::fillAvatars($profiles, $avatarSize);
    	
    	$p = Profile::current();
    	
    	$ids = array();
    	
    	foreach ($notices as $notice) {
    	    $ids[] = $notice->id;
    	}
    	
    	if (!empty($p)) {
    		Memcached_DataObject::pivotGet('Fave', 'notice_id', $ids, array('user_id' => $p->id));
    	}
147
    }
148
}