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     protected $_blIsTplBlocks = null;
00035 
00041     public static function getInstance()
00042     {
00043         // disable caching for test modules
00044         if ( defined( 'OXID_PHP_UNIT' ) ) {
00045             self::$_instance = modInstances::getMod( __CLASS__ );
00046         }
00047 
00048         if ( !self::$_instance instanceof oxUtilsView ) {
00049 
00050 
00051             self::$_instance = oxNew( 'oxUtilsView' );
00052 
00053             if ( defined( 'OXID_PHP_UNIT' ) ) {
00054                 modInstances::addMod( __CLASS__, self::$_instance);
00055             }
00056         }
00057         return self::$_instance;
00058     }
00059 
00069     public function getSmarty( $blReload = false )
00070     {
00071         if ( !self::$_oSmarty || $blReload ) {
00072             self::$_oSmarty = new Smarty;
00073             $this->_fillCommonSmartyProperties( self::$_oSmarty );
00074             $this->_smartyCompileCheck( self::$_oSmarty );
00075         }
00076 
00077         return self::$_oSmarty;
00078     }
00079 
00089     public function getTemplateOutput( $sTemplate, $oObject )
00090     {
00091         $oSmarty = $this->getSmarty();
00092         $iDebug  = $this->getConfig()->getConfigParam( 'iDebug' );
00093 
00094         // assign
00095         $aViewData = $oObject->getViewData();
00096         if ( is_array( $aViewData ) ) {
00097             foreach ( array_keys( $aViewData ) as $sViewName ) {
00098                 // show debbuging information
00099                 if ( $iDebug == 4 ) {
00100                     echo( "TemplateData[$sViewName] : \n");
00101                     print_r( $aViewData[$sViewName] );
00102                 }
00103                 $oSmarty->assign_by_ref( $sViewName, $aViewData[$sViewName] );
00104             }
00105         }
00106 
00107         return $oSmarty->fetch( $sTemplate );
00108     }
00109 
00118     public function passAllErrorsToView( &$aView, $aErrors )
00119     {
00120         if ( count( $aErrors ) > 0 ) {
00121             foreach ( $aErrors as $sLocation => $aEx2 ) {
00122                 foreach ( $aEx2 as $sKey => $oEr ) {
00123                     $aView['Errors'][$sLocation][$sKey] = unserialize( $oEr );
00124                 }
00125             }
00126         }
00127     }
00128 
00141     public function addErrorToDisplay( $oEr, $blFull = false, $blCustomDestination = false, $sCustomDestination = "" )
00142     {
00143         if ( $blCustomDestination && ( oxConfig::getParameter( 'CustomError' ) || $sCustomDestination!= '' ) ) {
00144             // check if the current request wants do display exceptions on its own
00145             $sDestination = oxConfig::getParameter( 'CustomError' );
00146             if ( $sCustomDestination != '' ) {
00147                 $sDestination = $sCustomDestination;
00148             }
00149         } else {
00150             //default
00151             $sDestination = 'default';
00152         }
00153 
00154         //starting session if not yet started as all exception
00155         //messages are stored in session
00156         $oSession = $this->getSession();
00157         if ( !$oSession->getId() && !$oSession->isHeaderSent() ) {
00158             $oSession->setForceNewSession();
00159             $oSession->start();
00160         }
00161 
00162         $aEx = oxSession::getVar( 'Errors' );
00163         if ( $oEr instanceof oxException ) {
00164              $oEx = oxNew( 'oxExceptionToDisplay' );
00165              $oEx->setMessage( $oEr->getMessage() );
00166              $oEx->setExceptionType( get_class( $oEr ) );
00167 
00168              if ( $oEr instanceof oxSystemComponentException ) {
00169                 $oEx->setMessageArgs( $oEr->getComponent() );
00170              }
00171 
00172              $oEx->setValues( $oEr->getValues() );
00173              $oEx->setStackTrace( $oEr->getTraceAsString() );
00174              $oEx->setDebug( $blFull );
00175              $oEr = $oEx;
00176         } elseif ( $oEr && ! ( $oEr instanceof oxIDisplayError ) ) {
00177             // assuming that a string was given
00178             $sTmp = $oEr;
00179             $oEr = oxNew( 'oxDisplayError' );
00180             $oEr->setMessage( $sTmp );
00181         } elseif ( $oEr instanceof oxIDisplayError ) {
00182             // take the object
00183         } else {
00184             $oEr = null;
00185         }
00186 
00187         if ( $oEr ) {
00188             $aEx[$sDestination][] = serialize( $oEr );
00189             oxSession::setVar( 'Errors', $aEx );
00190         }
00191     }
00192 
00205     public function parseThroughSmarty( $sDesc, $sOxid = null, $oActView = null, $blRecompile = false )
00206     {
00207         startProfile("parseThroughSmarty");
00208 
00209         if (!is_array($sDesc) && strpos($sDesc, "[{") === false) {
00210             stopProfile("parseThroughSmarty");
00211             return $sDesc;
00212         }
00213 
00214 
00215         $iLang = oxLang::getInstance()->getTplLanguage();
00216 
00217         // now parse it through smarty
00218         $oSmarty = clone $this->getSmarty();
00219 
00220         // save old tpl data
00221         $sTplVars = $oSmarty->_tpl_vars;
00222         $blForceRecompile = $oSmarty->force_compile;
00223 
00224         $oSmarty->force_compile = $blRecompile;
00225 
00226         if ( !$oActView ) {
00227             $oActView = oxNew( 'oxubase' );
00228             $oActView->addGlobalParams();
00229         }
00230 
00231         $aViewData = $oActView->getViewData();
00232         foreach ( array_keys( $aViewData ) as $sName ) {
00233             $oSmarty->assign_by_ref( $sName, $aViewData[$sName] );
00234         }
00235 
00236         if ( is_array( $sDesc ) ) {
00237             foreach ( $sDesc as $sName => $aData ) {
00238                 $oSmarty->oxidcache = new oxField( $aData[1], oxField::T_RAW );
00239                 $sRes[$sName] = $oSmarty->fetch( "ox:".$aData[0].$iLang );
00240             }
00241         } else {
00242             $oSmarty->oxidcache = new oxField($sDesc, oxField::T_RAW);
00243             $sRes = $oSmarty->fetch( "ox:{$sOxid}{$iLang}" );
00244         }
00245 
00246         // restore tpl vars for continuing smarty processing if it is in one
00247         $oSmarty->_tpl_vars = $sTplVars;
00248         $oSmarty->force_compile = $blForceRecompile;
00249 
00250         stopProfile("parseThroughSmarty");
00251 
00252         return $sRes;
00253     }
00254 
00262     public function setTemplateDir( $sTplDir )
00263     {
00264         if ( $sTplDir && !in_array( $sTplDir, $this->_aTemplateDir ) ) {
00265             $this->_aTemplateDir[] = $sTplDir;
00266         }
00267     }
00268 
00274     public function getTemplateDirs()
00275     {
00276         $myConfig = $this->getConfig();
00277 
00278         //T2010-01-13
00279         //#1531
00280         $this->setTemplateDir( $myConfig->getTemplateDir( $this->isAdmin() ) );
00281 
00282         if ( !$this->isAdmin() ) {
00283             $this->setTemplateDir( $myConfig->getOutDir( true ) . $myConfig->getConfigParam( 'sTheme' ) . "/tpl/" );
00284         }
00285 
00286         return $this->_aTemplateDir;
00287     }
00288 
00296     protected function _fillCommonSmartyProperties( $oSmarty )
00297     {
00298         $myConfig = $this->getConfig();
00299         $oSmarty->left_delimiter  = '[{';
00300         $oSmarty->right_delimiter = '}]';
00301 
00302         $oSmarty->register_resource( 'ox', array( 'ox_get_template',
00303                                                   'ox_get_timestamp',
00304                                                   'ox_get_secure',
00305                                                   'ox_get_trusted' ) );
00306 
00307         // $myConfig->blTemplateCaching; // DODGER #655 : permanently switched off as it doesnt work good enough
00308         $oSmarty->caching      = false;
00309         $oSmarty->compile_dir  = $myConfig->getConfigParam( 'sCompileDir' );
00310         $oSmarty->cache_dir    = $myConfig->getConfigParam( 'sCompileDir' );
00311         $oSmarty->template_dir = $this->getTemplateDirs();
00312         $oSmarty->compile_id   = md5( $oSmarty->template_dir[0].'__'.$myConfig->getShopId() );
00313 
00314         $oSmarty->default_template_handler_func = array(oxUtilsView::getInstance(),'_smartyDefaultTemplateHandler');
00315 
00316         include_once dirname(__FILE__).'/smarty/plugins/prefilter.oxblock.php';
00317         $oSmarty->register_prefilter('smarty_prefilter_oxblock');
00318 
00319         $iDebug = $myConfig->getConfigParam( 'iDebug' );
00320         if (  $iDebug == 1 || $iDebug == 3 || $iDebug == 4 ) {
00321             $oSmarty->debugging = true;
00322         }
00323 
00324         //demoshop security
00325         if ( !$myConfig->isDemoShop() ) {
00326             $oSmarty->php_handling = (int) $myConfig->getConfigParam( 'iSmartyPhpHandling' );
00327             $oSmarty->security     = false;
00328         } else {
00329             $oSmarty->php_handling = SMARTY_PHP_REMOVE;
00330             $oSmarty->security     = true;
00331             $oSmarty->security_settings['IF_FUNCS'][] = 'XML_ELEMENT_NODE';
00332             $oSmarty->security_settings['MODIFIER_FUNCS'][] = 'round';
00333             $oSmarty->security_settings['MODIFIER_FUNCS'][] = 'floor';
00334             $oSmarty->security_settings['MODIFIER_FUNCS'][] = 'trim';
00335             $oSmarty->security_settings['MODIFIER_FUNCS'][] = 'implode';
00336             $oSmarty->security_settings['MODIFIER_FUNCS'][] = 'is_array';
00337             $oSmarty->security_settings['ALLOW_CONSTANTS'] = true;
00338             $oSmarty->secure_dir = $oSmarty->template_dir;
00339         }
00340 
00341 
00342     }
00343 
00351     protected function _smartyCompileCheck( $oSmarty )
00352     {
00353         $myConfig = $this->getConfig();
00354         $oSmarty->compile_check  = $myConfig->getConfigParam( 'blCheckTemplates' );
00355 
00356     }
00357 
00369     public function _smartyDefaultTemplateHandler($sResourceType, $sResourceName, $sResourceContent, $sResourceTimestamp, $oSmarty)
00370     {
00371         $myConfig = oxConfig::getInstance();
00372         if ( $sResourceType == 'file' && !is_readable($sResourceName) ) {
00373             $sResourceName      = $myConfig->getTemplatePath($sResourceName, $myConfig->isAdmin());
00374             $sResourceContent   = $oSmarty->_read_file($sResourceName);
00375             $sResourceTimestamp = filemtime($sResourceName);
00376             return is_file($sResourceName) && is_readable($sResourceName);
00377         }
00378         return false;
00379     }
00380 
00392     protected function _getTemplateBlock($sModule, $sFile)
00393     {
00394         $sFileName = $this->getConfig()->getConfigParam( 'sShopDir' )."/modules/$sModule/out/blocks/$sFile.tpl";
00395         if (file_exists($sFileName) && is_readable($sFileName)) {
00396             return file_get_contents($sFileName);
00397         } else {
00398             throw oxNew( "oxException", "Template block file ($sFileName) not found for '$sModule' module." );
00399         }
00400     }
00401 
00411     public function getTemplateBlocks($sFile)
00412     {
00413         $oConfig = $this->getConfig();
00414 
00415         $sTplDir = trim($oConfig->getConfigParam('_sTemplateDir'), '/\\');
00416         $sFile = str_replace(array('\\', '//'), '/', $sFile);
00417         if (preg_match('@/'.preg_quote($sTplDir, '@').'/(.*)$@', $sFile, $m)) {
00418             $sFile = $m[1];
00419         }
00420 
00421         $aRet = array();
00422         $oDb = oxDb::getDb(true);
00423         $sFileParam = $oDb->quote($sFile);
00424         $sShpIdParam = $oDb->quote($oConfig->getShopId());
00425 
00426         if ( $this->_blIsTplBlocks === null ) {
00427             $this->_blIsTplBlocks = false;
00428             $sSql = "select COUNT(*) from oxtplblocks where oxactive=1 and oxshopid=$sShpIdParam";
00429             $rs = $oDb->getOne( $sSql );
00430             if ( $rs ) {
00431                 $this->_blIsTplBlocks = true;
00432             }
00433         }
00434 
00435         if ( $this->_blIsTplBlocks ) {
00436             $sSql = "select * from oxtplblocks where oxactive=1 and oxshopid=$sShpIdParam and oxtemplate=$sFileParam order by oxpos asc";
00437             $rs = $oDb->Execute($sSql);
00438 
00439             if ($rs != false && $rs->recordCount() > 0) {
00440                 while (!$rs->EOF) {
00441                     try {
00442                         if (!is_array($aRet[$rs->fields['OXBLOCKNAME']])) {
00443                             $aRet[$rs->fields['OXBLOCKNAME']] = array();
00444                         }
00445                         $aRet[$rs->fields['OXBLOCKNAME']][] = $this->_getTemplateBlock($rs->fields['OXMODULE'], $rs->fields['OXFILE']);
00446                     } catch (oxException $oE) {
00447                         $oE->debugOut();
00448                     }
00449 
00450                     $rs->moveNext();
00451                 }
00452             }
00453         }
00454 
00455         return $aRet;
00456     }
00457 }