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