From: Mikael Nordfeldth <mmn@hethane.se>
Date: Sun, 18 May 2014 11:31:31 +0000 (+0200)
Subject: FormAction extends ManagedAction
X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=23c288c699d557ab3f1b67b0ccd60a51bf209f38;p=quix0rs-gnu-social.git

FormAction extends ManagedAction

handlePost is now more naturally called and doesn't require a separate
'handle' function for each subclass.
---

diff --git a/lib/action.php b/lib/action.php
index 5ed943761c..402e73c19d 100644
--- a/lib/action.php
+++ b/lib/action.php
@@ -58,7 +58,8 @@ class Action extends HTMLOutputter // lawsuit
     protected $ajax   = false;
     protected $menus  = true;
     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)
     protected $scoped = null;
@@ -143,6 +144,11 @@ class Action extends HTMLOutputter // lawsuit
             $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 could be set with get_called_action and then
diff --git a/lib/formaction.php b/lib/formaction.php
index a3e5498aad..60d9b3ce13 100644
--- a/lib/formaction.php
+++ b/lib/formaction.php
@@ -41,11 +41,12 @@ if (!defined('STATUSNET')) {
  * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
  * @link     http://status.net/
  */
-class FormAction extends Action
+class FormAction extends ManagedAction
 {
     protected $form = null;
     protected $type = null;
     protected $needLogin = true;
+    protected $canPost = true;
 
     protected function prepare(array $args=array()) {
         parent::prepare($args);
@@ -63,22 +64,6 @@ class FormAction extends Action
         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) {
         return !$this->isPost();
     }
@@ -113,22 +98,14 @@ class FormAction extends Action
         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;
      * @return void
      */
     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) {
diff --git a/lib/managedaction.php b/lib/managedaction.php
index 580d8c09e3..318df320e1 100644
--- a/lib/managedaction.php
+++ b/lib/managedaction.php
@@ -39,6 +39,14 @@ class ManagedAction extends Action
     {
         parent::handle();
 
+        if ($this->canPost && $this->isPost()) {
+            try {
+                $this->msg = $this->handlePost();
+            } catch (Exception $e) {
+                $this->error = $e->getMessage();
+            }
+        }
+
         if (StatusNet::isAjax()) {
             $this->showAjax();
         } 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()
     {
         $this->startHTML('text/xml;charset=utf-8');