]> git.mxchange.org Git - friendica.git/commitdiff
Add new methods to src
authorHypolite Petovan <mrpetovan@gmail.com>
Sat, 28 Apr 2018 22:30:13 +0000 (18:30 -0400)
committerHypolite Petovan <mrpetovan@gmail.com>
Sat, 28 Apr 2018 22:30:13 +0000 (18:30 -0400)
- Add App::getCurrentTheme()
- Add App::getCurrentThemeStylesheetPath()
- Add Theme::getStylesheetPath()

src/App.php
src/Core/Theme.php

index d76351b97ac6a9db60470c9b7d999836ece1f799..815a02f281896efab1c1ce0b6f2e288115c6f521 100644 (file)
@@ -1067,4 +1067,85 @@ class App
 
                return $sender_email;
        }
+
+       /**
+        * Returns the current theme name.
+        *
+        * @return string
+        */
+       public function getCurrentTheme()
+       {
+               if (!$this->current_theme) {
+                       $this->computeCurrentTheme();
+               }
+
+               return $this->current_theme;
+       }
+
+       /**
+        * Computes the current theme name based on the node settings, the user settings and the device type
+        *
+        * @throws Exception
+        */
+       private function computeCurrentTheme()
+       {
+               $system_theme = Config::get('system', 'theme');
+               if (!$system_theme) {
+                       throw new Exception(L10n::t('No system theme config value set.'));
+               }
+
+               // Sane default
+               $this->current_theme = $system_theme;
+
+               $allowed_themes = explode(',', Config::get('system', 'allowed_themes', $system_theme));
+
+               $page_theme = null;
+               // Find the theme that belongs to the user whose stuff we are looking at
+               if ($this->profile_uid && ($this->profile_uid != local_user())) {
+                       // Allow folks to override user themes and always use their own on their own site.
+                       // This works only if the user is on the same server
+                       $user = dba::selectFirst('user', ['theme'], ['uid' => $this->profile_uid]);
+                       if (DBM::is_result($user) && !PConfig::get(local_user(), 'system', 'always_my_theme')) {
+                               $page_theme = $user['theme'];
+                       }
+               }
+
+               $user_theme = defaults($_SESSION, 'theme', $system_theme);
+               // Specific mobile theme override
+               if (($this->is_mobile || $this->is_tablet) && defaults($_SESSION, 'show-mobile', true)) {
+                       $system_mobile_theme = Config::get('system', 'mobile-theme');
+                       $user_mobile_theme = defaults($_SESSION, 'mobile-theme', $system_mobile_theme);
+
+                       // --- means same mobile theme as desktop
+                       if (!empty($user_mobile_theme) && $user_mobile_theme !== '---') {
+                               $user_theme = $user_mobile_theme;
+                       }
+               }
+
+               if ($page_theme) {
+                       $theme_name = $page_theme;
+               } else {
+                       $theme_name = $user_theme;
+               }
+
+               if ($theme_name
+                       && in_array($theme_name, $allowed_themes)
+                       && (file_exists('view/theme/' . $theme_name . '/style.css')
+                       || file_exists('view/theme/' . $theme_name . '/style.php'))
+               ) {
+                       $this->current_theme = $theme_name;
+               }
+       }
+
+       /**
+        * @brief Return full URL to theme which is currently in effect.
+        *
+        * Provide a sane default if nothing is chosen or the specified theme does not exist.
+        *
+        * @return string
+        */
+       public function getCurrentThemeStylesheetPath()
+       {
+               return Core\Theme::getStylesheetPath($this->getCurrentTheme());
+       }
 }
index 16eb7939c5224fe9f9585dee9c09fb3255b29839..fe38d09ede3ea0bbd06a6c4f518ce75c6d99b348 100644 (file)
@@ -180,4 +180,25 @@ class Theme
                }
                return '';
        }
+
+       /**
+        * @brief Return relative path to theme stylesheet file
+        *
+        * Provide a sane default if nothing is chosen or the specified theme does not exist.
+        *
+        * @param string $theme Theme name
+        *
+        * @return string
+        */
+       public static function getStylesheetPath($theme)
+       {
+               $a = get_app();
+
+               $opts = (($a->profile_uid) ? '?f=&puid=' . $a->profile_uid : '');
+               if (file_exists('view/theme/' . $theme . '/style.php')) {
+                       return 'view/theme/' . $theme . '/style.pcss' . $opts;
+               }
+
+               return 'view/theme/' . $theme . '/style.css';
+       }
 }