]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/profilelist.php
Added more checked type-hints.
[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', 'profile_list 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 $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',
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($this->profile);
157                 $this->out->elementEnd('a');
158                 Event::handle('EndProfileListItemAvatar', array($this));
159             }
160             if (Event::handle('StartProfileListItemNickname', array($this))) {
161                 $this->showNickname();
162                 Event::handle('EndProfileListItemNickname', 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 h-card');
192     }
193
194     function showNickname()
195     {
196         $this->out->element('a', array('href'=>$this->profile->getUrl(),
197                                        'class'=>'p-nickname'),
198                             $this->profile->getNickname());
199     }
200
201     function showFullName()
202     {
203         if (!empty($this->profile->fullname)) {
204             $this->out->element('span', 'p-name', $this->profile->fullname);
205         }
206     }
207
208     function showLocation()
209     {
210         if (!empty($this->profile->location)) {
211             $this->out->element('span', 'label p-locality', $this->profile->location);
212         }
213     }
214
215     function showHomepage()
216     {
217         if (!empty($this->profile->homepage)) {
218             $this->out->text(' ');
219             $aAttrs = $this->homepageAttributes();
220             $this->out->elementStart('a', $aAttrs);
221             $this->out->raw($this->highlight($this->profile->homepage));
222             $this->out->elementEnd('a');
223         }
224     }
225
226     function showBio()
227     {
228         if (!empty($this->profile->bio)) {
229             $this->out->elementStart('p', 'note');
230             $this->out->raw($this->highlight($this->profile->bio));
231             $this->out->elementEnd('p');
232         }
233     }
234
235     function showTags()
236     {
237         $user = common_current_user();
238         if (!empty($user)) {
239             if ($user->id == $this->profile->id) {
240                 $tags = new SelftagsWidget($this->out, $user, $this->profile);
241                 $tags->show();
242             } else if ($user->getProfile()->canTag($this->profile)) {
243                 $tags = new PeopletagsWidget($this->out, $user, $this->profile);
244                 $tags->show();
245             }
246         }
247     }
248
249     function endProfile()
250     {
251         $this->out->elementEnd('div');
252     }
253
254     function showActions()
255     {
256         $this->startActions();
257         if (Event::handle('StartProfileListItemActionElements', array($this))) {
258             $this->showSubscribeButton();
259             Event::handle('EndProfileListItemActionElements', array($this));
260         }
261         $this->endActions();
262     }
263
264     function startActions()
265     {
266         $this->out->elementStart('div', 'entity_actions');
267         $this->out->elementStart('ul');
268     }
269
270     function showSubscribeButton()
271     {
272         // Is this a logged-in user, looking at someone else's
273         // profile?
274
275         $user = common_current_user();
276
277         if (!empty($user) && $this->profile->id != $user->id) {
278             $this->out->elementStart('li', 'entity_subscribe');
279             if ($user->isSubscribed($this->profile)) {
280                 $usf = new UnsubscribeForm($this->out, $this->profile);
281                 $usf->show();
282             } else {
283                 if (Event::handle('StartShowProfileListSubscribeButton', array($this))) {
284                     $sf = new SubscribeForm($this->out, $this->profile);
285                     $sf->show();
286                     Event::handle('EndShowProfileListSubscribeButton', array($this));
287                 }
288             }
289             $this->out->elementEnd('li');
290         }
291     }
292
293     function endActions()
294     {
295         $this->out->elementEnd('ul');
296         $this->out->elementEnd('div');
297     }
298
299     function endItem()
300     {
301         $this->out->elementEnd('li');
302     }
303
304     function highlight($text)
305     {
306         return htmlspecialchars($text);
307     }
308
309     function linkAttributes()
310     {
311         return array('href' => $this->profile->profileurl,
312                      'class' => 'u-url',
313                      'rel' => 'contact');
314     }
315
316     function homepageAttributes()
317     {
318         return array('href' => $this->profile->homepage,
319                      'class' => 'u-url');
320     }
321 }