3
* Xibo - Digital Signage - http://www.xibo.org.uk
4
* Copyright (C) 2006-2013 Daniel Garner
6
* This file is part of Xibo.
8
* Xibo is free software: you can redistribute it and/or modify
9
* it under the terms of the GNU Affero General Public License as published by
10
* the Free Software Foundation, either version 3 of the License, or
13
* Xibo is distributed in the hope that it will be useful,
14
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
* GNU Affero General Public License for more details.
18
* You should have received a copy of the GNU Affero General Public License
19
* along with Xibo. If not, see <http://www.gnu.org/licenses/>.
21
namespace Xibo\Helper;
31
use Xibo\Factory\MenuFactory;
33
defined('XIBO') or die("Sorry, you are not allowed to directly access this page.<br /> Please press the back button in your browser.");
37
private static $instance = null;
42
private $pageName = '';
44
private $config = null;
46
public function __construct($theme = NULL)
48
// Store some things for the Theme engine to use
49
$this->help = new Help();
50
$this->dateManager = new Date();
52
// What is the currently selected theme?
53
$globalTheme = ($theme == NULL) ? Config::GetSetting('GLOBAL_THEME_NAME') : $theme;
55
// Is this theme valid?
56
if (!is_dir('theme/' . $globalTheme))
57
throw new Exception(__('The theme "%s" does not exist', $globalTheme));
59
// Store the theme name for later
60
$this->name = $globalTheme;
63
if (!file_exists('theme/' . $this->name . '/config.php'))
64
throw new Exception(__('The theme "%s" config file does not exist', $globalTheme));
66
require('theme/' . $this->name . '/config.php');
67
$this->config = $config;
69
self::$instance = $this;
73
* GetInstance of Theme
75
private static function GetInstance()
77
if (!isset(self::$instance))
78
self::$instance = new Theme();
80
return self::$instance;
85
* @param string $item Item to Render
86
* @throws Exception if the requested item doesn't exist
88
private static function Render($item)
90
$theme = Theme::GetInstance();
92
// See if we have the requested file in the theme folder
93
if (file_exists('theme/' . $theme->name . '/html/' . $item . '.php')) {
94
include('theme/' . $theme->name . '/html/' . $item . '.php');
95
} // Check the module theme folder
96
else if (file_exists('modules/theme/' . $item . '.php')) {
97
include('modules/theme/' . $item . '.php');
98
} // If not, then use the default folder
99
else if (file_exists('theme/default/html/' . $item . '.php')) {
100
include('theme/default/html/' . $item . '.php');
102
throw new Exception(__('The requested theme item does not exist. [%s, %s]', array($item, $theme->name)));
106
* Render Item but return the value as a string
107
* @param string $item Item to Render
109
* @throws \ErrorException
111
public static function RenderReturn($item)
114
Log::debug('Rendering %s', $item);
117
Theme::Render($item);
119
$output = ob_get_contents();
122
Log::debug('Rendered %s', $item);
125
catch (\ErrorException $e) {
126
Log::critical('Unable to render template. ' . $e->getMessage());
133
* Get an image from the Theme
134
* @param string $item The image filename
135
* @param string $class The class to apply [optional]
138
public static function Image($item, $class = '')
141
$theme = Theme::GetInstance();
143
// See if we have the requested file in the theme folder
144
if (file_exists('theme/' . $theme->name . '/img/' . $item)) {
145
return '<img ' . (($class != '') ? 'class="' . $class . '"' : '') . ' src="theme/' . $theme->name . '/img/' . $item . '" />';
146
} // If not, then use the default folder
147
elseif (file_exists('theme/default/img/' . $item)) {
148
return '<img ' . (($class != '') ? 'class="' . $class . '"' : '') . ' src="theme/default/img/' . $item . '" />';
155
* @param [string] $item the image
158
public static function ImageUrl($item)
161
$theme = Theme::GetInstance();
163
// See if we have the requested file in the theme folder
164
if (file_exists('theme/' . $theme->name . '/img/' . $item)) {
165
return Theme::Get('rootPath') . '/theme/' . $theme->name . '/img/' . $item;
166
} // If not, then use the default folder
167
elseif (file_exists('theme/default/img/' . $item)) {
168
return Theme::Get('rootPath') . '/theme/default/img/' . $item;
175
* @param string $item The Item required
178
public static function ItemPath($item)
181
$theme = Theme::GetInstance();
183
// See if we have the requested file in the theme folder
184
if (file_exists('theme/' . $theme->name . '/' . $item)) {
185
return Theme::Get('rootPath') . '/theme/' . $theme->name . '/' . $item;
186
} // If not, then use the default folder
187
elseif (file_exists('theme/default/' . $item)) {
188
return Theme::Get('rootPath') . '/theme/default/' . $item;
195
* @param string $item The Item required
198
public static function Script($item)
200
$theme = Theme::GetInstance();
202
// See if we have the requested file in the theme folder
203
if (file_exists('theme/' . $theme->name . '/' . $item)) {
204
return '<script src="' . Theme::Get('rootPath') . '/theme/' . $theme->name . '/' . $item . '"></script>';
205
} // If not, then use the default folder
206
elseif (file_exists('theme/default/' . $item)) {
207
return '<script src="' . Theme::Get('rootPath') . '/theme/default/' . $item . '"></script>';
213
* Get the root path for a given path
216
* @throws Exception if the theme is not initialised
218
public static function rootPath($path)
220
return Theme::Get('rootPath') . '/' . $path;
224
* Translate a string into the user language
225
* @param string $string The String to Translate
228
public static function Translate($string)
230
return call_user_func_array('__', func_get_args());
233
public static function Set($key, $value)
235
$theme = Theme::GetInstance();
237
$theme->vars[$key] = $value;
240
public static function Get($key)
242
$theme = Theme::GetInstance();
244
if (!isset($theme->vars[$key]))
247
$return = $theme->vars[$key];
249
if ($key == 'form_meta') {
250
// Append a token to the end
251
$return = $return . '<input type="hidden" name="' . Theme::Get('csrfKey') . '" value="' . Theme::Get('csrfToken') . '">';
256
public static function SetTranslation($key, $value)
258
// Get existing translations
259
$translations = Theme::Get('translations');
261
if ($translations == '') {
262
$translations = array();
264
$translations = json_decode($translations, true);
267
$translations[$key] = $value;
269
Theme::Set('translations', json_encode($translations));
272
public static function Prepare($string)
274
return htmlspecialchars($string);
277
public static function SetPagename($pageName)
279
Theme::GetInstance()->pageName = $pageName;
282
public static function GetPagename()
284
return Theme::GetInstance()->pageName;
287
public static function GetUsername()
289
return Theme::Get('thisUserName');
292
public static function GetUserHomeLink()
294
return Theme::urlFor('home');
297
public static function GetPageHelpLink()
302
public static function GetClock()
304
return Theme::GetInstance()->dateManager->GetClock();
307
public static function ApplicationName()
309
return Theme::GetInstance()->config['app_name'];
312
public static function ThemeName()
314
return Theme::GetInstance()->config['theme_name'];
317
public static function SourceLink()
319
return (isset(Theme::GetInstance()->config['cms_source_url']) ? Theme::GetInstance()->config['cms_source_url'] : 'https://github.com/xibosignage/xibo/');
322
public static function ThemeFolder()
324
return Theme::GetInstance()->name;
327
public static function urlFor($route)
329
$app = Slim::getInstance();
330
return $app->urlFor($route);
333
public static function GetConfig($settingName, $default = null)
335
$theme = Theme::GetInstance();
337
if (isset($theme->config[$settingName]))
338
return $theme->config[$settingName];
345
* @param string $menu The Name of the Menu
346
* @return array Array containing menu items (page, args, class, title, link, li)
348
public static function GetMenu($menu)
350
$theme = Theme::GetInstance();
353
foreach (MenuFactory::getByMenu($menu) as $menuItem) {
354
/* @var Menu $menuItem */
356
$item['page'] = $menuItem->page;
357
$item['args'] = $menuItem->args;
358
$item['class'] = $menuItem->class;
359
$item['title'] = __($menuItem->title);
360
$item['img'] = $menuItem->img;
361
$item['external'] = $menuItem->external;
363
$item['selected'] = ($item['page'] == $theme->pageName);
365
if ($item['external'] == 0) {
366
$item['link'] = Theme::urlFor($item['page'] . 'View');
368
$item['link'] = $item['args'];
371
$item['li'] = '<li class="' . $item['class'] . (($item['selected']) ? ' active' : '') . '"><a href="' . $item['link'] . '" class="' . $item['class'] . (($item['selected']) ? ' active' : '') . '">' . $item['title'] . '</a></li>';
380
* Generate a select list
381
* @param string Select list name
382
* @param array Array of Values
383
* @param string Key for item id
384
* @param string Key for item name
385
* @param string ID value for selected item
386
* @param string Extra attributes to put on the list
387
* @param string Key for item class
390
public static function SelectList($listName, $listValues, $idColumn, $nameColumn, $selectedId = null, $callBack = '', $classColumn = '', $styleColumn = '')
392
$list = '<select class="form-control" name="' . $listName . '" id="' . $listName . '"' . $callBack . '>';
394
foreach ($listValues as $listItem) {
395
$class = ($classColumn == '') ? '' : 'class="' . $listItem[$classColumn] . '"';
396
$style = ($styleColumn == '') ? '' : 'style="' . $listItem[$styleColumn] . '"';
397
$list .= '<option ' . $style . ' ' . $class . ' value="' . $listItem[$idColumn] . '" ' . (($listItem[$idColumn] == $selectedId) ? 'selected' : '') . '>' . $listItem[$nameColumn] . '</option>';
400
$list .= '</select>';