diff --git a/core/control/Controller.php b/core/control/Controller.php index fa04a6b3c..78dc43439 100644 --- a/core/control/Controller.php +++ b/core/control/Controller.php @@ -284,6 +284,13 @@ class Controller extends ViewableData { user_error("No current controller available", E_USER_WARNING); } } + + /** + * Tests whether we have a currently active controller or not + */ + public static function has_curr() { + return Controller::$controller_stack ? true : false; + } /** * Returns true if the member is allowed to do the given action. @@ -395,6 +402,16 @@ class Controller extends ViewableData { $this->session = $session; } + /** + * Returns true if this controller is processing an ajax request + */ + function isAjax() { + return ( + isset($this->requestParams['ajax']) || + (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == "XMLHttpRequest") + ); + } + } ?> diff --git a/core/control/Director.php b/core/control/Director.php index a277aba11..4e1ad663a 100644 --- a/core/control/Director.php +++ b/core/control/Director.php @@ -54,12 +54,39 @@ class Director { // Save the updated session back $_SESSION = $controllerObj->getSession()->inst_getAll(); - $response->output(); } if(isset($_GET['debug_profile'])) Profiler::unmark("Director","direct"); } + + /** + * Test a URL request, returning a response object. + * @param $url The URL to visit + * @param $post The $_POST & $_FILES variables + * @param $session The {@link Session} object representing the current session. + */ + function test($url, $post = null, $session = null) { + $getVars = array(); + if(strpos($url,'?') !== false) { + list($url, $getVarsEncoded) = explode('?', $url, 2); + parse_str($getVarsEncoded, $getVars); + } + + $controllerObj = Director::getControllerForURL($url); + + // Load the session into the controller + $controllerObj->setSession($session ? $session : new Session(null)); + + if(is_string($controllerObj) && substr($controllerObj,0,9) == 'redirect:') { + user_error("Redirection not implemented in Director::test", E_USER_ERROR); + + } else if($controllerObj) { + $response = $controllerObj->run( array_merge($getVars, (array)$post) ); + return $response; + } + } + static function getControllerForURL($url) { if(isset($_GET['debug_profile'])) Profiler::mark("Director","getControllerForURL"); @@ -289,10 +316,14 @@ class Director { * @return boolean */ static function is_ajax() { - return ( - isset($_REQUEST['ajax']) || - (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == "XMLHttpRequest") - ); + if(Controller::has_curr()) { + return Controller::curr()->isAjax(); + } else { + return ( + isset($_REQUEST['ajax']) || + (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == "XMLHttpRequest") + ); + } } diff --git a/forms/Form.php b/forms/Form.php index 798130181..e82cf497f 100644 --- a/forms/Form.php +++ b/forms/Form.php @@ -645,9 +645,11 @@ class Form extends ViewableData { function testSubmission($action, $data) { $data['action_' . $action] = true; $data['executeForm'] = $this->name; + + return Director::test($this->FormAction(), $data, Controller::curr()->getSession()); - $response = $this->controller->run($data); - return $response; + //$response = $this->controller->run($data); + //return $response; } function testAjaxSubmission($action, $data) {