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