/** * Out of the box, the handler "CurrentForm" value, which will return the rendered form. * Non-Ajax calls will redirect back. * * @param HTTPRequest $request * @param array $extraCallbacks List of anonymous functions or callables returning either a string * or HTTPResponse, keyed by their fragment identifier. The 'default' key can * be used as a fallback for non-ajax responses. * @return HTTPResponse * @throws HTTPResponse_Exception */ public function respond(HTTPRequest $request, $extraCallbacks = array()) { // Prepare the default options and combine with the others $callbacks = array_merge($this->callbacks, $extraCallbacks); $response = $this->getResponse(); $responseParts = array(); if (isset($this->fragmentOverride)) { $fragments = $this->fragmentOverride; } elseif ($fragmentStr = $request->getHeader('X-Pjax')) { $fragments = explode(',', $fragmentStr); } else { if ($request->isAjax()) { throw new HTTPResponse_Exception("Ajax requests to this URL require an X-Pjax header.", 400); } elseif (empty($callbacks['default'])) { throw new HTTPResponse_Exception("Missing default response handler for this URL", 400); } $response->setBody(call_user_func($callbacks['default'])); return $response; } // Execute the fragment callbacks and build the response. foreach ($fragments as $fragment) { if (isset($callbacks[$fragment])) { $res = call_user_func($callbacks[$fragment]); $responseParts[$fragment] = $res ? (string) $res : $res; } else { throw new HTTPResponse_Exception("X-Pjax = '{$fragment}' not supported for this URL.", 400); } } $response->setBody(Convert::raw2json($responseParts)); $response->addHeader('Content-Type', 'text/json'); return $response; }
public function testIsAjax() { $req = new HTTPRequest('GET', '/', array('ajax' => 0)); $this->assertFalse($req->isAjax()); $req = new HTTPRequest('GET', '/', array('ajax' => 1)); $this->assertTrue($req->isAjax()); $req = new HTTPRequest('GET', '/'); $req->addHeader('X-Requested-With', 'XMLHttpRequest'); $this->assertTrue($req->isAjax()); }
/** * Invoke a batch action * * @param HTTPRequest $request * @return HTTPResponse */ public function handleBatchAction($request) { // This method can't be called without ajax. if (!$request->isAjax()) { return $this->parentController->redirectBack(); } // Protect against CSRF on destructive action if (!SecurityToken::inst()->checkRequest($request)) { return $this->httpError(400); } // Find the action handler $action = $request->param('BatchAction'); $actionHandler = $this->actionByName($action); // Sanitise ID list and query the database for apges $csvIDs = $request->requestVar('csvIDs'); $ids = $this->cleanIDs($csvIDs); // Filter ids by those which are applicable to this action // Enforces front end filter in LeftAndMain.BatchActions.js:refreshSelected $ids = $actionHandler->applicablePages($ids); // Query ids and pass to action to process $pages = $this->getPages($ids); return $actionHandler->run($pages); }
/** * @param HTTPRequest $request * @return mixed */ public function edit($request) { $controller = $this->getToplevelController(); $form = $this->ItemEditForm(); $return = $this->customise(array('Backlink' => $controller->hasMethod('Backlink') ? $controller->Backlink() : $controller->Link(), 'ItemEditForm' => $form))->renderWith($this->getTemplates()); if ($request->isAjax()) { return $return; } else { // If not requested by ajax, we need to render it within the controller context+template return $controller->customise(array('Content' => $return)); } }