44 private $response_sent =
false;
49 private $allowed_views = [];
60 $this->hooks = $hooks;
62 $this->request = $request;
65 $message_filter = [$this,
'prepareResponse'];
75 $version = $this->request->headers->get(
'X-Elgg-Ajax-API');
99 if (!is_string($string)) {
102 $object = json_decode($string);
121 $api_response->setData(
$output);
123 $api_response->setData((
object)
$output);
125 $api_response->setData((
object) [
'value' =>
$output]);
127 $api_response = $this->filterApiResponse($api_response, $hook_type);
128 $response = $this->buildHttpResponse($api_response);
130 $this->response_sent =
true;
142 $api_response = $this->filterApiResponse($api_response, $hook_type);
143 $response = $this->buildHttpResponse($api_response);
145 $this->response_sent =
true;
157 $response =
new JsonResponse([
'error' => $msg], $status);
160 $this->msgs->dumpRegister();
162 $this->response_sent =
true;
174 private function filterApiResponse(
AjaxResponse $api_response, $hook_type =
'') {
175 $api_response->
setTtl($this->request->getParam(
'elgg_response_ttl', 0,
false));
179 $api_response = $this->hooks->trigger($hook, $hook_type, null, $api_response);
181 throw new RuntimeException(
"The value returned by hook [$hook, $hook_type] was not an ApiResponse");
185 return $api_response;
197 private function buildHttpResponse(
AjaxResponse $api_response, $allow_removing_headers = null) {
199 return new JsonResponse([
'error' =>
"The response was cancelled"], 400);
204 $ttl = $api_response->
getTtl();
207 if ($allow_removing_headers) {
208 header_remove(
'Expires');
209 header_remove(
'Pragma');
210 header_remove(
'Cache-Control');
214 $response->headers->remove(
'Cache-Control');
216 $response->setClientTtl($ttl);
219 $response->headers->set(
'Expires', gmdate(
'D, d M Y H:i:s \G\M\T', time() + $ttl));
241 if ($this->request->getParam(
'elgg_fetch_messages',
true)) {
242 $response->getData()->_elgg_msgs = (object) $this->msgs->dumpRegister();
245 if ($this->request->getParam(
'elgg_fetch_deps',
true)) {
246 $response->getData()->_elgg_deps = (array) $this->amd_config->getDependencies();
259 $this->allowed_views[
$view] =
true;
269 unset($this->allowed_views[
$view]);
277 return array_keys($this->allowed_views);
isCancelled()
Has the response been cancelled?
if(!$item instanceof ElggRiverItem) $object
$params
Saves global plugin settings.
unregisterView($view)
Unregister a view for ajax calls.
setTtl($ttl=0)
Set the max-age for client caching.
isAjax2Request()
Did the request come from the elgg/Ajax module?
catch(LoginException $e) if($request->isXhr()) $output
Models the Ajax API service.
registerView($view)
Register a view to be available for ajax calls.
if(!$owner||!$owner->canEdit()) if(!$owner->hasIcon('master')) if(!$owner->saveIconFromElggFile($owner->getIcon('master'), 'icon', $coords)) $view
decodeJson($string)
Attempt to JSON decode the given string.
respondFromOutput($output, $hook_type= '', $try_decode=true)
Send a JSON HTTP response with the given output.
respondFromApiResponse(AjaxResponse $api_response, $hook_type= '')
Send a JSON HTTP response based on the given API response.
prepareResponse($hook, $type, $response, $params)
Prepare the response with additional metadata, like system messages and required AMD modules...
getData()
Get the response data, which will be a stdClass object with property "value".
isReady()
Is the service ready to respond to the request?
__construct(PluginHooksService $hooks, SystemMessagesService $msgs, Request $request, Config $amdConfig)
Constructor.
_elgg_services()
Get the global service provider.
getTtl()
Get the max-age for client caching.
getViews()
Returns an array of views allowed for ajax calls.
respondWithError($msg= '', $status=400)
Send a JSON HTTP 400 response.