We are no longer offering accounts on this server. Consider https://gitlab.freedesktop.org/ as a place to host projects.

profilelist.php 10.4 KB
Newer Older
1
<?php
2
/**
3
 * StatusNet, the distributed open-source microblogging tool
4
 *
5 6 7 8 9
 * Widget to show a list of profiles
 *
 * PHP version 5
 *
 * LICENCE: This program is free software: you can redistribute it and/or modify
10 11 12 13 14 15 16 17 18 19 20
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * 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 32 33 34
    exit(1);
}

require_once INSTALLDIR.'/lib/widget.php';
Shashi Gowda's avatar
Shashi Gowda committed
35
require_once INSTALLDIR.'/lib/peopletags.php';
36

37 38 39 40
/**
 * Widget to show a list of profiles
 *
 * @category Public
41
 * @package  StatusNet
42 43
 * @author   Zach Copley <zach@status.net>
 * @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 48 49
 */
class ProfileList extends Widget
{
    /** Current profile, profile query. */
Evan Prodromou's avatar
Evan Prodromou committed
50
    var $profile = null;
51
    /** Action object using us. */
Evan Prodromou's avatar
Evan Prodromou committed
52
    var $action = null;
53

54
    function __construct($profile, $action=null)
55
    {
56 57
        parent::__construct($action);

58 59 60 61
        $this->profile = $profile;
        $this->action = $action;
    }

62
    function show()
63
    {
64 65 66 67 68 69 70 71 72
        $cnt = 0;

        if (Event::handle('StartProfileList', array($this))) {
            $this->startList();
            $cnt = $this->showProfiles();
            $this->endList();
            Event::handle('EndProfileList', array($this));
        }

Evan Prodromou's avatar
Evan Prodromou committed
73
        return $cnt;
74 75 76
    }

    function startList()
77
    {
78
        $this->out->elementStart('ul', 'profiles xoxo');
79
    }
80

81 82 83 84 85 86 87
    function endList()
    {
        $this->out->elementEnd('ul');
    }

    function showProfiles()
    {
88 89 90 91 92 93 94
        // Note: we don't use fetchAll() because it's borked with query()

        $profiles = array();

        while ($this->profile->fetch()) {
            $profiles[] = clone($this->profile);
        }
95

96 97 98 99 100 101
        $cnt = count($profiles);

        $max = min($cnt, $this->maxProfiles());

        for ($i = 0; $i < $max; $i++) {
            $pli = $this->newListItem($profiles[$i]);
102
            $pli->show();
103 104 105 106 107
        }

        return $cnt;
    }

108 109
    function newListItem($profile)
    {
110 111 112 113 114 115 116 117 118 119 120
        return new ProfileListItem($profile, $this->action);
    }

    function maxProfiles()
    {
        return PROFILES_PER_PAGE;
    }

    function avatarSize()
    {
        return AVATAR_STREAM_SIZE;
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
    }
}

class ProfileListItem extends Widget
{
    /** Current profile. */
    var $profile = null;
    /** Action object using us. */
    var $action = null;

    function __construct($profile, $action)
    {
        parent::__construct($action);

        $this->profile = $profile;
        $this->action  = $action;
    }

    function show()
    {
141 142 143 144 145 146 147 148 149 150 151 152 153
        if (Event::handle('StartProfileListItem', array($this))) {
            $this->startItem();
            if (Event::handle('StartProfileListItemProfile', array($this))) {
                $this->showProfile();
                Event::handle('EndProfileListItemProfile', array($this));
            }
            if (Event::handle('StartProfileListItemActions', array($this))) {
                $this->showActions();
                Event::handle('EndProfileListItemActions', array($this));
            }
            $this->endItem();
            Event::handle('EndProfileListItem', array($this));
        }
154 155 156
    }

    function startItem()
157
    {
158
        $this->out->elementStart('li', array('class' => 'profile hentry',
sarven's avatar
sarven committed
159
                                             'id' => 'profile-' . $this->profile->id));
160
    }
161

162 163 164
    function showProfile()
    {
        $this->startProfile();
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
        if (Event::handle('StartProfileListItemProfileElements', array($this))) {
            if (Event::handle('StartProfileListItemAvatar', array($this))) {
                $this->showAvatar();
                Event::handle('EndProfileListItemAvatar', array($this));
            }
            if (Event::handle('StartProfileListItemFullName', array($this))) {
                $this->showFullName();
                Event::handle('EndProfileListItemFullName', array($this));
            }
            if (Event::handle('StartProfileListItemLocation', array($this))) {
                $this->showLocation();
                Event::handle('EndProfileListItemLocation', array($this));
            }
            if (Event::handle('StartProfileListItemHomepage', array($this))) {
                $this->showHomepage();
                Event::handle('EndProfileListItemHomepage', array($this));
            }
            if (Event::handle('StartProfileListItemBio', array($this))) {
                $this->showBio();
                Event::handle('EndProfileListItemBio', array($this));
            }
Shashi Gowda's avatar
Shashi Gowda committed
186 187 188 189
            if (Event::handle('StartProfileListItemTags', array($this))) {
                $this->showTags();
                Event::handle('EndProfileListItemTags', array($this));
            }
190 191
            Event::handle('EndProfileListItemProfileElements', array($this));
        }
192 193
        $this->endProfile();
    }
194

195 196
    function startProfile()
    {
197
        $this->out->elementStart('div', 'entity_profile vcard entry-content');
198
    }
199

200 201
    function showAvatar()
    {
202
        $avatar = $this->profile->getAvatar(AVATAR_STREAM_SIZE);
203 204
        $aAttrs = $this->linkAttributes();
        $this->out->elementStart('a', $aAttrs);
205
        $this->out->element('img', array('src' => (!empty($avatar)) ? $avatar->displayUrl() : Avatar::defaultImage(AVATAR_STREAM_SIZE),
Evan Prodromou's avatar
Evan Prodromou committed
206 207 208 209 210 211
                                         'class' => 'photo avatar',
                                         'width' => AVATAR_STREAM_SIZE,
                                         'height' => AVATAR_STREAM_SIZE,
                                         'alt' =>
                                         ($this->profile->fullname) ? $this->profile->fullname :
                                         $this->profile->nickname));
212
        $this->out->text(' ');
213
        $hasFN = (!empty($this->profile->fullname)) ? 'nickname' : 'fn nickname';
sarven's avatar
sarven committed
214
        $this->out->elementStart('span', $hasFN);
215
        $this->out->raw($this->highlight($this->profile->nickname));
sarven's avatar
sarven committed
216
        $this->out->elementEnd('span');
217
        $this->out->elementEnd('a');
218
    }
Evan Prodromou's avatar
Evan Prodromou committed
219

220 221
    function showFullName()
    {
222
        if (!empty($this->profile->fullname)) {
223
            $this->out->text(' ');
sarven's avatar
sarven committed
224
            $this->out->elementStart('span', 'fn');
225 226
            $this->out->raw($this->highlight($this->profile->fullname));
            $this->out->elementEnd('span');
227
        }
228 229 230 231
    }

    function showLocation()
    {
232
        if (!empty($this->profile->location)) {
233
            $this->out->text(' ');
234
            $this->out->elementStart('span', 'label');
235
            $this->out->raw($this->highlight($this->profile->location));
236
            $this->out->elementEnd('span');
237
        }
238 239 240 241
    }

    function showHomepage()
    {
242
        if (!empty($this->profile->homepage)) {
243
            $this->out->text(' ');
244 245
            $aAttrs = $this->homepageAttributes();
            $this->out->elementStart('a', $aAttrs);
246 247
            $this->out->raw($this->highlight($this->profile->homepage));
            $this->out->elementEnd('a');
248
        }
249 250 251 252
    }

    function showBio()
    {
253
        if (!empty($this->profile->bio)) {
254
            $this->out->elementStart('p', 'note');
255
            $this->out->raw($this->highlight($this->profile->bio));
256
            $this->out->elementEnd('p');
257
        }
258
    }
259

Shashi Gowda's avatar
Shashi Gowda committed
260 261 262 263 264 265 266 267 268 269 270 271 272 273
    function showTags()
    {
        $user = common_current_user();
        if (!empty($user)) {
            if ($user->id == $this->profile->id) {
                $tags = new SelftagsWidget($this->out, $user, $this->profile);
                $tags->show();
            } else if ($user->getProfile()->canTag($this->profile)) {
                $tags = new PeopletagsWidget($this->out, $user, $this->profile);
                $tags->show();
            }
        }
    }

274 275
    function endProfile()
    {
sarven's avatar
sarven committed
276
        $this->out->elementEnd('div');
277
    }
sarven's avatar
sarven committed
278

279 280 281
    function showActions()
    {
        $this->startActions();
282 283 284 285
        if (Event::handle('StartProfileListItemActionElements', array($this))) {
            $this->showSubscribeButton();
            Event::handle('EndProfileListItemActionElements', array($this));
        }
286 287
        $this->endActions();
    }
Evan Prodromou's avatar
Evan Prodromou committed
288

289 290 291
    function startActions()
    {
        $this->out->elementStart('div', 'entity_actions');
Evan Prodromou's avatar
Evan Prodromou committed
292
        $this->out->elementStart('ul');
293
    }
Evan Prodromou's avatar
Evan Prodromou committed
294

295 296
    function showSubscribeButton()
    {
297 298 299
        // Is this a logged-in user, looking at someone else's
        // profile?

300 301
        $user = common_current_user();

302
        if (!empty($user) && $this->profile->id != $user->id) {
303 304 305 306 307
            $this->out->elementStart('li', 'entity_subscribe');
            if ($user->isSubscribed($this->profile)) {
                $usf = new UnsubscribeForm($this->out, $this->profile);
                $usf->show();
            } else {
308
                if (Event::handle('StartShowProfileListSubscribeButton', array($this))) {
309 310
                    $sf = new SubscribeForm($this->out, $this->profile);
                    $sf->show();
311
                    Event::handle('EndShowProfileListSubscribeButton', array($this));
312
                }
sarven's avatar
sarven committed
313
            }
314
            $this->out->elementEnd('li');
sarven's avatar
sarven committed
315
        }
316
    }
sarven's avatar
sarven committed
317

318 319
    function endActions()
    {
Evan Prodromou's avatar
Evan Prodromou committed
320
        $this->out->elementEnd('ul');
Evan Prodromou's avatar
Evan Prodromou committed
321
        $this->out->elementEnd('div');
322
    }
323

324
    function endItem()
325
    {
326
        $this->out->elementEnd('li');
327 328
    }

329 330
    function highlight($text)
    {
331 332
        return htmlspecialchars($text);
    }
333 334 335 336 337 338 339

    function linkAttributes()
    {
        return array('href' => $this->profile->profileurl,
                     'class' => 'url entry-title',
                     'rel' => 'contact');
    }
340 341 342 343 344 345

    function homepageAttributes()
    {
        return array('href' => $this->profile->homepage,
                     'class' => 'url');
    }
sarven's avatar
sarven committed
346
}