<?php
namespace App\Controller;
use App\Services\Api\ApiService;
use App\Services\SystemPageResources;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Routing\Annotation\Route;
class PageController extends AbstractController
{
private $router;
private $parameterBag;
public function __construct(UrlGeneratorInterface $router, ParameterBagInterface $parameterBag)
{
$this->router = $router;
$this->parameterBag = $parameterBag;
}
/**
* @Route("/", name="homepage" )
*/
public function homepage(Request $request, SessionInterface $session)
{
if ( !$this->getUser() || $request->getLocale() === "login"){ return $this->redirect('/app/login'); }
return $this->redirect("/".$this->getParameter("app.default_locale"), 301);
}
/**
* @Route("/{_locale}", name="homepage_localized")
*/
public function startPage(Request $request) {
if ( !$this->getUser() || $request->getLocale() === "app"){ return $this->redirect('/app/login'); }
if (in_array($request->getLocale(),$this->getParameter('app.supported_locales') ) === false)
{
return $this->redirect($this->getParameter("app.default_locale"));
}
$em = $this->getDoctrine()->getManager();
$pageResources = new SystemPageResources($em);
$ajaxRequest = $request->request->get("ajaxRequest");
$pageData = [];
$pageData['data'] = json_decode($pageResources->getResources($request), true);
if (!$ajaxRequest) {
$data = [
"leftsideMenu" => $this->get('twig')->render("/_pages/partials/leftsideMenu.html.twig", $pageData),
"header" => $this->get('twig')->render("/_pages/partials/baseHeader.html.twig", $pageData),
"footer" => $this->get('twig')->render("/_pages/partials/baseFooter.html.twig", $pageData)
];
$pageData['html'] = $data;
return $this->render("/_pages/homepage.html.twig", $pageData);
} else {
$data = [
"pageTitle" => "xPerto_PLATE",
"pageBody" => $this->get('twig')->render("/_bodies/homepage.html.twig", $pageData)
];
$pageData['html'] = $data;
return new JsonResponse($pageData);
}
}
/**
* @Route("/analytics/{locale}/{area}", name="area_analytics")
*/
public function analytics(Request $request, $locale, $area) {
if ( !$this->getUser() || $locale === "login"){ return $this->redirect('/app/login'); }
if ( in_array($locale, $this->getParameter('localeSet')) === false ) {
return $this->redirect( $this->get('router')->generate($request->attributes->get('_route'), array_merge($request->attributes->get('_route_params'), ['locale' => $this->getParameter('appDefaultLocale')]), ['force_canonical'=>true], 301));
}
$em = $this->getDoctrine()->getManager();
$pageResources = new SystemPageResources($em);
$api = new ApiService($em);
$ajaxRequest = $request->request->get("ajaxRequest");
$pageData = [];
$pageData['data'] = json_decode($pageResources->getResources($request, $locale), true);
$pageScripts = $this->get('twig')->render("/appArea_analytics/scripts/analytics.html.twig");
switch ($area) {
case "developmentBonus":
$periodsData = json_decode($api->endpointDirect('analytics/getDevelopmentBonusPeriods', ["locale"=>"pl"]), true);
$pageData['developmentBonusPeriods'] = $periodsData['periods'];
$pageData['developmentBonusPartners'] = $periodsData['partners'];
$pageScripts = $this->get('twig')->render("/appArea_analytics/scripts/analytics.html.twig");
break;
}
if (!$ajaxRequest) {
$data = [
"leftsideMenu" => $this->get('twig')->render("/_pages/partials/leftsideMenu.html.twig", $pageData),
"header" => $this->get('twig')->render("/_pages/partials/baseHeader.html.twig", $pageData),
"footer" => $this->get('twig')->render("/_pages/partials/baseFooter.html.twig", $pageData),
];
$pageData['html'] = $data;
return $this->render("/appArea_analytics/pages/".$area.".html.twig", $pageData);
} else {
$data = [
"pageTitle" => "xPerto_PLATE",
"pageBody" => $this->get('twig')->render("/appArea_analytics/bodies/".$area.".html.twig", $pageData),
"pageScripts" => $pageScripts
];
$pageData['html'] = $data;
return new JsonResponse($pageData);
}
}
/**
* @Route("/customer-service/{locale}/{area}", name="area_customer-service")
*/
public function customerService(Request $request, $locale, $area) {
if ( !$this->getUser() || $locale === "login"){ return $this->redirect('/app/login'); }
if ( in_array($locale, $this->getParameter('localeSet')) === false ) {
return $this->redirect( $this->get('router')->generate($request->attributes->get('_route'), array_merge($request->attributes->get('_route_params'), ['locale' => $this->getParameter('appDefaultLocale')]), ['force_canonical'=>true], 301));
}
$em = $this->getDoctrine()->getManager();
$pageResources = new SystemPageResources($em);
$ajaxRequest = $request->request->get("ajaxRequest");
$pageData = [];
$pageData['data'] = json_decode($pageResources->getResources($request, $locale), true);
if (!$ajaxRequest) {
$data = [
"leftsideMenu" => $this->get('twig')->render("/_pages/partials/leftsideMenu.html.twig", $pageData),
"header" => $this->get('twig')->render("/_pages/partials/baseHeader.html.twig", $pageData),
"footer" => $this->get('twig')->render("/_pages/partials/baseFooter.html.twig", $pageData),
"pageScripts" => $this->get('twig')->render("/appArea_customer_service/scripts/customerService.html.twig")
];
$pageData['html'] = $data;
return $this->render("/appArea_customer_service/pages/".$area.".html.twig", $pageData);
} else {
$data = [
"pageTitle" => "xPerto_PLATE",
"pageBody" => $this->get('twig')->render("/appArea_customer_service/bodies/".$area.".html.twig", $pageData),
"pageScripts" => $this->get('twig')->render("/appArea_customer_service/scripts/customerService.html.twig")
];
$pageData['html'] = $data;
return new JsonResponse($pageData);
}
}
/**
* @Route("/sandbox/{locale}/{area}", name="area_sandbox")
*/
public function sandbox(Request $request, $locale, $area) {
if ( !$this->getUser() || $locale === "login"){ return $this->redirect('/app/login'); }
if ( in_array($locale, $this->getParameter('localeSet')) === false ) {
return $this->redirect( $this->get('router')->generate($request->attributes->get('_route'), array_merge($request->attributes->get('_route_params'), ['locale' => $this->getParameter('appDefaultLocale')]), ['force_canonical'=>true], 301));
}
$em = $this->getDoctrine()->getManager();
$pageResources = new SystemPageResources($em);
$ajaxRequest = $request->request->get("ajaxRequest");
$pageData = [];
$pageData['data'] = json_decode($pageResources->getResources($request, $locale), true);
if (!$ajaxRequest) {
$data = [
"leftsideMenu" => $this->get('twig')->render("/_pages/partials/leftsideMenu.html.twig", $pageData),
"header" => $this->get('twig')->render("/_pages/partials/baseHeader.html.twig", $pageData),
"footer" => $this->get('twig')->render("/_pages/partials/baseFooter.html.twig", $pageData),
"pageScripts" => $this->get('twig')->render("/appArea_sandbox/scripts/sandbox.html.twig")
];
$pageData['html'] = $data;
return $this->render("/appArea_sandbox/pages/".$area.".html.twig", $pageData);
} else {
$data = [
"pageTitle" => "xPerto_PLATE",
"pageBody" => $this->get('twig')->render("/appArea_sandbox/bodies/".$area.".html.twig", $pageData),
"pageScripts" => $this->get('twig')->render("/appArea_sandbox/scripts/sandbox.html.twig")
];
$pageData['html'] = $data;
return new JsonResponse($pageData);
}
}
/**
* @Route("/reports/{locale}/{area}", name="area_reports")
*/
public function reports(Request $request, $locale, $area) {
if ( !$this->getUser() || $locale === "login"){ return $this->redirect('/app/login'); }
if ( in_array($locale, $this->getParameter('localeSet')) === false ) {
return $this->redirect( $this->get('router')->generate($request->attributes->get('_route'), array_merge($request->attributes->get('_route_params'), ['locale' => $this->getParameter('appDefaultLocale')]), ['force_canonical'=>true], 301));
}
$em = $this->getDoctrine()->getManager();
$pageResources = new SystemPageResources($em);
$filesystem = new Filesystem();
$ajaxRequest = $request->request->get("ajaxRequest");
$pageData = [];
$pageData['data'] = json_decode($pageResources->getResources($request, $locale), true);
switch($area) {
case "dashboard":
break;
case "sales-overal;":
break;
case "report_salesByItems":
$api = new ApiService($em);
$pageData['assortment'] = json_decode($api->getApiServiceData('getAssortment', ["activeOnly"=>false, "locale"=>"pl"]), true);
break;
}
if (!$ajaxRequest) {
$data = [
"leftsideMenu" => $this->get('twig')->render("/_pages/partials/leftsideMenu.html.twig", $pageData),
"header" => $this->get('twig')->render("/_pages/partials/baseHeader.html.twig", $pageData),
"footer" => $this->get('twig')->render("/_pages/partials/baseFooter.html.twig", $pageData),
"pageScripts" => $this->get('twig')->render("/appArea_reports/scripts/reports.html.twig")
];
$pageData['html'] = $data;
return $this->render("/appArea_reports/pages/".$area.".html.twig", $pageData);
} else {
$data = [
"pageTitle" => "xPerto_PLATE",
"pageBody" => $this->get('twig')->render("/appArea_reports/bodies/".$area.".html.twig", $pageData),
"pageScripts" => $this->get('twig')->render("/appArea_reports/scripts/reports.html.twig")
];
$pageData['html'] = $data;
return new JsonResponse($pageData);
}
}
/**
* @Route("/offline.html", name="offline")
*/
public function offline(Request $request) {
return $this->render("/_pages/offline.html.twig");
}
/**
* @Route("/app/login", name="login")
*/
public function login(Request $request, TranslatorInterface $translator) {
if ( $this->getUser() ){ return $this->redirect('/'); }
return $this->render("/_pages/login.html.twig", ['locale'=>'pl'] );
}
}