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
00050 public static function getInstance()
00051 {
00052 return oxRegistry::get("oxUtilsView");
00053 }
00054
00064 public function getSmarty( $blReload = false )
00065 {
00066 if ( !self::$_oSmarty || $blReload ) {
00067 self::$_oSmarty = new Smarty();
00068 $this->_fillCommonSmartyProperties( self::$_oSmarty );
00069 $this->_smartyCompileCheck( self::$_oSmarty );
00070 }
00071
00072 return self::$_oSmarty;
00073 }
00074
00084 public function getTemplateOutput( $sTemplate, $oObject )
00085 {
00086 $oSmarty = $this->getSmarty();
00087 $iDebug = $this->getConfig()->getConfigParam( 'iDebug' );
00088
00089
00090 $aViewData = $oObject->getViewData();
00091 if ( is_array( $aViewData ) ) {
00092 foreach ( array_keys( $aViewData ) as $sViewName ) {
00093
00094 if ( $iDebug == 4 ) {
00095 echo( "TemplateData[$sViewName] : \n");
00096 var_export( $aViewData[$sViewName] );
00097 }
00098 $oSmarty->assign_by_ref( $sViewName, $aViewData[$sViewName] );
00099 }
00100 }
00101
00102 return $oSmarty->fetch( $sTemplate );
00103 }
00104
00113 public function passAllErrorsToView( &$aView, $aErrors )
00114 {
00115 if ( count( $aErrors ) > 0 ) {
00116 foreach ( $aErrors as $sLocation => $aEx2 ) {
00117 foreach ( $aEx2 as $sKey => $oEr ) {
00118 $aView['Errors'][$sLocation][$sKey] = unserialize( $oEr );
00119 }
00120 }
00121 }
00122 }
00123
00137 public function addErrorToDisplay( $oEr, $blFull = false, $blCustomDestination = false, $sCustomDestination = "", $sActiveController = "" )
00138 {
00139 if ( $blCustomDestination && ( oxConfig::getParameter( 'CustomError' ) || $sCustomDestination!= '' ) ) {
00140
00141 $sDestination = oxConfig::getParameter( 'CustomError' );
00142 if ( $sCustomDestination != '' ) {
00143 $sDestination = $sCustomDestination;
00144 }
00145 } else {
00146
00147 $sDestination = 'default';
00148 }
00149
00150
00151
00152 $oSession = $this->getSession();
00153 if ( !$oSession->getId() && !$oSession->isHeaderSent() ) {
00154 $oSession->setForceNewSession();
00155 $oSession->start();
00156 }
00157
00158 $aEx = oxSession::getVar( 'Errors' );
00159 if ( $oEr instanceof oxException ) {
00160 $oEx = oxNew( 'oxExceptionToDisplay' );
00161 $oEx->setMessage( $oEr->getMessage() );
00162 $oEx->setExceptionType( get_class( $oEr ) );
00163
00164 if ( $oEr instanceof oxSystemComponentException ) {
00165 $oEx->setMessageArgs( $oEr->getComponent() );
00166 }
00167
00168 $oEx->setValues( $oEr->getValues() );
00169 $oEx->setStackTrace( $oEr->getTraceAsString() );
00170 $oEx->setDebug( $blFull );
00171 $oEr = $oEx;
00172 } elseif ( $oEr && ! ( $oEr instanceof oxIDisplayError ) ) {
00173
00174 $sTmp = $oEr;
00175 $oEr = oxNew( 'oxDisplayError' );
00176 $oEr->setMessage( $sTmp );
00177 } elseif ( $oEr instanceof oxIDisplayError ) {
00178
00179 } else {
00180 $oEr = null;
00181 }
00182
00183 if ( $oEr ) {
00184 $aEx[$sDestination][] = serialize( $oEr );
00185 oxRegistry::getSession()->setVariable( 'Errors', $aEx );
00186
00187 if ( $sActiveController == '' ) {
00188 $sActiveController = oxRegistry::getConfig()->getRequestParameter( 'actcontrol' );
00189 }
00190 if ( $sActiveController ) {
00191 $aControllerErrors[$sDestination] = $sActiveController;
00192 oxRegistry::getSession()->setVariable( 'ErrorController', $aControllerErrors );
00193 }
00194 }
00195 }
00196
00209 public function parseThroughSmarty( $sDesc, $sOxid = null, $oActView = null, $blRecompile = false )
00210 {
00211 startProfile("parseThroughSmarty");
00212
00213 if (!is_array($sDesc) && strpos($sDesc, "[{") === false) {
00214 stopProfile("parseThroughSmarty");
00215 return $sDesc;
00216 }
00217
00218
00219 $iLang = oxRegistry::getLang()->getTplLanguage();
00220
00221
00222 $oSmarty = clone $this->getSmarty();
00223
00224
00225 $sTplVars = $oSmarty->_tpl_vars;
00226 $blForceRecompile = $oSmarty->force_compile;
00227
00228 $oSmarty->force_compile = $blRecompile;
00229
00230 if ( !$oActView ) {
00231 $oActView = oxNew( 'oxubase' );
00232 $oActView->addGlobalParams();
00233 }
00234
00235 $aViewData = $oActView->getViewData();
00236 foreach ( array_keys( $aViewData ) as $sName ) {
00237 $oSmarty->assign_by_ref( $sName, $aViewData[$sName] );
00238 }
00239
00240 if ( is_array( $sDesc ) ) {
00241 foreach ( $sDesc as $sName => $aData ) {
00242 $oSmarty->oxidcache = new oxField( $aData[1], oxField::T_RAW );
00243 $sRes[$sName] = $oSmarty->fetch( "ox:".$aData[0].$iLang );
00244 }
00245 } else {
00246 $oSmarty->oxidcache = new oxField($sDesc, oxField::T_RAW);
00247 $sRes = $oSmarty->fetch( "ox:{$sOxid}{$iLang}" );
00248 }
00249
00250
00251 $oSmarty->_tpl_vars = $sTplVars;
00252 $oSmarty->force_compile = $blForceRecompile;
00253
00254 stopProfile("parseThroughSmarty");
00255
00256 return $sRes;
00257 }
00258
00266 public function setTemplateDir( $sTplDir )
00267 {
00268 if ( $sTplDir && !in_array( $sTplDir, $this->_aTemplateDir ) ) {
00269 $this->_aTemplateDir[] = $sTplDir;
00270 }
00271 }
00272
00278 public function getTemplateDirs()
00279 {
00280 $myConfig = oxRegistry::getConfig();
00281
00282
00283
00284 $this->setTemplateDir( $myConfig->getTemplateDir( $this->isAdmin() ) );
00285
00286 if ( !$this->isAdmin() ) {
00287 $this->setTemplateDir( $myConfig->getOutDir( true ) . $myConfig->getConfigParam( 'sTheme' ) . "/tpl/" );
00288 }
00289
00290 return $this->_aTemplateDir;
00291 }
00292
00298 public function getTemplateCompileId()
00299 {
00300 $sShopId = $this->getConfig()->getShopId();
00301 $aDirs = $this->getTemplateDirs();
00302 $sDir = reset($aDirs);
00303 return md5( $sDir.'__'.$sShopId );
00304 }
00305
00311 public function getSmartyDir()
00312 {
00313 $myConfig = $this->getConfig();
00314
00315
00316 $sCompileDir = $myConfig->getConfigParam( 'sCompileDir' );
00317 $sSmartyDir = $sCompileDir . "/smarty/";
00318 if ( !is_dir( $sSmartyDir ) ) {
00319 @mkdir($sSmartyDir);
00320 }
00321
00322 if ( !is_writable($sSmartyDir) ) {
00323 $sSmartyDir = $sCompileDir;
00324 }
00325
00326 return $sSmartyDir;
00327 }
00328
00336 protected function _fillCommonSmartyProperties( $oSmarty )
00337 {
00338 $myConfig = $this->getConfig();
00339 $oSmarty->left_delimiter = '[{';
00340 $oSmarty->right_delimiter = '}]';
00341
00342 $oSmarty->register_resource( 'ox', array( 'ox_get_template',
00343 'ox_get_timestamp',
00344 'ox_get_secure',
00345 'ox_get_trusted' ) );
00346
00347 $sSmartyDir = $this->getSmartyDir();
00348
00349
00350 $oSmarty->caching = false;
00351 $oSmarty->compile_dir = $sSmartyDir;
00352 $oSmarty->cache_dir = $sSmartyDir;
00353 $oSmarty->template_dir = $this->getTemplateDirs();
00354 $oSmarty->compile_id = $this->getTemplateCompileId();
00355
00356 $oSmarty->default_template_handler_func = array(oxRegistry::get("oxUtilsView"),'_smartyDefaultTemplateHandler');
00357
00358 include_once dirname(__FILE__).'/smarty/plugins/prefilter.oxblock.php';
00359 $oSmarty->register_prefilter('smarty_prefilter_oxblock');
00360
00361 $iDebug = $myConfig->getConfigParam( 'iDebug' );
00362 if ( $iDebug == 1 || $iDebug == 3 || $iDebug == 4 ) {
00363 $oSmarty->debugging = true;
00364 }
00365
00366 if ($iDebug == 8 && !$myConfig->isAdmin()) {
00367 include_once getShopBasePath().'core/smarty/plugins/prefilter.oxtpldebug.php';
00368 $oSmarty->register_prefilter('smarty_prefilter_oxtpldebug');
00369 }
00370
00371
00372 if ( !$myConfig->isDemoShop() ) {
00373 $oSmarty->php_handling = (int) $myConfig->getConfigParam( 'iSmartyPhpHandling' );
00374 $oSmarty->security = false;
00375 } else {
00376 $oSmarty->php_handling = SMARTY_PHP_REMOVE;
00377 $oSmarty->security = true;
00378 $oSmarty->security_settings['IF_FUNCS'][] = 'XML_ELEMENT_NODE';
00379 $oSmarty->security_settings['MODIFIER_FUNCS'][] = 'round';
00380 $oSmarty->security_settings['MODIFIER_FUNCS'][] = 'floor';
00381 $oSmarty->security_settings['MODIFIER_FUNCS'][] = 'trim';
00382 $oSmarty->security_settings['MODIFIER_FUNCS'][] = 'implode';
00383 $oSmarty->security_settings['MODIFIER_FUNCS'][] = 'is_array';
00384 $oSmarty->security_settings['ALLOW_CONSTANTS'] = true;
00385 $oSmarty->secure_dir = $oSmarty->template_dir;
00386 }
00387 }
00388
00396 protected function _smartyCompileCheck( $oSmarty )
00397 {
00398 $myConfig = $this->getConfig();
00399 $oSmarty->compile_check = $myConfig->getConfigParam( 'blCheckTemplates' );
00400
00401 }
00402
00414 public function _smartyDefaultTemplateHandler($sResourceType, $sResourceName, &$sResourceContent, &$sResourceTimestamp, $oSmarty)
00415 {
00416 $myConfig = oxRegistry::getConfig();
00417 if ( $sResourceType == 'file' && !is_readable($sResourceName) ) {
00418 $sResourceName = $myConfig->getTemplatePath($sResourceName, $myConfig->isAdmin());
00419 $sResourceContent = $oSmarty->_read_file($sResourceName);
00420 $sResourceTimestamp = filemtime($sResourceName);
00421 return is_file($sResourceName) && is_readable($sResourceName);
00422 }
00423 return false;
00424 }
00425
00437 protected function _getTemplateBlock($sModule, $sFile)
00438 {
00439 $aModuleInfo = $this->_getActiveModuleInfo();
00440 $sModulePath = $aModuleInfo[$sModule];
00441
00442 if ( substr($sFile, -4) != '.tpl' ) {
00443 $sFile = $sFile . ".tpl";
00444 }
00445
00446 if ( basename($sFile) == $sFile ) {
00447 $sFile = "out/blocks/$sFile";
00448 }
00449 $sFileName = $this->getConfig()->getConfigParam( 'sShopDir' )."/modules/$sModulePath/$sFile";
00450 if ( file_exists($sFileName) && is_readable($sFileName) ) {
00451 return file_get_contents($sFileName);
00452 } else {
00453 throw oxNew( "oxException", "Template block file ($sFileName) not found for '$sModule' module." );
00454 }
00455 }
00456
00466 public function getTemplateBlocks($sFile)
00467 {
00468 $oConfig = $this->getConfig();
00469
00470 $sTplDir = trim($oConfig->getConfigParam('_sTemplateDir'), '/\\');
00471 $sFile = str_replace(array('\\', '//'), '/', $sFile);
00472 if (preg_match('@/'.preg_quote($sTplDir, '@').'/(.*)$@', $sFile, $m)) {
00473 $sFile = $m[1];
00474 }
00475
00476 $oDb = oxDb::getDb( oxDb::FETCH_MODE_ASSOC );
00477 $sFileParam = $oDb->quote($sFile);
00478 $sShpIdParam = $oDb->quote($oConfig->getShopId());
00479 $aRet = array();
00480 $aIds = array();
00481
00482 if ( $this->_blIsTplBlocks === null ) {
00483 $this->_blIsTplBlocks = false;
00484 $aIds = $this->_getActiveModuleInfo();
00485 if (count($aIds)) {
00486 $sSql = "select COUNT(*) from oxtplblocks where oxactive=1 and oxshopid=$sShpIdParam and oxmodule in ( " . implode(", ", oxDb::getInstance()->quoteArray(array_keys($aIds)) ) . " ) ";
00487 $rs = $oDb->getOne( $sSql );
00488 if ( $rs ) {
00489 $this->_blIsTplBlocks = true;
00490 }
00491 }
00492 }
00493
00494 if ( $this->_blIsTplBlocks ) {
00495 $aIds = $this->_getActiveModuleInfo();
00496 if (count($aIds)) {
00497 $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";
00498 $oDb->setFetchMode( oxDb::FETCH_MODE_ASSOC );
00499 $rs = $oDb->select( $sSql );
00500
00501 if ($rs != false && $rs->recordCount() > 0) {
00502 while (!$rs->EOF) {
00503 try {
00504 if (!is_array($aRet[$rs->fields['OXBLOCKNAME']])) {
00505 $aRet[$rs->fields['OXBLOCKNAME']] = array();
00506 }
00507 $aRet[$rs->fields['OXBLOCKNAME']][] = $this->_getTemplateBlock($rs->fields['OXMODULE'], $rs->fields['OXFILE']);
00508 } catch (oxException $oE) {
00509 $oE->debugOut();
00510 }
00511 $rs->moveNext();
00512 }
00513 }
00514 }
00515 }
00516
00517 return $aRet;
00518 }
00519
00525 protected function _getActiveModuleInfo()
00526 {
00527 if ($this->_aActiveModuleInfo === null) {
00528 $oModulelist = oxNew('oxmodulelist');
00529 $this->_aActiveModuleInfo = $oModulelist->getActiveModuleInfo();
00530 }
00531 return $this->_aActiveModuleInfo;
00532 }
00533
00534 }