<?php
namespace App\Controller;
use App\Repository\ToolAlternativeRepository;
use App\Repository\VoteRepository;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use App\Entity\ToolPlatform;
use App\Entity\Platform;
use App\Entity\Category;
use App\Entity\ToolCategory;
use App\Entity\Tool;
use App\Service\Util;
use App\Service\Alternative;
use App\Service\ReviewsAndComments;
use App\Entity\Review;
use App\Entity\ToolAlternative;
use App\Entity\Vote;
use App\Entity\ToolImage;
use Doctrine\ORM\Tools\Pagination\Paginator;
use Doctrine\ORM\EntityManagerInterface;
class FrontendController extends AbstractController
{
/**
* @Route("/", defaults={}, name="home")
*/
public function index()
{
$em = $this->getDoctrine()->getManager();
$topPlatforms = $em->getRepository(ToolPlatform::class)->getTopPlatforms();
$platformsData = null;
foreach ($topPlatforms as $key => $topPlatform) {
$tools = $em->getRepository(ToolPlatform::class)->getTopToolsByPlatform((int)$topPlatform[2]);
$platformsData[] = array(
"name"=>$topPlatform["name"],
"tools"=>$tools
);
}
return $this->render('frontend/index.html.twig', ["platforms"=>$platformsData]);
}
/**
* @Route("/categories/{slug}/{page}", requirements={"page"="\d+"} , name="categories")
*/
public function categories($slug,$page = 1)
{
$em = $this->getDoctrine()->getManager();
$category = $em->getRepository(Category::class)->findOneBy(["slug"=>$slug]);
if(empty($category)){
throw $this->createNotFoundException('The Category does not exist');
}
$tooldata = $this->getToolPageWise($category,$page);
$maxTools = $em->getRepository(ToolCategory::class)->getToolCount($category);
$maxpage = round((int)$maxTools/12);
if($maxpage < $page){
throw $this->createNotFoundException('No Tools Found');
}
return $this->render('frontend/categoryDetails.html.twig', ["category" => $category , 'tools'=>$tooldata,"slug"=>$slug,"page"=>$page,"maxpage"=>$maxpage]);
}
/**
* @Route("/vote", methods={"POST"}, name="add_vote")
*/
public function vote()
{
$response = new Response();
$responseData = [];
$input = $_POST;
$data = [
'toolalternative_id' => $input['toolalternative_id'],
'username' => $input['username'],
'comment' => $input['comment'],
'type' => $input['type'],
];
$recaptchaResponse = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$_ENV['RECAPTCHA_SECRET_KEY'].'&response=' . urlencode($input['g-recaptcha-response']));
$recaptchaResponse = json_decode($recaptchaResponse, true);
if($recaptchaResponse['success'] == true){
$entityManager = $this->getDoctrine()->getManager();
$toolAlternative = $entityManager->find(ToolAlternative::class, $input['toolalternative_id']);
$vote = new Vote();
$vote->setUsername($input['username']);
$vote->setToolalternative($toolAlternative);
$vote->setComment($input['comment']);
$vote->setEmail($input['email']);
$vote->setType($input['type']);
$vote->setDate(new \DateTime());
$entityManager->persist($vote);
$entityManager->flush();
$data['id'] = $vote->getId();
}else{
$responseData['success'] = false;
$response->setStatusCode(Response::HTTP_BAD_REQUEST);
return $response;
}
$responseData['success'] = true;
$response->setStatusCode(Response::HTTP_OK);
$response->setContent(json_encode($responseData));
$response->headers->set('Content-Type', 'application/json');
return $response;
}
/**
* @Route("/categorytools", name="categorytools")
*/
public function categorytools(Request $req,Util $util){
$start = $req->query->get('start');
$slug = $req->query->get('slug');
$em = $this->getDoctrine()->getManager();
$category = $em->getRepository(Category::class)->findOneBy(["slug"=>$slug]);
if(empty($category)){
throw $this->createNotFoundException('The Category does not exist');
}
$tooldata = $this->getCategoryTools($category,$start);
$templateDir = $util->getTemplateDir();
$loader = new \Twig\Loader\FilesystemLoader($templateDir);
$twig = new \Twig\Environment($loader);
$html = $twig->render('/frontend/categorytoollist.html.twig', ['tools' =>$tooldata ]);
return new Response($html);
}
public function getCategoryTools($category,$start){
$em = $this->getDoctrine()->getManager();
$tools = $em->getRepository(ToolCategory::class)->findByStartIndexAndCateogry($category,$start);
$toolsdata = null;
foreach ($tools as $key => $tool) {
$tooldata = $tool->getTool()->getToolData();
$tooldata["url"] = $this->generateUrl('toolfrontview', array('slug' => $tool->getTool()->getSlug()));
$toolsdata[] =$tooldata;
}
return $toolsdata;
}
/**
* @Route("/tools/{slug}", defaults={}, name="toolfrontview")
*/
public function toolfrontview($slug,Alternative $alternatives,ReviewsAndComments $reviewsAndComments)
{
$em = $this->getDoctrine()->getManager();
$tool = $em->getRepository(Tool::class)->findOneBySlug($slug);
if(empty($tool)){
throw $this->createNotFoundException('Tool does not exist');
}
$ed = $tool->getExtendedDescription();
if (strpos($ed, '<br> <br> <br>') !== false) {
$ed = preg_replace('/(<br>(\s|)){3,500}/s', '<br> <br> ', $ed);
$tool->setExtendedDescription($ed);
}
$toolPlatforms = $em->getRepository(ToolPlatform::class)->findByToolPlatform($tool);
$toolCategories = $em->getRepository(ToolCategory::class)->findByToolCateogry($tool);
$toolImages = $em->getRepository(ToolImage::class)->findByToolImages($tool);
$toolAlternativesData = $alternatives->getAlternativeByStartIndex(0,$tool);
// $toolAlternativesData = array_values($toolAlternativesData);
// $copy = $toolAlternativesData;
// print_r($toolAlternativesData); die;
// $toolAlternativesData[0] = $copy[3];
// $toolAlternativesData[2] = $copy[4];
// $toolAlternativesData[3] = $copy[2];
// $toolAlternativesData[4] = $copy[0];
// foreach ($toolAlternativesData as $key => $value) {
// if (isset($value['votes']))
// $toolAlternativesData[$key]['votes'] = array_reverse($toolAlternativesData[$key]['votes']);
// }
//print_r($toolAlternativesData); die;
$noOfReviews = $em->getRepository(Review::class)->geCountReviewsOrComments($tool,true);
$noOfComments = $em->getRepository(Review::class)->geCountReviewsOrComments($tool,false);
$commentsarray = $reviewsAndComments->getCommentsAndReviews($tool,0,false);
$reviewsarray = $reviewsAndComments->getCommentsAndReviews($tool,0,true);
$toolUrl = $tool->getWebsiteUrl();
$adsense = true;
if (in_array($slug, [
'freemake-video-downloader',
'niigu'
]))
$adsense = false;
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]);
}
/**
* @Route("/frontendalternatives", defaults={}, name="frontendalternatives")
*/
public function frontendAlternatives(Request $req,Alternative $alternatives,Util $util)
{
$start = $req->query->get('start');
$slug = $req->query->get('slug');
$em = $this->getDoctrine()->getManager();
$tool = $em->getRepository(Tool::class)->findOneBy(["slug"=>$slug]);
$alternatives = $alternatives->getAlternativeByStartIndex((int) $start,$tool);
$templateDir = $util->getTemplateDir();
$loader = new \Twig\Loader\FilesystemLoader($templateDir);
$twig = new \Twig\Environment($loader);
$html = $twig->render('/frontend/alternativeslist.html.twig', ['alternatives' =>$alternatives ]);
return new Response($html);
}
/**
* @Route("/frontendcommentsorreviews", defaults={}, name="frontendcommentsorreviews")
*/
public function frontendcommentsorreviews(Request $req,ReviewsAndComments $reviewsAndComments,Util $util)
{
$html = '';
$start = $req->query->get('start');
$isReview = $req->query->get('isReview');
$toolslug = $req->query->get('slug');
$type = $req->query->get('type');
$em = $this->getDoctrine()->getManager();
$tool = $em->getRepository(Tool::class)->findOneBy(["slug"=>$toolslug]);
$commentsOrReviews = $reviewsAndComments->getCommentsAndReviews($tool,(int)$start,(bool)$isReview);
$templateDir = $util->getTemplateDir();
$loader = new \Twig\Loader\FilesystemLoader($templateDir);
$twig = new \Twig\Environment($loader);
if($isReview == "true"){
$html = $twig->render('/frontend/'.$type.'list.html.twig', ['reviews' =>$commentsOrReviews ]);
}else{
$html = $twig->render('/frontend/'.$type.'list.html.twig', ['comments' =>$commentsOrReviews ]);
}
return new Response($html);
}
public function getToolPageWise($category,$currentPage = 1){
$em = $this->getDoctrine()->getManager();
$tools = $em->getRepository(ToolCategory::class)->findCateogryTools($category,12,$currentPage);
$toolsdata = null;
foreach ($tools as $key => $tool) {
$tooldata = $tool->getTool()->getToolData();
$tooldata["url"] = $this->generateUrl('toolfrontview', array('slug' => $tool->getTool()->getSlug()));
$toolsdata[] =$tooldata;
}
return $toolsdata;
}
}