| 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 if the | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      * two conditions (display and auto-insert enabled) have been met. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      * @param  \SilverStripe\Control\HTTPRequest $request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      * @param  string $action | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      * @param  \SilverStripe\ORM\FieldType\DBHTMLText $result | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      *         from the original RequestHandler | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      * @return \SilverStripe\ORM\FieldType\DBHTMLText $result | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      *         with the EnvBar HTML inserted | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * @see SilverStripe\Control\RequestHandler::handleAction | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     public function afterCallActionHandler($request, $action, $result) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         // Check if display is turned on in the CMS | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         if (!SiteConfig::current_site_config()->EnvBarDisplay) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |             return $result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         // Check if automatic placement has been turned off in a _config yml | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         if (Config::inst()->get(__CLASS__, 'disable_auto_insert')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             return $result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         $html = $result->getValue(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         $envBar = $this->generateEnvBar()->getValue(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         $html = preg_replace( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |             '/(<body[^>]*>)/i', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |             '\\1' . $envBar, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |             $html | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         $result->setValue($html); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         return $result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |      * Check the environment type. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |      *         "dev" if the site is in dev mode, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |      *         "test" if the site is in test mode (e.g. QA or UAT), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |      *         "live" otherwise (e.g. Production) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     public function getEnvironment() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         if (Director::isDev()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |             return 'dev'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         } elseif (Director::isTest()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |             return 'test'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |             return 'live'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |      * Check the version of the page being viewed. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |      *         "published" if it is the current live version in this environment, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |      *         "draft" if it is a modified or unpublished version, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |      *         "not staged" otherwise | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |     public function getPageStatus() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |         if (Versioned::get_stage() === 'Live') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |             return 'published'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |         } elseif (Versioned::get_stage() === 'Stage') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |             return 'draft'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |             return 'not staged'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |      * Check whether CurrentUser has access to edit pages. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |      * @return boolean | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |      *         "true" if the user can edit pages, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |      *         "false" otherwise | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |     public function getCanAccess() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |         $member = Security::getCurrentUser(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |         if (Permission::checkMember($member, 'CMS_ACCESS_CMSMain')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |             return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |      * Get EnvBar HTML as a template variable. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |      * @return DBHTMLText | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |     public function getEnvBar() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |         if ( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |             Config::inst()->get(__CLASS__, 'disable_auto_insert') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |             && SiteConfig::current_site_config()->EnvBarDisplay | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |         ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |             return $this->generateEnvBar(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |      * Generate the HTML to inject using the EnvBar.ss template. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |      * @return \SilverStripe\ORM\FieldType\DBHTMLText | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |     private function generateEnvBar() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |         return $this->owner->renderWith('EnvBar'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 166 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 167 |  |  |  | 
            
                        
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.