FormAction extends ManagedAction

handlePost is now more naturally called and doesn't require a separate
'handle' function for each subclass.
This commit is contained in:
Mikael Nordfeldth 2014-05-18 13:31:31 +02:00
parent b48e3a22bf
commit 23c288c699
3 changed files with 24 additions and 28 deletions

View File

@ -58,7 +58,8 @@ class Action extends HTMLOutputter // lawsuit
protected $ajax = false; protected $ajax = false;
protected $menus = true; protected $menus = true;
protected $needLogin = false; protected $needLogin = false;
protected $needPost = false; protected $needPost = false; // implies canPost if true
protected $canPost = false; // can this action handle POST method?
// The currently scoped profile (normally Profile::current; from $this->auth_user for API) // The currently scoped profile (normally Profile::current; from $this->auth_user for API)
protected $scoped = null; protected $scoped = null;
@ -143,6 +144,11 @@ class Action extends HTMLOutputter // lawsuit
$this->clientError(_('This method requires a POST.'), 405); $this->clientError(_('This method requires a POST.'), 405);
} }
// needPost, of course, overrides canPost if true
if (!$this->canPost) {
$this->canPost = $this->needPost;
}
$this->args = common_copy_args($args); $this->args = common_copy_args($args);
// This could be set with get_called_action and then // This could be set with get_called_action and then

View File

@ -41,11 +41,12 @@ if (!defined('STATUSNET')) {
* @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3 * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
* @link http://status.net/ * @link http://status.net/
*/ */
class FormAction extends Action class FormAction extends ManagedAction
{ {
protected $form = null; protected $form = null;
protected $type = null; protected $type = null;
protected $needLogin = true; protected $needLogin = true;
protected $canPost = true;
protected function prepare(array $args=array()) { protected function prepare(array $args=array()) {
parent::prepare($args); parent::prepare($args);
@ -63,22 +64,6 @@ class FormAction extends Action
return true; return true;
} }
protected function handle()
{
parent::handle();
if ($this->isPost()) {
try {
$msg = $this->handlePost();
$this->showForm($msg, true);
} catch (Exception $e) {
$this->showForm($e->getMessage());
}
} else {
$this->showForm();
}
}
public function isReadOnly($args) { public function isReadOnly($args) {
return !$this->isPost(); return !$this->isPost();
} }
@ -113,22 +98,14 @@ class FormAction extends Action
return null; return null;
} }
public function showForm($msg=null, $success=false)
{
if ($success) {
$this->msg = $msg;
} else {
$this->error = $msg;
}
$this->showPage();
}
/** /**
* Gets called from handle() if isPost() is true; * Gets called from handle() if isPost() is true;
* @return void * @return void
*/ */
protected function handlePost() protected function handlePost()
{ {
parent::handlePost();
// check for this before token since all POST and FILES data // check for this before token since all POST and FILES data
// is losts when size is exceeded // is losts when size is exceeded
if (empty($_POST) && $_SERVER['CONTENT_LENGTH']>0) { if (empty($_POST) && $_SERVER['CONTENT_LENGTH']>0) {

View File

@ -39,6 +39,14 @@ class ManagedAction extends Action
{ {
parent::handle(); parent::handle();
if ($this->canPost && $this->isPost()) {
try {
$this->msg = $this->handlePost();
} catch (Exception $e) {
$this->error = $e->getMessage();
}
}
if (StatusNet::isAjax()) { if (StatusNet::isAjax()) {
$this->showAjax(); $this->showAjax();
} else { } else {
@ -46,6 +54,11 @@ class ManagedAction extends Action
} }
} }
protected function handlePost()
{
// This will only be run if the Action has the property canPost==true
}
public function showAjax() public function showAjax()
{ {
$this->startHTML('text/xml;charset=utf-8'); $this->startHTML('text/xml;charset=utf-8');