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