1 | <?php |
||
2 | |||
3 | namespace Sunnysideup\SunnysideupThemeBackend\Extensions; |
||
4 | |||
5 | use SilverStripe\Forms\FieldList; |
||
6 | use SilverStripe\Forms\DropdownField; |
||
7 | use SilverStripe\Forms\CheckboxField; |
||
8 | use SilverStripe\CMS\Model\SiteTree; |
||
9 | use SilverStripe\Forms\TextField; |
||
10 | use SilverStripe\CMS\Model\SiteTreeExtension; |
||
11 | |||
12 | use Psr\SimpleCache\CacheInterface; |
||
13 | use SilverStripe\Core\Injector\Injector; |
||
14 | use SilverStripe\Core\Manifest\ResourceURLGenerator; |
||
15 | use SilverStripe\Core\Config\Config; |
||
16 | use SilverStripe\Core\Flushable; |
||
17 | use SilverStripe\Control\Director; |
||
18 | use SilverStripe\Control\Controller; |
||
19 | use Sminnee\VerboseFields\VerboseOptionsetField; |
||
20 | |||
21 | |||
22 | class PageExtension extends SiteTreeExtension implements Flushable |
||
23 | { |
||
24 | |||
25 | public static function flush() |
||
26 | { |
||
27 | $cache = Injector::inst()->get(CacheInterface::class . '.randomImageCache'); |
||
28 | $cache->clear(); |
||
29 | } |
||
30 | |||
31 | private static $image_dir = 'vendor/sunnysideup/sunnysideup-theme-backend/images'; |
||
32 | |||
33 | protected static $_random_images = null; |
||
34 | |||
35 | private static $db = [ |
||
36 | 'Quote' => 'Varchar', |
||
37 | 'VimeoVideoID' => 'Int', |
||
38 | 'RandomImage' => 'Varchar(255)', |
||
39 | 'DefaultTheme' => 'Enum("sun,moon,rocket", "sun")', |
||
40 | 'ShadowOverLogo' => 'Enum("none,light,dark", "none")', |
||
41 | 'TitleColour' => 'Enum("natural,yellow,blue", "natural")', |
||
42 | ]; |
||
43 | |||
44 | public function updateCMSFields(FieldList $fields) |
||
45 | { |
||
46 | $owner = $this->owner; |
||
0 ignored issues
–
show
Unused Code
introduced
by
![]() |
|||
47 | |||
48 | $fields->addFieldsToTab( |
||
49 | 'Root.Theme', |
||
50 | [ |
||
51 | DropdownField::create('DefaultTheme', 'Default Theme', $this->getOwner()->dbObject('DefaultTheme')->enumValues()) |
||
52 | ->setEmptyString('--- no specific theme ---'), |
||
53 | DropdownField::create('ShadowOverLogo', 'Shadow over logo', $this->getOwner()->dbObject('ShadowOverLogo')->enumValues()), |
||
54 | DropdownField::create('TitleColour', 'Title Colour', $this->getOwner()->dbObject('TitleColour')->enumValues()), |
||
55 | ] |
||
56 | ); |
||
57 | |||
58 | $fields->addFieldsToTab( |
||
59 | 'Root.Quote', |
||
60 | [ |
||
61 | CheckboxField::create('TypeModeForQuote', 'Type it out'), |
||
62 | TextField::create('Quote', 'Quote'), |
||
63 | ] |
||
64 | ); |
||
65 | |||
66 | $fields->addFieldToTab( |
||
67 | 'Root.Video', |
||
68 | TextField::create('VimeoVideoID', 'Vimeo Video ID') |
||
69 | ); |
||
70 | |||
71 | $fields->addFieldToTab( |
||
72 | 'Root.RandomImage', |
||
73 | $this->getSelectRandomImageField() |
||
74 | ); |
||
75 | |||
76 | return $fields; |
||
77 | } |
||
78 | |||
79 | protected function getSelectRandomImageField() : VerboseOptionsetField |
||
80 | { |
||
81 | |||
82 | $descriptions = []; |
||
83 | $list = $this->getRandomImages(); |
||
84 | $source = array_combine($list, $list); |
||
85 | foreach($list as $image) { |
||
86 | $descriptions[$image] = '<img src="'.$this->getRandomImagesFrontEndFolder() .'/' . $image.'" style="max-width: 300px; max-height: 300px" />'; |
||
87 | } |
||
88 | |||
89 | return (new VerboseOptionsetField ('RandomImage', 'Random Image (optional)')) |
||
90 | ->setSource($source) |
||
91 | ->setSourceDescriptions($descriptions); |
||
92 | } |
||
93 | |||
94 | |||
95 | public function getRandomImages() :array |
||
96 | { |
||
97 | if (self::$_random_images === null) { |
||
98 | $cache = Injector::inst()->get(CacheInterface::class . '.randomImageCache'); |
||
99 | $files = $cache->get('images'); |
||
100 | if ($files) { |
||
101 | $files = explode(',', $files); |
||
102 | } |
||
103 | if (is_array($files) && count($files)) { |
||
104 | //do nothing |
||
105 | } else { |
||
106 | $files = scandir( $this->getRandomImagesFolderAbsolute()) ?? []; |
||
107 | foreach ($files as $key => $file) { |
||
108 | $ext = pathinfo($file, PATHINFO_EXTENSION); |
||
109 | if ($ext !== 'jpg') { |
||
110 | unset($files[$key]); |
||
111 | } |
||
112 | } |
||
113 | $cache->set('images', implode(',', $files)); |
||
114 | } |
||
115 | self::$_random_images = $files; |
||
116 | } |
||
117 | return self::$_random_images; |
||
118 | } |
||
119 | |||
120 | public function getRandomImagesFolderAbsolute(): string |
||
121 | { |
||
122 | return Controller::join_links( Director::baseFolder(), $this->getRandomImageFolder()); |
||
123 | } |
||
124 | |||
125 | public function getRandomImagesFrontEndFolder() : string |
||
126 | { |
||
127 | return Injector::inst()->get(ResourceURLGenerator::class)->urlForResource($this->getRandomImageFolder()); |
||
128 | } |
||
129 | |||
130 | public function getRandomImageFolder(): string |
||
131 | { |
||
132 | return Config::inst()->get(PageExtension::class, 'image_dir'); |
||
133 | } |
||
134 | |||
135 | } |
||
136 |