1 | <?php |
||
2 | |||
3 | namespace Sunnysideup\SunnysideupThemeBackend\Extensions; |
||
4 | |||
5 | use SilverStripe\CMS\Controllers\ContentController; |
||
6 | use SilverStripe\CMS\Model\SiteTree; |
||
7 | use SilverStripe\Control\Director; |
||
8 | use SilverStripe\Control\Controller; |
||
9 | use SilverStripe\UserForms\Model\UserDefinedForm; |
||
10 | use SilverStripe\Core\Extension; |
||
11 | use SilverStripe\Core\Config\Config; |
||
12 | use SilverStripe\Core\Manifest\ResourceURLGenerator; |
||
13 | use SilverStripe\Core\Injector\Injector; |
||
14 | |||
15 | class PageControllerExtension extends Extension |
||
16 | { |
||
17 | |||
18 | protected static $_random_images_assigned_to_pages = null; |
||
19 | |||
20 | public function IsHomePage() |
||
21 | { |
||
22 | return $this->owner->URLSegment === 'home'; |
||
23 | } |
||
24 | |||
25 | public function Siblings() |
||
26 | { |
||
27 | if ($this->owner->ParentID) { |
||
28 | return SiteTree::get() |
||
29 | ->filter(['ShowInMenus' => 1, 'ParentID' => $this->owner->ParentID]) |
||
30 | ->exclude(['ID' => $this->owner->ID]); |
||
31 | } |
||
32 | } |
||
33 | |||
34 | public function MenuChildren() |
||
35 | { |
||
36 | return $this->owner->Children()->filter('ShowInMenus', 1); |
||
37 | } |
||
38 | |||
39 | |||
40 | |||
41 | public function RandomImage() : string |
||
42 | { |
||
43 | $imageName = ''; |
||
44 | if($this->owner->RandomImage && in_array($this->owner->RandomImage, $this->owner->getRandomImages(), true)) { |
||
45 | $imageName = $this->owner->RandomImage; |
||
46 | } |
||
47 | else { |
||
48 | $array = $this->owner->getRandomImagesAssignedToPages(); |
||
49 | if (isset($_GET['testimg'])) { |
||
50 | $pos = intval($_GET['testimg']); |
||
51 | } else { |
||
52 | $pos = $this->owner->ID; |
||
53 | } |
||
54 | if(! isset($array[$pos]) && !empty($array)) { |
||
55 | $pos = array_rand($array); |
||
56 | } |
||
57 | if( isset($array[$pos])) { |
||
58 | $imageName = $array[$pos]; |
||
59 | } |
||
60 | } |
||
61 | if($imageName) { |
||
62 | return Controller::join_links($this->owner->getRandomImagesFrontEndFolder() , $imageName); |
||
63 | } else { |
||
64 | return ''; |
||
65 | } |
||
66 | } |
||
67 | |||
68 | public function getRandomImagesAssignedToPages() : array |
||
69 | { |
||
70 | if (self::$_random_images_assigned_to_pages === null) { |
||
71 | $files = $this->owner->getRequest()->getSession()->get('randomImages'); |
||
72 | if ($files) { |
||
73 | $files = unserialize($files); |
||
74 | } |
||
75 | if (is_array($files) && count($files)) { |
||
76 | //do nothing |
||
77 | } else { |
||
78 | $files = $this->owner->getRandomImages(); |
||
79 | shuffle($files); |
||
80 | $files = $this->owner->addSiteTreeIdsToFiles($files); |
||
81 | $this->owner->getRequest()->getSession()->set('randomImages', serialize($files)); |
||
82 | } |
||
83 | self::$_random_images_assigned_to_pages = $files; |
||
84 | } |
||
85 | return self::$_random_images_assigned_to_pages; |
||
86 | } |
||
87 | |||
88 | |||
89 | public function canCachePage(): bool |
||
90 | { |
||
91 | if ($this->owner->dataRecord instanceof UserDefinedForm) { |
||
92 | return false; |
||
93 | } |
||
94 | return true; |
||
95 | } |
||
96 | |||
97 | public function onAfterInit() |
||
98 | { |
||
99 | if (! empty($_POST['Website'])) { |
||
100 | die('Sorry, but this looks like spam. Please go back the previous page and try again.'); |
||
0 ignored issues
–
show
|
|||
101 | } |
||
102 | if($this->owner->getRequest()->getVar('flush')) { |
||
103 | $this->owner->getRequest()->getSession()->clear('randomImages'); |
||
104 | } |
||
105 | // $this->owner->addBasicMetatagRequirements(); |
||
106 | $this->owner->InsertGoogleAnalyticsAsHeadTag(); |
||
107 | } |
||
108 | |||
109 | |||
110 | public function addSiteTreeIdsToFiles(array $files) : array |
||
111 | { |
||
112 | $newArray = []; |
||
113 | if(count($files)) { |
||
114 | $originalFiles = $files; |
||
115 | $pageIds = SiteTree::get()->column('ID'); |
||
116 | if(count($pageIds)) { |
||
117 | foreach($pageIds as $id) { |
||
118 | if(empty($files)) { |
||
119 | $files = $originalFiles; |
||
120 | } |
||
121 | $file = array_pop($files); |
||
122 | $newArray[$id] = $file; |
||
123 | } |
||
124 | } |
||
125 | } |
||
126 | return $newArray; |
||
127 | } |
||
128 | |||
129 | public function getRandomImages() :array |
||
130 | { |
||
131 | if($this->owner && $this->owner->dataRecord && $this->owner->dataRecord->hasMethod('getRandomImages')) { |
||
132 | return $this->owner->dataRecord->getRandomImages(); |
||
133 | } |
||
134 | return []; |
||
135 | } |
||
136 | |||
137 | public function getRandomImagesFrontEndFolder() :string |
||
138 | { |
||
139 | if($this->owner && $this->owner->dataRecord && $this->owner->dataRecord->hasMethod('getRandomImagesFrontEndFolder')) { |
||
140 | return $this->owner->dataRecord->getRandomImagesFrontEndFolder(); |
||
141 | } |
||
142 | return ''; |
||
143 | } |
||
144 | } |
||
145 |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.