view classes/loader.php @ 120:71b611a676c3 extractapp

include maintemplate.php
author Calvin Yeh <cyeh@mpiwg-berlin.mpg.de>
date Thu, 28 Sep 2017 17:48:35 +0200
parents f1f849d31272
children
line wrap: on
line source

<?php
/*
 * loader.php
 * This file is part of Extraction-interface.
 *
 * Extraction-interface 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.
 *
 * Extraction-interface 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 Extraction-interface.  If not, see <http://www.gnu.org/licenses/>.
*/

/** 
 * Loader is used to route parameters from input url and set parameters for controller.
 * The routing is done by the RewriteRule written in .htaccess file.
*/

class Loader {
	private $controller;
	private $action;
	private $urlvalues;
	private $postdata = 0;

	public function __construct($urlvalues, $postdata) {
		/** 
		 * It stores parameters in URL on object creation.
		 * 
		 * For example, the URL structure is like this: some_domain_name/Extractapp/TaggingText. 
		 * The controller is "Extractapp" and the action is "TaggingText".
		 *
		 * For the "Extractapp" controller, there is a corresponding "extractapp.php" in "./controllers" folder.
		 *
		 * For the action "TaggingText", there is a corresponding "TaggingText.php" in "./views/Extractapp" folder.
		 * 
		 * Under "./view" folder, the first level of the sub-folder is named by the controller's name. 
		 * Each action controlled by the controller has it's own php file. The file name is the same as the the action's name.
		 */

		$this->urlvalues = $urlvalues;
		$this->postdata = $postdata;
		if ($this->urlvalues['controller'] == "") {
			$this->controller = "Extractapp";	// the default controller

		} else {
			$this->controller = $this->urlvalues['controller'];
		}

		if ($this->urlvalues['action'] == "") {
			$this->action = "TaggingText";	// the default action

		} else {
			$this->action = $this->urlvalues['action'];
		}
	}
	
	public function CreateController() {
		/** 
		 * Establish the requested controller as an object, and check if the query is valid.
		 * The queried controller, which should be extended from BaseController, exists and the queried action should be one of its method.
		 */

		//does the class exist?
		if (class_exists($this->controller)) {
			$parents = class_parents($this->controller);
			//does the class extend the controller class?
			if (in_array("BaseController",$parents)) {
				//does the class contain the requested method?
				if (method_exists($this->controller,$this->action)) {
					return new $this->controller($this->action,$this->urlvalues,$this->postdata);
				} else {
					//bad method error
					return new Error("badUrl",$this->urlvalues);
				}
			} else {
				//bad controller error
				return new Error("badUrl",$this->urlvalues);
			}
		} else {
			//bad controller error
			return new Error("badUrl",$this->urlvalues);
		}
	}
}

?>