a2g_travel_blog/Classes/Controller/MapController.php
2023-12-04 16:40:27 +01:00

473 lines
20 KiB
PHP

<?php
declare(strict_types=1);
namespace A2G\A2gTravelBlog\Controller;
use \A2G\A2gMaps\Domain\Traits\InjectMarkerRepositoryTrait;
use A2G\A2gTravelBlog\Domain\Traits\{
InjectMapEntryRepositoryTrait,
InjectTravelAuthorRepositoryTrait,
InjectImageServiceTrait,
InjectTravelPostRepositoryTrait,
InjectTravelCategoryRepositoryTrait,
};
use A2G\A2gMaps\Utility\MapConfigUtility;
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder;
use TYPO3\CMS\Extbase\Object\ObjectManager;
use TYPO3\CMS\Extbase\Mvc\Controller\ControllerContext;
use TYPO3\CMS\Fluid\View\TemplateView;
use TYPO3\CMS\Fluid\View\StandaloneView;
use T3G\AgencyPack\Blog\Constants;
use TYPO3\CMS\Extbase\Mvc\Request;
use TYPO3\CMS\Core\Utility\MathUtility;
use TYPO3\CMS\Core\MetaTag\MetaTagManagerRegistry;
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
/**
* This file is part of the "Products" Extension for TYPO3 CMS.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* (c) 2021 Raphael Martin <raphy.martin@gmail.com>, none
*/
/**
* MapController
*/
class MapController extends ActionController {
use InjectMarkerRepositoryTrait;
use InjectMapEntryRepositoryTrait;
use InjectTravelAuthorRepositoryTrait;
use InjectTravelPostRepositoryTrait;
use InjectTravelCategoryRepositoryTrait;
use InjectImageServiceTrait;
private static $mapIcons = null;
/**
*
* @param UriBuilder $uriBuilder
* @param TemplateView $sourceView
* @param type $arguments
* @param type $actionName
* @param type $controllerName
* @return string
*/
static private function generateContent(UriBuilder $uriBuilder, TemplateView &$sourceView, $arguments = [], $actionName = 'popup', $controllerName = 'Map'): StandaloneView {
$templatePath = $controllerName . '/' . ucfirst($actionName);
$objectManager = GeneralUtility::makeInstance(ObjectManager::class);
$templateRootPaths = $sourceView->getTemplateRootPaths();
$context = $objectManager->get(ControllerContext::class);
$request = $objectManager->get(Request::class);
$context->setRequest($request);
$context->setUriBuilder($uriBuilder);
/**
* @var StandaloneView $view
*/
$view = $objectManager->get(StandaloneView::class);
$view->setControllerContext($context);
$view->setLayoutRootPaths($sourceView->getLayoutRootPaths());
$view->setTemplateRootPaths($templateRootPaths);
$view->setPartialRootPaths($sourceView->getPartialRootPaths());
$view->setTemplate($templatePath);
$arguments['today'] = (new \DateTime())->format('d.m.Y');
$view->assignMultiple($arguments);
return $view;
}
private function setMapMarkerIcons() {
if (self::$mapIcons === null) {
self::$mapIcons = [];
$markers = $this->markerRepository->findAll();
foreach ($markers as $marker) {
if ($marker->getMapIcon() !== null) {
self::$mapIcons[] = [
'uid' => 'i-' . $marker->getUid(),
'icon' => $this->imageService->getImageUri($this->imageService->applyProcessingInstructions($this->imageService->getImage('', $marker->getMapIcon(), false), []), true)
];
}
}
}
}
/**
* action map
*
* @return string|object|null|void
*/
public function mapAction() {
$layerIds = explode(',', $this->settings['layers']);
$layerConfigs = MapConfigUtility::getMapLayerConfig($layerIds);
if ($this->settings['useLayerSwitcher'] === '1') {
for ($i = 0; $i < count($layerConfigs); $i++) {
$layerConfigs[$i]['active'] = true;
}
}
$this->uriBuilder->setTargetPageType(1652369512);
$centerLat = 0.0;
$centerLon = 0.0;
switch ((int) $this->settings['mapType']) {
case 0:
$markers = $this->mapEntryRepository->findAll();
$markerUri = $this->uriBuilder->uriFor('allMapMarkers', null, null, null, 'MapConfig');
$zoom = 1;
break;
case 1:
$travelPost = $this->travelPostRepository->findByUid((int) $this->configurationManager->getContentObject()->data['pid']);
if ($travelPost !== null && $travelPost->getDoktype() === Constants::DOKTYPE_BLOG_POST) {
$markers = $travelPost->getRelMapEntries();
foreach ($markers as $marker) {
$centerLat = $centerLat + $marker->getLatitude();
$centerLon = $centerLon + $marker->getLongitude();
}
$centerLat = $centerLat / count($markers);
$centerLon = $centerLon / count($markers);
$markerUri = $this->uriBuilder->uriFor('postMapMarkers', null, null, null, 'MapConfig');
$zoom = 6;
}
break;
case 2:
$cats = explode(',', $this->settings['showCategories']);
$args = ['tx_a2gtravelblog_mapconfig' => ['categories' => $cats]];
$travelPosts = $this->travelPostRepository->getFromCategoryUids($cats);
$this->uriBuilder->setArguments($args);
$markerUri = $this->uriBuilder->uriFor('mapMarkersFromCategories', null, null, null, 'MapConfig');
$markers = [];
foreach ($travelPosts as $travelPost) {
$markers[$travelPost->getUid()] = $travelPost->getRelMapEntries()->current();
$centerLat = $centerLat + $markers[$travelPost->getUid()]->getLatitude();
$centerLon = $centerLon + $markers[$travelPost->getUid()]->getLongitude();
}
$centerLat = $centerLat / count($markers);
$centerLon = $centerLon / count($markers);
$zoom = 3;
break;
case 3:
$authorUids = explode(',', $this->settings['showAuthors']);
$args = ['tx_a2gtravelblog_mapconfig' => ['authors' => $authorUids]];
$travelPosts = $this->travelPostRepository->getFromAuthorUids($authorUids);
$this->uriBuilder->setArguments($args);
$markerUri = $this->uriBuilder->uriFor('mapMarkersFromAuthor', null, null, null, 'MapConfig');
$markers = [];
foreach ($travelPosts as $travelPost) {
$markers[$travelPost->getUid()] = $travelPost->getRelMapEntries()->current();
$centerLat = $centerLat + $markers[$travelPost->getUid()]->getLatitude();
$centerLon = $centerLon + $markers[$travelPost->getUid()]->getLongitude();
}
$centerLat = $centerLat / count($markers);
$centerLon = $centerLon / count($markers);
$zoom = 2;
break;
default:
$markers = [];
$zoom = 1;
break;
}
if ((int) $this->settings['zoom'] > 0) {
$zoom = (int) $this->settings['zoom'];
}
if (count($markers) > 0) {
$showMarkers = true;
} else {
$showMarkers = false;
}
$this->view->assignMultiple([
'mapId' => 'a2g-map-' . uniqid(),
'countMapLayers' => count($layerIds),
'mapConfig' => \GuzzleHttp\json_encode(MapConfigUtility::getMapConfig(
$layerIds,
null,
$showMarkers === true ? $markerUri : null,
null,
$zoom,
18, $centerLon, $centerLat,
true, true, true))
]);
}
private function getMarkersFromCategories(array $uids) {
}
private function getMarkers(int $uid = 0) {
$travelPost = $this->travelPostRepository->findByUid($uid);
if ($travelPost !== null && $travelPost->getDoktype() === Constants::DOKTYPE_BLOG_POST) {
$markers = [];
foreach ($travelPost->getRelMapEntries() as $post) {
$markers[] = $post;
}
} else {
$markers = [];
$posts = $this->travelPostRepository->findAll();
foreach ($posts as $post) {
if (count($post->getRelMapEntries()) > 0) {
$markers[] = $post->getRelMapEntries()->current();
}
}
}
return $markers;
}
/**
* action mapMarkersFromTag
*
* @return string|object|null|void
*/
public function mapMarkersFromTagAction() {
$travelPosts = $this->travelPostRepository->getFromTagUids([(int)$_GET['tx_a2gtravelblog_mapconfig']['tag']]);
$markers = [];
foreach ($travelPosts as $post) {
if (count($post->getRelMapEntries()) > 0) {
$markers[] = $post->getRelMapEntries()->current();
}
}
$out = [
"type" => "FeatureCollection",
"totalFeatures" => count($markers),
"csr" => MapConfigUtility::getMarkerCsr(),
"features" => []
];
foreach ($markers as $marker) {
if ($marker->getRelMarker() !== null) {
$tmpIcon = "i-" . $marker->getRelMarker()->getUid();
} else if ($marker->getRelTravelPost()->getAuthors()->count() > 0) {
$author = $this->travelAuthorRepository->findByUid($marker->getRelTravelPost()->getAuthors()->current()->getUid());
$tmpIcon = $author === null || $author->getRelMapMarker() === null ? null : "i-" . $author->getRelMapMarker()->getUid();
} else {
$tmpIcon = null;
}
$out["features"][] = MapConfigUtility::getMarker($marker, $tmpIcon, self::generateContent($this->uriBuilder, $this->view, ['settings' => $this->settings, 'mapEntry' => $marker], 'popup')->render());
}
return \GuzzleHttp\json_encode($out);
}
/**
* action mapMarkers
*
* @return string|object|null|void
*/
public function mapMarkersFromAuthorAction() {
$travelPosts = $this->travelPostRepository->getFromAuthorUids($_GET['tx_a2gtravelblog_mapconfig']['authors']);
$markers = [];
foreach ($travelPosts as $post) {
if (count($post->getRelMapEntries()) > 0) {
$markers[] = $post->getRelMapEntries()->current();
}
}
$out = [
"type" => "FeatureCollection",
"totalFeatures" => count($markers),
"csr" => MapConfigUtility::getMarkerCsr(),
"features" => []
];
foreach ($markers as $marker) {
if ($marker->getRelMarker() !== null) {
$tmpIcon = "i-" . $marker->getRelMarker()->getUid();
} else if ($marker->getRelTravelPost()->getAuthors()->count() > 0) {
$author = $this->travelAuthorRepository->findByUid($marker->getRelTravelPost()->getAuthors()->current()->getUid());
$tmpIcon = $author === null || $author->getRelMapMarker() === null ? null : "i-" . $author->getRelMapMarker()->getUid();
} else {
$tmpIcon = null;
}
$out["features"][] = MapConfigUtility::getMarker($marker, $tmpIcon, self::generateContent($this->uriBuilder, $this->view, ['settings' => $this->settings, 'mapEntry' => $marker], 'popup')->render());
}
return \GuzzleHttp\json_encode($out);
}
/**
* action mapMarkers
*
* @return string|object|null|void
*/
public function mapMarkersFromCategoriesAction() {
$travelPosts = $this->travelPostRepository->getFromCategoryUids($_GET['tx_a2gtravelblog_mapconfig']['categories']);
$markers = [];
foreach ($travelPosts as $post) {
if (count($post->getRelMapEntries()) > 0) {
$markers[] = $post->getRelMapEntries()->current();
}
}
$out = [
"type" => "FeatureCollection",
"totalFeatures" => count($markers),
"csr" => MapConfigUtility::getMarkerCsr(),
"features" => []
];
foreach ($markers as $marker) {
if ($marker->getRelMarker() !== null) {
$tmpIcon = "i-" . $marker->getRelMarker()->getUid();
} else if ($marker->getRelTravelPost()->getAuthors()->count() > 0) {
$author = $this->travelAuthorRepository->findByUid($marker->getRelTravelPost()->getAuthors()->current()->getUid());
$tmpIcon = $author === null || $author->getRelMapMarker() === null ? null : "i-" . $author->getRelMapMarker()->getUid();
} else {
$tmpIcon = null;
}
$out["features"][] = MapConfigUtility::getMarker($marker, $tmpIcon, self::generateContent($this->uriBuilder, $this->view, ['settings' => $this->settings, 'mapEntry' => $marker], 'popup')->render());
}
return \GuzzleHttp\json_encode($out);
}
/**
* action mapMarkers
*
* @return string|object|null|void
*/
public function postMapMarkersAction() {
$markers = $this->getMarkers((int) $this->configurationManager->getContentObject()->data['uid']);
$out = [
"type" => "FeatureCollection",
"totalFeatures" => count($markers),
"csr" => MapConfigUtility::getMarkerCsr(),
"features" => []
];
foreach ($markers as $marker) {
if ($marker->getRelMarker() !== null) {
$tmpIcon = "i-" . $marker->getRelMarker()->getUid();
} else if ($marker->getRelTravelPost()->getAuthors()->count() > 0) {
$author = $this->travelAuthorRepository->findByUid($marker->getRelTravelPost()->getAuthors()->current()->getUid());
$tmpIcon = $author === null || $author->getRelMapMarker() === null ? null : "i-" . $author->getRelMapMarker()->getUid();
} else {
$tmpIcon = null;
}
$out["features"][] = MapConfigUtility::getMarker($marker, $tmpIcon, self::generateContent($this->uriBuilder, $this->view, ['mapEntry' => $marker], 'popupPost')->render());
}
return \GuzzleHttp\json_encode($out);
}
/**
* action mapMarkers
*
* @return string|object|null|void
*/
public function allMapMarkersAction() {
$markers = $this->getMarkers();
$out = [
"type" => "FeatureCollection",
"totalFeatures" => count($markers),
"csr" => MapConfigUtility::getMarkerCsr(),
"features" => []
];
foreach ($markers as $marker) {
if ($marker->getRelMarker() !== null) {
$tmpIcon = "i-" . $marker->getRelMarker()->getUid();
} else if ($marker->getRelTravelPost()->getAuthors()->count() > 0) {
$author = $this->travelAuthorRepository->findByUid($marker->getRelTravelPost()->getAuthors()->current()->getUid());
$tmpIcon = $author === null || $author->getRelMapMarker() === null ? null : "i-" . $author->getRelMapMarker()->getUid();
} else {
$tmpIcon = null;
}
$out["features"][] = MapConfigUtility::getMarker($marker, $tmpIcon, self::generateContent($this->uriBuilder, $this->view, ['settings' => $this->settings, 'mapEntry' => $marker])->render());
}
return \GuzzleHttp\json_encode($out);
}
public function activeCountryGeojsonAction(){
$travelCats=[];
foreach($_GET['tx_a2gtravelblog_mapconfig']['categories'] as $uid){
$travelCats[] = $this->travelCategoryRepository->findByUid((int)$uid);
}
$isoCodes = [];
foreach($travelCats as $travelCat){
$isoCodes[$travelCat->getIsoA2CountryCode()] = null;
}
return \GuzzleHttp\json_encode(MapConfigUtility::getCountryFeatureCollectionsFromIsoCodeList($isoCodes));
}
public function activeCountriesGeojsonAction(){
$travelCats = $this->travelCategoryRepository->findAll();
$isoCodes = [];
foreach($travelCats as $travelCat){
$isoCodes[$travelCat->getIsoA2CountryCode()] = self::generateContent($this->uriBuilder, $this->view, ['settings' => $this->settings, 'category' => $travelCat], 'popupCategory')->render();
}
return \GuzzleHttp\json_encode(MapConfigUtility::getCountryFeatureCollectionsFromIsoCodeList($isoCodes));
}
/**
* action mapConfig
*
* @return string|object|null|void
*/
public function mapConfigAction() {
$this->setMapMarkerIcons();
$this->view->assignMultiple([
'mapIcons' => \GuzzleHttp\json_encode(self::$mapIcons)
]);
}
/**
* action ajaxListAction
*
* @return string|object|null|void
*/
public function ajaxListAction() {
}
// /**
// * action show
// *
// * @param Ingredients $ingredient
// * @return string|object|null|void
// */
// public function showAction(Ingredients $ingredient) {
// $titleProvider = GeneralUtility::makeInstance(A2gPageTitleProvider::class);
//
// if($this->settings['pageTitleSuffix'] !== null && $this->settings['pageTitleSuffix'] !== '' && (strlen($ingredient->getTitle()) + strlen($this->settings['pageTitleSuffix'])) < 66 ){
// $pageTitle = $ingredient->getTitle(). ' ' . $this->settings['pageTitleSuffix'];
// } else {
// $pageTitle = $ingredient->getTitle();
// }
// $titleProvider->setTitle($pageTitle);
// $contentObject = GeneralUtility::makeInstance(ContentObjectRenderer::class);
// $description = $contentObject->crop($ingredient->getDescription(), 125 . '|...|' . true);
//
// $uri = $this->uriBuilder->reset()->setCreateAbsoluteUri(true)->setTargetPageUid(
// $GLOBALS['TSFE']->id)->uriFor('show', ['ingredient' => $ingredient], 'Ingredients', 'a2gProducts', 'ingredientsdetail');
//
// $metaTagManager = GeneralUtility::makeInstance(MetaTagManagerRegistry::class);
//
// $metaTagManager->getManagerForProperty('robots')->addProperty('robots', 'index, folow');
// $metaTagManager->getManagerForProperty('og:type')->addProperty('og:type', 'website');
// $metaTagManager->getManagerForProperty('twitter:card')->addProperty('twitter:card', 'summary_large_image');
//
// if ($ingredient->getTitle() !== '') {
// $metaTagManager->getManagerForProperty('title')->addProperty('title', $pageTitle);
// $metaTagManager->getManagerForProperty('og:title')->addProperty('og:title', $pageTitle);
// $metaTagManager->getManagerForProperty('twitter:title')->addProperty('twitter:title', $pageTitle);
// }
// if ($ingredient->getDescription() !== '') {
// $metaTagManager->getManagerForProperty('description')->addProperty('description', $description);
// $metaTagManager->getManagerForProperty('og:description')->addProperty('og:description', $description);
// $metaTagManager->getManagerForProperty('twitter:description')->addProperty('twitter:description', $description);
// }
// $metaTagManager->getManagerForProperty('og:url')->addProperty('og:url', $uri);
// $metaTagManager->getManagerForProperty('twitter:url')->addProperty('twitter:url', $uri);
//
// if ($ingredient->getImage() !== null) {
// $processedImage = $this->imageService->applyProcessingInstructions(
// $this->imageService->getImage('', $ingredient->getImage(), false),
// ['width' => '
// 1200', 'height' => '630c']
// );
// $metaTagManager->getManagerForProperty('twitter:image')->addProperty('twitter:image', $this->imageService->getImageUri($processedImage, true));
// $metaTagManager->getManagerForProperty('og:image')->addProperty('og:image', $this->imageService->getImageUri($processedImage, true));
// }
// $this->view->assign('ingredient', $ingredient);
// }
}