-
-
Notifications
You must be signed in to change notification settings - Fork 184
Expand file tree
/
Copy pathListingController.php
More file actions
153 lines (123 loc) · 5.7 KB
/
Copy pathListingController.php
File metadata and controls
153 lines (123 loc) · 5.7 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php
declare(strict_types=1);
namespace Bolt\Controller\Frontend;
use Bolt\Common\Str;
use Bolt\Configuration\Content\ContentType;
use Bolt\Controller\TwigAwareController;
use Bolt\Entity\Content;
use Bolt\Repository\ContentRepository;
use Bolt\Storage\Query;
use Pagerfanta\Adapter\ArrayAdapter;
use Pagerfanta\Pagerfanta;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\Attribute\Route;
class ListingController extends TwigAwareController implements FrontendZoneInterface
{
public function __construct(
private readonly Query $query
) {
}
#[Route(path: '/{contentTypeSlug}', name: 'listing', requirements: [
'contentTypeSlug' => '%bolt.requirement.contenttypes%',
], methods: [Request::METHOD_GET, Request::METHOD_POST])]
#[Route(path: '/{_locale}/{contentTypeSlug}', name: 'listing_locale', requirements: [
'contentTypeSlug' => '%bolt.requirement.contenttypes%',
'_locale' => '%app_locales%',
], methods: [Request::METHOD_GET, Request::METHOD_POST])]
public function listing(Request $request, ContentRepository $contentRepository, string $contentTypeSlug, ?string $_locale = null): Response
{
if ($_locale === null && ! $this->getFromRequest($request, '_locale')) {
$request->setLocale($this->defaultLocale);
}
$contentType = ContentType::factory($contentTypeSlug, $this->config->get('contenttypes'));
// If the ContentType has 'viewless_listing' set to `true`, we throw a 404.
if ($contentType->get('viewless_listing') === true) {
throw new NotFoundHttpException('Content is not viewable');
}
// If the locale is the wrong locale
if (! $this->validLocaleForContentType($request, $contentType)) {
return $this->redirectToDefaultLocale($request);
}
$page = (int) $this->getFromRequest($request, 'page', '1');
$amountPerPage = $contentType->get('listing_records');
$params = $this->parseQueryParams($request, $contentType);
/** @var Content|Pagerfanta $content */
$content = $this->query->getContent($contentTypeSlug, $params);
// If we're foolishly trying to "list" a singleton, we're getting a single Content here
if ($content instanceof Content) {
$route = $content->getDefinition()->get('record_route');
$controller = $this->container->get('router')->getRouteCollection()->get($route)->getDefault('_controller');
$parameters = $request->attributes->all();
$parameters['slugOrId'] = $content->getId();
return $this->forward($controller, $parameters);
}
$records = $this->setRecords($content, $amountPerPage, $page);
// Set canonical URL. Note: query params (order/status/filters from
// parseQueryParams) are intentionally NOT merged in — they are volatile and
// would pollute the canonical (e.g. ?order=-createdAt&status=published).
$this->canonical->setPath(
'listing_locale',
[
'contentTypeSlug' => $contentType->get('slug'),
'_locale' => $request->getLocale(),
]
);
// Render
$templates = $this->templateChooser->forListing($contentType);
$this->twig->addGlobal('records', $records);
$twigVars = [
'records' => $records,
$contentType->getSlug() => $records,
'contenttype' => $contentType,
];
return $this->render($templates, $twigVars);
}
private function parseQueryParams(Request $request, ContentType $contentType): array
{
if ($this->config->get('general/query_search')->get('enable', true) === false) {
return [
'order' => $contentType->get('order'),
'status' => 'published',
];
}
$queryParams = collect($request->query->all());
// Note, we're not including 'limit', 'printquery', 'returnsingle' or 'returnmultiple' on purpose
$allowedParams = array_merge(
$contentType['fields']->keys()->all(),
$contentType['taxonomy']->all(),
['order', 'earliest', 'latest', 'offset', 'page', 'random', 'author', 'anyField', 'anything']
);
$params = $queryParams->mapWithKeys(function ($value, $key) use ($allowedParams): array {
// Ensure we don't have arrays, if we get something like `title[]=…` passed in.
if (is_array($value)) {
$value = current($value);
}
if (Str::endsWith($key, '--like')) {
$key = Str::removeLast($key, '--like');
$value = '%' . $value . '%';
}
return in_array($key, $allowedParams, true) ? [$key => $value] : [];
})->toArray();
if (! array_key_exists('order', $params)) {
$params['order'] = $contentType->get('order');
}
// Ensure we only list things that are 'published'
$params['status'] = 'published';
if ($this->config->get('general/query_search')->get('ignore_empty', false) === true) {
$params = array_filter($params, fn ($param): bool => ! ($param === '' | $param === '%%'));
}
return $params;
}
private function setRecords($content, int $amountPerPage, int $page): Pagerfanta
{
if ($content instanceof Pagerfanta) {
$records = $content->setMaxPerPage($amountPerPage)
->setCurrentPage($page);
} else {
$records = new Pagerfanta(new ArrayAdapter([]));
}
return $records;
}
}