showfavorites.php 9.36 KB
Newer Older
1
<?php
Evan Prodromou's avatar
Evan Prodromou committed
2
/**
3
 * StatusNet, the distributed open-source microblogging tool
4
 *
Evan Prodromou's avatar
Evan Prodromou committed
5 6 7 8 9
 * List of replies
 *
 * PHP version 5
 *
 * LICENCE: This program is free software: you can redistribute it and/or modify
10 11 12 13 14 15 16 17 18 19 20
 * 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
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
Evan Prodromou's avatar
Evan Prodromou committed
21 22
 *
 * @category  Personal
23
 * @package   StatusNet
24
 * @author    Evan Prodromou <evan@status.net>
25
 * @copyright 2008-2011 StatusNet, Inc.
Evan Prodromou's avatar
Evan Prodromou committed
26
 * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27
 * @link      http://status.net/
28 29
 */

30
if (!defined('STATUSNET') && !defined('LACONICA')) {
Evan Prodromou's avatar
Evan Prodromou committed
31 32
    exit(1);
}
33

Evan Prodromou's avatar
Evan Prodromou committed
34 35 36
require_once INSTALLDIR.'/lib/personalgroupnav.php';
require_once INSTALLDIR.'/lib/noticelist.php';
require_once INSTALLDIR.'/lib/feedlist.php';
37

Evan Prodromou's avatar
Evan Prodromou committed
38 39 40 41
/**
 * List of replies
 *
 * @category Personal
42
 * @package  StatusNet
43
 * @author   Evan Prodromou <evan@status.net>
Evan Prodromou's avatar
Evan Prodromou committed
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/
Evan Prodromou's avatar
Evan Prodromou committed
46
 */
47
class ShowfavoritesAction extends Action
Evan Prodromou's avatar
Evan Prodromou committed
48 49 50 51 52 53
{
    /** User we're getting the faves of */
    var $user = null;
    /** Page of the faves we're on */
    var $page = null;

Evan Prodromou's avatar
Evan Prodromou committed
54 55 56 57 58
    /**
     * Is this a read-only page?
     *
     * @return boolean true
     */
59
    function isReadOnly($args)
Evan Prodromou's avatar
Evan Prodromou committed
60 61 62 63
    {
        return true;
    }

Evan Prodromou's avatar
Evan Prodromou committed
64 65 66 67 68 69 70 71
    /**
     * Title of the page
     *
     * Includes name of user and page number.
     *
     * @return string title of page
     */
    function title()
72
    {
Evan Prodromou's avatar
Evan Prodromou committed
73
        if ($this->page == 1) {
74 75
            // TRANS: Title for first page of favourite notices of a user.
            // TRANS: %s is the user for whom the favourite notices are displayed.
76
            return sprintf(_('%s\'s favorite notices'), $this->user->nickname);
Evan Prodromou's avatar
Evan Prodromou committed
77
        } else {
78 79
            // TRANS: Title for all but the first page of favourite notices of a user.
            // TRANS: %1$s is the user for whom the favourite notices are displayed, %2$d is the page number.
80
            return sprintf(_('%1$s\'s favorite notices, page %2$d'),
Evan Prodromou's avatar
Evan Prodromou committed
81 82 83 84
                           $this->user->nickname,
                           $this->page);
        }
    }
85

Evan Prodromou's avatar
Evan Prodromou committed
86 87 88 89 90 91 92 93 94 95 96 97 98
    /**
     * Prepare the object
     *
     * Check the input values and initialize the object.
     * Shows an error page on bad input.
     *
     * @param array $args $_REQUEST data
     *
     * @return boolean success flag
     */
    function prepare($args)
    {
        parent::prepare($args);
99

100
        $nickname = common_canonical_nickname($this->arg('nickname'));
101

Evan Prodromou's avatar
Evan Prodromou committed
102 103 104
        $this->user = User::staticGet('nickname', $nickname);

        if (!$this->user) {
105
            // TRANS: Client error displayed when trying to display favourite notices for a non-existing user.
106
            $this->clientError(_('No such user.'));
Evan Prodromou's avatar
Evan Prodromou committed
107
            return false;
108
        }
109

Evan Prodromou's avatar
Evan Prodromou committed
110
        $this->page = $this->trimmed('page');
111

Evan Prodromou's avatar
Evan Prodromou committed
112 113
        if (!$this->page) {
            $this->page = 1;
114
        }
115

116
        common_set_returnto($this->selfUrl());
117

118 119 120 121 122
        $cur = common_current_user();

        if (!empty($cur) && $cur->id == $this->user->id) {

            // Show imported/gateway notices as well as local if
123
            // the user is looking at their own favorites
124

125 126
            $this->notice = $this->user->favoriteNotices(true, ($this->page-1)*NOTICES_PER_PAGE,
                                                   NOTICES_PER_PAGE + 1);
127
        } else {
128 129
            $this->notice = $this->user->favoriteNotices(false, ($this->page-1)*NOTICES_PER_PAGE,
                                                   NOTICES_PER_PAGE + 1);
130 131 132
        }

        if (empty($this->notice)) {
133
            // TRANS: Server error displayed when favourite notices could not be retrieved from the database.
134 135 136 137 138
            $this->serverError(_('Could not retrieve favorite notices.'));
            return;
        }

        if($this->page > 1 && $this->notice->N == 0){
139
            // TRANS: Server error when page not found (404)
140
            $this->serverError(_('No such page.'),$code=404);
141 142
        }

Evan Prodromou's avatar
Evan Prodromou committed
143 144
        return true;
    }
145

Evan Prodromou's avatar
Evan Prodromou committed
146 147 148 149 150 151 152 153 154 155 156 157 158
    /**
     * Handle a request
     *
     * Just show the page. All args already handled.
     *
     * @param array $args $_REQUEST data
     *
     * @return void
     */
    function handle($args)
    {
        parent::handle($args);
        $this->showPage();
159
    }
160

Evan Prodromou's avatar
Evan Prodromou committed
161 162 163
    /**
     * Feeds for the <head> section
     *
164
     * @return array Feed objects to show
Evan Prodromou's avatar
Evan Prodromou committed
165
     */
166
    function getFeeds()
167
    {
168 169 170 171 172 173 174 175 176
        return array(new Feed(Feed::JSON,
                              common_local_url('ApiTimelineFavorites',
                                               array(
                                                    'id' => $this->user->nickname,
                                                    'format' => 'as')),
                              // TRANS: Feed link text. %s is a username.
                              sprintf(_('Feed for favorites of %s (Activity Streams JSON)'),
                                      $this->user->nickname)),
                     new Feed(Feed::RSS1,
177 178
                              common_local_url('favoritesrss',
                                               array('nickname' => $this->user->nickname)),
179
                              // TRANS: Feed link text. %s is a username.
180 181 182 183 184 185 186
                              sprintf(_('Feed for favorites of %s (RSS 1.0)'),
                                      $this->user->nickname)),
                     new Feed(Feed::RSS2,
                              common_local_url('ApiTimelineFavorites',
                                               array(
                                                    'id' => $this->user->nickname,
                                                    'format' => 'rss')),
187
                              // TRANS: Feed link text. %s is a username.
188 189 190 191 192 193 194
                              sprintf(_('Feed for favorites of %s (RSS 2.0)'),
                                      $this->user->nickname)),
                     new Feed(Feed::ATOM,
                              common_local_url('ApiTimelineFavorites',
                                               array(
                                                    'id' => $this->user->nickname,
                                                    'format' => 'atom')),
195
                              // TRANS: Feed link text. %s is a username.
196 197
                              sprintf(_('Feed for favorites of %s (Atom)'),
                                      $this->user->nickname)));
198
    }
199

200 201 202 203 204
    function showEmptyListMessage()
    {
        if (common_logged_in()) {
            $current_user = common_current_user();
            if ($this->user->id === $current_user->id) {
205
                // TRANS: Text displayed instead of favourite notices for the current logged in user that has no favourites.
206 207
                $message = _('You haven\'t chosen any favorite notices yet. Click the fave button on notices you like to bookmark them for later or shed a spotlight on them.');
            } else {
208 209
                // TRANS: Text displayed instead of favourite notices for a user that has no favourites while logged in.
                // TRANS: %s is a username.
210
                $message = sprintf(_('%s hasn\'t added any favorite notices yet. Post something interesting they would add to their favorites :)'), $this->user->nickname);
211 212 213
            }
        }
        else {
214 215 216
                // TRANS: Text displayed instead of favourite notices for a user that has no favourites while not logged in.
                // TRANS: %s is a username, %%%%action.register%%%% is a link to the user registration page.
                // TRANS: (link text)[link] is a Mark Down link.
217
            $message = sprintf(_('%s hasn\'t added any favorite notices yet. Why not [register an account](%%%%action.register%%%%) and then post something interesting they would add to their favorites :)'), $this->user->nickname);
218 219 220 221 222 223 224
        }

        $this->elementStart('div', 'guide');
        $this->raw(common_markup_to_html($message));
        $this->elementEnd('div');
    }

Evan Prodromou's avatar
Evan Prodromou committed
225 226 227 228 229 230 231 232 233
    /**
     * Show the content
     *
     * A list of notices that this user has marked as a favorite
     *
     * @return void
     */
    function showContent()
    {
234
        $nl = new FavoritesNoticeList($this->notice, $this);
Evan Prodromou's avatar
Evan Prodromou committed
235 236

        $cnt = $nl->show();
237
        if (0 == $cnt) {
238
            $this->showEmptyListMessage();
239
        }
240

Evan Prodromou's avatar
Evan Prodromou committed
241 242 243
        $this->pagination($this->page > 1, $cnt > NOTICES_PER_PAGE,
                          $this->page, 'showfavorites',
                          array('nickname' => $this->user->nickname));
244
    }
245 246

    function showPageNotice() {
247
        // TRANS: Page notice for show favourites page.
248 249
        $this->element('p', 'instructions', _('This is a way to share what you like.'));
    }
250
}
251

252 253 254 255 256 257 258 259 260 261 262 263
class FavoritesNoticeList extends NoticeList
{
    function newListItem($notice)
    {
        return new FavoritesNoticeListItem($notice, $this->out);
    }
}

// All handled by superclass
class FavoritesNoticeListItem extends DoFollowListItem
{
}