<?php
namespace App\OfficeBrain\Bundle\BannerBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Contracts\Service\Attribute\Required;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use App\OfficeBrain\Bundle\BannerBundle\Entity\Banner;
use App\OfficeBrain\Bundle\BannerBundle\Entity\BannerLanguage;
//use OfficeBrain\Bundle\BannerBundle\Entity\BannerCountry;
use App\OfficeBrain\Bundle\BannerBundle\Form\BannerType;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Validator\Constraints\Count;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use App\OfficeBrain\Bundle\CoreBundle\Cache\RedisCacheService;
// use Symfony\Component\Translation\TranslatorInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
use Knp\Component\Pager\PaginatorInterface;
use Doctrine\ORM\EntityManagerInterface;
use App\OfficeBrain\Bundle\BannerBundle\Manager\Extended\BannerExtended;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
*
* @author OfficeBrain 4571 <info@officebrain.com>
*
* Description: Front Controller used to Display Banner in front
*
* @Route("{_locale}")
*
*/
class BannerController extends AbstractController
{
private $translator;
private $request;
private $projectSetting;
private $manager;
private $isAjax;
private $session;
private $responseArray;
/**
*
* @author Employee Id: 4571
* Description : To initialize variables
*
* @param : null
*
* @return null
*
* @throws null
*
**/
public function __construct(EntityManagerInterface $entityManager, RequestStack $requestStack, TranslatorInterface $translator, PaginatorInterface $knpPaginator, BannerExtended $bannerExtended, ContainerInterface $container)
{
$this->request = null;
$this->requestStack = $requestStack->getCurrentRequest();
$this->manager = $bannerExtended;
$this->responseArray = array();
$this->projectSetting = null;
$this->session = null;
$this->isAjax = null;
$this->translator = null;
$this->statsh_cache_wrapper=RedisCacheService::create();
$this->entityManager = $entityManager;
$this->translator = $translator;
$this->knpPaginator = $knpPaginator;
$this->container = $container;
}
/**
*
* @author Employee Id: 4571
* Description : To unset member variables
*
* @param : null
*
* @return null
*
* @throws null
*
**/
public function __destruct()
{
unset($this->request);
unset($this->manager);
unset($this->responseArray);
unset($this->projectSetting);
unset($this->session);
unset($this->isAjax);
unset($this->translator);
unset($this->statsh_cache_wrapper);
}
/**
*
* @author Employee Id: 4571
* Description : to initialize request and services
*
* @param : null
*
* @return null
*
* @throws null
*
**/
public function initAction(Request $request)
{
$this->request = $request;
// $this->manager = $this->get('office_brain_banner_manager');
// $this->translator = $this->get('translator'); // symfony 4
$this->session = $this->request->getSession();
$this->isAjax = $this->request->isXmlHttpRequest();
$this->projectSetting = $this->request->get('project_setting');
}
/**
*
* @author Employee Id: 4571
* Description : Fetch Active Banners
*
* @param : null
*
* @return null
*
* @throws null
*
* @Route("/banner/get-banners", name="office_brain_bannerbundle_banner_getallactivebanner")
* @Template("OfficeBrain/Bundle/BannerBundle/Banner/getAllActiveBanner.html.twig")
*
**/
public function getAllActiveBannerAction(Request $request)
{
$this->initAction($request);
$locale=$this->request->getLocale();
$displayon=$this->request->get('displayOn');
$searchQuery = $this->request->get('searchQuery');
$categoryFilter = $this->request->get('categoryFilter');
// -----------------------------------------------------------------------
$cacheSiteKey = array('Banner',$displayon,$searchQuery,json_encode($categoryFilter),$locale,$this->projectSetting['instance_id'],$this->projectSetting['kernelEnvironment']);
$this->moduleCachePrefix='__BannerHomePage__:';
$cacheKey = $this->moduleCachePrefix.RedisCacheService::generateKey($this->statsh_cache_wrapper,$cacheSiteKey);
$cacheData = RedisCacheService::getCacheData($this->statsh_cache_wrapper,$cacheKey);
if($cacheData){
// print_r($cacheData);exit;
return $cacheData;
}
// -----------------------------------------------------------------------
$directory = $this->getParameter('banner.image_url');
// echo $directory;exit;
$this->responseArray['displayon'] = $displayon;
$this->responseArray['banner_list'] = $this->manager->fetchActiveBanner();
$this->responseArray['image_path'] = $directory;
/* echo "<pre>";
\Doctrine\Common\Util\Debug::dump($this->responseArray['banner_list']);
die;
*/
foreach($this->responseArray['banner_list'] as $val){
$this->responseArray['banner'][$val['bannerCategory']][] = $val;
}
$response = $this->render('OfficeBrain/Bundle/BannerBundle/Banner/getAllActiveBanner.html.twig', $this->responseArray);
if(!empty($cacheKey))
{
RedisCacheService::saveCacheData($this->statsh_cache_wrapper,$cacheKey,$response);
}
return $this->responseArray;
}
/**
*
* @author Employee Id: 4571
* Description : Fetch Banner Transitions
*
* @param : null
*
* @return js output
*
* @throws null
*
* @Route("/assets/js/banner-trans.js/{display}", name="office_brain_bannerbundle_banner_findbannertrans")
*
* @Method("GET")
* @Template()
*
**/
public function findBannerTransAction(Request $request, $display)
{
$this->initAction($request);
$this->responseArray['banner_transition'] = $this->manager->findBannerTransition($display);
$response = new Response();
$response->headers->set('Content-Type', 'text/javascript');
return $this->render(
'OfficeBrain/Bundle/BannerBundle/Banner/bannerCustomJs.html.twig',
$this->responseArray,
$response
);
}
/**
*
* @author Employee Id: 4835
* Description : details save in db for anayltics
*
* @param : $id banner id
*
* @return null
*
* @throws null
*
* @Route("/banner/linkajax/{id}", name="office_brain_bannerbundle_banner_linkajax",options={"expose"=true})
*
**/
public function linkAjaxAction(Request $request, $id)
{
$this->initAction($request);
$bannerServiceLog=$this->get("office_brain_activity_api_manager");
$bannerUrl=$this->manager->fetchBannerDetail($bannerServiceLog, $id);
return new JsonResponse($bannerUrl);
}
}