]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/profilelist.php
use an array of profiles rather than a looping cursor for profile lists
[quix0rs-gnu-social.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         $profiles = $this->profile->fetchAll();
89
90         $cnt = count($profiles);
91
92         $max = min($cnt, $this->maxProfiles());
93
94         for ($i = 0; $i < $max; $i++) {
95             $pli = $this->newListItem($profiles[$i]);
96             $pli->show();
97         }
98
99         return $cnt;
100     }
101
102     function newListItem($profile)
103     {
104         return new ProfileListItem($profile, $this->action);
105     }
106
107     function maxProfiles()
108     {
109         return PROFILES_PER_PAGE;
110     }
111
112     function avatarSize()
113     {
114         return AVATAR_STREAM_SIZE;
115     }
116 }
117
118 class ProfileListItem extends Widget
119 {
120     /** Current profile. */
121     var $profile = null;
122     /** Action object using us. */
123     var $action = null;
124
125     function __construct($profile, $action)
126     {
127         parent::__construct($action);
128
129         $this->profile = $profile;
130         $this->action  = $action;
131     }
132
133     function show()
134     {
135         if (Event::handle('StartProfileListItem', array($this))) {
136             $this->startItem();
137             if (Event::handle('StartProfileListItemProfile', array($this))) {
138                 $this->showProfile();
139                 Event::handle('EndProfileListItemProfile', array($this));
140             }
141             if (Event::handle('StartProfileListItemActions', array($this))) {
142                 $this->showActions();
143                 Event::handle('EndProfileListItemActions', array($this));
144             }
145             $this->endItem();
146             Event::handle('EndProfileListItem', array($this));
147         }
148     }
149
150     function startItem()
151     {
152         $this->out->elementStart('li', array('class' => 'profile hentry',
153                                              'id' => 'profile-' . $this->profile->id));
154     }
155
156     function showProfile()
157     {
158         $this->startProfile();
159         if (Event::handle('StartProfileListItemProfileElements', array($this))) {
160             if (Event::handle('StartProfileListItemAvatar', array($this))) {
161                 $this->showAvatar();
162                 Event::handle('EndProfileListItemAvatar', array($this));
163             }
164             if (Event::handle('StartProfileListItemFullName', array($this))) {
165                 $this->showFullName();
166                 Event::handle('EndProfileListItemFullName', array($this));
167             }
168             if (Event::handle('StartProfileListItemLocation', array($this))) {
169                 $this->showLocation();
170                 Event::handle('EndProfileListItemLocation', array($this));
171             }
172             if (Event::handle('StartProfileListItemHomepage', array($this))) {
173                 $this->showHomepage();
174                 Event::handle('EndProfileListItemHomepage', array($this));
175             }
176             if (Event::handle('StartProfileListItemBio', array($this))) {
177                 $this->showBio();
178                 Event::handle('EndProfileListItemBio', array($this));
179             }
180             if (Event::handle('StartProfileListItemTags', array($this))) {
181                 $this->showTags();
182                 Event::handle('EndProfileListItemTags', array($this));
183             }
184             Event::handle('EndProfileListItemProfileElements', array($this));
185         }
186         $this->endProfile();
187     }
188
189     function startProfile()
190     {
191         $this->out->elementStart('div', 'entity_profile vcard entry-content');
192     }
193
194     function showAvatar()
195     {
196         $avatar = $this->profile->getAvatar(AVATAR_STREAM_SIZE);
197         $aAttrs = $this->linkAttributes();
198         $this->out->elementStart('a', $aAttrs);
199         $this->out->element('img', array('src' => (!empty($avatar)) ? $avatar->displayUrl() : Avatar::defaultImage(AVATAR_STREAM_SIZE),
200                                          'class' => 'photo avatar',
201                                          'width' => AVATAR_STREAM_SIZE,
202                                          'height' => AVATAR_STREAM_SIZE,
203                                          'alt' =>
204                                          ($this->profile->fullname) ? $this->profile->fullname :
205                                          $this->profile->nickname));
206         $this->out->text(' ');
207         $hasFN = (!empty($this->profile->fullname)) ? 'nickname' : 'fn nickname';
208         $this->out->elementStart('span', $hasFN);
209         $this->out->raw($this->highlight($this->profile->nickname));
210         $this->out->elementEnd('span');
211         $this->out->elementEnd('a');
212     }
213
214     function showFullName()
215     {
216         if (!empty($this->profile->fullname)) {
217             $this->out->text(' ');
218             $this->out->elementStart('span', 'fn');
219             $this->out->raw($this->highlight($this->profile->fullname));
220             $this->out->elementEnd('span');
221         }
222     }
223
224     function showLocation()
225     {
226         if (!empty($this->profile->location)) {
227             $this->out->text(' ');
228             $this->out->elementStart('span', 'label');
229             $this->out->raw($this->highlight($this->profile->location));
230             $this->out->elementEnd('span');
231         }
232     }
233
234     function showHomepage()
235     {
236         if (!empty($this->profile->homepage)) {
237             $this->out->text(' ');
238             $aAttrs = $this->homepageAttributes();
239             $this->out->elementStart('a', $aAttrs);
240             $this->out->raw($this->highlight($this->profile->homepage));
241             $this->out->elementEnd('a');
242         }
243     }
244
245     function showBio()
246     {
247         if (!empty($this->profile->bio)) {
248             $this->out->elementStart('p', 'note');
249             $this->out->raw($this->highlight($this->profile->bio));
250             $this->out->elementEnd('p');
251         }
252     }
253
254     function showTags()
255     {
256         $user = common_current_user();
257         if (!empty($user)) {
258             if ($user->id == $this->profile->id) {
259                 $tags = new SelftagsWidget($this->out, $user, $this->profile);
260                 $tags->show();
261             } else if ($user->getProfile()->canTag($this->profile)) {
262                 $tags = new PeopletagsWidget($this->out, $user, $this->profile);
263                 $tags->show();
264             }
265         }
266     }
267
268     function endProfile()
269     {
270         $this->out->elementEnd('div');
271     }
272
273     function showActions()
274     {
275         $this->startActions();
276         if (Event::handle('StartProfileListItemActionElements', array($this))) {
277             $this->showSubscribeButton();
278             Event::handle('EndProfileListItemActionElements', array($this));
279         }
280         $this->endActions();
281     }
282
283     function startActions()
284     {
285         $this->out->elementStart('div', 'entity_actions');
286         $this->out->elementStart('ul');
287     }
288
289     function showSubscribeButton()
290     {
291         // Is this a logged-in user, looking at someone else's
292         // profile?
293
294         $user = common_current_user();
295
296         if (!empty($user) && $this->profile->id != $user->id) {
297             $this->out->elementStart('li', 'entity_subscribe');
298             if ($user->isSubscribed($this->profile)) {
299                 $usf = new UnsubscribeForm($this->out, $this->profile);
300                 $usf->show();
301             } else {
302                 if (Event::handle('StartShowProfileListSubscribeButton', array($this))) {
303                     $sf = new SubscribeForm($this->out, $this->profile);
304                     $sf->show();
305                     Event::handle('EndShowProfileListSubscribeButton', array($this));
306                 }
307             }
308             $this->out->elementEnd('li');
309         }
310     }
311
312     function endActions()
313     {
314         $this->out->elementEnd('ul');
315         $this->out->elementEnd('div');
316     }
317
318     function endItem()
319     {
320         $this->out->elementEnd('li');
321     }
322
323     function highlight($text)
324     {
325         return htmlspecialchars($text);
326     }
327
328     function linkAttributes()
329     {
330         return array('href' => $this->profile->profileurl,
331                      'class' => 'url entry-title',
332                      'rel' => 'contact');
333     }
334
335     function homepageAttributes()
336     {
337         return array('href' => $this->profile->homepage,
338                      'class' => 'url');
339     }
340 }