oxajax.php

Go to the documentation of this file.
00001 <?php
00002 
00003 // shop path for includes
00004 if ( !function_exists( 'getShopBasePath' )) {
00010     function getShopBasePath()
00011     {
00012         return dirname(__FILE__).'/../';
00013     }
00014 }
00015 
00016 //to load the session correctly
00017 if ( !function_exists( 'isAdmin' )) {
00023     function isAdmin()
00024     {
00025         return true;
00026     }
00027 }
00028 
00029 require_once getShopBasePath() . 'core/oxsupercfg.php';
00030 
00034 class ajaxListComponent extends oxSuperCfg
00035 {
00041     protected $_aPosDir = array( 'asc', 'desc' );
00042 
00048     protected $_aColumns = array();
00049 
00055     protected $_iSqlLimit = 2500;
00056 
00062     protected $_sContainer = null;
00063 
00071     public function init( $aColumns )
00072     {
00073         $this->_aColumns = $aColumns;
00074     }
00075 
00085     protected function _getActionIds( $sId )
00086     {
00087         $aColumns = $this->_getColNames();
00088         foreach ( $aColumns as $iPos => $aCol ) {
00089             if ( isset($aCol[4]) && $aCol[4] == 1 && $sId == $aCol[1].'.'.$aCol[0] ) {
00090                 return oxConfig::getParameter( '_'.$iPos );
00091             }
00092         }
00093     }
00094 
00102     public function setName( $sName )
00103     {
00104         $this->_sContainer = $sName;
00105     }
00106 
00112     protected function _getQuery()
00113     {
00114         return '';
00115     }
00116 
00124     protected function _getDataQuery( $sQ )
00125     {
00126         return 'select ' . $this->_getQueryCols() . $sQ;
00127     }
00128 
00136     protected function _getCountQuery( $sQ )
00137     {
00138         return 'select count( * ) ' . $sQ;
00139     }
00140 
00148     public function processRequest( $sFunction = null )
00149     {
00150         if ( $sFunction ) {
00151             $this->$sFunction();
00152         } else {
00153             $sQAdd = $this->_getQuery();
00154 
00155             // formatting SQL queries
00156             $sQ      = $this->_getDataQuery( $sQAdd );
00157             $sCountQ = $this->_getCountQuery( $sQAdd );
00158 
00159             $this->_outputResponse( $this->_getData( $sCountQ, $sQ ) );
00160         }
00161     }
00162 
00168     protected function _getSortCol()
00169     {
00170         $aVisibleNames = $this->_getVisibleColNames();
00171         $iCol = oxConfig::getParameter( 'sort' );
00172         $iCol = $iCol?( ( int ) str_replace( '_', '', $iCol )):0;
00173         $iCol = ( !isset( $aVisibleNames[$iCol] ) )?0:$iCol;
00174 
00175         return $iCol;
00176     }
00177 
00178 
00187     protected function _getColNames( $sId = null )
00188     {
00189         if ( $sId === null ) {
00190             $sId = oxConfig::getParameter( 'cmpid' );
00191         }
00192 
00193         if ( $sId && isset( $this->_aColumns[$sId] ) ) {
00194             return $this->_aColumns[$sId];
00195         }
00196 
00197         return $this->_aColumns;
00198     }
00199 
00206     protected function _getIdentColNames()
00207     {
00208         $aColNames = $this->_getColNames();
00209         $aCols = array();
00210         foreach ( $aColNames as $iKey =>  $aCol ) {
00211             if ( $aCol[4] ) // ident ?
00212                 $aCols[$iKey] = $aCol;
00213         }
00214         return $aCols;
00215     }
00216 
00222     protected function _getVisibleColNames()
00223     {
00224         $aColNames = $this->_getColNames();
00225         $aUserCols = oxConfig::getParameter( 'aCols' );
00226         $aVisibleCols = array();
00227 
00228         // user defined some cols to load ?
00229         if ( is_array( $aUserCols ) ) {
00230             foreach ( $aUserCols as $iKey => $sCol ) {
00231                 $iCol = ( int ) str_replace( '_', '', $sCol );
00232                 if ( isset( $aColNames[$iCol] ) && !$aColNames[$iCol][4] ) {
00233                     $aVisibleCols[$iCol] = $aColNames[$iCol];
00234                 }
00235             }
00236         }
00237 
00238         // no user defined valid cols ? setting defauls ..
00239         if ( !count( $aVisibleCols ) ) {
00240             foreach ( $aColNames as $sName => $aCol ) {
00241                 if ( $aCol[1] && !$aColNames[$sName][4]  ) // visible ?
00242                     $aVisibleCols[$sName] = $aCol;
00243             }
00244         }
00245 
00246         return $aVisibleCols;
00247     }
00248 
00255     protected function _getQueryCols()
00256     {
00257         $sLangTag = oxLang::getInstance()->getLanguageTag();
00258         $sQ = '';
00259         $blSep = false;
00260         $aVisiblecols = $this->_getVisibleColNames();
00261         foreach ( $aVisiblecols as $iCnt => $aCol ) {
00262             if ( $blSep )
00263                 $sQ .= ', ';
00264 
00265             // multijanguage
00266             $sCol = $aCol[3]?$aCol[0].$sLangTag:$aCol[0];
00267             $sQ  .= getViewName( $aCol[1] ) . '.' . $sCol . ' as _' . $iCnt;
00268             $blSep = true;
00269         }
00270 
00271         $aIdentCols = $this->_getIdentColNames();
00272         foreach ( $aIdentCols as $iCnt => $aCol ) {
00273             if ( $blSep )
00274                 $sQ .= ', ';
00275 
00276             // multijanguage
00277             $sCol = $aCol[3]?$aCol[0].$sLangTag:$aCol[0];
00278             $sQ  .= getViewName( $aCol[1] ) . '.' . $sCol . ' as _' . $iCnt;
00279         }
00280 
00281         return " $sQ ";
00282     }
00283 
00289     protected function _getSorting()
00290     {
00291         return ' order by _' . $this->_getSortCol() . ' '.$this->_getSortDir().' ';
00292     }
00293 
00301     protected function _getLimit( $iStart )
00302     {
00303         $iLimit = (int) oxConfig::getParameter("results");
00304         $iLimit = $iLimit?$iLimit:$this->_iSqlLimit;
00305 
00306         return " limit $iStart, $iLimit ";
00307     }
00308 
00314     protected function _getFilter()
00315     {
00316         $sQ = '';
00317         $aFilter = oxConfig::getParameter( 'aFilter' );
00318         if ( is_array( $aFilter ) && count( $aFilter ) ) {
00319             $aCols = $this->_getVisibleColNames();
00320             $blSep = false;
00321             $oDb = oxDb::getDb();
00322             $oLang = oxLang::getInstance();
00323             $oStr = getStr();
00324 
00325             $sLangTag = $oLang->getLanguageTag();
00326             $blIsUtf  = $this->getConfig()->isUtf();
00327             $sCharset = $oLang->translateString( "charset" );
00328 
00329             foreach ( $aFilter as $sCol => $sValue ) {
00330 
00331                 // skipping empty filters
00332                 if ( !$sValue ) {
00333                     continue;
00334                 }
00335 
00336                 $iCol = (int) str_replace( '_', '', $sCol );
00337                 if ( isset( $aCols[ $iCol ] ) ) {
00338                     if ( $sQ )
00339                         $sQ .= ' and ';
00340 
00341                     if ( !$blIsUtf ) {
00342                         $sValue = iconv( 'UTF-8', $sCharset, $sValue );
00343                     }
00344 
00345                     // escaping special characters
00346                     $sValue = str_replace( array( '%', '_' ), array( '\%', '\_' ), $sValue );
00347 
00348                     // possibility to search in the middle ..
00349                     $sValue = $oStr->preg_replace( '/^\*/', '%', $sValue );
00350 
00351                     $sCol = $aCols[ $iCol ][3]?$aCols[ $iCol ][0].$sLangTag:$aCols[ $iCol ][0];
00352                     $sQ .= getViewName( $aCols[ $iCol ][1] ) . '.' . $sCol;
00353                     $sQ .= ' like ' . $oDb->Quote( $sValue . '%' ). ' ';
00354                 }
00355 
00356             }
00357         }
00358         return $sQ;
00359     }
00360 
00368     protected function _addFilter( $sQ )
00369     {
00370         if ( $sQ && ( $sFilter = $this->_getFilter() ) ) {
00371             $sQ .= ( ( stristr( $sQ, 'where' ) === false )?'where':' and ' ) . $sFilter;
00372         }
00373         return $sQ;
00374     }
00375 
00383     protected function _getAll( $sQ )
00384     {
00385         $aReturn = array();
00386         $rs = oxDb::getDb()->execute( $sQ );
00387         if ($rs != false && $rs->recordCount() > 0) {
00388             while (!$rs->EOF) {
00389                 $aReturn[] = $rs->fields[0];
00390                 $rs->moveNext();
00391             }
00392         }
00393         return $aReturn;
00394     }
00395 
00401     protected function _getSortDir()
00402     {
00403         $sDir = oxConfig::getParameter( 'dir' );
00404         if ( !in_array( $sDir, $this->_aPosDir ) ) {
00405             $sDir = $this->_aPosDir[0];
00406         }
00407 
00408         return $sDir;
00409     }
00410 
00416     protected function _getStartIndex()
00417     {
00418         return (int) oxConfig::getParameter( 'startIndex' );
00419     }
00420 
00428     protected function _getTotalCount( $sQ )
00429     {
00430         // TODO: implement caching here
00431 
00432         // we can cache total count ...
00433 
00434         // $sCountCacheKey = md5( $sQ );
00435 
00436         return (int) oxDb::getDb()->getOne( $sQ );
00437     }
00438 
00446     protected function _getDataFields( $sQ )
00447     {
00448         return oxDb::getDb(true)->getArray( $sQ );
00449     }
00450 
00458     protected function _outputResponse( $aData )
00459     {
00460         if ( !$this->getConfig()->isUtf() ) {
00461             // TODO: improve this
00462             if ( is_array( $aData['records'] ) && ( $iRecSize = count( $aData['records'] ) ) ) {
00463                 $aKeys = array_keys( current( $aData['records'] ) );
00464                 $iKeySize = count( $aKeys );
00465                 $sCharset = oxLang::getInstance()->translateString("charset");
00466                 for ( $i = 0; $i < $iRecSize; $i++ ) {
00467                     for ( $c = 0; $c < $iKeySize; $c++ ) {
00468                         $aData['records'][$i][$aKeys[$c]] = iconv( $sCharset, "UTF-8", $aData['records'][$i][$aKeys[$c]] );
00469                     }
00470                 }
00471             }
00472         }
00473 
00474         $this->_output( json_encode( $aData ) );
00475     }
00476 
00484     protected function _output( $sOut )
00485     {
00486         echo $sOut;
00487     }
00488 
00497     protected function _getData( $sCountQ, $sQ )
00498     {
00499         $sQ = $this->_addFilter( $sQ );
00500         $sCountQ = $this->_addFilter( $sCountQ );
00501 
00502         $aResponse['startIndex'] = $iStart = $this->_getStartIndex();
00503         $aResponse['sort'] = '_' . $this->_getSortCol();
00504         $aResponse['dir']  = $this->_getSortDir();
00505 
00506         $iDebug = $this->getConfig()->getConfigParam( 'iDebug' );
00507         if ( $iDebug ) {
00508             $aResponse['countsql'] = $sCountQ;
00509         }
00510 
00511         $aResponse['records'] = array();
00512 
00513         // skip further execution if no records were found ...
00514         if ( ( $iTotal = $this->_getTotalCount( $sCountQ ) ) ) {
00515             $sQ .= $this->_getSorting();
00516             $sQ .= $this->_getLimit( $iStart );
00517 
00518             if ( $iDebug ) {
00519                 $aResponse['datasql'] = $sQ;
00520             }
00521 
00522             $aResponse['records'] = $this->_getDataFields( $sQ );
00523         }
00524 
00525         $aResponse['totalRecords'] = $iTotal;
00526 
00527         return $aResponse;
00528     }
00529 
00537     public function resetArtSeoUrl( $aArtIds )
00538     {
00539         if ( empty( $aArtIds ) ) {
00540             return;
00541         }
00542 
00543         if ( !is_array( $aArtIds ) ) {
00544             $aArtIds = array( $aArtIds );
00545         }
00546 
00547         $sShopId = $this->getConfig()->getShopId();
00548         foreach ( $aArtIds as $sArtId ) {
00549            oxSeoEncoder::getInstance()->markAsExpired( $sArtId, $sShopId, 1, null, "oxtype='oxarticle'" );
00550         }
00551     }
00552 
00558     public function resetContentCache()
00559     {
00560         $blDeleteCacheOnLogout = $this->getConfig()->getConfigParam( 'blClearCacheOnLogout' );
00561 
00562 
00563             if ( !$blDeleteCacheOnLogout ) {
00564                 oxUtils::getInstance()->oxResetFileCache();
00565             }
00566     }
00567 
00577     public function resetCounter( $sCounterType, $sValue = null )
00578     {
00579         $blDeleteCacheOnLogout = $this->getConfig()->getConfigParam( 'blClearCacheOnLogout' );
00580 
00581         if ( !$blDeleteCacheOnLogout ) {
00582             $myUtilsCount = oxUtilsCount::getInstance();
00583             switch ( $sCounterType ) {
00584                 case 'priceCatArticle':
00585                     $myUtilsCount->resetPriceCatArticleCount( $sValue );
00586                     break;
00587                 case 'catArticle':
00588                     $myUtilsCount->resetCatArticleCount( $sValue );
00589                     break;
00590                 case 'vendorArticle':
00591                     $myUtilsCount->resetVendorArticleCount( $sValue );
00592                     break;
00593                 case 'manufacturerArticle':
00594                     $myUtilsCount->resetManufacturerArticleCount( $sValue );
00595                     break;
00596             }
00597         }
00598     }
00599 
00600 }
00601 
00602 // processing ..
00603 $blAjaxCall = (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest');
00604 if ( $blAjaxCall ) {
00605 
00606 
00607     // Setting error reporting mode
00608     error_reporting( E_ALL ^ E_NOTICE);
00609 
00610     //setting basic configuration parameters
00611     ini_set('session.name', 'sid' );
00612     ini_set('session.use_cookies', 0 );
00613     ini_set('session.use_trans_sid', 0);
00614     ini_set('url_rewriter.tags', '');
00615     ini_set('magic_quotes_runtime', 0);
00616 
00617     // Generic utility method file.
00618     $sBasePath = getShopBasePath();
00619     include_once $sBasePath . 'modules/functions.php';
00620     include_once $sBasePath . 'core/oxfunctions.php';
00621     include_once $sBasePath . 'core/adodblite/adodb.inc.php';
00622     include_once $sBasePath . 'core/oxconfig.php';
00623     include_once $sBasePath . 'core/oxsupercfg.php';
00624 
00625 
00626     include_once $sBasePath . "core/oxutils.php";
00627 
00628     $myConfig = oxConfig::getInstance();
00629 
00630     // Includes Utility module.
00631     $sUtilModule = $myConfig->getConfigParam( 'sUtilModule' );
00632     if ( $sUtilModule && file_exists( getShopBasePath()."modules/".$sUtilModule ) )
00633         include_once getShopBasePath()."modules/".$sUtilModule;
00634 
00635     $myConfig->setConfigParam( 'blAdmin', true );
00636     $myConfig->setConfigParam( 'blTemplateCaching', false );
00637 
00638     // authorization
00639     if ( !(oxSession::getInstance()->checkSessionChallenge() && count(oxUtilsServer::getInstance()->getOxCookie()) && oxUtils::getInstance()->checkAccessRights())) {
00640         header( "location:index.php");
00641         oxUtils::getInstance()->showMessageAndExit( "" );
00642     }
00643 
00644     if ( $sContainer = oxConfig::getParameter( 'container' ) ) {
00645 
00646         $sContainer = trim(strtolower( basename( $sContainer ) ));
00647         $aColumns = array();
00648 
00649         include_once 'inc/'.$sContainer.'.inc.php';
00650 
00651         //$oAjaxComponent = new ajaxcomponent( $aColumns );
00652         $oAjaxComponent = oxNew("ajaxcomponent");
00653         $oAjaxComponent->init($aColumns);
00654 
00655         $oAjaxComponent->setName( $sContainer );
00656         $oAjaxComponent->processRequest( oxConfig::getParameter( 'fnc' ) );
00657 
00658     } else {
00659 
00660     }
00661 
00662     $myConfig->pageClose();
00663 
00664     // closing session handlers
00665     // session_write_close();
00666     return;
00667 }