don't use fetchAll() for profile lists ever
[statusnet:mainline.git] / lib / profilelist.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Widget to show a list of profiles
6  *
7  * PHP version 5
8  *
9  * LICENCE: This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU Affero General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU Affero General Public License for more details.
18  *
19  * You should have received a copy of the GNU Affero General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  *
22  * @category  Public
23  * @package   StatusNet
24  * @author    Evan Prodromou <evan@status.net>
25  * @copyright 2008-2009 StatusNet, Inc.
26  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27  * @link      http://status.net/
28  */
29
30 if (!defined('STATUSNET') && !defined('LACONICA')) {
31     exit(1);
32 }
33
34 require_once INSTALLDIR.'/lib/widget.php';
35 require_once INSTALLDIR.'/lib/peopletags.php';
36
37 /**
38  * Widget to show a list of profiles
39  *
40  * @category Public
41  * @package  StatusNet
42  * @author   Zach Copley <zach@status.net>
43  * @author   Evan Prodromou <evan@status.net>
44  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
45  * @link     http://status.net/
46  */
47 class ProfileList extends Widget
48 {
49     /** Current profile, profile query. */
50     var $profile = null;
51     /** Action object using us. */
52     var $action = null;
53
54     function __construct($profile, $action=null)
55     {
56         parent::__construct($action);
57
58         $this->profile = $profile;
59         $this->action = $action;
60     }
61
62     function show()
63     {
64         $cnt = 0;
65
66         if (Event::handle('StartProfileList', array($this))) {
67             $this->startList();
68             $cnt = $this->showProfiles();
69             $this->endList();
70             Event::handle('EndProfileList', array($this));
71         }
72
73         return $cnt;
74     }
75
76     function startList()
77     {
78         $this->out->elementStart('ul', 'profiles xoxo');
79     }
80
81     function endList()
82     {
83         $this->out->elementEnd('ul');
84     }
85
86     function showProfiles()
87     {
88         // Note: we don't use fetchAll() because it's borked with query()
89
90         $profiles = array();
91
92         while ($this->profile->fetch()) {
93             $profiles[] = clone($this->profile);
94         }
95
96         $cnt = count($profiles);
97
98         $max = min($cnt, $this->maxProfiles());
99
100         for ($i = 0; $i < $max; $i++) {
101             $pli = $this->newListItem($profiles[$i]);
102             $pli->show();
103         }
104
105         return $cnt;
106     }
107
108     function newListItem($profile)
109     {
110         return new ProfileListItem($profile, $this->action);
111     }
112
113     function maxProfiles()
114     {
115         return PROFILES_PER_PAGE;
116     }
117
118     function avatarSize()
119     {
120         return AVATAR_STREAM_SIZE;
121     }
122 }
123
124 class ProfileListItem extends Widget
125 {
126     /** Current profile. */
127     var $profile = null;
128     /** Action object using us. */
129     var $action = null;
130
131     function __construct($profile, $action)
132     {
133         parent::__construct($action);
134
135         $this->profile = $profile;
136         $this->action  = $action;
137     }
138
139     function show()
140     {
141         if (Event::handle('StartProfileListItem', array($this))) {
142             $this->startItem();
143             if (Event::handle('StartProfileListItemProfile', array($this))) {
144                 $this->showProfile();
145                 Event::handle('EndProfileListItemProfile', array($this));
146             }
147             if (Event::handle('StartProfileListItemActions', array($this))) {
148                 $this->showActions();
149                 Event::handle('EndProfileListItemActions', array($this));
150             }
151             $this->endItem();
152             Event::handle('EndProfileListItem', array($this));
153         }
154     }
155
156     function startItem()
157     {
158         $this->out->elementStart('li', array('class' => 'profile hentry',
159                                              'id' => 'profile-' . $this->profile->id));
160     }
161
162     function showProfile()
163     {
164         $this->startProfile();
165         if (Event::handle('StartProfileListItemProfileElements', array($this))) {
166             if (Event::handle('StartProfileListItemAvatar', array($this))) {
167                 $this->showAvatar();
168                 Event::handle('EndProfileListItemAvatar', array($this));
169             }
170             if (Event::handle('StartProfileListItemFullName', array($this))) {
171                 $this->showFullName();
172                 Event::handle('EndProfileListItemFullName', array($this));
173             }
174             if (Event::handle('StartProfileListItemLocation', array($this))) {
175                 $this->showLocation();
176                 Event::handle('EndProfileListItemLocation', array($this));
177             }
178             if (Event::handle('StartProfileListItemHomepage', array($this))) {
179                 $this->showHomepage();
180                 Event::handle('EndProfileListItemHomepage', array($this));
181             }
182             if (Event::handle('StartProfileListItemBio', array($this))) {
183                 $this->showBio();
184                 Event::handle('EndProfileListItemBio', array($this));
185             }
186             if (Event::handle('StartProfileListItemTags', array($this))) {
187                 $this->showTags();
188                 Event::handle('EndProfileListItemTags', array($this));
189             }
190             Event::handle('EndProfileListItemProfileElements', array($this));
191         }
192         $this->endProfile();
193     }
194
195     function startProfile()
196     {
197         $this->out->elementStart('div', 'entity_profile vcard entry-content');
198     }
199
200     function showAvatar()
201     {
202         $avatar = $this->profile->getAvatar(AVATAR_STREAM_SIZE);
203         $aAttrs = $this->linkAttributes();
204         $this->out->elementStart('a', $aAttrs);
205         $this->out->element('img', array('src' => (!empty($avatar)) ? $avatar->displayUrl() : Avatar::defaultImage(AVATAR_STREAM_SIZE),
206                                          'class' => 'photo avatar',
207                                          'width' => AVATAR_STREAM_SIZE,
208                                          'height' => AVATAR_STREAM_SIZE,
209                                          'alt' =>
210                                          ($this->profile->fullname) ? $this->profile->fullname :
211                                          $this->profile->nickname));
212         $this->out->text(' ');
213         $hasFN = (!empty($this->profile->fullname)) ? 'nickname' : 'fn nickname';
214         $this->out->elementStart('span', $hasFN);
215         $this->out->raw($this->highlight($this->profile->nickname));
216         $this->out->elementEnd('span');
217         $this->out->elementEnd('a');
218     }
219
220     function showFullName()
221     {
222         if (!empty($this->profile->fullname)) {
223             $this->out->text(' ');
224             $this->out->elementStart('span', 'fn');
225             $this->out->raw($this->highlight($this->profile->fullname));
226             $this->out->elementEnd('span');
227         }
228     }
229
230     function showLocation()
231     {
232         if (!empty($this->profile->location)) {
233             $this->out->text(' ');
234             $this->out->elementStart('span', 'label');
235             $this->out->raw($this->highlight($this->profile->location));
236             $this->out->elementEnd('span');
237         }
238     }
239
240     function showHomepage()
241     {
242         if (!empty($this->profile->homepage)) {
243             $this->out->text(' ');
244             $aAttrs = $this->homepageAttributes();
245             $this->out->elementStart('a', $aAttrs);
246             $this->out->raw($this->highlight($this->profile->homepage));
247             $this->out->elementEnd('a');
248         }
249     }
250
251     function showBio()
252     {
253         if (!empty($this->profile->bio)) {
254             $this->out->elementStart('p', 'note');
255             $this->out->raw($this->highlight($this->profile->bio));
256             $this->out->elementEnd('p');
257         }
258     }
259
260     function showTags()
261     {
262         $user = common_current_user();
263         if (!empty($user)) {
264             if ($user->id == $this->profile->id) {
265                 $tags = new SelftagsWidget($this->out, $user, $this->profile);
266                 $tags->show();
267             } else if ($user->getProfile()->canTag($this->profile)) {
268                 $tags = new PeopletagsWidget($this->out, $user, $this->profile);
269                 $tags->show();
270             }
271         }
272     }
273
274     function endProfile()
275     {
276         $this->out->elementEnd('div');
277     }
278
279     function showActions()
280     {
281         $this->startActions();
282         if (Event::handle('StartProfileListItemActionElements', array($this))) {
283             $this->showSubscribeButton();
284             Event::handle('EndProfileListItemActionElements', array($this));
285         }
286         $this->endActions();
287     }
288
289     function startActions()
290     {
291         $this->out->elementStart('div', 'entity_actions');
292         $this->out->elementStart('ul');
293     }
294
295     function showSubscribeButton()
296     {
297         // Is this a logged-in user, looking at someone else's
298         // profile?
299
300         $user = common_current_user();
301
302         if (!empty($user) && $this->profile->id != $user->id) {
303             $this->out->elementStart('li', 'entity_subscribe');
304             if ($user->isSubscribed($this->profile)) {
305                 $usf = new UnsubscribeForm($this->out, $this->profile);
306                 $usf->show();
307             } else {
308                 if (Event::handle('StartShowProfileListSubscribeButton', array($this))) {
309                     $sf = new SubscribeForm($this->out, $this->profile);
310                     $sf->show();
311                     Event::handle('EndShowProfileListSubscribeButton', array($this));
312                 }
313             }
314             $this->out->elementEnd('li');
315         }
316     }
317
318     function endActions()
319     {
320         $this->out->elementEnd('ul');
321         $this->out->elementEnd('div');
322     }
323
324     function endItem()
325     {
326         $this->out->elementEnd('li');
327     }
328
329     function highlight($text)
330     {
331         return htmlspecialchars($text);
332     }
333
334     function linkAttributes()
335     {
336         return array('href' => $this->profile->profileurl,
337                      'class' => 'url entry-title',
338                      'rel' => 'contact');
339     }
340
341     function homepageAttributes()
342     {
343         return array('href' => $this->profile->homepage,
344                      'class' => 'url');
345     }
346 }