[ Index ]
 

Code source de LifeType 1.2.4

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

/class/template/ -> templateservice.class.php (source)

   1  <?php
   2  
   3      lt_include( PLOG_CLASS_PATH.'class/config/config.class.php' );
   4      lt_include( PLOG_CLASS_PATH.'class/file/file.class.php' );
   5  
   6      /**
   7       * default permissions used to create temporary template folders. Seems like
   8       * Smarty creates them as 0771 but we have been adviced to create
   9       * them as 775 as per this discussion: http://bugs.plogworld.net/view.php?id=253
  10       */
  11      define( 'DEFAULT_TEMPLATE_TEMP_FOLDER_PERMISSIONS', 0775 );
  12  
  13      /**
  14       * \ingroup Template
  15       *
  16       * Factory class that takes care of providing Template or CachedTemplate objects
  17       * whenever requested.
  18       *
  19       * The advantage of using this TemplateService class is that we can delegate on it things
  20       * like finding the folder where the template is, choosing the right template depending
  21       * on the client type (normal browser, wap-enabled device, etc)
  22       *
  23       * In order to find the most suitable template, it takes several things into account:
  24       *
  25       * <ul>
  26       * <li>Settings stored for the current blog</li>
  27       * <li>User agent of the client</li> (<b>NOTE: </b>will not be implemented yet)
  28       * <li>The default template specified in the server-wide configuration file</li>
  29       * </ul>
  30       *
  31       * TemplateService is the preferred way to generate instances of the Template class:
  32       *
  33       * <pre>
  34       *  $ts = new TemplateService();
  35       *  // it's enough with "main", there is no need to specify "main.template" as the template name
  36       *  $template = $ts->Template( "main", $blogInfo->getBlogTemplate(), $blogInfo );
  37       *  $template->assign( "string", "This is a sample string" );
  38       *  ...
  39       *  print($template->display());
  40       * </pre>
  41       */
  42      class TemplateService
  43      {
  44  
  45          /**
  46           * Constructor
  47           */
  48      	function TemplateService()
  49          {
  50  
  51          }
  52  
  53          /**
  54           * Generates a Template object for the given template name. This fuction does <b>not</b>
  55           * require the full path to the file!!
  56           *
  57           * @param templateName The name of the template, it is not necessary to provide
  58           * the .template extension.
  59           * @param layout A predefined layout style, which corresponds with the name of a
  60           * folder under the templates/ folder so 'blueish' would mean templates/blueish/
  61           * @param blogInfo If this parameter is not null, then it will be used to locate
  62           * a blog-specific template. Otherwise, we will only look for the template
  63           * in the "global" folders. This parameter defaults to 'null'.
  64           * @return A Template object representing the template file we asked for.
  65           */
  66          function Template( $templateName, $layout, $blogInfo = null )
  67          {
  68              lt_include( PLOG_CLASS_PATH . 'class/template/template.class.php' );
  69  
  70              // get some information about the folder where the template is and the template file
  71              $templateInfo = $this->_getTemplateFileInfo( $templateName, $layout, $blogInfo );
  72              $templateFileName = $templateInfo['templateFileName'];
  73              $templateFolder = $templateInfo['templateFolder'];
  74  
  75              // create the template and make sure if we we have to force Smarty
  76              // to look for it somewhere else other than the default folder
  77              $t = new Template( $templateFileName );
  78              if( $templateFolder != '' )
  79                  $t->setTemplateDir( $templateFolder );
  80  
  81              $t->assign( 'templatename', $templateName );
  82              $t->assign( 'blogtemplate', $templateFolder.'/'.$layout );
  83              $t->config_dir = $templateFolder.'/'.$layout;
  84  
  85              // change a few things...
  86              $t = $this->_configureTemplateSettings( $t, $blogInfo, $layout );
  87  
  88              return $t;
  89          }
  90  
  91          /**
  92           * Returns a Template or CachedTemplate object pointing to a custom template.
  93           *
  94           * @param templateName The name of the template, it is not necessary to provide
  95           * the .template extension.
  96           * @param layout A predefined layout style, which corresponds with the name of a
  97           * folder under the templates/ folder so 'blueish' would mean templates/blueish/
  98           * @param cached Whether the custom template should be cached or not (this will affect
  99           * the kind of the returned object)
 100           * @return a Template or CachedTemplate object, depending on whether
 101           * $cached is 'true' or 'false'
 102           */
 103  		function customTemplate( $templateName, $layout, $cached = false )
 104          {
 105              lt_include( PLOG_CLASS_PATH.'class/template/cachedtemplate.class.php' );
 106  
 107              // get a nice Template object
 108              $config =& Config::getConfig();
 109              if( $cached )
 110                  $t = $this->CachedTemplate( $templateName, $layout );
 111              else
 112                  $t = $this->Template( $templateName, $layout );
 113  
 114              $tmpFolder = $config->getValue( "temp_folder" );
 115              $templateTmpFolder = $tmpFolder.'/'.$layout;
 116              $t->cache_dir = $templateTmpFolder;
 117              $t->compile_dir = $templateTmpFolder;
 118  
 119              // and now make sure that there is a folder where we can save
 120              // our rendered templates
 121              if( !File::exists( $templateTmpFolder )) {
 122                  File::createDir( $templateTmpFolder, DEFAULT_TEMPLATE_TEMP_FOLDER_PERMISSIONS );
 123              }
 124  
 125              $t->compile_check = $config->getValue( 'template_compile_check' );
 126  
 127              return $t;
 128          }
 129  
 130          /**
 131           * returns a template from the admin folder. It still uses TemplateService::Template but
 132           * exports additional information to the template such as the base template path so that
 133           * we can use {$admintemplatepath} from Smarty to get the right path, etc.
 134           *
 135           * @param templateName The name of the template, it is not necessary to provide
 136           * the .template extension.
 137           * @param blogInfo
 138           * @return A Template object pointing to a template from the templates/admin/ folder
 139           */
 140  		function AdminTemplate( $templateName, $blogInfo = null )
 141          {
 142              lt_include( PLOG_CLASS_PATH.'class/template/templatesets/templatesetstorage.class.php' );
 143  
 144              $t = $this->Template( $templateName, 'admin', $blogInfo );
 145              $t->assign( 'admintemplatepath', TemplateSetStorage::getAdminTemplateFolder());
 146              
 147              return $t;
 148          }
 149  
 150          /**
 151           * returns a CachedTemplate object, which works in exactly the same way as a Template
 152           * object but its contents will be cached as soon as they are generated. The lifetime
 153           * of cached contents is controlled via the 'template_cache_lifetime' configuration
 154           * parameter, but contents will be regenerated automatically as soon as
 155           * CacheControll::resetBlogCache() is called.
 156           *
 157           * @param templateName The name of the template, it is not necessary to provide
 158           * the .template extension.
 159           * @param layout A predefined layout style, which corresponds with the name of a
 160           * folder under the templates/ folder so 'blueish' would mean templates/blueish/
 161           * @param blogInfo
 162           * @return a CachedTemplate object pointing to the right .template file in disk
 163           */
 164          function CachedTemplate( $templateName, $layout, $blogInfo = null )
 165          {
 166              lt_include( PLOG_CLASS_PATH.'class/template/cachedtemplate.class.php' );
 167  
 168              // get some information about the folder where the template is and the template file
 169              $templateInfo = $this->_getTemplateFileInfo( $templateName, $layout, $blogInfo );
 170              $templateFileName = $templateInfo['templateFileName'];
 171              $templateFolder = $templateInfo['templateFolder'];
 172  
 173              // create the template and make sure if we we have to force Smarty
 174              // to look for it somewhere else other than the default folder
 175              $t = new CachedTemplate( $templateFileName );
 176              if( $templateFolder != '' )
 177                  $t->setTemplateDir( $templateFolder );
 178  
 179              $t->assign( 'templatename', $templateName );
 180              $t->assign( 'blogtemplate', $templateFolder.'/'.$layout );
 181              $t->config_dir = $templateFolder.'/'.$layout;
 182  
 183              // change a few things...
 184              $t = $this->_configureTemplateSettings( $t, $blogInfo );
 185              
 186              $t->useTemplateLoadOrder = true;
 187  
 188              return $t;
 189          }
 190  
 191          /**
 192           * @private
 193           * Factored out from above...
 194           */
 195  		function _getTemplateFileInfo( $templateName, $layout, $blogInfo )
 196          {
 197              lt_include( PLOG_CLASS_PATH.'class/template/templatesets/templatesetstorage.class.php' );
 198  
 199              // build the file name
 200              if( $blogInfo == null ) {
 201                  $templateFileName = $layout.'/'.$templateName.'.template';
 202                  $templateFolder = TemplateSetStorage::getBaseTemplateFolder();
 203              }
 204              else {
 205                  //
 206                  // might be the case that the template is not local but global, so
 207                  // by default, global templates will always have preference over
 208                  // local templates. If the template is global, then
 209                  //
 210                  $baseTemplateFolder = TemplateSetStorage::getBaseTemplateFolder();
 211                  $globalTemplateFolder = $baseTemplateFolder.'/'.$layout;
 212                  $localTemplateFolder  = $baseTemplateFolder.'/'.BLOG_BASE_TEMPLATE_FOLDER.$blogInfo->getId().'/'.$layout;
 213  
 214                  //print("local = $localTemplateFolder - global = $globalTemplateFolder<br/>");
 215                  $templateFileName = $layout.'/'.$templateName.'.template';
 216                  if( !File::isDir( $globalTemplateFolder )) {
 217                      //$templateFileName = $layout."/".$templateName.".template";
 218                      $templateFolder = $baseTemplateFolder.'/'.BLOG_BASE_TEMPLATE_FOLDER.$blogInfo->getId();
 219                  }
 220                  else
 221                      $templateFolder = $baseTemplateFolder.'/';
 222              }
 223  
 224              $result['templateFileName'] = $templateFileName;
 225              $result['templateFolder'] = $templateFolder;
 226  
 227              return $result;
 228          }
 229  
 230          /**
 231           * Returns a Template object loaded from a plugin template
 232           * Plugins are different in the sense that they store their templates in the
 233           * plugins/xxx/templates, where 'xxx' is the plugin identifier
 234           *
 235           * @param pluginId The id of the plugin, which matches the name of a folder
 236           * under the plugins/ folder.
 237           * @param templateName Name of the template, without the .template extension
 238           * @param blogInfo
 239           * @return a Template object
 240           * @see PluginCachedTemplate
 241           */
 242  		function PluginTemplate( $pluginId, $templateName, $blogInfo = null )
 243          {
 244              lt_include( PLOG_CLASS_PATH . 'class/template/templatesets/templatesetstorage.class.php' );
 245              lt_include( PLOG_CLASS_PATH . 'class/template/template.class.php' );
 246  
 247              // define the template file name
 248              $templateFolder = TemplateSetStorage::getPluginTemplateFolder( $pluginId );
 249              $templateFileName = $templateFolder.$templateName.'.template';
 250              $t = new Template( $templateFileName );
 251              $t->setTemplateDir( $templateFolder );
 252  
 253              $t->assign( 'templatename', $templateName );
 254              $t->assign( 'admintemplatepath', TemplateSetStorage::getAdminTemplateFolder());
 255              $t->assign( 'plugintemplatepath', $templateFolder );
 256  
 257              // change a few things...
 258              $t = $this->_configureTemplateSettings( $t, $blogInfo );
 259              
 260              
 261  
 262              return $t;
 263          }
 264  
 265          /**
 266           * Returns a CachedTemplate object loaded from a plugin template
 267           * Plugins are different in the sense that they store their templates in the
 268           * plugins/xxx/templates, where 'xxx' is the plugin identifier
 269           *
 270           * @param pluginId The id of the plugin, which matches the name of a folder
 271           * under the plugins/ folder.
 272           * @param templateName Name of the template, without the .template extension
 273           * @param blogInfo
 274           * @return a CachedTemplate object
 275           */
 276  		function PluginCachedTemplate( $pluginId, $templateName, $blogInfo = null )
 277          {
 278              lt_include( PLOG_CLASS_PATH.'class/template/cachedtemplate.class.php' );
 279  
 280              // define the template file name
 281              $templateFolder = TemplateSetStorage::getPluginTemplateFolder( $pluginId );
 282              $templateFileName = $templateFolder.$templateName.'.template';
 283              $t = new CachedTemplate( $templateFileName );
 284              $t->setTemplateDir( $templateFolder );
 285  
 286              $t->assign( 'templatename', $templateName );
 287              $t->assign( 'admintemplatepath', TemplateSetStorage::getAdminTemplateFolder());
 288              $t->assign( 'plugintemplatepath', $templateFolder );
 289  
 290              // change a few things...
 291              $t = $this->_configureTemplateSettings( $t, $blogInfo );
 292  
 293              return $t;
 294          }
 295  
 296          /**
 297           * @private
 298           */
 299  		function _configureTemplateSettings( $t, $blogInfo, $layout = "" )
 300          {
 301              // change a few things...
 302              $config =& Config::getConfig();
 303              $tmpFolder = $config->getValue( 'temp_folder' );
 304              if( $blogInfo == null )
 305                  $blogTmpFolder = $tmpFolder;
 306              else {
 307                  $blogTmpFolder = $tmpFolder.'/'.$blogInfo->getId();
 308                  if( !File::exists( $blogTmpFolder )) {
 309                      File::createDir( $blogTmpFolder, DEFAULT_TEMPLATE_TEMP_FOLDER_PERMISSIONS );
 310                  }
 311                  $t->secure_dir[] = "./templates/blog_".$blogInfo->getId()."/$layout";
 312              }
 313  
 314              $t->cache_dir    = $blogTmpFolder;
 315              $t->compile_dir  = $blogTmpFolder;
 316  
 317              $t->compile_check = $config->getValue( 'template_compile_check' );
 318  
 319              return $t;
 320          }
 321      }
 322  ?>


Généré le : Mon Nov 26 21:04:15 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics