src/Controller/SiteController.php line 27

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