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