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 include_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 
00064     public function __construct( $aColumns )
00065     {
00066         $this->_aColumns = $aColumns;
00067     }
00068 
00078     protected function _getActionIds( $sId )
00079     {
00080         $aColumns = $this->_getColNames();
00081         foreach ( $aColumns as $iPos => $aCol ) {
00082             if ( isset($aCol[4]) && $aCol[4] == 1 && $sId == $aCol[1].'.'.$aCol[0] ) {
00083                 return oxConfig::getParameter( '_'.$iPos );
00084             }
00085         }
00086     }
00087 
00095     public function setName( $sName )
00096     {
00097         $this->sContainer = $sName;
00098     }
00099 
00105     protected function _getQuery()
00106     {
00107         return '';
00108     }
00109 
00117     protected function _getDataQuery( $sQ )
00118     {
00119         return 'select ' . $this->_getQueryCols() . $sQ;
00120     }
00121 
00129     protected function _getCountQuery( $sQ )
00130     {
00131         return 'select count( * ) ' . $sQ;
00132     }
00133 
00141     public function processRequest( $sFunction = null )
00142     {
00143         if ( $sFunction ) {
00144             $this->$sFunction();
00145         } else {
00146             $sQAdd = $this->_getQuery();
00147 
00148             // formatting SQL queries
00149             $sQ      = $this->_getDataQuery( $sQAdd );
00150             $sCountQ = $this->_getCountQuery( $sQAdd );
00151 
00152             $this->_outputResponse( $this->_getData( $sCountQ, $sQ ) );
00153         }
00154     }
00155 
00161     protected function _getSortCol()
00162     {
00163         $aVisibleNames = $this->_getVisibleColNames();
00164         $iCol = oxConfig::getParameter( 'sort' );
00165         $iCol = $iCol?( ( int ) str_replace( '_', '', $iCol )):0;
00166         $iCol = ( !isset( $aVisibleNames[$iCol] ) )?0:$iCol;
00167 
00168         return $iCol;
00169     }
00170 
00171 
00180     protected function _getColNames( $sId = null )
00181     {
00182         if ( $sId === null ) {
00183             $sId = oxConfig::getParameter( 'cmpid' );
00184         }
00185 
00186         if ( $sId && isset( $this->_aColumns[$sId] ) ) {
00187             return $this->_aColumns[$sId];
00188         }
00189 
00190         return $this->_aColumns;
00191     }
00192 
00199     protected function _getIdentColNames()
00200     {
00201         $aColNames = $this->_getColNames();
00202         $aCols = array();
00203         foreach ( $aColNames as $iKey =>  $aCol ) {
00204             if ( $aCol[4] ) // ident ?
00205                 $aCols[$iKey] = $aCol;
00206         }
00207         return $aCols;
00208     }
00209 
00215     protected function _getVisibleColNames()
00216     {
00217         $aColNames = $this->_getColNames();
00218         $aUserCols = oxConfig::getParameter( 'aCols' );
00219         $aVisibleCols = array();
00220 
00221         // user defined some cols to load ?
00222         if ( is_array( $aUserCols ) ) {
00223             foreach ( $aUserCols as $iKey => $sCol ) {
00224                 $iCol = ( int ) str_replace( '_', '', $sCol );
00225                 if ( isset( $aColNames[$iCol] ) && !$aColNames[$iCol][4] ) {
00226                     $aVisibleCols[$iCol] = $aColNames[$iCol];
00227                 }
00228             }
00229         }
00230 
00231         // no user defined valid cols ? setting defauls ..
00232         if ( !count( $aVisibleCols ) ) {
00233             foreach ( $aColNames as $sName => $aCol ) {
00234                 if ( $aCol[1] && !$aColNames[$sName][4]  ) // visible ?
00235                     $aVisibleCols[$sName] = $aCol;
00236             }
00237         }
00238 
00239         return $aVisibleCols;
00240     }
00241 
00248     protected function _getQueryCols()
00249     {
00250         $sLangTag = oxLang::getInstance()->getLanguageTag();
00251         $sQ = '';
00252         $blSep = false;
00253         $aVisiblecols = $this->_getVisibleColNames();
00254         foreach ( $aVisiblecols as $iCnt => $aCol ) {
00255             if ( $blSep )
00256                 $sQ .= ', ';
00257 
00258             // multijanguage
00259             $sCol = $aCol[3]?$aCol[0].$sLangTag:$aCol[0];
00260             $sQ  .= getViewName( $aCol[1] ) . '.' . $sCol . ' as _' . $iCnt;
00261             $blSep = true;
00262         }
00263 
00264         $aIdentCols = $this->_getIdentColNames();
00265         foreach ( $aIdentCols as $iCnt => $aCol ) {
00266             if ( $blSep )
00267                 $sQ .= ', ';
00268 
00269             // multijanguage
00270             $sCol = $aCol[3]?$aCol[0].$sLangTag:$aCol[0];
00271             $sQ  .= getViewName( $aCol[1] ) . '.' . $sCol . ' as _' . $iCnt;
00272         }
00273 
00274         return " $sQ ";
00275     }
00276 
00282     protected function _getSorting()
00283     {
00284         return ' order by _' . $this->_getSortCol() . ' '.$this->_getSortDir().' ';
00285     }
00286 
00294     protected function _getLimit( $iStart )
00295     {
00296         $iLimit = (int) oxConfig::getParameter("results");
00297         $iLimit = $iLimit?$iLimit:$this->_iSqlLimit;
00298 
00299         return " limit $iStart, $iLimit ";
00300     }
00301 
00307     protected function _getFilter()
00308     {
00309         $myConfig = $this->getConfig();
00310         $sQ = '';
00311         $aFilter = oxConfig::getParameter( 'aFilter' );
00312         if ( is_array( $aFilter ) && count( $aFilter ) ) {
00313             $aCols = $this->_getVisibleColNames();
00314             $blSep = false;
00315             $oDb = oxDb::getDb();
00316             $sLangTag = oxLang::getInstance()->getLanguageTag();
00317             $oStr = getStr();
00318 
00319             foreach ( $aFilter as $sCol => $sValue ) {
00320 
00321                 // skipping empty filters
00322                 if ( !$sValue ) {
00323                     continue;
00324                 }
00325 
00326                 $iCol = (int) str_replace( '_', '', $sCol );
00327                 if ( isset( $aCols[ $iCol ] ) ) {
00328                     if ( $sQ )
00329                         $sQ .= ' and ';
00330 
00331                     if (!$myConfig->isUtf()) {
00332                         $sValue = iconv('UTF-8', oxLang::getInstance()->translateString("charset"), $sValue );
00333                     }
00334 
00335                     // escaping special characters
00336                     $sValue = str_replace( array( '%', '_' ), array( '\%', '\_' ), $sValue );
00337 
00338                     // possibility to search in the middle ..
00339                     $sValue = $oStr->preg_replace( '/^\*/', '%', $sValue );
00340 
00341                     $sCol = $aCols[ $iCol ][3]?$aCols[ $iCol ][0].$sLangTag:$aCols[ $iCol ][0];
00342                     $sQ .= getViewName( $aCols[ $iCol ][1] ) . '.' . $sCol;
00343                     $sQ .= ' like ' . $oDb->Quote( $sValue . '%' ). ' ';
00344                 }
00345 
00346             }
00347         }
00348         return $sQ;
00349     }
00350 
00358     protected function _addFilter( $sQ )
00359     {
00360         if ( $sQ && $sFilter = $this->_getFilter() ) {
00361             $sQ .= ( ( stristr( $sQ, 'where' ) === false )?'where':' and ' ) . $sFilter;
00362         }
00363         return $sQ;
00364     }
00365 
00373     protected function _getAll( $sQ )
00374     {
00375         $aReturn = array();
00376         $rs = oxDb::getDb()->Execute( $sQ );
00377         if ($rs != false && $rs->recordCount() > 0) {
00378             while (!$rs->EOF) {
00379                 $aReturn[] = $rs->fields[0];
00380                 $rs->moveNext();
00381             }
00382         }
00383         return $aReturn;
00384     }
00385 
00391     protected function _getSortDir()
00392     {
00393         $sDir = oxConfig::getParameter( 'dir' );
00394         if ( !in_array( $sDir, $this->_aPosDir ) ) {
00395             $sDir = $this->_aPosDir[0];
00396         }
00397 
00398         return $sDir;
00399     }
00400 
00406     protected function _getStartIndex()
00407     {
00408         return (int) oxConfig::getParameter( 'startIndex' );
00409     }
00410 
00418     protected function _getTotalCount( $sQ )
00419     {
00420         // TODO: implement caching here
00421 
00422         // we can cache total count ...
00423 
00424         // $sCountCacheKey = md5( $sQ );
00425 
00426         return (int) oxDb::getDb()->getOne( $sQ );
00427     }
00428 
00436     protected function _getDataFields( $sQ )
00437     {
00438         return oxDb::getDb(true)->getArray( $sQ );
00439     }
00440 
00448     protected function _outputResponse( $aData )
00449     {
00450         if ( !$this->getConfig()->isUtf() ) {
00451             // TODO: improve this
00452             if ( is_array( $aData['records'] ) && $iRecSize = count( $aData['records'] ) ) {
00453                 $aKeys = array_keys( current( $aData['records'] ) );
00454                 $iKeySize = count( $aKeys );
00455                 for ( $i = 0; $i < $iRecSize; $i++ ) {
00456                     for ( $c = 0; $c < $iKeySize; $c++ ) {
00457                         $aData['records'][$i][$aKeys[$c]] = iconv(oxLang::getInstance()->translateString("charset"), "UTF-8", $aData['records'][$i][$aKeys[$c]] );
00458                     }
00459                 }
00460             }
00461         }
00462 
00463         echo json_encode( $aData );
00464     }
00465 
00474     protected function _getData( $sCountQ, $sQ )
00475     {
00476         $sQ = $this->_addFilter( $sQ );
00477         $sCountQ = $this->_addFilter( $sCountQ );
00478 
00479         $aResponse['startIndex'] = $iStart = $this->_getStartIndex();
00480         $aResponse['sort'] = '_' . $this->_getSortCol();
00481         $aResponse['dir']  = $this->_getSortDir();
00482 
00483         $iDebug = $this->getConfig()->getConfigParam( 'iDebug' );
00484         if ( $iDebug ) {
00485             $aResponse['countsql'] = $sCountQ;
00486         }
00487 
00488         $aResponse['records'] = array();
00489 
00490         // skip further execution if no records were found ...
00491         if ( ( $iTotal = $this->_getTotalCount( $sCountQ ) ) ) {
00492             $sQ .= $this->_getSorting();
00493             $sQ .= $this->_getLimit( $iStart );
00494 
00495             if ( $iDebug ) {
00496                 $aResponse['datasql'] = $sQ;
00497             }
00498 
00499             $aResponse['records'] = $this->_getDataFields( $sQ );
00500         }
00501 
00502         $aResponse['totalRecords'] = $iTotal;
00503 
00504         return $aResponse;
00505     }
00506 
00512     public function resetContentCache()
00513     {
00514         $blDeleteCacheOnLogout = $this->getConfig()->getConfigParam( 'blClearCacheOnLogout' );
00515 
00516 
00517             if ( !$blDeleteCacheOnLogout ) {
00518                 oxUtils::getInstance()->oxResetFileCache();
00519             }
00520     }
00521 
00531     public function resetCounter( $sCounterType, $sValue = null )
00532     {
00533         $blDeleteCacheOnLogout = $this->getConfig()->getConfigParam( 'blClearCacheOnLogout' );
00534         $myUtilsCount = oxUtilsCount::getInstance();
00535 
00536         if ( !$blDeleteCacheOnLogout ) {
00537             switch ( $sCounterType ) {
00538                 case 'priceCatArticle':
00539                     $myUtilsCount->resetPriceCatArticleCount( $sValue );
00540                     break;
00541                 case 'catArticle':
00542                     $myUtilsCount->resetCatArticleCount( $sValue );
00543                     break;
00544                 case 'vendorArticle':
00545                     $myUtilsCount->resetVendorArticleCount( $sValue );
00546                     break;
00547                 case 'manufacturerArticle':
00548                     $myUtilsCount->resetManufacturerArticleCount( $sValue );
00549                     break;
00550             }
00551         }
00552     }
00553 
00554 }
00555 
00556 // processing ..
00557 $blAjaxCall = (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest');
00558 if ( $blAjaxCall ) {
00559 
00560 
00561     // Setting error reporting mode
00562     error_reporting( E_ALL ^ E_NOTICE);
00563 
00564     //setting basic configuration parameters
00565     ini_set('session.name', 'sid' );
00566     ini_set('session.use_cookies', 0 );
00567     ini_set('session.use_trans_sid', 0);
00568     ini_set('url_rewriter.tags', '');
00569     ini_set('magic_quotes_runtime', 0);
00570 
00571     // Generic utility method file.
00572     $sBasePath = getShopBasePath();
00573     include_once $sBasePath . 'modules/functions.php';
00574     include_once $sBasePath . 'core/oxfunctions.php';
00575     include_once $sBasePath . 'core/adodblite/adodb.inc.php';
00576     include_once $sBasePath . 'core/oxconfig.php';
00577     include_once $sBasePath . 'core/oxsupercfg.php';
00578 
00579 
00580     include_once $sBasePath . "core/oxutils.php";
00581 
00582     $myConfig = oxConfig::getInstance();
00583 
00584     // Includes Utility module.
00585     $sUtilModule = $myConfig->getConfigParam( 'sUtilModule' );
00586     if ( $sUtilModule && file_exists( getShopBasePath()."modules/".$sUtilModule ) )
00587         include_once getShopBasePath()."modules/".$sUtilModule;
00588 
00589     $myConfig->setConfigParam( 'blAdmin', true );
00590     $myConfig->setConfigParam( 'blTemplateCaching', false );
00591 
00592     // authorization
00593     if ( !count(oxUtilsServer::getInstance()->getOxCookie()) || !oxUtils::getInstance()->checkAccessRights()) {
00594         header( "location:index.php");
00595         exit();
00596     }
00597 
00598     if ( $sContainer = oxConfig::getParameter( 'container' ) ) {
00599 
00600         $sContainer = strtolower( basename( $sContainer ) );
00601         $aColumns = array();
00602         include_once 'inc/'.$sContainer.'.inc.php';
00603 
00604         $oAjaxComponent = new ajaxcomponent( $aColumns );
00605         $oAjaxComponent->setName( $sContainer );
00606         $oAjaxComponent->processRequest( oxConfig::getParameter( 'fnc' ) );
00607 
00608     } else {
00609 
00610     }
00611 
00612     $myConfig->pageClose();
00613 
00614     // closing session handlers
00615     // session_write_close();
00616     return;
00617 }

Generated on Tue Sep 29 16:45:12 2009 for OXID eShop CE by  doxygen 1.5.5