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 
00081     public function init( $aColumns = null )
00082     {
00083         if ( !is_null( $aColumns ) ) {
00084             $this->setColumns( $aColumns );
00085         }
00086     }
00087     
00093     public function getColumns()
00094     {
00095         return $this->_aColumns;        
00096     }
00097     
00105     public function setColumns( $aColumns )
00106     {
00107         $this->_aColumns = $aColumns;
00108     }
00109 
00119     protected function _getActionIds( $sId )
00120     {
00121         $aColumns = $this->_getColNames();
00122         foreach ( $aColumns as $iPos => $aCol ) {
00123             if ( isset($aCol[4]) && $aCol[4] == 1 && $sId == $aCol[1].'.'.$aCol[0] ) {
00124                 return oxConfig::getParameter( '_'.$iPos );
00125             }
00126         }
00127     }
00128 
00136     public function setName( $sName )
00137     {
00138         $this->_sContainer = $sName;
00139     }
00140 
00146     protected function _getQuery()
00147     {
00148         return '';
00149     }
00150 
00158     protected function _getDataQuery( $sQ )
00159     {
00160         return 'select ' . $this->_getQueryCols() . $sQ;
00161     }
00162 
00170     protected function _getCountQuery( $sQ )
00171     {
00172         return 'select count( * ) ' . $sQ;
00173     }
00174 
00182     public function processRequest( $sFunction = null )
00183     {
00184         if ( $sFunction ) {
00185             $this->$sFunction();
00186         } else {
00187             $sQAdd = $this->_getQuery();
00188 
00189             // formatting SQL queries
00190             $sQ      = $this->_getDataQuery( $sQAdd );
00191             $sCountQ = $this->_getCountQuery( $sQAdd );
00192 
00193             $this->_outputResponse( $this->_getData( $sCountQ, $sQ ) );
00194         }
00195     }
00196 
00202     protected function _getSortCol()
00203     {
00204         $aVisibleNames = $this->_getVisibleColNames();
00205         $iCol = oxConfig::getParameter( 'sort' );
00206         $iCol = $iCol?( ( int ) str_replace( '_', '', $iCol )):0;
00207         $iCol = ( !isset( $aVisibleNames[$iCol] ) )?0:$iCol;
00208 
00209         return $iCol;
00210     }
00211 
00212 
00221     protected function _getColNames( $sId = null )
00222     {
00223         if ( $sId === null ) {
00224             $sId = oxConfig::getParameter( 'cmpid' );
00225         }
00226 
00227         if ( $sId && isset( $this->_aColumns[$sId] ) ) {
00228             return $this->_aColumns[$sId];
00229         }
00230 
00231         return $this->_aColumns;
00232     }
00233 
00240     protected function _getIdentColNames()
00241     {
00242         $aColNames = $this->_getColNames();
00243         $aCols = array();
00244         foreach ( $aColNames as $iKey =>  $aCol ) {
00245             if ( $aCol[4] ) // ident ?
00246                 $aCols[$iKey] = $aCol;
00247         }
00248         return $aCols;
00249     }
00250 
00256     protected function _getVisibleColNames()
00257     {
00258         $aColNames = $this->_getColNames();
00259         $aUserCols = oxConfig::getParameter( 'aCols' );
00260         $aVisibleCols = array();
00261 
00262         // user defined some cols to load ?
00263         if ( is_array( $aUserCols ) ) {
00264             foreach ( $aUserCols as $iKey => $sCol ) {
00265                 $iCol = ( int ) str_replace( '_', '', $sCol );
00266                 if ( isset( $aColNames[$iCol] ) && !$aColNames[$iCol][4] ) {
00267                     $aVisibleCols[$iCol] = $aColNames[$iCol];
00268                 }
00269             }
00270         }
00271 
00272         // no user defined valid cols ? setting defauls ..
00273         if ( !count( $aVisibleCols ) ) {
00274             foreach ( $aColNames as $sName => $aCol ) {
00275                 if ( $aCol[1] && !$aColNames[$sName][4]  ) // visible ?
00276                     $aVisibleCols[$sName] = $aCol;
00277             }
00278         }
00279 
00280         return $aVisibleCols;
00281     }
00282 
00289     protected function _getQueryCols()
00290     {
00291         $sQ  = $this->_buildColsQuery( $this->_getVisibleColNames(), false ).", ";
00292         $sQ .= $this->_buildColsQuery( $this->_getIdentColNames() );
00293 
00294         return " $sQ ";
00295     }
00296 
00305     protected function _buildColsQuery( $aIdentCols, $blIdentCols = true )
00306     {
00307         $sQ = '';
00308         foreach ( $aIdentCols as $iCnt => $aCol ) {
00309             if ( $sQ ) {
00310                 $sQ .= ', ';
00311             }
00312 
00313             $sViewTable = $this->_getViewName( $aCol[1] );
00314             if ( !$blIdentCols && $this->_isExtendedColumn( $aCol[0] ) ) {
00315                 $sQ .= $this->_getExtendedColQuery( $sViewTable, $aCol[0], $iCnt );
00316             } else {
00317                 $sQ .=  $sViewTable. '.' . $aCol[0] . ' as _' . $iCnt;
00318             }
00319         }
00320 
00321         return $sQ;
00322     }
00323 
00332     protected function _isExtendedColumn( $sColumn )
00333     {
00334         $blBuild = false;
00335         if ( $this->_blAllowExtColumns && oxConfig::getInstance()->getConfigParam( 'blVariantsSelection' ) && $sColumn == 'oxtitle' ) {
00336             $blBuild = true;
00337         }
00338 
00339         return $blBuild;
00340     }
00341 
00352     protected function _getExtendedColQuery( $sViewTable, $sColumn, $iCnt )
00353     {
00354         // multilanguage
00355         $sVarSelect = "$sViewTable.oxvarselect";
00356         return " IF( {$sViewTable}.{$sColumn} != '', {$sViewTable}.{$sColumn}, CONCAT((select oxart.{$sColumn} from {$sViewTable} as oxart where oxart.oxid = {$sViewTable}.oxparentid),', ',{$sVarSelect})) as _{$iCnt}";
00357     }
00358 
00364     protected function _getSorting()
00365     {
00366         return ' order by _' . $this->_getSortCol() . ' '.$this->_getSortDir().' ';
00367     }
00368 
00376     protected function _getLimit( $iStart )
00377     {
00378         $iLimit = (int) oxConfig::getParameter("results");
00379         $iLimit = $iLimit?$iLimit:$this->_iSqlLimit;
00380 
00381         return " limit $iStart, $iLimit ";
00382     }
00383 
00389     protected function _getFilter()
00390     {
00391         $sQ = '';
00392         $aFilter = oxConfig::getParameter( 'aFilter' );
00393         if ( is_array( $aFilter ) && count( $aFilter ) ) {
00394             $aCols = $this->_getVisibleColNames();
00395             $blSep = false;
00396             $oDb = oxDb::getDb();
00397             $oLang = oxLang::getInstance();
00398             $oStr = getStr();
00399 
00400             $blIsUtf  = $this->getConfig()->isUtf();
00401             $sCharset = $oLang->translateString( "charset" );
00402 
00403             foreach ( $aFilter as $sCol => $sValue ) {
00404 
00405                 // skipping empty filters
00406                 if ( !$sValue ) {
00407                     continue;
00408                 }
00409 
00410                 $iCol = (int) str_replace( '_', '', $sCol );
00411                 if ( isset( $aCols[ $iCol ] ) ) {
00412                     if ( $sQ )
00413                         $sQ .= ' and ';
00414 
00415                     if ( !$blIsUtf ) {
00416                         $sValue = iconv( 'UTF-8', $sCharset, $sValue );
00417                     }
00418 
00419                     // escaping special characters
00420                     $sValue = str_replace( array( '%', '_' ), array( '\%', '\_' ), $sValue );
00421 
00422                     // possibility to search in the middle ..
00423                     $sValue = $oStr->preg_replace( '/^\*/', '%', $sValue );
00424 
00425                     $sQ .= $this->_getViewName( $aCols[ $iCol ][1] ) . '.' . $aCols[ $iCol ][0];
00426                     $sQ .= ' like ' . $oDb->Quote( $sValue . '%' ). ' ';
00427                 }
00428 
00429             }
00430         }
00431         return $sQ;
00432     }
00433 
00441     protected function _addFilter( $sQ )
00442     {
00443         if ( $sQ && ( $sFilter = $this->_getFilter() ) ) {
00444             $sQ .= ( ( stristr( $sQ, 'where' ) === false )?'where':' and ' ) . $sFilter;
00445         }
00446         return $sQ;
00447     }
00448 
00456     protected function _getAll( $sQ )
00457     {
00458         $aReturn = array();
00459         $rs = oxDb::getDb()->execute( $sQ );
00460         if ($rs != false && $rs->recordCount() > 0) {
00461             while (!$rs->EOF) {
00462                 $aReturn[] = $rs->fields[0];
00463                 $rs->moveNext();
00464             }
00465         }
00466         return $aReturn;
00467     }
00468 
00474     protected function _getSortDir()
00475     {
00476         $sDir = oxConfig::getParameter( 'dir' );
00477         if ( !in_array( $sDir, $this->_aPosDir ) ) {
00478             $sDir = $this->_aPosDir[0];
00479         }
00480 
00481         return $sDir;
00482     }
00483 
00489     protected function _getStartIndex()
00490     {
00491         return (int) oxConfig::getParameter( 'startIndex' );
00492     }
00493 
00501     protected function _getTotalCount( $sQ )
00502     {
00503         // TODO: implement caching here
00504 
00505         // we can cache total count ...
00506 
00507         // $sCountCacheKey = md5( $sQ );
00508 
00509         return (int) oxDb::getDb()->getOne( $sQ, false, false );
00510     }
00511 
00519     protected function _getDataFields( $sQ )
00520     {
00521         return oxDb::getDb( oxDB::FETCH_MODE_ASSOC )->getArray( $sQ, false, false );
00522     }
00523 
00531     protected function _outputResponse( $aData )
00532     {
00533         if ( !$this->getConfig()->isUtf() ) {
00534             // TODO: improve this
00535             if ( is_array( $aData['records'] ) && ( $iRecSize = count( $aData['records'] ) ) ) {
00536                 $aKeys = array_keys( current( $aData['records'] ) );
00537                 $iKeySize = count( $aKeys );
00538                 $sCharset = oxLang::getInstance()->translateString("charset");
00539                 for ( $i = 0; $i < $iRecSize; $i++ ) {
00540                     for ( $c = 0; $c < $iKeySize; $c++ ) {
00541                         $aData['records'][$i][$aKeys[$c]] = iconv( $sCharset, "UTF-8", $aData['records'][$i][$aKeys[$c]] );
00542                     }
00543                 }
00544             }
00545         }
00546 
00547         $this->_output( json_encode( $aData ) );
00548     }
00549 
00557     protected function _output( $sOut )
00558     {
00559         echo $sOut;
00560     }
00561 
00569     protected function _getViewName( $sTable )
00570     {
00571         return getViewName( $sTable, oxConfig::getParameter('editlanguage'));
00572     }
00573 
00582     protected function _getData( $sCountQ, $sQ )
00583     {
00584         $sQ = $this->_addFilter( $sQ );
00585         $sCountQ = $this->_addFilter( $sCountQ );
00586 
00587         $aResponse['startIndex'] = $iStart = $this->_getStartIndex();
00588         $aResponse['sort'] = '_' . $this->_getSortCol();
00589         $aResponse['dir']  = $this->_getSortDir();
00590 
00591         $iDebug = $this->getConfig()->getConfigParam( 'iDebug' );
00592         if ( $iDebug ) {
00593             $aResponse['countsql'] = $sCountQ;
00594         }
00595 
00596         $aResponse['records'] = array();
00597 
00598         // skip further execution if no records were found ...
00599         if ( ( $iTotal = $this->_getTotalCount( $sCountQ ) ) ) {
00600             $sQ .= $this->_getSorting();
00601             $sQ .= $this->_getLimit( $iStart );
00602 
00603             if ( $iDebug ) {
00604                 $aResponse['datasql'] = $sQ;
00605             }
00606 
00607             $aResponse['records'] = $this->_getDataFields( $sQ );
00608         }
00609 
00610         $aResponse['totalRecords'] = $iTotal;
00611 
00612         return $aResponse;
00613     }
00614 
00623     public function resetArtSeoUrl( $aArtIds, $aCatIds = null )
00624     {
00625         if ( empty( $aArtIds ) ) {
00626             return;
00627         }
00628 
00629         if ( !is_array( $aArtIds ) ) {
00630             $aArtIds = array( $aArtIds );
00631         }
00632 
00633         $blCleanCats = false;
00634         if ( $aCatIds ) {
00635             if ( !is_array( $aCatIds ) ) {
00636                 $aCatIds = array( $aCatIds );
00637             }
00638             $sShopId = $this->getConfig()->getShopId();
00639             $sQ = "delete from oxseo where oxtype='oxarticle' and oxobjectid='%s' and
00640                    oxshopid='{$sShopId}' and oxparams in (" . implode( ",", oxDb::getInstance()->quoteArray( $aCatIds ) ) . ")";
00641             $oDb = oxDb::getDb();
00642             $blCleanCats = true;
00643         }
00644 
00645         $sShopId = $this->getConfig()->getShopId();
00646         foreach ( $aArtIds as $sArtId ) {
00647             oxSeoEncoder::getInstance()->markAsExpired( $sArtId, $sShopId, 1, null, "oxtype='oxarticle'" );
00648             if ( $blCleanCats ) {
00649                 $oDb->execute( sprintf( $sQ, $sArtId ) );
00650             }
00651         }
00652     }
00653 
00659     public function resetContentCache()
00660     {
00661         $blDeleteCacheOnLogout = $this->getConfig()->getConfigParam( 'blClearCacheOnLogout' );
00662 
00663 
00664             if ( !$blDeleteCacheOnLogout ) {
00665                 oxUtils::getInstance()->oxResetFileCache();
00666             }
00667     }
00668 
00678     public function resetCounter( $sCounterType, $sValue = null )
00679     {
00680         $blDeleteCacheOnLogout = $this->getConfig()->getConfigParam( 'blClearCacheOnLogout' );
00681 
00682         if ( !$blDeleteCacheOnLogout ) {
00683             $myUtilsCount = oxUtilsCount::getInstance();
00684             switch ( $sCounterType ) {
00685                 case 'priceCatArticle':
00686                     $myUtilsCount->resetPriceCatArticleCount( $sValue );
00687                     break;
00688                 case 'catArticle':
00689                     $myUtilsCount->resetCatArticleCount( $sValue );
00690                     break;
00691                 case 'vendorArticle':
00692                     $myUtilsCount->resetVendorArticleCount( $sValue );
00693                     break;
00694                 case 'manufacturerArticle':
00695                     $myUtilsCount->resetManufacturerArticleCount( $sValue );
00696                     break;
00697             }
00698         }
00699     }
00700 
00701 }
00702 
00703 // processing ..
00704 $blAjaxCall = (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest');
00705 if ( $blAjaxCall ) {
00706 
00707 
00708     // Setting error reporting mode
00709     error_reporting( E_ALL ^ E_NOTICE);
00710 
00711     //setting basic configuration parameters
00712     ini_set('session.name', 'sid' );
00713     ini_set('session.use_cookies', 0 );
00714     ini_set('session.use_trans_sid', 0);
00715     ini_set('url_rewriter.tags', '');
00716     ini_set('magic_quotes_runtime', 0);
00717 
00718     // Generic utility method file.
00719     $sBasePath = getShopBasePath();
00720     include_once $sBasePath . 'modules/functions.php';
00721     include_once $sBasePath . 'core/oxfunctions.php';
00722     include_once $sBasePath . 'core/adodblite/adodb.inc.php';
00723     include_once $sBasePath . 'core/oxconfig.php';
00724     include_once $sBasePath . 'core/oxsupercfg.php';
00725 
00726 
00727     include_once $sBasePath . "core/oxutils.php";
00728 
00729     $myConfig = oxConfig::getInstance();
00730 
00731     // Includes Utility module.
00732     $sUtilModule = $myConfig->getConfigParam( 'sUtilModule' );
00733     if ( $sUtilModule && file_exists( getShopBasePath()."modules/".$sUtilModule ) )
00734         include_once getShopBasePath()."modules/".$sUtilModule;
00735 
00736     $myConfig->setConfigParam( 'blAdmin', true );
00737     $myConfig->setConfigParam( 'blTemplateCaching', false );
00738 
00739     // authorization
00740     if ( !(oxSession::getInstance()->checkSessionChallenge() && count(oxUtilsServer::getInstance()->getOxCookie()) && oxUtils::getInstance()->checkAccessRights())) {
00741         header( "location:index.php");
00742         oxUtils::getInstance()->showMessageAndExit( "" );
00743     }
00744 
00745     if ( $sContainer = oxConfig::getParameter( 'container' ) ) {
00746 
00747         $sContainer = trim(strtolower( basename( $sContainer ) ));
00748         
00749         try{
00750             $oAjaxComponent = oxNew($sContainer.'_ajax');
00751         }
00752         catch (oxSystemComponentException $oCe ){
00753             $sFile = 'inc/'.$sContainer.'.inc.php';
00754             if ( file_exists( $sFile ) ) {
00755                 $aColumns = array();
00756                 include_once $sFile;
00757                 $oAjaxComponent = new ajaxcomponent( $aColumns );
00758                 $oAjaxComponent->init( $aColumns );
00759             } else {
00760                 $oEx = oxNew ( 'oxFileException' );
00761                 $oEx->setMessage( 'EXCEPTION_FILENOTFOUND' );
00762                 $oEx->setFileName( $sFile );
00763                 $oEx->debugOut();
00764                 throw $oEx;
00765             }
00766         }        
00767 
00768         $oAjaxComponent->setName( $sContainer );
00769         $oAjaxComponent->processRequest( oxConfig::getParameter( 'fnc' ) );
00770 
00771     } else {
00772 
00773     }
00774 
00775     $myConfig->pageClose();
00776 
00777     // closing session handlers
00778     // session_write_close();
00779     return;
00780 }