oxutilsview.php

Go to the documentation of this file.
00001 <?php
00002 
00006 class oxUtilsView extends oxSuperCfg
00007 {
00013     private static $_instance = null;
00014 
00020     protected static $_oSmarty = null;
00021 
00027     protected $_aTemplateDir = array();
00028 
00034     public static function getInstance()
00035     {
00036         // disable caching for test modules
00037         if ( defined( 'OXID_PHP_UNIT' ) ) {
00038             self::$_instance = modInstances::getMod( __CLASS__ );
00039         }
00040 
00041         if ( !self::$_instance instanceof oxUtilsView ) {
00042 
00043 
00044             self::$_instance = oxNew( 'oxUtilsView' );
00045 
00046             if ( defined( 'OXID_PHP_UNIT' ) ) {
00047                 modInstances::addMod( __CLASS__, self::$_instance);
00048             }
00049         }
00050         return self::$_instance;
00051     }
00052 
00062     public function getSmarty( $blReload = false )
00063     {
00064         if ( !self::$_oSmarty || $blReload ) {
00065             self::$_oSmarty = new Smarty;
00066             $this->_fillCommonSmartyProperties( self::$_oSmarty );
00067             $this->_smartyCompileCheck( self::$_oSmarty );
00068         }
00069 
00070         return self::$_oSmarty;
00071     }
00072 
00082     public function getTemplateOutput( $sTemplate, $oObject )
00083     {
00084         $oSmarty = $this->getSmarty();
00085         $iDebug  = $this->getConfig()->getConfigParam( 'iDebug' );
00086 
00087         // assign
00088         $aViewData = $oObject->getViewData();
00089         if ( is_array( $aViewData ) ) {
00090             foreach ( array_keys( $aViewData ) as $sViewName ) {
00091                 // show debbuging information
00092                 if ( $iDebug == 4 ) {
00093                     echo( "TemplateData[$sViewName] : \n");
00094                     print_r( $aViewData[$sViewName] );
00095                 }
00096                 $oSmarty->assign_by_ref( $sViewName, $aViewData[$sViewName] );
00097             }
00098         }
00099 
00100         return $oSmarty->fetch( $sTemplate );
00101     }
00102 
00111     public function passAllErrorsToView( &$aView, $aErrors )
00112     {
00113         if ( count( $aErrors ) > 0 ) {
00114             foreach ( $aErrors as $sLocation => $aEx2 ) {
00115                 foreach ( $aEx2 as $sKey => $oEr ) {
00116                     $aView['Errors'][$sLocation][$sKey] = unserialize( $oEr );
00117                 }
00118             }
00119         }
00120     }
00121 
00134     public function addErrorToDisplay( $oEr, $blFull = false, $blCustomDestination = false, $sCustomDestination = "" )
00135     {
00136         if ( $blCustomDestination && ( oxConfig::getParameter( 'CustomError' ) || $sCustomDestination!= '' ) ) {
00137             // check if the current request wants do display exceptions on its own
00138             $sDestination = oxConfig::getParameter( 'CustomError' );
00139             if ( $sCustomDestination != '' ) {
00140                 $sDestination = $sCustomDestination;
00141             }
00142         } else {
00143             //default
00144             $sDestination = 'default';
00145         }
00146 
00147         $aEx = oxSession::getVar( 'Errors' );
00148         if ( $oEr instanceof oxException ) {
00149              $oEx = oxNew( 'oxExceptionToDisplay' );
00150              $oEx->setMessage( $oEr->getMessage() );
00151              $oEx->setExceptionType( get_class( $oEr ) );
00152              $oEx->setValues( $oEr->getValues() );
00153              $oEx->setStackTrace( $oEr->getTraceAsString() );
00154              $oEx->setDebug( $blFull );
00155              $oEr = $oEx;
00156         } elseif ( $oEr && ! ( $oEr instanceof oxIDisplayError ) ) {
00157             // assuming that a string was given
00158             $sTmp = $oEr;
00159             $oEr = oxNew( 'oxDisplayError' );
00160             $oEr->setMessage( $sTmp );
00161         } elseif ( $oEr instanceof oxIDisplayError ) {
00162             // take the object
00163         } else {
00164             $oEr = null;
00165         }
00166 
00167         if ( $oEr ) {
00168             $aEx[$sDestination][] = serialize( $oEr );
00169             oxSession::setVar( 'Errors', $aEx );
00170         }
00171     }
00172 
00185     public function parseThroughSmarty( $sDesc, $sOxid = null, $oActView = null, $blRecompile = false )
00186     {
00187         startProfile("parseThroughSmarty");
00188 
00189         $iLang = oxLang::getInstance()->getTplLanguage();
00190 
00191         // now parse it through smarty
00192         $oSmarty = clone $this->getSmarty();
00193 
00194         // save old tpl data
00195         $sTplVars = $oSmarty->_tpl_vars;
00196         $blForceRecompile = $oSmarty->force_compile;
00197 
00198         $oSmarty->force_compile = $blRecompile;
00199 
00200         if ( !$oActView ) {
00201             $oActView = oxNew( 'oxubase' );
00202             $oActView->addGlobalParams();
00203         }
00204 
00205         $aViewData = $oActView->getViewData();
00206         foreach ( array_keys( $aViewData ) as $sName ) {
00207             $oSmarty->assign_by_ref( $sName, $aViewData[$sName] );
00208         }
00209 
00210         if ( is_array( $sDesc ) ) {
00211             foreach ( $sDesc as $sName => $aData ) {
00212                 $oSmarty->oxidcache = new oxField( $aData[1], oxField::T_RAW );
00213                 $sRes[$sName] = $oSmarty->fetch( "ox:".$aData[0].$iLang );
00214             }
00215         } else {
00216             $oSmarty->oxidcache = new oxField($sDesc, oxField::T_RAW);
00217             $sRes = $oSmarty->fetch( "ox:{$sOxid}{$iLang}" );
00218         }
00219 
00220         // restore tpl vars for continuing smarty processing if it is in one
00221         $oSmarty->_tpl_vars = $sTplVars;
00222         $oSmarty->force_compile = $blForceRecompile;
00223 
00224         stopProfile("parseThroughSmarty");
00225 
00226         return $sRes;
00227     }
00228 
00236     public function setTemplateDir( $sTplDir )
00237     {
00238         if ( $sTplDir && !in_array( $sTplDir, $this->_aTemplateDir ) ) {
00239             $this->_aTemplateDir[] = $sTplDir;
00240         }
00241     }
00242 
00248     public function getTemplateDirs()
00249     {
00250         $myConfig = $this->getConfig();
00251 
00252         //T2010-01-13
00253         //#1531
00254         $this->setTemplateDir( $myConfig->getTemplateDir( $this->isAdmin() ) );
00255 
00256         if ( !$this->isAdmin() ) {
00257             $this->setTemplateDir( $myConfig->getOutDir( true ) . $myConfig->getConfigParam( 'sTheme' ) . "/tpl/" );
00258         }
00259 
00260         $this->setTemplateDir( $myConfig->getOutDir( true ) . "basic/tpl/" );
00261 
00262         return $this->_aTemplateDir;
00263     }
00264 
00272     protected function _fillCommonSmartyProperties( $oSmarty )
00273     {
00274         $myConfig = $this->getConfig();
00275 
00276         $oSmarty->left_delimiter  = '[{';
00277         $oSmarty->right_delimiter = '}]';
00278 
00279         $oSmarty->register_resource( 'ox', array( 'ox_get_template',
00280                                                   'ox_get_timestamp',
00281                                                   'ox_get_secure',
00282                                                   'ox_get_trusted' ) );
00283 
00284         // $myConfig->blTemplateCaching; // DODGER #655 : permanently switched off as it doesnt work good enough
00285         $oSmarty->caching      = false;
00286         $oSmarty->compile_dir  = $myConfig->getConfigParam( 'sCompileDir' );
00287         $oSmarty->cache_dir    = $myConfig->getConfigParam( 'sCompileDir' );
00288         $oSmarty->template_dir = $this->getTemplateDirs();
00289         $oSmarty->compile_id   = md5( $oSmarty->template_dir[0] );
00290 
00291         $oSmarty->default_template_handler_func = array(oxUtilsView::getInstance(),'_smartyDefaultTemplateHandler');
00292 
00293         $iDebug = $myConfig->getConfigParam( 'iDebug' );
00294         if (  $iDebug == 1 || $iDebug == 3 || $iDebug == 4 ) {
00295             $oSmarty->debugging = true;
00296         }
00297 
00298         //demoshop security
00299         if ( !$myConfig->isDemoShop() ) {
00300             $oSmarty->php_handling = (int) $myConfig->getConfigParam( 'iSmartyPhpHandling' );
00301             $oSmarty->security     = false;
00302         } else {
00303             $oSmarty->php_handling = SMARTY_PHP_REMOVE;
00304             $oSmarty->security     = true;
00305             $oSmarty->security_settings['IF_FUNCS'][] = 'XML_ELEMENT_NODE';
00306             $oSmarty->security_settings['MODIFIER_FUNCS'][] = 'round';
00307             $oSmarty->security_settings['MODIFIER_FUNCS'][] = 'trim';
00308             $oSmarty->security_settings['MODIFIER_FUNCS'][] = 'is_array';
00309             $oSmarty->security_settings['ALLOW_CONSTANTS'] = true;
00310             $oSmarty->secure_dir = $oSmarty->template_dir;
00311         }
00312 
00313 
00314     }
00315 
00323     protected function _smartyCompileCheck( $oSmarty )
00324     {
00325         $myConfig = $this->getConfig();
00326         $oSmarty->compile_check  = $myConfig->getConfigParam( 'blCheckTemplates' );
00327 
00328     }
00329 
00341     public function _smartyDefaultTemplateHandler($sResourceType, $sResourceName, $sResourceContent, $sResourceTimestamp, $oSmarty)
00342     {
00343         $myConfig = oxConfig::getInstance();
00344         if ( $sResourceType == 'file' && !is_readable($sResourceName) ) {
00345             $sResourceName      = $myConfig->getTemplatePath($sResourceName, $myConfig->isAdmin());
00346             $sResourceContent   = $oSmarty->_read_file($sResourceName);
00347             $sResourceTimestamp = filemtime($sResourceName);
00348             return is_file($sResourceName) && is_readable($sResourceName);
00349         }
00350         return false;
00351     }
00352 
00353 }

Generated by  doxygen 1.6.2