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 
00070     protected $_blAllowExtColumns = false;
00071 
00079     public function init( $aColumns )
00080     {
00081         $this->_aColumns = $aColumns;
00082     }
00083 
00093     protected function _getActionIds( $sId )
00094     {
00095         $aColumns = $this->_getColNames();
00096         foreach ( $aColumns as $iPos => $aCol ) {
00097             if ( isset($aCol[4]) && $aCol[4] == 1 && $sId == $aCol[1].'.'.$aCol[0] ) {
00098                 return oxConfig::getParameter( '_'.$iPos );
00099             }
00100         }
00101     }
00102 
00110     public function setName( $sName )
00111     {
00112         $this->_sContainer = $sName;
00113     }
00114 
00120     protected function _getQuery()
00121     {
00122         return '';
00123     }
00124 
00132     protected function _getDataQuery( $sQ )
00133     {
00134         return 'select ' . $this->_getQueryCols() . $sQ;
00135     }
00136 
00144     protected function _getCountQuery( $sQ )
00145     {
00146         return 'select count( * ) ' . $sQ;
00147     }
00148 
00156     public function processRequest( $sFunction = null )
00157     {
00158         if ( $sFunction ) {
00159             $this->$sFunction();
00160         } else {
00161             $sQAdd = $this->_getQuery();
00162 
00163             // formatting SQL queries
00164             $sQ      = $this->_getDataQuery( $sQAdd );
00165             $sCountQ = $this->_getCountQuery( $sQAdd );
00166 
00167             $this->_outputResponse( $this->_getData( $sCountQ, $sQ ) );
00168         }
00169     }
00170 
00176     protected function _getSortCol()
00177     {
00178         $aVisibleNames = $this->_getVisibleColNames();
00179         $iCol = oxConfig::getParameter( 'sort' );
00180         $iCol = $iCol?( ( int ) str_replace( '_', '', $iCol )):0;
00181         $iCol = ( !isset( $aVisibleNames[$iCol] ) )?0:$iCol;
00182 
00183         return $iCol;
00184     }
00185 
00186 
00195     protected function _getColNames( $sId = null )
00196     {
00197         if ( $sId === null ) {
00198             $sId = oxConfig::getParameter( 'cmpid' );
00199         }
00200 
00201         if ( $sId && isset( $this->_aColumns[$sId] ) ) {
00202             return $this->_aColumns[$sId];
00203         }
00204 
00205         return $this->_aColumns;
00206     }
00207 
00214     protected function _getIdentColNames()
00215     {
00216         $aColNames = $this->_getColNames();
00217         $aCols = array();
00218         foreach ( $aColNames as $iKey =>  $aCol ) {
00219             if ( $aCol[4] ) // ident ?
00220                 $aCols[$iKey] = $aCol;
00221         }
00222         return $aCols;
00223     }
00224 
00230     protected function _getVisibleColNames()
00231     {
00232         $aColNames = $this->_getColNames();
00233         $aUserCols = oxConfig::getParameter( 'aCols' );
00234         $aVisibleCols = array();
00235 
00236         // user defined some cols to load ?
00237         if ( is_array( $aUserCols ) ) {
00238             foreach ( $aUserCols as $iKey => $sCol ) {
00239                 $iCol = ( int ) str_replace( '_', '', $sCol );
00240                 if ( isset( $aColNames[$iCol] ) && !$aColNames[$iCol][4] ) {
00241                     $aVisibleCols[$iCol] = $aColNames[$iCol];
00242                 }
00243             }
00244         }
00245 
00246         // no user defined valid cols ? setting defauls ..
00247         if ( !count( $aVisibleCols ) ) {
00248             foreach ( $aColNames as $sName => $aCol ) {
00249                 if ( $aCol[1] && !$aColNames[$sName][4]  ) // visible ?
00250                     $aVisibleCols[$sName] = $aCol;
00251             }
00252         }
00253 
00254         return $aVisibleCols;
00255     }
00256 
00263     protected function _getQueryCols()
00264     {
00265         $sQ  = $this->_buildColsQuery( $this->_getVisibleColNames(), false ).", ";
00266         $sQ .= $this->_buildColsQuery( $this->_getIdentColNames() );
00267 
00268         return " $sQ ";
00269     }
00270 
00279     protected function _buildColsQuery( $aIdentCols, $blIdentCols = true )
00280     {
00281         $sQ = '';
00282         foreach ( $aIdentCols as $iCnt => $aCol ) {
00283             if ( $sQ ) {
00284                 $sQ .= ', ';
00285             }
00286 
00287             $sViewTable = $this->_getViewName( $aCol[1] );
00288             if ( !$blIdentCols && $this->_isExtendedColumn( $aCol[0] ) ) {
00289                 $sQ .= $this->_getExtendedColQuery( $sViewTable, $aCol[0], $iCnt );
00290             } else {
00291                 $sQ .=  $sViewTable. '.' . $aCol[0] . ' as _' . $iCnt;
00292             }
00293         }
00294 
00295         return $sQ;
00296     }
00297 
00306     protected function _isExtendedColumn( $sColumn )
00307     {
00308         $blBuild = false;
00309         if ( $this->_blAllowExtColumns && oxConfig::getInstance()->getConfigParam( 'blVariantsSelection' ) && $sColumn == 'oxtitle' ) {
00310             $blBuild = true;
00311         }
00312 
00313         return $blBuild;
00314     }
00315 
00326     protected function _getExtendedColQuery( $sViewTable, $sColumn, $iCnt )
00327     {
00328         // multilanguage
00329         $sVarSelect = "$sViewTable.oxvarselect";
00330         return " IF( {$sViewTable}.{$sColumn} != '', {$sViewTable}.{$sColumn}, CONCAT((select oxart.{$sColumn} from {$sViewTable} as oxart where oxart.oxid = {$sViewTable}.oxparentid),', ',{$sVarSelect})) as _{$iCnt}";
00331     }
00332 
00338     protected function _getSorting()
00339     {
00340         return ' order by _' . $this->_getSortCol() . ' '.$this->_getSortDir().' ';
00341     }
00342 
00350     protected function _getLimit( $iStart )
00351     {
00352         $iLimit = (int) oxConfig::getParameter("results");
00353         $iLimit = $iLimit?$iLimit:$this->_iSqlLimit;
00354 
00355         return " limit $iStart, $iLimit ";
00356     }
00357 
00363     protected function _getFilter()
00364     {
00365         $sQ = '';
00366         $aFilter = oxConfig::getParameter( 'aFilter' );
00367         if ( is_array( $aFilter ) && count( $aFilter ) ) {
00368             $aCols = $this->_getVisibleColNames();
00369             $blSep = false;
00370             $oDb = oxDb::getDb();
00371             $oLang = oxLang::getInstance();
00372             $oStr = getStr();
00373 
00374             $blIsUtf  = $this->getConfig()->isUtf();
00375             $sCharset = $oLang->translateString( "charset" );
00376 
00377             foreach ( $aFilter as $sCol => $sValue ) {
00378 
00379                 // skipping empty filters
00380                 if ( !$sValue ) {
00381                     continue;
00382                 }
00383 
00384                 $iCol = (int) str_replace( '_', '', $sCol );
00385                 if ( isset( $aCols[ $iCol ] ) ) {
00386                     if ( $sQ )
00387                         $sQ .= ' and ';
00388 
00389                     if ( !$blIsUtf ) {
00390                         $sValue = iconv( 'UTF-8', $sCharset, $sValue );
00391                     }
00392 
00393                     // escaping special characters
00394                     $sValue = str_replace( array( '%', '_' ), array( '\%', '\_' ), $sValue );
00395 
00396                     // possibility to search in the middle ..
00397                     $sValue = $oStr->preg_replace( '/^\*/', '%', $sValue );
00398 
00399                     $sQ .= $this->_getViewName( $aCols[ $iCol ][1] ) . '.' . $aCols[ $iCol ][0];
00400                     $sQ .= ' like ' . $oDb->Quote( $sValue . '%' ). ' ';
00401                 }
00402 
00403             }
00404         }
00405         return $sQ;
00406     }
00407 
00415     protected function _addFilter( $sQ )
00416     {
00417         if ( $sQ && ( $sFilter = $this->_getFilter() ) ) {
00418             $sQ .= ( ( stristr( $sQ, 'where' ) === false )?'where':' and ' ) . $sFilter;
00419         }
00420         return $sQ;
00421     }
00422 
00430     protected function _getAll( $sQ )
00431     {
00432         $aReturn = array();
00433         $rs = oxDb::getDb()->execute( $sQ );
00434         if ($rs != false && $rs->recordCount() > 0) {
00435             while (!$rs->EOF) {
00436                 $aReturn[] = $rs->fields[0];
00437                 $rs->moveNext();
00438             }
00439         }
00440         return $aReturn;
00441     }
00442 
00448     protected function _getSortDir()
00449     {
00450         $sDir = oxConfig::getParameter( 'dir' );
00451         if ( !in_array( $sDir, $this->_aPosDir ) ) {
00452             $sDir = $this->_aPosDir[0];
00453         }
00454 
00455         return $sDir;
00456     }
00457 
00463     protected function _getStartIndex()
00464     {
00465         return (int) oxConfig::getParameter( 'startIndex' );
00466     }
00467 
00475     protected function _getTotalCount( $sQ )
00476     {
00477         // TODO: implement caching here
00478 
00479         // we can cache total count ...
00480 
00481         // $sCountCacheKey = md5( $sQ );
00482 
00483         return (int) oxDb::getDb()->getOne( $sQ );
00484     }
00485 
00493     protected function _getDataFields( $sQ )
00494     {
00495         return oxDb::getDb(true)->getArray( $sQ );
00496     }
00497 
00505     protected function _outputResponse( $aData )
00506     {
00507         if ( !$this->getConfig()->isUtf() ) {
00508             // TODO: improve this
00509             if ( is_array( $aData['records'] ) && ( $iRecSize = count( $aData['records'] ) ) ) {
00510                 $aKeys = array_keys( current( $aData['records'] ) );
00511                 $iKeySize = count( $aKeys );
00512                 $sCharset = oxLang::getInstance()->translateString("charset");
00513                 for ( $i = 0; $i < $iRecSize; $i++ ) {
00514                     for ( $c = 0; $c < $iKeySize; $c++ ) {
00515                         $aData['records'][$i][$aKeys[$c]] = iconv( $sCharset, "UTF-8", $aData['records'][$i][$aKeys[$c]] );
00516                     }
00517                 }
00518             }
00519         }
00520 
00521         $this->_output( json_encode( $aData ) );
00522     }
00523 
00531     protected function _output( $sOut )
00532     {
00533         echo $sOut;
00534     }
00535 
00543     protected function _getViewName( $sTable )
00544     {
00545         return getViewName( $sTable, oxConfig::getParameter('editlanguage'));
00546     }
00547 
00556     protected function _getData( $sCountQ, $sQ )
00557     {
00558         $sQ = $this->_addFilter( $sQ );
00559         $sCountQ = $this->_addFilter( $sCountQ );
00560 
00561         $aResponse['startIndex'] = $iStart = $this->_getStartIndex();
00562         $aResponse['sort'] = '_' . $this->_getSortCol();
00563         $aResponse['dir']  = $this->_getSortDir();
00564 
00565         $iDebug = $this->getConfig()->getConfigParam( 'iDebug' );
00566         if ( $iDebug ) {
00567             $aResponse['countsql'] = $sCountQ;
00568         }
00569 
00570         $aResponse['records'] = array();
00571 
00572         // skip further execution if no records were found ...
00573         if ( ( $iTotal = $this->_getTotalCount( $sCountQ ) ) ) {
00574             $sQ .= $this->_getSorting();
00575             $sQ .= $this->_getLimit( $iStart );
00576 
00577             if ( $iDebug ) {
00578                 $aResponse['datasql'] = $sQ;
00579             }
00580 
00581             $aResponse['records'] = $this->_getDataFields( $sQ );
00582         }
00583 
00584         $aResponse['totalRecords'] = $iTotal;
00585 
00586         return $aResponse;
00587     }
00588 
00597     public function resetArtSeoUrl( $aArtIds, $aCatIds = null )
00598     {
00599         if ( empty( $aArtIds ) ) {
00600             return;
00601         }
00602 
00603         if ( !is_array( $aArtIds ) ) {
00604             $aArtIds = array( $aArtIds );
00605         }
00606 
00607         $blCleanCats = false;
00608         if ( $aCatIds ) {
00609             if ( !is_array( $aCatIds ) ) {
00610                 $aCatIds = array( $aCatIds );
00611             }
00612             $sShopId = $this->getConfig()->getShopId();
00613             $sQ = "delete from oxseo where oxtype='oxarticle' and oxobjectid='%s' and
00614                    oxshopid='{$sShopId}' and oxparams in ('" . implode( ",", oxDb::getInstance()->quoteArray( $aCatIds ) ) . "')";
00615             $oDb = oxDb::getDb();
00616             $blCleanCats = true;
00617         }
00618 
00619         $sShopId = $this->getConfig()->getShopId();
00620         foreach ( $aArtIds as $sArtId ) {
00621             oxSeoEncoder::getInstance()->markAsExpired( $sArtId, $sShopId, 1, null, "oxtype='oxarticle'" );
00622             if ( $blCleanCats ) {
00623                 $oDb->execute( sprintf( $sQ, $sArtId ) );
00624             }
00625         }
00626     }
00627 
00633     public function resetContentCache()
00634     {
00635         $blDeleteCacheOnLogout = $this->getConfig()->getConfigParam( 'blClearCacheOnLogout' );
00636 
00637 
00638             if ( !$blDeleteCacheOnLogout ) {
00639                 oxUtils::getInstance()->oxResetFileCache();
00640             }
00641     }
00642 
00643 
00644 
00654     public function resetCounter( $sCounterType, $sValue = null )
00655     {
00656         $blDeleteCacheOnLogout = $this->getConfig()->getConfigParam( 'blClearCacheOnLogout' );
00657 
00658         if ( !$blDeleteCacheOnLogout ) {
00659             $myUtilsCount = oxUtilsCount::getInstance();
00660             switch ( $sCounterType ) {
00661                 case 'priceCatArticle':
00662                     $myUtilsCount->resetPriceCatArticleCount( $sValue );
00663                     break;
00664                 case 'catArticle':
00665                     $myUtilsCount->resetCatArticleCount( $sValue );
00666                     break;
00667                 case 'vendorArticle':
00668                     $myUtilsCount->resetVendorArticleCount( $sValue );
00669                     break;
00670                 case 'manufacturerArticle':
00671                     $myUtilsCount->resetManufacturerArticleCount( $sValue );
00672                     break;
00673             }
00674         }
00675     }
00676 
00677 }
00678 
00679 // processing ..
00680 $blAjaxCall = (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest');
00681 if ( $blAjaxCall ) {
00682 
00683 
00684     // Setting error reporting mode
00685     error_reporting( E_ALL ^ E_NOTICE);
00686 
00687     //setting basic configuration parameters
00688     ini_set('session.name', 'sid' );
00689     ini_set('session.use_cookies', 0 );
00690     ini_set('session.use_trans_sid', 0);
00691     ini_set('url_rewriter.tags', '');
00692     ini_set('magic_quotes_runtime', 0);
00693 
00694     // Generic utility method file.
00695     $sBasePath = getShopBasePath();
00696     include_once $sBasePath . 'modules/functions.php';
00697     include_once $sBasePath . 'core/oxfunctions.php';
00698     include_once $sBasePath . 'core/adodblite/adodb.inc.php';
00699     include_once $sBasePath . 'core/oxconfig.php';
00700     include_once $sBasePath . 'core/oxsupercfg.php';
00701 
00702 
00703     include_once $sBasePath . "core/oxutils.php";
00704 
00705     $myConfig = oxConfig::getInstance();
00706 
00707     // Includes Utility module.
00708     $sUtilModule = $myConfig->getConfigParam( 'sUtilModule' );
00709     if ( $sUtilModule && file_exists( getShopBasePath()."modules/".$sUtilModule ) )
00710         include_once getShopBasePath()."modules/".$sUtilModule;
00711 
00712     $myConfig->setConfigParam( 'blAdmin', true );
00713     $myConfig->setConfigParam( 'blTemplateCaching', false );
00714 
00715     // authorization
00716     if ( !(oxSession::getInstance()->checkSessionChallenge() && count(oxUtilsServer::getInstance()->getOxCookie()) && oxUtils::getInstance()->checkAccessRights())) {
00717         header( "location:index.php");
00718         oxUtils::getInstance()->showMessageAndExit( "" );
00719     }
00720 
00721     if ( $sContainer = oxConfig::getParameter( 'container' ) ) {
00722 
00723         $sContainer = trim(strtolower( basename( $sContainer ) ));
00724         $aColumns = array();
00725 
00726         include_once 'inc/'.$sContainer.'.inc.php';
00727 
00728         //$oAjaxComponent = new ajaxcomponent( $aColumns );
00729         $oAjaxComponent = oxNew("ajaxcomponent");
00730         $oAjaxComponent->init($aColumns);
00731 
00732         $oAjaxComponent->setName( $sContainer );
00733         $oAjaxComponent->processRequest( oxConfig::getParameter( 'fnc' ) );
00734 
00735     } else {
00736 
00737     }
00738 
00739     $myConfig->pageClose();
00740 
00741     // closing session handlers
00742     // session_write_close();
00743     return;
00744 }