src/Controller/SiteController.php line 103

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Component\HttpFoundation\Request;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Symfony\Component\Routing\Annotation\Route;
  6. use Pimcore\Translation\Translator;
  7. use Pimcore\Localization\LocaleServiceInterface;
  8. use Pimcore\Analytics\Google\Config\SiteConfigProvider;
  9. use App\Helper\DocumentHelper;
  10. use App\Helper\Json\ObjectJson;
  11. use App\Services\LanguageService;
  12. use App\Services\LiveChatService;
  13. use App\Services\CodeConfigService;
  14. use App\Services\TxtConfigService;
  15. use Pimcore\Tool;
  16. use Symfony\Component\HttpFoundation\StreamedResponse;
  17. use Pimcore\Model\Asset\Image;
  18. use Symfony\Component\HttpFoundation\BinaryFileResponse;
  19. /**
  20.  * @Route("/api/site")
  21.  */
  22. class SiteController extends BaseController
  23. {
  24.     CONST INCLUDE_NAME 'includes';
  25.     CONST PAGE_NAME 'pages';
  26.     protected $translator;
  27.     protected $locale;
  28.     public function __construct(
  29.         Translator $translator,
  30.         LocaleServiceInterface $localeService
  31.     )
  32.     {
  33.         $this->translator $translator;
  34.         $this->locale $localeService->getLocale();
  35.     }
  36.     /**
  37.      * @Route("/flag-svg/{lang}", methods={"GET"})
  38.      */
  39.     public function getFlag($lang)
  40.     {
  41.         try {
  42.             $file PIMCORE_PROJECT_ROOT "/public" Tool::getLanguageFlagFile($langfalse);
  43.             $response = new BinaryFileResponse($file);
  44.             return $response;
  45.         } catch (\Throwable $e) {
  46.             
  47.         }
  48.         return $this->sendResponse();
  49.     }
  50.     /**
  51.      * @Route("/home", methods={"GET"})
  52.      */
  53.     public function getHome(
  54.         Request $request,
  55.         NewsController $newController,
  56.         EventController $eventController
  57.     )
  58.     {
  59.         $data = [];
  60.         $site $this->getSite($request);
  61.         if ($site) {
  62.             $rootDocument $site->getRootDocument();
  63.             $path $rootDocument->getRealFullpath() ."/"$this->locale;
  64.             $data DocumentHelper::getDataByPath($path);
  65.             $siteName $site->getRootDocument()->getKey();
  66.             $class str_replace(__NAMESPACE____NAMESPACE__ .'\\'$siteName__CLASS__);
  67.             $class $this->checkMiniSiteClass($class$siteName);
  68.             if (class_exists($class)) {
  69.                 $controller = new $class($data$site$this->locale);
  70.                 $method __FUNCTION__;
  71.                 if (method_exists($controller$method)) {
  72.                     $data $controller->{$method}();
  73.                 }
  74.             }
  75.             return $this->sendResponse($data);
  76.         }
  77.         return $this->returnNotFound();
  78.     }
  79.     /**
  80.      * Get custom data of header and footer of <domain>
  81.      * path in admin: Documents/<domain>/<locale>/<includes>/<header|footer>
  82.      * 
  83.      * @Route("/layout", methods={"GET"})
  84.      */
  85.     public function getLayout(Request $request)
  86.     {
  87.         $data = [
  88.             'header' => [],
  89.             'footer' => []
  90.         ];
  91.         $site $this->getSite($request);
  92.         if ($site) {
  93.             $rootDocument $site->getRootDocument();
  94.             $prefixPath $rootDocument->getRealFullpath() ."/"$this->locale ."/"self::INCLUDE_NAME;
  95.             foreach ($data as $partial => $value) {
  96.                 $path $prefixPath ."/"$partial;
  97.                 $data[$partial] = DocumentHelper::getDataByPath($path);
  98.             }
  99.             // get language list
  100.             list($list$contents) = LanguageService::getList($site);
  101.             $data['languages'] = $list;
  102.             $data['langContent'] = $contents;
  103.             $data['liveChats'] = LiveChatService::getList($site$this->locale);
  104.             $data['codeConfig'] = CodeConfigService::getConfig($site);
  105.             $siteName $site->getRootDocument()->getKey();
  106.             $class str_replace(__NAMESPACE____NAMESPACE__ .'\\'$siteName__CLASS__);
  107.             $class $this->checkMiniSiteClass($class$siteName);
  108.             if (class_exists($class)) {
  109.                 $controller = new $class($data$site$this->locale);
  110.                 $method __FUNCTION__;
  111.                 if (method_exists($controller$method)) {
  112.                     $data $controller->{$method}($this->translator);
  113.                 }
  114.             }
  115.             return $this->sendResponse($data);
  116.         }
  117.         return $this->returnNotFound();
  118.     }
  119.     /**
  120.      * Get custom data of page by using name
  121.      * "name" is key of page object in admin: Documents/<domain>/<locale>/<pages_folder>/<name>
  122.      * 
  123.      * @Route("/page/get-by-name", methods={"GET"})
  124.      */
  125.     public function getPageByName(Request $request)
  126.     {
  127.         $data = [];
  128.         $site $this->getSite($request);
  129.         $name $request->get('name');
  130.         if ($site && $name) {
  131.             $rootDocument $site->getRootDocument();
  132.             $path $rootDocument->getRealFullpath() ."/"$this->locale ."/"self::PAGE_NAME ."/"$name;
  133.             $data DocumentHelper::getDataByPath($path);
  134.             $siteName $site->getRootDocument()->getKey();
  135.             $class str_replace(__NAMESPACE____NAMESPACE__ .'\\'$siteName__CLASS__);
  136.             $class $this->checkMiniSiteClass($class$siteName);
  137.             if (class_exists($class)) {
  138.                 $controller = new $class($data$site$this->locale$request);
  139.                 $method __FUNCTION__;
  140.                 if (method_exists($controller$method)) {
  141.                     $data $controller->{$method}($name);
  142.                 }
  143.             }
  144.             return $this->sendResponse($data);
  145.         }
  146.         return $this->returnNotFound();
  147.     }
  148.     /**
  149.      * @Route("/marketing/config", methods={"GET"})
  150.      */
  151.     public function getMarketingConfig(Request $requestSiteConfigProvider $siteConfigProvider)
  152.     {
  153.         $site $this->getSite($request);
  154.         if ($site) {
  155.             $config $siteConfigProvider->getSiteConfig($site);
  156.             if ($config) {
  157.                 return $this->sendResponse($config->toArray());
  158.             }
  159.             return $this->sendResponse();
  160.         }
  161.         return $this->returnNotFound();
  162.     }
  163.     /**
  164.      * @Route("/site-map", methods={"GET"})
  165.      */
  166.     public function getSiteMap(Request $request)
  167.     {
  168.         $data = [];
  169.         $site $this->getSite($request);
  170.         if ($site) {
  171.             $siteName $site->getRootDocument()->getKey();
  172.             $class str_replace(__NAMESPACE____NAMESPACE__ .'\\'$siteName__CLASS__);
  173.             $class $this->checkMiniSiteClass($class$siteName);
  174.             if (class_exists($class)) {
  175.                 $controller = new $class($data$site$this->locale$request);
  176.                 $method __FUNCTION__;
  177.                 if (method_exists($controller$method)) {
  178.                     $data $controller->{$method}();
  179.                 }
  180.             }
  181.             return $this->sendResponse($data);
  182.         }
  183.         return $this->returnNotFound();
  184.     }
  185.     /**
  186.      * @Route("/check-redirects", methods={"GET"})
  187.      */
  188.     public function checkRedirects(Request $request)
  189.     {
  190.         $data = [];
  191.         $site $this->getSite($request);
  192.         if ($site) {
  193.             $siteName $site->getRootDocument()->getKey();
  194.             $class str_replace(__NAMESPACE____NAMESPACE__ .'\\'$siteName__CLASS__);
  195.             $class $this->checkMiniSiteClass($class$siteName);
  196.             if (class_exists($class)) {
  197.                 $controller = new $class($data$site$this->locale);
  198.                 $method __FUNCTION__;
  199.                 if (method_exists($controller$method)) {
  200.                     $data $controller->{$method}($request);
  201.                 }
  202.             }
  203.             return $this->sendResponse($data);
  204.         }
  205.         return $this->returnNotFound();
  206.     }
  207.     /**
  208.      * Get txt config document
  209.      * path in admin: Documents/<domain>/settings/txtConfig
  210.      * 
  211.      * @Route("/txt-config", methods={"GET"})
  212.      */
  213.     public function getTxtConfig(Request $request)
  214.     {
  215.         $site $this->getSite($request);
  216.         if ($site) {
  217.             $data TxtConfigService::getConfig($site);
  218.             return $this->sendResponse($data);
  219.         }
  220.         return $this->returnNotFound();
  221.     }
  222. }