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