]>
git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/Wallabag/Template.php
e8a6daa3b636091e8657dd2323c2744136b48e18
3 * wallabag, self hostable application allowing you to not miss any content anymore
6 * @author Nicolas LÅ“uillet <nicolas@loeuillet.org>
8 * @license http://opensource.org/licenses/MIT see COPYING file
11 namespace Wallabag\Wallabag
;
13 class Template
extends Twig_Environment
17 private $canRenderTemplates = TRUE ;
18 private $currentTheme = '' ;
20 public function __construct ( Wallabag
$wallabag )
22 $this- > wallabag
= $wallabag ;
25 $pocheUser = Session
:: getParam ( 'poche_user' );
27 $themeDirectory = ( is_null ( $pocheUser ) ? DEFAULT_THEME
: $pocheUser- > getConfigValue ( 'theme' ));
29 if ( $themeDirectory === false || ! is_dir ( THEME
. '/' . $themeDirectory )) {
30 $themeDirectory = DEFAULT_THEME
;
33 $this- > currentTheme
= $themeDirectory ;
35 if ( $this- > _themeIsInstalled () === array ()) {
41 * Returns true if selected theme is installed
45 private function _themeIsInstalled ()
49 // Twig is an absolute requirement for wallabag to function.
50 // Abort immediately if the Composer installer hasn't been run yet
51 if (! $this- > canRenderTemplates
) {
52 $errors [] = 'Twig does not seem to be installed. Please initialize the Composer installation to automatically fetch dependencies. You can also download <a href="http://wllbg.org/vendor">vendor.zip</a> and extract it in your wallabag folder.' ;
55 // Check if the selected theme and its requirements are present
56 $theme = $this- > getTheme ();
57 if ( $theme != '' && ! is_dir ( THEME
. '/' . $theme )) {
58 $errors [] = 'The currently selected theme (' . $theme . ') does not seem to be properly installed (Missing directory: ' . THEME
. '/' . $theme . ')' ;
59 $this- > canRenderTemplates
= FALSE ;
62 $themeInfo = $this- > getThemeInfo ( $theme );
63 if ( isset ( $themeInfo [ 'requirements' ]) && is_array ( $themeInfo [ 'requirements' ])) {
64 foreach ( $themeInfo [ 'requirements' ] as $requiredTheme ) {
65 if (! is_dir ( THEME
. '/' . $requiredTheme )) {
66 $errors [] = 'The required "' . $requiredTheme . '" theme is missing for the current theme (' . $theme . ')' ;
67 $this- > canRenderTemplates
= FALSE ;
72 $currentErrors = ( is_null ( Session
:: getParam ( 'errors' ))? array () : Session
:: getParam ( 'errors' ));
73 Session
:: setParam ( 'errors' , array_merge ( $errors , $currentErrors ));
79 * Initialization for templates
81 private function _init ()
83 $loaderChain = new Twig_Loader_Chain ();
84 $theme = $this- > getTheme ();
86 // add the current theme as first to the loader chain
87 // so Twig will look there first for overridden template files
89 $loaderChain- > addLoader ( new Twig_Loader_Filesystem ( THEME
. '/' . $theme ));
90 } catch ( Twig_Error_Loader
$e ) {
91 # @todo isInstalled() should catch this, inject Twig later
92 die ( 'The currently selected theme (' . $theme . ') does not seem to be properly installed (' . THEME
. '/' . $theme . ' is missing)' );
95 // add all required themes to the loader chain
96 $themeInfo = $this- > getThemeInfo ( $theme );
97 if ( isset ( $themeInfo [ 'requirements' ]) && is_array ( $themeInfo [ 'requirements' ])) {
98 foreach ( $themeInfo [ 'requirements' ] as $requiredTheme ) {
100 $loaderChain- > addLoader ( new Twig_Loader_Filesystem ( THEME
. '/' . $requiredTheme ));
101 } catch ( Twig_Error_Loader
$e ) {
102 # @todo isInstalled() should catch this, inject Twig later
103 die ( 'The required "' . $requiredTheme . '" theme is missing for the current theme (' . $theme . ')' );
109 $twigParams = array ();
111 $twigParams = array ( 'cache' => CACHE
);
114 parent
:: __construct ( $loaderChain , $twigParams );
116 //$tpl = new Twig_Environment($loaderChain, $twigParams);
117 $this- > addExtension ( new Twig_Extensions_Extension_I18n ());
119 # filter to display domain name of an url
120 $filter = new Twig_SimpleFilter ( 'getDomain' , 'Tools::getDomain' );
121 $this- > addFilter ( $filter );
123 # filter for reading time
124 $filter = new Twig_SimpleFilter ( 'getReadingTime' , 'Tools::getReadingTime' );
125 $this- > addFilter ( $filter );
129 * Returns current theme
133 public function getTheme ()
135 return $this- > currentTheme
;
139 * Provides theme information by parsing theme.ini file if present in the theme's root directory.
140 * In all cases, the following data will be returned:
141 * - name: theme's name, or key if the theme is unnamed,
142 * - current: boolean informing if the theme is the current user theme.
144 * @param string $theme Theme key (directory name)
145 * @return array|boolean Theme information, or false if the theme doesn't exist.
147 public function getThemeInfo ( $theme )
149 if (! is_dir ( THEME
. '/' . $theme )) {
153 $themeIniFile = THEME
. '/' . $theme . '/theme.ini' ;
154 $themeInfo = array ();
156 if ( is_file ( $themeIniFile ) && is_readable ( $themeIniFile )) {
157 $themeInfo = parse_ini_file ( $themeIniFile );
160 if ( $themeInfo === false ) {
161 $themeInfo = array ();
164 if (! isset ( $themeInfo [ 'name' ])) {
165 $themeInfo [ 'name' ] = $theme ;
168 $themeInfo [ 'current' ] = ( $theme === $this- > getTheme ());
174 * Returns an array with installed themes
178 public function getInstalledThemes ()
180 $handle = opendir ( THEME
);
183 while (( $theme = readdir ( $handle )) !== false ) {
184 # Themes are stored in a directory, so all directory names are themes
185 # @todo move theme installation data to database
186 if (! is_dir ( THEME
. '/' . $theme ) || in_array ( $theme , array ( '.' , '..' , '_global' ))) {
190 $themes [ $theme ] = $this- > getThemeInfo ( $theme );
199 * Update theme for the current user
203 public function updateTheme ( $newTheme )
205 # we are not going to change it to the current theme...
206 if ( $newTheme == $this- > getTheme ()) {
207 $this- > wallabag
-> messages
-> add ( 'w' , _ ( 'still using the "' . $this- > getTheme () . '" theme!' ));
208 Tools
:: redirect ( '?view=config' );
211 $themes = $this- > getInstalledThemes ();
212 $actualTheme = false ;
214 foreach ( array_keys ( $themes ) as $theme ) {
215 if ( $theme == $newTheme ) {
222 $this- > wallabag
-> messages
-> add ( 'e' , _ ( 'that theme does not seem to be installed' ));
223 Tools
:: redirect ( '?view=config' );
226 $this- > wallabag
-> store
-> updateUserConfig ( $this- > wallabag
-> user
-> getId (), 'theme' , $newTheme );
227 $this- > wallabag
-> messages
-> add ( 's' , _ ( 'you have changed your theme preferences' ));
229 $currentConfig = $_SESSION [ 'poche_user' ]-> config
;
230 $currentConfig [ 'theme' ] = $newTheme ;
232 $_SESSION [ 'poche_user' ]-> setConfig ( $currentConfig );
235 Tools
:: redirect ( '?view=config' );