public function render($vars, $controller, $function) { $this->controller = $controller; $this->html = new HtmlHelper(MVCContext::getContext()->getController()); extract($vars); ob_start(); include ViewCompiler::getView($this->path, $controller, $function, $this->html); $contents = ob_get_contents(); ob_end_clean(); $this->contents = $contents; }
public static final function executeController($controllerScript, $controllerFunction, $controllerParams, $requestURI, $methodParameters, $queryString, $presetVariables = array()) { // First enable the API, if used if (property_exists('AppConfiguration', 'API')) { if (!empty(AppConfiguration::$API)) { // Include the APIController, which will load itself in the routes as well require_once LIBS_ROOT . "/api.php"; } } // Check routing $routes = array(); if (property_exists('AppConfiguration', 'ROUTES')) { $routes = AppConfiguration::$ROUTES; } if (file_exists(WEBAPP_ROOT . "/tmp/autoroutes.php")) { require_once WEBAPP_ROOT . "/tmp/autoroutes.php"; $routes = array_merge($routes, AutoRoutes::$AUTO_ROUTES); } /** * Check if the request is mapped normally or if we have a routing rule for it */ if (!empty($routes["/{$controllerScript}/{$controllerFunction}"])) { /* Use routing */ $route = $routes["/{$controllerScript}/{$controllerFunction}"]; $controllerFilename = $route["file"]; $controllerClass = $route["class"]; } else { /* Normal mapping */ // Find the controller class $controllerFilename = WEBAPP_ROOT . "/controllers/" . $controllerScript . "_controller.php"; $controllerClass = ucfirst($controllerScript) . "Controller"; } if (!file_exists($controllerFilename)) { controllerErrors::missingcontroller($controllerScript); } require_once $controllerFilename; /* * Populate some variable to the controller */ $controller = new $controllerClass(); $controller->_requestURI = $requestURI; $controller->params = $controllerParams; $controller->controllerName = strtolower($controllerScript); /* Set the controller to the context */ MVCContext::getContext()->setController(&$controller); if (!empty($presetVariables)) { $controller->_contextVariables = $presetVariables; } // Initialize the controller $controller->_init(); /* * See if the requested method exists. If not, we try to find * a method with the name _default to call, which is a "catch-all" -method */ if (!method_exists($controller, $controllerFunction)) { if (!method_exists($controller, "_default")) { controllerErrors::missingMethod($controllerScript, $controllerFunction); } else { $methodParameters = array_merge(array($controllerScript, "default", $controllerFunction), array_slice($methodParameters, 3)); $controllerFunction = "_default"; } } /* * Populate input */ $input = array(); if (!empty($_POST)) { $input = $_POST; $controller->inputmethod = INPUT_METHOD_POST; } else { parse_str($queryString, $input); $controller->inputmethod = INPUT_METHOD_GET; } /* * Begin the execution by processing any filters */ if (property_exists('AppConfiguration', 'FILTERS')) { foreach (AppConfiguration::$FILTERS as $filter) { if ($filter["target"][0] == $controllerScript || $filter["target"][0] == "*") { // Controller -part matches, see if the action matches if ($filter["target"][1] == $controllerFunction || $filter["target"][1] == "*" || is_array($filter["target"][1]) && in_array($controllerFunction, $filter["target"][1])) { // Matches, execute the filter if (!class_exists($filter["filter"])) { require_once WEBAPP_ROOT . "/filters/" . Inflector::decamelize($filter["filter"]) . ".php"; } /* * Invoke the filter. We provide as parameter the controller name, action name and the input * sent from the browser. The input is passed by reference so it can be modified * by the filter along with any parameters defined in the AppConfiguration */ $filterClass = $filter["filter"]; $filterImpl = new $filterClass(); if (call_user_func_array(array($filterImpl, "processFilter"), array($controllerScript, $controllerFunction, $filter["parameters"], &$input)) === false) { // By returning false, the filter can stop the processing // Then we check if we just "die" or do we do some rendering if ($filterImpl->_renderView) { MVC::renderView(!empty($filterImpl->view) ? $filterImpl->view : $controllerFunction, $filterImpl->template, $filterImpl->_contextVariables, $controllerScript, $controllerFunction); } // Exit exit; } } } } } /* * Populate the controller with the input, now that it's been through the filters */ $controller->input = $input; /* * Then continue to the controller */ call_user_func_array(array($controller, $controllerFunction), $methodParameters); /* * Render the view */ if ($controller->_renderView) { /* * Render the view */ MVC::renderView(empty($controller->view) ? $controllerFunction : $controller->view, $controller->template, $controller->_contextVariables, $controllerScript, $controllerFunction); } return $controller; }
/** * Validate the data before storing it */ private function __validate($data = array()) { /* Empty any existing errors */ $this->errors = array(); if (empty($data)) { foreach ($this->fields as $field) { $data[$field] = $this->{$field}; } } if (is_array($this->validators)) { /** * Iterate through the fields */ foreach ($this->validators as $field => $validators) { if (is_array($validators)) { /** * And each of the validators for the field */ foreach ($validators as $validator => $parameters) { /** * Validators can be defined without parameters, so if $parameters is not an arrary, * then it actually contains the name of our validator */ if (!is_array($parameters)) { $validator = $parameters; $parameters = array(); } /** * Find the validator and execute it */ $validatorClass = Inflector::camelize(sprintf("%s_validator", $validator)); if (!class_exists($validatorClass)) { /** * Try to find the validator implementation file */ $path = sprintf("%s/validators/%s.php", dirname(__FILE__), strtolower($validator)); if (!file_exists($path)) { SwissMVCErrors::generalError("Error in " . get_class($this) . "->validators: Could not find validator class file ({$path}) for validator '{$validator}'", false); } require_once $path; // Now make sure that the class actually was in the file if (!class_exists($validatorClass)) { SwissMVCErrors::generalError("Error in " . get_class($this) . "->validators: Could not find validator implementation '{$validator}'. File was found, but doesn't contain correct implementation " . "(class {$validatorClass} not found).", false); } } $_impl = new $validatorClass(); /* Do the actual validation. If validator returns true, the field is valid */ if (($response = $_impl->validate($data[$field], $parameters)) !== true) { if (!is_array($this->errors[$field])) { $this->errors[$field] = array(); } $this->errors[$field][] = $response; } } } } } if (!empty($this->errors)) { // Pass validation stuff to the controller MVCContext::getContext()->getController()->errors = $this->errors; } return empty($this->errors); }