Skip to content
This repository
Browse code

API CHANGE Moved RequestHandler->isAjax() to SS_HTTPRequest->isAjax()

  • Loading branch information...
commit a44b67bae2229acbcd374847615e1d1338c99f6c 1 parent 6f89fe0
Ingo Schommer authored April 05, 2012
2  admin/code/CMSBatchActionHandler.php
@@ -68,7 +68,7 @@ function Link() {
68 68
 
69 69
 	function handleAction($request) {
70 70
 		// This method can't be called without ajax.
71  
-		if(!$this->parentController->isAjax()) {
  71
+		if(!$request->isAjax()) {
72 72
 			$this->parentController->redirectBack();
73 73
 			return;
74 74
 		}
4  admin/code/LeftAndMain.php
@@ -193,7 +193,7 @@ function init() {
193 193
 		if(Director::redirected_to()) return;
194 194
 
195 195
 		// Audit logging hook
196  
-		if(empty($_REQUEST['executeForm']) && !$this->isAjax()) $this->extend('accessedCMS');
  196
+		if(empty($_REQUEST['executeForm']) && !$this->request->isAjax()) $this->extend('accessedCMS');
197 197
 		
198 198
 		// Set the members html editor config
199 199
 		HtmlEditorConfig::set_active(Member::currentUser()->getHtmlEditorConfigForCMS());
@@ -341,7 +341,7 @@ function handleRequest(SS_HTTPRequest $request, DataModel $model = null) {
341 341
 	}
342 342
 
343 343
 	function index($request) {
344  
-		return ($this->isAjax()) ? $this->show($request) : $this->getViewer('index')->process($this);
  344
+		return ($request->isAjax()) ? $this->show($request) : $this->getViewer('index')->process($this);
345 345
 	}
346 346
 
347 347
 	
11  control/Controller.php
@@ -536,17 +536,6 @@ function setSession(Session $session) {
536 536
 	}
537 537
 	
538 538
 	/**
539  
-	 * Returns true if this controller is processing an ajax request
540  
-	 * @return boolean True if this controller is processing an ajax request
541  
-	 */
542  
-	function isAjax() {
543  
-		return (
544  
-			isset($this->requestParams['ajax']) || isset($_REQUEST['ajax']) ||
545  
-			(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == "XMLHttpRequest")
546  
-		);
547  
-	}
548  
-	
549  
-	/**
550 539
 	 * Joins two or more link segments together, putting a slash between them if necessary.
551 540
 	 * Use this for building the results of {@link Link()} methods.
552 541
 	 * If either of the links have query strings, 
2  control/Director.php
@@ -682,7 +682,7 @@ static function forceWWW() {
682 682
 	 */
683 683
 	static function is_ajax() {
684 684
 		if(Controller::has_curr()) {
685  
-			return Controller::curr()->isAjax();
  685
+			return Controller::curr()->getRequest()->isAjax();
686 686
 		} else {
687 687
 			return (
688 688
 				isset($_REQUEST['ajax']) ||
14  control/HTTPRequest.php
@@ -232,6 +232,20 @@ function removeHeader($header) {
232 232
 	function getURL() {
233 233
 		return ($this->getExtension()) ? $this->url . '.' . $this->getExtension() : $this->url; 
234 234
 	}
  235
+
  236
+	/**
  237
+	 * Returns true if this request an ajax request,
  238
+	 * based on custom HTTP ajax added by common JavaScript libraries,
  239
+	 * or based on an explicit "ajax" request parameter.
  240
+	 * 
  241
+	 * @return boolean
  242
+	 */
  243
+	function isAjax() {
  244
+		return (
  245
+			$this->requestVar('ajax') ||
  246
+			$this->getHeader('X-Requested-With') && $this->getHeader('X-Requested-With') == "XMLHttpRequest"
  247
+		);
  248
+	}
235 249
 	
236 250
 	/**
237 251
 	 * Enables the existence of a key-value pair in the request to be checked using
53  control/RequestHandler.php
@@ -338,6 +338,59 @@ public function httpError($errorCode, $errorMessage = null) {
338 338
 
339 339
 		throw $e;
340 340
 	}
  341
+
  342
+	/**
  343
+	 * @deprecated 3.0 Use SS_HTTPRequest->isAjax() instead (through Controller->getRequest())
  344
+	 */
  345
+	function isAjax() {
  346
+		Deprecation::notice('3.0', 'Use SS_HTTPRequest->isAjax() instead (through Controller->getRequest())');
  347
+		return $this->request->isAjax();
  348
+	}
  349
+
  350
+	/**
  351
+	 * Handle the X-Get-Fragment header that AJAX responses may provide, returning the 
  352
+	 * fragment, or, in the case of non-AJAX form submissions, redirecting back to the submitter.
  353
+	 *
  354
+	 * X-Get-Fragment ensures that users won't end up seeing the unstyled form HTML in their browser
  355
+	 * If a JS error prevents the Ajax overriding of form submissions from happening. It also provides
  356
+	 * better non-JS operation.
  357
+	 * 
  358
+	 * Out of the box, the handler "CurrentForm" value, which will return the rendered form.  Non-Ajax
  359
+	 * calls will redirect back.
  360
+	 * 
  361
+	 * To extend its responses, pass a map to the $options argument.  Each key is the value of X-Get-Fragment
  362
+	 * that will work, and the value is a PHP 'callable' value that will return the response for that
  363
+	 * value.
  364
+	 * 
  365
+	 * If you specify $options['default'], this will be used as the non-ajax response.
  366
+	 * 
  367
+	 * Note that if you use handleFragmentResponse, then any Ajax requests will have to include X-Get-Fragment
  368
+	 * or an error will be thrown.
  369
+	 */
  370
+	function handleFragmentResponse($form, $options = array()) {
  371
+		// Prepare the default options and combine with the others
  372
+		$lOptions = array(
  373
+			'currentform' => array($form, 'forTemplate'),
  374
+			'default' => array('Director', 'redirectBack'),
  375
+		);
  376
+		if($options) foreach($options as $k => $v) {
  377
+			$lOptions[strtolower($k)] = $v;
  378
+		}
  379
+		
  380
+		if($fragment = $this->request->getHeader('X-Get-Fragment')) {
  381
+			$fragment = strtolower($fragment);
  382
+			if(isset($lOptions[$fragment])) {
  383
+				return call_user_func($lOptions[$fragment]);
  384
+			} else {
  385
+				throw new SS_HTTPResponse_Exception("X-Get-Fragment = '$fragment' not supported for this URL.", 400);
  386
+			}
  387
+			
  388
+		} else {
  389
+			if($this->isAjax()) throw new SS_HTTPResponse_Exception("Ajax requests to this URL require an X-Get-Fragment header.", 400);
  390
+			return call_user_func($lOptions['default']);
  391
+		}
  392
+		
  393
+	}
341 394
 	
342 395
 	/**
343 396
 	 * Returns the SS_HTTPRequest object that this controller is using.
2  forms/gridfield/GridFieldDetailForm.php
@@ -196,7 +196,7 @@ function edit($request) {
196 196
 			'ItemEditForm' => $form,
197 197
 		))->renderWith($this->template);
198 198
 
199  
-		if($controller->isAjax()) {
  199
+		if($request->isAjax()) {
200 200
 			return $return;	
201 201
 		} else {
202 202
 			// If not requested by ajax, we need to render it within the controller context+template
12  tests/control/HTTPRequestTest.php
@@ -230,4 +230,16 @@ public function testRequestVars() {
230 230
 			'Nested GET parameters should supplement POST parameters'
231 231
 		);
232 232
 	}
  233
+
  234
+	function testIsAjax() {
  235
+		$req = new SS_HTTPRequest('GET', '/', array('ajax' => 0));
  236
+		$this->assertFalse($req->isAjax());
  237
+
  238
+		$req = new SS_HTTPRequest('GET', '/', array('ajax' => 1));
  239
+		$this->assertTrue($req->isAjax());
  240
+
  241
+		$req = new SS_HTTPRequest('GET', '/');
  242
+		$req->addHeader('X-Requested-With', 'XMLHttpRequest');
  243
+		$this->assertTrue($req->isAjax());
  244
+	}
233 245
 }

0 notes on commit a44b67b

Please sign in to comment.
Something went wrong with that request. Please try again.