src/Controller/LocationController.php line 154

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Location;
  4. use App\Entity\LocationReference;
  5. use App\Entity\Reference;
  6. use App\Form\LocationFormType;
  7. use App\Logic\SyntaxLogic;
  8. use App\Repository\LocationRepository;
  9. use App\Service\UploaderHelper;
  10. use Doctrine\ORM\EntityManagerInterface;
  11. use Knp\Component\Pager\PaginatorInterface;
  12. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\Routing\Annotation\Route;
  15. use Symfony\Component\Serializer\Encoder\JsonEncoder;
  16. use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer;
  17. use Symfony\Component\Serializer\Serializer;
  18. class LocationController extends BaseController
  19. {
  20.     /**
  21.      * @var LocationRepository
  22.      */
  23.     private $locationRepository;
  24.     public function __construct(LocationRepository $locationRepository)
  25.     {
  26.         $this->locationRepository $locationRepository;
  27.     }
  28.     /**
  29.      * @Route("/location", name="location")
  30.      */
  31.     public function index()
  32.     {
  33.         return $this->render('location/index.html.twig', [
  34.             'controller_name' => 'LocationController',
  35.         ]);
  36.     }
  37.     /**
  38.      * @Route("/location/list", name="locations")
  39.      */
  40.     public function list(Request $requestPaginatorInterface $paginator)
  41.     {
  42.         //$locationsSerialized = $this->locationRepository->getAll();
  43.        // $locations = $this->locationRepository->findAll();
  44.         $sort = ['field' => 'l.name''direction' => 'asc'];
  45.         $q $request->query->get('q');
  46.         //$queryBuilder = $this->locationRepository->getWithSearchQueryBuilder($this->getUser(), $q);
  47.        // $queryBuilder =  $this->locationRepository->filterByUser('l', $this->getUser());
  48.         $queryBuilder null;
  49.         $queryBuilder $this->locationRepository->addSearchQuery($queryBuilder'l'$qfalse);
  50.         $pagination $paginator->paginate(
  51.             $queryBuilder/* query NOT result */
  52.             $request->query->getInt('page'1),
  53.             100, [
  54.                 'defaultSortFieldName' => $sort['field'],
  55.                 'defaultSortDirection' => $sort['direction']
  56.             ]
  57.         );
  58.         $pagination->setCustomParameters([
  59.             'align' => 'left'# center|right (for template: twitter_bootstrap_v4_pagination and foundation_v6_pagination)
  60.             'size' => 'small'# small|large (for template: twitter_bootstrap_v4_pagination)
  61.             'style' => 'bottom',
  62.             'span_class' => 'whatever',
  63.         ]);
  64.         $pagination->setPageRange(1);
  65.         $locationsSerialized $this->locationRepository->serializeResults($queryBuilder);
  66.         return $this->render('location/list.html.twig', [
  67.             'locationsSerialized' => $locationsSerialized,
  68.             'pagination' => $pagination
  69.         ]);
  70.     }
  71.     /**
  72.      * @Route("/location/edit/{id}", name="location_edit")
  73.      * @IsGranted("ROLE_EDIT_ENTITY", subject="location")
  74.      */
  75.     public function edit(Location $locationRequest $requestEntityManagerInterface $emUploaderHelper $uploaderHelper)
  76.     {
  77.         $user $this->getUser();
  78.         $location $location->getUpdate($user);
  79.         if ($request->getMethod() === 'POST' && $request->get('location_form')) {
  80.             $cloneNeeded $location->isCloneNeeded($user);
  81.             if($cloneNeeded)
  82.             {
  83.                 $locationClone = clone $this->locationRepository->find($request->get('id'));
  84.                 $locationClone->setUpdateOf($location);
  85.                 $locationClone->setApproved(false);
  86.                 $locationClone->setOwner($user);
  87.                 $em->persist($locationClone);
  88.                 $em->flush();
  89.                 $location $locationClone;
  90.             }
  91.         }
  92.         $form $this->createForm(LocationFormType::class, $location, [
  93.             /* 'include_x' => true*/
  94.         ]);
  95.         $form->handleRequest($request);
  96.         if($form->isSubmitted() && $form->isValid()) {
  97.             /** @var UploadedFile $uploadedFile */
  98.             $uploadedFile =  $form['imageFile']->getData();
  99.             if($uploadedFile) {
  100.                 $newFilename $uploaderHelper->uploadPersonImage($uploadedFile$location->getImage());
  101.                 $location->setImage($newFilename);
  102.             }
  103.             $em->persist($location);
  104.             $em->flush();
  105.             $this->addFlash('success''Location updated');
  106.             return $this->redirectToRoute('location_edit', [
  107.                 'id' => $location->getId()
  108.             ]);
  109.         }
  110.         /*$addChildren = [];
  111.         $q = $request->query->get('q');
  112.         if(strlen($q) > 2) {
  113.             $addChildren = $this->locationRepositoryRepository->findAllPossibleChildren($location, $q);
  114.         }*/
  115.         return $this->render(
  116.             'location/edit.html.twig', [
  117.             'locationForm' => $form->createView(),
  118.             'location' => $location,
  119.         ]);
  120.     }
  121.     /**
  122.      * @Route("/location/{id}", name="location_show")
  123.      */
  124.     public function show(Location $location)
  125.     {
  126.         return $this->render(
  127.             'location/show.html.twig', [
  128.             'location' => $location,
  129.         ]);
  130.     }
  131.     /**
  132.      * @Route ("/location/recalc/latlng", name ="recalc_latlng")
  133.      * @IsGranted("ROLE_ADMIN")
  134.      */
  135.     public function recalcLatLng(EntityManagerInterface $emSyntaxLogic $syntaxLogic)
  136.     {
  137.         $syntaxLogic->latlngReader($em$this->getUser());
  138.     }
  139.     /**
  140.      * @Route("/location/remove_reference/{location}/{id}", name="location_remove_reference")
  141.      *  @IsGranted("ROLE_EDIT_ENTITY")
  142.      */
  143.     public function removeReference(Location $locationLocationReference $locationReference)
  144.     {
  145.         $location->removeLocationReference($locationReference);
  146.         $em $this->getDoctrine()->getManager();
  147.         $em->remove($locationReference);
  148.         $em->persist($location);
  149.         $em->flush();
  150.         return $this->redirectToRoute('location_edit', [
  151.             'id' => $location->getId()
  152.         ]);
  153.     }
  154.     /**
  155.      * @Route("/location/add_reference/{location}", name="location_add_reference")
  156.      * @IsGranted("ROLE_EDIT_ENTITY")
  157.      */
  158.     public function addReferenceAction(BibleBooksUtilityController $bibleBooksUtilityControllerRequest $requestLocation $location) {
  159.         $type $request->request->get('reftype');
  160.         $term $request->request->get('submit_param');
  161.         $valid $bibleBooksUtilityController->checkIsValid($term);
  162.         if($valid['fullReference']!="") {
  163.             $em $this->getDoctrine()->getManager();
  164.             $reference $em->getRepository("App:Reference")->findOneBy([
  165.                 'url' => $valid['fullReference']
  166.             ]);
  167.             if($reference == null) {
  168.                 $reference = new Reference();
  169.                 $reference->setUrl($valid['fullReference']);
  170.                 $reference->setIsBibleRef($valid['book']!="");
  171.                 $em->persist($reference);
  172.             }
  173.             $locationReference = new LocationReference();
  174.             $locationReference->setType($type);
  175.             $locationReference->setLocation($location);
  176.             $locationReference->setReference($reference);
  177.             $user $this->getUser();
  178.             if($user->acceptsChanges()) {
  179.                 $locationReference->setApproved(true);
  180.             } else {
  181.                 $locationReference->setApproved(false);
  182.                 $locationReference->setOwner($user);
  183.             }
  184.             $em->persist($locationReference);
  185.             $location->addLocationReference($locationReference);
  186.             $em->persist($location);
  187.             $em->flush();
  188.             $this->addFlash('success''Reference added');
  189.         }else {
  190.             $this->addFlash('error''Data not valid');
  191.         }
  192.         return $this->redirectToRoute('location_edit', [
  193.             'id' => $location->getId()
  194.         ]);
  195.     }
  196.     /**
  197.      * @Route ("/location/{id}/delete", name="location_delete")
  198.      * @IsGranted("ROLE_EDIT_ENTITY", subject="location")
  199.      */
  200.     public function deleteLocation(Location $locationRequest $request)
  201.     {
  202.         $user $this->getUser();
  203.         $accceptsChanges $user->acceptsChanges();
  204.         $deleteOwnUnaccepted = !$location->getApproved() && $location->getOwner() == $user;
  205.         if($accceptsChanges || $deleteOwnUnaccepted){
  206.             $location->PrepareDelete($user$this->em);
  207.             $this->em->persist($location);
  208.             $this->em->remove($location);
  209.         } else {
  210.             $location->setTaggedForDeleteBy($user);
  211.             $this->em->persist($location);
  212.         }
  213.         $this->em->flush();
  214.         return $this->redirectToRoute('locations', [
  215.         ]);
  216.     }
  217.     /**
  218.      * @Route("/location/utility/info", methods="GET", name="location_utility_info")
  219.      *
  220.      */
  221.     public function getLocationInfo(
  222.         LocationRepository $locationRepository,
  223.         Request $request
  224.     ){
  225.         $id1 $request->query->get('id1');
  226.         $id2 $request->query->get('id2');
  227.         $l = [];
  228.         $l1 $locationRepository->findOneBy(['id' => $id1]);
  229.         $l2 $locationRepository->findOneBy(['id' => $id2]);
  230.         $l[1] = (object)[
  231.             'lat' => $l1->getLat(),
  232.             'lon' => $l1->getLon(),        ];
  233.         $l[2] = (object)[
  234.             'lat' => $l2->getLat(),
  235.             'lon' => $l2->getLon(),        ];
  236.         return $this->json([
  237.             'locations' => json_encode($l)
  238.         ], 200, []
  239.         );
  240.     }
  241. }