]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/gallery.php
Extract image management code to a helper function
[quix0rs-gnu-social.git] / lib / gallery.php
1 <?php
2 /**
3  * Laconica - a distributed open-source microblogging tool
4  * Copyright (C) 2008, Controlez-Vous, Inc.
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Affero General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU Affero General Public License for more details.
15  *
16  * You should have received a copy of the GNU Affero General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 if (!defined('LACONICA')) {
21     exit(1);
22 }
23
24 require_once INSTALLDIR.'/lib/profilelist.php';
25
26 // 10x8
27
28 define('AVATARS_PER_PAGE', 80);
29
30 class GalleryAction extends Action
31 {
32     function is_readonly()
33     {
34         return true;
35     }
36
37     function handle($args)
38     {
39         parent::handle($args);
40
41         // Post from the tag dropdown; redirect to a GET
42
43         if ($_SERVER['REQUEST_METHOD'] == 'POST') {
44             common_redirect($this->self_url(), 307);
45         }
46
47         $nickname = common_canonical_nickname($this->arg('nickname'));
48
49         $user = User::staticGet('nickname', $nickname);
50
51         if (!$user) {
52             $this->no_such_user();
53             return;
54         }
55
56         $profile = $user->getProfile();
57
58         if (!$profile) {
59             $this->server_error(_('User without matching profile in system.'));
60             return;
61         }
62
63         $page = $this->arg('page');
64
65         if (!$page) {
66             $page = 1;
67         }
68
69         $display = $this->arg('display');
70
71         if (!$display) {
72             $display = 'list';
73         }
74
75         $tag = $this->arg('tag');
76
77         common_show_header($profile->nickname . ": " . $this->gallery_type(),
78                            null, $profile,
79                            array($this, 'show_top'));
80
81         $this->display_links($profile, $page, $display);
82         $this->show_tags_dropdown($profile);
83
84         $this->show_gallery($profile, $page, $display, $tag);
85         common_show_footer();
86     }
87
88     function no_such_user()
89     {
90         $this->client_error(_('No such user.'));
91     }
92
93     function show_tags_dropdown($profile)
94     {
95         $tag = $this->trimmed('tag');
96
97         list($lst, $usr) = $this->fields();
98
99         $tags = $this->get_all_tags($profile, $lst, $usr);
100
101         $content = array();
102
103         foreach ($tags as $t) {
104             $content[$t] = $t;
105         }
106         if ($tags) {
107             common_element_start('dl', array('id'=>'filter_tags'));
108             common_element('dt', null, _('Filter tags'));
109             common_element_start('dd');
110             common_element_start('ul');
111             common_element_start('li', array('id' => 'filter_tags_all',
112                                              'class' => 'child_1'));
113             common_element('a',
114                            array('href' =>
115                                  common_local_url($this->trimmed('action'),
116                                                   array('nickname' =>
117                                                         $profile->nickname))),
118                            _('All'));
119             common_element_end('li');
120             common_element_start('li', array('id'=>'filter_tags_item'));
121             common_element_start('form', array('name' => 'bytag',
122                                                'id' => 'bytag',
123                                                'method' => 'post'));
124             common_dropdown('tag', _('Tag'), $content,
125                             _('Choose a tag to narrow list'), false, $tag);
126             common_submit('go', _('Go'));
127             common_element_end('form');
128             common_element_end('li');
129             common_element_end('ul');
130             common_element_end('dd');
131             common_element_end('dl');
132         }
133     }
134
135     function show_top($profile)
136     {
137         common_element('div', 'instructions',
138                        $this->get_instructions($profile));
139         $this->show_menu();
140     }
141
142     function show_menu()
143     {
144         // action => array('prompt', 'title', $args)
145         $action   = $this->trimmed('action');
146         $nickname = $this->trimmed('nickname');
147
148         $menu =
149           array('subscriptions' =>
150                 array( _('Subscriptions'),
151                        _('Subscriptions'),
152                        array('nickname' => $nickname)),
153                 'subscribers' =>
154                 array(
155                       _('Subscribers'),
156                       _('Subscribers'),
157                       array('nickname' => $nickname)),
158                 );
159         $this->nav_menu($menu);
160     }
161
162     function show_gallery($profile, $page, $display='list', $tag=null)
163     {
164         $other = new Profile();
165
166         list($lst, $usr) = $this->fields();
167
168         $per_page = ($display == 'list') ? PROFILES_PER_PAGE : AVATARS_PER_PAGE;
169
170         $offset = ($page-1)*$per_page;
171         $limit = $per_page + 1;
172
173         if (common_config('db', 'type') == 'pgsql') {
174             $lim = ' LIMIT ' . $limit . ' OFFSET ' . $offset;
175         } else {
176             $lim = ' LIMIT ' . $offset . ', ' . $limit;
177         }
178
179         // XXX: memcached results
180         // FIXME: SQL injection on $tag
181
182         $other->query('SELECT profile.* ' .
183                       'FROM profile JOIN subscription ' .
184                       'ON profile.id = subscription.' . $lst . ' ' .
185                       (($tag) ? 'JOIN profile_tag ON (profile.id = profile_tag.tagged AND subscription.'.$usr.'= profile_tag.tagger) ' : '') .
186                       'WHERE ' . $usr . ' = ' . $profile->id . ' ' .
187                       'AND subscriber != subscribed ' .
188                       (($tag) ? 'AND profile_tag.tag= "' . $tag . '" ': '') .
189                       'ORDER BY subscription.created DESC, profile.id DESC ' .
190                       $lim);
191
192         if ($display == 'list') {
193             $cls = $this->profile_list_class();
194             $profile_list = new $cls($other, $profile, $this->trimmed('action'));
195             $cnt = $profile_list->show_list();
196         } else {
197             $cnt = $this->icon_list($other);
198         }
199
200         // For building the pagination URLs
201
202         $args = array('nickname' => $profile->nickname);
203
204         if ($display != 'list') {
205             $args['display'] = $display;
206         }
207
208         common_pagination($page > 1,
209                           $cnt > $per_page,
210                           $page,
211                           $this->trimmed('action'),
212                           $args);
213     }
214
215     function profile_list_class()
216     {
217         return 'ProfileList';
218     }
219
220     function icon_list($other)
221     {
222         common_element_start('ul', $this->div_class());
223
224         $cnt = 0;
225
226         while ($other->fetch()) {
227
228             $cnt++;
229
230             if ($cnt > AVATARS_PER_PAGE) {
231                 break;
232             }
233
234             common_element_start('li');
235
236             common_element_start('a', array('title' => ($other->fullname) ?
237                                             $other->fullname :
238                                             $other->nickname,
239                                             'href' => $other->profileurl,
240                                             'class' => 'subscription'));
241             $avatar = $other->getAvatar(AVATAR_STREAM_SIZE);
242             common_element('img',
243                            array('src' =>
244                                  (($avatar) ? common_avatar_display_url($avatar) :
245                                   common_default_avatar(AVATAR_STREAM_SIZE)),
246                                  'width' => AVATAR_STREAM_SIZE,
247                                  'height' => AVATAR_STREAM_SIZE,
248                                  'class' => 'avatar stream',
249                                  'alt' => ($other->fullname) ?
250                                  $other->fullname :
251                                  $other->nickname));
252             common_element_end('a');
253
254             // XXX: subscribe form here
255
256             common_element_end('li');
257         }
258
259         common_element_end('ul');
260
261         return $cnt;
262     }
263
264     function gallery_type()
265     {
266         return null;
267     }
268
269     function get_instructions(&$profile)
270     {
271         return null;
272     }
273
274     function fields()
275     {
276         return null;
277     }
278
279     function div_class()
280     {
281         return '';
282     }
283
284     function display_links($profile, $page, $display)
285     {
286         $tag = $this->trimmed('tag');
287
288         common_element_start('dl', array('id'=>'subscriptions_nav'));
289         common_element('dt', null, _('Subscriptions navigation'));
290         common_element_start('dd');
291         common_element_start('ul', array('class'=>'nav'));
292
293         switch ($display) {
294          case 'list':
295             common_element('li', array('class'=>'child_1'), _('List'));
296             common_element_start('li');
297             $url_args = array('display' => 'icons',
298                               'nickname' => $profile->nickname,
299                               'page' => 1 + floor((($page - 1) * PROFILES_PER_PAGE) / AVATARS_PER_PAGE));
300             if ($tag) {
301                 $url_args['tag'] = $tag;
302             }
303             $url = common_local_url($this->trimmed('action'), $url_args);
304             common_element('a', array('href' => $url),
305                            _('Icons'));
306             common_element_end('li');
307             break;
308          default:
309             common_element_start('li', array('class'=>'child_1'));
310             $url_args = array('nickname' => $profile->nickname,
311                               'page' => 1 + floor((($page - 1) * AVATARS_PER_PAGE) / PROFILES_PER_PAGE));
312             if ($tag) {
313                 $url_args['tag'] = $tag;
314             }
315             $url = common_local_url($this->trimmed('action'), $url_args);
316             common_element('a', array('href' => $url),
317                            _('List'));
318             common_element_end('li');
319             common_element('li', null, _('Icons'));
320             break;
321         }
322
323         common_element_end('ul');
324         common_element_end('dd');
325         common_element_end('dl');
326     }
327
328     // Get list of tags we tagged other users with
329
330     function get_all_tags($profile, $lst, $usr)
331     {
332         $profile_tag = new Notice_tag();
333         $profile_tag->query('SELECT DISTINCT(tag) ' .
334                             'FROM profile_tag, subscription ' .
335                             'WHERE tagger = ' . $profile->id . ' ' .
336                             'AND ' . $usr . ' = ' . $profile->id . ' ' .
337                             'AND ' . $lst . ' = tagged ' .
338                             'AND tagger != tagged');
339         $tags = array();
340         while ($profile_tag->fetch()) {
341             $tags[] = $profile_tag->tag;
342         }
343         $profile_tag->free();
344         return $tags;
345     }
346 }