Make group edit and logo great again by XRevan86

This commit is contained in:
Diogo Cordeiro 2019-04-27 17:39:42 +01:00
parent f6dbf66983
commit d75b5d2f4a
2 changed files with 400 additions and 376 deletions

View File

@ -29,7 +29,7 @@
* @link http://status.net/
*/
if (!defined('STATUSNET') && !defined('LACONICA')) {
if (!defined('STATUSNET') && !defined('LACONICA') && !defined('GNUSOCIAL')) {
exit(1);
}
@ -42,24 +42,63 @@ if (!defined('STATUSNET') && !defined('LACONICA')) {
* @package StatusNet
* @author Evan Prodromou <evan@status.net>
* @author Zach Copley <zach@status.net>
* @author Alexei Sorokin <sor.alexei@meowr.ru>
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
* @link http://status.net/
*/
class EditgroupAction extends GroupAction
{
var $msg;
public $message = null;
public $success = null;
protected $canPost = true;
function title()
public function title()
{
// TRANS: Title for form to edit a group. %s is a group nickname.
return sprintf(_('Edit %s group'), $this->group->nickname);
}
public function showContent()
{
$form = new GroupEditForm($this, $this->group);
$form->show();
}
public function showPageNoticeBlock()
{
parent::showPageNoticeBlock();
if ($this->message) {
$this->element(
'p',
($this->success) ? 'success' : 'error',
$this->message
);
} else {
$this->element(
'p',
'instructions',
// TRANS: Form instructions for group edit form.
_('Use this form to edit the group.')
);
}
}
public function showScripts()
{
parent::showScripts();
$this->autofocus('fullname');
}
/**
* Prepare to run
* @param array $args
* @return bool
* @throws ClientException
* @throws NicknameException
*/
protected function prepare(array $args=array())
protected function prepare(array $args = [])
{
parent::prepare($args);
@ -74,7 +113,7 @@ class EditgroupAction extends GroupAction
// Permanent redirect on non-canonical nickname
if ($nickname_arg != $nickname) {
$args = array('nickname' => $nickname);
$args = ['nickname' => $nickname];
common_redirect(common_local_url('editgroup', $args), 301);
}
@ -109,75 +148,32 @@ class EditgroupAction extends GroupAction
return true;
}
/**
* Handle the request
*
* On GET, show the form. On POST, try to save the group.
*
* @return void
*/
protected function handle()
protected function handlePost()
{
parent::handle();
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$this->trySave();
} else {
$this->showForm();
}
}
parent::handlePost();
function showForm($msg=null)
{
$this->msg = $msg;
$this->showPage();
}
function showContent()
{
$form = new GroupEditForm($this, $this->group);
$form->show();
}
function showPageNotice()
{
if ($this->msg) {
$this->element('p', 'error', $this->msg);
} else {
$this->element('p', 'instructions',
// TRANS: Form instructions for group edit form.
_('Use this form to edit the group.'));
}
}
function showScripts()
{
parent::showScripts();
$this->autofocus('fullname');
}
function trySave()
{
$cur = common_current_user();
if (!$cur->isAdmin($this->group)) {
// TRANS: Client error displayed trying to edit a group while not being a group admin.
$this->clientError(_('You must be an admin to edit the group.'), 403);
}
if (Event::handle('StartGroupSaveForm', array($this))) {
if (Event::handle('StartGroupSaveForm', [$this])) {
// $nickname will only be set if this changenick value is true.
$nickname = null;
if (common_config('profile', 'changenick') == true) {
try {
$nickname = Nickname::normalize($this->trimmed('newnickname'), true);
} catch (NicknameTakenException $e) {
// Abort only if the nickname is occupied by _another_ group
if ($e->profile->id != $this->group->profile_id) {
$this->showForm($e->getMessage());
$this->setMessage($e->getMessage(), true);
return;
}
$nickname = Nickname::normalize($this->trimmed('newnickname')); // without in-use check this time
} catch (NicknameException $e) {
$this->showForm($e->getMessage());
$this->setMessage($e->getMessage(), true);
return;
}
}
@ -200,40 +196,49 @@ class EditgroupAction extends GroupAction
if (!is_null($homepage) && (strlen($homepage) > 0) &&
!common_valid_http_url($homepage)) {
// TRANS: Group edit form validation error.
$this->showForm(_('Homepage is not a valid URL.'));
$this->setMessage(_('Homepage is not a valid URL.'), true);
return;
} elseif (!is_null($fullname) && mb_strlen($fullname) > 255) {
// TRANS: Group edit form validation error.
$this->showForm(_('Full name is too long (maximum 255 characters).'));
$this->setMessage(_('Full name is too long (maximum 255 characters).'), true);
return;
} elseif (User_group::descriptionTooLong($description)) {
$this->showForm(sprintf(
$this->setMessage(sprintf(
// TRANS: Group edit form validation error.
_m('Description is too long (maximum %d character).',
_m(
'Description is too long (maximum %d character).',
'Description is too long (maximum %d characters).',
User_group::maxDescription()),
User_group::maxDescription()));
User_group::maxDescription()
),
User_group::maxDescription()
), true);
return;
} elseif (!is_null($location) && mb_strlen($location) > 255) {
// TRANS: Group edit form validation error.
$this->showForm(_('Location is too long (maximum 255 characters).'));
$this->setMessage(_('Location is too long (maximum 255 characters).'), true);
return;
}
if (!empty($aliasstring)) {
$aliases = array_map(array('Nickname', 'normalize'),
array_unique(preg_split('/[\s,]+/', $aliasstring)));
$aliases = array_map(
['Nickname', 'normalize'],
array_unique(preg_split('/[\s,]+/', $aliasstring))
);
} else {
$aliases = array();
$aliases = [];
}
if (count($aliases) > common_config('group', 'maxaliases')) {
// TRANS: Group edit form validation error.
// TRANS: %d is the maximum number of allowed aliases.
$this->showForm(sprintf(_m('Too many aliases! Maximum %d allowed.',
$this->setMessage(sprintf(
_m(
'Too many aliases! Maximum %d allowed.',
common_config('group', 'maxaliases')),
common_config('group', 'maxaliases')));
'Too many aliases! Maximum %d allowed.',
common_config('group', 'maxaliases')
),
common_config('group', 'maxaliases')
), true);
return;
}
@ -243,9 +248,9 @@ class EditgroupAction extends GroupAction
if (common_config('profile', 'changenick') == true && $this->group->nickname !== $nickname) {
assert(Nickname::normalize($nickname) === $nickname);
common_debug("Changing group nickname from '{$profile->nickname}' to '{$nickname}'.");
common_debug("Changing group nickname from '{$this->group->nickname}' to '{$nickname}'.");
$this->group->nickname = $nickname;
$this->group->mainpage = common_local_url('showgroup', array('nickname' => $this->group->nickname));
$this->group->mainpage = common_local_url('showgroup', ['nickname' => $this->group->nickname]);
}
$this->group->fullname = $fullname;
$this->group->homepage = $homepage;
@ -271,14 +276,20 @@ class EditgroupAction extends GroupAction
$this->group->query('COMMIT');
Event::handle('EndGroupSaveForm', array($this));
}
Event::handle('EndGroupSaveForm', [$this]);
if ($this->group->nickname != $orig->nickname) {
common_redirect(common_local_url('editgroup', array('nickname' => $this->group->nickname)), 303);
} else {
common_redirect(common_local_url('editgroup', ['nickname' => $this->group->nickname]), 303);
}
}
// TRANS: Group edit form success message.
$this->showForm(_('Options saved.'));
}
$this->setMessage(_('Options saved.'));
}
public function setMessage($msg, $error = false)
{
$this->message = $msg;
$this->success = !$error;
}
}

View File

@ -28,7 +28,9 @@
* @link http://status.net/
*/
if (!defined('GNUSOCIAL')) { exit(1); }
if (!defined('GNUSOCIAL')) {
exit(1);
}
/**
* Upload an avatar
@ -40,111 +42,30 @@ if (!defined('GNUSOCIAL')) { exit(1); }
* @author Evan Prodromou <evan@status.net>
* @author Zach Copley <zach@status.net>
* @author Sarven Capadisli <csarven@status.net>
* @author Alexei Sorokin <sor.alexei@meowr.ru>
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
* @link http://status.net/
*/
class GrouplogoAction extends GroupAction
{
var $mode = null;
var $imagefile = null;
var $filename = null;
var $msg = null;
var $success = null;
/**
* Prepare to run
*/
protected function prepare(array $args=array())
{
parent::prepare($args);
if (!common_logged_in()) {
// TRANS: Client error displayed when trying to create a group while not logged in.
$this->clientError(_('You must be logged in to create a group.'));
}
$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);
common_redirect(common_local_url('grouplogo', $args), 301);
}
if (!$nickname) {
// TRANS: Client error displayed when trying to change group logo settings without providing a nickname.
$this->clientError(_('No nickname.'), 404);
}
$groupid = $this->trimmed('groupid');
if ($groupid) {
$this->group = User_group::getKV('id', $groupid);
} else {
$local = Local_group::getKV('nickname', $nickname);
if ($local) {
$this->group = User_group::getKV('id', $local->group_id);
}
}
if (!$this->group) {
// TRANS: Client error displayed when trying to update logo settings for a non-existing group.
$this->clientError(_('No such group.'), 404);
}
$cur = common_current_user();
if (!$cur->isAdmin($this->group)) {
// TRANS: Client error displayed when trying to change group logo settings while not being a group admin.
$this->clientError(_('You must be an admin to edit the group.'), 403);
}
return true;
}
protected function handle()
{
parent::handle();
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$this->handlePost();
} else {
$this->showForm();
}
}
function showForm($msg = null, $success = false)
{
$this->msg = $msg;
$this->success = $success;
$this->showPage();
}
public $mode = null;
public $imagefile = null;
public $filename = null;
public $message = null;
public $success = null;
protected $canPost = true;
/**
* Title of the page
*
* @return string Title of the page
*/
function title()
public function title()
{
// TRANS: Title for group logo settings page.
return _('Group logo');
}
/**
* Instructions for use
*
* @return instructions for use
*/
function getInstructions()
{
// TRANS: Instructions for group logo page.
// TRANS: %s is the maximum file size for that site.
return sprintf(_('You can upload a logo image for your group. The maximum file size is %s.'), ImageFile::maxFileSize());
}
/**
* Content area of the page
*
@ -152,7 +73,7 @@ class GrouplogoAction extends GroupAction
*
* @return void
*/
function showContent()
public function showContent()
{
if ($this->mode == 'crop') {
$this->showCropForm();
@ -161,7 +82,69 @@ class GrouplogoAction extends GroupAction
}
}
function showUploadForm()
public function showCropForm()
{
$this->elementStart('form', array('method' => 'post',
'id' => 'form_settings_avatar',
'class' => 'form_settings',
'action' =>
common_local_url(
'grouplogo',
array('nickname' => $this->group->nickname)
)));
$this->elementStart('fieldset');
// TRANS: Legend for group logo settings fieldset.
$this->element('legend', null, _('Avatar settings'));
$this->hidden('token', common_session_token());
$this->elementStart('ul', 'form_data');
$this->elementStart(
'li',
array('id' => 'avatar_original',
'class' => 'avatar_view')
);
// TRANS: Header for originally uploaded file before a crop on the group logo page.
$this->element('h2', null, _('Original'));
$this->elementStart('div', array('id' => 'avatar_original_view'));
$this->element('img', array('src' => Avatar::url($this->filedata['filename']),
'width' => $this->filedata['width'],
'height' => $this->filedata['height'],
'alt' => $this->group->nickname));
$this->elementEnd('div');
$this->elementEnd('li');
$this->elementStart(
'li',
array('id' => 'avatar_preview',
'class' => 'avatar_view')
);
// TRANS: Header for the cropped group logo on the group logo page.
$this->element('h2', null, _('Preview'));
$this->elementStart('div', array('id' => 'avatar_preview_view'));
$this->element('img', array('src' => Avatar::url($this->filedata['filename']),
'width' => AVATAR_PROFILE_SIZE,
'height' => AVATAR_PROFILE_SIZE,
'alt' => $this->group->nickname));
$this->elementEnd('div');
foreach (array('avatar_crop_x', 'avatar_crop_y',
'avatar_crop_w', 'avatar_crop_h') as $crop_info) {
$this->element('input', array('name' => $crop_info,
'type' => 'hidden',
'id' => $crop_info));
}
// TRANS: Button text for cropping an uploaded group logo.
$this->submit('crop', _('Crop'));
$this->elementEnd('li');
$this->elementEnd('ul');
$this->elementEnd('fieldset');
$this->elementEnd('form');
}
public function showUploadForm()
{
$user = common_current_user();
@ -180,8 +163,10 @@ class GrouplogoAction extends GroupAction
'id' => 'form_settings_avatar',
'class' => 'form_settings',
'action' =>
common_local_url('grouplogo',
array('nickname' => $this->group->nickname))));
common_local_url(
'grouplogo',
array('nickname' => $this->group->nickname)
)));
$this->elementStart('fieldset');
// TRANS: Group logo form legend.
$this->element('legend', null, _('Group logo'));
@ -240,60 +225,121 @@ class GrouplogoAction extends GroupAction
$this->elementEnd('form');
}
function showCropForm()
public function showPageNoticeBlock()
{
$this->elementStart('form', array('method' => 'post',
'id' => 'form_settings_avatar',
'class' => 'form_settings',
'action' =>
common_local_url('grouplogo',
array('nickname' => $this->group->nickname))));
$this->elementStart('fieldset');
// TRANS: Legend for group logo settings fieldset.
$this->element('legend', null, _('Avatar settings'));
$this->hidden('token', common_session_token());
parent::showPageNoticeBlock();
$this->elementStart('ul', 'form_data');
if ($this->message) {
$this->element(
'div',
($this->success) ? 'success' : 'error',
$this->message
);
} else {
$inst = $this->getInstructions();
$output = common_markup_to_html($inst);
$this->elementStart('li',
array('id' => 'avatar_original',
'class' => 'avatar_view'));
// TRANS: Header for originally uploaded file before a crop on the group logo page.
$this->element('h2', null, _('Original'));
$this->elementStart('div', array('id'=>'avatar_original_view'));
$this->element('img', array('src' => Avatar::url($this->filedata['filename']),
'width' => $this->filedata['width'],
'height' => $this->filedata['height'],
'alt' => $this->group->nickname));
$this->elementStart('div', 'instructions');
$this->raw($output);
$this->elementEnd('div');
$this->elementEnd('li');
$this->elementStart('li',
array('id' => 'avatar_preview',
'class' => 'avatar_view'));
// TRANS: Header for the cropped group logo on the group logo page.
$this->element('h2', null, _('Preview'));
$this->elementStart('div', array('id'=>'avatar_preview_view'));
$this->element('img', array('src' => Avatar::url($this->filedata['filename']),
'width' => AVATAR_PROFILE_SIZE,
'height' => AVATAR_PROFILE_SIZE,
'alt' => $this->group->nickname));
$this->elementEnd('div');
foreach (array('avatar_crop_x', 'avatar_crop_y',
'avatar_crop_w', 'avatar_crop_h') as $crop_info) {
$this->element('input', array('name' => $crop_info,
'type' => 'hidden',
'id' => $crop_info));
}
}
// TRANS: Button text for cropping an uploaded group logo.
$this->submit('crop', _('Crop'));
/**
* Instructions for use
*
* @return string instructions for use
*/
public function getInstructions()
{
// TRANS: Instructions for group logo page.
// TRANS: %s is the maximum file size for that site.
return sprintf(_('You can upload a logo image for your group. The maximum file size is %s.'), ImageFile::maxFileSize());
}
$this->elementEnd('li');
$this->elementEnd('ul');
$this->elementEnd('fieldset');
$this->elementEnd('form');
/**
* Add the jCrop stylesheet
*
* @return void
*/
public function showStylesheets()
{
parent::showStylesheets();
$this->cssLink('js/extlib/jquery-jcrop/css/jcrop.css', 'base', 'screen, projection, tv');
}
/**
* Add the jCrop scripts
*
* @return void
*/
public function showScripts()
{
parent::showScripts();
if ($this->mode == 'crop') {
$this->script('extlib/jquery-jcrop/jcrop.js');
$this->script('jcrop.go.js');
}
$this->autofocus('avatarfile');
}
/**
* Prepare to run
* @param array $args
* @return bool
* @throws ClientException
* @throws NicknameException
*/
protected function prepare(array $args = [])
{
parent::prepare($args);
if (!common_logged_in()) {
// TRANS: Client error displayed when trying to create a group while not logged in.
$this->clientError(_('You must be logged in to create a group.'));
}
$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);
common_redirect(common_local_url('grouplogo', $args), 301);
}
if (!$nickname) {
// TRANS: Client error displayed when trying to change group logo settings without providing a nickname.
$this->clientError(_('No nickname.'), 404);
}
$groupid = $this->trimmed('groupid');
if ($groupid) {
$this->group = User_group::getKV('id', $groupid);
} else {
$local = Local_group::getKV('nickname', $nickname);
if ($local) {
$this->group = User_group::getKV('id', $local->group_id);
}
}
if (!$this->group) {
// TRANS: Client error displayed when trying to update logo settings for a non-existing group.
$this->clientError(_('No such group.'), 404);
}
$cur = common_current_user();
if (!$cur->isAdmin($this->group)) {
// TRANS: Client error displayed when trying to change group logo settings while not being a group admin.
$this->clientError(_('You must be an admin to edit the group.'), 403);
}
return true;
}
/**
@ -302,18 +348,14 @@ class GrouplogoAction extends GroupAction
* We mux on the button name to figure out what the user actually wanted.
*
* @return void
* @throws ClientException
* @throws NoResultException
* @throws UnsupportedMediaException
* @throws UseFileAsThumbnailException
*/
function handlePost()
protected function handlePost()
{
// CSRF protection
$token = $this->trimmed('token');
if (!$token || $token != common_session_token()) {
// TRANS: Form validation error message.
$this->show_form(_('There was a problem with your session token. '.
'Try again, please.'));
return;
}
parent::handlePost();
if ($this->arg('upload')) {
$this->uploadLogo();
@ -323,7 +365,7 @@ class GrouplogoAction extends GroupAction
$this->deleteLogo();
} else {
// TRANS: Form validation error message when an unsupported argument is used.
$this->showForm(_('Unexpected form submission.'));
$this->setMessage(_('Unexpected form submission.'), true);
}
}
@ -335,20 +377,22 @@ class GrouplogoAction extends GroupAction
*
* @return void
*/
function uploadLogo()
public function uploadLogo()
{
try {
$imagefile = ImageFile::fromUpload('avatarfile');
} catch (Exception $e) {
$this->showForm($e->getMessage());
$this->setMessage($e->getMessage(), true);
return;
}
$type = $imagefile->preferredType();
$filename = Avatar::filename($this->group->id,
$filename = Avatar::filename(
$this->group->id,
image_type_to_extension($type),
null,
'group-temp-'.common_timestamp());
'group-temp-' . common_timestamp()
);
$filepath = Avatar::path($filename);
@ -367,16 +411,24 @@ class GrouplogoAction extends GroupAction
$this->mode = 'crop';
// TRANS: Form instructions on the group logo page.
$this->showForm(_('Pick a square area of the image to be the logo.'),
true);
$this->setMessage(_('Pick a square area of the image to be the logo.'));
}
public function setMessage($msg, $error = false)
{
$this->message = $msg;
$this->success = !$error;
}
/**
* Handle the results of jcrop.
*
* @return void
* @throws NoResultException
* @throws UnsupportedMediaException
* @throws UseFileAsThumbnailException
*/
function cropLogo()
public function cropLogo()
{
$filedata = $_SESSION['FILEDATA'];
@ -398,8 +450,12 @@ class GrouplogoAction extends GroupAction
$profile = $this->group->getProfile();
$imagefile = new ImageFile(null, $filedata['filepath']);
$filename = Avatar::filename($profile->getID(), image_type_to_extension($imagefile->preferredType()),
$size, common_timestamp());
$filename = Avatar::filename(
$profile->getID(),
image_type_to_extension($imagefile->preferredType()),
$size,
common_timestamp()
);
$imagefile->resizeTo(Avatar::path($filename), $box);
@ -408,10 +464,10 @@ class GrouplogoAction extends GroupAction
unset($_SESSION['FILEDATA']);
$this->mode = 'upload';
// TRANS: Form success message after updating a group logo.
$this->showForm(_('Logo updated.'), true);
$this->setMessage(_('Logo updated.'));
} else {
// TRANS: Form failure message after failing to update a group logo.
$this->showForm(_('Failed updating logo.'));
$this->setMessage(_('Failed updating logo.'), true);
}
}
@ -420,7 +476,7 @@ class GrouplogoAction extends GroupAction
*
* @return void
*/
function deleteLogo()
public function deleteLogo()
{
$orig = clone($this->group);
Avatar::deleteFromProfile($this->group->getProfile());
@ -435,49 +491,6 @@ class GrouplogoAction extends GroupAction
$this->group->update($orig);
// TRANS: Success message for deleting the group logo.
$this->showForm(_('Logo deleted.'));
}
function showPageNotice()
{
if ($this->msg) {
$this->element('div', ($this->success) ? 'success' : 'error',
$this->msg);
} else {
$inst = $this->getInstructions();
$output = common_markup_to_html($inst);
$this->elementStart('div', 'instructions');
$this->raw($output);
$this->elementEnd('div');
}
}
/**
* Add the jCrop stylesheet
*
* @return void
*/
function showStylesheets()
{
parent::showStylesheets();
$this->cssLink('js/extlib/jquery-jcrop/css/jcrop.css','base','screen, projection, tv');
}
/**
* Add the jCrop scripts
*
* @return void
*/
function showScripts()
{
parent::showScripts();
if ($this->mode == 'crop') {
$this->script('extlib/jquery-jcrop/jcrop.js');
$this->script('jcrop.go.js');
}
$this->autofocus('avatarfile');
$this->setMessage(_('Logo deleted.'));
}
}