From 755c9c5ad854245d902a247284a8f2afcda0e711 Mon Sep 17 00:00:00 2001 From: Ryan Cramer Date: Fri, 26 Feb 2021 10:46:33 -0500 Subject: [PATCH] Add processwire/processwire-requests#241 to make user info and roles available to PW's admin JS config (ProcessWire.config), plus some related improvements --- wire/core/AdminTheme.php | 87 ++++++++++++++++++++++++------- wire/core/AdminThemeFramework.php | 28 ++-------- 2 files changed, 72 insertions(+), 43 deletions(-) diff --git a/wire/core/AdminTheme.php b/wire/core/AdminTheme.php index 27acef24..80665e27 100644 --- a/wire/core/AdminTheme.php +++ b/wire/core/AdminTheme.php @@ -115,50 +115,101 @@ abstract class AdminTheme extends WireData implements Module { public function init() { self::$numAdminThemes++; - $info = $this->wire('modules')->getModuleInfo($this); + $info = $this->wire()->modules->getModuleInfo($this); $this->version = $info['version']; + $page = $this->wire()->page; // if module has been called when it shouldn't (per the 'autoload' conditional) // then module author probably forgot the right 'autoload' string, so this // serves as secondary stopgap to keep this module from loading when it shouldn't. - if(!$this->wire('page') || $this->wire('page')->template != 'admin') return; + if(!$page || $page->template->name !== 'admin') return; - if(self::$numAdminThemes > 1 && !$this->wire('fields')->get('admin_theme')) $this->install(); + if(self::$numAdminThemes > 1 && !$this->wire()->fields->get('admin_theme')) $this->install(); // if admin theme has already been set, then no need to continue if($this->wire('adminTheme')) return; - /** @var Config $config */ - $config = $this->wire('config'); - /** @var Session $session */ - $session = $this->wire('session'); - /** @var User $user */ - $user = $this->wire('user'); - /** @var string $adminTheme */ - $adminTheme = $user->admin_theme; + $config = $this->wire()->config; + $user = $this->wire()->user; + $adminTheme = $user->admin_theme; /** @var string $adminTheme */ + $isCurrent = false; if($adminTheme) { // there is user specified admin theme // check if this is the one that should be used - if($adminTheme == $this->className()) $this->setCurrent(); + if($adminTheme == $this->className()) { + $isCurrent = true; + $this->setCurrent(); + } - } else if($this->wire('config')->defaultAdminTheme == $this->className()) { + } else if($config->defaultAdminTheme == $this->className()) { // there is no user specified admin theme, so use this one + $isCurrent = true; $this->setCurrent(); } + + if($isCurrent) $this->initConfig(); + } + /** + * Initialize configuration properties and JS config for when this is current admin theme + * + * @since 3.0.173 + * + */ + protected function initConfig() { + + $config = $this->wire()->config; + $user = $this->wire()->user; + $session = $this->wire()->session; + $page = $this->wire()->page; + $urls = $config->urls; + // adjust $config adminThumbOptions[scale] for auto detect when requested - $o = $config->adminThumbOptions; + $o = $config->adminThumbOptions; if($o && isset($o['scale']) && $o['scale'] === 1) { - $o['scale'] = $session->get('hidpi') ? 0.5 : 1.0; + $o['scale'] = $session->get('hidpi') ? 0.5 : 1.0; $config->adminThumbOptions = $o; } - $config->js('modals', $config->modals); + $config->jsConfig('urls', array( + 'root' => $urls->root, + 'admin' => $urls->admin, + 'modules' => $urls->modules, + 'core' => $urls->core, + 'files' => $urls->files, + 'templates' => $urls->templates, + 'adminTemplates' => $urls->adminTemplates, + )); + $config->js('modals', true); // share at render time + $config->jsConfig('debug', $config->debug); + + if($user) { + $userInfo = array( + 'id' => $user->id, + 'name' => $user->name, + 'roles' => array(), + ); + $roles = $user->isLoggedin() ? $user->roles : null; + $guestRoleID = $config->guestUserRolePageID; + if($roles) foreach($roles as $role) { + if($role->id !== $guestRoleID) $userInfo['roles'][] = $role->name; + } + $config->jsConfig('user', $userInfo); + } + + if($page) { + $config->jsConfig('page', array( + 'id' => $page->id, + 'name' => $page->name, + 'process' => (string) $page->process, + )); + } + if($session->get('hidpi')) $this->addBodyClass('hidpi-device'); - if($session->get('touch')) $this->addBodyClass('touch-device'); - + if($session->get('touch')) $this->addBodyClass('touch-device'); + $this->addBodyClass($this->className()); } diff --git a/wire/core/AdminThemeFramework.php b/wire/core/AdminThemeFramework.php index 7006ccbc..ac4d80cb 100644 --- a/wire/core/AdminThemeFramework.php +++ b/wire/core/AdminThemeFramework.php @@ -293,32 +293,10 @@ abstract class AdminThemeFramework extends AdminTheme { * */ public function getHeadJS() { - - /** @var Config $config */ - $config = $this->wire('config'); - - /** @var Paths $urls */ - $urls = $config->urls; - - /** @var array $jsConfig */ - $jsConfig = $config->js(); - $jsConfig['debug'] = $config->debug; - - $jsConfig['urls'] = array( - 'root' => $urls->root, - 'admin' => $urls->admin, - 'modules' => $urls->modules, - 'core' => $urls->core, - 'files' => $urls->files, - 'templates' => $urls->templates, - 'adminTemplates' => $urls->adminTemplates, - ); - - $out = - "var ProcessWire = { config: " . wireEncodeJSON($jsConfig, true, $config->debug) . " }; " . + $config = $this->wire()->config; + return + "var ProcessWire = { config: " . wireEncodeJSON($config->js(), true, $config->debug) . " }; " . "var config = ProcessWire.config;\n"; // legacy support - - return $out; }