]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Favorite/actions/showfavorites.php
Added more checked type-hints.
[quix0rs-gnu-social.git] / plugins / Favorite / actions / showfavorites.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * List of replies
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  Personal
23  * @package   StatusNet
24  * @author    Evan Prodromou <evan@status.net>
25  * @copyright 2008-2011 StatusNet, Inc.
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')) {
31     exit(1);
32 }
33
34 require_once INSTALLDIR.'/lib/personalgroupnav.php';
35 require_once INSTALLDIR.'/lib/noticelist.php';
36 require_once INSTALLDIR.'/lib/feedlist.php';
37
38 /**
39  * List of replies
40  *
41  * @category Personal
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 class ShowfavoritesAction extends Action
48 {
49     /** User we're getting the faves of */
50     var $user = null;
51     /** Page of the faves we're on */
52     var $page = null;
53
54     /**
55      * Is this a read-only page?
56      *
57      * @return boolean true
58      */
59     function isReadOnly(array $args=array())
60     {
61         return true;
62     }
63
64     /**
65      * Title of the page
66      *
67      * Includes name of user and page number.
68      *
69      * @return string title of page
70      */
71     function title()
72     {
73         if ($this->page == 1) {
74             // TRANS: Title for first page of favourite notices of a user.
75             // TRANS: %s is the user for whom the favourite notices are displayed.
76             return sprintf(_('%s\'s favorite notices'), $this->user->nickname);
77         } else {
78             // TRANS: Title for all but the first page of favourite notices of a user.
79             // 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'),
81                            $this->user->nickname,
82                            $this->page);
83         }
84     }
85
86     /**
87      * Prepare the object
88      *
89      * Check the input values and initialize the object.
90      * Shows an error page on bad input.
91      *
92      * @param array $args $_REQUEST data
93      *
94      * @return boolean success flag
95      */
96     function prepare(array $args=array())
97     {
98         parent::prepare($args);
99
100         $nickname = common_canonical_nickname($this->arg('nickname'));
101
102         $this->user = User::getKV('nickname', $nickname);
103
104         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.'));
107         }
108
109         $this->page = $this->trimmed('page');
110
111         if (!$this->page) {
112             $this->page = 1;
113         }
114
115         common_set_returnto($this->selfUrl());
116
117         $cur = common_current_user();
118
119         // Show imported/gateway notices as well as local if
120         // the user is looking at their own favorites, otherwise not.
121         $this->notice = Fave::stream($this->user->id,
122                                      ($this->page-1)*NOTICES_PER_PAGE,  // offset
123                                      NOTICES_PER_PAGE + 1,              // limit
124                                      (!empty($cur) && $cur->id == $this->user->id)  // own feed?
125                                     );
126
127         if (empty($this->notice)) {
128             // TRANS: Server error displayed when favourite notices could not be retrieved from the database.
129             $this->serverError(_('Could not retrieve favorite notices.'));
130         }
131
132         if($this->page > 1 && $this->notice->N == 0){
133             // TRANS: Client error when page not found (404)
134             $this->clientError(_('No such page.'), 404);
135         }
136
137         return true;
138     }
139
140     /**
141      * Handle a request
142      *
143      * Just show the page. All args already handled.
144      *
145      * @param array $args $_REQUEST data
146      *
147      * @return void
148      */
149     function handle(array $args=array())
150     {
151         parent::handle($args);
152         $this->showPage();
153     }
154
155     /**
156      * Feeds for the <head> section
157      *
158      * @return array Feed objects to show
159      */
160     function getFeeds()
161     {
162         return array(new Feed(Feed::JSON,
163                               common_local_url('ApiTimelineFavorites',
164                                                array(
165                                                     'id' => $this->user->nickname,
166                                                     'format' => 'as')),
167                               // TRANS: Feed link text. %s is a username.
168                               sprintf(_('Feed for favorites of %s (Activity Streams JSON)'),
169                                       $this->user->nickname)),
170                      new Feed(Feed::RSS1,
171                               common_local_url('favoritesrss',
172                                                array('nickname' => $this->user->nickname)),
173                               // TRANS: Feed link text. %s is a username.
174                               sprintf(_('Feed for favorites of %s (RSS 1.0)'),
175                                       $this->user->nickname)),
176                      new Feed(Feed::RSS2,
177                               common_local_url('ApiTimelineFavorites',
178                                                array(
179                                                     'id' => $this->user->nickname,
180                                                     'format' => 'rss')),
181                               // TRANS: Feed link text. %s is a username.
182                               sprintf(_('Feed for favorites of %s (RSS 2.0)'),
183                                       $this->user->nickname)),
184                      new Feed(Feed::ATOM,
185                               common_local_url('ApiTimelineFavorites',
186                                                array(
187                                                     'id' => $this->user->nickname,
188                                                     'format' => 'atom')),
189                               // TRANS: Feed link text. %s is a username.
190                               sprintf(_('Feed for favorites of %s (Atom)'),
191                                       $this->user->nickname)));
192     }
193
194     function showEmptyListMessage()
195     {
196         if (common_logged_in()) {
197             $current_user = common_current_user();
198             if ($this->user->id === $current_user->id) {
199                 // TRANS: Text displayed instead of favourite notices for the current logged in user that has no favourites.
200                 $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.');
201             } else {
202                 // TRANS: Text displayed instead of favourite notices for a user that has no favourites while logged in.
203                 // TRANS: %s is a username.
204                 $message = sprintf(_('%s hasn\'t added any favorite notices yet. Post something interesting they would add to their favorites :)'), $this->user->nickname);
205             }
206         }
207         else {
208                 // TRANS: Text displayed instead of favourite notices for a user that has no favourites while not logged in.
209                 // TRANS: %s is a username, %%%%action.register%%%% is a link to the user registration page.
210                 // TRANS: (link text)[link] is a Mark Down link.
211             $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);
212         }
213
214         $this->elementStart('div', 'guide');
215         $this->raw(common_markup_to_html($message));
216         $this->elementEnd('div');
217     }
218
219     /**
220      * Show the content
221      *
222      * A list of notices that this user has marked as a favorite
223      *
224      * @return void
225      */
226     function showContent()
227     {
228         $nl = new FavoritesNoticeList($this->notice, $this);
229
230         $cnt = $nl->show();
231         if (0 == $cnt) {
232             $this->showEmptyListMessage();
233         }
234
235         $this->pagination($this->page > 1, $cnt > NOTICES_PER_PAGE,
236                           $this->page, 'showfavorites',
237                           array('nickname' => $this->user->nickname));
238     }
239
240     function showPageNotice() {
241         // TRANS: Page notice for show favourites page.
242         $this->element('p', 'instructions', _('This is a way to share what you like.'));
243     }
244 }
245
246 class FavoritesNoticeList extends NoticeList
247 {
248     function newListItem(Notice $notice)
249     {
250         return new FavoritesNoticeListItem($notice, $this->out);
251     }
252 }
253
254 // All handled by superclass
255 class FavoritesNoticeListItem extends DoFollowListItem
256 {
257 }