-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathGalleryCreatorNewsController.php
More file actions
117 lines (94 loc) · 4.12 KB
/
Copy pathGalleryCreatorNewsController.php
File metadata and controls
117 lines (94 loc) · 4.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php
declare(strict_types=1);
/*
* This file is part of Gallery Creator Bundle.
*
* (c) Marko Cupic 2022 <m.cupic@gmx.ch>
* @license GPL-3.0-or-later
* For the full copyright and license information,
* please view the LICENSE file that was distributed with this source code.
* @link https://github.com/markocupic/gallery-creator-bundle
*/
namespace Markocupic\GalleryCreatorBundle\Controller\ContentElement;
use Contao\ContentModel;
use Contao\CoreBundle\ServiceAnnotation\ContentElement;
use Contao\PageModel;
use Contao\Template;
use Doctrine\DBAL\Driver\Exception as DoctrineDBALDriverException;
use Doctrine\DBAL\Exception as DoctrineDBALException;
use Markocupic\GalleryCreatorBundle\Model\GalleryCreatorAlbumsModel;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Twig\Environment as TwigEnvironment;
/**
* @ContentElement(GalleryCreatorNewsController::TYPE, category="gallery_creator_elements", template="ce_gallery_creator_news")
*/
class GalleryCreatorNewsController extends AbstractGalleryCreatorController
{
public const TYPE = 'gallery_creator_news';
protected TwigEnvironment $twig;
protected ?GalleryCreatorAlbumsModel $activeAlbum = null;
protected ?ContentModel $model = null;
protected ?PageModel $pageModel = null;
public function __construct(DependencyAggregate $dependencyAggregate, TwigEnvironment $twig)
{
$this->twig = $twig;
parent::__construct($dependencyAggregate);
}
public function __invoke(Request $request, ContentModel $model, string $section, array $classes = null, PageModel $pageModel = null): Response
{
// Do not parse the content element in the backend
if ($this->scopeMatcher->isBackendRequest($request)) {
return new Response(
$this->twig->render('@MarkocupicGalleryCreator/Backend/backend_element_view.html.twig', [])
);
}
$this->model = $model;
$this->pageModel = $pageModel;
if (!$this->model->gcPublishSingleAlbum) {
return new Response('', Response::HTTP_NO_CONTENT);
}
$this->activeAlbum = GalleryCreatorAlbumsModel::findOneBy(
['tl_gallery_creator_albums.id = ? AND tl_gallery_creator_albums.published = ?'],
[$this->model->gcPublishSingleAlbum, '1']
);
// Return empty response if the album doesn't exist
if (null === $this->activeAlbum) {
return new Response('', Response::HTTP_NO_CONTENT);
}
// Check permission for protected albums
if (!$this->securityUtil->isAuthorized($this->activeAlbum)) {
return new Response('', Response::HTTP_NO_CONTENT);
}
return parent::__invoke($request, $this->model, $section, $classes);
}
/**
* @throws DoctrineDBALDriverException
* @throws DoctrineDBALException
*
* @return Response|null
*/
protected function getResponse(Template $template, ContentModel $model, Request $request): Response
{
// Add the picture collection and the pagination to the template.
$this->addAlbumPicturesToTemplate($this->activeAlbum, $this->model, $template, $this->pageModel);
// Augment template with more properties.
$this->addAlbumToTemplate($this->activeAlbum, $model, $template, $this->pageModel);
// Count views
$this->albumUtil->countAlbumViews($this->activeAlbum);
// Add content model to template.
$template->content = $model->row();
// Add meta tags to the page header.
$this->addMetaTagsToPage($this->pageModel, $this->activeAlbum);
// Trigger gcGenerateFrontendTemplateHook
$this->triggerGenerateFrontendTemplateHook($template, $this->activeAlbum);
return $template->getResponse();
}
/**
* Augment template with some more properties of the active album.
*/
protected function addAlbumToTemplate(GalleryCreatorAlbumsModel $albumModel, ContentModel $contentModel, Template $template, PageModel $pageModel): void
{
parent::addAlbumToTemplate($albumModel, $contentModel, $template, $pageModel);
}
}