<?php
namespace App\Controller\Frontend;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\JsonResponse;
use Nzo\UrlEncryptorBundle\Annotations\ParamDecryptor;
use Nzo\UrlEncryptorBundle\Annotations\ParamEncryptor;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Security\Core\Security;
use App\Service\HelperService;
use Symfony\Component\String\Slugger\SluggerInterface;
use Nzo\UrlEncryptorBundle\Encryptor\Encryptor;
use Symfony\Component\HttpFoundation\RequestStack;
/**
* @Route("main")
*/
class MainController extends AbstractController
{
private $requestStack;
public function __construct(RequestStack $requestStack)
{
$this->requestStack = $requestStack;
}
public function mainMenuAction(Request $request)
{
$lastUsername = $request->getSession()->get(Security::LAST_USERNAME);
if($lastUsername)
{
$customerData = $request->getSession()->get($lastUsername);
} else {
$customerData = "";
}
$masterRequest = $this->requestStack->getMainRequest();
$path = $masterRequest->getPathInfo();
$slugArray = explode("/", $path);
$slug = "";
if(count($slugArray) > 0)
{
$slug = $slugArray[1];
}
$routeName = $request->attributes->get('_route');
//$client_id = $this->getParameter('client_id');
//$client = $this->getDoctrine()->getRepository(\App\Entity\Client::class)->findOneBy(array("clientId" => $client_id ));
$client = $this->getDoctrine()->getRepository(\App\Entity\Client::class)->findOneBy(["slug" => $slug]);
$settings = $this->getDoctrine()->getRepository(\App\Entity\WebSetting::class)->findOneBy(["Client" => $client ]);
$categories = $this->getDoctrine()->getRepository(\App\Entity\Category::class)->findBy([
"Client" => $client,
"is_active" => 1,
"featured_menu" => 1
]);
$brands = $this->getDoctrine()->getRepository(\App\Entity\Brand::class)->findBrandsMenu($client->getClientId());
return $this->render('Frontend/main_menu.html.twig', array(
'settings' => $settings,
'brands' => $brands,
'categories' => $categories,
'routeName' => $routeName,
'customerData' => $customerData,
'client' => $client,
'slug' => $slug,
'chatWidget' => $client->getChatWidget()
));
}
public function chatWidgetAction(Request $request)
{
$client_id = $this->getParameter('client_id');
$settings = $this->getDoctrine()->getRepository(\App\Entity\Client::class)->findOneBy(array("clientId" => $client_id ));
return $this->render('Frontend/chat_widget.html.twig', array(
'clientObj' => $settings
));
}
public function titleAction(Request $request)
{
$client_id = $this->getParameter('client_id');
$settings = $this->getDoctrine()->getRepository(\App\Entity\Client::class)->findOneBy(array("clientId" => $client_id ));
return $this->render('Frontend/_load_title.html.twig', array(
'clientObj' => $settings
));
}
public function whatsappWidgetAction(Request $request)
{
$client_id = $this->getParameter('client_id');
$settings = $this->getDoctrine()->getRepository(\App\Entity\Client::class)->findOneBy(array("clientId" => $client_id ));
return $this->render('Frontend/whatsapp_widget.html.twig', array(
'clientObj' => $settings
));
}
public function customStylesAction(Request $request)
{
$userData = $this->get("session")->get("userData");
$client_id = $this->getParameter('client_id');
$settings = $this->getDoctrine()->getRepository(\App\Entity\WebSetting::class)->findOneBy(array("Client" => $client_id ));
$mainColorRgba = $this->hex2rgba($settings->getMainColor(),'0.8');
return $this->render('Frontend/custom_style.html.twig', array(
"userData" => $userData,
'settings' => $settings,
'mainColorRgba' => $mainColorRgba
));
}
public function hex2rgba($color, $opacity = false)
{
$default = 'rgb(0,0,0)';
//Return default if no color provided
if(empty($color))
return $default;
//Sanitize $color if "#" is provided
if ($color[0] == '#' ) {
$color = substr( $color, 1 );
}
//Check if color has 6 or 3 characters and get values
if (strlen($color) == 6) {
$hex = array( $color[0] . $color[1], $color[2] . $color[3], $color[4] . $color[5] );
} elseif ( strlen( $color ) == 3 ) {
$hex = array( $color[0] . $color[0], $color[1] . $color[1], $color[2] . $color[2] );
} else {
return $default;
}
//Convert hexadec to rgb
$rgb = array_map('hexdec', $hex);
//Check if opacity is set(rgba or rgb)
if($opacity){
if(abs($opacity) > 1)
$opacity = 1.0;
$output = 'rgba('.implode(",",$rgb).','.$opacity.')';
} else {
$output = 'rgb('.implode(",",$rgb).')';
}
//Return rgb(a) color string
return $output;
}
}