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