Exemplo n.º 1
8
 /**
  * The search action first render an empty page, if the query is set, then the template generates
  * some ajax request to retrieve results for each admin. The Ajax query returns a JSON response.
  *
  * @param Request $request
  *
  * @return JsonResponse|Response
  *
  * @throws \RuntimeException
  */
 public function searchAction(Request $request)
 {
     if ($request->get('admin') && $request->isXmlHttpRequest()) {
         try {
             $admin = $this->getAdminPool()->getAdminByAdminCode($request->get('admin'));
         } catch (ServiceNotFoundException $e) {
             throw new \RuntimeException('Unable to find the Admin instance', $e->getCode(), $e);
         }
         if (!$admin instanceof AdminInterface) {
             throw new \RuntimeException('The requested service is not an Admin instance');
         }
         $handler = $this->getSearchHandler();
         $results = array();
         if ($pager = $handler->search($admin, $request->get('q'), $request->get('page'), $request->get('offset'))) {
             foreach ($pager->getResults() as $result) {
                 $results[] = array('label' => $admin->toString($result), 'link' => $admin->generateObjectUrl('edit', $result), 'id' => $admin->id($result));
             }
         }
         $response = new JsonResponse(array('results' => $results, 'page' => $pager ? (int) $pager->getPage() : false, 'total' => $pager ? (int) $pager->getNbResults() : false));
         $response->setPrivate();
         return $response;
     }
     return $this->render($this->container->get('sonata.admin.pool')->getTemplate('search'), array('base_template' => $this->getBaseTemplate(), 'breadcrumbs_builder' => $this->get('sonata.admin.breadcrumbs_builder'), 'admin_pool' => $this->container->get('sonata.admin.pool'), 'query' => $request->get('q'), 'groups' => $this->getAdminPool()->getDashboardGroups()));
 }
Exemplo n.º 2
1
 /**
  * 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;
 }
 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');
 }