/
BlockView.php
503 lines (446 loc) · 16.8 KB
/
BlockView.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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
<?php
namespace Concrete\Core\Block\View;
use Concrete\Core\Block\Events\BlockBeforeRender;
use Concrete\Core\Block\Events\BlockOutput;
use Concrete\Core\Localization\Localization;
use Concrete\Core\Support\Facade\Application;
use Concrete\Core\View\AbstractView;
use Config;
use Area;
use Environment;
use User;
use Page;
use Concrete\Core\Block\Block;
use BlockType;
use URL;
use View;
class BlockView extends AbstractView
{
protected $block;
protected $area;
protected $blockType;
protected $blockTypePkgHandle;
protected $blockViewHeaderFile;
protected $blockViewFooterFile;
protected $outputContent = false;
protected $viewToRender = false;
protected $viewPerformed = false;
protected $showControls = true;
protected $didPullFromOutputCache = false;
protected function constructView($mixed)
{
if ($mixed instanceof Block) {
$this->blockType = $mixed->getBlockTypeObject();
$this->block = $mixed;
$this->area = $mixed->getBlockAreaObject();
} else {
$this->blockType = $mixed;
if ($this->blockType->controller) {
$this->controller = $this->blockType->controller;
}
}
$this->blockTypePkgHandle = $this->blockType->getPackageHandle();
if (!isset($this->controller)) {
if (isset($this->block)) {
$this->controller = $this->block->getInstance();
$this->controller->setBlockObject($this->block);
} else {
$this->controller = $this->blockType->getController();
}
}
}
public function showControls()
{
return $this->showControls;
}
public function disableControls()
{
$this->showControls = false;
}
public function setAreaObject(Area $area)
{
$this->area = $area;
}
public function getAreaObject()
{
return $this->area;
}
public function start($state)
{
if (is_object($this->area)) {
$this->controller->setAreaObject($this->area);
}
/*
* Legacy shit
*/
if ($state instanceof Block) {
$this->block = $state;
$this->viewToRender = 'view';
} else {
$this->viewToRender = $state;
}
}
/**
* Creates a URL that can be posted or navigated to that, when done so, will automatically run the corresponding method inside the block's controller.
* <code>
* <a href="<?=$this->action('get_results')?>">Get the results</a>
* </code>.
*
* @param string $task
*
* @return string $url
*/
public function action($task)
{
try {
if ($this->viewToRender == 'add') {
$c = $this->area->getAreaCollectionObject();
$arguments = array('/ccm/system/block/action/add',
$c->getCollectionID(),
urlencode($this->area->getAreaHandle()),
$this->blockType->getBlockTypeID(),
$task,
);
return call_user_func_array(array('\URL', 'to'), $arguments);
} elseif (is_object($this->block)) {
if (is_object($this->block->getProxyBlock())) {
$b = $this->block->getProxyBlock();
} else {
$b = $this->block;
}
if ($this->viewToRender == 'edit') {
$c = $this->area->getAreaCollectionObject();
$arguments = array('/ccm/system/block/action/edit',
$c->getCollectionID(),
urlencode($this->area->getAreaHandle()),
$b->getBlockID(),
$task,
);
return call_user_func_array(array('\URL', 'to'), $arguments);
} else {
$c = Page::getCurrentPage();
if (is_object($b) && is_object($c)) {
$arguments = func_get_args();
$arguments[] = $b->getBlockID();
array_unshift($arguments, $c);
return call_user_func_array(array('\URL', 'page'), $arguments);
}
}
}
} catch (Exception $e) {
}
}
public function startRender()
{
}
public function setupRender()
{
$this->runControllerTask();
$view = $this->viewToRender;
$env = Environment::get();
if ($this->viewToRender == 'scrapbook') {
$scrapbookTemplate = $this->getBlockPath(
FILENAME_BLOCK_VIEW_SCRAPBOOK
) . '/' . FILENAME_BLOCK_VIEW_SCRAPBOOK;
if (file_exists($scrapbookTemplate)) {
$view = 'scrapbook';
} else {
$view = 'view';
}
}
$customFilenameToRender = null;
if (!in_array($this->viewToRender, array('view', 'add', 'edit', 'scrapbook'))) {
// then we're trying to render a custom view file, which we'll pass to the bottom functions as $_filename
$customFilenameToRender = $view . '.php';
$view = 'view';
}
switch ($view) {
case 'view':
if (is_object($this->block) && is_object($this->area)) {
$this->setBlockViewHeaderFile(DIR_FILES_ELEMENTS_CORE . '/block_header_view.php');
$this->setBlockViewFooterFile(DIR_FILES_ELEMENTS_CORE . '/block_footer_view.php');
}
if ($this->controller->blockViewRenderOverride) {
$template = DIRNAME_BLOCKS . '/' . $this->blockType->getBlockTypeHandle(
) . '/' . $this->controller->blockViewRenderOverride . '.php';
$this->setViewTemplate($env->getPath($template, $this->blockTypePkgHandle));
} else {
$bFilename = false;
if ($this->block) {
$bFilename = $this->block->getBlockFilename();
$bvt = new BlockViewTemplate($this->block);
if (!$bFilename && is_object($this->area)) {
$templates = $this->area->getAreaCustomTemplates();
if (isset($templates[$this->block->getBlockTypeHandle()])) {
$bFilename = $templates[$this->block->getBlockTypeHandle()];
}
}
} else {
$bvt = new BlockViewTemplate($this->blockType);
}
if ($bFilename) {
$bvt->setBlockCustomTemplate(
$bFilename
); // this is PROBABLY already set by the method above, but in the case that it's passed by area we have to set it here
} else {
if ($customFilenameToRender) {
$bvt->setBlockCustomRender($customFilenameToRender);
}
}
$this->setViewTemplate($bvt->getTemplate());
}
break;
case 'add':
if ($this->controller->blockViewRenderOverride) {
$template = DIRNAME_BLOCKS . '/' . $this->blockType->getBlockTypeHandle(
) . '/' . $this->controller->blockViewRenderOverride . '.php';
} else {
$template = DIRNAME_BLOCKS . '/' . $this->blockType->getBlockTypeHandle(
) . '/' . FILENAME_BLOCK_ADD;
}
$this->setViewTemplate($env->getPath($template, $this->blockTypePkgHandle));
break;
case 'scrapbook':
$this->setViewTemplate(
$env->getPath(
DIRNAME_BLOCKS . '/' . $this->blockType->getBlockTypeHandle(
) . '/' . FILENAME_BLOCK_VIEW_SCRAPBOOK,
$this->blockTypePkgHandle
)
);
break;
case 'edit':
if ($this->controller->blockViewRenderOverride) {
$template = DIRNAME_BLOCKS . '/' . $this->blockType->getBlockTypeHandle(
) . '/' . $this->controller->blockViewRenderOverride . '.php';
} else {
$template = DIRNAME_BLOCKS . '/' . $this->blockType->getBlockTypeHandle(
) . '/' . FILENAME_BLOCK_EDIT;
}
$this->setBlockViewHeaderFile(DIR_FILES_ELEMENTS_CORE . '/block_header_edit.php');
$this->setBlockViewFooterFile(DIR_FILES_ELEMENTS_CORE . '/block_footer_edit.php');
$this->setViewTemplate($env->getPath($template, $this->blockTypePkgHandle));
break;
}
$this->viewPerformed = $view;
}
protected function onBeforeGetContents()
{
if (in_array($this->viewPerformed, array('scrapbook', 'view'))) {
$this->controller->runAction('on_page_view', array($this));
$this->controller->outputAutoHeaderItems();
}
}
public function renderViewContents($scopeItems)
{
$shouldRender = function() {
$app = Application::getFacadeApplication();
// If you hook into this event and use `preventRendering()`
// you can prevent the block from being displayed.
$event = new BlockBeforeRender($this->block);
$app->make('director')->dispatch('on_block_before_render', $event);
return $event->proceed();
};
if (!$shouldRender()) {
return;
}
unset($shouldRender);
extract($scopeItems);
if (!$this->outputContent) {
ob_start();
include $this->template;
$this->outputContent = ob_get_contents();
ob_end_clean();
}
// The translatable texts in the block header/footer need to be printed
// out in the system language.
$loc = Localization::getInstance();
$loc->pushActiveContext(Localization::CONTEXT_UI);
if ($this->blockViewHeaderFile) {
include $this->blockViewHeaderFile;
}
$this->controller->registerViewAssets($this->outputContent);
$this->onBeforeGetContents();
$this->fireOnBlockOutputEvent();
echo $this->outputContent;
$this->onAfterGetContents();
if ($this->blockViewFooterFile) {
include $this->blockViewFooterFile;
}
$loc->popActiveContext();
}
protected function setBlockViewHeaderFile($file)
{
$this->blockViewHeaderFile = $file;
}
protected function setBlockViewFooterFile($file)
{
$this->blockViewFooterFile = $file;
}
public function postProcessViewContents($contents)
{
return $contents;
}
/**
* Returns the path to the current block's directory.
*
*
* @deprecated
*
* @return string
*/
public function getBlockPath($filename = null)
{
$obj = $this->blockType;
if (file_exists(DIR_FILES_BLOCK_TYPES . '/' . $obj->getBlockTypeHandle() . '/' . $filename)) {
$base = DIR_FILES_BLOCK_TYPES . '/' . $obj->getBlockTypeHandle();
} else {
if ($obj->getPackageID() > 0) {
if (is_dir(DIR_PACKAGES . '/' . $obj->getPackageHandle())) {
$base = DIR_PACKAGES . '/' . $obj->getPackageHandle(
) . '/' . DIRNAME_BLOCKS . '/' . $obj->getBlockTypeHandle();
} else {
$base = DIR_PACKAGES_CORE . '/' . $obj->getPackageHandle(
) . '/' . DIRNAME_BLOCKS . '/' . $obj->getBlockTypeHandle();
}
} else {
$base = DIR_FILES_BLOCK_TYPES_CORE . '/' . $obj->getBlockTypeHandle();
}
}
return $base;
}
/**
* Returns a relative path to the current block's directory. If a filename is specified it will be appended and searched for as well.
*
* @return string
*/
public function getBlockURL($filename = null)
{
$obj = $this->blockType;
if ($obj->getPackageID() > 0) {
if (is_dir(DIR_PACKAGES_CORE . '/' . $obj->getPackageHandle())) {
$base = ASSETS_URL . '/' . DIRNAME_PACKAGES . '/' . $obj->getPackageHandle(
) . '/' . DIRNAME_BLOCKS . '/' . $obj->getBlockTypeHandle();
} else {
$base = DIR_REL . '/' . DIRNAME_PACKAGES . '/' . $obj->getPackageHandle(
) . '/' . DIRNAME_BLOCKS . '/' . $obj->getBlockTypeHandle();
}
} else {
if (file_exists(DIR_FILES_BLOCK_TYPES . '/' . $obj->getBlockTypeHandle() . '/' . $filename)) {
$base = REL_DIR_APPLICATION . '/' . DIRNAME_BLOCKS . '/' . $obj->getBlockTypeHandle();
} else {
$base = ASSETS_URL . '/' . DIRNAME_BLOCKS . '/' . $obj->getBlockTypeHandle();
}
}
return $base;
}
public function inc($fileToInclude, $args = array())
{
extract($args);
extract($this->getScopeItems());
$env = Environment::get();
include $env->getPath(
DIRNAME_BLOCKS . '/' . $this->blockType->getBlockTypeHandle() . '/' . $fileToInclude,
$this->blockTypePkgHandle
);
}
public function getScopeItems()
{
$items = parent::getScopeItems();
$items['b'] = $this->block;
$items['bt'] = $this->blockType;
$items['a'] = $this->area;
return $items;
}
protected function useBlockCache()
{
$u = new User();
$c = Page::getCurrentPage();
if ($this->viewToRender == 'view' && Config::get('concrete.cache.blocks') && $this->block instanceof Block
&& $this->block->cacheBlockOutput() && is_object($c) && $c->isPageDraft() === false
) {
if ((!$u->isRegistered() || ($this->block->cacheBlockOutputForRegisteredUsers())) &&
(($_SERVER['REQUEST_METHOD'] != 'POST' || ($this->block->cacheBlockOutputOnPost() == true)))
) {
return true;
}
}
return false;
}
public function field($field)
{
return $field;
}
public function usedBlockCacheDuringRender()
{
return $this->didPullFromOutputCache;
}
public function finishRender($contents)
{
if ($this->useBlockCache() && !$this->didPullFromOutputCache) {
$this->block->setBlockCachedOutput(
$this->outputContent,
$this->block->getBlockOutputCacheLifetime(),
$this->area
);
}
return $contents;
}
public function runControllerTask()
{
$this->controller->on_start();
if ($this->useBlockCache()) {
$this->didPullFromOutputCache = true;
$this->outputContent = $this->block->getBlockCachedOutput($this->area);
}
if (!$this->outputContent) {
$this->didPullFromOutputCache = false;
if (in_array($this->viewToRender, array('view', 'add', 'edit', 'composer'))) {
$method = $this->viewToRender;
} else {
$method = 'view';
}
$passthru = false;
if ($method == 'view' && is_object($this->block)) {
$c = Page::getCurrentPage();
if (is_object($c)) {
$cnt = $c->getController();
$controller = $cnt->getPassThruBlockController($this->block);
if (is_object($controller)) {
$passthru = true;
$this->controller = $controller;
}
}
}
$parameters = array();
if (!$passthru) {
$this->controller->runAction($method, $parameters);
}
$this->controller->on_before_render();
}
}
/**
* Legacy.
*/
public function getThemePath()
{
$v = View::getInstance();
return $v->getThemePath();
}
/**
* Fire an event just before the block is outputted on the page
*
* Custom code can modify the block contents before
* the block contents are 'echoed' out on the page.
*
* @since 8.4.1
*/
private function fireOnBlockOutputEvent()
{
$event = new BlockOutput($this->block);
$event->setContents($this->outputContent);
$app = Application::getFacadeApplication();
$app->make('director')->dispatch('on_block_output', $event);
$this->outputContent = $event->getContents();
}
}