gnu-social/actions/editpeopletag.php

350 lines
11 KiB
PHP
Raw Normal View History

2011-03-06 19:13:31 +00:00
<?php
// This file is part of GNU social - https://www.gnu.org/software/social
//
// GNU social 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.
//
// GNU social 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 GNU social. If not, see <http://www.gnu.org/licenses/>.
2011-03-06 19:13:31 +00:00
/**
* Edit an existing group
*
* @category Group
* @package GNUsocial
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
2011-03-06 19:13:31 +00:00
*/
defined('GNUSOCIAL') || die();
2011-03-06 19:13:31 +00:00
/**
* Add a new group
*
* This is the form for adding a new group
*
* @category Group
* @package GNUsocial
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
2011-03-06 19:13:31 +00:00
*/
class EditpeopletagAction extends Action
2011-03-06 19:13:31 +00:00
{
public $msg;
public $confirm;
public $confirm_args = [];
2011-03-06 19:13:31 +00:00
public function title()
2011-03-06 19:13:31 +00:00
{
if ($_SERVER['REQUEST_METHOD'] == 'POST' && $this->boolean('delete')) {
2011-04-15 09:20:34 +01:00
// TRANS: Title for edit list page after deleting a tag.
// TRANS: %s is a list.
return sprintf(_('Delete %s list'), $this->peopletag->tag);
2011-03-06 19:13:31 +00:00
}
2011-04-15 09:20:34 +01:00
// TRANS: Title for edit list page.
// TRANS: %s is a list.
return sprintf(_('Edit list %s'), $this->peopletag->tag);
2011-03-06 19:13:31 +00:00
}
/**
* Prepare to run
*/
public function prepare(array $args = [])
2011-03-06 19:13:31 +00:00
{
parent::prepare($args);
if (!common_logged_in()) {
// TRANS: Error message displayed when trying to perform an action that requires a logged in user.
2011-03-06 19:13:31 +00:00
$this->clientError(_('Not logged in.'));
}
$id = $this->arg('id');
if (common_config('singleuser', 'enabled')) {
$tagger_arg = User::singleUserNickname();
} else {
$tagger_arg = $this->arg('tagger');
}
2011-03-06 19:13:31 +00:00
$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);
}
$user = null;
if ($id) {
$this->peopletag = Profile_list::getKV('id', $id);
2011-03-06 19:13:31 +00:00
if (!empty($this->peopletag)) {
$user = User::getKV('id', $this->peopletag->tagger);
2011-03-06 19:13:31 +00:00
}
} else {
if (!$tagger) {
// TRANS: Error message displayed when trying to perform an action that requires a tagging user or ID.
2011-03-06 19:13:31 +00:00
$this->clientError(_('No tagger or ID.'), 404);
}
$user = User::getKV('nickname', $tagger);
2011-03-06 19:13:31 +00:00
$this->peopletag = Profile_list::pkeyGet(array('tagger' => $user->id, 'tag' => $tag));
}
if (!$this->peopletag) {
2011-04-16 23:53:29 +01:00
// TRANS: Client error displayed when referring to a non-existing list.
$this->clientError(_('No such list.'), 404);
2011-03-06 19:13:31 +00:00
}
if (!$user) {
// This should not be happening
// TRANS: Client error displayed when referring to non-local user.
2011-03-06 19:13:31 +00:00
$this->clientError(_('Not a local user.'), 404);
}
if ($current->id != $user->id) {
// TRANS: Client error displayed when reting to edit a tag that was not self-created.
2011-03-06 19:13:31 +00:00
$this->clientError(_('You must be the creator of the tag to edit it.'), 404);
}
$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
*/
public function handle()
2011-03-06 19:13:31 +00:00
{
parent::handle();
2011-03-06 19:13:31 +00:00
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$this->trySave();
} else {
$this->showForm();
}
}
public function showConfirm($msg = null, $fwd = null)
2011-03-06 19:13:31 +00:00
{
$this->confirm = $msg;
$this->confirm_args = $fwd;
$this->showPage();
}
public function showConfirmForm()
2011-03-06 19:13:31 +00:00
{
$this->elementStart(
'form',
[
'id' => 'form_peopletag_edit_confirm',
'class' => 'form_settings',
'method' => 'post',
'action' => common_local_url(
'editpeopletag',
[
'tagger' => $this->tagger->nickname,
'tag' => $this->peopletag->tag,
]
),
]
);
2011-03-06 19:13:31 +00:00
$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'
);
2011-03-06 19:13:31 +00:00
$this->elementEnd('fieldset');
$this->elementEnd('form');
}
public function showForm($msg = null)
2011-03-06 19:13:31 +00:00
{
$this->msg = $msg;
$this->showPage();
}
public function showObjectNav()
2011-03-06 19:13:31 +00:00
{
$nav = new PeopletagGroupNav($this, $this->peopletag);
$nav->show();
}
public function showContent()
2011-03-06 19:13:31 +00:00
{
if ($this->confirm) {
$this->showConfirmForm();
return;
}
$form = new PeopletagEditForm($this, $this->peopletag);
$form->show();
$form->showProfileList();
}
public function showPageNotice()
2011-03-06 19:13:31 +00:00
{
if ($this->msg) {
$this->element('p', 'error', $this->msg);
} elseif ($this->confirm) {
2011-03-06 19:13:31 +00:00
$this->element('p', 'instructions', $this->confirm);
} else {
$this->element(
'p',
'instructions',
// TRANS: Form instruction for edit list form.
_('Use this form to edit the list.')
);
2011-03-06 19:13:31 +00:00
}
}
public function showScripts()
2011-03-06 19:13:31 +00:00
{
parent::showScripts();
$this->autofocus('tag');
}
public function trySave()
2011-03-06 19:13:31 +00:00
{
$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) {
// TRANS: Form validation error displayed if the form data for deleting a tag was incorrect.
2011-03-06 19:13:31 +00:00
$this->showForm(_('Delete aborted.'));
return;
}
$set_private = $private && $this->peopletag->private != $private;
if ($delete && !$confirm) {
// TRANS: Text in confirmation dialog for deleting a tag.
2011-03-06 19:13:31 +00:00
$this->showConfirm(_('Deleting this tag will permanantly remove ' .
'all its subscription and membership records. ' .
'Do you still want to continue?'), array('delete' => 1));
return;
} elseif (common_valid_tag($tag)) {
// TRANS: Form validation error displayed if a given tag is invalid.
2011-03-06 19:13:31 +00:00
$this->showForm(_('Invalid tag.'));
return;
} elseif ($tag != $this->peopletag->tag && $this->tagExists($tag)) {
// TRANS: Form validation error displayed if a given tag is already present.
// TRANS: %s is the already present tag.
2011-03-06 19:13:31 +00:00
$this->showForm(sprintf(_('You already have a tag named %s.'), $tag));
return;
} elseif (Profile_list::descriptionTooLong($description)) {
$this->showForm(sprintf(
// TRANS: Client error shown when providing too long a description when editing a list.
// 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()
));
2011-03-06 19:13:31 +00:00
return;
} elseif ($set_private && !$confirm && !$cancel) {
2011-03-06 19:13:31 +00:00
$fwd = array('tag' => $tag,
'description' => $description,
'private' => (int) $private);
// TRANS: Text in confirmation dialog for setting a tag from public to private.
2011-03-06 19:13:31 +00:00
$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;
}
// We tested $delete && !$confirm earlier so confirmation is required before getting here
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->getNickname())), 303);
}
$this->peopletag->query('START TRANSACTION');
2011-03-06 19:13:31 +00:00
$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__);
2011-06-19 10:15:31 +01:00
// TRANS: Server error displayed when updating a list fails.
$this->serverError(_('Could not update list.'));
2011-03-06 19:13:31 +00:00
}
$this->peopletag->query('COMMIT');
if ($set_private && $confirm) {
Profile_tag_subscription::cleanup($this->peopletag);
}
if ($tag != $orig->tag) {
common_redirect(
common_local_url(
'editpeopletag',
[
'tagger' => $this->tagger->nickname,
'tag' => $tag,
]
),
303
);
2011-03-06 19:13:31 +00:00
} else {
// TRANS: Edit list form success message.
2011-03-06 19:13:31 +00:00
$this->showForm(_('Options saved.'));
}
}
public function tagExists($tag)
2011-03-06 19:13:31 +00:00
{
$args = array('tagger' => $this->tagger->id, 'tag' => $tag);
$ptag = Profile_list::pkeyGet($args);
return !empty($ptag);
}
}