src/Controller/Front/AnimalController.php line 292

  1. <?php
  2. namespace App\Controller\Front;
  3. use App\Entity\Animal;
  4. use App\Entity\AnimalPoll;
  5. use App\Entity\AnimalPollStatus;
  6. use App\Entity\AnimalSearch;
  7. use App\Entity\BlogCategory;
  8. use App\Entity\BlogNote;
  9. use App\Entity\KidAcceptance;
  10. use App\Entity\LifeStyle;
  11. use App\Entity\NotAccepting;
  12. use App\Entity\PreferableLive;
  13. use App\Entity\Rotator;
  14. use App\Entity\RotatorPlace;
  15. use App\Entity\RotatorType;
  16. use App\Entity\Sex;
  17. use App\Entity\Species;
  18. use App\Entity\TimeCareNeeded;
  19. use App\Repository\AnimalPollRepository;
  20. use App\Repository\AnimalPollStatusRepository;
  21. use App\Repository\AnimalRepository;
  22. use App\Repository\AnimalSearchRepository;
  23. use App\Repository\BlogCategoryRepository;
  24. use App\Repository\BlogNoteRepository;
  25. use App\Repository\KidAcceptanceRepository;
  26. use App\Repository\LifeStyleRepository;
  27. use App\Repository\PreferableLiveRepository;
  28. use App\Repository\RotatorRepository;
  29. use App\Repository\SpeciesRepository;
  30. use App\Repository\TimeCareNeededRepository;
  31. use App\Service\AdoptionPollService;
  32. use App\Service\AnimalService;
  33. use App\Service\ImageService;
  34. use Doctrine\Persistence\ManagerRegistry;
  35. use Knp\Component\Pager\PaginatorInterface;
  36. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  37. use Symfony\Component\HttpFoundation\Cookie;
  38. use Symfony\Component\HttpFoundation\JsonResponse;
  39. use Symfony\Component\HttpFoundation\Request;
  40. use Symfony\Component\HttpFoundation\Response;
  41. use Symfony\Component\Routing\Annotation\Route;
  42. use Symfony\Component\Routing\Generator\UrlGenerator;
  43. use Symfony\Component\Serializer\Encoder\JsonEncoder;
  44. use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
  45. use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
  46. use Symfony\Component\Serializer\Serializer;
  47. class AnimalController extends AbstractController
  48. {
  49.     const
  50.         ANIMAL_SEARCH_COOKIE 'animalSearch',
  51.         ANIMAL_SEARCH_SEED 'animalOrderSeed';
  52.     private $_aTranslate = [
  53.         'pies' => Species::TYPE_DOG,
  54.         'kot' => Species::TYPE_CAT,
  55.         'samica' => 'Żeńska',
  56.         'samiec' => 'Męska',
  57.         'maly' => 'Mały',
  58.         'sredni' => 'Średni',
  59.         'duzy' => 'Duży',
  60.         'kanapowiec' => 'home',
  61.         'srednio-aktywny' => 'middle-active',
  62.         'bardzo-aktywny' => 'very-active',
  63.         'od-0-do-4-lat' => 'to5',
  64.         'od-0-do-5-lat' => 'to5',
  65.         'od-5-do-10-lat' => 'to10',
  66.         'powyzej-10-lat' => 'over10',
  67.         'do-6-godzin' => 'to6',
  68.         'zawsze-ktos-jest-w-domu' => 'always',
  69.         'powyzej-6-godzin' => 'over6',
  70.         'ponad-6-godzin' => 'over6',
  71.     ];
  72.     #[Route('/adopciaki/wszystkie/{page}''animals_all')]
  73.     public function allAnimalsAction(Request $requestManagerRegistry $doctrinePaginatorInterface $paginator$page 1)
  74.     {
  75.         $searchCriteria = [
  76.             'isPublished' => true,
  77.         ];
  78.         $animalsQb $doctrine->getRepository(Animal::class)->findAllAdvancedSearchQb(
  79.             $searchCriteria,
  80.             false,
  81.             $this->getOrderSeed($request)
  82.         );
  83.         if($request->isXmlHttpRequest()) {
  84.             $page $request->query->get('page');
  85.             return $this->render(
  86.                 'front/animal/list.html.twig',
  87.                 [
  88.                     'animals' => $paginator->paginate($animalsQb$page20)
  89.                 ]
  90.             );
  91.         }
  92.         return $this->render(
  93.                 'front/animal/index.html.twig',
  94.             [
  95.                 'animals' => $paginator->paginate($animalsQb$page20)
  96.             ]
  97.         );
  98.     }
  99.     #[Route('/adopciaki/szukaj/{page}')]
  100.     public function searchAction(Request $requestManagerRegistry $doctrinePaginatorInterface $paginator$page 1)
  101.     {
  102.         $search $request->get('search');
  103.         $animalsQb $doctrine->getRepository(Animal::class)->searchByName($search);
  104.         if($request->isXmlHttpRequest()) {
  105.             $page $request->query->get('page');
  106.             return $this->render(
  107.                 'front/animal/list.html.twig',
  108.                 [
  109.                     'animals' => $paginator->paginate($animalsQb$page20)
  110.                 ]
  111.             );
  112.         }
  113.         return $this->render(
  114.                 'front/animal/index.html.twig',
  115.             [
  116.                 'animals' => $paginator->paginate($animalsQb$page20)
  117.             ]
  118.         );
  119.     }
  120.     #[Route('/adopciaki/adoptowane/{page}')]
  121.     public function adoptedAction(Request $requestManagerRegistry $doctrinePaginatorInterface $paginator$page 1)
  122.     {
  123.         $animalsQb $doctrine->getRepository(Animal::class)->findAllAdvancedSearchQb([], true$this->getOrderSeed($request));
  124.         if($request->isXmlHttpRequest()) {
  125.             $page $request->query->get('page');
  126.             return $this->render(
  127.                 'front/animal/list.html.twig',
  128.                 [
  129.                     'animals' => $paginator->paginate($animalsQb$page20)
  130.                 ]
  131.             );
  132.         }
  133.         return $this->render(
  134.                 'front/animal/index.html.twig',
  135.             [
  136.                 'animals' => $paginator->paginate($animalsQb$page20)
  137.                 //'searchForm' => $searchForm->createView(),
  138.             ]
  139.         );
  140.     }
  141.     #[Route('/adopciaki/koty/{page}')]
  142.     public function allCatsAction(Request $requestManagerRegistry $doctrinePaginatorInterface $paginator$page 1)
  143.     {
  144.         $searchCriteria = [
  145.             'species' => 2,
  146.             'isPublished' => true,
  147.         ];
  148.         $animalsQb $doctrine->getRepository(Animal::class)->findAllAdvancedSearchQb(
  149.             $searchCriteria,
  150.             false,
  151.             $this->getOrderSeed($request)
  152.         );
  153.         if($request->isXmlHttpRequest()) {
  154.             $page $request->query->get('page');
  155.             return $this->render(
  156.                 'front/animal/list.html.twig',
  157.                 [
  158.                     'animals' => $paginator->paginate($animalsQb$page20)
  159.                 ]
  160.             );
  161.         }
  162.         return $this->render(
  163.                 'front/animal/index.html.twig',
  164.             [
  165.                 'animals' => $paginator->paginate($animalsQb$page20)
  166.             ]
  167.         );
  168.     }
  169.     #[Route('/adopciaki/psy/{page}')]
  170.     public function allDogsAction(Request $requestManagerRegistry $doctrinePaginatorInterface $paginator$page 1)
  171.     {
  172.         $searchCriteria = [
  173.             'species' => 1,
  174.             'isPublished' => true,
  175.         ];
  176.         $animalsQb $doctrine->getRepository(Animal::class)->findAllAdvancedSearchQb(
  177.             $searchCriteria,
  178.             false,
  179.             $this->getOrderSeed($request)
  180.         );
  181.         if($request->isXmlHttpRequest()) {
  182.             $page $request->query->get('page');
  183.             return $this->render(
  184.                 'front/animal/list.html.twig',
  185.                 [
  186.                     'animals' => $paginator->paginate($animalsQb$page20)
  187.                 ]
  188.             );
  189.         }
  190.         return $this->render(
  191.                 'front/animal/index.html.twig',
  192.             [
  193.                 'animals' => $paginator->paginate($animalsQb$page20)
  194.             ]
  195.         );
  196.     }
  197.     #[Route('/adopciak/{id}/{name}'name'base_front_animal_single')]
  198.     public function singleAction($idAnimalService $animalServiceManagerRegistry $doctrine)
  199.     {
  200.         $animal $animalService->getSingleAnimal($idfalse);
  201.         $similarAnimals $animalService->getSimilarAnimals($animal);
  202.         $notes $doctrine->getRepository(BlogNote::class)->findBy(['animal' => $animal'isPublished' => true]);
  203. //        $poll = $doctrine->getRepository(AnimalPoll::class)->findBy(
  204. //            ['createdBy' => $this->getUser()],
  205. //            ['id' => 'DESC']
  206. //        );
  207.         return $this->render(
  208.                 'front/animal/single.html.twig',
  209.             [
  210.                 'animal' => $animal,
  211. //                'poll' => $poll,
  212.                 'notes' => $notes,
  213.                 'similarAnimals' => $similarAnimals
  214.             ]
  215.         );
  216.     }
  217.     #[Route('/adopciaki/{page}')]
  218.     public function indexAction(Request $requestManagerRegistry $doctrineAnimalService $animalServicePaginatorInterface $paginator$page 1)
  219.     {
  220.         $searchCriteria $this->_getSearchCriteria($request$animalService$doctrine);
  221.         $animalsQb $doctrine->getRepository(Animal::class)->findAllAdvancedSearchQb(
  222.             $searchCriteria,
  223.             false,
  224.             $this->getOrderSeed($request)
  225.         );
  226.         if($request->isXmlHttpRequest()) {
  227.             $page $request->query->get('page');
  228.             return $this->render(
  229.                 'front/animal/list.html.twig',
  230.                 [
  231.                     'animals' => $paginator->paginate($animalsQb$page20),
  232.                     'searchCriteria' => $searchCriteria
  233.                 ]
  234.             );
  235.         }
  236.         return $this->render(
  237.                 'front/animal/index.html.twig',
  238.             [
  239.                 'animals' => $paginator->paginate($animalsQb$page20),
  240.                 'searchCriteria' => []
  241.             ]
  242.         );
  243.     }
  244.     #[Route('/{id}/ankieta/{animalPollToken}/{step}')]
  245.     public function pollAction(Request $requestAdoptionPollService $adoptionPollService$id$animalPollToken 0$step 0)
  246.     {
  247.         $animalPoll $adoptionPollService->preparePollEntity($id$animalPollToken);
  248.         $returnForm $adoptionPollService->prepareForm($animalPoll);
  249.         //synchronize steps if necessary
  250.         if (!$request->isMethod('POST') && $step != $animalPoll->getCurrentStep()) {
  251.             return $this->redirectToRoute(
  252.                 $request->get('_route'),
  253.                 ['id' => $id'step' => $animalPoll->getCurrentStep(), 'animalPollToken' => $animalPoll->getToken()]
  254.             );
  255.         }
  256.         if ($request->isXmlHttpRequest()) {
  257.             return new JsonResponse(['msg' => 'saved'], 200);
  258.         }
  259.         return $this->render(
  260.                 'front/animal/poll.html.twig',
  261.             [
  262.                 'pollForm' => $returnForm->createView(),
  263.                 'animalPoll' => $animalPoll,
  264.                 'animal' => $this->getAnimalRepository()->find($id),
  265.             ]
  266.         );
  267.     }
  268.     #[Route('/catalog600x600.csv')]
  269.     public function productCatalogAction()
  270.     {
  271.         $router $this->get('router');
  272.         $rows = array();
  273.         $aHeaderData $this->getCatalogHeaderData();
  274.         $rows[] = implode(','$aHeaderData);
  275.         $animals $this->getAnimalRepository()->getWithNoRejectedPolls();
  276.         foreach ($animals as $animal) {
  277.             $file $animal->getFbSqImageId();
  278.             $kidAcceptance $this->getKidAcceptanceForCatalog($animal);
  279.             if (!$animal->getPreferableLives()->first()) {
  280.                 $sPreferableLive 'Dom z ogrodem';
  281.             } else {
  282.                 $sPreferableLive $animal->getPreferableLives()->first()->getName();
  283.             }
  284.             $data = [
  285.                 $animal->getId(),
  286.                 'in stock',
  287.                 'new',
  288.                 '"'.str_replace(array("\r""\n"), ""strip_tags($animal->getShortDescription())).'"',
  289.                 $router->generate('base_file_index', ['id' => $file], UrlGenerator::ABSOLUTE_URL),
  290.                 $router->generate(
  291.                     'base_front_animal_single',
  292.                     ['id' => $animal->getId(), 'name' => $animal->getName()],
  293.                     UrlGenerator::ABSOLUTE_URL
  294.                 ),
  295.                 $animal->getName(),
  296.                 '0 PLN',
  297.                 'Adopciaki',
  298.                 'Animals & Pet Supplies',
  299.                 $animal->getSpecies()->getName(),
  300.                 $animal->getLifestyle()->getName(),
  301.                 $sPreferableLive,
  302.                 '"'.$kidAcceptance.'"',
  303.                 $animal->getTimeCareNeeded()->getName(),
  304.             ];
  305.             $rows[] = implode(','$data);
  306.         }
  307.         $content implode("\n"$rows);
  308.         $response = new Response();
  309.         $response->headers->set('Content-Type'"text/plain;charset=utf-8");
  310.         $response->headers->set('Content-Disposition''inline;filename="catalog600x600.csv"');
  311.         $response->headers->set('Pragma'"no-cache");
  312.         $response->headers->set('Expires'"0");
  313.         $response->headers->set('Content-Transfer-Encoding'"binary");
  314.         $response->sendHeaders();
  315.         $response->setContent($content);
  316.         return $response;
  317.     }
  318.     #[Route('/catalog1280x628.csv')]
  319.     public function productCatalogLargeFBAction()
  320.     {
  321.         $router $this->get('router');
  322.         $rows = array();
  323.         $aHeaderData $this->getCatalogHeaderData();
  324.         $rows[] = implode(','$aHeaderData);
  325.         $animals $this->getAnimalRepository()->getWithNoRejectedPolls();
  326.         foreach ($animals as $animal) {
  327.             $kidAcceptance $this->getKidAcceptanceForCatalog($animal);
  328.             $file $animal->getFbLargeImageId();
  329.             if (!$animal->getPreferableLives()->first()) {
  330.                 $sPreferableLive 'Dom z ogrodem';
  331.             } else {
  332.                 $sPreferableLive $animal->getPreferableLives()->first()->getName();
  333.             }
  334.             $data = [
  335.                 $animal->getId(),
  336.                 'in stock',
  337.                 'new',
  338.                 '"'.str_replace(array("\r""\n"), ""strip_tags($animal->getShortDescription())).'"',
  339.                 $router->generate('base_file_index', ['id' => $file], UrlGenerator::ABSOLUTE_URL),
  340.                 $router->generate(
  341.                     'base_front_animal_single',
  342.                     ['id' => $animal->getId(), 'name' => $animal->getName()],
  343.                     UrlGenerator::ABSOLUTE_URL
  344.                 ),
  345.                 $animal->getName(),
  346.                 '0 PLN',
  347.                 'Adopciaki',
  348.                 'Animals & Pet Supplies',
  349.                 $animal->getSpecies()->getName(),
  350.                 $animal->getLifestyle()->getName(),
  351.                 $sPreferableLive,
  352.                 '"'.$kidAcceptance.'"',
  353.                 $animal->getTimeCareNeeded()->getName(),
  354.             ];
  355.             $rows[] = implode(','$data);
  356.         }
  357.         $content implode("\n"$rows);
  358.         $response = new Response();
  359.         $response->headers->set('Content-Type'"text/plain;charset=utf-8");
  360.         $response->headers->set('Content-Disposition''inline;filename="catalog1280x628.csv"');
  361.         $response->headers->set('Pragma'"no-cache");
  362.         $response->headers->set('Expires'"0");
  363.         $response->headers->set('Content-Transfer-Encoding'"binary");
  364.         $response->sendHeaders();
  365.         $response->setContent($content);
  366.         return $response;
  367.     }
  368.     private function getKidAcceptanceForCatalog($animal)
  369.     {
  370.         $aData = [];
  371.         $kidAcceptances $animal->getKidAcceptances();
  372.         if ($kidAcceptances->isEmpty()) {
  373.             return '0';
  374.         }
  375.         $aNameVal = [
  376.             'to5' => '5',
  377.             'to10' => '10',
  378.             'over10' => '10+',
  379.         ];
  380.         foreach ($kidAcceptances as $acceptance) {
  381.             $name $acceptance->getSlug();
  382.             array_push($aData$aNameVal[$name]);
  383.         }
  384.         return implode(','$aData);
  385.     }
  386.     private function _getTranslatedValue($urlSearch)
  387.     {
  388.         $aTranslate $this->_aTranslate;
  389.         if (isset($aTranslate[$urlSearch])) {
  390.             return $aTranslate[$urlSearch];
  391.         }
  392.         return '';
  393.     }
  394.     private function getCatalogHeaderData()
  395.     {
  396.         $aHeaderData = [
  397.             'id',
  398.             'availability',
  399.             'condition',
  400.             'description',
  401.             'image_link',
  402.             'link',
  403.             'title',
  404.             'price',
  405.             'brand',
  406.             'google_product_category',
  407.             'color',
  408.             'material',
  409.             'pattern',
  410.             'product_type',
  411.             'custom_label_0',
  412.         ];
  413.         return $aHeaderData;
  414.     }
  415.     /**
  416.      * @param Request $request
  417.      * @return int
  418.      */
  419.     private function getOrderSeed(Request $request)
  420.     {
  421.         if ($request->getSession()->has(self::ANIMAL_SEARCH_SEED)) {
  422.             return $request->getSession()->get(self::ANIMAL_SEARCH_SEED);
  423.         } else {
  424.             $seed rand(199);
  425.             $request->getSession()->set(self::ANIMAL_SEARCH_SEED$seed);
  426.             return $seed;
  427.         }
  428.     }
  429.     /**
  430.      * @param Request $request
  431.      * @return array
  432.      * @todo Move it into animal service
  433.      */
  434.     private function _getSearchCriteria(Request $requestAnimalService $animalServiceManagerRegistry $doctrine)
  435.     {
  436. //        if($this->getUser()) {
  437. //            $searchData = $doctrine->getRepository(AnimalSearch::class)->findOneBy(['user' => $this->getUser()]);
  438. //        }
  439.         if (empty($searchData)) {
  440.             $searchData unserialize($request->cookies->get(self::ANIMAL_SEARCH_COOKIE));
  441.             if (empty($searchData)) {
  442.                 $searchData = new AnimalSearch();
  443.             }
  444.         }
  445.         if (!is_null($request->get('szukaj'))) {
  446.             $aUrlSearch explode(','$request->get('szukaj'));
  447.             //Zwierzak
  448.             if (empty($aUrlSearch[0])) {
  449.                 $searchData->setSpecies(null);
  450.             } else {
  451.                 $specie $doctrine->getRepository(Species::class)->findOneBy(
  452.                     ['type' => $this->_getTranslatedValue($aUrlSearch[0])]
  453.                 );
  454.                 $searchData->setSpecies($specie);
  455.             }
  456.             //Płeć
  457.             if (empty($aUrlSearch[1])) {
  458.                 $searchData->getSex()->clear();
  459.             } else {
  460.                 $sexArray explode(';'$aUrlSearch[1]);
  461.                 $searchData->getSex()->clear();
  462.                 foreach ($sexArray as $item) {
  463.                     $sex $doctrine->getRepository(Sex::class)->findOneBy(
  464.                         ['name' => $this->_getTranslatedValue($item)]
  465.                     );
  466.                     $searchData->addSex($sex);
  467.                 }
  468.             }
  469.             //Wiek
  470.             if (empty($aUrlSearch[2])) {
  471.                 $searchData->getAge()->clear();
  472.             } else {
  473.                 $ageArray explode(';'$aUrlSearch[2]);
  474.                 $searchData->getAge()->clear();
  475.                 foreach ($ageArray as $age) {
  476.                     $searchData->addAge($age);
  477.                 }
  478.             }
  479.             //Waga
  480.             if (empty($aUrlSearch[3])) {
  481.                 $searchData->getSize()->clear();
  482.             } else {
  483.                 $weightArray explode(';'$aUrlSearch[3]);
  484.                 $searchData->getSize()->clear();
  485.                 foreach ($weightArray as $weight) {
  486.                     $searchData->addSize($this->_getTranslatedValue($weight));
  487.                 }
  488.             }
  489.             //Aktywność
  490.             if (empty($aUrlSearch[4])) {
  491.                 $searchData->setLifeStyle(null);
  492.             } else  {
  493.                 $lifeStyle $doctrine->getRepository(LifeStyle::class)->findOneBy(
  494.                     ['slug' => $this->_getTranslatedValue($aUrlSearch[4])]
  495.                 );
  496.                 $searchData->setLifeStyle($lifeStyle);
  497.             }
  498.             //Czy masz dzieci?
  499.             if (empty($aUrlSearch[5]) || $aUrlSearch[5] === 'nie-mam-dzieci') {
  500.                 $searchData->getKidAcceptances()->clear();
  501.             } else {
  502.                 $aKids explode(';'$aUrlSearch[5]);
  503.                 $searchData->getKidAcceptances()->clear();
  504.                 foreach ($aKids as $Kid) {
  505.                     $searchData->addKidAcceptance($Kid);
  506.                 }
  507.             }
  508.             //Czy masz inne zwierzęta?
  509.             if (empty($aUrlSearch[6])) {
  510.                 $searchData->getAnimalAcceptances()->clear();
  511.             } else {
  512.                 $aKids explode(';'$aUrlSearch[6]);
  513.                 $searchData->getAnimalAcceptances()->clear();
  514.                 foreach ($aKids as $Kid) {
  515.                     $searchData->addAnimalAcceptances($Kid);
  516.                 }
  517.             }
  518.             //Jak długo zwierzak będzie zostawał sam?
  519.             if (empty($aUrlSearch[7])) {
  520.                 $searchData->setTimeCareNeeded(null);
  521.             } else {
  522.                 $timeCare $doctrine->getRepository(TimeCareNeeded::class)->findOneBy(
  523.                     ['slug' => $this->_getTranslatedValue($aUrlSearch[7])]
  524.                 );
  525.                 $searchData->setTimeCareNeeded($timeCare);
  526.             }
  527.             return $animalService->prepareAdvancedSearchCriteria($searchData);
  528.         }
  529.         return $animalService->prepareAdvancedSearchCriteria($searchData);
  530.     }
  531.     /**
  532.      * @param array $data
  533.      */
  534.     private function setSearchCookie($data)
  535.     {
  536.         $response = new Response();
  537.         $cookie = new Cookie(self::ANIMAL_SEARCH_COOKIEserialize($data), 0'/'nullfalsefalse);
  538.         $response->headers->setCookie($cookie);
  539.         $response->send();
  540.     }
  541.     /**
  542.      * @param array $data
  543.      */
  544.     private function deleteSearchCookie()
  545.     {
  546.         $response = new Response();
  547.         $response->headers->clearCookie(self::ANIMAL_SEARCH_COOKIE);
  548.         $response->send();
  549.     }
  550. }