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     public function processRequest( $sFunction = null )
00118     {
00119         if ( $sFunction ) {
00120             $this->$sFunction();
00121         } else {
00122             $sQAdd = $this->_getQuery();
00123 
00124             // formatting SQL queries
00125             $sQ      = 'select ' . $this->_getQueryCols() . $sQAdd;
00126             $sCountQ = 'select count( * ) ' . $sQAdd;
00127 
00128             $this->_outputResponse( $this->_getData( $sCountQ, $sQ ) );
00129         }
00130     }
00131 
00137     protected function _getSortCol()
00138     {
00139         $aVisibleNames = $this->_getVisibleColNames();
00140         $iCol = oxConfig::getParameter( 'sort' );
00141         $iCol = $iCol?( ( int ) str_replace( '_', '', $iCol )):0;
00142         $iCol = ( !isset( $aVisibleNames[$iCol] ) )?0:$iCol;
00143 
00144         return $iCol;
00145     }
00146 
00147 
00156     protected function _getColNames( $sId = null )
00157     {
00158         if ( $sId === null ) {
00159             $sId = oxConfig::getParameter( 'cmpid' );
00160         }
00161 
00162         if ( $sId && isset( $this->_aColumns[$sId] ) ) {
00163             return $this->_aColumns[$sId];
00164         }
00165 
00166         return $this->_aColumns;
00167     }
00168 
00175     protected function _getIdentColNames()
00176     {
00177         $aColNames = $this->_getColNames();
00178         $aCols = array();
00179         foreach ( $aColNames as $iKey =>  $aCol ) {
00180             if ( $aCol[4] ) // ident ?
00181                 $aCols[$iKey] = $aCol;
00182         }
00183         return $aCols;
00184     }
00185 
00191     protected function _getVisibleColNames()
00192     {
00193         $aColNames = $this->_getColNames();
00194         $aUserCols = oxConfig::getParameter( 'aCols' );
00195         $aVisibleCols = array();
00196 
00197         // user defined some cols to load ?
00198         if ( is_array( $aUserCols ) ) {
00199             foreach ( $aUserCols as $iKey => $sCol ) {
00200                 $iCol = ( int ) str_replace( '_', '', $sCol );
00201                 if ( isset( $aColNames[$iCol] ) && !$aColNames[$iCol][4] ) {
00202                     $aVisibleCols[$iCol] = $aColNames[$iCol];
00203                 }
00204             }
00205         }
00206 
00207         // no user defined valid cols ? setting defauls ..
00208         if ( !count( $aVisibleCols ) ) {
00209             foreach ( $aColNames as $sName => $aCol ) {
00210                 if ( $aCol[1] && !$aColNames[$sName][4]  ) // visible ?
00211                     $aVisibleCols[$sName] = $aCol;
00212             }
00213         }
00214 
00215         return $aVisibleCols;
00216     }
00217 
00224     protected function _getQueryCols()
00225     {
00226         $sLangTag = oxLang::getInstance()->getLanguageTag();
00227         $sQ = '';
00228         $blSep = false;
00229         $aVisiblecols = $this->_getVisibleColNames();
00230         foreach ( $aVisiblecols as $iCnt => $aCol ) {
00231             if ( $blSep )
00232                 $sQ .= ', ';
00233 
00234             // multijanguage
00235             $sCol = $aCol[3]?$aCol[0].$sLangTag:$aCol[0];
00236             $sQ  .= getViewName( $aCol[1] ) . '.' . $sCol . ' as _' . $iCnt;
00237             $blSep = true;
00238         }
00239 
00240         $aIdentCols = $this->_getIdentColNames();
00241         foreach ( $aIdentCols as $iCnt => $aCol ) {
00242             if ( $blSep )
00243                 $sQ .= ', ';
00244 
00245             // multijanguage
00246             $sCol = $aCol[3]?$aCol[0].$sLangTag:$aCol[0];
00247             $sQ  .= getViewName( $aCol[1] ) . '.' . $sCol . ' as _' . $iCnt;
00248         }
00249 
00250         return " $sQ ";
00251     }
00252 
00258     protected function _getSorting()
00259     {
00260         $aVisibleCols = $this->_getVisibleColNames();
00261         $aSortCol = $aVisibleCols[ $this->_getSortCol() ];
00262         $sCol = $aSortCol[3]?$aSortCol[0].oxLang::getInstance()->getLanguageTag():$aSortCol[0];
00263         return ' order by ' . getViewName( $aSortCol[1] ) . '.' . $sCol . ' '.$this->_getSortDir().' ';
00264     }
00265 
00273     protected function _getLimit( $iStart )
00274     {
00275         $iLimit = (int) oxConfig::getParameter("results");
00276         $iLimit = $iLimit?$iLimit:$this->_iSqlLimit;
00277 
00278         return " limit $iStart, $iLimit ";
00279     }
00280 
00286     protected function _getFilter()
00287     {
00288         $myConfig = $this->getConfig();
00289         $sQ = '';
00290         $aFilter = oxConfig::getParameter( 'aFilter' );
00291         if ( is_array( $aFilter ) && count( $aFilter ) ) {
00292             $aCols = $this->_getVisibleColNames();
00293             $blSep = false;
00294             $oDb = oxDb::getDb();
00295             $sLangTag = oxLang::getInstance()->getLanguageTag();
00296             $oStr = getStr();
00297 
00298             foreach ( $aFilter as $sCol => $sValue ) {
00299 
00300                 // skipping empty filters
00301                 if ( !$sValue ) {
00302                     continue;
00303                 }
00304 
00305                 $iCol = (int) str_replace( '_', '', $sCol );
00306                 if ( isset( $aCols[ $iCol ] ) ) {
00307                     if ( $sQ )
00308                         $sQ .= ' and ';
00309 
00310                     if (!$myConfig->isUtf()) {
00311                         $sValue = iconv('UTF-8', oxLang::getInstance()->translateString("charset"), $sValue );
00312                     }
00313 
00314                     // escaping special characters
00315                     $sValue = str_replace( array( '%', '_' ), array( '\%', '\_' ), $sValue );
00316 
00317                     // possibility to search in the middle ..
00318                     $sValue = $oStr->preg_replace( '/^\*/', '%', $sValue );
00319 
00320                     $sCol = $aCols[ $iCol ][3]?$aCols[ $iCol ][0].$sLangTag:$aCols[ $iCol ][0];
00321                     $sQ .= getViewName( $aCols[ $iCol ][1] ) . '.' . $sCol;
00322                     $sQ .= ' like ' . $oDb->Quote( $sValue . '%' ). ' ';
00323                 }
00324 
00325             }
00326         }
00327         return $sQ;
00328     }
00329 
00337     protected function _addFilter( $sQ )
00338     {
00339         if ( $sQ && $sFilter = $this->_getFilter() ) {
00340             $sQ .= ( ( stristr( $sQ, 'where' ) === false )?'where':' and ' ) . $sFilter;
00341         }
00342         return $sQ;
00343     }
00344 
00352     protected function _getAll( $sQ )
00353     {
00354         $aReturn = array();
00355         $rs = oxDb::getDb()->Execute( $sQ );
00356         if ($rs != false && $rs->recordCount() > 0) {
00357             while (!$rs->EOF) {
00358                 $aReturn[] = $rs->fields[0];
00359                 $rs->moveNext();
00360             }
00361         }
00362         return $aReturn;
00363     }
00364 
00370     protected function _getSortDir()
00371     {
00372         $sDir = oxConfig::getParameter( 'dir' );
00373         if ( !in_array( $sDir, $this->_aPosDir ) ) {
00374             $sDir = $this->_aPosDir[0];
00375         }
00376 
00377         return $sDir;
00378     }
00379 
00385     protected function _getStartIndex()
00386     {
00387         return (int) oxConfig::getParameter( 'startIndex' );
00388     }
00389 
00397     protected function _getTotalCount( $sQ )
00398     {
00399         // TODO: implement caching here
00400 
00401         // we can cache total count ...
00402 
00403         // $sCountCacheKey = md5( $sQ );
00404 
00405         return (int) oxDb::getDb()->getOne( $sQ );
00406     }
00407 
00415     protected function _getDataFields( $sQ )
00416     {
00417         return oxDb::getDb(true)->getArray( $sQ );
00418     }
00419 
00427     protected function _outputResponse( $aData )
00428     {
00429         if ( !$this->getConfig()->isUtf() ) {
00430             // TODO: improve this
00431             if ( is_array( $aData['records'] ) && $iRecSize = count( $aData['records'] ) ) {
00432                 $aKeys = array_keys( current( $aData['records'] ) );
00433                 $iKeySize = count( $aKeys );
00434                 for ( $i = 0; $i < $iRecSize; $i++ ) {
00435                     for ( $c = 0; $c < $iKeySize; $c++ ) {
00436                         $aData['records'][$i][$aKeys[$c]] = iconv(oxLang::getInstance()->translateString("charset"), "UTF-8", $aData['records'][$i][$aKeys[$c]] );
00437                     }
00438                 }
00439             }
00440         }
00441 
00442         echo json_encode( $aData );
00443     }
00444 
00453     protected function _getData( $sCountQ, $sQ )
00454     {
00455         $sQ = $this->_addFilter( $sQ );
00456         $sCountQ = $this->_addFilter( $sCountQ );
00457 
00458         $aResponse['startIndex'] = $iStart = $this->_getStartIndex();
00459         $aResponse['sort'] = '_' . $this->_getSortCol();
00460         $aResponse['dir']  = $this->_getSortDir();
00461 
00462         $iDebug = $this->getConfig()->getConfigParam( 'iDebug' );
00463         if ( $iDebug ) {
00464             $aResponse['countsql'] = $sCountQ;
00465         }
00466 
00467         $aResponse['records'] = array();
00468 
00469         // skip further execution if no records were found ...
00470         if ( ( $iTotal = $this->_getTotalCount( $sCountQ ) ) ) {
00471             $sQ .= $this->_getSorting();
00472             $sQ .= $this->_getLimit( $iStart );
00473 
00474             if ( $iDebug ) {
00475                 $aResponse['datasql'] = $sQ;
00476             }
00477 
00478             $aResponse['records'] = $this->_getDataFields( $sQ );
00479         }
00480 
00481         $aResponse['totalRecords'] = $iTotal;
00482 
00483         return $aResponse;
00484     }
00485 
00491     public function resetContentCache()
00492     {
00493         $blDeleteCacheOnLogout = $this->getConfig()->getConfigParam( 'blClearCacheOnLogout' );
00494 
00495 
00496             oxUtils::getInstance()->oxResetFileCache();
00497     }
00498 
00508     public function resetCounter( $sCounterType, $sValue = null )
00509     {
00510         $blDeleteCacheOnLogout = $this->getConfig()->getConfigParam( 'blClearCacheOnLogout' );
00511         $myUtilsCount = oxUtilsCount::getInstance();
00512 
00513         if ( !$blDeleteCacheOnLogout ) {
00514             switch ( $sCounterType ) {
00515                 case 'priceCatArticle':
00516                     $myUtilsCount->resetPriceCatArticleCount( $sValue );
00517                     break;
00518                 case 'catArticle':
00519                     $myUtilsCount->resetCatArticleCount( $sValue );
00520                     break;
00521                 case 'vendorArticle':
00522                     $myUtilsCount->resetVendorArticleCount( $sValue );
00523                     break;
00524                 case 'manufacturerArticle':
00525                     $myUtilsCount->resetManufacturerArticleCount( $sValue );
00526                     break;
00527             }
00528         }
00529     }
00530 
00531 }
00532 
00533 // processing ..
00534 $blAjaxCall = (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest');
00535 if ( $blAjaxCall ) {
00536 
00537 
00538     // Setting error reporting mode
00539     error_reporting( E_ALL ^ E_NOTICE);
00540 
00541     //setting basic configuration parameters
00542     ini_set('session.name', 'sid' );
00543     ini_set('session.use_cookies', 0 );
00544     ini_set('session.use_trans_sid', 0);
00545     ini_set('url_rewriter.tags', '');
00546     ini_set('magic_quotes_runtime', 0);
00547 
00548     // Generic utility method file.
00549     $sBasePath = getShopBasePath();
00550     include_once $sBasePath . 'core/oxfunctions.php';
00551     include_once $sBasePath . 'core/adodblite/adodb.inc.php';
00552     include_once $sBasePath . 'core/oxconfig.php';
00553     include_once $sBasePath . 'core/oxsupercfg.php';
00554 
00555 
00556     include_once $sBasePath . "core/oxutils.php";
00557 
00558     $myConfig = oxConfig::getInstance();
00559 
00560     // Includes Utility module.
00561     $sUtilModule = $myConfig->getConfigParam( 'sUtilModule' );
00562     if ( $sUtilModule && file_exists( getShopBasePath()."modules/".$sUtilModule ) )
00563         include_once getShopBasePath()."modules/".$sUtilModule;
00564 
00565     $myConfig->setConfigParam( 'blAdmin', true );
00566     $myConfig->setConfigParam( 'blTemplateCaching', false );
00567 
00568     // authorization
00569     if ( !count(oxUtilsServer::getInstance()->getOxCookie()) || !oxUtils::getInstance()->checkAccessRights()) {
00570         header( "location:index.php");
00571         exit();
00572     }
00573 
00574     if ( $sContainer = oxConfig::getParameter( 'container' ) ) {
00575 
00576         $sContainer = strtolower( basename( $sContainer ) );
00577         $aColumns = array();
00578         include_once 'inc/'.$sContainer.'.inc.php';
00579 
00580         $oAjaxComponent = new ajaxcomponent( $aColumns );
00581         $oAjaxComponent->setName( $sContainer );
00582         $oAjaxComponent->processRequest( oxConfig::getParameter( 'fnc' ) );
00583 
00584     } else {
00585 
00586     }
00587 
00588     // closing session handlers
00589     // session_write_close();
00590     return;
00591 }

Generated on Tue Apr 21 15:45:44 2009 for OXID eShop CE by  doxygen 1.5.5