[ 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/view/ -> smartyview.class.php (source)

   1  <?php
   2  
   3      lt_include( PLOG_CLASS_PATH.'class/view/view.class.php' );
   4      lt_include( PLOG_CLASS_PATH.'class/template/templateservice.class.php' );
   5      
   6      /**
   7       * different smarty cache modes that we can use
   8       */
   9      define( 'SMARTY_VIEW_CACHE_DISABLED', 0 );
  10      define( 'SMARTY_VIEW_CACHE_ENABLED', 1 );
  11      define( 'SMARTY_VIEW_CACHE_CHECK', 2 );
  12      
  13      /**
  14       * \ingroup View
  15       *
  16       * Implementation of views in the plog framework with support for Smarty templates. This is the first
  17       * useful class that extends from View that adds some functionality, such as working with BlogInfo objects
  18       * and providing support for smarty templates. 
  19       *
  20       * This class uses the TemplateService class for dealing with templates.
  21       *
  22       * @see BlogView
  23       */
  24      class SmartyView extends View
  25      {
  26      
  27          var $_cachingEnabled;
  28          var $_templateName;
  29          var $_templateSetName;
  30          var $_template;
  31          var $_data;
  32          var $_viewId;
  33  
  34          /**
  35           * Constructor of the class
  36           *
  37           * @param blogInfo A valid BlogInfo object representing the blog to which this View belongs
  38           * @param templateName A template name
  39           * @param cachingEnabled either SMARTY_VIEW_CACHE_ENABLED, SMARTY_VIEW_CACHED_DISABLED, SMARTY_VIEW_CACHE_CHECK.
  40           * If left as SMARTY_VIEW_CACHE_CHECK, the blog settings will checked to determine whether caching is enabled
  41           * or not.
  42           * @param data Data that will be used to generate a unique id for the cached view (it will be ignored
  43           * if caching is not enabled)
  44           */
  45  		function SmartyView( $blogInfo, $templateName, $cachingEnabled = SMARTY_VIEW_CACHE_CHECK, $data = Array())
  46          {
  47              // parent constructor
  48              $this->View();
  49              
  50              if( $cachingEnabled == SMARTY_VIEW_CACHE_CHECK ) {
  51                  // detect whether caching should be enabled or not
  52                  $config =& Config::getConfig();            
  53                  $cachingEnabled = $config->getValue( "template_cache_enabled" );
  54              }
  55              
  56              // whether caching is enabled or not
  57              $this->_cachingEnabled = $cachingEnabled;
  58                  
  59              // save the blogInfo object
  60              $this->_blogInfo = $blogInfo;
  61  
  62              // name of the tepmlate
  63              $this->_templateName = $templateName;
  64  
  65              // name of the template set name
  66              $blogSettings = $this->_blogInfo->getSettings();
  67              $this->_templateSetName = $blogSettings->getValue( 'template' );
  68              
  69              // Check the template exist or not
  70              // If not, we use the defaule_template and also assign the default_template back to blogInfo
  71              if ( !$this->isTemplateSetExist( $this->_templateSetName ) )
  72              {
  73                  $config =& Config::getConfig();            
  74                  $this->_templateSetName =  $config->getValue( "default_template" );
  75                  $this->_blogInfo->setTemplate( $this->_templateSetName );
  76  
  77                  lt_include( PLOG_CLASS_PATH.'class/dao/blogs.class.php' );
  78                  $blogs = new Blogs();
  79                  $blogs->updateBlog( $this->_blogInfo );
  80              }
  81                          
  82              // get the right CachedTemplate or Template object
  83              $ts = new TemplateService();            
  84              if( $this->isCachingEnabled()) {
  85                  // get a CachedTemplate object
  86                  $this->_template = $ts->CachedTemplate( $this->_templateName, $this->_templateSetName, $this->_blogInfo );
  87                  // data used to calculate the view id
  88                  $this->_data = $data;
  89                  // and generate the right cache id for it
  90                  $this->_data["blogId"] = $blogInfo->getId();                
  91                  $this->_viewId = $this->generateCacheId();
  92              }
  93              else {
  94                  $this->_template = $ts->Template( $this->_templateName, $blogSettings->getValue( 'template' ), $this->_blogInfo );
  95                  $this->_template->useTemplateLoadOrder = true;
  96              }
  97          }
  98          
  99          /**
 100           * returns true if caching is enabled for this particular view
 101           *
 102           * @return true if caching is enabled or not.
 103           */
 104  		function isCachingEnabled()
 105          {
 106              return $this->_cachingEnabled;
 107          }
 108          
 109          
 110          /**
 111           * returns true if the current view is cached or false if it is not or caching is disabled
 112           *
 113           * @return true if view enabled or false otherwise
 114           */
 115  		function isCached()
 116          {
 117              if( $this->isCachingEnabled())
 118                  $isCached = $this->_template->isCached( $this->_viewId );
 119              else
 120                  $isCached = false;
 121              
 122              return $isCached;
 123          }        
 124  
 125          /**
 126           * returns true if the current template set does exist or flase if it does not exist
 127           *
 128           * @return true if template set exist or false otherwise
 129           */
 130  		function isTemplateSetExist( $templateSetName )
 131          {
 132              lt_include( PLOG_CLASS_PATH."class/template/templatesets/templatesets.class.php" );
 133              $templateSets = new TemplateSets();
 134              if( !$templateSets->isTemplate( $templateSetName ) && !$templateSets->isBlogTemplate( $templateSetName, $this->_blogInfo->getId() ) ) 
 135                  return false;
 136              else
 137                  return true;
 138          }
 139  
 140          /**
 141           * generates a unique identifier for this view. The cache identifier is generated
 142           * based on the last parameter passed to the view constructor
 143           *
 144           * @param returns a unique id for this view
 145           */
 146  		function generateCacheId()
 147          {
 148              $viewId = "";
 149              foreach( $this->_data as $key => $value )
 150                  $viewId .= "$key=$value";
 151  
 152              $viewId = md5($this->_blogInfo->getId()."-".$viewId);
 153              
 154              return $viewId;
 155          }
 156                  
 157          /**
 158           * returns this view's id
 159           *
 160           * @return this view's id
 161           */
 162  		function getSmartyViewId()
 163          {
 164              return $this->_viewId;
 165          }
 166          
 167          /**
 168           * returns true whether the support for HTTP condiditional requests
 169           * is enabled or not
 170           *
 171           * @return true if support is enabled or false otherwise
 172           */
 173  		function isTemplateHttpCacheEnabled()
 174          {
 175              $config =& Config::getConfig();
 176              
 177              if( $config->getValue( "template_cache_enabled" ))
 178                  $httpCacheEnabled = $config->getValue( "template_http_cache_enabled" );
 179              else
 180                  $httpCacheEnabled = false;
 181  
 182              return( $httpCacheEnabled );
 183          }
 184          
 185  		function getCacheTimeSeconds()
 186          {
 187              $config =& Config::getConfig();
 188              
 189              $cacheTime = $config->getValue( "http_cache_lifetime", 1800 );
 190              
 191              if( $cacheTime == "" || !is_numeric($cacheTime))
 192                  $cacheTime = 1; // [almost] no value, just one second of caching
 193              if( $cacheTime == -1 )
 194                  $cacheTime = 788400000; // a veeery long time!
 195              if( $cacheTime > 788400000)
 196                  $cacheTime = 788400000;
 197  
 198              return( $cacheTime );
 199          }
 200          
 201          /**
 202           * Public method that allows view classes to do something to the content right before it is
 203           * sent to the client. It takes the content itself as a string as the first parameter and is
 204           * expected to return the final version of the content.
 205           *
 206           * This method is only called in case content needs to be returned. If HTTP caching is enabled and
 207           * the caching logic determines that no new data needs to be sent (only a Last-Modified header in that case)
 208           * then this method will <b>not</b> called.
 209           *
 210           * @param content the content generated after rendering the template but before being sent to the client
 211           * @return The final version of the content
 212           */
 213  		function preProcessViewContents( $content )
 214          {
 215              return( $content );
 216          }
 217  
 218          /**
 219           * Renders the view using the Smarty template object that we created in the constructor. This method
 220           * sends data to the client so it should be called as the last bit of code in our custom classes
 221           * extending SmartyView.
 222           *
 223           * It has no paramaters and returns nothing.
 224           */
 225          function render()
 226          {
 227              // the View class also needs to do some things...
 228              parent::render();
 229          
 230              $sendOutput = true;
 231              
 232              // check if plog is configured to use conditional http headers and stuff like
 233              // that... Also, use the HttpCache class to determine whether we should send the
 234              // content or not
 235              if( $this->isTemplateHttpCacheEnabled() && $this->isCached()) {
 236                  // some debug information
 237                  $timestamp = $this->_template->getCreationTimestamp();
 238                  // and now send the correct headers
 239                  
 240                  lt_include( PLOG_CLASS_PATH.'class/net/http/httpcache.class.php' );                    
 241                  if( HttpCache::httpConditional( $timestamp, $this->getCacheTimeSeconds()))
 242                      $sendOutput = false;
 243                      
 244                  $header = "Last-Modified: ".gmdate('D, d M Y H:i:s', $timestamp).' GMT';
 245                  header( $header );                    
 246              }
 247              else {
 248                  // send the results if needed                
 249                  $sendOutput = true;
 250              }
 251              
 252              if( $sendOutput ) {
 253                  // pass all the values to the template object
 254                  $this->_template->assign( $this->_params->getAsArray());            
 255              
 256                  // and finally send them after calling the pre-processing method
 257                  $content = $this->preProcessViewContents( $this->_template->fetch( $this->getSmartyViewId()));                
 258                  print( $content );
 259              }
 260          }
 261       }
 262  ?>


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