~xibo-maintainers/xibo/tempel

« back to all changes in this revision

Viewing changes to lib/Helper/Theme.php

  • Committer: Dan Garner
  • Date: 2015-03-26 14:08:33 UTC
  • Revision ID: git-v1:70d14044444f8dc5d602b99890d59dea46d9470c
Moved web servable files to web folder

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/*
 
3
 * Xibo - Digital Signage - http://www.xibo.org.uk
 
4
 * Copyright (C) 2006-2013 Daniel Garner
 
5
 *
 
6
 * This file is part of Xibo.
 
7
 *
 
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
 
11
 * any later version. 
 
12
 *
 
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.
 
17
 *
 
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/>.
 
20
 */
 
21
namespace Xibo\Helper;
 
22
use Config;
 
23
use Exception;
 
24
use Extra;
 
25
use ID;
 
26
use Key;
 
27
use MenuManager;
 
28
use Select;
 
29
use Slim\Slim;
 
30
use Xibo\Entity\Menu;
 
31
use Xibo\Factory\MenuFactory;
 
32
 
 
33
defined('XIBO') or die("Sorry, you are not allowed to directly access this page.<br /> Please press the back button in your browser.");
 
34
 
 
35
class Theme
 
36
{
 
37
    private static $instance = null;
 
38
 
 
39
    private $dateManager;
 
40
 
 
41
    private $name = '';
 
42
    private $pageName = '';
 
43
    private $vars = null;
 
44
    private $config = null;
 
45
 
 
46
    public function __construct($theme = NULL)
 
47
    {
 
48
        // Store some things for the Theme engine to use
 
49
        $this->help = new Help();
 
50
        $this->dateManager = new Date();
 
51
 
 
52
        // What is the currently selected theme?
 
53
        $globalTheme = ($theme == NULL) ? Config::GetSetting('GLOBAL_THEME_NAME') : $theme;
 
54
 
 
55
        // Is this theme valid?
 
56
        if (!is_dir('theme/' . $globalTheme))
 
57
            throw new Exception(__('The theme "%s" does not exist', $globalTheme));
 
58
 
 
59
        // Store the theme name for later
 
60
        $this->name = $globalTheme;
 
61
 
 
62
        // Get config
 
63
        if (!file_exists('theme/' . $this->name . '/config.php'))
 
64
            throw new Exception(__('The theme "%s" config file does not exist', $globalTheme));
 
65
 
 
66
        require('theme/' . $this->name . '/config.php');
 
67
        $this->config = $config;
 
68
 
 
69
        self::$instance = $this;
 
70
    }
 
71
 
 
72
    /**
 
73
     * GetInstance of Theme
 
74
     */
 
75
    private static function GetInstance()
 
76
    {
 
77
        if (!isset(self::$instance))
 
78
            self::$instance = new Theme();
 
79
 
 
80
        return self::$instance;
 
81
    }
 
82
 
 
83
    /**
 
84
     * Render Item
 
85
     * @param string $item Item to Render
 
86
     * @throws Exception if the requested item doesn't exist
 
87
     */
 
88
    private static function Render($item)
 
89
    {
 
90
        $theme = Theme::GetInstance();
 
91
 
 
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');
 
101
        } else
 
102
            throw new Exception(__('The requested theme item does not exist. [%s, %s]', array($item, $theme->name)));
 
103
    }
 
104
 
 
105
    /**
 
106
     * Render Item but return the value as a string
 
107
     * @param string $item Item to Render
 
108
     * @return string
 
109
     * @throws \ErrorException
 
110
     */
 
111
    public static function RenderReturn($item)
 
112
    {
 
113
        try {
 
114
            Log::debug('Rendering %s', $item);
 
115
            ob_start();
 
116
 
 
117
            Theme::Render($item);
 
118
 
 
119
            $output = ob_get_contents();
 
120
 
 
121
            ob_end_clean();
 
122
            Log::debug('Rendered %s', $item);
 
123
            return $output;
 
124
        }
 
125
        catch (\ErrorException $e) {
 
126
            Log::critical('Unable to render template. ' . $e->getMessage());
 
127
            ob_end_clean();
 
128
            throw $e;
 
129
        }
 
130
    }
 
131
 
 
132
    /**
 
133
     * Get an image from the Theme
 
134
     * @param string $item The image filename
 
135
     * @param string $class The class to apply [optional]
 
136
     * @return string
 
137
     */
 
138
    public static function Image($item, $class = '')
 
139
    {
 
140
 
 
141
        $theme = Theme::GetInstance();
 
142
 
 
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 . '" />';
 
149
        } else
 
150
            return '';
 
151
    }
 
152
 
 
153
    /**
 
154
     * Get an image URL
 
155
     * @param [string] $item the image
 
156
     * @return string
 
157
     */
 
158
    public static function ImageUrl($item)
 
159
    {
 
160
 
 
161
        $theme = Theme::GetInstance();
 
162
 
 
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;
 
169
        } else
 
170
            return '';
 
171
    }
 
172
 
 
173
    /**
 
174
     * Get Item Path
 
175
     * @param string $item The Item required
 
176
     * @return string
 
177
     */
 
178
    public static function ItemPath($item)
 
179
    {
 
180
 
 
181
        $theme = Theme::GetInstance();
 
182
 
 
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;
 
189
        } else
 
190
            return '';
 
191
    }
 
192
 
 
193
    /**
 
194
     * Get Item Path
 
195
     * @param string $item The Item required
 
196
     * @return string
 
197
     */
 
198
    public static function Script($item)
 
199
    {
 
200
        $theme = Theme::GetInstance();
 
201
 
 
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>';
 
208
        } else
 
209
            return '';
 
210
    }
 
211
 
 
212
    /**
 
213
     * Get the root path for a given path
 
214
     * @param $path
 
215
     * @return string
 
216
     * @throws Exception if the theme is not initialised
 
217
     */
 
218
    public static function rootPath($path)
 
219
    {
 
220
        return Theme::Get('rootPath') . '/' . $path;
 
221
    }
 
222
 
 
223
    /**
 
224
     * Translate a string into the user language
 
225
     * @param string $string The String to Translate
 
226
     * @return string
 
227
     */
 
228
    public static function Translate($string)
 
229
    {
 
230
        return call_user_func_array('__', func_get_args());
 
231
    }
 
232
 
 
233
    public static function Set($key, $value)
 
234
    {
 
235
        $theme = Theme::GetInstance();
 
236
 
 
237
        $theme->vars[$key] = $value;
 
238
    }
 
239
 
 
240
    public static function Get($key)
 
241
    {
 
242
        $theme = Theme::GetInstance();
 
243
 
 
244
        if (!isset($theme->vars[$key]))
 
245
            $return = null;
 
246
        else
 
247
            $return = $theme->vars[$key];
 
248
 
 
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') . '">';
 
252
        }
 
253
        return $return;
 
254
    }
 
255
 
 
256
    public static function SetTranslation($key, $value)
 
257
    {
 
258
        // Get existing translations
 
259
        $translations = Theme::Get('translations');
 
260
 
 
261
        if ($translations == '') {
 
262
            $translations = array();
 
263
        } else {
 
264
            $translations = json_decode($translations, true);
 
265
        }
 
266
 
 
267
        $translations[$key] = $value;
 
268
 
 
269
        Theme::Set('translations', json_encode($translations));
 
270
    }
 
271
 
 
272
    public static function Prepare($string)
 
273
    {
 
274
        return htmlspecialchars($string);
 
275
    }
 
276
 
 
277
    public static function SetPagename($pageName)
 
278
    {
 
279
        Theme::GetInstance()->pageName = $pageName;
 
280
    }
 
281
 
 
282
    public static function GetPagename()
 
283
    {
 
284
        return Theme::GetInstance()->pageName;
 
285
    }
 
286
 
 
287
    public static function GetUsername()
 
288
    {
 
289
        return Theme::Get('thisUserName');
 
290
    }
 
291
 
 
292
    public static function GetUserHomeLink()
 
293
    {
 
294
        return Theme::urlFor('home');
 
295
    }
 
296
 
 
297
    public static function GetPageHelpLink()
 
298
    {
 
299
        return Help::Link();
 
300
    }
 
301
 
 
302
    public static function GetClock()
 
303
    {
 
304
        return Theme::GetInstance()->dateManager->GetClock();
 
305
    }
 
306
 
 
307
    public static function ApplicationName()
 
308
    {
 
309
        return Theme::GetInstance()->config['app_name'];
 
310
    }
 
311
 
 
312
    public static function ThemeName()
 
313
    {
 
314
        return Theme::GetInstance()->config['theme_name'];
 
315
    }
 
316
 
 
317
    public static function SourceLink()
 
318
    {
 
319
        return (isset(Theme::GetInstance()->config['cms_source_url']) ? Theme::GetInstance()->config['cms_source_url'] : 'https://github.com/xibosignage/xibo/');
 
320
    }
 
321
 
 
322
    public static function ThemeFolder()
 
323
    {
 
324
        return Theme::GetInstance()->name;
 
325
    }
 
326
 
 
327
    public static function urlFor($route)
 
328
    {
 
329
        $app = Slim::getInstance();
 
330
        return $app->urlFor($route);
 
331
    }
 
332
 
 
333
    public static function GetConfig($settingName, $default = null)
 
334
    {
 
335
        $theme = Theme::GetInstance();
 
336
 
 
337
        if (isset($theme->config[$settingName]))
 
338
            return $theme->config[$settingName];
 
339
        else
 
340
            return $default;
 
341
    }
 
342
 
 
343
    /**
 
344
     * Get Menu
 
345
     * @param string $menu The Name of the Menu
 
346
     * @return array Array containing menu items (page, args, class, title, link, li)
 
347
     */
 
348
    public static function GetMenu($menu)
 
349
    {
 
350
        $theme = Theme::GetInstance();
 
351
        $array = array();
 
352
 
 
353
        foreach (MenuFactory::getByMenu($menu) as $menuItem) {
 
354
            /* @var Menu $menuItem */
 
355
            $item = array();
 
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;
 
362
 
 
363
            $item['selected'] = ($item['page'] == $theme->pageName);
 
364
 
 
365
            if ($item['external'] == 0) {
 
366
                $item['link'] = Theme::urlFor($item['page'] . 'View');
 
367
            } else {
 
368
                $item['link'] = $item['args'];
 
369
            }
 
370
 
 
371
            $item['li'] = '<li class="' . $item['class'] . (($item['selected']) ? ' active' : '') . '"><a href="' . $item['link'] . '" class="' . $item['class'] . (($item['selected']) ? ' active' : '') . '">' . $item['title'] . '</a></li>';
 
372
 
 
373
            $array[] = $item;
 
374
        }
 
375
 
 
376
        return $array;
 
377
    }
 
378
 
 
379
    /**
 
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
 
388
     * @return string
 
389
     */
 
390
    public static function SelectList($listName, $listValues, $idColumn, $nameColumn, $selectedId = null, $callBack = '', $classColumn = '', $styleColumn = '')
 
391
    {
 
392
        $list = '<select class="form-control" name="' . $listName . '" id="' . $listName . '"' . $callBack . '>';
 
393
 
 
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>';
 
398
        }
 
399
 
 
400
        $list .= '</select>';
 
401
 
 
402
        return $list;
 
403
    }
 
404
}
 
405
 
 
406
?>