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         //starting session if not yet started as all exception
00148         //messages are stored in session
00149         $oSession = $this->getSession();
00150         if ( !$oSession->getId() && !$oSession->isHeaderSent() ) {
00151             $oSession->setForceNewSession();
00152             $oSession->start();
00153         }
00154 
00155         $aEx = oxSession::getVar( 'Errors' );
00156         if ( $oEr instanceof oxException ) {
00157              $oEx = oxNew( 'oxExceptionToDisplay' );
00158              $oEx->setMessage( $oEr->getMessage() );
00159              $oEx->setExceptionType( get_class( $oEr ) );
00160 
00161              if ( $oEr instanceof oxSystemComponentException ) {
00162                 $oEx->setMessageArgs( $oEr->getComponent() );
00163              }
00164 
00165              $oEx->setValues( $oEr->getValues() );
00166              $oEx->setStackTrace( $oEr->getTraceAsString() );
00167              $oEx->setDebug( $blFull );
00168              $oEr = $oEx;
00169         } elseif ( $oEr && ! ( $oEr instanceof oxIDisplayError ) ) {
00170             // assuming that a string was given
00171             $sTmp = $oEr;
00172             $oEr = oxNew( 'oxDisplayError' );
00173             $oEr->setMessage( $sTmp );
00174         } elseif ( $oEr instanceof oxIDisplayError ) {
00175             // take the object
00176         } else {
00177             $oEr = null;
00178         }
00179 
00180         if ( $oEr ) {
00181             $aEx[$sDestination][] = serialize( $oEr );
00182             oxSession::setVar( 'Errors', $aEx );
00183         }
00184     }
00185 
00198     public function parseThroughSmarty( $sDesc, $sOxid = null, $oActView = null, $blRecompile = false )
00199     {
00200         startProfile("parseThroughSmarty");
00201 
00202         if (!is_array($sDesc) && strpos($sDesc, "[{") === false) {
00203             stopProfile("parseThroughSmarty");
00204             return $sDesc;
00205         }
00206 
00207 
00208         $iLang = oxLang::getInstance()->getTplLanguage();
00209 
00210         // now parse it through smarty
00211         $oSmarty = clone $this->getSmarty();
00212 
00213         // save old tpl data
00214         $sTplVars = $oSmarty->_tpl_vars;
00215         $blForceRecompile = $oSmarty->force_compile;
00216 
00217         $oSmarty->force_compile = $blRecompile;
00218 
00219         if ( !$oActView ) {
00220             $oActView = oxNew( 'oxubase' );
00221             $oActView->addGlobalParams();
00222         }
00223 
00224         $aViewData = $oActView->getViewData();
00225         foreach ( array_keys( $aViewData ) as $sName ) {
00226             $oSmarty->assign_by_ref( $sName, $aViewData[$sName] );
00227         }
00228 
00229         if ( is_array( $sDesc ) ) {
00230             foreach ( $sDesc as $sName => $aData ) {
00231                 $oSmarty->oxidcache = new oxField( $aData[1], oxField::T_RAW );
00232                 $sRes[$sName] = $oSmarty->fetch( "ox:".$aData[0].$iLang );
00233             }
00234         } else {
00235             $oSmarty->oxidcache = new oxField($sDesc, oxField::T_RAW);
00236             $sRes = $oSmarty->fetch( "ox:{$sOxid}{$iLang}" );
00237         }
00238 
00239         // restore tpl vars for continuing smarty processing if it is in one
00240         $oSmarty->_tpl_vars = $sTplVars;
00241         $oSmarty->force_compile = $blForceRecompile;
00242 
00243         stopProfile("parseThroughSmarty");
00244 
00245         return $sRes;
00246     }
00247 
00255     public function setTemplateDir( $sTplDir )
00256     {
00257         if ( $sTplDir && !in_array( $sTplDir, $this->_aTemplateDir ) ) {
00258             $this->_aTemplateDir[] = $sTplDir;
00259         }
00260     }
00261 
00267     public function getTemplateDirs()
00268     {
00269         $myConfig = $this->getConfig();
00270 
00271         //T2010-01-13
00272         //#1531
00273         $this->setTemplateDir( $myConfig->getTemplateDir( $this->isAdmin() ) );
00274 
00275         if ( !$this->isAdmin() ) {
00276             $this->setTemplateDir( $myConfig->getOutDir( true ) . $myConfig->getConfigParam( 'sTheme' ) . "/tpl/" );
00277         }
00278 
00279         return $this->_aTemplateDir;
00280     }
00281 
00289     protected function _fillCommonSmartyProperties( $oSmarty )
00290     {
00291         $myConfig = $this->getConfig();
00292         $oSmarty->left_delimiter  = '[{';
00293         $oSmarty->right_delimiter = '}]';
00294 
00295         $oSmarty->register_resource( 'ox', array( 'ox_get_template',
00296                                                   'ox_get_timestamp',
00297                                                   'ox_get_secure',
00298                                                   'ox_get_trusted' ) );
00299 
00300         // $myConfig->blTemplateCaching; // DODGER #655 : permanently switched off as it doesnt work good enough
00301         $oSmarty->caching      = false;
00302         $oSmarty->compile_dir  = $myConfig->getConfigParam( 'sCompileDir' );
00303         $oSmarty->cache_dir    = $myConfig->getConfigParam( 'sCompileDir' );
00304         $oSmarty->template_dir = $this->getTemplateDirs();
00305         $oSmarty->compile_id   = md5( $oSmarty->template_dir[0] );
00306 
00307         $oSmarty->default_template_handler_func = array(oxUtilsView::getInstance(),'_smartyDefaultTemplateHandler');
00308 
00309         include_once dirname(__FILE__).'/smarty/plugins/prefilter.oxblock.php';
00310         $oSmarty->register_prefilter('smarty_prefilter_oxblock');
00311 
00312         $iDebug = $myConfig->getConfigParam( 'iDebug' );
00313         if (  $iDebug == 1 || $iDebug == 3 || $iDebug == 4 ) {
00314             $oSmarty->debugging = true;
00315         }
00316 
00317         //demoshop security
00318         if ( !$myConfig->isDemoShop() ) {
00319             $oSmarty->php_handling = (int) $myConfig->getConfigParam( 'iSmartyPhpHandling' );
00320             $oSmarty->security     = false;
00321         } else {
00322             $oSmarty->php_handling = SMARTY_PHP_REMOVE;
00323             $oSmarty->security     = true;
00324             $oSmarty->security_settings['IF_FUNCS'][] = 'XML_ELEMENT_NODE';
00325             $oSmarty->security_settings['MODIFIER_FUNCS'][] = 'round';
00326             $oSmarty->security_settings['MODIFIER_FUNCS'][] = 'floor';
00327             $oSmarty->security_settings['MODIFIER_FUNCS'][] = 'trim';
00328             $oSmarty->security_settings['MODIFIER_FUNCS'][] = 'implode';
00329             $oSmarty->security_settings['MODIFIER_FUNCS'][] = 'is_array';
00330             $oSmarty->security_settings['ALLOW_CONSTANTS'] = true;
00331             $oSmarty->secure_dir = $oSmarty->template_dir;
00332         }
00333 
00334 
00335     }
00336 
00344     protected function _smartyCompileCheck( $oSmarty )
00345     {
00346         $myConfig = $this->getConfig();
00347         $oSmarty->compile_check  = $myConfig->getConfigParam( 'blCheckTemplates' );
00348 
00349     }
00350 
00362     public function _smartyDefaultTemplateHandler($sResourceType, $sResourceName, $sResourceContent, $sResourceTimestamp, $oSmarty)
00363     {
00364         $myConfig = oxConfig::getInstance();
00365         if ( $sResourceType == 'file' && !is_readable($sResourceName) ) {
00366             $sResourceName      = $myConfig->getTemplatePath($sResourceName, $myConfig->isAdmin());
00367             $sResourceContent   = $oSmarty->_read_file($sResourceName);
00368             $sResourceTimestamp = filemtime($sResourceName);
00369             return is_file($sResourceName) && is_readable($sResourceName);
00370         }
00371         return false;
00372     }
00373 
00385     protected function _getTemplateBlock($sModule, $sFile)
00386     {
00387         $sFileName = $this->getConfig()->getConfigParam( 'sShopDir' )."/modules/$sModule/out/blocks/$sFile.tpl";
00388         if (file_exists($sFileName) && is_readable($sFileName)) {
00389             return file_get_contents($sFileName);
00390         } else {
00391             throw new oxException("Template block file ($sFileName) not found for '$sModule' module.");
00392         }
00393     }
00394 
00404     public function getTemplateBlocks($sFile)
00405     {
00406         $oConfig = $this->getConfig();
00407 
00408         $sTplDir = trim($oConfig->getConfigParam('_sTemplateDir'), '/\\');
00409         $sFile = str_replace(array('\\', '//'), '/', $sFile);
00410         if (preg_match('@/'.preg_quote($sTplDir, '@').'/(.*)$@', $sFile, $m)) {
00411             $sFile = $m[1];
00412         }
00413 
00414         $oDb = oxDb::getDb(true);
00415         $sFileParam = $oDb->quote($sFile);
00416         $sShpIdParam = $oDb->quote($oConfig->getShopId());
00417         $sSql = "select * from oxtplblocks where oxactive=1 and oxshopid=$sShpIdParam and oxtemplate=$sFileParam order by oxpos asc";
00418         $rs = $oDb->Execute($sSql);
00419         $aRet = array();
00420         if ($rs != false && $rs->recordCount() > 0) {
00421             while (!$rs->EOF) {
00422                 try {
00423                     if (!is_array($aRet[$rs->fields['OXBLOCKNAME']])) {
00424                         $aRet[$rs->fields['OXBLOCKNAME']] = array();
00425                     }
00426                     $aRet[$rs->fields['OXBLOCKNAME']][] = $this->_getTemplateBlock($rs->fields['OXMODULE'], $rs->fields['OXFILE']);
00427                 } catch (oxException $oE) {
00428                     $oE->debugOut();
00429                 }
00430 
00431                 $rs->moveNext();
00432             }
00433         }
00434         return $aRet;
00435     }
00436 }