]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/attachmentlist.php
Attachments and their list now provide "ajax" view. Also added a few sidebars relatin...
[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 //        $this->out->elementStart('div', array('id' =>'attachments_primary'));
84         $this->out->elementStart('div', array('id' =>'content'));
85         $this->out->element('h2', null, _('Attachments'));
86         $this->out->elementStart('ul', array('class' => 'attachments'));
87
88         $atts = new File;
89         $att = $atts->getAttachments($this->notice->id);
90         foreach ($att as $n=>$attachment) {
91             $item = $this->newListItem($attachment);
92             $item->show();
93         }
94
95         $this->out->elementEnd('ul');
96         $this->out->elementEnd('div');
97
98         return count($att);
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($attachment)
113     {
114         return new AttachmentListItem($attachment, $this->out);
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 AttachmentListItem extends Widget
137 {
138     /** The attachment this item will show. */
139
140     var $attachment = null;
141
142     var $oembed = null;
143
144     /**
145      * constructor
146      *
147      * Also initializes the profile attribute.
148      *
149      * @param Notice $notice The notice we'll display
150      */
151
152     function __construct($attachment, $out=null)
153     {
154         parent::__construct($out);
155         $this->attachment  = $attachment;
156         $this->oembed = File_oembed::staticGet('file_id', $this->attachment->id);
157     }
158
159     function title() {
160         if (empty($this->attachment->title)) {
161             if (empty($this->oembed->title)) {
162                 $title = $this->attachment->url;
163             } else {
164                 $title = $this->oembed->title;
165             }
166         } else {
167             $title = $this->attachment->title;
168         }
169
170         return $title;
171     }
172
173     function linkTitle() {
174         return 'Our page for ' . $this->title();
175     }
176
177     /**
178      * recipe function for displaying a single notice.
179      *
180      * This uses all the other methods to correctly display a notice. Override
181      * it or one of the others to fine-tune the output.
182      *
183      * @return void
184      */
185
186     function show()
187     {
188         $this->showStart();
189         $this->showNoticeAttachment();
190         $this->showEnd();
191     }
192
193     function linkAttr() {
194         return array('class' => 'attachment', 'href' => common_local_url('attachment', array('attachment' => $this->attachment->id)));
195     }
196
197     function showLink() {
198         $attr = $this->linkAttr();
199         $text = $this->linkTitle();
200         $this->out->elementStart('h4');
201         $this->out->element('a', $attr, $text);
202
203         if ($this->attachment->url !== $this->title())
204             $this->out->element('span', null, " ({$this->attachment->url})");
205
206
207         $this->out->elementEnd('h4');
208     }
209
210     function showNoticeAttachment()
211     {
212         $this->showLink();
213         $this->showRepresentation();
214     }
215
216     function showRepresentation() {
217         $thumbnail = File_thumbnail::staticGet('file_id', $this->attachment->id);
218         if (!empty($thumbnail)) {
219             $this->out->elementStart('a', $this->linkAttr()/*'href' => $this->linkTo()*/);
220             $this->out->element('img', array('alt' => 'nothing to say', 'src' => $thumbnail->url, 'width' => $thumbnail->width, 'height' => $thumbnail->height));
221             $this->out->elementEnd('a');
222         }
223     }
224
225     /**
226      * start a single notice.
227      *
228      * @return void
229      */
230
231     function showStart()
232     {
233         // XXX: RDFa
234         // TODO: add notice_type class e.g., notice_video, notice_image
235         $this->out->elementStart('li');
236     }
237
238     /**
239      * finish the notice
240      *
241      * Close the last elements in the notice list item
242      *
243      * @return void
244      */
245
246     function showEnd()
247     {
248         $this->out->elementEnd('li');
249     }
250 }
251
252 class Attachment extends AttachmentListItem
253 {
254     function show() {
255         $this->showNoticeAttachment();
256     }
257
258     function linkAttr() {
259         return array('class' => 'external', 'href' => $this->attachment->url);
260     }
261
262     function linkTitle() {
263         return 'Direct link to ' . $this->title();
264     }
265
266     function showRepresentation() {
267         if (empty($this->oembed->type)) {
268             if (empty($this->attachment->mimetype)) {
269                 $this->out->element('pre', null, 'oh well... not sure how to handle the following: ' . print_r($this->attachment, true));
270             } else {
271                 switch ($this->attachment->mimetype) {
272                 case 'image/gif':
273                 case 'image/png':
274                 case 'image/jpg':
275                 case 'image/jpeg':
276                     $this->out->element('img', array('src' => $this->attachment->url, 'alt' => 'alt'));
277                     break;
278                 }
279             }
280         } else {
281             switch ($this->oembed->type) {
282             case 'rich':
283             case 'video':
284             case 'link':
285                 if (!empty($this->oembed->html)) {
286                     $this->out->raw($this->oembed->html);
287                 }
288                 break;
289
290             case 'photo':
291                 $this->out->element('img', array('src' => $this->oembed->url, 'width' => $this->oembed->width, 'height' => $this->oembed->height, 'alt' => 'alt'));
292                 break;
293
294             default:
295                 $this->out->element('pre', null, 'oh well... not sure how to handle the following oembed: ' . print_r($this->oembed, true));
296             }
297         }
298     }
299 }
300