]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/noticelist.php
Add an optional theme parameter to theme functions
[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->showFaveForm();
173         $this->showAuthor();
174         $this->showContent();
175         $this->startTimeSection();
176         $this->showNoticeLink();
177         $this->showNoticeSource();
178         $this->showReplyTo();
179         $this->showReplyLink();
180         $this->showDeleteLink();
181         $this->endTimeSection();
182         $this->showEnd();
183     }
184
185     /**
186      * start a single notice.
187      *
188      * @return void
189      */
190
191     function showStart()
192     {
193         // XXX: RDFa
194         common_element_start('li', array('class' => 'notice_single hentry',
195                                          'id' => 'notice-' . $this->notice->id));
196     }
197
198     /**
199      * show the "favorite" form
200      *
201      * @return void
202      */
203
204     function showFaveForm()
205     {
206         $user = common_current_user();
207         if ($user) {
208             if ($user->hasFave($this->notice)) {
209                 common_disfavor_form($this->notice);
210             } else {
211                 common_favor_form($this->notice);
212             }
213         }
214     }
215
216     /**
217      * show the author of a notice
218      *
219      * By default, this shows the avatar and (linked) nickname of the author.
220      *
221      * @return void
222      */
223
224     function showAuthor()
225     {
226         common_element_start('span', 'vcard author');
227         $this->showAvatar();
228         $this->showNickname();
229         common_element_end('span');
230     }
231
232     /**
233      * show the avatar of the notice's author
234      *
235      * This will use the default avatar if no avatar is assigned for the author.
236      * It makes a link to the author's profile.
237      *
238      * @return void
239      */
240
241     function showAvatar()
242     {
243         $avatar = $this->profile->getAvatar(AVATAR_STREAM_SIZE);
244         common_element_start('a', array('href' => $this->profile->profileurl));
245         common_element('img', array('src' => ($avatar) ?
246                                     common_avatar_display_url($avatar) :
247                                     common_default_avatar(AVATAR_STREAM_SIZE),
248                                     'class' => 'avatar stream photo',
249                                     'width' => AVATAR_STREAM_SIZE,
250                                     'height' => AVATAR_STREAM_SIZE,
251                                     'alt' =>
252                                     ($this->profile->fullname) ?
253                                     $this->profile->fullname :
254                                     $this->profile->nickname));
255         common_element_end('a');
256     }
257
258     /**
259      * show the nickname of the author
260      *
261      * Links to the author's profile page
262      *
263      * @return void
264      */
265
266     function showNickname()
267     {
268         common_element('a', array('href' => $this->profile->profileurl,
269                                   'class' => 'nickname fn url'),
270                        $this->profile->nickname);
271     }
272
273     /**
274      * show the content of the notice
275      *
276      * Shows the content of the notice. This is pre-rendered for efficiency
277      * at save time. Some very old notices might not be pre-rendered, so
278      * they're rendered on the spot.
279      *
280      * @return void
281      */
282
283     function showContent()
284     {
285         // FIXME: URL, image, video, audio
286         common_element_start('p', array('class' => 'content entry-title'));
287         if ($this->notice->rendered) {
288             common_raw($this->notice->rendered);
289         } else {
290             // XXX: may be some uncooked notices in the DB,
291             // we cook them right now. This should probably disappear in future
292             // versions (>> 0.4.x)
293             common_raw(common_render_content($this->notice->content, $this->notice));
294         }
295         common_element_end('p');
296     }
297
298     /**
299      * show the "time" section of a notice
300      *
301      * This is the greyed-out section that appears beneath the content, including
302      * links to delete or reply to the notice. Probably should be called something
303      * else.
304      *
305      * @return void
306      */
307
308     function startTimeSection()
309     {
310         common_element_start('p', 'time');
311     }
312
313     /**
314      * show the link to the main page for the notice
315      *
316      * Displays a link to the page for a notice, with "relative" time. Tries to
317      * get remote notice URLs correct, but doesn't always succeed.
318      *
319      * @return void
320      */
321
322     function showNoticeLink()
323     {
324         $noticeurl = common_local_url('shownotice',
325                                       array('notice' => $this->notice->id));
326         // XXX: we need to figure this out better. Is this right?
327         if (strcmp($this->notice->uri, $noticeurl) != 0 &&
328             preg_match('/^http/', $this->notice->uri)) {
329             $noticeurl = $this->notice->uri;
330         }
331         common_element_start('a', array('class' => 'permalink',
332                                         'rel' => 'bookmark',
333                                         'href' => $noticeurl));
334         $dt = common_date_iso8601($this->notice->created);
335         common_element('abbr', array('class' => 'published',
336                                      'title' => $dt),
337                        common_date_string($this->notice->created));
338         common_element_end('a');
339     }
340
341     /**
342      * Show the source of the notice
343      *
344      * Either the name (and link) of the API client that posted the notice,
345      * or one of other other channels.
346      *
347      * @return void
348      */
349
350     function showNoticeSource()
351     {
352         if ($this->notice->source) {
353             common_element('span', null, _(' from '));
354             $source_name = _($this->notice->source);
355             switch ($this->notice->source) {
356             case 'web':
357             case 'xmpp':
358             case 'mail':
359             case 'omb':
360             case 'api':
361                 common_element('span', 'noticesource', $source_name);
362                 break;
363             default:
364                 $ns = Notice_source::staticGet($this->notice->source);
365                 if ($ns) {
366                     common_element('a', array('href' => $ns->url),
367                                    $ns->name);
368                 } else {
369                     common_element('span', 'noticesource', $source_name);
370                 }
371                 break;
372             }
373         }
374     }
375
376     /**
377      * show link to notice this notice is a reply to
378      *
379      * If this notice is a reply, show a link to the notice it is replying to. The
380      * heavy lifting for figuring out replies happens at save time.
381      *
382      * @return void
383      */
384
385     function showReplyTo()
386     {
387         if ($this->notice->reply_to) {
388             $replyurl = common_local_url('shownotice',
389                                          array('notice' => $this->notice->reply_to));
390             common_text(' (');
391             common_element('a', array('class' => 'inreplyto',
392                                       'href' => $replyurl),
393                            _('in reply to...'));
394             common_text(')');
395         }
396     }
397
398     /**
399      * show a link to reply to the current notice
400      *
401      * Should either do the reply in the current notice form (if available), or
402      * link out to the notice-posting form. A little flakey, doesn't always work.
403      *
404      * @return void
405      */
406
407     function showReplyLink()
408     {
409         $reply_url = common_local_url('newnotice',
410                                       array('replyto' => $this->profile->nickname));
411
412         $reply_js =
413           'return doreply("'.$this->profile->nickname.'",'.$this->notice->id.');';
414
415         common_element_start('a',
416                              array('href' => $reply_url,
417                                    'onclick' => $reply_js,
418                                    'title' => _('reply'),
419                                    'class' => 'replybutton'));
420         common_raw(' &#8594;');
421         common_element_end('a');
422     }
423
424     /**
425      * if the user is the author, let them delete the notice
426      *
427      * @return void
428      */
429
430     function showDeleteLink()
431     {
432         $user = common_current_user();
433         if ($user && $this->notice->profile_id == $user->id) {
434             $deleteurl = common_local_url('deletenotice',
435                                           array('notice' => $this->notice->id));
436             common_element_start('a', array('class' => 'deletenotice',
437                                             'href' => $deleteurl,
438                                             'title' => _('delete')));
439             common_raw(' &#215;');
440             common_element_end('a');
441         }
442     }
443
444     /**
445      * end the time section
446      *
447      * @return void
448      */
449
450     function endTimeSection()
451     {
452         common_element_end('p');
453     }
454
455     /**
456      * finish the notice
457      *
458      * Close the last elements in the notice list item
459      *
460      * @return void
461      */
462
463     function showEnd()
464     {
465         common_element_end('li');
466     }
467 }