Introduced genericHanleRequestLoginFailedRedirect().
[core.git] / inc / classes / main / controller / class_BaseController.php
index 0fa79fbac92b2eb84e49360b2ffca525ddcd64a1..d195c9e76bd21972997495f7a2b5f7a39562dc23 100644 (file)
@@ -4,11 +4,11 @@
  * write your own controller. You get the advantage that you can use the pre and
  * post filters.
  *
- * @author             Roland Haeder <webmaster@ship-simu.org>
+ * @author             Roland Haeder <webmaster@shipsimu.org>
  * @version            0.0.0
- * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2011 Core Developer Team
+ * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Core Developer Team
  * @license            GNU GPL 3.0 or any newer version
- * @link               http://www.ship-simu.org
+ * @link               http://www.shipsimu.org
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -54,6 +54,82 @@ class BaseController extends BaseFrameworkSystem implements Registerable {
                Registry::getRegistry()->addInstance('controller', $this);
        }
 
+       /**
+        * Executes a command with pre and post filters
+        *
+        * @param       $requestInstance        A Requestable class
+        * @param       $responseInstance       A Responseable class
+        * @return      void
+        */
+       public function executeGenericPrePostCommand (Requestable $requestInstance, Responseable $responseInstance) {
+               // Get the command instance from the resolver by sending a request instance to the resolver
+               $commandInstance = $this->getResolverInstance()->resolveCommandByRequest($requestInstance);
+
+               // Add more filters by the command
+               $commandInstance->addExtraFilters($this, $requestInstance);
+
+               // Run the pre filters
+               $this->executePreFilters($requestInstance, $responseInstance);
+
+               // This request was valid! :-D
+               $requestInstance->requestIsValid();
+
+               // Execute the command
+               $commandInstance->execute($requestInstance, $responseInstance);
+
+               // Run the post filters
+               $this->executePostFilters($requestInstance, $responseInstance);
+
+               // Flush the response out
+               $responseInstance->flushBuffer();
+       }
+
+       /**
+        * Handles the given request and response, redirects to login_failed if
+        * UserAuthorizationException is thrown.
+        *
+        * @param       $requestInstance        An instance of a request class
+        * @param       $responseInstance       An instance of a response class
+        * @return      void
+        */
+       public function genericHanleRequestLoginFailedRedirect (Requestable $requestInstance, Responseable $responseInstance) {
+               // Get the "form action"
+               $formAction = $requestInstance->getRequestElement('form');
+
+               // Get command instance from resolver
+               $commandInstance = $this->getResolverInstance()->resolveCommand($formAction);
+
+               // Add more filters by the command
+               $commandInstance->addExtraFilters($this, $requestInstance);
+
+               // Try to run the pre filters, if auth exceptions come through redirect here
+               try {
+                       // Run the pre filters
+                       $this->executePreFilters($requestInstance, $responseInstance);
+               } catch (UserAuthorizationException $e) {
+                       // Redirect to main page
+                       $responseInstance->redirectToConfiguredUrl('login_failed');
+
+                       // Exit here
+                       exit();
+               }
+
+               /*
+                * Is the request still valid? Post filters shall only be executed of
+                * the request is valid
+                */
+               if ($requestInstance->isRequestValid()) {
+                       // Execute the command
+                       $commandInstance->execute($requestInstance, $responseInstance);
+
+                       // Execute *very* generic ppost filters
+                       $this->executePostFilters($requestInstance, $responseInstance);
+               } // END - if
+
+               // Flush the buffer out
+               $responseInstance->flushBuffer();
+       }
+
        /**
         * Private method to initialize a given filter chain
         *
@@ -61,9 +137,9 @@ class BaseController extends BaseFrameworkSystem implements Registerable {
         * @return      void
         */
        protected function initFilterChain ($filterChain) {
-               //* DEBUG: */ $this->debugOutput('CONTROLLER: ' . $filterChain . ' init: START');
+               //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONTROLLER: ' . $filterChain . ' init: START');
                $this->filterChains[$filterChain] = ObjectFactory::createObjectByConfiguredName('filter_chain_class');
-               //* DEBUG: */ $this->debugOutput('CONTROLLER: ' . $filterChain . ' init: FINISHED');
+               //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONTROLLER: ' . $filterChain . ' init: FINISHED');
        }
 
        /**
@@ -75,7 +151,7 @@ class BaseController extends BaseFrameworkSystem implements Registerable {
         * @throws      InvalidFilterChainException     If the filter chain is invalid
         */
        protected function addFilter ($filterChain, Filterable $filterInstance) {
-               //* DEBUG: */ $this->debugOutput('CONTROLLER: ' . $filterChain . ',' . $filterInstance->__toString(). ' add: START');
+               //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONTROLLER: ' . $filterChain . ',' . $filterInstance->__toString(). ' add: START');
 
                // Test if the filter is there
                if (!isset($this->filterChains[$filterChain])) {
@@ -85,7 +161,7 @@ class BaseController extends BaseFrameworkSystem implements Registerable {
 
                // Add the filter
                $this->filterChains[$filterChain]->addFilter($filterInstance);
-               //* DEBUG: */ $this->debugOutput('CONTROLLER: ' . $filterChain . ',' . $filterInstance->__toString(). ' add: FINISH');
+               //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONTROLLER: ' . $filterChain . ',' . $filterInstance->__toString(). ' add: FINISH');
        }
 
        /**
@@ -110,6 +186,16 @@ class BaseController extends BaseFrameworkSystem implements Registerable {
                $this->addFilter(self::FILTER_CHAIN_POST_COMMAND, $filterInstance);
        }
 
+       /**
+        * Add a shutdown filter
+        *
+        * @param       $filterInstance         A Filterable class
+        * @return      void
+        */
+       public function addShutdownFilter (Filterable $filterInstance) {
+               $this->addFilter('shutdown', $filterInstance);
+       }
+
        /**
         * Executes given filter chain chain
         *
@@ -153,6 +239,17 @@ class BaseController extends BaseFrameworkSystem implements Registerable {
                // Execute all post filters
                $this->executeFilters(self::FILTER_CHAIN_POST_COMMAND, $requestInstance, $responseInstance);
        }
+
+       /**
+        * Executes all shutdown filters
+        *
+        * @param       $requestInstance        A Requestable class
+        * @param       $responseInstance       A Responseable class
+        * @return      void
+        */
+       public function executeShutdownFilters (Requestable $requestInstance, Responseable $responseInstance) {
+               $this->executeFilters('shutdown', $requestInstance, $responseInstance);
+       }
 }
 
 // [EOF]