A bunch of FormAction and ManagedAction synchronization

This commit is contained in:
Mikael Nordfeldth 2015-03-08 20:41:42 +01:00
parent 55894f02c7
commit 1ea876296d
11 changed files with 103 additions and 246 deletions

View File

@ -43,23 +43,17 @@ class CancelsubscriptionAction extends FormAction
{
protected $needPost = true;
protected function prepare(array $args=array())
protected function doPreparation()
{
parent::prepare($args);
$profile_id = $this->int('unsubscribeto');
$this->target = Profile::getKV('id', $profile_id);
if (!$this->target instanceof Profile) {
throw new NoProfileException($profile_id);
}
return true;
}
protected function handlePost()
protected function doPost()
{
parent::handlePost();
try {
$request = Subscription_queue::pkeyGet(array('subscriber' => $this->scoped->id,
'subscribed' => $this->target->id));
@ -82,10 +76,7 @@ class CancelsubscriptionAction extends FormAction
$this->elementEnd('body');
$this->endHTML();
exit();
} else {
common_redirect(common_local_url('subscriptions',
array('nickname' => $this->scoped->nickname)),
303);
}
common_redirect(common_local_url('subscriptions', array('nickname' => $this->scoped->getNickname())), 303);
}
}

View File

@ -61,10 +61,8 @@ class LoginAction extends FormAction
*
* @return void
*/
protected function handlePost()
protected function doPost()
{
parent::handlePost();
// XXX: login throttle
$nickname = $this->trimmed('nickname');
@ -104,22 +102,6 @@ class LoginAction extends FormAction
common_redirect($url, 303);
}
/**
* Store an error and show the page
*
* This used to show the whole page; now, it's just a wrapper
* that stores the error in an attribute.
*
* @param string $error error, if any.
*
* @return void
*/
public function showForm($msg=null, $success=false)
{
common_ensure_session();
return parent::showForm($msg, $success);
}
function showScripts()
{
parent::showScripts();

View File

@ -61,27 +61,15 @@ class NewApplicationAction extends FormAction
$this->clientError(_('Unexpected form submission.'));
}
function showForm($msg=null)
protected function getForm()
{
$this->msg = $msg;
$this->showPage();
return new ApplicationEditForm($this);
}
function showContent()
public function getInstructions()
{
$form = new ApplicationEditForm($this);
$form->show();
}
function showPageNotice()
{
if ($this->msg) {
$this->element('p', 'error', $this->msg);
} else {
$this->element('p', 'instructions',
// TRANS: Form instructions for registering a new application.
_('Use this form to register a new application.'));
}
// TRANS: Form instructions for registering a new application.
return _('Use this form to register a new application.');
}
private function trySave()

View File

@ -29,9 +29,7 @@
* @link http://status.net/
*/
if (!defined('STATUSNET')) {
exit(1);
}
if (!defined('GNUSOCIAL')) { exit(1); }
/**
* Add a new group
@ -48,6 +46,8 @@ class NewgroupAction extends FormAction
{
protected $group;
protected $form = 'GroupEdit';
function getGroup() {
return $this->group;
}
@ -58,39 +58,23 @@ class NewgroupAction extends FormAction
return _('New group');
}
/**
* Prepare to run
*/
protected function prepare(array $args=array())
protected function doPreparation()
{
parent::prepare($args);
// $this->scoped is the current user profile
if (!$this->scoped->hasRight(Right::CREATEGROUP)) {
// TRANS: Client exception thrown when a user tries to create a group while banned.
$this->clientError(_('You are not allowed to create groups on this site.'), 403);
}
return true;
}
public function showContent()
protected function getInstructions()
{
$form = new GroupEditForm($this);
$form->show();
// TRANS: Form instructions for group create form.
return _('Use this form to create a new group.');
}
public function showInstructions()
protected function doPost()
{
$this->element('p', 'instructions',
// TRANS: Form instructions for group create form.
_('Use this form to create a new group.'));
}
protected function handlePost()
{
parent::handlePost();
if (Event::handle('StartGroupSaveForm', array($this))) {
$nickname = Nickname::normalize($this->trimmed('newnickname'), true);
@ -133,7 +117,6 @@ class NewgroupAction extends FormAction
'Too many aliases! Maximum %d allowed.',
common_config('group', 'maxaliases')),
common_config('group', 'maxaliases')));
return;
}
if ($private) {

View File

@ -30,9 +30,7 @@
* @link http://status.net/
*/
if (!defined('STATUSNET')) {
exit(1);
}
if (!defined('GNUSOCIAL')) { exit(1); }
/**
* Action for posting new notices
@ -83,7 +81,7 @@ class NewnoticeAction extends FormAction
}
/**
* This handlePost saves a new notice, based on arguments
* This doPost saves a new notice, based on arguments
*
* If successful, will show the notice, or return an Ajax-y result.
* If not, it will show an error message -- possibly Ajax-y.
@ -93,17 +91,15 @@ class NewnoticeAction extends FormAction
*
* @return void
*/
protected function handlePost()
protected function doPost()
{
parent::handlePost();
assert($this->scoped); // XXX: maybe an error instead...
assert($this->scoped instanceof Profile); // XXX: maybe an error instead...
$user = $this->scoped->getUser();
$content = $this->trimmed('status_textarea');
$options = array();
Event::handle('StartSaveNewNoticeWeb', array($this, $user, &$content, &$options));
if (!$content) {
if (empty($content)) {
// TRANS: Client error displayed trying to send a notice without content.
$this->clientError(_('No content!'));
}

View File

@ -40,15 +40,16 @@ if (!defined('GNUSOCIAL')) { exit(1); }
*/
class RepeatAction extends FormAction
{
protected $needPost = true; // At least for now, until repeat interface is available
protected $notice = null; // Notice that is being repeated.
protected $repeat = null; // The resulting repeat object/notice.
protected function prepare(array $args=array())
function title()
{
parent::prepare($args);
return _m('TITLE', 'Repeat notice');
}
protected function doPreparation()
{
$id = $this->trimmed('notice');
if (empty($id)) {

View File

@ -27,9 +27,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
if (!defined('STATUSNET')) {
exit(1);
}
if (!defined('GNUSOCIAL')) { exit(1); }
/**
* Form action extendable class
@ -125,8 +123,6 @@ class FormAction extends ManagedAction
*/
protected function handlePost()
{
parent::handlePost();
// check for this before token since all POST and FILES data
// is losts when size is exceeded
if (empty($_POST) && $_SERVER['CONTENT_LENGTH']>0) {
@ -138,6 +134,6 @@ class FormAction extends ManagedAction
throw new ClientException($msg);
}
$this->checkSessionToken();
return parent::handlePost();
}
}

View File

@ -64,9 +64,27 @@ class ManagedAction extends Action
$this->showPage();
}
/**
* If this is extended in child classes, they should
* end with 'return parent::handlePost();' - and they
* should only extend this function if what they do
* cannot be handled in ->doPost()
*/
protected function handlePost()
{
// This will only be run if the Action has the property canPost==true
assert($this->canPost);
$this->checkSessionToken();
return $this->doPost();
}
/**
* Do Post stuff. Return a string if successful,
* describing what has been done. Always throw an
* exception on failure, with a descriptive message.
*/
protected function doPost() {
throw new Exception('Unhandled POST');
}
}

View File

@ -30,9 +30,7 @@
* @link http://status.net/
*/
if (!defined('STATUSNET')) {
exit(1);
}
if (!defined('GNUSOCIAL')) { exit(1); }
/**
* Action for posting new direct messages
@ -52,7 +50,7 @@ class NewmessageAction extends FormAction
var $to = null;
var $other = null;
protected $form = 'message'; // will become MessageForm later
protected $form = 'Message'; // will become MessageForm later
/**
* Title of the page
@ -68,18 +66,8 @@ class NewmessageAction extends FormAction
return _('New message');
}
/**
* Handle input, produce output
*
* @param array $args $_REQUEST contents
*
* @return void
*/
protected function prepare(array $args=array())
protected function doPreparation()
{
parent::prepare($args);
$this->content = $this->trimmed('content');
$this->to = $this->trimmed('to');
@ -107,29 +95,27 @@ class NewmessageAction extends FormAction
return true;
}
protected function handlePost()
protected function doPost()
{
parent::handlePost();
assert($this->scoped instanceof Profile); // XXX: maybe an error instead...
if (!$this->content) {
if (empty($this->content)) {
// TRANS: Form validator error displayed trying to send a direct message without content.
$this->clientError(_('No content!'));
} else {
$content_shortened = $this->scoped->shortenLinks($this->content);
if (Message::contentTooLong($content_shortened)) {
// TRANS: Form validation error displayed when message content is too long.
// TRANS: %d is the maximum number of characters for a message.
$this->clientError(sprintf(_m('That\'s too long. Maximum message size is %d character.',
'That\'s too long. Maximum message size is %d characters.',
Message::maxContent()),
Message::maxContent()));
}
}
if (!$this->other) {
$content_shortened = $this->scoped->shortenLinks($this->content);
if (Message::contentTooLong($content_shortened)) {
// TRANS: Form validation error displayed when message content is too long.
// TRANS: %d is the maximum number of characters for a message.
$this->clientError(sprintf(_m('That\'s too long. Maximum message size is %d character.',
'That\'s too long. Maximum message size is %d characters.',
Message::maxContent()),
Message::maxContent()));
}
if (!$this->other instanceof Profile) {
// TRANS: Form validation error displayed trying to send a direct message without specifying a recipient.
$this->clientError(_('No recipient specified.'));
} else if (!$this->scoped->mutuallySubscribed($this->other)) {
@ -145,86 +131,18 @@ class NewmessageAction extends FormAction
$message = Message::saveNew($this->scoped->id, $this->other->id, $this->content, 'web');
$message->notify();
if ($this->boolean('ajax')) {
$this->startHTML('text/xml;charset=utf-8');
$this->elementStart('head');
// TRANS: Page title after sending a direct message.
$this->element('title', null, _('Message sent'));
$this->elementEnd('head');
$this->elementStart('body');
$this->element('p', array('id' => 'command_result'),
// TRANS: Confirmation text after sending a direct message.
// TRANS: %s is the direct message recipient.
sprintf(_('Direct message to %s sent.'),
$this->other->nickname));
$this->elementEnd('body');
$this->endHTML();
} else {
$url = common_local_url('outbox',
array('nickname' => $this->scoped->nickname));
common_redirect($url, 303);
if (GNUsocial::isAjax()) {
// TRANS: Confirmation text after sending a direct message.
// TRANS: %s is the direct message recipient.
return sprintf(_('Direct message to %s sent.'), $this->other->getNickname());
}
$url = common_local_url('outbox', array('nickname' => $this->scoped->getNickname()));
common_redirect($url, 303);
}
/**
* Show an Ajax-y error message
*
* Goes back to the browser, where it's shown in a popup.
*
* @param string $msg Message to show
*
* @return void
*/
function ajaxErrorMsg($msg)
{
$this->startHTML('text/xml;charset=utf-8', true);
$this->elementStart('head');
// TRANS: Page title after an AJAX error occurred on the "send direct message" page.
$this->element('title', null, _('Ajax Error'));
$this->elementEnd('head');
$this->elementStart('body');
$this->element('p', array('id' => 'error'), $msg);
$this->elementEnd('body');
$this->endHTML();
}
function showForm($msg = null)
{
if ($msg && $this->boolean('ajax')) {
$this->ajaxErrorMsg($msg);
return;
}
$this->msg = $msg;
if ($this->trimmed('ajax')) {
$this->startHTML('text/xml;charset=utf-8');
$this->elementStart('head');
// TRANS: Page title on page for sending a direct message.
$this->element('title', null, _('New message'));
$this->elementEnd('head');
$this->elementStart('body');
$this->showNoticeForm();
$this->elementEnd('body');
$this->endHTML();
}
else {
$this->showPage();
}
}
function showPageNotice()
{
if ($this->msg) {
$this->element('p', 'error', $this->msg);
}
}
// Do nothing (override)
function showNoticeForm()
{
$message_form = new MessageForm($this, $this->other, $this->content);
$message_form->show();
// Just don't show a NoticeForm
}
}

View File

@ -44,48 +44,41 @@ if (!defined('GNUSOCIAL')) { exit(1); }
*/
class DisfavorAction extends FormAction
{
public function showForm($msg=null, $success=false)
protected $needPost = true;
protected function doPreparation()
{
if ($success) {
common_redirect(common_local_url('showfavorites',
array('nickname' => $this->scoped->nickname)), 303);
$this->target = Notice::getKV($this->trimmed('notice'));
if (!$this->target instanceof Notice) {
throw new ServerException(_m('No such notice.'));
}
parent::showForm($msg, $success);
}
protected function handlePost()
protected function doPost()
{
$id = $this->trimmed('notice');
$notice = Notice::getKV($id);
if (!$notice instanceof Notice) {
$this->serverError(_('Notice not found'));
}
$fave = new Fave();
$fave->user_id = $this->scoped->id;
$fave->notice_id = $notice->id;
$fave->user_id = $this->scoped->getID();
$fave->notice_id = $this->target->getID();
if (!$fave->find(true)) {
throw new NoResultException($fave);
// TRANS: Client error displayed when trying to remove a 'favor' when there is none in the first place.
throw new AlreadyFulfilledException(_('This is already not favorited.'));
}
$result = $fave->delete();
if (!$result) {
if ($result === false) {
common_log_db_error($fave, 'DELETE', __FILE__);
// TRANS: Server error displayed when removing a favorite from the database fails.
$this->serverError(_('Could not delete favorite.'));
}
Fave::blowCacheForProfileId($this->scoped->id);
if (GNUsocial::isAjax()) {
$this->startHTML('text/xml;charset=utf-8');
$this->elementStart('head');
// TRANS: Title for page on which favorites can be added.
$this->element('title', null, _('Add to favorites'));
$this->elementEnd('head');
$this->elementStart('body');
$favor = new FavorForm($this, $notice);
$favor->show();
$this->elementEnd('body');
$this->endHTML();
exit;
throw new ServerException(_('Could not delete favorite.'));
}
Fave::blowCacheForProfileId($this->scoped->getID());
// TRANS: Message when a disfavor action has been taken for a notice.
return _('Disfavored the notice.');
}
protected function showContent()
{
// We show the 'Favor' form because right now all calls to Disfavor will directly disfavor a notice.
$disfavor = new FavorForm($this, $this->target);
$disfavor->show();
}
}

View File

@ -46,29 +46,21 @@ class FavorAction extends FormAction
{
protected $needPost = true;
protected $object = null;
protected function prepare(array $args=array())
protected function doPreparation()
{
parent::prepare($args);
$this->target = Notice::getKV($this->trimmed('notice'));
if (!$this->target instanceof Notice) {
throw new ServerException(_m('No such notice.'));
}
if (!$this->target->inScope($this->scoped)) {
// TRANS: Client error displayed when trying to reply to a notice a the target has no access to.
// TRANS: Client error displayed when trying to interact with a notice a the target has no access to.
// TRANS: %1$s is a user nickname, %2$d is a notice ID (number).
throw new ClientException(sprintf(_m('%1$s has no right to reply to notice %2$d.'), $this->scoped->getNickname(), $this->target->id), 403);
throw new ClientException(sprintf(_m('%1$s has no right to interact with notice %2$d.'), $this->scoped->getNickname(), $this->target->getID()), 403);
}
return true;
}
protected function handlePost()
protected function doPost()
{
parent::handlePost();
if (Fave::existsForProfile($this->target, $this->scoped)) {
// TRANS: Client error displayed when trying to mark a notice as favorite that already is a favorite.
throw new AlreadyFulfilledException(_('You have already favorited this!'));
@ -77,14 +69,13 @@ class FavorAction extends FormAction
// throws exception on failure
$stored = Fave::addNew($this->scoped, $this->target);
// TRANS: Message when a favor action has been taken for a notice.
return _('Favorited the notice');
}
protected function showContent()
{
if ($this->target instanceof Notice) {
$disfavor = new DisfavorForm($this, $this->target);
$disfavor->show();
}
$disfavor = new DisfavorForm($this, $this->target);
$disfavor->show();
}
}