/
controller.php
342 lines (311 loc) · 11.1 KB
/
controller.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
<?php
namespace Concrete\Block\PageAttributeDisplay;
use Concrete\Core\Block\BlockController;
use Concrete\Core\Attribute\Key\CollectionKey as CollectionAttributeKey;
use Concrete\Core\Block\View\BlockViewTemplate;
use Concrete\Core\Entity\Attribute\Value\Value\SelectValue;
use Concrete\Core\Feature\Features;
use Concrete\Core\Feature\UsesFeatureInterface;
use Concrete\Core\Page\Page;
use Concrete\Core\Localization\Service\Date;
defined('C5_EXECUTE') or die('Access Denied.');
/**
* @author Ryan Tyler
*/
class Controller extends BlockController implements UsesFeatureInterface
{
protected $btTable = 'btPageAttributeDisplay';
protected $btInterfaceWidth = "500";
protected $btInterfaceHeight = "365";
/** @var string|null */
public $dateFormat;
/** @var bool */
protected $btCacheBlockOutput = true;
/** @var bool */
protected $btCacheBlockOutputOnPost = true;
/** @var bool */
protected $btCacheBlockOutputForRegisteredUsers = false;
/**
* @var int thumbnail height
*/
public $thumbnailHeight = 250;
/**
* @var int thumbnail width
*/
public $thumbnailWidth = 250;
/** @var string|null */
public $attributeHandle;
/** @var string|null */
public $attributeTitleText;
/** @var string|null */
public $displayTag;
public function getBlockTypeDescription()
{
return t("Displays the value of a page attribute for the current page.");
}
public function getBlockTypeName()
{
return t("Page Attribute Display");
}
/**
* @return void
* @throws \Illuminate\Contracts\Container\BindingResolutionException
*/
public function add()
{
$this->dateFormat = $this->app->make('date')->getPHPDateTimePattern();
$this->set('dateFormat', $this->dateFormat);
$this->set('thumbnailWidth', $this->thumbnailWidth);
$this->set('thumbnailHeight', $this->thumbnailHeight);
}
/**
* Used to validate a blocks data before saving to the database
* Generally should return an empty ErrorList if valid
* Custom Packages may return a boolean value
*
* @param array<string,mixed>|string|null $args
* @return \Concrete\Core\Error\ErrorList\ErrorList|boolean
* @throws \Illuminate\Contracts\Container\BindingResolutionException
* @version <= 8.4.3 Method returns ErrorList|boolean
* @version 8.5.0a3 Method returns ErrorList
*/
public function validate($args)
{
$error = $this->app->make('helper/validation/error');
if (!is_numeric($args['thumbnailHeight'])) {
$error->add(t('Thumbnail Height must be a number.'));
}
if (!is_numeric($args['thumbnailWidth'])) {
$error->add(t('Thumbnail Width must be a number.'));
}
return $error;
}
/**
* {@inheritDoc}
* @return string[]
*/
public function getRequiredFeatures(): array
{
return [
Features::BASICS
];
}
/**
* @return mixed AttributeValue
*/
public function getContent()
{
$c = Page::getCurrentPage();
$content = "";
switch ($this->attributeHandle) {
case "rpv_pageName":
$content = h($c->getCollectionName());
break;
case "rpv_pageDescription":
$content = nl2br(h($c->getCollectionDescription()));
break;
case "rpv_pageDateCreated":
$content = $c->getCollectionDateAdded();
break;
case "rpv_pageDateLastModified":
$content = $c->getCollectionDateLastModified();
break;
case "rpv_pageDatePublic":
$content = $c->getCollectionDatePublic();
break;
default:
$content = $c->getAttribute($this->attributeHandle);
if ($content instanceof \DateTime) {
$content = $content->format(Date::DB_FORMAT);
} else {
$content_alt = $c->getAttributeValue($this->attributeHandle);
if (is_object($content) && $content instanceof \Concrete\Core\Entity\File\File) {
if ($this->thumbnailWidth > 0 || $this->thumbnailHeight > 0) {
$im = $this->app->make('helper/image');
$thumb = $im->getThumbnail(
$content,
$this->thumbnailWidth,
$this->thumbnailHeight
); //<-- set these 2 numbers to max width and height of thumbnails
$content = "<img class=\"img-fluid\" src=\"{$thumb->src}\" width=\"{$thumb->width}\" height=\"{$thumb->height}\" alt=\"\" />";
} else {
$image = $this->app->make('html/image', ['f' => $content]);
$content = (string) $image->getTag();
}
} elseif (is_object($content_alt)) {
if (is_array($content) && $content[0] instanceof \Concrete\Core\Tree\Node\Type\Topic) {
$content = str_replace(', ', "\n", $content_alt->getDisplayValue());
} elseif ($content instanceof SelectValue) {
$content = (string) $content;
} else {
$content = $content_alt->getDisplayValue();
}
}
}
break;
}
$is_stack = $c->getController() instanceof \Concrete\Controller\SinglePage\Dashboard\Blocks\Stacks;
if (!strlen(trim(strip_tags($content))) && ($c->isMasterCollection() || $is_stack)) {
$content = $this->getPlaceHolderText($this->attributeHandle);
}
if (!empty($this->delimiter)) {
$parts = explode("\n", $content);
if (count($parts) > 1) {
switch ($this->delimiter) {
case 'comma':
$delimiter = ',';
break;
case 'commaSpace':
$delimiter = ', ';
break;
case 'pipe':
$delimiter = '|';
break;
case 'dash':
$delimiter = '-';
break;
case 'semicolon':
$delimiter = ';';
break;
case 'semicolonSpace':
$delimiter = '; ';
break;
case 'break':
$delimiter = '<br />';
break;
default:
$delimiter = ' ';
break;
}
$content = implode($delimiter, $parts);
}
}
return $content;
}
/**
* Returns a place holder for pages that are new or when editing default page types.
*
* @param string $handle
*
* @return string
*/
public function getPlaceHolderText($handle)
{
$placeHolder = 'Unknown Attribute'; // incase our attribute doesnt exist
$pageValues = $this->getAvailablePageValues();
if (in_array($handle, array_keys($pageValues))) {
$placeHolder = $pageValues[$handle];
} else {
$attributeKey = CollectionAttributeKey::getByHandle($handle);
if (is_object($attributeKey)) {
$placeHolder = $attributeKey->getAttributeKeyName();
}
}
return "[" . $placeHolder . "]";
}
/**
* Returns the title text to display in front of the value.
*
* @return string
*/
public function getTitle()
{
return strlen($this->attributeTitleText) ? $this->attributeTitleText . " " : "";
}
/**
* @return array<string,string>
*/
public function getAvailablePageValues()
{
return [
'rpv_pageName' => t('Page Name'),
'rpv_pageDescription' => t('Page Description'),
'rpv_pageDateCreated' => t('Page Date Created'),
'rpv_pageDatePublic' => t('Page Date Published'),
'rpv_pageDateLastModified' => t('Page Date Modified'),
];
}
/**
* @return \Concrete\Core\Entity\Attribute\Key\PageKey[]
*/
public function getAvailableAttributes()
{
$categoryService = $this->app->make('\Concrete\Core\Attribute\Category\PageCategory');
return $categoryService->getList();
}
/**
* @return string|null
*/
protected function getTemplateHandle()
{
$templateHandle = null;
if (in_array($this->attributeHandle, array_keys($this->getAvailablePageValues()))) {
switch ($this->attributeHandle) {
case "rpv_pageDateCreated":
case 'rpv_pageDateLastModified':
case "rpv_pageDatePublic":
$templateHandle = 'date_time';
break;
}
} else {
$attributeKey = CollectionAttributeKey::getByHandle($this->attributeHandle);
if (is_object($attributeKey)) {
$attributeType = $attributeKey->getAttributeType();
$templateHandle = $attributeType->getAttributeTypeHandle();
}
}
return $templateHandle;
}
/**
* Returns opening html tag.
*
* @return string
*/
public function getOpenTag()
{
$tag = "";
if (strlen($this->displayTag)) {
$tag = "<" . $this->displayTag . " class=\"ccm-block-page-attribute-display-wrapper\">";
}
return $tag;
}
/**
* Returns closing html tag.
*
* @return string
*/
public function getCloseTag()
{
$tag = "";
if (strlen($this->displayTag)) {
$tag = "</" . $this->displayTag . ">";
}
return $tag;
}
/**
* @return void
* @throws \Illuminate\Contracts\Container\BindingResolutionException
*/
public function view()
{
// only use the type specific template if there is NOT a custom template defined
$b = $this->getBlockObject();
if ($b->getBlockFilename()) {
// custom template
} else {
$templateHandle = $this->getTemplateHandle();
if (in_array($templateHandle, ['date_time', 'boolean'])) {
$this->render('templates/' . $templateHandle);
} else {
// check if there is a template that matches the selected attribute
/** @var BlockViewTemplate $template */
$template = $this->app->make(BlockViewTemplate::class, ['obj' => $this->getBlockObject()]);
$template->setBlockCustomTemplate("templates/" . $this->attributeHandle . '.php');
$info = pathinfo($template->getTemplate());
if ($info['basename'] != 'view.php') {
$this->render('templates/' . $info['filename'] );
}
}
}
}
}