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