]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/noticelist.php
is_readonly() -> isReadOnly()
[quix0rs-gnu-social.git] / lib / noticelist.php
1 <?php
2 /**
3  * Laconica, 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   Laconica
24  * @author    Evan Prodromou <evan@controlyourself.ca>
25  * @copyright 2008 Control Yourself, Inc.
26  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27  * @link      http://laconi.ca/
28  */
29
30 if (!defined('LACONICA')) {
31     exit(1);
32 }
33
34 /**
35  * widget for displaying a list of notices
36  *
37  * There are a number of actions that display a list of notices, in
38  * reverse chronological order. This widget abstracts out most of the
39  * code for UI for notice lists. It's overridden to hide some
40  * data for e.g. the profile page.
41  *
42  * @category UI
43  * @package  Laconica
44  * @author   Evan Prodromou <evan@controlyourself.ca>
45  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
46  * @link     http://laconi.ca/
47  * @see      Notice
48  * @see      StreamAction
49  * @see      NoticeListItem
50  * @see      ProfileNoticeList
51  */
52
53 class NoticeList extends Widget
54 {
55     /** the current stream of notices being displayed. */
56
57     var $notice = null;
58
59     /**
60      * constructor
61      *
62      * @param Notice $notice stream of notices from DB_DataObject
63      */
64
65     function __construct($notice, $out=null)
66     {
67         parent::__construct($out);
68         $this->notice = $notice;
69     }
70
71     /**
72      * show the list of notices
73      *
74      * "Uses up" the stream by looping through it. So, probably can't
75      * be called twice on the same list.
76      *
77      * @return int count of notices listed.
78      */
79
80     function show()
81     {
82         $this->out->elementStart('div', array('id' =>'notices_primary'));
83         $this->out->element('h2', null, _('Notices'));
84         $this->out->elementStart('ul', array('class' => 'notices'));
85
86         $cnt = 0;
87
88         while ($this->notice->fetch() && $cnt <= NOTICES_PER_PAGE) {
89             $cnt++;
90
91             if ($cnt > NOTICES_PER_PAGE) {
92                 break;
93             }
94
95             $item = $this->newListItem($this->notice);
96             $item->show();
97         }
98
99         $this->out->elementEnd('ul');
100         $this->out->elementEnd('div');
101
102         return $cnt;
103     }
104
105     /**
106      * returns a new list item for the current notice
107      *
108      * Recipe (factory?) method; overridden by sub-classes to give
109      * a different list item class.
110      *
111      * @param Notice $notice the current notice
112      *
113      * @return NoticeListItem a list item for displaying the notice
114      */
115
116     function newListItem($notice)
117     {
118         return new NoticeListItem($notice, $this->out);
119     }
120 }
121
122 /**
123  * widget for displaying a single notice
124  *
125  * This widget has the core smarts for showing a single notice: what to display,
126  * where, and under which circumstances. Its key method is show(); this is a recipe
127  * that calls all the other show*() methods to build up a single notice. The
128  * ProfileNoticeListItem subclass, for example, overrides showAuthor() to skip
129  * author info (since that's implicit by the data in the page).
130  *
131  * @category UI
132  * @package  Laconica
133  * @author   Evan Prodromou <evan@controlyourself.ca>
134  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
135  * @link     http://laconi.ca/
136  * @see      NoticeList
137  * @see      ProfileNoticeListItem
138  */
139
140 class NoticeListItem extends Widget
141 {
142     /** The notice this item will show. */
143
144     var $notice = null;
145
146     /** The profile of the author of the notice, extracted once for convenience. */
147
148     var $profile = null;
149
150     /**
151      * constructor
152      *
153      * Also initializes the profile attribute.
154      *
155      * @param Notice $notice The notice we'll display
156      */
157
158     function __construct($notice, $out=null)
159     {
160         parent::__construct($out);
161         $this->notice  = $notice;
162         $this->profile = $notice->getProfile();
163     }
164
165     /**
166      * recipe function for displaying a single notice.
167      *
168      * This uses all the other methods to correctly display a notice. Override
169      * it or one of the others to fine-tune the output.
170      *
171      * @return void
172      */
173
174     function show()
175     {
176         $this->showStart();
177         $this->showNotice();
178         $this->showNoticeInfo();
179         $this->showNoticeOptions();
180         $this->showEnd();
181     }
182
183     function showNotice()
184     {
185         $this->out->elementStart('div', 'entry-title');
186         $this->showAuthor();
187         $this->showContent();
188         $this->out->elementEnd('div');
189     }
190
191     function showNoticeInfo()
192     {
193         $this->out->elementStart('div', 'entry-content');
194         $this->showNoticeLink();
195         $this->showNoticeSource();
196         $this->showReplyTo();
197         $this->out->elementEnd('div');
198     }
199
200     function showNoticeOptions()
201     {
202         $this->out->elementStart('div', 'notice-options');
203         $this->showFaveForm();
204         $this->showReplyLink();
205         $this->showDeleteLink();
206         $this->out->elementEnd('div');
207     }
208
209
210     /**
211      * start a single notice.
212      *
213      * @return void
214      */
215
216     function showStart()
217     {
218         // XXX: RDFa
219         // TODO: add notice_type class e.g., notice_video, notice_image
220         $this->out->elementStart('li', array('class' => 'hentry notice',
221                                          'id' => 'notice-' . $this->notice->id));
222     }
223
224     /**
225      * show the "favorite" form
226      *
227      * @return void
228      */
229
230     function showFaveForm()
231     {
232         $user = common_current_user();
233         if ($user) {
234             if ($user->hasFave($this->notice)) {
235                 $disfavor = new DisfavorForm($this->out, $this->notice);
236                 $disfavor->show();
237             } else {
238                 $favor = new FavorForm($this->out, $this->notice);
239                 $favor->show();
240             }
241         }
242     }
243
244     /**
245      * show the author of a notice
246      *
247      * By default, this shows the avatar and (linked) nickname of the author.
248      *
249      * @return void
250      */
251
252     function showAuthor()
253     {
254         $this->out->elementStart('span', 'vcard author');
255         $this->out->elementStart('a', array('href' => $this->profile->profileurl,
256                                         'class' => 'url'));
257         $this->showAvatar();
258         $this->showNickname();
259         $this->out->elementEnd('a');
260         $this->out->elementEnd('span');
261     }
262
263     /**
264      * show the avatar of the notice's author
265      *
266      * This will use the default avatar if no avatar is assigned for the author.
267      * It makes a link to the author's profile.
268      *
269      * @return void
270      */
271
272     function showAvatar()
273     {
274         $avatar = $this->profile->getAvatar(AVATAR_STREAM_SIZE);
275
276         $this->out->element('img', array('src' => ($avatar) ?
277                                     common_avatar_display_url($avatar) :
278                                     common_default_avatar(AVATAR_STREAM_SIZE),
279                                     'class' => 'avatar photo',
280                                     'width' => AVATAR_STREAM_SIZE,
281                                     'height' => AVATAR_STREAM_SIZE,
282                                     'alt' =>
283                                     ($this->profile->fullname) ?
284                                     $this->profile->fullname :
285                                     $this->profile->nickname));
286     }
287
288     /**
289      * show the nickname of the author
290      *
291      * Links to the author's profile page
292      *
293      * @return void
294      */
295
296     function showNickname()
297     {
298         $this->out->element('span', array('class' => 'nickname fn'),
299                        $this->profile->nickname);
300     }
301
302     /**
303      * show the content of the notice
304      *
305      * Shows the content of the notice. This is pre-rendered for efficiency
306      * at save time. Some very old notices might not be pre-rendered, so
307      * they're rendered on the spot.
308      *
309      * @return void
310      */
311
312     function showContent()
313     {
314         // FIXME: URL, image, video, audio
315         $this->out->elementStart('p', array('class' => 'entry-content'));
316         if ($this->notice->rendered) {
317             $this->out->raw($this->notice->rendered);
318         } else {
319             // XXX: may be some uncooked notices in the DB,
320             // we cook them right now. This should probably disappear in future
321             // versions (>> 0.4.x)
322             $this->out->raw(common_render_content($this->notice->content, $this->notice));
323         }
324         $this->out->elementEnd('p');
325     }
326
327     /**
328      * show the link to the main page for the notice
329      *
330      * Displays a link to the page for a notice, with "relative" time. Tries to
331      * get remote notice URLs correct, but doesn't always succeed.
332      *
333      * @return void
334      */
335
336     function showNoticeLink()
337     {
338         $noticeurl = common_local_url('shownotice',
339                                       array('notice' => $this->notice->id));
340         // XXX: we need to figure this out better. Is this right?
341         if (strcmp($this->notice->uri, $noticeurl) != 0 &&
342             preg_match('/^http/', $this->notice->uri)) {
343             $noticeurl = $this->notice->uri;
344         }
345         $this->out->elementStart('dl', 'timestamp');
346         $this->out->element('dt', _('Published')); 
347         $this->out->elementStart('dd', null);
348         $this->out->elementStart('a', array('rel' => 'bookmark',
349                                         'href' => $noticeurl));
350         $dt = common_date_iso8601($this->notice->created);
351         $this->out->element('abbr', array('class' => 'published',
352                                      'title' => $dt),
353                        common_date_string($this->notice->created));
354         $this->out->elementEnd('a');
355         $this->out->elementEnd('dd');
356         $this->out->elementEnd('dl');
357     }
358
359     /**
360      * Show the source of the notice
361      *
362      * Either the name (and link) of the API client that posted the notice,
363      * or one of other other channels.
364      *
365      * @return void
366      */
367
368     function showNoticeSource()
369     {
370         if ($this->notice->source) {
371             $this->out->elementStart('dl', 'device');
372             $this->out->element('dt', null, _('From'));
373             $source_name = _($this->notice->source);
374             switch ($this->notice->source) {
375             case 'web':
376             case 'xmpp':
377             case 'mail':
378             case 'omb':
379             case 'api':
380                 $this->out->element('dd', 'noticesource', $source_name);
381                 break;
382             default:
383                 $ns = Notice_source::staticGet($this->notice->source);
384                 if ($ns) {
385                     $this->out->elementStart('dd', null);
386                     $this->out->element('a', array('href' => $ns->url,
387                                               'rel' => 'external'),
388                                    $ns->name);
389                     $this->out->elementEnd('dd');
390                 } else {
391                     $this->out->element('dd', 'noticesource', $source_name);
392                 }
393                 break;
394             }
395             $this->out->elementEnd('dl');
396         }
397     }
398
399     /**
400      * show link to notice this notice is a reply to
401      *
402      * If this notice is a reply, show a link to the notice it is replying to. The
403      * heavy lifting for figuring out replies happens at save time.
404      *
405      * @return void
406      */
407
408     function showReplyTo()
409     {
410         if ($this->notice->reply_to) {
411             $replyurl = common_local_url('shownotice',
412                                          array('notice' => $this->notice->reply_to));
413             $this->out->elementStart('dl', 'response');
414             $this->out->element('dt', null, _('To'));
415             $this->out->elementStart('dd');
416             $this->out->element('a', array('href' => $replyurl,
417                                       'rel' => 'in-reply-to'),
418                            _('in reply to'));
419             $this->out->elementEnd('dd');
420             $this->out->elementEnd('dl');
421         }
422     }
423
424     /**
425      * show a link to reply to the current notice
426      *
427      * Should either do the reply in the current notice form (if available), or
428      * link out to the notice-posting form. A little flakey, doesn't always work.
429      *
430      * @return void
431      */
432
433     function showReplyLink()
434     {
435         $reply_url = common_local_url('newnotice',
436                                       array('replyto' => $this->profile->nickname));
437
438         $reply_js =
439           'return doreply("'.$this->profile->nickname.'",'.$this->notice->id.');';
440
441         $this->out->elementStart('a',
442                              array('href' => $reply_url,
443                                    'onclick' => $reply_js,
444                                    'title' => _('reply'),
445                                    'class' => 'replybutton'));
446         $this->out->raw(' &#8594;');
447         $this->out->elementEnd('a');
448     }
449
450     /**
451      * if the user is the author, let them delete the notice
452      *
453      * @return void
454      */
455
456     function showDeleteLink()
457     {
458         $user = common_current_user();
459         if ($user && $this->notice->profile_id == $user->id) {
460             $deleteurl = common_local_url('deletenotice',
461                                           array('notice' => $this->notice->id));
462             $this->out->elementStart('a', array('class' => 'deletenotice',
463                                             'href' => $deleteurl,
464                                             'title' => _('delete')));
465             $this->out->raw(' &#215;');
466             $this->out->elementEnd('a');
467         }
468     }
469
470     /**
471      * finish the notice
472      *
473      * Close the last elements in the notice list item
474      *
475      * @return void
476      */
477
478     function showEnd()
479     {
480         $this->out->elementEnd('li');
481     }
482 }