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/ * @link http://status.net/
*/ */
if (!defined('STATUSNET') && !defined('LACONICA')) { if (!defined('STATUSNET') && !defined('LACONICA') && !defined('GNUSOCIAL')) {
exit(1); exit(1);
} }
@ -42,24 +42,63 @@ if (!defined('STATUSNET') && !defined('LACONICA')) {
* @package StatusNet * @package StatusNet
* @author Evan Prodromou <evan@status.net> * @author Evan Prodromou <evan@status.net>
* @author Zach Copley <zach@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 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
* @link http://status.net/ * @link http://status.net/
*/ */
class EditgroupAction extends GroupAction 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. // TRANS: Title for form to edit a group. %s is a group nickname.
return sprintf(_('Edit %s group'), $this->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 * 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); parent::prepare($args);
@ -74,7 +113,7 @@ class EditgroupAction extends GroupAction
// Permanent redirect on non-canonical nickname // Permanent redirect on non-canonical nickname
if ($nickname_arg != $nickname) { if ($nickname_arg != $nickname) {
$args = array('nickname' => $nickname); $args = ['nickname' => $nickname];
common_redirect(common_local_url('editgroup', $args), 301); common_redirect(common_local_url('editgroup', $args), 301);
} }
@ -109,85 +148,42 @@ class EditgroupAction extends GroupAction
return true; return true;
} }
/** protected function handlePost()
* Handle the request
*
* On GET, show the form. On POST, try to save the group.
*
* @return void
*/
protected function handle()
{ {
parent::handle(); parent::handlePost();
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$this->trySave();
} else {
$this->showForm();
}
}
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(); $cur = common_current_user();
if (!$cur->isAdmin($this->group)) { if (!$cur->isAdmin($this->group)) {
// TRANS: Client error displayed trying to edit a group while not being a group admin. // 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); $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 will only be set if this changenick value is true.
$nickname = null;
if (common_config('profile', 'changenick') == true) { if (common_config('profile', 'changenick') == true) {
try { try {
$nickname = Nickname::normalize($this->trimmed('newnickname'), true); $nickname = Nickname::normalize($this->trimmed('newnickname'), true);
} catch (NicknameTakenException $e) { } catch (NicknameTakenException $e) {
// Abort only if the nickname is occupied by _another_ group // Abort only if the nickname is occupied by _another_ group
if ($e->profile->id != $this->group->profile_id) { if ($e->profile->id != $this->group->profile_id) {
$this->showForm($e->getMessage()); $this->setMessage($e->getMessage(), true);
return; return;
} }
$nickname = Nickname::normalize($this->trimmed('newnickname')); // without in-use check this time $nickname = Nickname::normalize($this->trimmed('newnickname')); // without in-use check this time
} catch (NicknameException $e) { } catch (NicknameException $e) {
$this->showForm($e->getMessage()); $this->setMessage($e->getMessage(), true);
return; return;
} }
} }
$fullname = $this->trimmed('fullname'); $fullname = $this->trimmed('fullname');
$homepage = $this->trimmed('homepage'); $homepage = $this->trimmed('homepage');
$description = $this->trimmed('description'); $description = $this->trimmed('description');
$location = $this->trimmed('location'); $location = $this->trimmed('location');
$aliasstring = $this->trimmed('aliases'); $aliasstring = $this->trimmed('aliases');
$private = $this->boolean('private'); $private = $this->boolean('private');
if ($private) { if ($private) {
$force_scope = 1; $force_scope = 1;
@ -198,42 +194,51 @@ class EditgroupAction extends GroupAction
} }
if (!is_null($homepage) && (strlen($homepage) > 0) && if (!is_null($homepage) && (strlen($homepage) > 0) &&
!common_valid_http_url($homepage)) { !common_valid_http_url($homepage)) {
// TRANS: Group edit form validation error. // TRANS: Group edit form validation error.
$this->showForm(_('Homepage is not a valid URL.')); $this->setMessage(_('Homepage is not a valid URL.'), true);
return; return;
} else if (!is_null($fullname) && mb_strlen($fullname) > 255) { } elseif (!is_null($fullname) && mb_strlen($fullname) > 255) {
// TRANS: Group edit form validation error. // 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; return;
} else if (User_group::descriptionTooLong($description)) { } 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).',
'Description is too long (maximum %d characters).',
User_group::maxDescription()),
User_group::maxDescription()));
return;
} else if (!is_null($location) && mb_strlen($location) > 255) {
// TRANS: Group edit form validation error. // TRANS: Group edit form validation error.
$this->showForm(_('Location is too long (maximum 255 characters).')); _m(
'Description is too long (maximum %d character).',
'Description is too long (maximum %d characters).',
User_group::maxDescription()
),
User_group::maxDescription()
), true);
return;
} elseif (!is_null($location) && mb_strlen($location) > 255) {
// TRANS: Group edit form validation error.
$this->setMessage(_('Location is too long (maximum 255 characters).'), true);
return; return;
} }
if (!empty($aliasstring)) { if (!empty($aliasstring)) {
$aliases = array_map(array('Nickname', 'normalize'), $aliases = array_map(
array_unique(preg_split('/[\s,]+/', $aliasstring))); ['Nickname', 'normalize'],
array_unique(preg_split('/[\s,]+/', $aliasstring))
);
} else { } else {
$aliases = array(); $aliases = [];
} }
if (count($aliases) > common_config('group', 'maxaliases')) { if (count($aliases) > common_config('group', 'maxaliases')) {
// TRANS: Group edit form validation error. // TRANS: Group edit form validation error.
// TRANS: %d is the maximum number of allowed aliases. // TRANS: %d is the maximum number of allowed aliases.
$this->showForm(sprintf(_m('Too many aliases! Maximum %d allowed.', $this->setMessage(sprintf(
'Too many aliases! Maximum %d allowed.', _m(
common_config('group', 'maxaliases')), 'Too many aliases! Maximum %d allowed.',
common_config('group', 'maxaliases'))); 'Too many aliases! Maximum %d allowed.',
common_config('group', 'maxaliases')
),
common_config('group', 'maxaliases')
), true);
return; return;
} }
@ -242,15 +247,15 @@ class EditgroupAction extends GroupAction
$orig = clone($this->group); $orig = clone($this->group);
if (common_config('profile', 'changenick') == true && $this->group->nickname !== $nickname) { if (common_config('profile', 'changenick') == true && $this->group->nickname !== $nickname) {
assert(Nickname::normalize($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->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->fullname = $fullname;
$this->group->homepage = $homepage; $this->group->homepage = $homepage;
$this->group->description = $description; $this->group->description = $description;
$this->group->location = $location; $this->group->location = $location;
$this->group->join_policy = $join_policy; $this->group->join_policy = $join_policy;
$this->group->force_scope = $force_scope; $this->group->force_scope = $force_scope;
@ -271,14 +276,20 @@ class EditgroupAction extends GroupAction
$this->group->query('COMMIT'); $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', ['nickname' => $this->group->nickname]), 303);
}
} }
if ($this->group->nickname != $orig->nickname) { // TRANS: Group edit form success message.
common_redirect(common_local_url('editgroup', array('nickname' => $this->group->nickname)), 303); $this->setMessage(_('Options saved.'));
} else { }
// TRANS: Group edit form success message.
$this->showForm(_('Options saved.')); public function setMessage($msg, $error = false)
} {
$this->message = $msg;
$this->success = !$error;
} }
} }

View File

@ -28,7 +28,9 @@
* @link http://status.net/ * @link http://status.net/
*/ */
if (!defined('GNUSOCIAL')) { exit(1); } if (!defined('GNUSOCIAL')) {
exit(1);
}
/** /**
* Upload an avatar * Upload an avatar
@ -40,21 +42,257 @@ if (!defined('GNUSOCIAL')) { exit(1); }
* @author Evan Prodromou <evan@status.net> * @author Evan Prodromou <evan@status.net>
* @author Zach Copley <zach@status.net> * @author Zach Copley <zach@status.net>
* @author Sarven Capadisli <csarven@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 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
* @link http://status.net/ * @link http://status.net/
*/ */
class GrouplogoAction extends GroupAction class GrouplogoAction extends GroupAction
{ {
var $mode = null; public $mode = null;
var $imagefile = null; public $imagefile = null;
var $filename = null; public $filename = null;
var $msg = null; public $message = null;
var $success = null; public $success = null;
protected $canPost = true;
/**
* Title of the page
*
* @return string Title of the page
*/
public function title()
{
// TRANS: Title for group logo settings page.
return _('Group logo');
}
/**
* Content area of the page
*
* Shows a form for uploading an avatar.
*
* @return void
*/
public function showContent()
{
if ($this->mode == 'crop') {
$this->showCropForm();
} else {
$this->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();
$profile = $user->getProfile();
if (!$profile) {
common_log_db_error($user, 'SELECT', __FILE__);
// TRANS: Error message displayed when referring to a user without a profile.
$this->serverError(_('User has no profile.'));
}
$original = $this->group->original_logo;
$this->elementStart('form', array('enctype' => 'multipart/form-data',
'method' => 'post',
'id' => 'form_settings_avatar',
'class' => 'form_settings',
'action' =>
common_local_url(
'grouplogo',
array('nickname' => $this->group->nickname)
)));
$this->elementStart('fieldset');
// TRANS: Group logo form legend.
$this->element('legend', null, _('Group logo'));
$this->hidden('token', common_session_token());
$this->elementStart('ul', 'form_data');
if ($original) {
$this->elementStart('li', array('id' => 'avatar_original',
'class' => 'avatar_view'));
// TRANS: Uploaded original file in group logo form.
$this->element('h2', null, _('Original'));
$this->elementStart('div', array('id' => 'avatar_original_view'));
$this->element('img', array('src' => $this->group->original_logo,
'alt' => $this->group->nickname));
$this->elementEnd('div');
$this->elementEnd('li');
}
if ($this->group->homepage_logo) {
$this->elementStart('li', array('id' => 'avatar_preview',
'class' => 'avatar_view'));
// TRANS: Header for preview of to be displayed group logo.
$this->element('h2', null, _('Preview'));
$this->elementStart('div', array('id' => 'avatar_preview_view'));
$this->element('img', array('src' => $this->group->homepage_logo,
'width' => AVATAR_PROFILE_SIZE,
'height' => AVATAR_PROFILE_SIZE,
'alt' => $this->group->nickname));
$this->elementEnd('div');
if (!empty($this->group->homepage_logo)) {
// TRANS: Button on group logo upload page to delete current group logo.
$this->submit('delete', _('Delete'));
}
$this->elementEnd('li');
}
$this->elementStart('li', array('id' => 'settings_attach'));
$this->element('input', array('name' => 'MAX_FILE_SIZE',
'type' => 'hidden',
'id' => 'MAX_FILE_SIZE',
'value' => ImageFile::maxFileSizeInt()));
$this->element('input', array('name' => 'avatarfile',
'type' => 'file',
'id' => 'avatarfile'));
$this->elementEnd('li');
$this->elementEnd('ul');
$this->elementStart('ul', 'form_actions');
$this->elementStart('li');
// TRANS: Submit button for uploading a group logo.
$this->submit('upload', _('Upload'));
$this->elementEnd('li');
$this->elementEnd('ul');
$this->elementEnd('fieldset');
$this->elementEnd('form');
}
public function showPageNoticeBlock()
{
parent::showPageNoticeBlock();
if ($this->message) {
$this->element(
'div',
($this->success) ? 'success' : 'error',
$this->message
);
} else {
$inst = $this->getInstructions();
$output = common_markup_to_html($inst);
$this->elementStart('div', 'instructions');
$this->raw($output);
$this->elementEnd('div');
}
}
/**
* 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());
}
/**
* 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 * 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); parent::prepare($args);
@ -104,226 +342,30 @@ class GrouplogoAction extends GroupAction
return true; 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();
}
/**
* Title of the page
*
* @return string Title of the page
*/
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
*
* Shows a form for uploading an avatar.
*
* @return void
*/
function showContent()
{
if ($this->mode == 'crop') {
$this->showCropForm();
} else {
$this->showUploadForm();
}
}
function showUploadForm()
{
$user = common_current_user();
$profile = $user->getProfile();
if (!$profile) {
common_log_db_error($user, 'SELECT', __FILE__);
// TRANS: Error message displayed when referring to a user without a profile.
$this->serverError(_('User has no profile.'));
}
$original = $this->group->original_logo;
$this->elementStart('form', array('enctype' => 'multipart/form-data',
'method' => 'post',
'id' => 'form_settings_avatar',
'class' => 'form_settings',
'action' =>
common_local_url('grouplogo',
array('nickname' => $this->group->nickname))));
$this->elementStart('fieldset');
// TRANS: Group logo form legend.
$this->element('legend', null, _('Group logo'));
$this->hidden('token', common_session_token());
$this->elementStart('ul', 'form_data');
if ($original) {
$this->elementStart('li', array('id' => 'avatar_original',
'class' => 'avatar_view'));
// TRANS: Uploaded original file in group logo form.
$this->element('h2', null, _('Original'));
$this->elementStart('div', array('id'=>'avatar_original_view'));
$this->element('img', array('src' => $this->group->original_logo,
'alt' => $this->group->nickname));
$this->elementEnd('div');
$this->elementEnd('li');
}
if ($this->group->homepage_logo) {
$this->elementStart('li', array('id' => 'avatar_preview',
'class' => 'avatar_view'));
// TRANS: Header for preview of to be displayed group logo.
$this->element('h2', null, _('Preview'));
$this->elementStart('div', array('id'=>'avatar_preview_view'));
$this->element('img', array('src' => $this->group->homepage_logo,
'width' => AVATAR_PROFILE_SIZE,
'height' => AVATAR_PROFILE_SIZE,
'alt' => $this->group->nickname));
$this->elementEnd('div');
if (!empty($this->group->homepage_logo)) {
// TRANS: Button on group logo upload page to delete current group logo.
$this->submit('delete', _('Delete'));
}
$this->elementEnd('li');
}
$this->elementStart('li', array ('id' => 'settings_attach'));
$this->element('input', array('name' => 'MAX_FILE_SIZE',
'type' => 'hidden',
'id' => 'MAX_FILE_SIZE',
'value' => ImageFile::maxFileSizeInt()));
$this->element('input', array('name' => 'avatarfile',
'type' => 'file',
'id' => 'avatarfile'));
$this->elementEnd('li');
$this->elementEnd('ul');
$this->elementStart('ul', 'form_actions');
$this->elementStart('li');
// TRANS: Submit button for uploading a group logo.
$this->submit('upload', _('Upload'));
$this->elementEnd('li');
$this->elementEnd('ul');
$this->elementEnd('fieldset');
$this->elementEnd('form');
}
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');
}
/** /**
* Handle a post * Handle a post
* *
* We mux on the button name to figure out what the user actually wanted. * We mux on the button name to figure out what the user actually wanted.
* *
* @return void * @return void
* @throws ClientException
* @throws NoResultException
* @throws UnsupportedMediaException
* @throws UseFileAsThumbnailException
*/ */
function handlePost() protected function handlePost()
{ {
// CSRF protection parent::handlePost();
$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;
}
if ($this->arg('upload')) { if ($this->arg('upload')) {
$this->uploadLogo(); $this->uploadLogo();
} else if ($this->arg('crop')) { } elseif ($this->arg('crop')) {
$this->cropLogo(); $this->cropLogo();
} else if ($this->arg('delete')) { } elseif ($this->arg('delete')) {
$this->deleteLogo(); $this->deleteLogo();
} else { } else {
// TRANS: Form validation error message when an unsupported argument is used. // TRANS: Form validation error message when an unsupported argument is used.
$this->showForm(_('Unexpected form submission.')); $this->setMessage(_('Unexpected form submission.'), true);
} }
} }
@ -335,30 +377,32 @@ class GrouplogoAction extends GroupAction
* *
* @return void * @return void
*/ */
function uploadLogo() public function uploadLogo()
{ {
try { try {
$imagefile = ImageFile::fromUpload('avatarfile'); $imagefile = ImageFile::fromUpload('avatarfile');
} catch (Exception $e) { } catch (Exception $e) {
$this->showForm($e->getMessage()); $this->setMessage($e->getMessage(), true);
return; return;
} }
$type = $imagefile->preferredType(); $type = $imagefile->preferredType();
$filename = Avatar::filename($this->group->id, $filename = Avatar::filename(
image_type_to_extension($type), $this->group->id,
null, image_type_to_extension($type),
'group-temp-'.common_timestamp()); null,
'group-temp-' . common_timestamp()
);
$filepath = Avatar::path($filename); $filepath = Avatar::path($filename);
$imagefile->copyTo($filepath); $imagefile->copyTo($filepath);
$filedata = array('filename' => $filename, $filedata = array('filename' => $filename,
'filepath' => $filepath, 'filepath' => $filepath,
'width' => $imagefile->width, 'width' => $imagefile->width,
'height' => $imagefile->height, 'height' => $imagefile->height,
'type' => $type); 'type' => $type);
$_SESSION['FILEDATA'] = $filedata; $_SESSION['FILEDATA'] = $filedata;
@ -367,16 +411,24 @@ class GrouplogoAction extends GroupAction
$this->mode = 'crop'; $this->mode = 'crop';
// TRANS: Form instructions on the group logo page. // TRANS: Form instructions on the group logo page.
$this->showForm(_('Pick a square area of the image to be the logo.'), $this->setMessage(_('Pick a square area of the image to be the logo.'));
true); }
public function setMessage($msg, $error = false)
{
$this->message = $msg;
$this->success = !$error;
} }
/** /**
* Handle the results of jcrop. * Handle the results of jcrop.
* *
* @return void * @return void
* @throws NoResultException
* @throws UnsupportedMediaException
* @throws UseFileAsThumbnailException
*/ */
function cropLogo() public function cropLogo()
{ {
$filedata = $_SESSION['FILEDATA']; $filedata = $_SESSION['FILEDATA'];
@ -386,20 +438,24 @@ class GrouplogoAction extends GroupAction
} }
// If image is not being cropped assume pos & dimentions of original // If image is not being cropped assume pos & dimentions of original
$dest_x = $this->arg('avatar_crop_x') ? $this->arg('avatar_crop_x'):0; $dest_x = $this->arg('avatar_crop_x') ? $this->arg('avatar_crop_x') : 0;
$dest_y = $this->arg('avatar_crop_y') ? $this->arg('avatar_crop_y'):0; $dest_y = $this->arg('avatar_crop_y') ? $this->arg('avatar_crop_y') : 0;
$dest_w = $this->arg('avatar_crop_w') ? $this->arg('avatar_crop_w'):$filedata['width']; $dest_w = $this->arg('avatar_crop_w') ? $this->arg('avatar_crop_w') : $filedata['width'];
$dest_h = $this->arg('avatar_crop_h') ? $this->arg('avatar_crop_h'):$filedata['height']; $dest_h = $this->arg('avatar_crop_h') ? $this->arg('avatar_crop_h') : $filedata['height'];
$size = min($dest_w, $dest_h, common_config('avatar', 'maxsize')); $size = min($dest_w, $dest_h, common_config('avatar', 'maxsize'));
$box = array('width' => $size, 'height' => $size, $box = array('width' => $size, 'height' => $size,
'x' => $dest_x, 'y' => $dest_y, 'x' => $dest_x, 'y' => $dest_y,
'w' => $dest_w, 'h' => $dest_h); 'w' => $dest_w, 'h' => $dest_h);
$profile = $this->group->getProfile(); $profile = $this->group->getProfile();
$imagefile = new ImageFile(null, $filedata['filepath']); $imagefile = new ImageFile(null, $filedata['filepath']);
$filename = Avatar::filename($profile->getID(), image_type_to_extension($imagefile->preferredType()), $filename = Avatar::filename(
$size, common_timestamp()); $profile->getID(),
image_type_to_extension($imagefile->preferredType()),
$size,
common_timestamp()
);
$imagefile->resizeTo(Avatar::path($filename), $box); $imagefile->resizeTo(Avatar::path($filename), $box);
@ -408,10 +464,10 @@ class GrouplogoAction extends GroupAction
unset($_SESSION['FILEDATA']); unset($_SESSION['FILEDATA']);
$this->mode = 'upload'; $this->mode = 'upload';
// TRANS: Form success message after updating a group logo. // TRANS: Form success message after updating a group logo.
$this->showForm(_('Logo updated.'), true); $this->setMessage(_('Logo updated.'));
} else { } else {
// TRANS: Form failure message after failing to update a group logo. // 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 * @return void
*/ */
function deleteLogo() public function deleteLogo()
{ {
$orig = clone($this->group); $orig = clone($this->group);
Avatar::deleteFromProfile($this->group->getProfile()); Avatar::deleteFromProfile($this->group->getProfile());
@ -428,56 +484,13 @@ class GrouplogoAction extends GroupAction
@unlink(Avatar::path(basename($this->group->homepage_logo))); @unlink(Avatar::path(basename($this->group->homepage_logo)));
@unlink(Avatar::path(basename($this->group->stream_logo))); @unlink(Avatar::path(basename($this->group->stream_logo)));
@unlink(Avatar::path(basename($this->group->mini_logo))); @unlink(Avatar::path(basename($this->group->mini_logo)));
$this->group->original_logo=User_group::defaultLogo(AVATAR_PROFILE_SIZE); $this->group->original_logo = User_group::defaultLogo(AVATAR_PROFILE_SIZE);
$this->group->homepage_logo=User_group::defaultLogo(AVATAR_PROFILE_SIZE); $this->group->homepage_logo = User_group::defaultLogo(AVATAR_PROFILE_SIZE);
$this->group->stream_logo=User_group::defaultLogo(AVATAR_STREAM_SIZE); $this->group->stream_logo = User_group::defaultLogo(AVATAR_STREAM_SIZE);
$this->group->mini_logo=User_group::defaultLogo(AVATAR_MINI_SIZE); $this->group->mini_logo = User_group::defaultLogo(AVATAR_MINI_SIZE);
$this->group->update($orig); $this->group->update($orig);
// TRANS: Success message for deleting the group logo. // TRANS: Success message for deleting the group logo.
$this->showForm(_('Logo deleted.')); $this->setMessage(_('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');
} }
} }