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