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     protected $_aActiveModuleInfo = null;
00042 
00048     public static function getInstance()
00049     {
00050         // disable caching for test modules
00051         if ( defined( 'OXID_PHP_UNIT' ) ) {
00052             self::$_instance = modInstances::getMod( __CLASS__ );
00053         }
00054 
00055         if ( !self::$_instance instanceof oxUtilsView ) {
00056 
00057 
00058             self::$_instance = oxNew( 'oxUtilsView' );
00059 
00060             if ( defined( 'OXID_PHP_UNIT' ) ) {
00061                 modInstances::addMod( __CLASS__, self::$_instance);
00062             }
00063         }
00064         return self::$_instance;
00065     }
00066 
00076     public function getSmarty( $blReload = false )
00077     {
00078         if ( !self::$_oSmarty || $blReload ) {
00079             self::$_oSmarty = new Smarty();
00080             $this->_fillCommonSmartyProperties( self::$_oSmarty );
00081             $this->_smartyCompileCheck( self::$_oSmarty );
00082         }
00083 
00084         return self::$_oSmarty;
00085     }
00086 
00096     public function getTemplateOutput( $sTemplate, $oObject )
00097     {
00098         $oSmarty = $this->getSmarty();
00099         $iDebug  = $this->getConfig()->getConfigParam( 'iDebug' );
00100 
00101         // assign
00102         $aViewData = $oObject->getViewData();
00103         if ( is_array( $aViewData ) ) {
00104             foreach ( array_keys( $aViewData ) as $sViewName ) {
00105                 // show debbuging information
00106                 if ( $iDebug == 4 ) {
00107                     echo( "TemplateData[$sViewName] : \n");
00108                     print_r( $aViewData[$sViewName] );
00109                 }
00110                 $oSmarty->assign_by_ref( $sViewName, $aViewData[$sViewName] );
00111             }
00112         }
00113 
00114         return $oSmarty->fetch( $sTemplate );
00115     }
00116 
00125     public function passAllErrorsToView( &$aView, $aErrors )
00126     {
00127         if ( count( $aErrors ) > 0 ) {
00128             foreach ( $aErrors as $sLocation => $aEx2 ) {
00129                 foreach ( $aEx2 as $sKey => $oEr ) {
00130                     $aView['Errors'][$sLocation][$sKey] = unserialize( $oEr );
00131                 }
00132             }
00133         }
00134     }
00135 
00148     public function addErrorToDisplay( $oEr, $blFull = false, $blCustomDestination = false, $sCustomDestination = "" )
00149     {
00150         if ( $blCustomDestination && ( oxConfig::getParameter( 'CustomError' ) || $sCustomDestination!= '' ) ) {
00151             // check if the current request wants do display exceptions on its own
00152             $sDestination = oxConfig::getParameter( 'CustomError' );
00153             if ( $sCustomDestination != '' ) {
00154                 $sDestination = $sCustomDestination;
00155             }
00156         } else {
00157             //default
00158             $sDestination = 'default';
00159         }
00160 
00161         //starting session if not yet started as all exception
00162         //messages are stored in session
00163         $oSession = $this->getSession();
00164         if ( !$oSession->getId() && !$oSession->isHeaderSent() ) {
00165             $oSession->setForceNewSession();
00166             $oSession->start();
00167         }
00168 
00169         $aEx = oxSession::getVar( 'Errors' );
00170         if ( $oEr instanceof oxException ) {
00171              $oEx = oxNew( 'oxExceptionToDisplay' );
00172              $oEx->setMessage( $oEr->getMessage() );
00173              $oEx->setExceptionType( get_class( $oEr ) );
00174 
00175              if ( $oEr instanceof oxSystemComponentException ) {
00176                 $oEx->setMessageArgs( $oEr->getComponent() );
00177              }
00178 
00179              $oEx->setValues( $oEr->getValues() );
00180              $oEx->setStackTrace( $oEr->getTraceAsString() );
00181              $oEx->setDebug( $blFull );
00182              $oEr = $oEx;
00183         } elseif ( $oEr && ! ( $oEr instanceof oxIDisplayError ) ) {
00184             // assuming that a string was given
00185             $sTmp = $oEr;
00186             $oEr = oxNew( 'oxDisplayError' );
00187             $oEr->setMessage( $sTmp );
00188         } elseif ( $oEr instanceof oxIDisplayError ) {
00189             // take the object
00190         } else {
00191             $oEr = null;
00192         }
00193 
00194         if ( $oEr ) {
00195             $aEx[$sDestination][] = serialize( $oEr );
00196             oxSession::setVar( 'Errors', $aEx );
00197         }
00198     }
00199 
00212     public function parseThroughSmarty( $sDesc, $sOxid = null, $oActView = null, $blRecompile = false )
00213     {
00214         startProfile("parseThroughSmarty");
00215 
00216         if (!is_array($sDesc) && strpos($sDesc, "[{") === false) {
00217             stopProfile("parseThroughSmarty");
00218             return $sDesc;
00219         }
00220 
00221 
00222         $iLang = oxLang::getInstance()->getTplLanguage();
00223 
00224         // now parse it through smarty
00225         $oSmarty = clone $this->getSmarty();
00226 
00227         // save old tpl data
00228         $sTplVars = $oSmarty->_tpl_vars;
00229         $blForceRecompile = $oSmarty->force_compile;
00230 
00231         $oSmarty->force_compile = $blRecompile;
00232 
00233         if ( !$oActView ) {
00234             $oActView = oxNew( 'oxubase' );
00235             $oActView->addGlobalParams();
00236         }
00237 
00238         $aViewData = $oActView->getViewData();
00239         foreach ( array_keys( $aViewData ) as $sName ) {
00240             $oSmarty->assign_by_ref( $sName, $aViewData[$sName] );
00241         }
00242 
00243         if ( is_array( $sDesc ) ) {
00244             foreach ( $sDesc as $sName => $aData ) {
00245                 $oSmarty->oxidcache = new oxField( $aData[1], oxField::T_RAW );
00246                 $sRes[$sName] = $oSmarty->fetch( "ox:".$aData[0].$iLang );
00247             }
00248         } else {
00249             $oSmarty->oxidcache = new oxField($sDesc, oxField::T_RAW);
00250             $sRes = $oSmarty->fetch( "ox:{$sOxid}{$iLang}" );
00251         }
00252 
00253         // restore tpl vars for continuing smarty processing if it is in one
00254         $oSmarty->_tpl_vars = $sTplVars;
00255         $oSmarty->force_compile = $blForceRecompile;
00256 
00257         stopProfile("parseThroughSmarty");
00258 
00259         return $sRes;
00260     }
00261 
00269     public function setTemplateDir( $sTplDir )
00270     {
00271         if ( $sTplDir && !in_array( $sTplDir, $this->_aTemplateDir ) ) {
00272             $this->_aTemplateDir[] = $sTplDir;
00273         }
00274     }
00275 
00281     public function getTemplateDirs()
00282     {
00283         $myConfig = $this->getConfig();
00284 
00285         //T2010-01-13
00286         //#1531
00287         $this->setTemplateDir( $myConfig->getTemplateDir( $this->isAdmin() ) );
00288 
00289         if ( !$this->isAdmin() ) {
00290             $this->setTemplateDir( $myConfig->getOutDir( true ) . $myConfig->getConfigParam( 'sTheme' ) . "/tpl/" );
00291         }
00292 
00293         return $this->_aTemplateDir;
00294     }
00295 
00301     public function getTemplateCompileId()
00302     {
00303         $sShopId = $this->getConfig()->getShopId();
00304         $aDirs   = $this->getTemplateDirs();
00305         $sDir    = reset($aDirs);
00306         return md5( $sDir.'__'.$sShopId );
00307     }
00308 
00316     protected function _fillCommonSmartyProperties( $oSmarty )
00317     {
00318         $myConfig = $this->getConfig();
00319         $oSmarty->left_delimiter  = '[{';
00320         $oSmarty->right_delimiter = '}]';
00321 
00322         $oSmarty->register_resource( 'ox', array( 'ox_get_template',
00323                                                   'ox_get_timestamp',
00324                                                   'ox_get_secure',
00325                                                   'ox_get_trusted' ) );
00326 
00327         // $myConfig->blTemplateCaching; // DODGER #655 : permanently switched off as it doesnt work good enough
00328         $oSmarty->caching      = false;
00329         $oSmarty->compile_dir  = $myConfig->getConfigParam( 'sCompileDir' );
00330         $oSmarty->cache_dir    = $myConfig->getConfigParam( 'sCompileDir' );
00331         $oSmarty->template_dir = $this->getTemplateDirs();
00332         $oSmarty->compile_id   = $this->getTemplateCompileId();
00333 
00334         $oSmarty->default_template_handler_func = array(oxUtilsView::getInstance(),'_smartyDefaultTemplateHandler');
00335 
00336         include_once dirname(__FILE__).'/smarty/plugins/prefilter.oxblock.php';
00337         $oSmarty->register_prefilter('smarty_prefilter_oxblock');
00338 
00339         $iDebug = $myConfig->getConfigParam( 'iDebug' );
00340         if (  $iDebug == 1 || $iDebug == 3 || $iDebug == 4 ) {
00341             $oSmarty->debugging = true;
00342         }
00343 
00344         if ($iDebug == 8 && !$myConfig->isAdmin()) {
00345             include_once getShopBasePath().'core/smarty/plugins/prefilter.oxtpldebug.php';
00346             $oSmarty->register_prefilter('smarty_prefilter_oxtpldebug');
00347         }
00348 
00349         //demoshop security
00350         if ( !$myConfig->isDemoShop() ) {
00351             $oSmarty->php_handling = (int) $myConfig->getConfigParam( 'iSmartyPhpHandling' );
00352             $oSmarty->security     = false;
00353         } else {
00354             $oSmarty->php_handling = SMARTY_PHP_REMOVE;
00355             $oSmarty->security     = true;
00356             $oSmarty->security_settings['IF_FUNCS'][] = 'XML_ELEMENT_NODE';
00357             $oSmarty->security_settings['MODIFIER_FUNCS'][] = 'round';
00358             $oSmarty->security_settings['MODIFIER_FUNCS'][] = 'floor';
00359             $oSmarty->security_settings['MODIFIER_FUNCS'][] = 'trim';
00360             $oSmarty->security_settings['MODIFIER_FUNCS'][] = 'implode';
00361             $oSmarty->security_settings['MODIFIER_FUNCS'][] = 'is_array';
00362             $oSmarty->security_settings['ALLOW_CONSTANTS'] = true;
00363             $oSmarty->secure_dir = $oSmarty->template_dir;
00364         }
00365     }
00366 
00374     protected function _smartyCompileCheck( $oSmarty )
00375     {
00376         $myConfig = $this->getConfig();
00377         $oSmarty->compile_check  = $myConfig->getConfigParam( 'blCheckTemplates' );
00378 
00379     }
00380 
00392     public function _smartyDefaultTemplateHandler($sResourceType, $sResourceName, $sResourceContent, $sResourceTimestamp, $oSmarty)
00393     {
00394         $myConfig = oxConfig::getInstance();
00395         if ( $sResourceType == 'file' && !is_readable($sResourceName) ) {
00396             $sResourceName      = $myConfig->getTemplatePath($sResourceName, $myConfig->isAdmin());
00397             $sResourceContent   = $oSmarty->_read_file($sResourceName);
00398             $sResourceTimestamp = filemtime($sResourceName);
00399             return is_file($sResourceName) && is_readable($sResourceName);
00400         }
00401         return false;
00402     }
00403 
00415     protected function _getTemplateBlock($sModule, $sFile)
00416     {
00417         $aModuleInfo = $this->_getActiveModuleInfo();
00418         $sModulePath = $aModuleInfo[$sModule];
00419         // for 4.5 modules, since 4.6 insert in oxtplblocks the full file name
00420         if (substr($sFile, -4) != '.tpl') {
00421             $sFile = $sFile . ".tpl";
00422         }
00423         $sFileName   = $this->getConfig()->getConfigParam( 'sShopDir' )."/modules/$sModulePath/out/blocks/$sFile";
00424         if (file_exists($sFileName) && is_readable($sFileName)) {
00425             return file_get_contents($sFileName);
00426         } else {
00427             throw oxNew( "oxException", "Template block file ($sFileName) not found for '$sModule' module." );
00428         }
00429     }
00430 
00440     public function getTemplateBlocks($sFile)
00441     {
00442         $oConfig = $this->getConfig();
00443 
00444         $sTplDir = trim($oConfig->getConfigParam('_sTemplateDir'), '/\\');
00445         $sFile = str_replace(array('\\', '//'), '/', $sFile);
00446         if (preg_match('@/'.preg_quote($sTplDir, '@').'/(.*)$@', $sFile, $m)) {
00447             $sFile = $m[1];
00448         }
00449 
00450         $oDb = oxDb::getDb( oxDb::FETCH_MODE_ASSOC );
00451         $sFileParam = $oDb->quote($sFile);
00452         $sShpIdParam = $oDb->quote($oConfig->getShopId());
00453         $aRet = array();
00454         $aIds = array();
00455 
00456         if ( $this->_blIsTplBlocks === null ) {
00457             $this->_blIsTplBlocks = false;
00458             $aIds = $this->_getActiveModuleInfo();
00459             if (count($aIds)) {
00460                 $sSql = "select COUNT(*) from oxtplblocks where oxactive=1 and oxshopid=$sShpIdParam and oxmodule in ( " . implode(", ", oxDb::getInstance()->quoteArray(array_keys($aIds)) ) . " ) ";
00461                 $rs = $oDb->getOne( $sSql );
00462                 if ( $rs ) {
00463                     $this->_blIsTplBlocks = true;
00464                 }
00465             }
00466         }
00467 
00468         if ( $this->_blIsTplBlocks ) {
00469             $aIds = $this->_getActiveModuleInfo();
00470             if (count($aIds)) {
00471                 $sSql = "select * from oxtplblocks where oxactive=1 and oxshopid=$sShpIdParam and oxtemplate=$sFileParam and oxmodule in ( " . implode(", ", oxDb::getInstance()->quoteArray(array_keys($aIds)) ) . " ) order by oxpos asc";
00472                 $oDb->setFetchMode( oxDb::FETCH_MODE_ASSOC );
00473                 $rs = $oDb->select( $sSql );
00474 
00475                 if ($rs != false && $rs->recordCount() > 0) {
00476                     while (!$rs->EOF) {
00477                         try {
00478                             if (!is_array($aRet[$rs->fields['OXBLOCKNAME']])) {
00479                                 $aRet[$rs->fields['OXBLOCKNAME']] = array();
00480                             }
00481                             $aRet[$rs->fields['OXBLOCKNAME']][] = $this->_getTemplateBlock($rs->fields['OXMODULE'], $rs->fields['OXFILE']);
00482                         } catch (oxException $oE) {
00483                             $oE->debugOut();
00484                         }
00485                         $rs->moveNext();
00486                     }
00487                 }
00488             }
00489         }
00490 
00491         return $aRet;
00492     }
00493 
00499     protected function _getActiveModuleInfo()
00500     {
00501         if ($this->_aActiveModuleInfo === null) {
00502             $oModulelist = oxNew('oxmodulelist');
00503             $this->_aActiveModuleInfo = $oModulelist->getActiveModuleInfo();
00504         }
00505         return $this->_aActiveModuleInfo;
00506     }
00507 
00508 }