src/Application/Controller/HubsController.php line 191

Open in your IDE?
  1. <?php
  2. namespace App\Application\Controller;
  3. use App\Admin\Document\Chain;
  4. use App\Admin\Document\Hub;
  5. use App\Admin\Document\Review;
  6. use AvenueAdminBundle\Filter\MongoDB\StringFilter;
  7. use AvenueAdminBundle\Util\FileUtil;
  8. use Symfony\Component\HttpFoundation\JsonResponse;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\HttpFoundation\Response;
  11. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  12. use \Symfony\Component\Routing\Annotation\Route;
  13. class HubsController extends AbstractController
  14. {
  15.     const TOP_CHAINS_LIMIT 5;
  16.     /**
  17.      * @Route("/hubs/", name="hubs")
  18.      */
  19.     public function index(): Response
  20.     {
  21.         return $this->getHubsView();
  22.     }
  23.     private function getHubsView($openHubId null) {
  24.         $hubs $this->getHubs();
  25.         return $this->render(
  26.             'application/hubs/index.html.twig', [
  27.                 'topChains' => $this->getDocumentRepository(Chain::class)->findBy(['active' => true],
  28.                     ['hubCount' => 'desc'], self::TOP_CHAINS_LIMIT),
  29.                 'showMoreChains' => $this->getQueryBuilder(Chain::class)->field('active')->equals(true)
  30.                         ->count()->getQuery()->execute() > self::TOP_CHAINS_LIMIT,
  31.                 'hubs' => $hubs,
  32.                 'hubPoints' => $this->getHubPoints($hubs),
  33.                 'ratings' => Review::RATINGS,
  34.                 'openHubId' => $openHubId
  35.             ]
  36.         );
  37.     }
  38.     /**
  39.      * @Route("/hubs/{id}", name="hubs-hub")
  40.      */
  41.     public function hub($id) {
  42.         return $this->getHubsView($id);
  43.     }
  44.     /**
  45.      * @Route("/select-chain/", name="select-chain")
  46.      */
  47.     public function selectChain(Request $request): Response
  48.     {
  49.         /**
  50.          * @var Chain $chain
  51.          */
  52.         $chain $this->getDocumentRepository(Chain::class)->find($request->get('id'));
  53.         if ($chain) {
  54.             $hubs = [];
  55.             foreach ($chain->getHubs() as $hub) {
  56.                 if ($hub->getActive()) {
  57.                     $hubs[] = $hub;
  58.                 }
  59.             }
  60.             usort($hubs, function (Hub $aHub $b) {
  61.                 return $a->getName() > $b->getName() ? 1
  62.                     : ($a->getName() < $b->getName() ? -0);
  63.             });
  64.         } else {
  65.             $hubs $this->getHubs();
  66.         }
  67.         return new JsonResponse([
  68.             'hubPoints' => $this->getHubPoints($hubs),
  69.             'html' => $this->renderView(
  70.                 'application/hubs/hub-table.html.twig', [
  71.                     'hubs' => $hubs,
  72.                 ]
  73.             )
  74.         ]);
  75.     }
  76.     private function getHubFromRequest(Request $request): Hub {
  77.         $id $request->get('id');
  78.         $hub $this->getDocumentRepository(Hub::class)->find($id);
  79.         if (!$hub) {
  80.             throw new \Exception('no hub with id: ' $id);
  81.         }
  82.         return $hub;
  83.     }
  84.     /**
  85.      * @Route("/hub-panel/", name="hub-panel")
  86.      */
  87.     public function hubPanel(Request $request): Response
  88.     {
  89.         $hub $this->getHubFromRequest($request);
  90.         return $this->render(
  91.             'application/hubs/hub-panel.html.twig', [
  92.                 'hub' => $hub
  93.             ]
  94.         );
  95.     }
  96.     /**
  97.      * @Route("/hub-search/", name="hub-search")
  98.      */
  99.     public function hubSearch(Request $request): Response
  100.     {
  101.         $text $request->get('text');
  102.         $hubs = [];
  103.         foreach (array_merge($this->getHubs('name'$text), $this->getHubs('address'$text)) as $hub) {
  104.             if (!isset($hubs[$hub->getId()])) {
  105.                 $hubs[$hub->getId()] = $hub;
  106.             }
  107.         }
  108.         usort($hubs, function (Hub $aHub $b) {
  109.             return $a->getName() > $b->getName() ? 1
  110.                 : ($a->getName() < $b->getName() ? -0);
  111.         });
  112.         return new JsonResponse([
  113.             'hubPoints' => $this->getHubPoints($hubs),
  114.             'html' => $this->renderView(
  115.                 'application/hubs/hub-table.html.twig', [
  116.                     'hubs' => $hubs,
  117.                 ]
  118.             )
  119.         ]);
  120.     }
  121.     /**
  122.      * @Route("/hub-search-chain/", name="hub-search-chain")
  123.      */
  124.     public function hubSearchChain(Request $request): Response
  125.     {
  126.         $text $request->get('text');
  127.         $items = [];
  128.         /**
  129.          * @var Hub $hub
  130.          */
  131.         foreach ($this->getHubs('name'$text) as $hub) {
  132.             $items[$hub->getName()] = [
  133.                 'name' => $hub->getName(),
  134.                 'id' => null,
  135.                 'image' => $hub->getChain() ? $hub->getChain()->getImage() : null,
  136.             ];;
  137.         }
  138.         $qb $this->getQueryBuilder(Chain::class);
  139.         $qb->field('active')->equals(true);
  140.         $qb->sort('hubCount''desc');
  141.         $qb->limit(10);
  142.         $filter = new StringFilter();
  143.         $filter->filter($qb'name'$request->get('text'));
  144.         /**
  145.          * @var Chain $chain
  146.          */
  147.         foreach ($qb->getQuery()->execute() as $chain) {
  148.             $items[$chain->getName()] = [
  149.                 'name' => $chain->getName(),
  150.                 'id' => $chain->getId(),
  151.                 'image' => $chain->getImage(),
  152.             ];
  153.         }
  154.         ksort($items);
  155.         return $this->render(
  156.             'application/hubs/search-result.html.twig', [
  157.                 'items' => array_values($items)
  158.             ]
  159.         );
  160.     }
  161.     private function getHubs($field null$text null) {
  162.         $qb $this->getQueryBuilder(Hub::class);
  163.         $qb->field('active')->equals(true);
  164.         $qb->sort('name');
  165.         if ($field && $text) {
  166.             $filter = new StringFilter();
  167.             $filter->filter($qb$field$text);
  168.             $qb->limit(50);
  169.         }
  170.         return $qb->getQuery()->toArray();
  171.     }
  172.     /**
  173.      * @param Hub[] $hubs
  174.      */
  175.     private function getHubPoints($hubs) {
  176.         $result = [];
  177.         foreach ($hubs as $hub) {
  178.             $result[] = [
  179.                 'id' => $hub->getId(),
  180.                 'name' => $hub->getName(),
  181.                 'coordinates' => $hub->getCoordinatesAsArray(),
  182.                 'icon' => $hub->getChain() && $hub->getChain()->getImage() ? '/' $hub->getChain()->getImage() : null
  183.             ];
  184.         }
  185.         return $result;
  186.     }
  187.     /**
  188.      * @Route("/hubs-add-to-favorites/", name="hubs-add-to-favorites")
  189.      */
  190.     public function addToFavorites(Request $request) {
  191.         if ($this->getLoggedInUser()->getPerson()) {
  192.             $hub $this->getHubFromRequest($request);
  193.             $this->getLoggedInUser()->getPerson()->addFavoriteHub($hub);
  194.             $this->getDocumentManager()->flush();
  195.         }
  196.         return new JsonResponse();
  197.     }
  198.     /**
  199.      * @Route("/hubs-delete-from-favorites/", name="hubs-delete-from-favorites")
  200.      */
  201.     public function deleteFromFavorites(Request $request) {
  202.         if ($this->getLoggedInUser()->getPerson()) {
  203.             $hub $this->getHubFromRequest($request);
  204.             $this->getLoggedInUser()->getPerson()->deleteFavoriteHub($hub);
  205.             $this->getDocumentManager()->flush();
  206.         }
  207.         return new JsonResponse();
  208.     }
  209. }