editpeopletag.php 11.1 KB
Newer Older
Shashi Gowda's avatar
Shashi Gowda committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
<?php
/**
 * StatusNet, the distributed open-source microblogging tool
 *
 * Edit an existing 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
 * @package   StatusNet
 * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
 * @link      http://status.net/
 */

if (!defined('STATUSNET') && !defined('LACONICA')) {
    exit(1);
}

/**
 * Add a new group
 *
 * This is the form for adding a new group
 *
 * @category Group
 * @package  StatusNet
 * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
 * @link     http://status.net/
 */

43
class EditpeopletagAction extends Action
Shashi Gowda's avatar
Shashi Gowda committed
44 45 46 47 48 49
{
    var $msg, $confirm, $confirm_args=array();

    function title()
    {
        if ($_SERVER['REQUEST_METHOD'] == 'POST' && $this->boolean('delete')) {
50 51
            // TRANS: Title for edit list page after deleting a tag.
            // TRANS: %s is a list.
52
            return sprintf(_('Delete %s list'), $this->peopletag->tag);
Shashi Gowda's avatar
Shashi Gowda committed
53
        }
54 55
        // TRANS: Title for edit list page.
        // TRANS: %s is a list.
56
        return sprintf(_('Edit list %s'), $this->peopletag->tag);
Shashi Gowda's avatar
Shashi Gowda committed
57 58 59 60 61 62 63 64 65 66 67
    }

    /**
     * Prepare to run
     */

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

        if (!common_logged_in()) {
68
            // TRANS: Error message displayed when trying to perform an action that requires a logged in user.
Shashi Gowda's avatar
Shashi Gowda committed
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
            $this->clientError(_('Not logged in.'));
            return false;
        }

        $id = $this->arg('id');
        $tagger_arg = $this->arg('tagger');
        $tag_arg = $this->arg('tag');

        $tagger = common_canonical_nickname($tagger_arg);
        $tag = common_canonical_tag($tag_arg);

        $current = common_current_user();

        // Permanent redirect on non-canonical tag

        if ($tagger_arg != $tagger || $tag_arg != $tag) {
            $args = array('tagger' => $tagger, 'tag' => $tag);
            common_redirect(common_local_url('editpeopletag', $args), 301);
            return false;
        }

        $user = null;
        if ($id) {
            $this->peopletag = Profile_list::staticGet('id', $id);
            if (!empty($this->peopletag)) {
                $user = User::staticGet('id', $this->peopletag->tagger);
            }
        } else {
            if (!$tagger) {
98
                // TRANS: Error message displayed when trying to perform an action that requires a tagging user or ID.
Shashi Gowda's avatar
Shashi Gowda committed
99 100 101 102 103 104 105 106 107
                $this->clientError(_('No tagger or ID.'), 404);
                return false;
            }

            $user = User::staticGet('nickname', $tagger);
            $this->peopletag = Profile_list::pkeyGet(array('tagger' => $user->id, 'tag' => $tag));
        }

        if (!$this->peopletag) {
Siebrand Mazeland's avatar
Siebrand Mazeland committed
108
            // TRANS: Client error displayed when referring to a non-existing list.
109
            $this->clientError(_('No such list.'), 404);
Shashi Gowda's avatar
Shashi Gowda committed
110 111 112 113 114
            return false;
        }

        if (!$user) {
            // This should not be happening
115
            // TRANS: Client error displayed when referring to non-local user.
Shashi Gowda's avatar
Shashi Gowda committed
116 117 118 119 120
            $this->clientError(_('Not a local user.'), 404);
            return false;
        }

        if ($current->id != $user->id) {
121
            // TRANS: Client error displayed when reting to edit a tag that was not self-created.
Shashi Gowda's avatar
Shashi Gowda committed
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
            $this->clientError(_('You must be the creator of the tag to edit it.'), 404);
            return false;
        }

        $this->tagger = $user->getProfile();

        return true;
    }

    /**
     * Handle the request
     *
     * On GET, show the form. On POST, try to save the group.
     *
     * @param array $args unused
     *
     * @return void
     */
    function handle($args)
    {
        parent::handle($args);
        if ($_SERVER['REQUEST_METHOD'] == 'POST') {
            $this->trySave();
        } else {
            $this->showForm();
        }
    }

    function showConfirm($msg=null, $fwd=null)
    {
        $this->confirm = $msg;
        $this->confirm_args = $fwd;
        $this->showPage();
    }

    function showConfirmForm()
    {
        $this->elementStart('form', array('id' => 'form_peopletag_edit_confirm',
                                          'class' => 'form_settings',
                                          'method' => 'post',
                                          'action' => common_local_url('editpeopletag',
                                              array('tagger' => $this->tagger->nickname,
                                                    'tag' => $this->peopletag->tag))));
        $this->elementStart('fieldset');
        $this->hidden('token', common_session_token());
        $this->hidden('id', $this->arg('id'));

        foreach ($this->confirm_args as $key => $val) {
            $this->hidden($key, $val);
        }

        $this->submit('form_action-no',
                      _m('BUTTON','No'),
                      'submit form_action-primary',
                      'cancel');
        $this->submit('form_action-yes',
                      _m('BUTTON','Yes'),
                      'submit form_action-secondary',
                      'confirm');
        $this->elementEnd('fieldset');
        $this->elementEnd('form');
    }

    function showForm($msg=null)
    {
        $this->msg = $msg;
        $this->showPage();
    }

191
    function showObjectNav()
Shashi Gowda's avatar
Shashi Gowda committed
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
    {
        $nav = new PeopletagGroupNav($this, $this->peopletag);
        $nav->show();
    }

    function showContent()
    {
        if ($this->confirm) {
            $this->showConfirmForm();
            return;
        }

        $form = new PeopletagEditForm($this, $this->peopletag);
        $form->show();

        $form->showProfileList();
    }

    function showPageNotice()
    {
        if ($this->msg) {
            $this->element('p', 'error', $this->msg);
        } else if ($this->confirm) {
            $this->element('p', 'instructions', $this->confirm);
        } else {
            $this->element('p', 'instructions',
218
                           // TRANS: Form instruction for edit list form.
219
                           _('Use this form to edit the list.'));
Shashi Gowda's avatar
Shashi Gowda committed
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
        }
    }

    function showScripts()
    {
        parent::showScripts();
        $this->autofocus('tag');
    }

    function trySave()
    {
        $tag         = common_canonical_tag($this->trimmed('tag'));
        $description = $this->trimmed('description');
        $private     = $this->boolean('private');
        $delete      = $this->arg('delete');
        $confirm     = $this->arg('confirm');
        $cancel      = $this->arg('cancel');

        if ($delete && $cancel) {
239
            // TRANS: Form validation error displayed if the form data for deleting a tag was incorrect.
Shashi Gowda's avatar
Shashi Gowda committed
240 241 242 243 244 245 246
            $this->showForm(_('Delete aborted.'));
            return;
        }

        $set_private = $private && $this->peopletag->private != $private;

        if ($delete && !$confirm) {
247
            // TRANS: Text in confirmation dialog for deleting a tag.
Shashi Gowda's avatar
Shashi Gowda committed
248 249 250 251 252
            $this->showConfirm(_('Deleting this tag will permanantly remove ' .
                                 'all its subscription and membership records. ' .
                                 'Do you still want to continue?'), array('delete' => 1));
            return;
        } else if (common_valid_tag($tag)) {
253
            // TRANS: Form validation error displayed if a given tag is invalid.
Shashi Gowda's avatar
Shashi Gowda committed
254 255 256
            $this->showForm(_('Invalid tag.'));
            return;
        } else if ($tag != $this->peopletag->tag && $this->tagExists($tag)) {
257 258
            // TRANS: Form validation error displayed if a given tag is already present.
            // TRANS: %s is the already present tag.
Shashi Gowda's avatar
Shashi Gowda committed
259 260 261
            $this->showForm(sprintf(_('You already have a tag named %s.'), $tag));
            return;
        } else if (Profile_list::descriptionTooLong($description)) {
262
            $this->showForm(sprintf(
263
                    // TRANS: Client error shown when providing too long a description when editing a list.
264 265 266 267 268
                    // TRANS: %d is the maximum number of allowed characters.
                    _m('Description is too long (maximum %d character).',
                      'Description is too long (maximum %d characters).',
                      Profile_list::maxDescription()),
                    Profile_list::maxDescription()));
Shashi Gowda's avatar
Shashi Gowda committed
269 270 271 272 273 274
            return;
        } else if ($set_private && !$confirm && !$cancel) {
            $fwd = array('tag' => $tag,
                         'description' => $description,
                         'private' => (int) $private);

275
            // TRANS: Text in confirmation dialog for setting a tag from public to private.
Shashi Gowda's avatar
Shashi Gowda committed
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
            $this->showConfirm(_('Setting a public tag as private will ' .
                                 'permanently remove all the existing ' .
                                 'subscriptions to it. Do you still want to continue?'), $fwd);
            return;
        }

        $this->peopletag->query('BEGIN');

        $orig = clone($this->peopletag);

        $this->peopletag->tag         = $tag;
        $this->peopletag->description = $description;
        if (!$set_private || $confirm) {
            $this->peopletag->private     = $private;
        }

        $result = $this->peopletag->update($orig);

        if (!$result) {
            common_log_db_error($this->group, 'UPDATE', __FILE__);
296
            // TRANS: Server error displayed when updating a list fails.
297
            $this->serverError(_('Could not update list.'));
Shashi Gowda's avatar
Shashi Gowda committed
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
        }

        $this->peopletag->query('COMMIT');

        if ($set_private && $confirm) {
            Profile_tag_subscription::cleanup($this->peopletag);
        }

        if ($delete) {
            // This might take quite a bit of time.
            $this->peopletag->delete();
            // send home.
            common_redirect(common_local_url('all',
                                         array('nickname' => $this->tagger->nickname)),
                            303);
        }

        if ($tag != $orig->tag) {
            common_redirect(common_local_url('editpeopletag',
                                             array('tagger' => $this->tagger->nickname,
                                                   'tag'    => $tag)),
                            303);
        } else {
321
            // TRANS: Edit list form success message.
Shashi Gowda's avatar
Shashi Gowda committed
322 323 324 325 326 327 328 329 330 331 332 333
            $this->showForm(_('Options saved.'));
        }
    }

    function tagExists($tag)
    {
        $args = array('tagger' => $this->tagger->id, 'tag' => $tag);
        $ptag = Profile_list::pkeyGet($args);

        return !empty($ptag);
    }
}