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