<?php
namespace App\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Pimcore\Translation\Translator;
use Pimcore\Localization\LocaleServiceInterface;
use Pimcore\Analytics\Google\Config\SiteConfigProvider;
use App\Helper\DocumentHelper;
use App\Helper\Json\ObjectJson;
use App\Services\LanguageService;
use App\Services\LiveChatService;
use App\Services\CodeConfigService;
use Pimcore\Tool;
use Symfony\Component\HttpFoundation\StreamedResponse;
use Pimcore\Model\Asset\Image;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
/**
* @Route("/api/site")
*/
class SiteController extends BaseController
{
CONST INCLUDE_NAME = 'includes';
CONST PAGE_NAME = 'pages';
protected $translator;
protected $locale;
public function __construct(
Translator $translator,
LocaleServiceInterface $localeService
)
{
$this->translator = $translator;
$this->locale = $localeService->getLocale();
}
/**
* @Route("/flag-svg/{lang}", methods={"GET"})
*/
public function getFlag($lang)
{
try {
$file = PIMCORE_PROJECT_ROOT . "/public" . Tool::getLanguageFlagFile($lang, false);
$response = new BinaryFileResponse($file);
return $response;
} catch (\Throwable $e) {
}
return $this->sendResponse();
}
/**
* @Route("/home", methods={"GET"})
*/
public function getHome(
Request $request,
NewsController $newController,
EventController $eventController
)
{
$data = [];
$site = $this->getSite($request);
if ($site) {
$rootDocument = $site->getRootDocument();
$path = $rootDocument->getRealFullpath() ."/". $this->locale;
$data = DocumentHelper::getDataByPath($path);
$siteName = $site->getRootDocument()->getKey();
$class = str_replace(__NAMESPACE__, __NAMESPACE__ .'\\'. $siteName, __CLASS__);
$class = $this->checkMiniSiteClass($class, $siteName);
if (class_exists($class)) {
$controller = new $class($data, $site, $this->locale);
$method = __FUNCTION__;
if (method_exists($controller, $method)) {
$data = $controller->{$method}();
}
}
return $this->sendResponse($data);
}
return $this->returnNotFound();
}
/**
* Get custom data of header and footer of <domain>
* path in admin: Documents/<domain>/<locale>/<includes>/<header|footer>
*
* @Route("/layout", methods={"GET"})
*/
public function getLayout(Request $request)
{
$data = [
'header' => [],
'footer' => []
];
$site = $this->getSite($request);
if ($site) {
$rootDocument = $site->getRootDocument();
$prefixPath = $rootDocument->getRealFullpath() ."/". $this->locale ."/". self::INCLUDE_NAME;
foreach ($data as $partial => $value) {
$path = $prefixPath ."/". $partial;
$data[$partial] = DocumentHelper::getDataByPath($path);
}
// get language list
list($list, $contents) = LanguageService::getList($site);
$data['languages'] = $list;
$data['langContent'] = $contents;
$data['liveChats'] = LiveChatService::getList($site, $this->locale);
$data['codeConfig'] = CodeConfigService::getConfig($site);
$siteName = $site->getRootDocument()->getKey();
$class = str_replace(__NAMESPACE__, __NAMESPACE__ .'\\'. $siteName, __CLASS__);
$class = $this->checkMiniSiteClass($class, $siteName);
if (class_exists($class)) {
$controller = new $class($data, $site, $this->locale);
$method = __FUNCTION__;
if (method_exists($controller, $method)) {
$data = $controller->{$method}($this->translator);
}
}
return $this->sendResponse($data);
}
return $this->returnNotFound();
}
/**
* Get custom data of page by using name
* "name" is key of page object in admin: Documents/<domain>/<locale>/<pages_folder>/<name>
*
* @Route("/page/get-by-name", methods={"GET"})
*/
public function getPageByName(Request $request)
{
$data = [];
$site = $this->getSite($request);
$name = $request->get('name');
if ($site && $name) {
$rootDocument = $site->getRootDocument();
$path = $rootDocument->getRealFullpath() ."/". $this->locale ."/". self::PAGE_NAME ."/". $name;
$data = DocumentHelper::getDataByPath($path);
$siteName = $site->getRootDocument()->getKey();
$class = str_replace(__NAMESPACE__, __NAMESPACE__ .'\\'. $siteName, __CLASS__);
$class = $this->checkMiniSiteClass($class, $siteName);
if (class_exists($class)) {
$controller = new $class($data, $site, $this->locale, $request);
$method = __FUNCTION__;
if (method_exists($controller, $method)) {
$data = $controller->{$method}($name);
}
}
return $this->sendResponse($data);
}
return $this->returnNotFound();
}
/**
* @Route("/marketing/config", methods={"GET"})
*/
public function getMarketingConfig(Request $request, SiteConfigProvider $siteConfigProvider)
{
$site = $this->getSite($request);
if ($site) {
$config = $siteConfigProvider->getSiteConfig($site);
if ($config) {
return $this->sendResponse($config->toArray());
}
return $this->sendResponse();
}
return $this->returnNotFound();
}
/**
* @Route("/site-map", methods={"GET"})
*/
public function getSiteMap(Request $request)
{
$data = [];
$site = $this->getSite($request);
if ($site) {
$siteName = $site->getRootDocument()->getKey();
$class = str_replace(__NAMESPACE__, __NAMESPACE__ .'\\'. $siteName, __CLASS__);
$class = $this->checkMiniSiteClass($class, $siteName);
if (class_exists($class)) {
$controller = new $class($data, $site, $this->locale, $request);
$method = __FUNCTION__;
if (method_exists($controller, $method)) {
$data = $controller->{$method}();
}
}
return $this->sendResponse($data);
}
return $this->returnNotFound();
}
/**
* @Route("/check-redirects", methods={"GET"})
*/
public function checkRedirects(Request $request)
{
$data = [];
$site = $this->getSite($request);
if ($site) {
$siteName = $site->getRootDocument()->getKey();
$class = str_replace(__NAMESPACE__, __NAMESPACE__ .'\\'. $siteName, __CLASS__);
$class = $this->checkMiniSiteClass($class, $siteName);
if (class_exists($class)) {
$controller = new $class($data, $site, $this->locale);
$method = __FUNCTION__;
if (method_exists($controller, $method)) {
$data = $controller->{$method}($request);
}
}
return $this->sendResponse($data);
}
return $this->returnNotFound();
}
}