Code syncronized with shipsimu code base
[mailer.git] / inc / classes / main / resolver / controller / class_BaseControllerResolver.php
diff --git a/inc/classes/main/resolver/controller/class_BaseControllerResolver.php b/inc/classes/main/resolver/controller/class_BaseControllerResolver.php
new file mode 100644 (file)
index 0000000..2548b4f
--- /dev/null
@@ -0,0 +1,129 @@
+<?php
+/**
+ * A generic controller resolver class
+ *
+ * @author             Roland Haeder <webmaster@ship-simu.org>
+ * @version            0.0.0
+ * @copyright  Copyright (c) 2007, 2008 Roland Haeder, this is free software
+ * @license            GNU GPL 3.0 or any newer version
+ * @link               http://www.ship-simu.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
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+class BaseControllerResolver extends BaseResolver {
+       /**
+        * Prefix for local, remote or other resolver
+        */
+       private $controllerPrefix = "";
+
+       /**
+        * Validated controller name
+        */
+       private $controllerName = "";
+
+       /**
+        * Protected constructor
+        *
+        * @param       $className      Name of the real class
+        * @return      void
+        */
+       protected function __construct ($className) {
+               // Call parent constructor
+               parent::__construct($className);
+       }
+
+       /**
+        * Setter for controller prefix
+        *
+        * @param       $controllerPrefix       Last validated controllerPrefix
+        * @return      void
+        */
+       protected final function setControllerPrefix ($controllerPrefix) {
+               $this->controllerPrefix = $controllerPrefix;
+       }
+
+       /**
+        * Setter for controller name
+        *
+        * @param       $controllerName         Last validated controller name
+        * @return      void
+        */
+       protected final function setControllerName ($controllerName) {
+               $this->controllerName = $controllerName;
+       }
+
+       /**
+        * Getter for controller name
+        *
+        * @return      $controllerName Last validated controller name
+        */
+       public final function getControllerName () {
+               return $this->controllerName;
+       }
+
+       /**
+        * Checks wether the given controller is valid
+        *
+        * @param       $controllerName         The default controller we shall execute
+        * @return      $isValid                        Wether the given controller is valid
+        * @throws      EmptyVariableException  Thrown if the given controller is not set
+        * @throws      DefaultControllerException      If the default controller was not found
+        */
+       public function isControllerValid ($controllerName) {
+               // By default nothing shall be valid
+               $isValid = false;
+
+               // Is a controller set?
+               if (empty($controllerName)) {
+                       // Then thrown an exception here
+                       throw new EmptyVariableException(array($this, 'controllerName'), self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
+               } // END - if
+
+               // Now, let us create the full name of the controller class
+               $this->setClassName(sprintf("%s%sController",
+                       $this->controllerPrefix,
+                       $this->convertToClassName($controllerName)
+               ));
+
+               // Try it hard to get an controller
+               while (!$isValid) {
+                       // Is this class already loaded?
+                       if (class_exists($this->getClassName())) {
+                               // This class does exist. :-)
+                               $isValid = true;
+                       } elseif (($this->getClassName() != $this->controllerPrefix.'DefaultController') && ($this->getClassName() != $this->controllerPrefix.'DefaultNewsController')) {
+                               // Do we have news?
+                               if ($this->getConfigInstance()->readConfig('page_with_news') == $this->getApplicationInstance()->getRequestInstance()->getRequestElement('page')) {
+                                       // Yes, display news in home then set default controller with news
+                                       $this->setClassName($this->controllerPrefix . 'DefaultNewsController');
+                               } else {
+                                       // No news at home page or non-news page
+                                       $this->setClassName($this->controllerPrefix . 'DefaultController');
+                               }
+                       } else {
+                               // All is tried, give it up here
+                               throw new DefaultControllerException($this, self::EXCEPTION_DEFAULT_CONTROLLER_GONE);
+                       }
+               } // END - while
+
+               // Debug output
+               //* DEBUG: */ $this->debugBackTrace();
+
+               // Return the result
+               return $isValid;
+       }
+}
+
+// [EOF]
+?>