]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/attachmentlist.php
b5513ade7dc736071a22b91a21770ff8adb26b88
[quix0rs-gnu-social.git] / lib / attachmentlist.php
1 <?php
2 /**
3  * Laconica, the distributed open-source microblogging tool
4  *
5  * widget for displaying a list of notice attachments
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  * @author    Sarven Capadisli <csarven@controlyourself.ca>
26  * @copyright 2008 Control Yourself, Inc.
27  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
28  * @link      http://laconi.ca/
29  */
30
31 if (!defined('LACONICA')) {
32     exit(1);
33 }
34
35 /**
36  * widget for displaying a list of notice attachments
37  *
38  * There are a number of actions that display a list of notices, in
39  * reverse chronological order. This widget abstracts out most of the
40  * code for UI for notice lists. It's overridden to hide some
41  * data for e.g. the profile page.
42  *
43  * @category UI
44  * @package  Laconica
45  * @author   Evan Prodromou <evan@controlyourself.ca>
46  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
47  * @link     http://laconi.ca/
48  * @see      Notice
49  * @see      NoticeListItem
50  * @see      ProfileNoticeList
51  */
52
53 class AttachmentList 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         $atts = new File;
83         $att = $atts->getAttachments($this->notice->id);
84         if (empty($att)) return 0;
85         $this->out->elementStart('dl', array('id' =>'attachments'));
86         $this->out->element('dt', null, _('Attachments'));
87         $this->out->elementStart('dd');
88         $this->out->elementStart('ol', array('class' => 'attachments'));
89
90         foreach ($att as $n=>$attachment) {
91             $item = $this->newListItem($attachment);
92             $item->show();
93         }
94
95         $this->out->elementEnd('dd');
96         $this->out->elementEnd('ol');
97         $this->out->elementEnd('dl');
98
99         return count($att);
100     }
101
102     /**
103      * returns a new list item for the current notice
104      *
105      * Recipe (factory?) method; overridden by sub-classes to give
106      * a different list item class.
107      *
108      * @param Notice $notice the current notice
109      *
110      * @return NoticeListItem a list item for displaying the notice
111      */
112
113     function newListItem($attachment)
114     {
115         return new AttachmentListItem($attachment, $this->out);
116     }
117 }
118
119 /**
120  * widget for displaying a single notice
121  *
122  * This widget has the core smarts for showing a single notice: what to display,
123  * where, and under which circumstances. Its key method is show(); this is a recipe
124  * that calls all the other show*() methods to build up a single notice. The
125  * ProfileNoticeListItem subclass, for example, overrides showAuthor() to skip
126  * author info (since that's implicit by the data in the page).
127  *
128  * @category UI
129  * @package  Laconica
130  * @author   Evan Prodromou <evan@controlyourself.ca>
131  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
132  * @link     http://laconi.ca/
133  * @see      NoticeList
134  * @see      ProfileNoticeListItem
135  */
136
137 class AttachmentListItem extends Widget
138 {
139     /** The attachment this item will show. */
140
141     var $attachment = null;
142
143     var $oembed = null;
144
145     /**
146      * constructor
147      *
148      * Also initializes the profile attribute.
149      *
150      * @param Notice $notice The notice we'll display
151      */
152
153     function __construct($attachment, $out=null)
154     {
155         parent::__construct($out);
156         $this->attachment  = $attachment;
157         $this->oembed = File_oembed::staticGet('file_id', $this->attachment->id);
158     }
159
160     function title() {
161         if (empty($this->attachment->title)) {
162             if (empty($this->oembed->title)) {
163                 $title = $this->attachment->url;
164             } else {
165                 $title = $this->oembed->title;
166             }
167         } else {
168             $title = $this->attachment->title;
169         }
170
171         return $title;
172     }
173
174     function linkTitle() {
175         return $this->title();
176     }
177
178     /**
179      * recipe function for displaying a single notice.
180      *
181      * This uses all the other methods to correctly display a notice. Override
182      * it or one of the others to fine-tune the output.
183      *
184      * @return void
185      */
186
187     function show()
188     {
189         $this->showStart();
190         $this->showNoticeAttachment();
191         $this->showEnd();
192     }
193
194     function linkAttr() {
195         return array('class' => 'attachment', 'href' => $this->attachment->url, 'id' => 'attachment-' . $this->attachment->id);
196     }
197
198     function showLink() {
199         $this->out->elementStart('a', $this->linkAttr());
200         $this->out->element('span', null, $this->linkTitle());
201         $this->showRepresentation();
202         $this->out->elementEnd('a');
203     }
204
205     function showNoticeAttachment()
206     {
207         $this->showLink();
208     }
209
210     function showRepresentation() {
211         $thumbnail = File_thumbnail::staticGet('file_id', $this->attachment->id);
212         if (!empty($thumbnail)) {
213             $this->out->element('img', array('alt' => '', 'src' => $thumbnail->url, 'width' => $thumbnail->width, 'height' => $thumbnail->height));
214         }
215     }
216
217     /**
218      * start a single notice.
219      *
220      * @return void
221      */
222
223     function showStart()
224     {
225         // XXX: RDFa
226         // TODO: add notice_type class e.g., notice_video, notice_image
227         $this->out->elementStart('li');
228     }
229
230     /**
231      * finish the notice
232      *
233      * Close the last elements in the notice list item
234      *
235      * @return void
236      */
237
238     function showEnd()
239     {
240         $this->out->elementEnd('li');
241     }
242 }
243
244 class Attachment extends AttachmentListItem
245 {
246     function showLink() {
247         $this->out->elementStart('div', array('id' => 'attachment_view',
248                                               'class' => 'hentry'));
249         $this->out->elementStart('div', 'entry-title');
250         $this->out->elementStart('a', $this->linkAttr());
251         $this->out->element('span', null, $this->linkTitle());
252         $this->out->elementEnd('a');
253         $this->out->elementEnd('div');
254
255         $this->out->elementStart('div', 'entry-content');
256         $this->showRepresentation();
257         $this->out->elementEnd('div');
258
259         if (!empty($this->oembed->author_name) || !empty($this->oembed->provider)) {
260             $this->out->elementStart('div', array('id' => 'oembed_info', 
261                                                   'class' => 'entry-content'));
262             if (!empty($this->oembed->author_name)) {
263                 $this->out->elementStart('dl', 'vcard author');
264                 $this->out->element('dt', null, _('Author'));
265                 $this->out->elementStart('dd', 'fn');
266                 if (empty($this->oembed->author_url)) {
267                     $this->out->text($this->oembed->author_name);
268                 } else {
269                     $this->out->element('a', array('href' => $this->oembed->author_url,
270                                                    'class' => 'url'), $this->oembed->author_name);
271                 }
272                 $this->out->elementEnd('dd');
273                 $this->out->elementEnd('dl');
274             }
275             if (!empty($this->oembed->provider)) {
276                 $this->out->elementStart('dl', 'vcard');
277                 $this->out->element('dt', null, _('Provider'));
278                 $this->out->elementStart('dd', 'fn');
279                 if (empty($this->oembed->provider_url)) {
280                     $this->out->text($this->oembed->provider);
281                 } else {
282                     $this->out->element('a', array('href' => $this->oembed->provider_url,
283                                                    'class' => 'url'), $this->oembed->provider);
284                 }
285                 $this->out->elementEnd('dd');
286                 $this->out->elementEnd('dl');
287             }
288             $this->out->elementEnd('div');
289         }
290         $this->out->elementEnd('div');
291     }
292
293     function show() {
294         $this->showNoticeAttachment();
295     }
296
297     function linkAttr() {
298         return array('class' => 'external', 'href' => $this->attachment->url);
299     }
300
301     function linkTitle() {
302         return $this->attachment->url;
303     }
304
305     function showRepresentation() {
306         if (empty($this->oembed->type)) {
307             if (empty($this->attachment->mimetype)) {
308                 $this->out->element('pre', null, 'oh well... not sure how to handle the following: ' . print_r($this->attachment, true));
309             } else {
310                 switch ($this->attachment->mimetype) {
311                 case 'image/gif':
312                 case 'image/png':
313                 case 'image/jpg':
314                 case 'image/jpeg':
315                     $this->out->element('img', array('src' => $this->attachment->url, 'alt' => 'alt'));
316                     break;
317
318                 case 'application/ogg':
319                 case 'audio/x-speex':
320                 case 'video/mpeg':
321                 case 'audio/mpeg':
322                 case 'video/mp4':
323                 case 'video/quicktime':
324                     $arr  = array('type' => $this->attachment->mimetype,
325                         'data' => $this->attachment->url,
326                         'width' => 320,
327                         'height' => 240
328                     );
329                     $this->out->elementStart('object', $arr);
330                     $this->out->element('param', array('name' => 'src', 'value' => $this->attachment->url));
331                     $this->out->element('param', array('name' => 'autoStart', 'value' => 1));
332                     $this->out->elementEnd('object');
333                     break;
334                 }
335             }
336         } else {
337             switch ($this->oembed->type) {
338             case 'rich':
339             case 'video':
340             case 'link':
341                 if (!empty($this->oembed->html)) {
342                     $this->out->raw($this->oembed->html);
343                 }
344                 break;
345
346             case 'photo':
347                 $this->out->element('img', array('src' => $this->oembed->url, 'width' => $this->oembed->width, 'height' => $this->oembed->height, 'alt' => 'alt'));
348                 break;
349
350             default:
351                 $this->out->element('pre', null, 'oh well... not sure how to handle the following oembed: ' . print_r($this->oembed, true));
352             }
353         }
354     }
355 }
356