/
Theme.php
1393 lines (1268 loc) · 41.7 KB
/
Theme.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\Page\Theme;
use Concrete\Core\Cache\Level\RequestCache;
use Concrete\Core\Entity\Page\Theme\CustomSkin;
use Concrete\Core\Entity\Permission\IpAccessControlCategory;
use Concrete\Core\Entity\Site\Site;
use Concrete\Core\Filesystem\FileLocator\Record;
use Concrete\Core\Http\ResponseAssetGroup;
use Concrete\Core\Page\PageList;
use Concrete\Core\Page\Theme\Color\ColorCollection;
use Concrete\Core\Page\Theme\Documentation\DocumentationProviderInterface;
use Concrete\Core\Page\Theme\Documentation\Installer;
use Concrete\Core\StyleCustomizer\Customizer\Customizer;
use Concrete\Core\StyleCustomizer\Customizer\CustomizerFactory;
use Concrete\Core\StyleCustomizer\Customizer\CustomizerInterface;
use Concrete\Core\StyleCustomizer\Normalizer\NormalizedVariableCollection;
use Concrete\Core\StyleCustomizer\Normalizer\NormalizedVariableCollectionFactory;
use Concrete\Core\StyleCustomizer\Skin\SkinFactory;
use Concrete\Core\StyleCustomizer\Skin\SkinInterface;
use Concrete\Core\Support\Facade\Facade;
use Config;
use Doctrine\ORM\EntityManager;
use Illuminate\Filesystem\Filesystem;
use Loader;
use Concrete\Core\Page\Page;
use Environment;
use Core;
use Concrete\Core\Page\Theme\File as PageThemeFile;
use Concrete\Core\Package\PackageList;
use Concrete\Core\Foundation\ConcreteObject;
use PageTemplate;
use Concrete\Core\Page\Theme\GridFramework\GridFramework;
use Concrete\Core\Page\Single as SinglePage;
use Concrete\Core\StyleCustomizer\Preset;
use Concrete\Core\Entity\StyleCustomizer\CustomCssRecord;
use Localization;
use Punic\Comparer;
/**
* A page's theme is a pointer to a directory containing templates, CSS files and optionally PHP includes, images and JavaScript files.
* Themes inherit down the tree when a page is added, but can also be set at the site-wide level (thereby overriding any previous choices.).
*/
class Theme extends ConcreteObject implements \JsonSerializable
{
const E_THEME_INSTALLED = 1;
const THEME_EXTENSION = '.php';
const FILENAME_TYPOGRAPHY_CSS = 'typography.css';
protected $pThemeName;
protected $pThemeID;
protected $pThemeDescription;
protected $pThemeDirectory;
protected $pThemeThumbnail;
protected $pThemeHandle;
protected $pThemeURL;
protected $pThemeIsPreview = false;
protected $pkgID;
protected $stylesheetCachePath;
protected $stylesheetCacheRelativePath = REL_DIR_FILES_CACHE;
public function __construct()
{
$this->setStylesheetCachePath(Config::get('concrete.cache.directory'));
}
/**
* Get the installed themes provided by packages.
*
* @return \Concrete\Core\Page\Theme\Theme[]
*/
public static function getGlobalList()
{
return static::getList('pkgID > 0');
}
/**
* Get the installed themes provided by the core.
*
* @return \Concrete\Core\Page\Theme\Theme[]
*/
public static function getLocalList()
{
return static::getList('pkgID = 0');
}
/**
* Get the installed themes provided by a package.
*
* @return \Concrete\Core\Page\Theme\Theme[]
* @var \Concrete\Core\Entity\Package|\Concrete\Core\Package\Package $pkg
*
*/
public static function getListByPackage($pkg)
{
return static::getList('pkgID = ' . $pkg->getPackageID());
}
/**
* Get the installed themes.
*
* @param string|null $where a custom SQL criteria to filter the installed themes.
*
* @return \Concrete\Core\Page\Theme\Theme[]
*/
public static function getList($where = null)
{
if ($where != null) {
$where = ' where ' . $where;
}
$db = Loader::db();
$r = $db->query('select pThemeID from PageThemes' . $where);
$themes = [];
while ($row = $r->fetch()) {
$pl = static::getByID($row['pThemeID']);
$themes[] = $pl;
}
return $themes;
}
/**
* Get the handles of all the installed themes.
*
* @return string[]
*/
public static function getInstalledHandles()
{
$db = Loader::db();
return $db->GetCol('select pThemeHandle from PageThemes');
}
/**
* Mark an asset as provided by this theme.
*
* @param string $assetType E.g. 'css' or 'javascript' (or an asset group identifier like 'jquery/ui')
* @param string|false $assetHandle E.g. 'core/colorpicker'.
*/
public function providesAsset($assetType, $assetHandle = null)
{
$r = ResponseAssetGroup::get();
$r->markAssetAsIncluded($assetType, $assetHandle);
}
/**
* Mark an asset as reuired by this theme.
* Accepts the same arguments as \Concrete\Core\Http\ResponseAssetGroup::requireAsset().
*
* @see \Concrete\Core\Http\ResponseAssetGroup::requireAsset()
*/
public function requireAsset()
{
$r = ResponseAssetGroup::get();
$args = func_get_args();
call_user_func_array([$r, 'requireAsset'], $args);
}
/**
* Get the all the themes available in the /application/themes directory.
*
* @param bool $filterInstalled true (default) to exclude the already installed themes, false to include them.
*
* @return \Concrete\Core\Page\Theme\Theme[]
*/
public static function getAvailableThemes($filterInstalled = true)
{
$db = Loader::db();
$dh = Loader::helper('file');
$themes = $dh->getDirectoryContents(DIR_FILES_THEMES);
if ($filterInstalled) {
// strip out themes we've already installed
$handles = $db->GetCol('select pThemeHandle from PageThemes');
$themesTemp = [];
foreach ($themes as $t) {
if (!in_array($t, $handles)) {
$themesTemp[] = $t;
}
}
$themes = $themesTemp;
}
if (count($themes) > 0) {
$themesTemp = [];
// get theme objects from the file system
foreach ($themes as $t) {
$th = static::getByFileHandle($t);
if (!empty($th)) {
$themesTemp[] = $th;
}
}
$themes = $themesTemp;
}
return $themes;
}
/**
* Get a theme from the file system.
*
* @param string $handle the theme handle
* @param string $dir the parent directory that contains the theme directory
* @param string $pkgHandle the handle of the package providing the theme
*
* @return \Concrete\Core\Page\Theme\Theme|null
*/
public static function getByFileHandle($handle, $dir = DIR_FILES_THEMES, $pkgHandle = '')
{
$dirt = $dir . '/' . $handle;
if (is_dir($dirt)) {
$res = static::getThemeNameAndDescription($dirt, $handle, $pkgHandle);
$th = new static();
$th->pThemeHandle = $handle;
$th->pThemeDirectory = $dirt;
$th->pThemeName = $res->pThemeName;
$th->pThemeDescription = $res->pThemeDescription;
if (strlen($res->pError) > 0) {
$th->error = $res->pError;
}
switch ($dir) {
case DIR_FILES_THEMES:
$th->pThemeURL = DIR_REL . '/' . DIRNAME_APPLICATION . '/' . DIRNAME_THEMES . '/' . $handle;
break;
}
return $th;
}
}
/**
* Returns true if theme or user preset skins are available
*
* @return bool
*/
public function hasSkins(): bool
{
return $this->hasPresetSkins();
}
/**
* Checks the filesystem and returns true if custom skins are available for the theme.
*
* @return bool
*/
public function hasPresetSkins(): bool
{
$r = $this->getSkinDirectoryRecord();
return $r->exists();
}
public function getSkinDirectoryRecord(): Record
{
$env = Environment::get();
return $env->getRecord(
DIRNAME_THEMES . '/' . $this->getThemeHandle() . '/' . DIRNAME_CSS . '/' . DIRNAME_STYLE_CUSTOMIZER_SKINS,
$this->getPackageHandle()
);
}
/**
*
* @return SkinInterface[]
*/
public function getPresetSkins(): array
{
$skins = [];
if ($this->hasPresetSkins()) {
$factory = app(SkinFactory::class);
$skins = $factory->createMultipleFromDirectory($this->getSkinDirectoryRecord()->getFile(), $this);
}
return $skins;
}
/**
*
* @return SkinInterface[]
*/
public function getCustomSkins(): array
{
$entityManager = app(EntityManager::class);
$skins = $entityManager->getRepository(CustomSkin::class)->findBy(['pThemeID' => $this->getThemeID()]);
return $skins;
}
/**
*
* @return SkinInterface[]
*/
public function getSkins(): array
{
$allSkins = array_merge($this->getPresetSkins(), $this->getCustomSkins());
$cmp = new Comparer();
usort(
$allSkins,
function (SkinInterface $a, SkinInterface $b) use ($cmp) {
$cmp->compare($a->getName(), $b->getName());
}
);
return $allSkins;
}
/**
* @return bool
*/
public function isThemeCustomizable()
{
if ($this->getThemeCustomizer()) {
return true;
}
return false;
}
public function getThemeCustomizer(): ?Customizer
{
$customizer = app(CustomizerFactory::class)->createFromTheme($this);
return $customizer;
}
/**
* Gets the default skin for this theme
*
* @return SkinInterface|null
*/
public function getThemeDefaultSkin(): ?SkinInterface
{
return $this->getSkinByIdentifier(SkinInterface::SKIN_DEFAULT);
}
/**
* Returns a skin object when passed a string identifier
*
* @param string $skinIdentifier
* @return SkinInterface|null
*/
public function getSkinByIdentifier(string $skinIdentifier): ?SkinInterface
{
if ($this->hasSkins()) {
$skins = $this->getSkins();
foreach ($skins as $skin) {
if ($skin->getIdentifier() == $skinIdentifier) {
return $skin;
}
}
}
return null;
}
/**
* @return mixed|void
*/
public function jsonSerialize()
{
$hasSkins = $this->hasSkins();
$data = [
'pThemeID' => $this->getThemeID(),
'pThemeDisplayName' => $this->getThemeDisplayName(),
'pThemeDisplayDescription' => $this->getThemeDisplayDescription(),
'hasSkins' => $hasSkins,
'skins' => [],
];
if ($hasSkins) {
$data['skins'] = $this->getSkins();
}
return $data;
}
/**
* @deprecated but still required by the legacy theme customizer
* Set this instance to be a preview for the current request.
*/
public function enablePreviewRequest()
{
$this->setStylesheetCacheRelativePath(REL_DIR_FILES_CACHE.'/preview');
$this->setStylesheetCachePath(Config::get('concrete.cache.directory').'/preview');
$this->pThemeIsPreview = true;
}
/**
* Get all the customizable LESS stylesheets.
* @deprecated
* @return \Concrete\Core\StyleCustomizer\Stylesheet[]
*/
public function getThemeCustomizableStyleSheets()
{
$sheets = [];
$env = Environment::get();
if ($this->isThemeCustomizable()) {
$directory = $env->getPath(
DIRNAME_THEMES.'/'.$this->getThemeHandle().'/'.DIRNAME_CSS,
$this->getPackageHandle()
);
$dh = Loader::helper('file');
$files = $dh->getDirectoryContents($directory);
foreach ($files as $f) {
if (strrchr($f, '.') == '.less') {
$sheets[] = $this->getStylesheetObject($f);
}
}
}
return $sheets;
}
/**
* @deprecated
* Is this instance a preview for the current request?
*
* @return bool
*/
public function isThemePreviewRequest()
{
return $this->pThemeIsPreview;
}
/**
* @param string $stylesheet
*
* @return \Concrete\Core\StyleCustomizer\Stylesheet
* @deprecated
*
* Get a customizable LESS stylesheet given the stylesheed base file name.
*
*/
public function getStylesheetObject($stylesheet)
{
$env = Environment::get();
$output = $this->getStylesheetCachePath() . '/' . DIRNAME_CSS . '/' . $this->getThemeHandle();
$relative = $this->getStylesheetCacheRelativePath() . '/' . DIRNAME_CSS . '/' . $this->getThemeHandle();
$r = $env->getRecord(
DIRNAME_THEMES . '/' . $this->getThemeHandle() . '/' . DIRNAME_CSS . '/' . $stylesheet,
$this->getPackageHandle()
);
$stylesheet = new \Concrete\Core\StyleCustomizer\Stylesheet($stylesheet, $r->file, $r->url, $output, $relative);
return $stylesheet;
}
/**
* @param string $stylesheet The LESS stylesheet to compile
*
* @return string The path to the stylesheet
* @deprecated
* Look into the current CSS directory and return a fully compiled stylesheet when passed a LESS stylesheet.
* Also serves up custom value list values for the stylesheet if they exist.
*
*/
public function getStylesheet($stylesheet)
{
$stylesheet = $this->getStylesheetObject($stylesheet);
$style = $this->getThemeCustomStyleObject();
if (is_object($style)) {
$style = $this->getThemeCustomStyleObject();
if (is_object($style)) {
$valueList = $style->getValueList();
$factory = app(NormalizedVariableCollectionFactory::class);
$collection = $factory->createFromStyleValueList($valueList);
}
}
if (isset($collection) && $collection instanceof NormalizedVariableCollection) {
$stylesheet->setVariableCollection($collection);
}
if (!$this->isThemePreviewRequest()) {
if (!$stylesheet->outputFileExists() || !Config::get('concrete.cache.theme_css')) {
$stylesheet->output();
}
}
$path = $stylesheet->getOutputRelativePath();
if ($this->isThemePreviewRequest()) {
$path .= '?ts='.time();
} else {
$path .= '?ts='.filemtime($stylesheet->getOutputPath());
}
return $path;
}
/**
* @return \Concrete\Core\Page\CustomStyle|null
* @deprecated
*
* Get a CustomStyle object for the theme if one exists.
*
*/
public function getThemeCustomStyleObject()
{
$db = Loader::db();
$row = $db->FetchAssoc('select * from PageThemeCustomStyles where pThemeID = ?', [$this->getThemeID()]);
if (isset($row['pThemeID'])) {
$o = new \Concrete\Core\Page\CustomStyle();
$o->setThemeID($this->getThemeID());
$o->setValueListID($row['scvlID']);
$o->setPresetHandle($row['preset']);
$o->setCustomCssRecordID($row['sccRecordID']);
return $o;
}
}
/**
* Get an installed theme given its handle.
*
* @param string $pThemeHandle
*
* @return \Concrete\Core\Page\Theme\Theme|null
*/
public static function getByHandle($pThemeHandle)
{
$where = 'pThemeHandle = ?';
$args = [$pThemeHandle];
$pt = static::populateThemeQuery($where, $args);
return $pt;
}
/**
* Get an installed theme given its ID.
*
* @param int $pThemeID
*
* @return \Concrete\Core\Page\Theme\Theme|null
*/
public static function getByID($pThemeID)
{
/** @var RequestCache $cache */
$cache = Facade::getFacadeApplication()->make('cache/request');
$key = '/PageTheme/' . $pThemeID;
if ($cache->isEnabled()) {
$item = $cache->getItem($key);
if ($item->isHit()) {
return $item->get();
}
}
$where = 'pThemeID = ?';
$args = [$pThemeID];
$pt = static::populateThemeQuery($where, $args);
if (isset($item) && $item->isMiss()) {
$item->set($pt);
$cache->save($item);
}
return $pt;
}
/**
* Get the instance representing an installed theme.
*
* @param string $where the SQL part used in the SELECT query
* @param array $args the parameters of the SQL query
*
* @return \Concrete\Core\Page\Theme\Theme|null
*/
protected static function populateThemeQuery($where, $args)
{
$db = Loader::db();
$row = $db->GetRow(
"select pThemeID, pThemeHandle, pThemeDescription, pkgID, pThemeName, pThemeHasCustomClass from PageThemes where {$where}",
$args
);
$env = Environment::get();
$pl = null;
if (!empty($row)) {
$standardClass = '\\Concrete\Core\\Page\\Theme\\Theme';
if ($row['pThemeHasCustomClass']) {
$pkgHandle = PackageList::getHandle($row['pkgID']);
$r = $env->getRecord(
DIRNAME_THEMES . '/' . $row['pThemeHandle'] . '/' . FILENAME_THEMES_CLASS,
$pkgHandle
);
$prefix = $r->override ? true : $pkgHandle;
$customClass = core_class(
'Theme\\' .
Loader::helper('text')->camelcase($row['pThemeHandle']) .
'\\PageTheme',
$prefix
);
try {
$pl = Core::make($customClass);
} catch (\ReflectionException $e) {
$pl = Core::make($standardClass);
}
} else {
$pl = Core::make($standardClass);
}
$pl->setPropertiesFromArray($row);
$pkgHandle = $pl->getPackageHandle();
$pl->pThemeDirectory = $env->getPath(DIRNAME_THEMES . '/' . $row['pThemeHandle'], $pkgHandle);
$pl->pThemeURL = $env->getURL(DIRNAME_THEMES . '/' . $row['pThemeHandle'], $pkgHandle);
}
return $pl;
}
/**
* Install a theme given its handle.
*
* @param string $pThemeHandle the handle of the theme to be installed.
* @param \Concrete\Core\Entity\Package|\Concrete\Core\Package\Package|null $pkg
*
* @return \Concrete\Core\Page\Theme\Theme|null returns NULL if the directory containing the theme could not be found
* @throws \Exception in case of errors.
*
*/
public static function add($pThemeHandle, $pkg = null)
{
if (is_object($pkg)) {
if (is_dir(DIR_PACKAGES . '/' . $pkg->getPackageHandle())) {
$dir = DIR_PACKAGES . '/' . $pkg->getPackageHandle() . '/' . DIRNAME_THEMES . '/' . $pThemeHandle;
} else {
$dir = DIR_PACKAGES_CORE . '/' . $pkg->getPackageHandle() . '/' . DIRNAME_THEMES . '/' . $pThemeHandle;
}
$pkgID = $pkg->getPackageID();
} else {
if (is_dir(DIR_FILES_THEMES . '/' . $pThemeHandle)) {
$dir = DIR_FILES_THEMES . '/' . $pThemeHandle;
$pkgID = 0;
} else {
$dir = DIR_FILES_THEMES_CORE . '/' . $pThemeHandle;
$pkgID = 0;
}
}
$l = static::install($dir, $pThemeHandle, $pkgID);
return $l;
}
/**
* Grab all files in theme that are PHP based (or html if we go that route)
* and then lists them out, by type, allowing people to install them as page type, etc...
*
* @return \Concrete\Core\Page\Theme\File[]
*/
public function getFilesInTheme()
{
$dh = Loader::helper('file');
$templateList = PageTemplate::getList();
$pts = [];
foreach ($templateList as $pt) {
$pts[] = $pt->getPageTemplateHandle();
}
$files = [];
$filesTmp = $dh->getDirectoryContents($this->pThemeDirectory);
foreach ($filesTmp as $f) {
if (strrchr($f, '.') == static::THEME_EXTENSION) {
$fHandle = substr($f, 0, strpos($f, '.'));
if ($f == FILENAME_THEMES_VIEW) {
$type = PageThemeFile::TFTYPE_VIEW;
} elseif ($f == FILENAME_THEMES_CLASS) {
$type = PageThemeFile::TFTYPE_PAGE_CLASS;
} else {
if ($f == FILENAME_THEMES_DEFAULT) {
$type = PageThemeFile::TFTYPE_DEFAULT;
} else {
if (in_array($f, SinglePage::getThemeableCorePages())) {
$type = PageThemeFile::TFTYPE_SINGLE_PAGE;
} else {
if (in_array($fHandle, $pts)) {
$type = PageThemeFile::TFTYPE_PAGE_TEMPLATE_EXISTING;
} else {
$type = PageThemeFile::TFTYPE_PAGE_TEMPLATE_NEW;
}
}
}
}
$pf = new PageThemeFile();
$pf->setFilename($f);
$pf->setType($type);
$files[] = $pf;
}
}
return $files;
}
/**
* Get the theme details by reading a directory containing the theme.
*
* @param string $dir
* @param string $pThemeHandle
* @param string $pkgHandle
*
* @return \stdClass
*/
private static function getThemeNameAndDescription($dir, $pThemeHandle, $pkgHandle = '')
{
$res = new \stdClass();
$res->pThemeName = '';
$res->pThemeDescription = '';
$res->pError = '';
if (file_exists($dir . '/' . FILENAME_THEMES_DESCRIPTION)) {
$con = file($dir . '/' . FILENAME_THEMES_DESCRIPTION);
$res->pThemeName = trim($con[0]);
$res->pThemeDescription = trim($con[1]);
}
$pageThemeFile = $dir . '/' . FILENAME_THEMES_CLASS;
if (is_file($pageThemeFile)) {
try {
$cn = '\\Theme\\' . camelcase($pThemeHandle) . '\\PageTheme';
$classNames = [];
if (strlen($pkgHandle)) {
$classNames[] = '\\Concrete\\Package\\' . camelcase($pkgHandle) . $cn;
} else {
$classNames[] = '\\Application' . $cn;
$classNames[] = '\\Concrete' . $cn;
}
$className = null;
foreach ($classNames as $cn) {
if (class_exists($cn, false)) {
$className = $cn;
break;
}
}
if (is_null($className)) {
include_once $pageThemeFile;
foreach ($classNames as $cn) {
if (class_exists($cn, false)) {
$className = $cn;
break;
}
}
}
if (is_null($className)) {
$res->pError = t(/*i18n: %1$s is a filename, %2$s is a PHP class name */
'The theme file %1$s does not define the class %2$s',
FILENAME_THEMES_CLASS,
ltrim($classNames[0], '\\')
);
} else {
$instance = new $className();
$extensionOf = '\\Concrete\\Core\\Page\\Theme\\Theme';
if (!is_a($instance, $extensionOf)) {
$res->pError = t(/*i18n: %1$s is a filename, %2$s and %3$s are PHP class names */
'The theme file %1$s should define a %2$s class that extends the class %3$s',
FILENAME_THEMES_CLASS,
ltrim($className, '\\'),
ltrim($extensionOf, '\\')
);
} else {
if (method_exists($instance, 'getThemeName')) {
$s = $instance->getThemeName();
if (strlen($s) > 0) {
$res->pThemeName = $s;
}
}
if (method_exists($instance, 'getThemeDescription')) {
$s = $instance->getThemeDescription();
if (strlen($s) > 0) {
$res->pThemeDescription = $s;
}
}
}
}
} catch (\Exception $x) {
$res->pError = $x->getMessage();
}
}
return $res;
}
/**
* Export this theme by creating the XML nodes under the provided XML node.
*
* @param \SimpleXMLElement $node
*/
public function export($node)
{
$pst = static::getSiteTheme();
$activated = 0;
if ($pst->getThemeID() == $this->getThemeID()) {
$activated = 1;
}
$type = $node->addChild('theme');
$type->addAttribute('handle', $this->getThemeHandle());
$type->addAttribute('package', $this->getPackageHandle());
$type->addAttribute('activated', $activated);
}
/**
* Export all the installed themes by creating the XML nodes under the provided XML node.
*
* @param \SimpleXMLElement $xml
*/
public static function exportList($xml)
{
$nxml = $xml->addChild('themes');
$list = static::getList();
foreach ($list as $pt) {
$pt->export($nxml);
}
}
/**
* Install a theme.
*
* @param string $dir
* @param string $pThemeHandle
* @param int|null $pkgID
*
* @return \Concrete\Core\Page\Theme\Theme|null returns NULL if $dir does not exist
* @throws \Exception in case of errors.
*/
protected static function install($dir, $pThemeHandle, $pkgID)
{
$result = null;
if (is_dir($dir)) {
$pkg = null;
if ($pkgID) {
$pkg = \Concrete\Core\Package\Package::getByID($pkgID);
}
$db = Loader::db();
$cnt = $db->getOne('select count(pThemeID) from PageThemes where pThemeHandle = ?', [$pThemeHandle]);
if ($cnt > 0) {
throw new \Exception(static::E_THEME_INSTALLED);
}
$loc = Localization::getInstance();
$loc->pushActiveContext(Localization::CONTEXT_SYSTEM);
try {
$res = static::getThemeNameAndDescription(
$dir,
$pThemeHandle,
is_object($pkg) ? $pkg->getPackageHandle() : ''
);
} catch (\Exception $x) {
$loc->popActiveContext();
throw $x;
}
$loc->popActiveContext();
if (strlen($res->pError) === 0) {
$pThemeName = $res->pThemeName;
$pThemeDescription = $res->pThemeDescription;
$db->query(
'insert into PageThemes (pThemeHandle, pThemeName, pThemeDescription, pkgID) values (?, ?, ?, ?)',
[$pThemeHandle, $pThemeName, $pThemeDescription, $pkgID]
);
$env = Environment::get();
$env->clearOverrideCache();
$pt = static::getByID($db->Insert_ID());
$pt->updateThemeCustomClass();
$result = $pt;
} else {
throw new \Exception($res->pError);
}
}
return $result;
}
public function getDocumentationProvider(): ?DocumentationProviderInterface
{
return null;
}
public function getColorCollection(): ?ColorCollection
{
return null;
}
public function hasColorCollection()
{
return $this->getColorCollection() instanceof ColorCollection;
}
/**
* Checks to see whether the capability of theme documentation exists for this theme.
*
* @return bool
*/
public function supportsThemeDocumentation(): bool
{
$provider = $this->getDocumentationProvider();
if ($provider instanceof DocumentationProviderInterface) {
return true;
}
return false;
}
public function getThemeDocumentationParentPage(): ?Page
{
$parentPage = Page::getByPath(THEME_DOCUMENTATION_PAGE_PATH . '/' . $this->getThemeHandle());
if ($parentPage && !$parentPage->isError()) {
return $parentPage;
}
return null;
}
/**
* Checks to see if theme documentation has been installed
*
* @return bool
*/
public function hasThemeDocumentation(): bool
{
$documentationPage = $this->getThemeDocumentationParentPage();
return !is_null($documentationPage);
}
/**
* Returns an array of documentation pages for this theme
*
* @return array
*/
public function getThemeDocumentationPages(): array
{
$parentPage = Page::getByPath(THEME_DOCUMENTATION_PAGE_PATH . '/' . $this->getThemeHandle());
if ($parentPage && !$parentPage->isError()) {
$documentationList = new PageList();
$documentationList->setSiteTreeToAll();
$documentationList->includeSystemPages();
$documentationList->filterByPath($parentPage->getCollectionPath());
$documentationList->filterByPageTypeHandle([
THEME_DOCUMENTATION_PAGE_TYPE,
THEME_DOCUMENTATION_CATEGORY_PAGE_TYPE]
);
$documentationList->sortByDisplayOrder();
$themeDocumentationPages = $documentationList->getResults();
return $themeDocumentationPages;
}
return [];
}
/**
* (Re)Scan the theme folder to check if it contains the page_theme.php file: if so, marks the theme as having a controller.
*/
public function updateThemeCustomClass()
{
$env = Environment::get();
$db = Loader::db();
$r = $env->getRecord(
DIRNAME_THEMES.'/'.$this->pThemeHandle.'/'.FILENAME_THEMES_CLASS,
$this->getPackageHandle()
);
if ($r->exists()) {
$db->Execute('update PageThemes set pThemeHasCustomClass = 1 where pThemeID = ?', [$this->pThemeID]);
$this->pThemeHasCustomClass = true;
} else {
$db->Execute('update PageThemes set pThemeHasCustomClass = 0 where pThemeID = ?', [$this->pThemeID]);
$this->pThemeHasCustomClass = false;
}
}
/**
* Get the ID of the installed theme (if available).
*
* @return int|null
*/
public function getThemeID()
{
return $this->pThemeID;
}
/**
* Get the (English) name of the theme.
*
* @return string
*/
public function getThemeName()
{
return $this->pThemeName;
}
/**
* Get the localized name for this theme (escaped accordingly to $format).
*
* @param string $format Escape the result in html format (if $format is 'html'); if $format is 'text' or any other value, the display name won't be escaped
*
* @return string
*/
public function getThemeDisplayName($format = 'html')
{
$value = $this->getThemeName();
if (strlen($value)) {
$value = t($value);
} else {
$value = t('(No Name)');
}
switch ($format) {
case 'html':
return h($value);
case 'text':
default:
return $value;
}