vendor/pdir/krpano-bundle/src/Controller/ContentElement/KrPanoramaController.php line 67

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4.  * Contao KrPano bundle for Contao Open Source CMS
  5.  *
  6.  * Copyright (c) 2022 pdir / digital agentur // pdir GmbH
  7.  *
  8.  * @package    krpano-bundle
  9.  * @link       https://pdir.de/krpano-bundle/
  10.  * @license    LGPL-3.0-or-later
  11.  * @author     Mathias Arzberger <develop@pdir.de>
  12.  * @author     Christian Mette <develop@pdir.de>
  13.  *
  14.  * For the full copyright and license information, please view the LICENSE
  15.  * file that was distributed with this source code.
  16.  */
  17. namespace Pdir\KrPanoBundle\Controller\ContentElement;
  18. use Contao\BackendTemplate;
  19. use Contao\ContentModel;
  20. use Contao\CoreBundle\Controller\ContentElement\AbstractContentElementController;
  21. use Contao\CoreBundle\DependencyInjection\Attribute\AsContentElement;
  22. use Contao\FilesModel;
  23. use Contao\PageModel;
  24. use Contao\StringUtil;
  25. use Contao\System;
  26. use Contao\Template;
  27. use Pdir\KrPanoBundle\Model\ConfigModel;
  28. use Pdir\KrPanoBundle\Model\PanoramaModel;
  29. use Symfony\Component\HttpFoundation\Request;
  30. use Symfony\Component\HttpFoundation\Response;
  31. #[AsContentElement(category:'pdir'template:'ce_krpanorama')]
  32. class KrPanoramaController extends AbstractContentElementController
  33. {
  34.     // set the template name
  35.     public $strTemplate 'ce_krpanorama';
  36.     public null|PageModel $page;
  37.     public null|Template $template;
  38.     public null|ContentModel $model;
  39.     // set the assets dir
  40.     protected $assetsDir 'bundles/pdirkrpano';
  41.     // sets the source directory for the html5 viewer
  42.     protected $viewerDir 'bundles/pdirkrpano/krpano/viewer';
  43.     // sets the source directory for all plugins
  44.     protected $pluginsDir '/bundles/pdirkrpano/krpano/viewer/plugins';
  45.     // sets the source directory for the default skin
  46.     protected $skinDir '/bundles/pdirkrpano/krpano/skins/myskin';
  47.     // sets the pano source.xml for an empty panorama
  48.     protected $emptyPano '/bundles/pdirkrpano/krpano/skins/myskin/empty_pano.xml';
  49.     protected $objPanoPanorama;
  50.     protected $objConfig;
  51.     /**
  52.      * Retrieve the panorama and config model.
  53.      */
  54.     public function retrievePanoramaAndConfig(): void
  55.     {
  56.         // get the panorama data record
  57.         /** @var PanoramaModel $this->objPanoPanorama */
  58.         $this->objPanoPanorama PanoramaModel::findByPk($this->model->pdir_pano_panorama_id);
  59.         // get the panorama configuration
  60.         $this->objConfig ConfigModel::findByPk($this->model->pdir_pano_config_id);
  61.         // check whether the viewer directory has been overwritten
  62.         if (isset($this->objConfig->override_krpanojs)) {
  63.             // user has own krpano viewer activated - krpanojsSRC is mandatory
  64.             if ($this->objConfig->krpanojsSRC) {
  65.                 // get the path
  66.                 $this->viewerDir FilesModel::findByUuid($this->objConfig->krpanojsSRC)->path;
  67.             }
  68.         }
  69.     }
  70.     /**
  71.      * Render a wildcard in backend view.
  72.      */
  73.     public function renderBackendView($id$debug): void
  74.     {
  75.         $this->template = new BackendTemplate('be_wildcard');
  76.         $this->template->wildcard sprintf(
  77.             '### %s - Panorama-Id: %s (%s) ###%s',
  78.             $this->objPanoPanorama->name ?? 'NO PANORAMA SET',
  79.             $id ?? '-',
  80.             $this->objConfig->title ?? 'NO CONFIG SET',
  81.             $debug ' <span style="color:red;"> (DEBUG-MODE)</span> ###' ''
  82.         );
  83.     }
  84.     public function renderPanoramaView(): void
  85.     {
  86.         // set the customTpl template
  87.         if ($this->model->customTpl) {
  88.             $this->template->strTemplate $this->model->customTpl;
  89.         }
  90.         // prevent accumulation of CSS ToDo: move this stuff into config?
  91.         $feCSS $this->assetsDir.'/css/fe_krpano.scss|static';
  92.         if (!isset($GLOBALS['TL_CSS']['fe_krpano'])) {
  93.             $GLOBALS['TL_CSS']['fe_krpano'] = $feCSS;
  94.         }
  95.         // prevent accumulation of javascript
  96.         $krpanojs $this->viewerDir.'/krpano.js';
  97.         $fekrpanojs $this->assetsDir.'/js/fe_krpano.js';
  98.         if (!isset($GLOBALS['TL_JAVASCRIPT']['krpano'])) {
  99.             $GLOBALS['TL_JAVASCRIPT']['krpano'] = $krpanojs;
  100.         }
  101.         if (!isset($GLOBALS['TL_JAVASCRIPT']['fe_krpano'])) {
  102.             $GLOBALS['TL_JAVASCRIPT']['fe_krpano'] = $fekrpanojs;
  103.         }
  104.         $this->template->errorTitle $GLOBALS['TL_LANG']['tl_pano_panorama']['errorTitle'];
  105.         $this->template->errorText $GLOBALS['TL_LANG']['tl_pano_panorama']['errorText'];
  106.         $this->template->viewerDir $this->viewerDir;
  107.         $this->template->pluginsDir $this->pluginsDir;
  108.         $this->template->skinDir $this->skinDir;
  109.         $this->template->contentId $this->model->id;
  110.         // use the panorama_id for xml requests
  111.         $this->template->panoramaId $this->model->pdir_pano_panorama_id;
  112.         $this->template->configId $this->model->pdir_pano_config_id;
  113.         // arstempano fix @todo remove if not needed!
  114.         $hlookat $this->model->pdir_pano_hlookat;
  115.         if ($hlookat 180) {
  116.             $hlookat -= 360;
  117.         }
  118.         if ($hlookat < -180) {
  119.             $hlookat += 360;
  120.         }
  121.         $vlookat $this->model->pdir_pano_vlookat;
  122.         if ($vlookat 180) {
  123.             $vlookat -= 360;
  124.         }
  125.         if ($vlookat < -180) {
  126.             $vlookat += 360;
  127.         }
  128.         // check overriding view parameter
  129.         if ('1' === $this->model->pdir_pano_override_view) {
  130.             // use tl_content view parameters
  131.             $this->template->hlookat $hlookat;
  132.             $this->template->vlookat $vlookat;
  133.             $this->template->fov $this->model->pdir_pano_fov;
  134.         } else {
  135.             // use tl_pano_panorama view parameters
  136.             $this->template->hlookat $this->objPanoPanorama->start_poh;
  137.             $this->template->vlookat $this->objPanoPanorama->start_pov;
  138.             $this->template->fov $this->objPanoPanorama->start_zoom;
  139.         }
  140.         // calculate xml uri
  141.         $this->template->panoUri FilesModel::findByUuid($this->objPanoPanorama->pano_xml)->path;
  142.         if ($this->template->panoUri) {
  143.             $this->template->panoUri "krpano/config/{$this->template->configId}/panorama/{$this->template->panoramaId}";
  144.         } else {
  145.             $this->template->panoUri $this->emptyPano;
  146.             $this->template->hlookat 0.0;
  147.             $this->template->vlookat 0.0;
  148.             $this->template->fov 150.0;
  149.         }
  150.         // set browser session history
  151.         $this->template->jumpTo false;
  152.         if ($this->model->pdir_pano_history) {
  153.             $this->template->urlSuffix System::getContainer()->getParameter('contao.url_suffix');
  154.             // use current page
  155.             $this->template->jumpTo $this->page->alias;
  156.             // use redirect page
  157.             if ($this->model->pdir_pano_history_jump_to) {
  158.                 $this->template->jumpTo PageModel::findById($this->pdir_pano_history_jump_to)->alias;
  159.             }
  160.         }
  161.         $cssID StringUtil::deserialize($this->model->cssIDtrue);
  162.         $this->template->cssID = !empty($cssID[0]) ? {$cssID[0]}'';
  163.         $this->template->cssClass = !empty($cssID[1]) ? {$cssID[1]}'';
  164.         $this->template->tlMode TL_MODE;
  165.         // prepare debug data
  166.         $this->template->debug = (bool) $this->model->pdir_pano_debug;
  167.         $this->template->debugHtml sprintf(
  168.             '<div id="debug_%s" class="overlay debug"></div>',
  169.             $this->model->id
  170.         );
  171.         $this->template->invisible $this->model->invisible;
  172.     }
  173.     protected function getResponse(Template $templateContentModel $modelRequest $request): ?Response
  174.     {
  175.         $request System::getContainer()->get('request_stack')->getCurrentRequest();
  176.         $this->template $template;
  177.         $this->model $model;
  178.         $this->page $this->getPageModel();
  179.         System::loadLanguageFile('tl_pano_panorama');
  180.         $this->retrievePanoramaAndConfig();
  181.         // Display a wildcard in the back end.
  182.         if ($request && System::getContainer()->get('contao.routing.scope_matcher')->isBackendRequest($request)) {
  183.             $this->renderBackendView($model->id$model->pdir_pano_debug);
  184.         }
  185.         if ($request && System::getContainer()->get('contao.routing.scope_matcher')->isFrontendRequest($request) && ($model->invisible || null === $this->objPanoPanorama || '1' !== $this->objPanoPanorama->published)) {
  186.             return new Response('');
  187.         }
  188.         if ($request && System::getContainer()->get('contao.routing.scope_matcher')->isFrontendRequest($request)) {
  189.             $this->renderPanoramaView();
  190.         }
  191.         return $this->template->getResponse();
  192.     }
  193. }