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