joingroup.php 4.58 KB
Newer Older
1 2
<?php
/**
3
 * StatusNet, the distributed open-source microblogging tool
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 * Join a group
 *
 * PHP version 5
 *
 * LICENCE: This program is free software: you can redistribute it and/or modify
 * 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/>.
 *
 * @category  Group
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 35 36 37 38 39 40
    exit(1);
}

/**
 * Join a group
 *
 * This is the action for joining a group. It works more or less like the subscribe action
 * for users.
 *
 * @category Group
41
 * @package  StatusNet
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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
 */

class JoingroupAction extends Action
{
    var $group = null;

    /**
     * Prepare to run
     */

    function prepare($args)
    {
        parent::prepare($args);

        if (!common_logged_in()) {
            $this->clientError(_('You must be logged in to join a group.'));
            return false;
        }

        $nickname_arg = $this->trimmed('nickname');
        $nickname = common_canonical_nickname($nickname_arg);

        // Permanent redirect on non-canonical nickname

        if ($nickname_arg != $nickname) {
            $args = array('nickname' => $nickname);
71
            common_redirect(common_local_url('joingroup', $args), 301);
72 73 74 75
            return false;
        }

        if (!$nickname) {
76
            $this->clientError(_('No nickname.'), 404);
77 78 79
            return false;
        }

80 81 82 83 84 85 86 87
        $local = Local_group::staticGet('nickname', $nickname);

        if (!$local) {
            $this->clientError(_('No such group.'), 404);
            return false;
        }

        $this->group = User_group::staticGet('id', $local->group_id);
88 89

        if (!$this->group) {
90
            $this->clientError(_('No such group.'), 404);
91 92 93 94 95
            return false;
        }

        $cur = common_current_user();

96
        if ($cur->isMember($this->group)) {
97
            $this->clientError(_('You are already a member of that group.'), 403);
98 99 100
            return false;
        }

101 102 103 104 105
        if (Group_block::isBlocked($this->group, $cur->getProfile())) {
            $this->clientError(_('You have been blocked from that group by the admin.'), 403);
            return false;
        }

106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
        return true;
    }

    /**
     * Handle the request
     *
     * On POST, add the current user to the group
     *
     * @param array $args unused
     *
     * @return void
     */

    function handle($args)
    {
        parent::handle($args);

        $cur = common_current_user();

125 126 127 128 129 130
        try {
            if (Event::handle('StartJoinGroup', array($this->group, $cur))) {
                Group_member::join($this->group->id, $cur->id);
                Event::handle('EndJoinGroup', array($this->group, $cur));
            }
        } catch (Exception $e) {
131
            $this->serverError(sprintf(_('Could not join user %1$s to group %2$s.'),
132 133 134 135 136 137
                                       $cur->nickname, $this->group->nickname));
        }

        if ($this->boolean('ajax')) {
            $this->startHTML('text/xml;charset=utf-8');
            $this->elementStart('head');
138
            $this->element('title', null, sprintf(_('%1$s joined group %2$s'),
139 140 141 142 143 144 145 146 147 148
                                                  $cur->nickname,
                                                  $this->group->nickname));
            $this->elementEnd('head');
            $this->elementStart('body');
            $lf = new LeaveForm($this, $this->group);
            $lf->show();
            $this->elementEnd('body');
            $this->elementEnd('html');
        } else {
            common_redirect(common_local_url('groupmembers', array('nickname' =>
149 150
                                                                   $this->group->nickname)),
                            303);
151 152 153
        }
    }
}