|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Signify\EnvBar\Extensions; |
|
4
|
|
|
|
|
5
|
|
|
use SilverStripe\Control\Director; |
|
6
|
|
|
use SilverStripe\Core\Config\Config; |
|
7
|
|
|
use SilverStripe\ORM\DataExtension; |
|
8
|
|
|
use SilverStripe\ORM\FieldType\DBHTMLText; |
|
9
|
|
|
use SilverStripe\Security\Permission; |
|
10
|
|
|
use SilverStripe\Security\Security; |
|
11
|
|
|
use SilverStripe\SiteConfig\SiteConfig; |
|
12
|
|
|
use SilverStripe\Versioned\Versioned; |
|
13
|
|
|
use SilverStripe\View\Requirements; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* EnvBarExtension |
|
17
|
|
|
* |
|
18
|
|
|
* This class extends the @see SilverStripe\CMS\Controllers\ContentController |
|
19
|
|
|
* class. |
|
20
|
|
|
* |
|
21
|
|
|
* It contains methods to check the environment type, page status (versioning) |
|
22
|
|
|
* and user access (member permissions) and returns values to populate the |
|
23
|
|
|
* EnvBar. |
|
24
|
|
|
* |
|
25
|
|
|
* It also contains methods to modify the @see SilverStripe\Control\HTTPResponse |
|
26
|
|
|
* before and after the @see SilverStripe\Control\RequestHandler |
|
27
|
|
|
* inserting the CSS and HTML required to produce the EnvBar. |
|
28
|
|
|
* |
|
29
|
|
|
* @package Signify\EnvBar\Extensions |
|
30
|
|
|
* @author Lani Field <[email protected]> |
|
31
|
|
|
* @version 1.0.1 |
|
32
|
|
|
* |
|
33
|
|
|
* @param \SilverStripe\Control\HTTPRequest $request |
|
34
|
|
|
* @return \SilverStripe\Control\HTTPResponse $result |
|
35
|
|
|
* with the EnvBar CSS and HTML inserted. |
|
36
|
|
|
*/ |
|
37
|
|
|
class EnvBarExtension extends DataExtension |
|
38
|
|
|
{ |
|
39
|
|
|
/** |
|
40
|
|
|
* Load the CSS requirement. |
|
41
|
|
|
* |
|
42
|
|
|
* @param \SilverStripe\Control\HTTPRequest $request |
|
43
|
|
|
* @param string $action |
|
44
|
|
|
* @return void |
|
45
|
|
|
* @see SilverStripe\Control\RequestHandler::handleAction |
|
46
|
|
|
*/ |
|
47
|
|
|
public function beforeCallActionHandler($request, $action) |
|
|
|
|
|
|
48
|
|
|
{ |
|
49
|
|
|
Requirements::css('signify-nz/silverstripe-environmentindicator:client/dist/css/envbar.css'); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Rewrite the HTML of the viewed page to insert the EnvBar. |
|
54
|
|
|
* |
|
55
|
|
|
* @param \SilverStripe\Control\HTTPRequest $request |
|
56
|
|
|
* @param string $action |
|
57
|
|
|
* @param \SilverStripe\ORM\FieldType\DBHTMLText $result |
|
58
|
|
|
* from the original RequestHandler |
|
59
|
|
|
* @return \SilverStripe\ORM\FieldType\DBHTMLText $result |
|
60
|
|
|
* with the EnvBar HTML inserted |
|
61
|
|
|
* @see SilverStripe\Control\RequestHandler::handleAction |
|
62
|
|
|
*/ |
|
63
|
|
|
public function afterCallActionHandler($request, $action, $result) |
|
|
|
|
|
|
64
|
|
|
{ |
|
65
|
|
|
if (!($result instanceof DBHTMLText)) { |
|
|
|
|
|
|
66
|
|
|
return $result; |
|
67
|
|
|
} |
|
68
|
|
|
if (SiteConfig::current_site_config()->EnvBarOverride) { |
|
69
|
|
|
return $result; |
|
70
|
|
|
} |
|
71
|
|
|
if (Config::inst()->get(__CLASS__, 'disable_auto_insert')) { |
|
72
|
|
|
return $result; |
|
73
|
|
|
} |
|
74
|
|
|
$html = $result->getValue(); |
|
75
|
|
|
$envBar = $this->generateEnvBar()->getValue(); |
|
76
|
|
|
$html = preg_replace( |
|
77
|
|
|
'/(<body[^>]*>)/i', |
|
78
|
|
|
'\\1' . $envBar, |
|
79
|
|
|
$html |
|
80
|
|
|
); |
|
81
|
|
|
$result->setValue($html); |
|
82
|
|
|
return $result; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Check the environment type. |
|
87
|
|
|
* |
|
88
|
|
|
* @return string |
|
89
|
|
|
* "dev" if the site is in dev mode, |
|
90
|
|
|
* "test" if the site is in test mode (e.g. QA or UAT), |
|
91
|
|
|
* "live" otherwise (e.g. Production) |
|
92
|
|
|
*/ |
|
93
|
|
|
public function getEnvironment() |
|
94
|
|
|
{ |
|
95
|
|
|
if (Director::isDev()) { |
|
96
|
|
|
return 'dev'; |
|
97
|
|
|
} elseif (Director::isTest()) { |
|
98
|
|
|
return 'test'; |
|
99
|
|
|
} else { |
|
100
|
|
|
return 'live'; |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Check the version of the page being viewed. |
|
106
|
|
|
* |
|
107
|
|
|
* @return string |
|
108
|
|
|
* "published" if it is the current live version in this environment, |
|
109
|
|
|
* "draft" if it is a modified or unpublished version, |
|
110
|
|
|
* "not staged" otherwise |
|
111
|
|
|
*/ |
|
112
|
|
|
public function getPageStatus() |
|
113
|
|
|
{ |
|
114
|
|
|
if (Versioned::get_stage() === 'Live') { |
|
115
|
|
|
return 'published'; |
|
116
|
|
|
} elseif (Versioned::get_stage() === 'Stage') { |
|
117
|
|
|
return 'draft'; |
|
118
|
|
|
} else { |
|
119
|
|
|
return 'not staged'; |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Check whether CurrentUser has access to edit pages. |
|
125
|
|
|
* |
|
126
|
|
|
* @return boolean |
|
127
|
|
|
* "true" if the user can edit pages, |
|
128
|
|
|
* "false" otherwise |
|
129
|
|
|
*/ |
|
130
|
|
|
public function getCanAccess() |
|
131
|
|
|
{ |
|
132
|
|
|
$member = Security::getCurrentUser(); |
|
133
|
|
|
if (Permission::checkMember($member, 'CMS_ACCESS_CMSMain')) { |
|
134
|
|
|
return true; |
|
135
|
|
|
} else { |
|
136
|
|
|
return false; |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Get EnvBar html as a template variable. |
|
142
|
|
|
* |
|
143
|
|
|
* @return DBHTMLText |
|
144
|
|
|
*/ |
|
145
|
|
|
public function getEnvBar() |
|
146
|
|
|
{ |
|
147
|
|
|
if ( |
|
148
|
|
|
Config::inst()->get(__CLASS__, 'disable_auto_insert') |
|
149
|
|
|
&& !(SiteConfig::current_site_config()->EnvBarOverride) |
|
150
|
|
|
) { |
|
151
|
|
|
return $this->generateEnvBar(); |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* Generate the HTML to inject using the EnvBar.ss template. |
|
157
|
|
|
* |
|
158
|
|
|
* @return \SilverStripe\ORM\FieldType\DBHTMLText |
|
159
|
|
|
*/ |
|
160
|
|
|
private function generateEnvBar() |
|
161
|
|
|
{ |
|
162
|
|
|
return $this->owner->renderWith('EnvBar'); |
|
163
|
|
|
} |
|
164
|
|
|
} |
|
165
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.