1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sunnysideup\UnderConstruction\Extensions; |
4
|
|
|
|
5
|
|
|
use SilverStripe\AssetAdmin\Forms\UploadField; |
|
|
|
|
6
|
|
|
use SilverStripe\Assets\Image; |
7
|
|
|
use SilverStripe\Control\Controller; |
8
|
|
|
use SilverStripe\Control\Director; |
9
|
|
|
use SilverStripe\Core\Config\Config; |
10
|
|
|
use SilverStripe\Core\Injector\Injector; |
11
|
|
|
|
12
|
|
|
use SilverStripe\Forms\DropdownField; |
13
|
|
|
|
14
|
|
|
use SilverStripe\Forms\FieldList; |
15
|
|
|
use SilverStripe\Forms\NumericField; |
16
|
|
|
use SilverStripe\Forms\OptionsetField; |
17
|
|
|
use SilverStripe\Forms\ReadonlyField; |
18
|
|
|
use SilverStripe\Forms\TextField; |
19
|
|
|
|
20
|
|
|
|
21
|
|
|
use SilverStripe\ORM\DataExtension; |
22
|
|
|
use SilverStripe\ORM\FieldType\DBField; |
23
|
|
|
use Sunnysideup\UnderConstruction\Api\CalculatedValues; |
24
|
|
|
|
25
|
|
|
use Sunnysideup\UnderConstruction\Tasks\GoOffline; |
26
|
|
|
|
27
|
|
|
use Sunnysideup\UnderConstruction\Tasks\GoOnline; |
28
|
|
|
|
29
|
|
|
class SiteConfigExtension extends DataExtension |
30
|
|
|
{ |
31
|
|
|
protected static $loop_count = 0; |
32
|
|
|
|
33
|
|
|
private static $db = [ |
34
|
|
|
'UnderConstructionOnOff' => 'Enum("Online,Offline", "Online")', |
35
|
|
|
'UnderConstructionMinutesOffline' => 'Int', |
36
|
|
|
'UnderConstructionTitle' => 'Varchar', |
37
|
|
|
'UnderConstructionSubTitle' => 'Varchar', |
38
|
|
|
'UnderConstructionExcludedIps' => 'Varchar', |
39
|
|
|
'UnderConstructionOutcome' => 'Text', |
40
|
|
|
'UnderConstructionBackgroundColour' => 'Varchar(200)', |
41
|
|
|
'UnderConstructionForegroundColour' => 'Varchar(200)', |
42
|
|
|
]; |
43
|
|
|
|
44
|
|
|
private static $has_one = [ |
45
|
|
|
'UnderConstructionImage' => Image::class, |
46
|
|
|
]; |
47
|
|
|
|
48
|
|
|
private static $defaults = [ |
49
|
|
|
'UnderConstructionBackgroundColour' => '#000', |
50
|
|
|
'UnderConstructionForegroundColour' => '#fff', |
51
|
|
|
'UnderConstructionOnOff' => 'Online', |
52
|
|
|
'UnderConstructionMinutesOffline' => 20, |
53
|
|
|
'UnderConstructionTitle' => 'Sorry, we are offline for an upgrade.', |
54
|
|
|
'UnderConstructionSubTitle' => 'Please come back soon.', |
55
|
|
|
]; |
56
|
|
|
|
57
|
|
|
private static $owns = [ |
58
|
|
|
'UnderConstructionImage', |
59
|
|
|
]; |
60
|
|
|
|
61
|
|
|
public function UnderConstructionMinutesOfflineAlwaysInt() : int |
62
|
|
|
{ |
63
|
|
|
return intval($this->getOwner()->UnderConstructionMinutesOffline) ? $this->getOwner()->UnderConstructionMinutesOffline : 20; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
private $underConstructionCalculatedValues; |
67
|
|
|
|
68
|
|
|
public function updateCMSFields(FieldList $fields) |
69
|
|
|
{ |
70
|
|
|
$fields->removeByName('UnderConstructionOutcome'); |
71
|
|
|
$fields->addFieldsToTab( |
72
|
|
|
'Root.Offline', |
73
|
|
|
[ |
74
|
|
|
ReadonlyField::create( |
75
|
|
|
'UnderConstructionOutcome', |
76
|
|
|
'Status ...' |
77
|
|
|
) |
78
|
|
|
->setDescription('Was the last action successful? Are there any worries?'), |
79
|
|
|
OptionsetField::create( |
80
|
|
|
'UnderConstructionOnOff', |
81
|
|
|
'Is the site Online or Offline', |
82
|
|
|
['Online' => 'Online', 'Offline' => 'Offline'] |
83
|
|
|
) |
84
|
|
|
->setDescription('Make the site go Online / Offline. Please use with care!'), |
85
|
|
|
NumericField::create( |
86
|
|
|
'UnderConstructionMinutesOffline', |
87
|
|
|
'Minutes Offline' |
88
|
|
|
) |
89
|
|
|
->setDescription('Indication to the user for how long the site will be offline.'), |
90
|
|
|
TextField::create( |
91
|
|
|
'UnderConstructionTitle', |
92
|
|
|
'Page Title' |
93
|
|
|
), |
94
|
|
|
TextField::create( |
95
|
|
|
'UnderConstructionSubTitle', |
96
|
|
|
'Page Sub-Title' |
97
|
|
|
), |
98
|
|
|
TextField::create( |
99
|
|
|
'UnderConstructionExcludedIps', |
100
|
|
|
'Excluded IPs' |
101
|
|
|
) |
102
|
|
|
->setDescription('Separated by comma. Your IP address (' . Controller::curr()->getRequest()->getIp() . ') will be added automatically.'), |
103
|
|
|
UploadField::create( |
104
|
|
|
'UnderConstructionImage', |
105
|
|
|
'Background Image' |
106
|
|
|
) |
107
|
|
|
->setFolderName('offline/images') |
108
|
|
|
->setAllowedFileCategories('image') |
109
|
|
|
->setIsMultiUpload(false), |
110
|
|
|
DropdownField::create( |
111
|
|
|
'UnderConstructionForegroundColour', |
112
|
|
|
'Text Colour', |
113
|
|
|
Config::inst()->get(CalculatedValues::class, 'under_construction_fg_options') |
114
|
|
|
), |
115
|
|
|
DropdownField::create( |
116
|
|
|
'UnderConstructionBackgroundColour', |
117
|
|
|
'Background Colour', |
118
|
|
|
Config::inst()->get(CalculatedValues::class, 'under_construction_bg_options') |
119
|
|
|
), |
120
|
|
|
|
121
|
|
|
] |
122
|
|
|
); |
123
|
|
|
if ($this->owner->UnderConstructionOnOff === 'Offline') { |
124
|
|
|
$fields->replaceField( |
125
|
|
|
'UnderConstructionExcludedIps', |
126
|
|
|
ReadonlyField::create('UnderConstructionExcludedIps', 'Allowed IP Addresses') |
127
|
|
|
->setDescription('This can only be changed when the site is Online.') |
128
|
|
|
); |
129
|
|
|
} |
130
|
|
|
if ($this->getUnderConstructionCalculatedValues()->UnderConstructionIsReady()) { |
131
|
|
|
$publicUrl = $this->getUnderConstructionCalculatedValues()->UnderConstructionUrlPath(); |
132
|
|
|
$html = '<a href="' . $publicUrl . '" target="_offline">' . $publicUrl . '</a>'; |
133
|
|
|
} else { |
134
|
|
|
$html = 'No offline page has been created yet.'; |
135
|
|
|
} |
136
|
|
|
$fields->addFieldsToTab( |
137
|
|
|
'Root.Offline', |
138
|
|
|
[ |
139
|
|
|
ReadonlyField::create( |
140
|
|
|
'UnderConstructionPublicUrl', |
141
|
|
|
'Preview', |
142
|
|
|
DBField::create_field( |
143
|
|
|
'HTMLText', |
144
|
|
|
$html |
145
|
|
|
), |
146
|
|
|
), |
147
|
|
|
], |
148
|
|
|
'UnderConstructionMinutesOffline', |
149
|
|
|
); |
150
|
|
|
return $fields; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
public function getUnderConstructionCalculatedValues(): CalculatedValues |
154
|
|
|
{ |
155
|
|
|
if ($this->underConstructionCalculatedValues === null) { |
156
|
|
|
$this->underConstructionCalculatedValues = CalculatedValues::create($this->owner); |
157
|
|
|
} |
158
|
|
|
return $this->underConstructionCalculatedValues; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
public function onBeforeWrite() |
162
|
|
|
{ |
163
|
|
|
$currentController = @Controller::curr(); |
164
|
|
|
if ($currentController && $currentController->getRequest()) { |
165
|
|
|
$currentIp = $currentController->getRequest()->getIp(); |
166
|
|
|
$array = explode(',', $this->owner->UnderConstructionExcludedIps); |
|
|
|
|
167
|
|
|
$array = array_map('trim', $array); |
168
|
|
|
$array = array_filter($array); |
169
|
|
|
if ($currentIp) { |
170
|
|
|
if (! in_array($currentIp, $array, true)) { |
171
|
|
|
$array[] = $currentIp; |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
$this->owner->UnderConstructionExcludedIps = implode(', ', $array); |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
public function onAfterWrite() |
179
|
|
|
{ |
180
|
|
|
if (self::$loop_count < 2) { |
181
|
|
|
++self::$loop_count; |
182
|
|
|
$this->CreateFiles(); |
183
|
|
|
// 2 = only real changes. |
184
|
|
|
if ($this->owner->isChanged('UnderConstructionOnOff', 2)) { |
185
|
|
|
$task = null; |
186
|
|
|
if ($this->owner->UnderConstructionOnOff === 'Offline') { |
187
|
|
|
$task = Injector::inst()->get(GoOffline::class); |
188
|
|
|
} elseif ($this->owner->UnderConstructionOnOff === 'Online') { |
189
|
|
|
$task = Injector::inst()->get(GoOnline::class); |
190
|
|
|
} |
191
|
|
|
if ($task) { |
192
|
|
|
$this->owner->UnderConstructionOutcome = $task->run(null); |
193
|
|
|
$this->owner->write(); |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
register_shutdown_function([$this->owner, 'CreateFiles']); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
public function CreateFiles() |
201
|
|
|
{ |
202
|
|
|
$this->getUnderConstructionCalculatedValues()->CreateFiles(); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
public function requireDefaultRecords() |
206
|
|
|
{ |
207
|
|
|
if (! Director::is_cli()) { |
208
|
|
|
$this->getUnderConstructionCalculatedValues()->CreateDirAndTest(); |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
} |
212
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths