-
-
Notifications
You must be signed in to change notification settings - Fork 158
/
RelatedExtension.php
191 lines (154 loc) · 6.07 KB
/
RelatedExtension.php
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<?php
declare(strict_types=1);
namespace Bolt\Twig;
use Bolt\Configuration\Config;
use Bolt\Entity\Content;
use Bolt\Entity\Relation;
use Bolt\Repository\RelationRepository;
use Bolt\Utils\RelatedOptionsUtility;
use Tightenco\Collect\Support\Collection;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
use Twig\TwigFunction;
class RelatedExtension extends AbstractExtension
{
/** @var RelationRepository */
private $relationRepository;
/** @var Config */
private $config;
/** @var Notifications */
private $notifications;
/** @var RelatedOptionsUtility */
private $optionsUtility;
public function __construct(
RelationRepository $relationRepository,
Config $config,
Notifications $notifications,
RelatedOptionsUtility $optionsUtility)
{
$this->relationRepository = $relationRepository;
$this->config = $config;
$this->notifications = $notifications;
$this->optionsUtility = $optionsUtility;
}
/**
* {@inheritdoc}
*/
public function getFilters(): array
{
return [
new TwigFilter('related', [$this, 'getRelatedContent']),
new TwigFilter('related_by_type', [$this, 'getRelatedContentByType']),
new TwigFilter('related_first', [$this, 'getFirstRelatedContent']),
new TwigFilter('related_options', [$this, 'getRelatedOptions']),
new TwigFilter('related_values', [$this, 'getRelatedValues']),
];
}
/**
* {@inheritdoc}
*/
public function getFunctions(): array
{
return [
new TwigFunction('related_content', [$this, 'getRelatedContent']),
new TwigFunction('related_content_by_type', [$this, 'getRelatedContentByType']),
new TwigFunction('first_related_content', [$this, 'getFirstRelatedContent']),
new TwigFunction('related_options', [$this, 'getRelatedOptions']),
new TwigFunction('related_values', [$this, 'getRelatedValues']),
];
}
/**
* @return array name => Content[]
*/
public function getRelatedContentByType(Content $content, bool $bidirectional = true, ?int $limit = null, bool $publishedOnly = true): array
{
if (! $this->checkforContent($content, 'related_by_type')) {
return [];
}
$relations = $this->relationRepository->findRelations($content, null, $limit, $publishedOnly);
return (new Collection($relations))
->reduce(function (array $result, Relation $relation) use ($content): array {
$relatedContent = $this->extractContentFromRelation($relation, $content);
if ($relatedContent !== null) {
if (isset($result[$relatedContent->getContentTypeSlug()]) === false) {
$result[$relatedContent->getContentTypeSlug()] = [];
}
$result[$relatedContent->getContentTypeSlug()][] = $relatedContent;
}
return $result;
}, []);
}
/**
* @return Content[]
*/
public function getRelatedContent($content, ?string $name = null, bool $bidirectional = true, ?int $limit = null, bool $publishedOnly = true): array
{
if (! $this->checkforContent($content, 'related')) {
return [];
}
$relations = $this->relationRepository->findRelations($content, $name, $limit, $publishedOnly);
return (new Collection($relations))
->map(function (Relation $relation) use ($content) {
return $this->extractContentFromRelation($relation, $content);
})
->filter()
->toArray();
}
public function getFirstRelatedContent($content, ?string $name = null, bool $publishedOnly = true): ?Content
{
if (! $this->checkforContent($content, 'related_first')) {
return null;
}
$relation = $this->relationRepository->findFirstRelation($content, $name, $publishedOnly);
if ($relation === null) {
return null;
}
return $this->extractContentFromRelation($relation, $content);
}
private function extractContentFromRelation(Relation $relation, Content $source): ?Content
{
if ($relation->getFromContent()->getId() === $source->getId()) {
return $relation->getToContent();
} elseif ($relation->getToContent()->getId() === $source->getId()) {
return $relation->getFromContent();
}
return null;
}
public function getRelatedOptions(string $contentTypeSlug, ?string $order = null, string $format = '', ?bool $required = false, ?bool $allowEmpty = false): Collection
{
$maxAmount = $this->config->get('maximum_listing_select', 1000);
$contentType = $this->config->getContentType($contentTypeSlug);
if (! $order) {
$order = $contentType->get('order');
}
$options = $this->optionsUtility->fetchRelatedOptions($contentTypeSlug, $order, $format, $required, $allowEmpty, $maxAmount);
return new Collection($options);
}
public function getRelatedValues(Content $source, string $contentType): Collection
{
if (! $this->checkforContent($source, 'related_values')) {
return new Collection([]);
}
if ($source->getId() === null) {
return new Collection([]);
}
$content = $this->getRelatedContent($source, $contentType, true, null, false);
$values = [];
/** @var Content $record */
foreach ($content as $record) {
$values[] = $record->getId();
}
return new Collection($values);
}
private function checkforContent($content, string $keyword): bool
{
if (! $content instanceof Content) {
$this->notifications->warning(
'Incorrect usage of `' . $keyword . '`-filter or function',
'The `' . $keyword . '`-filter or function can only be applied to a single Record.'
);
return false;
}
return true;
}
}