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