/
Block.php
2111 lines (1878 loc) · 68.6 KB
/
Block.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
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
namespace Concrete\Core\Block;
use BlockType;
use CacheLocal;
use Collection;
use Concrete\Core\Area\Area;
use Concrete\Core\Backup\ContentExporter;
use Concrete\Core\Block\Events\BlockDuplicate;
use Concrete\Core\Block\View\BlockView;
use Concrete\Core\Database\Connection\Connection;
use Concrete\Core\Foundation\ConcreteObject;
use Concrete\Core\Package\PackageList;
use Concrete\Core\Page\Cloner;
use Concrete\Core\Permission\Key\Key as PermissionKey;
use Concrete\Core\StyleCustomizer\Inline\StyleSet;
use Concrete\Core\Support\Facade\Application;
use Concrete\Core\Support\Facade\Facade;
use Config;
use Loader;
use Page;
class Block extends ConcreteObject implements \Concrete\Core\Permission\ObjectInterface
{
/**
* @deprecated use the getBlockAreaObject() method (what's deprecated is the "public" part, it should be protected)
*
* @var \Concrete\Core\Area\Area|null
*/
public $a;
/**
* The ID of the collection containing the block.
*
* @var int|null
*/
protected $cID;
/**
* The handle of the area containing the block.
*
* @var string|null
*/
protected $arHandle;
/**
* The collection instance containing the block.
*
* @var \Concrete\Core\Page\Collection\Collection|null
*/
protected $c;
/**
* The custom style set ID.
*
* @var int|null
*/
protected $issID;
/**
* The proxy block instance.
*
* @var \Concrete\Core\Block\Block|false
*/
protected $proxyBlock = false;
/**
* The ID of the associated block.
*
* @var int|null
*/
protected $cbRelationID;
/**
* The ID of the collection that's associated to the block actions.
*
* @var int|null
*/
protected $bActionCID;
/**
* The block cache settings.
*
* @var \Concrete\Core\Block\CacheSettings|null
*/
protected $cacheSettings;
/**
* Override cache settings?
*
* @var int|null 1 for true; 0/null for false
*/
protected $cbOverrideBlockTypeCacheSettings;
/**
* The custom template name.
*
* @var string|null
*/
protected $bFilename;
/**
* Destruct the class instance.
*/
public function __destruct()
{
unset($this->c);
unset($this->a);
unset($this->instance);
}
/**
* Get a block instance given its ID.
*
* @param int $bID The block ID
* @param \Concrete\Core\Page\Collection\Collection|null $c the collection instance containing the block
* @param \Concrete\Core\Area\Area|string|null $a the area containing the block (or its handle)
*
* @return \Concrete\Core\Block\Block|null|false Return NULL if the block wasn't found; false if the block type class wasn't found; a Block instance otherwise
*/
public static function getByID($bID, $c = null, $a = null)
{
if ($c == null && $a == null) {
$cID = 0;
$arHandle = '';
$cvID = 0;
$b = CacheLocal::getEntry('block', $bID);
} else {
if (is_object($a)) {
$arHandle = $a->getAreaHandle();
} else {
if ($a != null) {
$arHandle = $a;
$a = Area::getOrCreate($c, $a);
}
}
$cID = $c->getCollectionID();
$cvID = $c->getVersionID();
$b = CacheLocal::getEntry('block', $bID . ':' . $cID . ':' . $cvID . ':' . $arHandle);
}
if ($b instanceof self) {
return $b;
}
$db = Loader::db();
$b = new self();
if ($c == null && $a == null) {
// just grab really specific block stuff
$q = 'select bID, bIsActive, BlockTypes.btID, Blocks.btCachedBlockRecord, BlockTypes.btHandle, BlockTypes.pkgID, BlockTypes.btName, bName, bDateAdded, bDateModified, bFilename, Blocks.uID from Blocks inner join BlockTypes on (Blocks.btID = BlockTypes.btID) where bID = ?';
$b->isOriginal = 1;
$v = [$bID];
} else {
$b->arHandle = $arHandle;
$b->a = $a;
$b->cID = $cID;
$b->c = ($c) ? $c : '';
$vo = $c->getVersionObject();
$cvID = $vo->getVersionID();
$v = [$b->arHandle, $cID, $cvID, $bID];
$q = 'select CollectionVersionBlocks.isOriginal, CollectionVersionBlocks.cbIncludeAll, Blocks.btCachedBlockRecord, BlockTypes.pkgID, CollectionVersionBlocks.cbOverrideAreaPermissions, CollectionVersionBlocks.cbOverrideBlockTypeCacheSettings, CollectionVersionBlocks.cbRelationID,
CollectionVersionBlocks.cbOverrideBlockTypeContainerSettings, CollectionVersionBlocks.cbEnableBlockContainer, CollectionVersionBlocks.cbDisplayOrder, Blocks.bIsActive, Blocks.bID, Blocks.btID, bName, bDateAdded, bDateModified, bFilename, btHandle, Blocks.uID from CollectionVersionBlocks inner join Blocks on (CollectionVersionBlocks.bID = Blocks.bID) inner join BlockTypes on (Blocks.btID = BlockTypes.btID) where CollectionVersionBlocks.arHandle = ? and CollectionVersionBlocks.cID = ? and (CollectionVersionBlocks.cvID = ? or CollectionVersionBlocks.cbIncludeAll=1) and CollectionVersionBlocks.bID = ?';
}
$row = $db->fetchAssoc($q, $v);
if ($row !== false) {
$b->setPropertiesFromArray($row);
$bt = BlockType::getByID($b->getBlockTypeID());
$class = $bt->getBlockTypeClass();
if ($class == false) {
// we can't find the class file, so we return
return false;
}
$app = Facade::getFacadeApplication();
$b->instance = $app->make($class, ['obj' => $b]);
if ($c != null || $a != null) {
CacheLocal::set('block', $bID . ':' . $cID . ':' . $cvID . ':' . $arHandle, $b);
} else {
CacheLocal::set('block', $bID, $b);
}
return $b;
}
}
/**
* Get a block instance given its name.
*
* @param string $blockName
*
* @return \Concrete\Core\Block\Block|null returns NULL if $blockName is empty; a Block instance otherwise (the getBlockID() method will return NULL if the block was not found)
*/
public static function getByName($blockName)
{
if (!$blockName) {
return;
}
$app = Application::getFacadeApplication();
$now = $app->make('date')->getOverridableNow();
$db = Loader::db();
$sql = <<<'EOT'
SELECT
b.bID,
cvb.arHandle,
cvb.cID
FROM
Blocks AS b
INNER JOIN CollectionVersionBlocks AS cvb
ON b.bID = cvb.bID
INNER JOIN CollectionVersions AS cv
ON cvb.cID = cv.cID AND cvb.cvID = cv.cvID
WHERE
b.bName = ?
AND cv.cvIsApproved = 1 AND (cv.cvPublishDate IS NULL or cv.cvPublishDate <= ?) AND (cv.cvPublishEndDate IS NULL or cv.cvPublishEndDate >= ?)
ORDER BY
cvb.cvID DESC
LIMIT 1
EOT
;
$vals = [$blockName, $now, $now];
$row = $db->getRow($sql, $vals);
if ($row != false && isset($row['bID']) && $row['bID'] > 0) {
return self::getByID($row['bID'], Page::getByID($row['cID']), $row['arHandle']);
} else {
return new self();
}
}
/**
* Initialize the instance by manually specifying the data.
*
* @param array|\Iterator $blockInfo A set key-value pairs used to initialize the instance
* @param \Concrete\Core\Page\Collection\Collection $c The collection containing the block
* @param \Concrete\Core\Area\Area|string|null $a the area containing the block (or its handle)
*
* @return \Concrete\Core\Block\Block
*/
public static function populateManually($blockInfo, $c, $a)
{
$b = new self();
$b->setPropertiesFromArray($blockInfo);
if (is_object($a)) {
$b->a = $a;
$b->arHandle = $a->getAreaHandle();
} else {
if ($a != null) {
$b->arHandle = $a; // passing the area name. We only pass the object when we're adding from the front-end
}
}
$b->cID = $c->getCollectionID();
$b->c = $c;
return $b;
}
/**
* Returns the block identifier (if available).
*
* @version <= 8.4.2 Method returns string|null
* @version 8.4.3 Method returns int|null
*
* @return int|null
*/
public function getBlockID()
{
return isset($this->bID) ? (int) $this->bID : null;
}
/**
* Get the absolute path of the block directory (.../blocks/block_handle).
*
* @return string
*/
public function getBlockPath()
{
if ($this->getPackageID() > 0) {
$pkgHandle = $this->getPackageHandle();
$dirp = (is_dir(DIR_PACKAGES . '/' . $pkgHandle)) ? DIR_PACKAGES : DIR_PACKAGES_CORE;
$dir = $dirp . '/' . $pkgHandle . '/' . DIRNAME_BLOCKS . '/' . $this->getBlockTypeHandle();
} else {
if (is_dir(DIR_FILES_BLOCK_TYPES . '/' . $this->getBlockTypeHandle())) {
$dir = DIR_FILES_BLOCK_TYPES . '/' . $this->getBlockTypeHandle();
} else {
$dir = DIR_FILES_BLOCK_TYPES_CORE . '/' . $this->getBlockTypeHandle();
}
}
return $dir;
}
/**
* Get the block name.
*
* @return string|null
*/
public function getBlockName()
{
return isset($this->bName) ? $this->bName : null;
}
/**
* Set the name of the block.
*
* @param string $name
*/
public function setName($name)
{
$data['bName'] = $name;
$this->updateBlockInformation($data);
}
/**
* Get the name of the custom template.
*
* @return string|null
*/
public function getBlockFilename()
{
return $this->bFilename;
}
/**
* Set the name of the custom template.
*
* @param string $template
*/
public function setCustomTemplate($template)
{
$data['bFilename'] = $template;
$this->updateBlockInformation($data);
}
/**
* Get the ID of the block type (if available).
*
* @return int|null
*/
public function getBlockTypeID()
{
return isset($this->btID) ? $this->btID : null;
}
/**
* Get the block type instance.
*
* @return \Concrete\Core\Block\BlockType\BlockType|null
*/
public function getBlockTypeObject()
{
$btID = $this->getBlockTypeID();
return $btID ? BlockType::getByID($btID) : null;
}
/**
* Get the block type handle.
*
* @return string|null
*/
public function getBlockTypeHandle()
{
return isset($this->btHandle) ? $this->btHandle : null;
}
/**
* Get the block type name.
*
* @return string|null
*/
public function getBlockTypeName()
{
return isset($this->btName) ? $this->btName : null;
}
/**
* Get the ID of the user that created the block instance.
*
* @return int|null
*/
public function getBlockUserID()
{
return isset($this->uID) ? $this->uID : null;
}
/**
* Gets the date/time when block was added (in the system time zone).
*
* @return string|null date/time formated like: 2009-01-01 00:00:00
*/
public function getBlockDateAdded()
{
return isset($this->bDateAdded) ? $this->bDateAdded : null;
}
/**
* Gets the date/time when block was last modified (in the system time zone).
*
* @return string|null date/time formated like: 2009-01-01 00:00:00
*/
public function getBlockDateLastModified()
{
return isset($this->bDateModified) ? $this->bDateModified : null;
}
/**
* Get the ID of the package owning this block type.
*
* @return int|null
*/
public function getPackageID()
{
return isset($this->pkgID) ? $this->pkgID : null;
}
/**
* Get the handle of the package owning this block type.
*
* @return string|false
*/
public function getPackageHandle()
{
$pkgID = $this->getPackageID();
return $pkgID ? PackageList::getHandle($pkgID) : null;
}
/**
* Get the ID of the collection containing the block.
*
* @return int|null
*/
public function getBlockCollectionID()
{
return $this->cID;
}
/**
* Get the collection instance containing the block.
*
* @return \Concrete\Core\Page\Collection\Collection|null
*/
public function getBlockCollectionObject()
{
if (is_object($this->c)) {
return $this->c;
} else {
return $this->getOriginalCollection();
}
}
/**
* Get the page instance where this block is defined (or the page where the original block is defined if this block is an alias).
*
* @return \Concrete\Core\Page\Page|null
*/
public function getOriginalCollection()
{
$bID = $this->getBlockID();
if ($bID) {
$db = Loader::db();
$q = 'select Pages.cID, cIsTemplate from Pages inner join CollectionVersionBlocks on (CollectionVersionBlocks.cID = Pages.cID) where CollectionVersionBlocks.bID = ? and CollectionVersionBlocks.isOriginal = 1';
$row = $db->fetchAssoc($q, [$bID]);
if ($row !== false) {
$cID = $row['cID'];
$nc = Page::getByID($cID, 'ACTIVE');
if (is_object($nc) && !$nc->isError()) {
return $nc;
}
}
}
}
/**
* Gets a list of pages that include this block, along with area name, etc...
*
* @return \Concrete\Core\Page\Page[]
*/
public function getPageList()
{
$cArray = [];
$bID = $this->getBlockID();
if ($bID) {
$db = Loader::db();
$q = 'select DISTINCT Pages.cID from CollectionVersionBlocks inner join Pages on (CollectionVersionBlocks.cID = Pages.cID) inner join CollectionVersions on (CollectionVersions.cID = Pages.cID) where CollectionVersionBlocks.bID = ?';
$r = $db->query($q, [$bID]);
if ($r) {
while ($row = $r->fetch()) {
$cArray[] = Page::getByID($row['cID'], 'RECENT');
}
}
}
return $cArray;
}
/**
* Set the collection instance containing the block.
*
* @param \Concrete\Core\Page\Collection\Collection $c
*/
public function setBlockCollectionObject($c)
{
$this->c = $c;
$this->cID = $c ? $c->getCollectionID() : null;
}
/**
* Get the handle of the area containing the block.
*
* @return string|null
*/
public function getAreaHandle()
{
return $this->arHandle;
}
/**
* Get the the instance of the Area containing the block.
*
* @return \Concrete\Core\Area\Area|null
*/
public function getBlockAreaObject()
{
if (is_object($this->a)) {
return $this->a;
}
}
/**
* Set the area containing the block.
*
* @param \Concrete\Core\Area\Area $a
*/
public function setBlockAreaObject(&$a)
{
$this->a = $a;
$this->arHandle = $a ? $a->getAreaHandle() : null;
}
/**
* Get the block display order (if available).
*
* @return int|null
*/
public function getBlockDisplayOrder()
{
return isset($this->cbDisplayOrder) && is_numeric($this->cbDisplayOrder) ? (int) $this->cbDisplayOrder : null;
}
/**
* Move this block after another block (in the block page & area).
*
* @param \Concrete\Core\Block\Block|null $afterBlock set to NULL to move this block at the first position; Set to a Block instance to move this block after that instance
*/
public function moveBlockToDisplayOrderPosition($afterBlock)
{
// first, we increase the display order of all blocks found after this one.
$db = Loader::db();
$c = $this->getBlockCollectionObject();
if ($afterBlock instanceof self) {
$block = self::getByID(
$afterBlock->getBlockID(),
$this->getBlockCollectionObject(),
$this->getBlockAreaObject()
);
$q = 'update CollectionVersionBlocks set cbDisplayOrder = cbDisplayOrder + 1 where cID = ? and (cvID = ? or cbIncludeAll=1) and arHandle = ? and cbDisplayOrder > ?';
$v = [$c->getCollectionID(), $c->getVersionID(), $this->getAreaHandle(), $block->getBlockDisplayOrder()];
$db->Execute($q, $v);
// now we set this block's display order to 1 + the current block
$q = 'update CollectionVersionBlocks set cbDisplayOrder = ? where bID = ? and cID = ? and (cvID = ? or cbIncludeAll=1) and arHandle = ?';
$v = [
$block->getBlockDisplayOrder() + 1,
$this->getBlockID(),
$c->getCollectionID(),
$c->getVersionID(),
$this->getAreaHandle(),
];
$db->Execute($q, $v);
} else {
$q = 'update CollectionVersionBlocks set cbDisplayOrder = cbDisplayOrder + 1 where cID = ? and (cvID = ? or cbIncludeAll=1) and arHandle = ?';
$v = [$c->getCollectionID(), $c->getVersionID(), $this->getAreaHandle()];
$db->Execute($q, $v);
$q = 'update CollectionVersionBlocks set cbDisplayOrder = ? where bID = ? and cID = ? and (cvID = ? or cbIncludeAll=1) and arHandle = ?';
$v = [0, $this->getBlockID(), $c->getCollectionID(), $c->getVersionID(), $this->getAreaHandle()];
$db->Execute($q, $v);
}
}
/**
* Set the absolute position of this block, regardless other blocks in the same page & area.
*
* @param int $do the new absolute position of the block (starting from 0)
*/
public function setAbsoluteBlockDisplayOrder($do)
{
$db = Loader::db();
$cID = $this->getBlockCollectionID();
$bID = $this->getBlockID();
$arHandle = $this->getAreaHandle();
$c = $this->getBlockCollectionObject();
$cvID = $c->getVersionID();
$q = 'update CollectionVersionBlocks set cbDisplayOrder = ? where bID = ? and cID = ? and (cvID = ? or cbIncludeAll=1) and arHandle = ?';
$r = $db->query($q, [$do, $bID, $cID, $cvID, $arHandle]);
}
/**
* Get the original block ID.
*
* @param int|null $originalBID
*/
public function setOriginalBlockID($originalBID)
{
$this->originalBID = $originalBID;
}
/**
* Get the proxy block instance.
*
* @return \Concrete\Core\Block\Block|false
*/
public function getProxyBlock()
{
return $this->proxyBlock;
}
/**
* Set the proxy block instance.
*
* @param \Concrete\Core\Block\Block|null $block
*/
public function setProxyBlock($block)
{
$this->proxyBlock = $block;
}
/**
* Get the ID of the associated block.
*
* @return int|null
*/
public function getBlockRelationID()
{
return $this->cbRelationID;
}
/**
* Get the ID of the collection that's associated to the block actions (or false if not found).
*
* @return int|false
*/
public function getBlockActionCollectionID()
{
if ($this->bActionCID > 0) {
return $this->bActionCID;
}
$c = Page::getCurrentPage();
if (is_object($c)) {
return $c->getCollectionID();
}
$c = $this->getBlockCollectionObject();
if (is_object($c)) {
return $c->getCollectionID();
}
return false;
}
/**
* Set the ID of the collection that's associated to the block actions.
*
* @param int|null $bActionCID
*/
public function setBlockActionCollectionID($bActionCID)
{
$this->bActionCID = $bActionCID;
}
/**
* Does this block have an edit user interface?
*
* @return bool
*/
public function isEditable()
{
if ($this->getBlockTypeHandle() === BLOCK_HANDLE_SCRAPBOOK_PROXY) {
$controller = $this->getController();
$originalBlockID = $controller->getOriginalBlockID();
$originalBlock = self::getByID($originalBlockID);
return $originalBlock && $originalBlock->isEditable();
}
$bv = new BlockView($this);
$path = $bv->getBlockPath(FILENAME_BLOCK_EDIT);
if (file_exists($path . '/' . FILENAME_BLOCK_EDIT)) {
return true;
}
return false;
}
/**
* Is the block active?
*
* @return int 0 for false, 1 for true
*/
public function isActive()
{
return isset($this->bIsActive) ? $this->bIsActive : null;
}
/**
* Deactivate the block.
*/
public function deactivate()
{
$db = Loader::db();
$q = 'update Blocks set bIsActive = 0 where bID = ?';
$db->query($q, [$this->getBlockID()]);
}
/**
* Activate the block.
*/
public function activate()
{
$db = Loader::db();
$q = 'update Blocks set bIsActive = 1 where bID = ?';
$db->query($q, [$this->getBlockID()]);
}
/**
* Is the block versioning disabled?
*
* @return int 0 for false, 1 for true
*/
public function disableBlockVersioning()
{
return isset($this->cbIncludeAll) ? $this->cbIncludeAll : null;
}
/**
* Check if this block instance is an alias.
*
* @param \Concrete\Core\Page\Collection\Collection|null $c if specified, check if the block with the ID of this instance is an alias in that page (otherwise the check is done agains this specific block instance)
*
* @return bool|null return NULL if $c is specified but there's no block with the same ID in that page
*/
public function isAlias($c = null)
{
if ($c) {
$db = Loader::db();
$cID = $c->getCollectionID();
$vo = $c->getVersionObject();
$cvID = $vo->getVersionID();
$q = 'select bID from CollectionVersionBlocks where bID = ? and cID=? and isOriginal = 0 and cvID = ?';
$r = $db->query($q, [$this->getBlockID(), $cID, $cvID]);
if ($r) {
return $r->rowCount() > 0;
}
} else {
return isset($this->isOriginal) ? (!$this->isOriginal) : false;
}
}
/**
* Check if this block is an alias from a page default.
*
* @return bool
*/
public function isAliasOfMasterCollection()
{
return $this->getBlockCollectionObject()->isBlockAliasedFromMasterCollection($this);
}
/**
* Get the number of alias of this block.
*
* @return int
*/
public function getNumChildren()
{
$db = Loader::db();
$q = 'select count(*) as total from CollectionVersionBlocks where bID = ? and isOriginal = 0';
$total = $db->getOne($q, [$this->getBlockID()]);
return $total;
}
/**
* Is this block inside a stack?
*
* @return bool
*/
public function isBlockInStack()
{
$co = $this->getBlockCollectionObject();
if (is_object($co)) {
if ($co->getPageTypeHandle() == STACKS_PAGE_TYPE) {
return true;
}
}
return false;
}
/**
* Get the ID of the custom style set.
*
* @return int returns 0 or false if the block does not have custom styles
*/
public function getCustomStyleSetID()
{
$db = Loader::db();
if (!isset($this->issID)) {
$co = $this->getBlockCollectionObject();
$arHandle = $this->getAreaHandle();
if ($arHandle) {
$a = $this->getBlockAreaObject();
if ($a->isGlobalArea()) {
// then we need to check against the global area name. We currently have the wrong area handle passed in
$arHandle = STACKS_AREA_NAME;
}
$v = [
$co->getCollectionID(),
$co->getVersionID(),
$arHandle,
$this->getBlockID(),
];
$this->issID = (int) $db->GetOne(
'select issID from CollectionVersionBlockStyles where cID = ? and cvID = ? and arHandle = ? and bID = ?',
$v
);
} else {
$this->issID = 0;
}
}
return $this->issID;
}
/**
* Get the custom style object associated to this block.
*
* @param bool $force Do you want a CustomStyle instance even if the block does not have custom styles?
*
* @return \Concrete\Core\Block\CustomStyle|null
*/
public function getCustomStyle($force = false)
{
if ($this->getCustomStyleSetID() > 0 || $force) {
$csr = StyleSet::getByID($this->getCustomStyleSetID());
$theme = $this->c->getCollectionThemeObject();
switch ($this->getBlockTypeHandle()) {
case BLOCK_HANDLE_LAYOUT_PROXY:
$bs = new CoreAreaLayoutCustomStyle($csr, $this, $theme);
break;
default:
$bs = new CustomStyle($csr, $this, $theme);
break;
}
return $bs;
}
}
/**
* Set the block custom styles.
*
* @param \Concrete\Core\Entity\StyleCustomizer\Inline\StyleSet $set
*/
public function setCustomStyleSet(\Concrete\Core\Entity\StyleCustomizer\Inline\StyleSet $set)
{
$db = Loader::db();
$c = $this->getBlockCollectionObject();
$cvID = $c->getVersionID();
$db->Replace(
'CollectionVersionBlockStyles',
[
'cID' => $this->getBlockCollectionID(),
'cvID' => $cvID,
'arHandle' => $this->getAreaHandle(),
'bID' => $this->getBlockID(),
'issID' => $set->getID(),
],
[
'cID',
'cvID',
'bID',
'arHandle',
],
true
);
$this->issID = (int) $set->getID();
}
/**
* Remove the block custom styles.
*/
public function resetCustomStyle()
{
$db = Loader::db();
$c = $this->getBlockCollectionObject();
$cvID = $c->getVersionID();
$db->Execute(
'delete from CollectionVersionBlockStyles where cID = ? and cvID = ? and arHandle = ? and bID = ?',
[
$this->getBlockCollectionID(),
$cvID,
$this->getAreaHandle(),
$this->getBlockID(),
]
);
$this->issID = 0;
}
/**
* Get the cache settings instance.
*
* @return \Concrete\Core\Block\CacheSettings
*/
public function getBlockCacheSettingsObject()
{
if ($this->cacheSettings === null) {
$this->cacheSettings = CacheSettings::get($this);
}
return $this->cacheSettings;
}
/**
* Override cache settings?
*
* @var int|null 1 for true; 0/null for false
*/
public function overrideBlockTypeCacheSettings()
{
if (!$this->cbOverrideBlockTypeCacheSettings) {
$this->cbOverrideBlockTypeCacheSettings = 0;
}
return $this->cbOverrideBlockTypeCacheSettings;
}
/**
* Customize the cache settings, overriding the values of the block type controller.
*
* @param bool $enabled Should the block output be cached?
* @param bool $enabledOnPost Should the block output be cached upon POST requests?
* @param bool $enabledForRegistered Should the block output be cached when site visitors are registered users?
* @param int $lifetime cache lifetime (in seconds); if empty we'll assume 5 years
*/
public function setCustomCacheSettings($enabled, $enabledOnPost, $enabledForRegistered, $lifetime)
{
$db = Loader::db();
$c = $this->getBlockCollectionObject();
$cvID = $c->getVersionID();
$enabled = (bool) $enabled;
$enabledOnPost = (bool) $enabledOnPost;
$enabledForRegistered = (bool) $enabledForRegistered;
$lifetime = (int) $lifetime;
$db->Replace(
'CollectionVersionBlocksCacheSettings',
[
'cID' => $this->getBlockCollectionID(),
'cvID' => $cvID,
'arHandle' => $this->getAreaHandle(),
'bID' => $this->getBlockID(),
'btCacheBlockOutput' => (int) $enabled,
'btCacheBlockOutputOnPost' => (int) $enabledOnPost,
'btCacheBlockOutputForRegisteredUsers' => (int) $enabledForRegistered,
'btCacheBlockOutputLifetime' => (int) $lifetime,
],
[
'cID',
'cvID',
'bID',
'arHandle',
],