diff --git a/app/src/Controllers/MapElementController.php b/app/src/Controllers/MapElementController.php index 1b1eb0a..951cda4 100644 --- a/app/src/Controllers/MapElementController.php +++ b/app/src/Controllers/MapElementController.php @@ -12,8 +12,6 @@ class MapElementController extends ElementController { parent::init(); - DeferedRequirements::Auto(); - DeferedRequirements::loadCSS('https://api.tiles.mapbox.com/mapbox-gl-js/v0.48.0/mapbox-gl.css'); DeferedRequirements::loadJS('https://api.tiles.mapbox.com/mapbox-gl-js/v0.48.0/mapbox-gl.js'); diff --git a/app/src/Pages/PageController.php b/app/src/Pages/PageController.php index 232c743..03982d5 100644 --- a/app/src/Pages/PageController.php +++ b/app/src/Pages/PageController.php @@ -20,6 +20,7 @@ use SilverStripe\ORM\ArrayList; use DNADesign\Elemental\Models\ElementContent; use DNADesign\Elemental\Models\ElementalArea; use DNADesign\ElementalUserForms\Control\ElementFormController; +use Site\Templates\DeferedRequirements; class PageController extends ContentController { @@ -30,6 +31,13 @@ class PageController extends ContentController private $site_message; private $search_term; + public function init() + { + DeferedRequirements::Auto(); + + return parent::init(); + } + public function index(HTTPRequest $request) { $search = $request->getVar('q'); @@ -42,7 +50,7 @@ class PageController extends ContentController public function ElementalArea() { - if ($this->CurrentElement() || $this->getAction() !== 'index') { + if($this->CurrentElement() || $this->getAction() !== 'index') { return false; } @@ -51,10 +59,10 @@ class PageController extends ContentController public function CurrentElement() { - $contoller_curr = Controller::curr(); + $controller_curr = Controller::curr(); - if (is_a($contoller_curr, ElementFormController::class)) { - return $contoller_curr; + if(is_a($controller_curr, ElementFormController::class)) { + return $controller_curr; } return false; @@ -62,17 +70,25 @@ class PageController extends ContentController public function SearchForm() { + $config = $this->SiteConfig(); return Form::create( $this, __FUNCTION__, FieldList::create( TextField::create('q', 'Search ...') + ->setAttribute('placeholder', 'Search '.$config->getField('Title').' Website') ), FieldList::create( FormAction::create( 'doSearch', 'Find it!' ) + ->setUseButtonTag(true) + ->addExtraClass('btn-secondary') + ->setButtonContent( + '' + .'Search' + ) ), RequiredFields::create(['q']) )->setFormMethod('POST'); @@ -88,7 +104,7 @@ class PageController extends ContentController public function SearchResults() { $term = $this->search_term; - if (!$term) { + if(!$term) { return false; } @@ -110,7 +126,7 @@ class PageController extends ContentController foreach ($elements as $element) { $page = Page::get()->filter('ElementalAreaID', $element->getField('ParentID'))->first(); - if (!$page) { + if(!$page) { continue; } diff --git a/app/src/Templates/DeferedRequirements.php b/app/src/Templates/DeferedRequirements.php index 575f057..041dd89 100644 --- a/app/src/Templates/DeferedRequirements.php +++ b/app/src/Templates/DeferedRequirements.php @@ -14,7 +14,7 @@ class DeferedRequirements implements TemplateGlobalProvider { private static $css = []; private static $js = []; - private static $defered = false; + private static $deferred = false; private static $static_domain; private static $version; private static $nojquery = false; @@ -38,11 +38,11 @@ class DeferedRequirements implements TemplateGlobalProvider $config = Config::inst()->get(self::class); $projectName = WebpackTemplateProvider::projectName(); $mainTheme = WebpackTemplateProvider::mainTheme(); - $mainTheme = $mainTheme ? $mainTheme : $projectName; + $mainTheme = $mainTheme ?: $projectName; $dir = Path::join( Director::publicFolder(), - ManifestFileFinder::RESOURCES_DIR, + RESOURCES_DIR, $projectName, 'client', 'dist' @@ -59,14 +59,14 @@ class DeferedRequirements implements TemplateGlobalProvider // Main libs if (!$config['nojquery']) { - DeferedRequirements::loadJS('//ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js'); + self::loadJS('//ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js'); } // App libs if (!$config['nofontawesome']) { - DeferedRequirements::loadCSS('//use.fontawesome.com/releases/v5.4.0/css/all.css'); + self::loadCSS('//use.fontawesome.com/releases/v5.4.0/css/all.css'); } - DeferedRequirements::loadCSS($mainTheme.'.css'); - DeferedRequirements::loadJS($mainTheme.'.js'); + self::loadCSS($mainTheme.'.css'); + self::loadJS($mainTheme.'.js'); // Custom controller requirements $class = get_class(Controller::curr()); @@ -87,17 +87,17 @@ class DeferedRequirements implements TemplateGlobalProvider $themePath = Path::join($cssPath, $mainTheme.'_'.$class . '.css'); $projectPath = Path::join($cssPath, $projectName.'_'.$class . '.css'); if ($mainTheme && file_exists($themePath)) { - DeferedRequirements::loadCSS($mainTheme.'_'.$class . '.css'); + self::loadCSS($mainTheme.'_'.$class . '.css'); } elseif (file_exists($projectPath)) { - DeferedRequirements::loadCSS($projectName.'_'.$class . '.css'); + self::loadCSS($projectName.'_'.$class . '.css'); } $themePath = Path::join($jsPath, $mainTheme.'_'.$class . '.js'); $projectPath = Path::join($jsPath, $projectName.'_'.$class . '.js'); if ($mainTheme && file_exists($themePath)) { - DeferedRequirements::loadJS($mainTheme.'_'.$class . '.js'); + self::loadJS($mainTheme.'_'.$class . '.js'); } elseif (file_exists($projectPath)) { - DeferedRequirements::loadJS($projectName.'_'.$class . '.js'); + self::loadJS($projectName.'_'.$class . '.js'); } return self::forTemplate(); @@ -105,8 +105,8 @@ class DeferedRequirements implements TemplateGlobalProvider public static function loadCSS($css) { - $external = (mb_substr($css, 0, 2) === '//' || mb_substr($css, 0, 4) === 'http'); - if ($external || (self::$defered && !self::_webpackActive())) { + $external = (mb_strpos($css, '//') === 0 || mb_strpos($css, 'http') === 0); + if ($external || (self::$deferred && !self::_webpackActive())) { self::$css[] = $css; } else { WebpackTemplateProvider::loadCSS($css); @@ -118,7 +118,7 @@ class DeferedRequirements implements TemplateGlobalProvider /*$external = (mb_substr($js, 0, 2) === '//' || mb_substr($js, 0, 4) === 'http'); if ($external || (self::$defered && !self::_webpackActive())) {*/ // webpack supposed to load external JS - if (self::$defered && !self::_webpackActive()) { + if (self::$deferred && !self::_webpackActive()) { self::$js[] = $js; } else { WebpackTemplateProvider::loadJS($js); @@ -130,9 +130,9 @@ class DeferedRequirements implements TemplateGlobalProvider return class_exists('WebpackTemplateProvider') && WebpackTemplateProvider::isActive(); } - public static function setDefered($bool) + public static function setDeferred($bool) { - self::$defered = $bool; + self::$deferred = $bool; } public static function forTemplate() @@ -175,7 +175,7 @@ class DeferedRequirements implements TemplateGlobalProvider : ''; // no version defined $static_domain = $config['static_domain']; - $static_domain = $static_domain ? $static_domain : ''; + $static_domain = $static_domain ?: ''; return $url.$version; } diff --git a/app/src/Templates/WebpackTemplateProvider.php b/app/src/Templates/WebpackTemplateProvider.php index d21e6f2..09a3afe 100644 --- a/app/src/Templates/WebpackTemplateProvider.php +++ b/app/src/Templates/WebpackTemplateProvider.php @@ -34,7 +34,7 @@ class WebpackTemplateProvider implements TemplateGlobalProvider /** * @return array */ - public static function get_template_global_variables() + public static function get_template_global_variables(): array { return [ 'WebpackDevServer' => 'isActive', @@ -49,7 +49,7 @@ class WebpackTemplateProvider implements TemplateGlobalProvider * Load CSS file * @param $path */ - public static function loadCSS($path) + public static function loadCSS($path): void { if (self::isActive()) { return; @@ -62,12 +62,12 @@ class WebpackTemplateProvider implements TemplateGlobalProvider * Load JS file * @param $path */ - public static function loadJS($path) + public static function loadJS($path): void { Requirements::javascript(self::_getPath($path)); } - public static function projectName() + public static function projectName(): string { return Config::inst()->get(ModuleManifest::class, 'project'); } @@ -78,7 +78,7 @@ class WebpackTemplateProvider implements TemplateGlobalProvider return is_array($themes) && $themes[0] !== '$public' && $themes[0] !== '$default' ? $themes[0] : false; } - public static function resourcesURL($link = null) + public static function resourcesURL($link = null): string { return Controller::join_links(Director::baseURL(), '/resources/'.self::projectName().'/client/dist/img/', $link); } @@ -88,41 +88,49 @@ class WebpackTemplateProvider implements TemplateGlobalProvider * Checks if dev mode is enabled and if webpack server is online * @return bool */ - public static function isActive() + public static function isActive(): bool { - return Director::isDev() && !!@fsockopen( - Config::inst()->get(__CLASS__, 'HOSTNAME'), - Config::inst()->get(__CLASS__, 'PORT') + $cfg = self::config(); + return Director::isDev() && @fsockopen( + $cfg['HOSTNAME'], + $cfg['PORT'] ); } - protected static function _getPath($path) + protected static function _getPath($path): string { return self::isActive() && strpos($path, '//') === false ? self::_toDevServerPath($path) : self::_toPublicPath($path); } - protected static function _toDevServerPath($path) + protected static function _toDevServerPath($path): string { + $cfg = self::config(); return sprintf( '%s%s:%s/%s', Director::protocol(), - Config::inst()->get(__CLASS__, 'HOSTNAME'), - Config::inst()->get(__CLASS__, 'PORT'), + $cfg['HOSTNAME'], + $cfg['PORT'], basename($path) ); } - protected static function _toPublicPath($path) + protected static function _toPublicPath($path): string { + $cfg = self::config(); return strpos($path, '//') === false ? Controller::join_links( self::projectName(), - Config::inst()->get(__CLASS__, 'DIST'), + $cfg['DIST'], (strpos($path, '.css') ? 'css' : 'js'), $path ) : $path; } + + public static function config(): array + { + return Config::inst()->get(__CLASS__); + } }