]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/showstream.php
0db76806d9e783fe67c5730ecad7898bf4d61f48
[quix0rs-gnu-social.git] / actions / showstream.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')) { exit(1); }
21
22 require_once(INSTALLDIR.'/lib/stream.php');
23
24 define('SUBSCRIPTIONS_PER_ROW', 5);
25 define('SUBSCRIPTIONS', 80);
26
27 class ShowstreamAction extends StreamAction {
28
29         function handle($args) {
30
31                 parent::handle($args);
32
33                 $nickname = common_canonical_nickname($this->arg('nickname'));
34                 $user = User::staticGet('nickname', $nickname);
35
36                 if (!$user) {
37                         $this->no_such_user();
38                         return;
39                 }
40
41                 $profile = $user->getProfile();
42
43                 if (!$profile) {
44                         common_server_error(_t('User record exists without profile.'));
45                         return;
46                 }
47
48                 # Looks like we're good; show the header
49
50                 common_show_header($profile->nickname);
51
52                 $cur = common_current_user();
53
54                 if ($cur && $profile->id == $cur->id) {
55                         $this->notice_form();
56                 }
57
58                 $this->show_profile($profile);
59
60                 $this->show_last_notice($profile);
61
62                 if ($cur && $cur->id != $profile->id) {
63                         if ($cur->isSubscribed($profile)) {
64                                 $this->show_unsubscribe_form($profile);
65                         } else {
66                                 $this->show_subscribe_form($profile);
67                         }
68                 }
69
70                 $this->show_statistics($profile);
71
72                 $this->show_subscriptions($profile);
73
74                 $this->show_notices($profile);
75
76                 common_show_footer();
77         }
78
79         function no_such_user() {
80                 common_user_error('No such user');
81         }
82
83         function notice_form() {
84                 common_element_start('form', array('id' => 'newnotice', 'method' => 'POST',
85                                                                                    'action' => common_local_url('newnotice')));
86                 common_element('textarea', array('rows' => 3, 'cols' => 60,
87                                                                                  'name' => 'content',
88                                                                                  'id' => 'content'),
89                                            ' ');
90                 common_submit('submit', _t('Send'));
91                 common_element_end('form');
92         }
93
94         function show_profile($profile) {
95                 common_element_start('div', 'profile');
96
97                 common_element('h2', 'nickname', $profile->nickname);
98
99                 $avatar = $profile->getAvatar(AVATAR_PROFILE_SIZE);
100                 if ($avatar) {
101                         common_element('img', array('src' => $avatar->url,
102                                                                                 'class' => 'avatar profile',
103                                                                                 'width' => AVATAR_PROFILE_SIZE,
104                                                                                 'height' => AVATAR_PROFILE_SIZE,
105                                                                                 'alt' => $profile->nickname));
106                 }
107                 if ($profile->fullname) {
108                         if ($profile->homepage) {
109                                 common_element('a', array('href' => $profile->homepage,
110                                                                                   'class' => 'fullname'),
111                                                            $profile->fullname);
112                         } else {
113                                 common_element('span', 'fullname', $profile->fullname);
114                         }
115                 }
116                 if ($profile->location) {
117                         common_element('span', 'location', $profile->location);
118                 }
119                 if ($profile->bio) {
120                         common_element('div', 'bio', $profile->bio);
121                 }
122         }
123
124         function show_subscribe_form($profile) {
125                 common_element_start('form', array('id' => 'subscribe', 'method' => 'POST',
126                                                                                    'action' => common_local_url('subscribe')));
127                 common_element('input', array('id' => 'subscribeto',
128                                                                           'name' => 'subscribeto',
129                                                                           'type' => 'hidden',
130                                                                           'value' => $profile->nickname));
131                 common_element('input', array('type' => 'submit', 'value' => _t('Subscribe')));
132                 common_element_end('form');
133         }
134
135         function show_unsubscribe_form($profile) {
136                 common_element_start('form', array('id' => 'unsubscribe', 'method' => 'POST',
137                                                                                    'action' => common_local_url('unsubscribe')));
138                 common_element('input', array('id' => 'unsubscribeto',
139                                                                           'name' => 'unsubscribeto',
140                                                                           'type' => 'hidden',
141                                                                           'value' => $profile->nickname));
142                 common_element('input', array('type' => 'submit'), _t('Unsubscribe'));
143                 common_element_end('form');
144         }
145
146         function show_subscriptions($profile) {
147
148                 # XXX: add a limit
149                 $subs = $profile->getLink('id', 'subscription', 'subscriber');
150
151                 common_element_start('div', 'subscriptions');
152
153                 common_element('h2', 'subscriptions', _t('Subscriptions'));
154
155                 $cnt = 0;
156
157                 if ($subs) {
158                         while ($subs->fetch()) {
159                                 $cnt++;
160                                 if ($cnt % SUBSCRIPTIONS_PER_ROW == 1) {
161                                         common_element_start('div', 'row');
162                                 }
163
164                                 common_element_start('a', array('title' => ($subs->fullname) ?
165                                                                                                 $subs->fullname :
166                                                                                                 $subs->nickname,
167                                                                                                 'href' => $subs->profileurl,
168                                                                                                 'class' => 'subscription'));
169                                 $avatar = $subs->getAvatar(AVATAR_MINI_SIZE);
170                                 common_element('img', array('src' => (($avatar) ? $avatar->url : DEFAULT_MINI_AVATAR),
171                                                                                         'width' => AVATAR_MINI_SIZE,
172                                                                                         'height' => AVATAR_MINI_SIZE,
173                                                                                         'class' => 'avatar mini',
174                                                                                         'alt' =>  ($subs->fullname) ?
175                                                                                                 $subs->fullname :
176                                                                                                 $subs->nickname));
177                                 common_element_end('a');
178
179                                 if ($cnt % SUBSCRIPTIONS_PER_ROW == 0) {
180                                         common_element_end('div');
181                                 }
182
183                                 if ($cnt == SUBSCRIPTIONS) {
184                                         break;
185                                 }
186                         }
187                 }
188
189                 common_element('a', array('href' => common_local_url('subscriptions',
190                                                                                                                          array('nickname' => $profile->nickname)),
191                                                                   'class' => 'moresubscriptions'),
192                                            _t('All subscriptions'));
193
194                 common_element_end('div');
195         }
196
197         function show_statistics($profile) {
198
199                 // XXX: WORM cache this
200                 $subs = DB_DataObject::factory('subscription');
201                 $subs->subscriber = $profile->id;
202                 $subs_count = $subs->count();
203
204                 if (!$subs_count) {
205                         $subs_count = 0;
206                 }
207
208                 $subbed = DB_DataObject::factory('subscription');
209                 $subbed->subscribed = $profile->id;
210                 $subbed_count = $subbed->count();
211
212                 if (!$subbed_count) {
213                         $subbed_count = 0;
214                 }
215
216                 $notices = DB_DataObject::factory('notice');
217                 $notices->profile_id = $profile->id;
218                 $notice_count = $notices->count();
219
220                 if (!$notice_count) {
221                         $notice_count = 0;
222                 }
223
224                 common_element_start('div', 'statistics');
225                 common_element('h2', 'statistics', _t('Statistics'));
226
227                 # Other stats...?
228                 common_element_start('dl', 'statistics');
229                 common_element('dt', 'subscriptions', _t('Subscriptions'));
230                 common_element('dd', 'subscriptions', $subs_count);
231                 common_element('dt', 'subscribers', _t('Subscribers'));
232                 common_element('dd', 'subscribers', $subbed_count);
233                 common_element('dt', 'notices', _t('Notices'));
234                 common_element('dd', 'notices', $notice_count);
235                 common_element_end('dl');
236
237                 common_element_end('div');
238         }
239
240         function show_notices($profile) {
241
242                 $notice = DB_DataObject::factory('notice');
243                 $notice->profile_id = $profile->id;
244
245                 $notice->orderBy('created DESC');
246
247                 $page = $this->arg('page') || 1;
248
249                 $notice->limit((($page-1)*NOTICES_PER_PAGE), NOTICES_PER_PAGE);
250
251                 $notice->find();
252
253                 common_element_start('div', 'notices');
254                 common_element('h2', 'notices', _t('Notices'));
255
256                 while ($notice->fetch()) {
257                         $this->show_notice($notice);
258                 }
259                 # XXX: show a link for the next page
260                 common_element_end('div');
261         }
262
263         function show_last_notice($profile) {
264
265                 common_element_start('div', 'lastnotice');
266                 common_element('h2', 'lastnotice', _t('Currently'));
267
268                 $notice = DB_DataObject::factory('notice');
269                 $notice->profile_id = $profile->id;
270                 $notice->orderBy('created DESC');
271                 $notice->limit(0, 1);
272
273                 if ($notice->find(true)) {
274                         # FIXME: URL, image, video, audio
275                         common_element('span', array('class' => 'content'),
276                                                    $notice->content);
277                         common_element('span', array('class' => 'date'),
278                                                    common_date_string($notice->created));
279                 }
280
281                 common_element_end('div');
282         }
283 }