]>
git.immae.eu Git - github/wallabag/wallabag.git/blob - inc/poche/Template.class.php
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 class Template
extends Twig_Environment
15 private $canRenderTemplates = TRUE ;
16 private $currentTheme = '' ;
18 public function __construct ( Poche
$wallabag )
20 $this- > wallabag
= $wallabag ;
23 $pocheUser = Session
:: getParam ( 'poche_user' );
25 $themeDirectory = ( is_null ( $pocheUser ) ? DEFAULT_THEME
: $pocheUser- > getConfigValue ( 'theme' ));
27 if ( $themeDirectory === false || ! is_dir ( THEME
. '/' . $themeDirectory )) {
28 $themeDirectory = DEFAULT_THEME
;
31 $this- > currentTheme
= $themeDirectory ;
33 if ( $this- > _themeIsInstalled () === array ()) {
39 * Returns true if selected theme is installed
43 private function _themeIsInstalled ()
47 // Twig is an absolute requirement for wallabag to function.
48 // Abort immediately if the Composer installer hasn't been run yet
49 if (! $this- > canRenderTemplates
) {
50 $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.' ;
53 // Check if the selected theme and its requirements are present
54 $theme = $this- > getTheme ();
55 if ( $theme != '' && ! is_dir ( THEME
. '/' . $theme )) {
56 $errors [] = 'The currently selected theme (' . $theme . ') does not seem to be properly installed (Missing directory: ' . THEME
. '/' . $theme . ')' ;
57 $this- > canRenderTemplates
= FALSE ;
60 $themeInfo = $this- > getThemeInfo ( $theme );
61 if ( isset ( $themeInfo [ 'requirements' ]) && is_array ( $themeInfo [ 'requirements' ])) {
62 foreach ( $themeInfo [ 'requirements' ] as $requiredTheme ) {
63 if (! is_dir ( THEME
. '/' . $requiredTheme )) {
64 $errors [] = 'The required "' . $requiredTheme . '" theme is missing for the current theme (' . $theme . ')' ;
65 $this- > canRenderTemplates
= FALSE ;
70 $currentErrors = ( is_null ( Session
:: getParam ( 'errors' ))? array () : Session
:: getParam ( 'errors' ));
71 Session
:: setParam ( 'errors' , array_merge ( $errors , $currentErrors ));
77 * Initialization for templates
79 private function _init ()
81 $loaderChain = new Twig_Loader_Chain ();
82 $theme = $this- > getTheme ();
84 // add the current theme as first to the loader chain
85 // so Twig will look there first for overridden template files
87 $loaderChain- > addLoader ( new Twig_Loader_Filesystem ( THEME
. '/' . $theme ));
88 } catch ( Twig_Error_Loader
$e ) {
89 # @todo isInstalled() should catch this, inject Twig later
90 die ( 'The currently selected theme (' . $theme . ') does not seem to be properly installed (' . THEME
. '/' . $theme . ' is missing)' );
93 // add all required themes to the loader chain
94 $themeInfo = $this- > getThemeInfo ( $theme );
95 if ( isset ( $themeInfo [ 'requirements' ]) && is_array ( $themeInfo [ 'requirements' ])) {
96 foreach ( $themeInfo [ 'requirements' ] as $requiredTheme ) {
98 $loaderChain- > addLoader ( new Twig_Loader_Filesystem ( THEME
. '/' . $requiredTheme ));
99 } catch ( Twig_Error_Loader
$e ) {
100 # @todo isInstalled() should catch this, inject Twig later
101 die ( 'The required "' . $requiredTheme . '" theme is missing for the current theme (' . $theme . ')' );
107 $twigParams = array ();
109 $twigParams = array ( 'cache' => CACHE
);
112 parent
:: __construct ( $loaderChain , $twigParams );
114 //$tpl = new Twig_Environment($loaderChain, $twigParams);
115 $this- > addExtension ( new Twig_Extensions_Extension_I18n ());
117 # filter to display domain name of an url
118 $filter = new Twig_SimpleFilter ( 'getDomain' , 'Tools::getDomain' );
119 $this- > addFilter ( $filter );
121 # filter for reading time
122 $filter = new Twig_SimpleFilter ( 'getReadingTime' , 'Tools::getReadingTime' );
123 $this- > addFilter ( $filter );
127 * Returns current theme
131 public function getTheme ()
133 return $this- > currentTheme
;
137 * Provides theme information by parsing theme.ini file if present in the theme's root directory.
138 * In all cases, the following data will be returned:
139 * - name: theme's name, or key if the theme is unnamed,
140 * - current: boolean informing if the theme is the current user theme.
142 * @param string $theme Theme key (directory name)
143 * @return array|boolean Theme information, or false if the theme doesn't exist.
145 public function getThemeInfo ( $theme )
147 if (! is_dir ( THEME
. '/' . $theme )) {
151 $themeIniFile = THEME
. '/' . $theme . '/theme.ini' ;
152 $themeInfo = array ();
154 if ( is_file ( $themeIniFile ) && is_readable ( $themeIniFile )) {
155 $themeInfo = parse_ini_file ( $themeIniFile );
158 if ( $themeInfo === false ) {
159 $themeInfo = array ();
162 if (! isset ( $themeInfo [ 'name' ])) {
163 $themeInfo [ 'name' ] = $theme ;
166 $themeInfo [ 'current' ] = ( $theme === $this- > getTheme ());
172 * Returns an array with installed themes
176 public function getInstalledThemes ()
178 $handle = opendir ( THEME
);
181 while (( $theme = readdir ( $handle )) !== false ) {
182 # Themes are stored in a directory, so all directory names are themes
183 # @todo move theme installation data to database
184 if (! is_dir ( THEME
. '/' . $theme ) || in_array ( $theme , array ( '.' , '..' , '_global' ))) {
188 $themes [ $theme ] = $this- > getThemeInfo ( $theme );
197 * Update theme for the current user
201 public function updateTheme ( $newTheme )
203 # we are not going to change it to the current theme...
204 if ( $newTheme == $this- > getTheme ()) {
205 $this- > wallabag
-> messages
-> add ( 'w' , _ ( 'still using the "' . $this- > getTheme () . '" theme!' ));
206 Tools
:: redirect ( '?view=config' );
209 $themes = $this- > getInstalledThemes ();
210 $actualTheme = false ;
212 foreach ( array_keys ( $themes ) as $theme ) {
213 if ( $theme == $newTheme ) {
220 $this- > wallabag
-> messages
-> add ( 'e' , _ ( 'that theme does not seem to be installed' ));
221 Tools
:: redirect ( '?view=config' );
224 $this- > wallabag
-> store
-> updateUserConfig ( $this- > wallabag
-> user
-> getId (), 'theme' , $newTheme );
225 $this- > wallabag
-> messages
-> add ( 's' , _ ( 'you have changed your theme preferences' ));
227 $currentConfig = $_SESSION [ 'poche_user' ]-> config
;
228 $currentConfig [ 'theme' ] = $newTheme ;
230 $_SESSION [ 'poche_user' ]-> setConfig ( $currentConfig );
233 Tools
:: redirect ( '?view=config' );