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         if (!is_array($sDesc) && strpos($sDesc, "[{") === false) {
00190             stopProfile("parseThroughSmarty");
00191             return $sDesc;
00192         }
00193 
00194 
00195         $iLang = oxLang::getInstance()->getTplLanguage();
00196 
00197         // now parse it through smarty
00198         $oSmarty = clone $this->getSmarty();
00199 
00200         // save old tpl data
00201         $sTplVars = $oSmarty->_tpl_vars;
00202         $blForceRecompile = $oSmarty->force_compile;
00203 
00204         $oSmarty->force_compile = $blRecompile;
00205 
00206         if ( !$oActView ) {
00207             $oActView = oxNew( 'oxubase' );
00208             $oActView->addGlobalParams();
00209         }
00210 
00211         $aViewData = $oActView->getViewData();
00212         foreach ( array_keys( $aViewData ) as $sName ) {
00213             $oSmarty->assign_by_ref( $sName, $aViewData[$sName] );
00214         }
00215 
00216         if ( is_array( $sDesc ) ) {
00217             foreach ( $sDesc as $sName => $aData ) {
00218                 $oSmarty->oxidcache = new oxField( $aData[1], oxField::T_RAW );
00219                 $sRes[$sName] = $oSmarty->fetch( "ox:".$aData[0].$iLang );
00220             }
00221         } else {
00222             $oSmarty->oxidcache = new oxField($sDesc, oxField::T_RAW);
00223             $sRes = $oSmarty->fetch( "ox:{$sOxid}{$iLang}" );
00224         }
00225 
00226         // restore tpl vars for continuing smarty processing if it is in one
00227         $oSmarty->_tpl_vars = $sTplVars;
00228         $oSmarty->force_compile = $blForceRecompile;
00229 
00230         stopProfile("parseThroughSmarty");
00231 
00232         return $sRes;
00233     }
00234 
00242     public function setTemplateDir( $sTplDir )
00243     {
00244         if ( $sTplDir && !in_array( $sTplDir, $this->_aTemplateDir ) ) {
00245             $this->_aTemplateDir[] = $sTplDir;
00246         }
00247     }
00248 
00254     public function getTemplateDirs()
00255     {
00256         $myConfig = $this->getConfig();
00257 
00258         //T2010-01-13
00259         //#1531
00260         $this->setTemplateDir( $myConfig->getTemplateDir( $this->isAdmin() ) );
00261 
00262         if ( !$this->isAdmin() ) {
00263             $this->setTemplateDir( $myConfig->getOutDir( true ) . $myConfig->getConfigParam( 'sTheme' ) . "/tpl/" );
00264         }
00265 
00266         $this->setTemplateDir( $myConfig->getOutDir( true ) . "basic/tpl/" );
00267 
00268         return $this->_aTemplateDir;
00269     }
00270 
00278     protected function _fillCommonSmartyProperties( $oSmarty )
00279     {
00280         $myConfig = $this->getConfig();
00281 
00282         $oSmarty->left_delimiter  = '[{';
00283         $oSmarty->right_delimiter = '}]';
00284 
00285         $oSmarty->register_resource( 'ox', array( 'ox_get_template',
00286                                                   'ox_get_timestamp',
00287                                                   'ox_get_secure',
00288                                                   'ox_get_trusted' ) );
00289 
00290         // $myConfig->blTemplateCaching; // DODGER #655 : permanently switched off as it doesnt work good enough
00291         $oSmarty->caching      = false;
00292         $oSmarty->compile_dir  = $myConfig->getConfigParam( 'sCompileDir' );
00293         $oSmarty->cache_dir    = $myConfig->getConfigParam( 'sCompileDir' );
00294         $oSmarty->template_dir = $this->getTemplateDirs();
00295         $oSmarty->compile_id   = md5( $oSmarty->template_dir[0] );
00296 
00297         $oSmarty->default_template_handler_func = array(oxUtilsView::getInstance(),'_smartyDefaultTemplateHandler');
00298 
00299         $iDebug = $myConfig->getConfigParam( 'iDebug' );
00300         if (  $iDebug == 1 || $iDebug == 3 || $iDebug == 4 ) {
00301             $oSmarty->debugging = true;
00302         }
00303 
00304         //demoshop security
00305         if ( !$myConfig->isDemoShop() ) {
00306             $oSmarty->php_handling = (int) $myConfig->getConfigParam( 'iSmartyPhpHandling' );
00307             $oSmarty->security     = false;
00308         } else {
00309             $oSmarty->php_handling = SMARTY_PHP_REMOVE;
00310             $oSmarty->security     = true;
00311             $oSmarty->security_settings['IF_FUNCS'][] = 'XML_ELEMENT_NODE';
00312             $oSmarty->security_settings['MODIFIER_FUNCS'][] = 'round';
00313             $oSmarty->security_settings['MODIFIER_FUNCS'][] = 'floor';
00314             $oSmarty->security_settings['MODIFIER_FUNCS'][] = 'trim';
00315             $oSmarty->security_settings['MODIFIER_FUNCS'][] = 'is_array';
00316             $oSmarty->security_settings['ALLOW_CONSTANTS'] = true;
00317             $oSmarty->secure_dir = $oSmarty->template_dir;
00318         }
00319 
00320 
00321     }
00322 
00330     protected function _smartyCompileCheck( $oSmarty )
00331     {
00332         $myConfig = $this->getConfig();
00333         $oSmarty->compile_check  = $myConfig->getConfigParam( 'blCheckTemplates' );
00334 
00335     }
00336 
00348     public function _smartyDefaultTemplateHandler($sResourceType, $sResourceName, $sResourceContent, $sResourceTimestamp, $oSmarty)
00349     {
00350         $myConfig = oxConfig::getInstance();
00351         if ( $sResourceType == 'file' && !is_readable($sResourceName) ) {
00352             $sResourceName      = $myConfig->getTemplatePath($sResourceName, $myConfig->isAdmin());
00353             $sResourceContent   = $oSmarty->_read_file($sResourceName);
00354             $sResourceTimestamp = filemtime($sResourceName);
00355             return is_file($sResourceName) && is_readable($sResourceName);
00356         }
00357         return false;
00358     }
00359 
00360 }