/
BlockType.php
692 lines (619 loc) · 20 KB
/
BlockType.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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
<?php
namespace Concrete\Core\Entity\Block\BlockType;
use Concrete\Core\Block\Block;
use BlockTypeSet;
use Concrete\Block\CoreStackDisplay\Controller;
use Concrete\Core\Block\BlockType\BlockTypeList;
use Concrete\Core\Block\View\BlockView;
use Concrete\Core\Database\Schema\Schema;
use Concrete\Core\Filesystem\TemplateFile;
use Concrete\Core\Package\PackageList;
use Concrete\Core\Support\Facade\Application;
use Concrete\Core\Support\Facade\Facade;
use Core;
use Database as DB;
use Environment;
use Loader;
use Localization;
use Package;
use Page;
use User;
use Doctrine\ORM\Mapping as ORM;
use Concrete\Core\Database\Connection\Connection;
/**
* @ORM\Entity
* @ORM\Table(name="BlockTypes")
*/
class BlockType
{
public $controller;
/**
* @ORM\Column(type="boolean")
*/
protected $btIgnorePageThemeGridFrameworkContainer = false;
/**
* @ORM\Id @ORM\Column(type="integer")
* @ORM\GeneratedValue
*/
protected $btID;
/**
* @ORM\Column(type="string", length=128)
*/
protected $btHandle;
/**
* @ORM\Column(type="string", length=128)
*/
protected $btName;
/**
* @ORM\Column(type="text")
*/
protected $btDescription;
/**
* @ORM\Column(type="boolean")
*/
protected $btCopyWhenPropagate = false;
/**
* @ORM\Column(type="boolean")
*/
protected $btIncludeAll = false;
/**
* @ORM\Column(type="boolean")
*/
protected $btIsInternal = false;
/**
* @ORM\Column(type="boolean")
*/
protected $btSupportsInlineEdit = false;
/**
* @ORM\Column(type="boolean")
*/
protected $btSupportsInlineAdd = false;
/**
* @ORM\Column(type="integer")
*/
protected $btDisplayOrder = 0;
/**
* @ORM\Column(type="integer")
*/
protected $btInterfaceHeight;
/**
* @ORM\Column(type="integer")
*/
protected $btInterfaceWidth;
/**
* @ORM\Column(type="integer", options={"unsigned": true})
*/
protected $pkgID = 0;
public function getBlockTypeInSetName()
{
if ($this->controller) {
return $this->controller->getBlockTypeInSetName();
}
}
/**
* Sets the Ignore Page Theme Gride Framework Container.
*/
public function setBlockTypeIgnorePageThemeGridFrameworkContainer($btIgnorePageThemeGridFrameworkContainer)
{
$this->btIgnorePageThemeGridFrameworkContainer = $btIgnorePageThemeGridFrameworkContainer;
}
/**
* Sets the block type handle.
*/
public function setBlockTypeName($btName)
{
$this->btName = $btName;
}
/**
* Sets the block type description.
*/
public function setBlockTypeDescription($btDescription)
{
$this->btDescription = $btDescription;
}
/**
* Sets the block type handle.
*/
public function setBlockTypeHandle($btHandle)
{
$this->btHandle = $btHandle;
}
public function setPackageID($pkgID)
{
$this->pkgID = $pkgID;
}
/**
* Determines if the block type has templates available.
*
* @return bool
*/
public function hasAddTemplate()
{
$bv = new BlockView($this);
$path = $bv->getBlockPath(FILENAME_BLOCK_ADD);
if (file_exists($path . '/' . FILENAME_BLOCK_ADD)) {
return true;
}
return false;
}
/**
* gets the available composer templates
* used for editing instances of the BlockType while in the composer ui in the dashboard.
*
* @return TemplateFile[]
*/
public function getBlockTypeComposerTemplates()
{
$btHandle = $this->getBlockTypeHandle();
$files = array();
$fh = Loader::helper('file');
$dir = DIR_FILES_BLOCK_TYPES . "/{$btHandle}/" . DIRNAME_BLOCK_TEMPLATES_COMPOSER;
if (is_dir($dir)) {
$files = array_merge($files, $fh->getDirectoryContents($dir));
}
foreach (PackageList::get()->getPackages() as $pkg) {
$dir =
(is_dir(DIR_PACKAGES . '/' . $pkg->getPackageHandle()) ? DIR_PACKAGES : DIR_PACKAGES_CORE)
. '/' . $pkg->getPackageHandle() . '/' . DIRNAME_BLOCKS . '/' . $btHandle . '/' . DIRNAME_BLOCK_TEMPLATES_COMPOSER;
if (is_dir($dir)) {
$files = array_merge($files, $fh->getDirectoryContents($dir));
}
}
$dir = DIR_FILES_BLOCK_TYPES_CORE . "/{$btHandle}/" . DIRNAME_BLOCK_TEMPLATES_COMPOSER;
if (file_exists($dir)) {
$files = array_merge($files, $fh->getDirectoryContents($dir));
}
$templates = array();
foreach (array_unique($files) as $file) {
$templates[] = new TemplateFile($this, $file);
}
return TemplateFile::sortTemplateFileList($templates);
}
/**
* @return string
*/
public function getBlockTypeHandle()
{
return $this->btHandle;
}
/**
* if a the current BlockType supports inline edit or not.
*
* @return bool
*/
public function supportsInlineEdit()
{
return $this->btSupportsInlineEdit;
}
/**
* if a the current BlockType supports inline add or not.
*
* @return bool
*/
public function supportsInlineAdd()
{
return $this->btSupportsInlineAdd;
}
/**
* Returns true if the block type is internal (and therefore cannot be removed) a core block.
*
* @return bool
*/
public function isInternalBlockType()
{
return $this->btIsInternal;
}
/**
* returns the width in pixels that the block type's editing dialog will open in.
*
* @return int
*/
public function getBlockTypeInterfaceWidth()
{
return $this->btInterfaceWidth;
}
/**
* returns the height in pixels that the block type's editing dialog will open in.
*
* @return int
*/
public function getBlockTypeInterfaceHeight()
{
return $this->btInterfaceHeight;
}
/**
* If true, container classes will not be wrapped around this block type in edit mode (if the
* theme in question supports a grid framework.
*
* @return bool
*/
public function ignorePageThemeGridFrameworkContainer()
{
return $this->btIgnorePageThemeGridFrameworkContainer;
}
/**
* returns the id of the BlockType's package if it's in a package.
*
* @return int
*/
public function getPackageID()
{
return $this->pkgID;
}
/**
* gets the BlockTypes description text.
*
* @return string
*/
public function getBlockTypeDescription()
{
return $this->btDescription;
}
/**
* @return string
*/
public function getBlockTypeName()
{
return $this->btName;
}
/**
* @return bool
*/
public function isCopiedWhenPropagated()
{
return $this->btCopyWhenPropagate;
}
/**
* If true, this block is not versioned on a page – it is included as is on all versions of the page, even when updated.
*
* @return bool
*/
public function includeAll()
{
return $this->btIncludeAll;
}
/**
* @deprecated
*/
public function getBlockTypeClassFromHandle()
{
return $this->getBlockTypeClass();
}
/**
* Returns the class for the current block type.
*/
public function getBlockTypeClass()
{
return \Concrete\Core\Block\BlockType\BlockType::getBlockTypeMappedClass($this->getBlockTypeHandle(), $this->getPackageHandle());
}
/**
* returns the handle of the BlockType's package if it's in a package.
*
* @return string
*/
public function getPackageHandle()
{
return \Concrete\Core\Package\PackageList::getHandle($this->pkgID);
}
/**
* Returns an array of all BlockTypeSet objects that this block is in.
*
* @return BlockTypeSet[]
*/
public function getBlockTypeSets()
{
$db = Loader::db();
$list = array();
$r = $db->Execute(
'select btsID from BlockTypeSetBlockTypes where btID = ? order by displayOrder asc',
array($this->getBlockTypeID()));
while ($row = $r->FetchRow()) {
$list[] = BlockTypeSet::getByID($row['btsID']);
}
$r->Close();
return $list;
}
/**
* @return int
*/
public function getBlockTypeID()
{
return $this->btID;
}
/**
* Returns the number of unique instances of this block throughout the entire site
* note - this count could include blocks in areas that are no longer rendered by the theme.
*
* @param bool specify true if you only want to see the number of blocks in active pages
*
* @return int
*/
public function getCount($ignoreUnapprovedVersions = false)
{
$app = Application::getFacadeApplication();
$db = $app->make(Connection::class);
$now = $app->make('date')->getOverridableNow();
if ($ignoreUnapprovedVersions) {
$count = $db->GetOne(<<<'EOT'
SELECT
count(btID)
FROM
Blocks b
INNER JOIN CollectionVersionBlocks cvb
ON b.bID=cvb.bID
INNER JOIN CollectionVersions cv
ON cvb.cID=cv.cID AND cvb.cvID=cv.cvID AND cv.cvIsApproved=1 AND (cv.cvPublishDate IS NULL OR cv.cvPublishDate <= ?) AND (cv.cvPublishEndDate IS NULL OR cv.cvPublishEndDate >= ?)
WHERE
b.btID = ?
EOT
,
[$now, $now, $this->btID]
);
} else {
$count = $db->GetOne("SELECT count(btID) FROM Blocks WHERE btID = ?", array($this->btID));
}
return $count;
}
/**
* Not a permissions call. Actually checks to see whether this block is not an internal one.
*
* @return bool
*/
public function canUnInstall()
{
return !$this->isBlockTypeInternal();
}
/**
* if a the current BlockType is Internal or not - meaning one of the core built-in concrete5 blocks.
*
* @return bool
*/
public function isBlockTypeInternal()
{
return $this->btIsInternal;
}
/**
* Renders a particular view of a block type, using the public $controller variable as the block type's controller.
*
* @param string template 'view' for the default
*/
public function render($view = 'view')
{
$bv = new BlockView($this);
$bv->render($view);
}
/**
* get's the block type controller.
*
* @return BlockTypeController
*/
public function getController()
{
return $this->controller;
}
/**
* Gets the custom templates available for the current BlockType.
*
* @return TemplateFile[]
*/
public function getBlockTypeCustomTemplates(Block $b)
{
$btHandle = $this->getBlockTypeHandle();
$fh = Loader::helper('file');
$files = array();
$dir = DIR_FILES_BLOCK_TYPES . "/{$btHandle}/" . DIRNAME_BLOCK_TEMPLATES;
if (is_dir($dir)) {
$files = array_merge($files, $fh->getDirectoryContents($dir));
}
// Next, check the current theme.
$c = $b->getBlockCollectionObject();
if (is_object($c)) {
$theme = $c->getCollectionThemeObject();
if (is_object($theme)) {
$dir = DIR_FILES_THEMES . "/" . $theme->getThemeHandle() . "/" . DIRNAME_BLOCKS . "/" . $btHandle . "/" . DIRNAME_BLOCK_TEMPLATES;
if (is_dir($dir)) {
$files = array_merge($files, $fh->getDirectoryContents($dir));
}
if ($theme->getPackageHandle()) {
$dir =
(is_dir(DIR_PACKAGES . '/' . $theme->getPackageHandle()) ? DIR_PACKAGES : DIR_PACKAGES_CORE)
. '/' . $theme->getPackageHandle() . '/' . DIRNAME_THEMES . '/' . $theme->getThemeHandle() . '/' . DIRNAME_BLOCKS . '/' . $btHandle . '/' . DIRNAME_BLOCK_TEMPLATES;
if (is_dir($dir)) {
$files = array_merge($files, $fh->getDirectoryContents($dir));
}
}
$dir = DIR_FILES_THEMES_CORE . "/" . $theme->getThemeHandle() . "/" . DIRNAME_BLOCKS . "/" . $btHandle . "/" . DIRNAME_BLOCK_TEMPLATES;
if (is_dir($dir)) {
$files = array_merge($files, $fh->getDirectoryContents($dir));
}
}
}
// NOW, we check to see if this btHandle has any custom templates that have been installed as separate packages
foreach (PackageList::get()->getPackages() as $pkg) {
$dir =
(is_dir(DIR_PACKAGES . '/' . $pkg->getPackageHandle()) ? DIR_PACKAGES : DIR_PACKAGES_CORE)
. '/' . $pkg->getPackageHandle() . '/' . DIRNAME_BLOCKS . '/' . $btHandle . '/' . DIRNAME_BLOCK_TEMPLATES;
if (is_dir($dir)) {
$files = array_merge($files, $fh->getDirectoryContents($dir));
}
}
$dir = DIR_FILES_BLOCK_TYPES_CORE . "/{$btHandle}/" . DIRNAME_BLOCK_TEMPLATES;
if (is_dir($dir)) {
$files = array_merge($files, $fh->getDirectoryContents($dir));
}
$templates = array();
foreach (array_unique($files) as $file) {
$templates[] = new TemplateFile($this, $file);
}
return TemplateFile::sortTemplateFileList($templates);
}
/**
* @private
*/
public function setBlockTypeDisplayOrder($displayOrder)
{
$db = Loader::db();
$displayOrder = intval($displayOrder); //in case displayOrder came from a string (so ADODB escapes it properly)
$sql = "UPDATE BlockTypes SET btDisplayOrder = btDisplayOrder - 1 WHERE btDisplayOrder > ?";
$vals = array($this->btDisplayOrder);
$db->Execute($sql, $vals);
$sql = "UPDATE BlockTypes SET btDisplayOrder = btDisplayOrder + 1 WHERE btDisplayOrder >= ?";
$vals = array($displayOrder);
$db->Execute($sql, $vals);
$sql = "UPDATE BlockTypes SET btDisplayOrder = ? WHERE btID = ?";
$vals = array($displayOrder, $this->btID);
$db->Execute($sql, $vals);
// now we remove the block type from cache
/** @var \Concrete\Core\Cache\Cache $cache */
$cache = Core::make('cache');
$cache->delete('blockTypeByID/' . $this->btID);
$cache->delete('blockTypeByHandle/' . $this->btHandle);
$cache->delete('blockTypeList');
}
/**
* Get the display order of this block type when it's not assigned to any block type set.
*
* @return int
*/
public function getBlockTypeDisplayOrder()
{
return $this->btDisplayOrder;
}
/**
* refreshes the BlockType's database schema throws an Exception if error.
*/
public function refresh()
{
$app = Facade::getFacadeApplication();
$db = $app->make('database')->connection();
$pkgHandle = false;
if ($this->pkgID > 0) {
$pkgHandle = $this->getPackageHandle();
}
$class = \Concrete\Core\Block\BlockType\BlockType::getBlockTypeMappedClass($this->btHandle, $pkgHandle);
$bta = $app->build($class);
$this->loadFromController($bta);
$em = \ORM::entityManager();
$em->persist($this);
$em->flush();
$env = Environment::get();
$r = $env->getRecord(DIRNAME_BLOCKS . '/' . $this->btHandle . '/' . FILENAME_BLOCK_DB, $this->getPackageHandle());
if ($r->exists()) {
$parser = Schema::getSchemaParser(simplexml_load_file($r->file));
$parser->setIgnoreExistingTables(false);
$toSchema = $parser->parse($db);
$fromSchema = $db->getSchemaManager()->createSchema();
$comparator = new \Doctrine\DBAL\Schema\Comparator();
$schemaDiff = $comparator->compare($fromSchema, $toSchema);
$saveQueries = $schemaDiff->toSaveSql($db->getDatabasePlatform());
foreach ($saveQueries as $query) {
$db->query($query);
}
}
}
public function loadFromController($bta)
{
$this->btName = $bta->getBlockTypeName();
$this->btDescription = $bta->getBlockTypeDescription();
$this->btCopyWhenPropagate = $bta->isCopiedWhenPropagated();
$this->btIncludeAll = $bta->includeAll();
$this->btIsInternal = $bta->isBlockTypeInternal();
$this->btSupportsInlineEdit = $bta->supportsInlineEdit();
$this->btSupportsInlineAdd = $bta->supportsInlineAdd();
$this->btIgnorePageThemeGridFrameworkContainer = $bta->ignorePageThemeGridFrameworkContainer();
$this->btInterfaceHeight = $bta->getInterfaceHeight();
$this->btInterfaceWidth = $bta->getInterfaceWidth();
}
/**
* Removes the block type. Also removes instances of content.
*/
public function delete()
{
$db = Loader::db();
$r = $db->Execute(
'select cID, cvID, b.bID, arHandle
from CollectionVersionBlocks cvb
inner join Blocks b on b.bID = cvb.bID
where btID = ?
union
select cID, cvID, cvb.bID, arHandle
from CollectionVersionBlocks cvb
inner join btCoreScrapbookDisplay btCSD on cvb.bID = btCSD.bID
inner join Blocks b on b.bID = btCSD.bOriginalID
where btID = ?',
array($this->getBlockTypeID(), $this->getBlockTypeID()));
while ($row = $r->FetchRow()) {
$nc = Page::getByID($row['cID'], $row['cvID']);
if (!is_object($nc) || $nc->isError()) {
continue;
}
$b = Block::getByID($row['bID'], $nc, $row['arHandle']);
if (is_object($b)) {
$b->deleteBlock();
}
}
$em = \ORM::entityManager();
$em->remove($this);
$em->flush();
//Remove gaps in display order numbering (to avoid future sorting errors)
BlockTypeList::resetBlockTypeDisplayOrder('btDisplayOrder');
}
/**
* Adds a block to the system without adding it to a collection.
* Passes page and area data along if it is available, however.
*
* @param mixed $data
* @param bool|\Collection $c
* @param bool|\Area $a
*
* @return bool|\Concrete\Core\Block\Block
*/
public function add($data, $c = false, $a = false)
{
$app = Facade::getFacadeApplication();
$db = $app->make('database')->connection();
$u = new User();
if (isset($data['uID'])) {
$uID = $data['uID'];
} else {
$uID = $u->getUserID();
}
$bName = '';
if (isset($data['bName'])) {
$bName = $data['bName'];
}
$btID = $this->btID;
$dh = $app->make('helper/date');
$bDate = $dh->getOverridableNow();
$bIsActive = (isset($this->btActiveWhenAdded) && $this->btActiveWhenAdded == 1) ? 1 : 0;
$v = array($bName, $bDate, $bDate, $bIsActive, $btID, $uID);
$q = "insert into Blocks (bName, bDateAdded, bDateModified, bIsActive, btID, uID) values (?, ?, ?, ?, ?, ?)";
$res = $db->executeQuery($q, $v);
// we get the block object for the block we just added
if ($res) {
$bIDnew = $db->lastInsertId();
$nb = Block::getByID($bIDnew);
if (is_object($c)) {
$nb->setBlockCollectionObject($c);
}
if (is_object($a)) {
$nb->setBlockAreaObject($a);
}
$class = $this->getBlockTypeClass();
$bc = $app->build($class, [$nb]);
$bc->save($data);
return Block::getByID($bIDnew);
}
}
/**
* Loads controller.
*/
public function loadController()
{
$class = $this->getBlockTypeClass();
/** @var Controller controller */
if ($class) {
$this->controller = Facade::getFacadeApplication()->build($class, [$this]);
}
}
}