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     public static function getInstance()
00028     {
00029         // disable caching for test modules
00030         if ( defined( 'OXID_PHP_UNIT' ) ) {
00031             self::$_instance = modInstances::getMod( __CLASS__ );
00032         }
00033 
00034         if ( !self::$_instance instanceof oxUtilsView ) {
00035 
00036 
00037             self::$_instance = oxNew( 'oxUtilsView' );
00038 
00039             if ( defined( 'OXID_PHP_UNIT' ) ) {
00040                 modInstances::addMod( __CLASS__, self::$_instance);
00041             }
00042         }
00043         return self::$_instance;
00044     }
00045 
00055     public function getSmarty( $blReload = false )
00056     {
00057         if ( !self::$_oSmarty || $blReload ) {
00058             self::$_oSmarty = new Smarty;
00059             $this->_fillCommonSmartyProperties( self::$_oSmarty );
00060             $this->_smartyCompileCheck( self::$_oSmarty );
00061         }
00062 
00063         return self::$_oSmarty;
00064     }
00065 
00075     public function getTemplateOutput( $sTemplate, $oObject )
00076     {
00077         $oSmarty = $this->getSmarty();
00078         $iDebug  = $this->getConfig()->getConfigParam( 'iDebug' );
00079 
00080         // assign
00081         $aViewData = $oObject->getViewData();
00082         if ( is_array( $aViewData ) ) {
00083             foreach ( array_keys( $aViewData ) as $sViewName ) {
00084                 // show debbuging information
00085                 if ( $iDebug == 4 ) {
00086                     echo( "TemplateData[$sViewName] : \n");
00087                     print_r( $aViewData[$sViewName] );
00088                 }
00089                 $oSmarty->assign_by_ref( $sViewName, $aViewData[$sViewName] );
00090             }
00091         }
00092 
00093         return $oSmarty->fetch( $sTemplate );
00094     }
00095 
00104     public function passAllErrorsToView( &$aView, $aErrors )
00105     {
00106         if ( count( $aErrors ) > 0 ) {
00107             foreach ( $aErrors as $sLocation => $aEx2 ) {
00108                 foreach ( $aEx2 as $sKey => $oEr ) {
00109                     $aView['Errors'][$sLocation][$sKey] = unserialize( $oEr );
00110                 }
00111             }
00112         }
00113     }
00114 
00127     public function addErrorToDisplay( $oEr, $blFull = false, $blCustomDestination = false, $sCustomDestination = "" )
00128     {
00129         if ( $blCustomDestination && ( oxConfig::getParameter( 'CustomError' ) || $sCustomDestination!= '' ) ) {
00130             // check if the current request wants do display exceptions on its own
00131             $sDestination = oxConfig::getParameter( 'CustomError' );
00132             if ( $sCustomDestination != '' ) {
00133                 $sDestination = $sCustomDestination;
00134             }
00135         } else {
00136             //default
00137             $sDestination = 'default';
00138         }
00139 
00140         $aEx = oxSession::getVar( 'Errors' );
00141         if ( $oEr instanceof oxException ) {
00142              $oEx = oxNew( 'oxExceptionToDisplay' );
00143              $oEx->setMessage( $oEr->getMessage() );
00144              $oEx->setExceptionType( get_class( $oEr ) );
00145              $oEx->setValues( $oEr->getValues() );
00146              $oEx->setStackTrace( $oEr->getTraceAsString() );
00147              $oEx->setDebug( $blFull );
00148              $oEr = $oEx;
00149         } elseif ( $oEr && ! ( $oEr instanceof oxIDisplayError ) ) {
00150             // assuming that a string was given
00151             $sTmp = $oEr;
00152             $oEr = oxNew( 'oxDisplayError' );
00153             $oEr->setMessage( $sTmp );
00154         } elseif ( $oEr instanceof oxIDisplayError ) {
00155             // take the object
00156         } else {
00157             $oEr = null;
00158         }
00159 
00160         if ( $oEr ) {
00161             $aEx[$sDestination][] = serialize( $oEr );
00162             oxSession::setVar( 'Errors', $aEx );
00163         }
00164     }
00165 
00178     public function parseThroughSmarty( $sDesc, $sOxid = null, $oActView = null, $blRecompile = false )
00179     {
00180         $iLang = oxLang::getInstance()->getTplLanguage();
00181 
00182         // now parse it through smarty
00183         $oSmarty = clone $this->getSmarty();
00184 
00185         // save old tpl data
00186         $sTplVars = $oSmarty->_tpl_vars;
00187         $blForceRecompile = $oSmarty->force_compile;
00188 
00189         $oSmarty->force_compile = $blRecompile;
00190 
00191         if ( !$oActView ) {
00192             $oActView = oxNew( 'oxubase' );
00193             $oActView->addGlobalParams();
00194         }
00195 
00196         $aViewData = $oActView->getViewData();
00197         foreach ( array_keys( $aViewData ) as $sName ) {
00198             $oSmarty->assign_by_ref( $sName, $aViewData[$sName] );
00199         }
00200 
00201         if ( is_array( $sDesc ) ) {
00202             foreach ( $sDesc as $sName => $aData ) {
00203                 $oSmarty->oxidcache = new oxField( $aData[1], oxField::T_RAW );
00204                 $sRes[$sName] = $oSmarty->fetch( "ox:".$aData[0].$iLang );
00205             }
00206         } else {
00207             $oSmarty->oxidcache = new oxField($sDesc, oxField::T_RAW);
00208             $sRes = $oSmarty->fetch( "ox:{$sOxid}{$iLang}" );
00209         }
00210 
00211         // restore tpl vars for continuing smarty processing if it is in one
00212         $oSmarty->_tpl_vars = $sTplVars;
00213         $oSmarty->force_compile = $blForceRecompile;
00214 
00215         return $sRes;
00216     }
00217 
00225     protected function _fillCommonSmartyProperties( $oSmarty )
00226     {
00227 
00228         $myConfig = $this->getConfig();
00229 
00230         //T2010-01-13
00231         //#1531
00232         $sTplDir1 = $myConfig->getTemplateDir( $this->isAdmin() );
00233         $sTplDir2 = $myConfig->getOutDir() . $myConfig->getConfigParam('sTheme') . "/tpl/";
00234         $aTemplateDir = array($sTplDir1);
00235         if (!$this->isAdmin() && $sTplDir1 != $sTplDir2)
00236             $aTemplateDir[] = $sTplDir2;
00237 
00238 
00239         $aTemplateDir = array($myConfig->getTemplateDir( $this->isAdmin() ));
00240         $aTemplateDir[] = "/htdocs/oxideshop/eshop/source/out/basic/tpl/";
00241 
00242 
00243         $aTemplateDir = array($myConfig->getTemplateDir( $this->isAdmin() ));
00244         $aTemplateDir[] = $myConfig->getOutDir()."basic/tpl/";
00245 
00246         $oSmarty->left_delimiter  = '[{';
00247         $oSmarty->right_delimiter = '}]';
00248 
00249         $oSmarty->register_resource( 'ox', array( 'ox_get_template',
00250                                                   'ox_get_timestamp',
00251                                                   'ox_get_secure',
00252                                                   'ox_get_trusted' ) );
00253 
00254         // $myConfig->blTemplateCaching; // DODGER #655 : permanently switched off as it doesnt work good enough
00255         $oSmarty->caching      = false;
00256         $oSmarty->compile_dir  = $myConfig->getConfigParam( 'sCompileDir' );
00257         $oSmarty->cache_dir    = $myConfig->getConfigParam( 'sCompileDir' );
00258         $oSmarty->template_dir = $aTemplateDir;
00259         $oSmarty->compile_id   = md5( $oSmarty->template_dir[0] );
00260 
00261         $oSmarty->default_template_handler_func = array(oxUtilsView::getInstance(),'_smartyDefaultTemplateHandler');
00262 
00263         $iDebug = $myConfig->getConfigParam( 'iDebug' );
00264         if (  $iDebug == 1 || $iDebug == 3 || $iDebug == 4 ) {
00265             $oSmarty->debugging = true;
00266         }
00267 
00268         //demoshop security
00269         if ( !$myConfig->isDemoShop() ) {
00270             $oSmarty->php_handling = (int) $myConfig->getConfigParam( 'iSmartyPhpHandling' );
00271             $oSmarty->security     = false;
00272         } else {
00273             $oSmarty->php_handling = SMARTY_PHP_REMOVE;
00274             $oSmarty->security     = true;
00275             $oSmarty->security_settings['IF_FUNCS'][] = 'XML_ELEMENT_NODE';
00276             $oSmarty->security_settings['MODIFIER_FUNCS'][] = 'round';
00277             $oSmarty->security_settings['MODIFIER_FUNCS'][] = 'trim';
00278             $oSmarty->security_settings['MODIFIER_FUNCS'][] = 'is_array';
00279             $oSmarty->security_settings['ALLOW_CONSTANTS'] = true;
00280             $oSmarty->secure_dir = $oSmarty->template_dir;
00281         }
00282 
00283 
00284     }
00285 
00293     protected function _smartyCompileCheck( $oSmarty )
00294     {
00295         $myConfig = $this->getConfig();
00296         $oSmarty->compile_check  = $myConfig->getConfigParam( 'blCheckTemplates' );
00297 
00298     }
00299 
00311     public function _smartyDefaultTemplateHandler($sResourceType, $sResourceName, $sResourceContent, $sResourceTimestamp, $oSmarty)
00312     {
00313         $myConfig = oxConfig::getInstance();
00314         if ( $sResourceType == 'file' && !is_readable($sResourceName) ) {
00315             $sResourceName      = $myConfig->getTemplatePath($sResourceName, $myConfig->isAdmin());
00316             $sResourceContent   = $oSmarty->_read_file($sResourceName);
00317             $sResourceTimestamp = filemtime($sResourceName);
00318             return is_file($sResourceName) && is_readable($sResourceName);
00319         }
00320         return false;
00321     }
00322 
00323 }

Generated by  doxygen 1.6.2