/** * Returns the rendered subtree of each top-level toolbar link. * * @return \Symfony\Component\HttpFoundation\JsonResponse */ public function subtreesJsonp() { $subtrees = toolbar_get_rendered_subtrees(); $response = new JsonResponse($subtrees); $response->setCallback('Drupal.toolbar.setSubtrees.resolve'); // The Expires HTTP header is the heart of the client-side HTTP caching. The // additional server-side page cache only takes effect when the client // accesses the callback URL again (e.g., after clearing the browser cache // or when force-reloading a Drupal page). $max_age = 365 * 24 * 60 * 60; $response->setPrivate(); $response->setMaxAge($max_age); $expires = new \DateTime(); $expires->setTimestamp(REQUEST_TIME + $max_age); $response->setExpires($expires); return $response; }
/** * * @Route("/{_locale}/{_code}/statistics/{dataset}.json", requirements={"_locale" = "de|en|es|fr", "_code" = "[a-zA-Z0-9]{10}", "dataset" = "[a-zA-Z0-9_-]+"}) */ public function statisticsJsonAction($_code, $dataset, Request $request) { try { $context = $this->getContext($_code, 'statistics'); $stats = new Statistics($this, $this->account); if (preg_match('!^wallet-([0-9]+)$!', $dataset, $m)) { $method = 'getDatasetWallet'; if (method_exists($stats, $method)) { $data = $stats->{$method}($m[1]); if (!empty($data)) { $response = new JsonResponse($data); } } } else { $method = 'getDataset' . $dataset; if (method_exists($stats, $method)) { $response = new JsonResponse($stats->{$method}()); } } if (isset($response)) { $response->setMaxAge(900); $response->setExpires(new \DateTime('@' . (time() + 900))); $response->setPublic(); return $response; } } catch (\Exception $ex) { return new JsonResponse(['error' => $ex->getMessage()]); } }
public function ajaxGetLastMessageAction() { $this->onlyAjaxRequest(); $repo = $this->get('social.private_message.repository'); /* @var $lastPm \Trismegiste\Socialist\PrivateMessage */ $lastPm = $repo->getLastReceived(); $lastUpdate = is_null($lastPm) ? null : $lastPm->getSentAt(); $response = new JsonResponse(['lastUpdate' => $lastUpdate]); $response->setMaxAge(60); return $response; }
public function getListAction(Application $app) { $userList = $this->repository->findAll(); $date = new \DateTime(); $date->modify('+' . self::MAX_AGE . ' seconds'); $response = new JsonResponse($userList, JsonResponse::HTTP_OK); $responseHash = sha1($response->getContent()); $response->setMaxAge(self::MAX_AGE); $response->setSharedMaxAge(self::MAX_AGE); $response->setExpires($date); $response->setETag($responseHash); $response->isNotModified($app['request']); return $response; }
public function rateAction(Request $request) { // Retreive and normalise query parameters // Epic security here, this would obviously be the current logged in user's ID $user_id = $request->query->get('user_id'); $id = $request->query->get('comment_id'); $vote = $request->query->get('vote'); switch ($vote) { case 'up': $value = 1; break; case 'down': $value = -1; break; default: $value = false; } $makeResponse = function ($data, $statusCode = 200) use($user_id, $id, $vote) { $output = $this->presentResponse($data, 'comment_rating_add', array('vote' => $vote, 'user_id' => $user_id, 'comment_id' => $id)); $resp = new JsonResponse($output, $statusCode); $resp->setPrivate(); $resp->setMaxAge(0); return $resp; }; // Make sure the vote is valid if ($value === false) { return $makeResponse('Param "vote" invalid. Expected "up" or "down"', Response::HTTP_BAD_REQUEST); } // Make sure it's a valid comment we're voting on $comment = $this->getCommentById($id); if (!$comment) { return $makeResponse('Comment not found for id ' . $id, Response::HTTP_NOT_FOUND); } // Make sure they haven't already voted on the comment $ratingRepo = $this->getDoctrine()->getRepository('AppBundle:CommentRating'); $rating = $ratingRepo->findBy(array('user_id' => $user_id)); if (!!$rating) { return $makeResponse('User has already submitted rating', Response::HTTP_BAD_REQUEST); } try { // If we made it here, everything looks good $result = $ratingRepo->addNewVote(array('comment_id' => $id, 'value' => $value, 'user_id' => $user_id)); } catch (Exception $e) { return $makeResponse('Failed to save vote', Response::HTTP_INTERNAL_SERVER_ERROR); } return $makeResponse('Vote Added'); }