/
AbstractContentType.php
359 lines (300 loc) · 8.44 KB
/
AbstractContentType.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
<?php
declare(strict_types=1);
/*
* This file is part of the Zikula package.
*
* Copyright Zikula Foundation - https://ziku.la/
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Zikula\Common\Content;
use Exception;
use Twig\Environment;
use Twig\Loader\FilesystemLoader;
use Zikula\Common\Translator\TranslatorInterface;
use Zikula\ThemeModule\Engine\Asset;
/**
* Content type base class for convenient implementation.
*/
abstract class AbstractContentType implements ContentTypeInterface
{
/**
* Translator instance
*
* @var TranslatorInterface
*/
protected $translator;
/**
* Bundle name
*
* @var string
*/
protected $bundleName;
/**
* Translation domain
*
* @var string
*/
protected $domain;
/**
* The name of this content type
*
* @var string
*/
protected $name;
/**
* Twig template engine
*
* @var Environment
*/
protected $twig;
/**
* Twig template loader
*
* @var FilesystemLoader
*/
protected $twigLoader;
/**
* @var \Zikula\ContentModule\Helper\PermissionHelper
*/
protected $permissionHelper;
/**
* @var Asset
*/
protected $assetHelper;
/**
* Reference to content item instance which allows to access page data,
* layout area and styling data and other information.
*
* @var \Zikula\ContentModule\Entity\ContentItemEntity
*/
protected $entity;
/**
* Reference to the data fields loaded from either default values or the entity.
*
* @var array
*/
protected $data;
public function __construct(
TranslatorInterface $translator,
Environment $twig,
FilesystemLoader $twigLoader,
/*PermissionHelper */$permissionHelper,
Asset $assetHelper
) {
$this->translator = $translator;
$nsParts = explode('\\', get_class($this));
list($vendor, $nameAndType) = $nsParts;
$this->bundleName = $vendor . $nameAndType;
$this->domain = mb_strtolower($this->bundleName);
$this->name = str_replace('Type', '', array_pop($nsParts));
$this->twig = $twig;
$this->twigLoader = $twigLoader;
$this->permissionHelper = $permissionHelper;
$this->assetHelper = $assetHelper;
$this->data = $this->getDefaultData();
}
/**
* Performs a singular translation.
*/
public function __(string $msg, string $locale = null): string
{
/** @Ignore */
return $this->translator->__($msg, $this->domain, $locale);
}
/**
* Performs a plural translation.
*/
public function _n(string $m1, string $m2, int $number, string $locale = null): string
{
/** @Ignore */
return $this->translator->_n($m1, $m2, $number, $this->domain, $locale);
}
/**
* Performs a format singular translation.
*/
public function __f(string $msg, array $parameters = [], string $locale = null): string
{
/** @Ignore */
return $this->translator->__f($msg, $parameters, $this->domain, $locale);
}
/**
* Performs a format plural translation.
*/
public function _fn(string $m1, string $m2, int $number, array $parameters = [], string $locale = null): string
{
/** @Ignore */
return $this->translator->_fn($m1, $m2, $number, $parameters, $this->domain, $locale);
}
/**
* Returns content item instance.
*
* @return \Zikula\ContentModule\Entity\ContentItemEntity
*/
public function getEntity()/*: ContentItemEntity*/
{
return $this->entity;
}
/**
* Sets content item instance.
*
* @param \Zikula\ContentModule\Entity\ContentItemEntity $entity
*/
public function setEntity(/*ContentItemEntity */$entity): void
{
$this->entity = $entity;
$this->data = $entity->getContentData();
}
public function getBundleName(): string
{
return $this->bundleName;
}
public function getName(): string
{
return $this->name;
}
public function getCategory(): string
{
return ContentTypeInterface::CATEGORY_INTEGRATION;
}
public function getIcon(): string
{
return 'cube';
}
public function getTitle(): string
{
return '- ' . $this->__('no title defined') . ' -';
}
public function getDescription(): string
{
return '';
}
public function getAdminInfo(): string
{
return '';
}
public function isActive(): bool
{
return $this->permissionHelper->mayReadContentType($this->getName());
}
public function getMinimumAmountOfGridColumns(): int
{
return 2;
}
public function getDefaultData(): array
{
return [];
}
public function getTranslatableDataFields(): array
{
return [];
}
public function getData(): array
{
return array_merge($this->getDefaultData(), $this->data);
}
public function getSearchableText(): string
{
return '';
}
/**
* Returns output for normal or editing display.
*/
public function display(bool $editMode = false): string
{
$output = '';
if (!$this->isActive()) {
return $output;
}
$output .= $this->displayStart();
if (true === $editMode) {
$output .= $this->displayEditing();
} else {
$output .= $this->displayView();
}
$output .= $this->displayEnd();
return $output;
}
/**
* Returns any text displayed before the actual content.
*
* Use this method to display styling like float and width for the content.
* The default implementation adds a generic <div> around the content, but
* you can choose to override this method in inherited plugins in order to
* generate more compact HTML where the styling is included in the actual
* content.
*/
protected function displayStart(): string
{
$classHtml = '';
$stylingClasses = $this->getEntity()->getStylingClasses();
if (null !== $stylingClasses && is_array($stylingClasses) && count($stylingClasses) > 0) {
$classHtml = ' class="' . implode(' ', $stylingClasses) . '"';
}
return '<div' . $classHtml . '>' . "\n";
}
/**
* Returns any text displayed after the actual content.
*/
protected function displayEnd(): string
{
return '</div>';
}
public function displayView(): string
{
$templateParameters = $this->getData();
$templateParameters['contentId'] = null !== $this->getEntity() ? $this->getEntity()->getId() : 0;
$contentTypeOutput = $this->twig->render($this->getViewTemplatePath(), $templateParameters);
$outerTemplate = '@ZikulaContentModule/ContentItem/display.html.twig';
return $this->twig->render($outerTemplate, [
'contentTypeOutput' => $contentTypeOutput,
'contentItem' => $this->getEntity()
]);
}
public function displayEditing(): string
{
return $this->displayView();
}
public function getViewTemplatePath(): string
{
return $this->getTemplatePath('View');
}
public function getEditTemplatePath(): string
{
return $this->getTemplatePath('Edit');
}
/**
* Tries to resolve a certain template using a given suffix in 'namespaced' name-style
* and returns the resulting path.
*/
protected function getTemplatePath(string $suffix = ''): string
{
$template = '@' . $this->getBundleName() . '/ContentType/' . lcfirst($this->getName()) . $suffix . '.html.twig';
if (!$this->twigLoader->exists($template)) {
throw new Exception($this->__f('Error! Could not resolve %template% template.', ['%template%' => $template]));
}
return $template;
}
public function getEditFormClass(): string
{
return '';
}
public function getEditFormOptions(string $context): array
{
return [
'context' => $context
];
}
public function getAssets(string $context): array
{
return [
'css' => [],
'js' => []
];
}
public function getJsEntrypoint(string $context): ?string
{
return null;
}
}