]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/attachmentlist.php
Added configurable options for attachments: supported mimetypes and quotas for uploads.
[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      StreamAction
50  * @see      NoticeListItem
51  * @see      ProfileNoticeList
52  */
53
54 class AttachmentList extends Widget
55 {
56     /** the current stream of notices being displayed. */
57
58     var $notice = null;
59
60     /**
61      * constructor
62      *
63      * @param Notice $notice stream of notices from DB_DataObject
64      */
65
66     function __construct($notice, $out=null)
67     {
68         parent::__construct($out);
69         $this->notice = $notice;
70     }
71
72     /**
73      * show the list of notices
74      *
75      * "Uses up" the stream by looping through it. So, probably can't
76      * be called twice on the same list.
77      *
78      * @return int count of notices listed.
79      */
80
81     function show()
82     {
83         $atts = new File;
84         $att = $atts->getAttachments($this->notice->id);
85         if (empty($att)) return 0;
86
87         $this->out->elementStart('dl', array('id' =>'attachment'));
88         $this->out->element('dt', null, _('Attachments'));
89         $this->out->elementStart('dd');
90         $this->out->elementStart('ul', array('class' => 'attachments'));
91
92         foreach ($att as $n=>$attachment) {
93             $item = $this->newListItem($attachment);
94             $item->show();
95         }
96
97         $this->out->elementEnd('dd');
98         $this->out->elementEnd('ul');
99         $this->out->elementEnd('dl');
100
101         return count($att);
102     }
103
104     /**
105      * returns a new list item for the current notice
106      *
107      * Recipe (factory?) method; overridden by sub-classes to give
108      * a different list item class.
109      *
110      * @param Notice $notice the current notice
111      *
112      * @return NoticeListItem a list item for displaying the notice
113      */
114
115     function newListItem($attachment)
116     {
117         return new AttachmentListItem($attachment, $this->out);
118     }
119 }
120
121 /**
122  * widget for displaying a single notice
123  *
124  * This widget has the core smarts for showing a single notice: what to display,
125  * where, and under which circumstances. Its key method is show(); this is a recipe
126  * that calls all the other show*() methods to build up a single notice. The
127  * ProfileNoticeListItem subclass, for example, overrides showAuthor() to skip
128  * author info (since that's implicit by the data in the page).
129  *
130  * @category UI
131  * @package  Laconica
132  * @author   Evan Prodromou <evan@controlyourself.ca>
133  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
134  * @link     http://laconi.ca/
135  * @see      NoticeList
136  * @see      ProfileNoticeListItem
137  */
138
139 class AttachmentListItem extends Widget
140 {
141     /** The attachment this item will show. */
142
143     var $attachment = null;
144
145     var $oembed = null;
146
147     /**
148      * constructor
149      *
150      * Also initializes the profile attribute.
151      *
152      * @param Notice $notice The notice we'll display
153      */
154
155     function __construct($attachment, $out=null)
156     {
157         parent::__construct($out);
158         $this->attachment  = $attachment;
159         $this->oembed = File_oembed::staticGet('file_id', $this->attachment->id);
160     }
161
162     function title() {
163         if (empty($this->attachment->title)) {
164             if (empty($this->oembed->title)) {
165                 $title = $this->attachment->url;
166             } else {
167                 $title = $this->oembed->title;
168             }
169         } else {
170             $title = $this->attachment->title;
171         }
172
173         return $title;
174     }
175
176     function linkTitle() {
177         return $this->title();
178     }
179
180     /**
181      * recipe function for displaying a single notice.
182      *
183      * This uses all the other methods to correctly display a notice. Override
184      * it or one of the others to fine-tune the output.
185      *
186      * @return void
187      */
188
189     function show()
190     {
191         $this->showStart();
192         $this->showNoticeAttachment();
193         $this->showEnd();
194     }
195
196     function linkAttr() {
197         return array('class' => 'attachment', 'href' => $this->attachment->url, 'id' => 'attachment-' . $this->attachment->id);
198     }
199
200     function showLink() {
201         $this->out->elementStart('a', $this->linkAttr());
202         $this->out->element('span', null, $this->linkTitle());
203         $this->showRepresentation();
204         $this->out->elementEnd('a');
205     }
206
207     function showNoticeAttachment()
208     {
209         $this->showLink();
210     }
211
212     function showRepresentation() {
213         $thumbnail = File_thumbnail::staticGet('file_id', $this->attachment->id);
214         if (!empty($thumbnail)) {
215             $this->out->element('img', array('alt' => 'nothing to say', 'src' => $thumbnail->url, 'width' => $thumbnail->width, 'height' => $thumbnail->height));
216         }
217     }
218
219     /**
220      * start a single notice.
221      *
222      * @return void
223      */
224
225     function showStart()
226     {
227         // XXX: RDFa
228         // TODO: add notice_type class e.g., notice_video, notice_image
229         $this->out->elementStart('li');
230     }
231
232     /**
233      * finish the notice
234      *
235      * Close the last elements in the notice list item
236      *
237      * @return void
238      */
239
240     function showEnd()
241     {
242         $this->out->elementEnd('li');
243     }
244 }
245
246 class Attachment extends AttachmentListItem
247 {
248     function show() {
249         $this->showNoticeAttachment();
250     }
251
252     function linkAttr() {
253         return array('class' => 'external', 'href' => $this->attachment->url);
254     }
255
256     function linkTitle() {
257         return $this->attachment->url;
258     }
259
260     function showRepresentation() {
261         if (empty($this->oembed->type)) {
262             if (empty($this->attachment->mimetype)) {
263                 $this->out->element('pre', null, 'oh well... not sure how to handle the following: ' . print_r($this->attachment, true));
264             } else {
265                 switch ($this->attachment->mimetype) {
266                 case 'image/gif':
267                 case 'image/png':
268                 case 'image/jpg':
269                 case 'image/jpeg':
270                     $this->out->element('img', array('src' => $this->attachment->url, 'alt' => 'alt'));
271                     break;
272                 }
273             }
274         } else {
275             switch ($this->oembed->type) {
276             case 'rich':
277             case 'video':
278             case 'link':
279                 if (!empty($this->oembed->html)) {
280                     $this->out->raw($this->oembed->html);
281                 }
282                 break;
283
284             case 'photo':
285                 $this->out->element('img', array('src' => $this->oembed->url, 'width' => $this->oembed->width, 'height' => $this->oembed->height, 'alt' => 'alt'));
286                 break;
287
288             default:
289                 $this->out->element('pre', null, 'oh well... not sure how to handle the following oembed: ' . print_r($this->oembed, true));
290             }
291         }
292     }
293 }
294