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