<?php
declare(strict_types=1);
/*
* Contao Panorama bundle for Contao Open Source CMS
*
* Copyright (c) 2022 pdir / digital agentur // pdir GmbH
*
* @package panorama-bundle
* @link https://pdir.de/panorama-bundle/
* @license pdir license - All-rights-reserved - commercial extension
* @author Mathias Arzberger <develop@pdir.de>
* @author Christian Mette <develop@pdir.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Pdir\PanoramaBundle\Controller\ContentElement;
use Contao\Combiner;
use Contao\Config;
use Contao\ContentModel;
use Contao\Controller;
use Contao\CoreBundle\DependencyInjection\Attribute\AsContentElement;
use Contao\Environment;
use Contao\FilesModel;
use Contao\Input;
use Contao\System;
use Contao\Template;
use Pdir\KrPanoBundle\Controller\ContentElement\KrPanoramaController;
use Pdir\KrPanoBundle\Model\ConfigModel;
use Pdir\KrPanoBundle\Model\PanoramaModel;
use Pdir\PanoramaBundle\Model\PanoCityMapModel;
use Pdir\PanoramaBundle\Model\PanoCityModel;
use Pdir\PanoramaBundle\Model\PanoramaExtendedModel;
use Pdir\PanoramaBundle\Model\PanoTimeperiodModel;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
#[AsContentElement(category:'pdir', template:'ce_krpanorama')]
class KrPanoramaExtController extends KrPanoramaController
{
/**
* Assets directory.
*
* @var string
*/
protected $extAssetsDir = 'bundles/pdirpanorama';
public function retrievePanoramaAndConfigByAutoItem(): void
{
// Retrieve current item by auto_item request parameter
if (Config::get('useAutoItem') && ($autoItem = Input::get('auto_item'))) {
// tell contao that the auto_item parameter has been used -> else a UnusedArgumentsException is thrown resulting in a 404 response
Input::get('auto_item');
}
// get the panorama data record
/** @var PanoramaModel $this->objPanoPanorama */
$this->objPanoPanorama = PanoramaModel::findByIdOrAlias($autoItem);
// get the panorama configuration
$this->objConfig = ConfigModel::findByPk($this->model->pdir_pano_config_id);
// check whether the viewer directory has been overwritten
if (isset($this->objConfig->override_krpanojs)) {
// user has own krpano viewer activated - krpanojsSRC is mandatory
if ($this->objConfig->krpanojsSRC) {
// get the path
$this->viewerDir = FilesModel::findByUuid($this->objConfig->krpanojsSRC)->path;
}
}
}
protected function getResponse(Template $template, ContentModel $model, Request $request): Response
{
$request = System::getContainer()->get('request_stack')->getCurrentRequest();
$this->template = $template;
$this->model = $model;
$this->page = $this->getPageModel();
System::loadLanguageFile('tl_pano_panorama');
$this->retrievePanoramaAndConfigByAutoItem();
// Display a wildcard in the back end.
if ($request && System::getContainer()->get('contao.routing.scope_matcher')->isBackendRequest($request)) {
$this->renderBackendView($model->id, $model->pdir_pano_debug);
}
if ($request && System::getContainer()->get('contao.routing.scope_matcher')->isFrontendRequest($request) && ($model->invisible || null === $this->objPanoPanorama || '1' !== $this->objPanoPanorama->published)) {
return new Response('');
}
if ($request && System::getContainer()->get('contao.routing.scope_matcher')->isFrontendRequest($request)) {
$this->renderPanoramaView();
$this->renderExtendedPanoramaView();
}
return $this->template->getResponse();
}
protected function renderExtendedPanoramaView(): void
{
// overwrite element id to use split mode
$this->template->contentId = 1;
$combinedJS = new Combiner();
// render timeline
if ($this->model->pdir_pano_timeline) {
$this->template->timeline = $this->generateTimeline();
$GLOBALS['TL_CSS'][] = $this->extAssetsDir.'/css/timeline.scss|static';
}
// render control panel
if ($this->model->pdirPanoControlPanel) {
$combinedJS->add($this->extAssetsDir.'/js/control-panel.js');
$GLOBALS['TL_CSS'][] = $this->extAssetsDir.'/css/control-panel.scss|static';
$this->template->controlPanel = $this->renderView(
'@PdirPanorama/mod_pano_control_panel_view.twig',
[
'panoName' => $this->objPanoPanorama->name,
'panoDescription' => $this->objPanoPanorama->description,
'panoLink' => null !== $this->objPanoPanorama ? Environment::get('url').'/'.Controller::generateFrontendUrl($this->page->row(), '/'.$this->objPanoPanorama->alias) : null,
]
);
}
// render map view
$GLOBALS['TL_CSS'][] = $this->extAssetsDir.'/css/catalog.scss|static';
$GLOBALS['TL_CSS'][] = $this->extAssetsDir.'/css/card.scss|static';
$GLOBALS['TL_CSS'][] = $this->extAssetsDir.'/css/map-container.scss|static';
$GLOBALS['TL_CSS'][] = $this->extAssetsDir.'/css/panorama.scss|static';
$combinedJS->add($this->extAssetsDir.'/js/helper.js');
$combinedJS->add($this->extAssetsDir.'/js/map.js');
$combinedJS->add($this->extAssetsDir.'/js/sidebar.js');
$GLOBALS['TL_JAVASCRIPT'][] = 'https://maps.googleapis.com/maps/api/js?key='.$this->model->pano_google_maps_api_key;
$GLOBALS['TL_JAVASCRIPT'][] = $this->extAssetsDir.'/js/gmap.js|static';
$GLOBALS['TL_JAVASCRIPT'][] = $this->extAssetsDir.'/js/markercluster.js|static';
$objCityModel = PanoCityModel::findOneBy('id', $this->objPanoPanorama->city);
if (null === $objCityModel) {
// @todo allow pano only mode
// throw new InvalidArgumentException('NO CITY SELECTED!');
}
// get city map model from pdirPanoCityMap
$cityMap = null;
if ($this->model->pdirPanoCityMap) {
$cityMap = PanoCityMapModel::findOneById($this->model->pdirPanoCityMap);
}
//dump($objCityModel);
//dump($cityMap);
//dump($cityMap->id.'/catalog/1');
// lade city map from pano object if pdirPanoCityMap is not set
if (null === $cityMap) {
$cityMap = PanoCityMapModel::findBy(
['city=?', 'time_period=?'],
[$this->objPanoPanorama->city, $this->objPanoPanorama->time_period],
);
}
//dump($cityMap);
//dump($cityMap->getOverlayImage);
if (null === $cityMap) {
// @todo allow pano only mode
// throw new InvalidArgumentException('NO CITY MAP AVAILABLE!');
}
// fix overlayImage
if($cityMap->overlay_imag) {
$cityMap->overlayImage = $cityMap->overlay_imag;
}
if (null !== $cityMap && null !== $objCityModel) {
$timePeriod = PanoTimeperiodModel::findOneBy('id', $this->objPanoPanorama->time_period);
$this->template->mapView = $this->renderView(
'@PdirPanorama/mod_pano_map_view.twig',
[
'city' => $objCityModel,
'cityMap' => $cityMap,
'name' => $objCityModel->name,
'actYear' => $this->getTimePeriodYear($timePeriod),
'yearLinkText' => $timePeriod->yearLinkText,
'timePeriod' => $timePeriod?->id,
'iCatalogPageId' => 77,
'sCatalogUri' => 'dresden/ajax/ajaxgetcatalog/973ea6f32f8e2ae501b061fab480e805/?tx_arstempanocityselection_cityplugin%5BbPanoCatalog%5D=1',
'cat' => '2',
'iControlPanel' => 1,
'overlayImage' => $cityMap->overlay_image,
'url' => Environment::get('url'),
'catalogUri' => $cityMap->id.'/catalog/1',
]
);
}
// add assets
$GLOBALS['TL_BODY'][] = '<script src="'.$combinedJS->getCombinedFile().'"></script>';
$GLOBALS['TL_CSS'][] = $this->extAssetsDir.'/fontawesome/css/brands.min.css|static';
}
protected function getTimePeriodYear(PanoTimeperiodModel $model)
{
if (null === $model) {
return date('Y', time());
}
return $model->year;
}
/**
* generate a timeline html and return it as string.
*/
private function generateTimeline(): string
{
$timelineValues = [];
$panoTimes = unserialize($this->model->pdirPanoTimes);
foreach ($panoTimes as $time) {
/** @var PanoTimeperiodModel $timeperiodModel */
$timeperiodModel = PanoTimeperiodModel::findByPk($time);
// find panorama by catalog category
$panorama = PanoramaExtendedModel::findOneBy(['selectioncatalog = ?', 'time_period = ?'], [$this->objPanoPanorama->selectioncatalog, $timeperiodModel->id]);
$timelineValues[] = [
'id' => $timeperiodModel->id,
'desc' => $timeperiodModel->desciption,
'year' => $timeperiodModel->year,
'label' => !empty($timeperiodModel->yearLinkText) ? $timeperiodModel->yearLinkText : $timeperiodModel->year,
'link' => null !== $panorama ? null : Controller::generateFrontendUrl($this->page->row(), '/'.$panorama->alias),
'class' => null === $panorama ? 'hidden' : ($time === $this->objPanoPanorama->time_period ? '' : 'inactive'),
];
}
return $this->renderView(
'@PdirPanorama/mod_pano_timeline_view.twig',
[
'times' => $timelineValues,
'krpanoId' => 1, // $this->template->contentId,
'activeTimePeriod' => $this->objPanoPanorama->time_period,
'dualView' => $this->model->pdirPanoDualView,
]
);
}
}