src/Controller/FrontendController.php line 162

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Repository\ToolAlternativeRepository;
  4. use App\Repository\VoteRepository;
  5. use Symfony\Component\HttpFoundation\Response;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\Routing\Annotation\Route;
  8. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  9. use App\Entity\ToolPlatform;
  10. use App\Entity\Platform;
  11. use App\Entity\Category;
  12. use App\Entity\ToolCategory;
  13. use App\Entity\Tool;
  14. use App\Service\Util;
  15. use App\Service\Alternative;
  16. use App\Service\ReviewsAndComments;
  17. use App\Entity\Review;
  18. use App\Entity\ToolAlternative;
  19. use App\Entity\Vote;
  20. use App\Entity\ToolImage;
  21. use Doctrine\ORM\Tools\Pagination\Paginator;
  22. use Doctrine\ORM\EntityManagerInterface;
  23. class FrontendController extends AbstractController
  24. {
  25.     /**
  26.      * @Route("/", defaults={}, name="home")
  27.      */
  28.     public function index()
  29.     {    
  30.         $em $this->getDoctrine()->getManager();
  31.         $topPlatforms $em->getRepository(ToolPlatform::class)->getTopPlatforms();
  32.         $platformsData null;
  33.     
  34.         foreach ($topPlatforms as $key => $topPlatform) {
  35.             $tools $em->getRepository(ToolPlatform::class)->getTopToolsByPlatform((int)$topPlatform[2]);
  36.             
  37.             $platformsData[] = array(
  38.                 "name"=>$topPlatform["name"],
  39.                 "tools"=>$tools
  40.             );
  41.         }
  42.         return $this->render('frontend/index.html.twig', ["platforms"=>$platformsData]);
  43.     }
  44.     /**
  45.      * @Route("/categories/{slug}/{page}", requirements={"page"="\d+"} , name="categories")
  46.      */
  47.     public function categories($slug,$page 1)
  48.     {    
  49.         $em $this->getDoctrine()->getManager();
  50.         $category $em->getRepository(Category::class)->findOneBy(["slug"=>$slug]);
  51.          if(empty($category)){
  52.               throw $this->createNotFoundException('The Category does not exist');
  53.          }
  54.         $tooldata $this->getToolPageWise($category,$page);
  55.        $maxTools $em->getRepository(ToolCategory::class)->getToolCount($category);
  56.        $maxpage round((int)$maxTools/12);
  57.        
  58.         if($maxpage $page){
  59.              throw $this->createNotFoundException('No Tools Found');
  60.         }
  61.         return $this->render('frontend/categoryDetails.html.twig', ["category" => $category 'tools'=>$tooldata,"slug"=>$slug,"page"=>$page,"maxpage"=>$maxpage]);
  62.     }
  63.     /**
  64.      * @Route("/vote", methods={"POST"}, name="add_vote")
  65.      */
  66.     public function vote()
  67.     {
  68.         $response = new Response();
  69.         $responseData = [];
  70.         $input $_POST;
  71.         $data = [
  72.             'toolalternative_id' => $input['toolalternative_id'],
  73.             'username' => $input['username'],
  74.             'comment' => $input['comment'],
  75.             'type' => $input['type'],
  76.         ];
  77.         $recaptchaResponse file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$_ENV['RECAPTCHA_SECRET_KEY'].'&response=' urlencode($input['g-recaptcha-response']));
  78.         $recaptchaResponse json_decode($recaptchaResponsetrue);
  79.         if($recaptchaResponse['success'] == true){
  80.             $entityManager $this->getDoctrine()->getManager();
  81.             $toolAlternative $entityManager->find(ToolAlternative::class, $input['toolalternative_id']);
  82.             $vote = new Vote();
  83.             $vote->setUsername($input['username']);
  84.             $vote->setToolalternative($toolAlternative);
  85.             $vote->setComment($input['comment']);
  86.             $vote->setEmail($input['email']);
  87.             $vote->setType($input['type']);
  88.             $vote->setDate(new \DateTime());
  89.             $entityManager->persist($vote);
  90.             $entityManager->flush();
  91.             $data['id'] = $vote->getId();
  92.         }else{
  93.             $responseData['success'] = false;
  94.             $response->setStatusCode(Response::HTTP_BAD_REQUEST);
  95.             return $response;
  96.         }
  97.         $responseData['success'] = true;
  98.         $response->setStatusCode(Response::HTTP_OK);
  99.         $response->setContent(json_encode($responseData));
  100.         $response->headers->set('Content-Type''application/json');
  101.         return $response;
  102.     }
  103.     /**
  104.      * @Route("/categorytools", name="categorytools")
  105.      */
  106.     public function categorytools(Request $req,Util $util){
  107.         $start $req->query->get('start');
  108.         $slug $req->query->get('slug');
  109.         $em $this->getDoctrine()->getManager();
  110.         $category $em->getRepository(Category::class)->findOneBy(["slug"=>$slug]);
  111.          if(empty($category)){
  112.               throw $this->createNotFoundException('The Category does not exist');
  113.          }
  114.          $tooldata $this->getCategoryTools($category,$start);
  115.          $templateDir $util->getTemplateDir();
  116.         $loader = new \Twig\Loader\FilesystemLoader($templateDir);
  117.         $twig = new \Twig\Environment($loader);
  118.         $html =  $twig->render('/frontend/categorytoollist.html.twig', ['tools' =>$tooldata ]);
  119.         return new Response($html);
  120.     }
  121.     public function getCategoryTools($category,$start){
  122.         $em $this->getDoctrine()->getManager();
  123.          $tools $em->getRepository(ToolCategory::class)->findByStartIndexAndCateogry($category,$start);
  124.          $toolsdata null;
  125.          foreach ($tools as $key => $tool) {
  126.              $tooldata $tool->getTool()->getToolData();
  127.             $tooldata["url"] = $this->generateUrl('toolfrontview', array('slug' => $tool->getTool()->getSlug()));
  128.             $toolsdata[] =$tooldata;
  129.          }
  130.          return $toolsdata;
  131.     }
  132.     /**
  133.      * @Route("/tools/{slug}", defaults={}, name="toolfrontview")
  134.      */
  135.     public function toolfrontview($slug,Alternative $alternatives,ReviewsAndComments $reviewsAndComments)
  136.     {   
  137.         $em $this->getDoctrine()->getManager();
  138.         $tool $em->getRepository(Tool::class)->findOneBySlug($slug);
  139.         if(empty($tool)){
  140.              throw $this->createNotFoundException('Tool does not exist');
  141.         }
  142.         $ed $tool->getExtendedDescription();
  143.         if (strpos($ed'<br> <br> <br>') !== false) {
  144.             $ed preg_replace('/(<br>(\s|)){3,500}/s''<br> <br> '$ed);
  145.             $tool->setExtendedDescription($ed);
  146.         }
  147.         $toolPlatforms $em->getRepository(ToolPlatform::class)->findByToolPlatform($tool);
  148.         $toolCategories $em->getRepository(ToolCategory::class)->findByToolCateogry($tool);
  149.         $toolImages $em->getRepository(ToolImage::class)->findByToolImages($tool);
  150.         
  151.         $toolAlternativesData $alternatives->getAlternativeByStartIndex(0,$tool);
  152.         // $toolAlternativesData = array_values($toolAlternativesData);
  153.         // $copy = $toolAlternativesData;
  154.         // print_r($toolAlternativesData); die;
  155.         // $toolAlternativesData[0] = $copy[3];
  156.         // $toolAlternativesData[2] = $copy[4];
  157.         // $toolAlternativesData[3] = $copy[2];
  158.         // $toolAlternativesData[4] = $copy[0];
  159.         // foreach ($toolAlternativesData as $key => $value) {
  160.         //     if (isset($value['votes']))
  161.         //         $toolAlternativesData[$key]['votes'] = array_reverse($toolAlternativesData[$key]['votes']);
  162.         // }
  163.         //print_r($toolAlternativesData); die;
  164.         $noOfReviews $em->getRepository(Review::class)->geCountReviewsOrComments($tool,true);
  165.         $noOfComments $em->getRepository(Review::class)->geCountReviewsOrComments($tool,false);
  166.         $commentsarray $reviewsAndComments->getCommentsAndReviews($tool,0,false);
  167.         $reviewsarray $reviewsAndComments->getCommentsAndReviews($tool,0,true);
  168.         $toolUrl $tool->getWebsiteUrl();
  169.      
  170.         $adsense true;
  171.         if (in_array($slug, [
  172.             'freemake-video-downloader',
  173.             'niigu'
  174.         ]))
  175.             $adsense false;
  176.      
  177.        
  178.         return $this->render('frontend/toolDetails.html.twig', ["tool"=>$tool,"alternatives"=>$toolAlternativesData,"noOfReviews"=>$noOfReviews,"comments"=>$commentsarray ,"reviews"=> $reviewsarray,"noOfComments"=>$noOfComments,"toolCategories"=>$toolCategories,"toolImages"=>$toolImages,"toolPlatforms"=>$toolPlatforms"officialWebsite" => $toolUrl'adsense' => $adsense]);
  179.     }
  180.     
  181.    
  182.     /**
  183.      * @Route("/frontendalternatives", defaults={}, name="frontendalternatives")
  184.      */
  185.     public function frontendAlternatives(Request $req,Alternative $alternatives,Util $util)
  186.     {   
  187.         $start $req->query->get('start');
  188.         $slug $req->query->get('slug');
  189.         $em $this->getDoctrine()->getManager();
  190.         $tool $em->getRepository(Tool::class)->findOneBy(["slug"=>$slug]);
  191.         $alternatives $alternatives->getAlternativeByStartIndex((int) $start,$tool);
  192.        
  193.         $templateDir $util->getTemplateDir();
  194.         $loader = new \Twig\Loader\FilesystemLoader($templateDir);
  195.         $twig = new \Twig\Environment($loader);
  196.         $html =  $twig->render('/frontend/alternativeslist.html.twig', ['alternatives' =>$alternatives ]);
  197.         return new Response($html);
  198.         
  199.     }
  200.     /**
  201.      * @Route("/frontendcommentsorreviews", defaults={}, name="frontendcommentsorreviews")
  202.      */
  203.     public function frontendcommentsorreviews(Request $req,ReviewsAndComments $reviewsAndComments,Util $util)
  204.     {   
  205.         $html '';
  206.         $start $req->query->get('start');
  207.         $isReview $req->query->get('isReview');
  208.         $toolslug $req->query->get('slug');
  209.         $type $req->query->get('type');
  210.         $em $this->getDoctrine()->getManager();
  211.         $tool $em->getRepository(Tool::class)->findOneBy(["slug"=>$toolslug]);
  212.         $commentsOrReviews $reviewsAndComments->getCommentsAndReviews($tool,(int)$start,(bool)$isReview);
  213.       
  214.         $templateDir $util->getTemplateDir();
  215.         $loader = new \Twig\Loader\FilesystemLoader($templateDir);
  216.         $twig = new \Twig\Environment($loader);
  217.        
  218.         if($isReview == "true"){
  219.             $html $twig->render('/frontend/'.$type.'list.html.twig', ['reviews' =>$commentsOrReviews ]);
  220.         }else{
  221.             $html $twig->render('/frontend/'.$type.'list.html.twig', ['comments' =>$commentsOrReviews ]);
  222.         }
  223.         
  224.         return new Response($html);
  225.         
  226.     }
  227.     public function getToolPageWise($category,$currentPage 1){
  228.         $em $this->getDoctrine()->getManager();
  229.         $tools $em->getRepository(ToolCategory::class)->findCateogryTools($category,12,$currentPage); 
  230.         $toolsdata null;
  231.         foreach ($tools as $key => $tool) {
  232.             $tooldata $tool->getTool()->getToolData();
  233.             $tooldata["url"] = $this->generateUrl('toolfrontview', array('slug' => $tool->getTool()->getSlug()));
  234.             $toolsdata[] =$tooldata;
  235.         }
  236.         return $toolsdata;
  237.     }
  238. }