2016-09-02 10:42:05 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Helpers;
|
|
|
|
|
|
|
|
use App\Configuration;
|
2016-09-21 12:10:37 +01:00
|
|
|
use App\Facade\UserConfig;
|
2016-09-02 10:42:05 +01:00
|
|
|
|
|
|
|
class ThemeHelper
|
|
|
|
{
|
|
|
|
const DEFAULT_THEME = 'base';
|
|
|
|
|
|
|
|
public function current()
|
|
|
|
{
|
|
|
|
return $this->getThemeName();
|
|
|
|
}
|
|
|
|
|
2017-04-14 10:23:47 +01:00
|
|
|
public function getPermission($allPermissions, $section, $description)
|
|
|
|
{
|
|
|
|
foreach ($allPermissions as $permission)
|
|
|
|
{
|
|
|
|
if (strtolower($permission->section) == strtolower($section) && strtolower($permission->description) == strtolower($description))
|
|
|
|
{
|
|
|
|
return $permission;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-09-02 12:36:34 +01:00
|
|
|
public function hasStylesheet()
|
|
|
|
{
|
|
|
|
$cssFilename = sprintf('%s/%s/theme.css', $this->getThemePublicPath(), $this->getThemeName());
|
|
|
|
return file_exists($cssFilename);
|
|
|
|
}
|
|
|
|
|
2017-03-22 16:55:39 +00:00
|
|
|
public static function gravatarUrl($emailAddress, $size = 48, $default = 'identicon')
|
|
|
|
{
|
|
|
|
$hash = md5(strtolower(trim($emailAddress)));
|
|
|
|
return sprintf('https://www.gravatar.com/avatar/%s?s=%d&d=%s', $hash, $size, $default);
|
|
|
|
}
|
|
|
|
|
2016-09-02 10:42:05 +01:00
|
|
|
public function info()
|
|
|
|
{
|
|
|
|
$themeName = $this->getThemeName();
|
2016-09-02 12:36:34 +01:00
|
|
|
$jsonFile = sprintf('%s/%s/theme.json', $this->getThemePrivatePath(), $this->sanitiseThemeName($themeName));
|
2016-09-02 10:42:05 +01:00
|
|
|
if (file_exists($jsonFile))
|
|
|
|
{
|
|
|
|
return json_decode(file_get_contents($jsonFile), true);
|
|
|
|
}
|
|
|
|
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function render($viewPath, array $viewData = array())
|
2016-09-06 12:23:14 +01:00
|
|
|
{
|
|
|
|
return view($this->viewName($viewPath), $viewData);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function viewName($viewPath)
|
2016-09-02 10:42:05 +01:00
|
|
|
{
|
|
|
|
$themeName = $this->getThemeName();
|
|
|
|
|
|
|
|
// First see if the current theme has the specified file
|
|
|
|
$requestedViewFilename = $this->getRealFilePath($themeName, $viewPath);
|
|
|
|
|
|
|
|
if (!file_exists($requestedViewFilename))
|
|
|
|
{
|
|
|
|
// If it doesn't, revert to the base theme
|
|
|
|
// TODO allow themes to specify another theme as the parent, and traverse up the theme tree to find the
|
|
|
|
// relevant file - this allows for sub-themes
|
|
|
|
$themeName = ThemeHelper::DEFAULT_THEME;
|
|
|
|
}
|
|
|
|
|
2016-09-06 12:23:14 +01:00
|
|
|
return sprintf('themes.%s.%s', $themeName, $viewPath);
|
2016-09-02 10:42:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private function getRealFilePath($themeName, $viewPath)
|
|
|
|
{
|
|
|
|
return sprintf(
|
|
|
|
'%s/%s/%s.blade.php',
|
2016-09-02 12:36:34 +01:00
|
|
|
$this->getThemePrivatePath(),
|
2016-09-02 10:42:05 +01:00
|
|
|
$themeName,
|
|
|
|
str_replace('.', DIRECTORY_SEPARATOR, $viewPath)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-09-02 12:36:34 +01:00
|
|
|
private function getThemePrivatePath()
|
2016-09-02 10:42:05 +01:00
|
|
|
{
|
|
|
|
return sprintf('%s/resources/views/themes', dirname(dirname(__DIR__)));
|
|
|
|
}
|
|
|
|
|
2016-09-02 12:36:34 +01:00
|
|
|
private function getThemePublicPath()
|
|
|
|
{
|
|
|
|
return sprintf('%s/public/themes', dirname(dirname(__DIR__)));
|
|
|
|
}
|
|
|
|
|
2016-09-02 10:42:05 +01:00
|
|
|
private function getThemeName()
|
|
|
|
{
|
2016-09-21 12:10:37 +01:00
|
|
|
return $this->sanitiseThemeName(UserConfig::get('theme'));
|
2016-09-02 10:42:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private function sanitiseThemeName($themeName)
|
|
|
|
{
|
|
|
|
// Ensure nasty people can't try and trick us into traversing the directory tree
|
|
|
|
return preg_replace('/[\\\.\/]/', '', strtolower($themeName));
|
|
|
|
}
|
|
|
|
}
|