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         $sQ = '';
00227         $blSep = false;
00228         $aVisiblecols = $this->_getVisibleColNames();
00229         foreach ( $aVisiblecols as $iCnt => $aCol ) {
00230             if ( $blSep )
00231                 $sQ .= ', ';
00232 
00233             // multijanguage
00234             $sCol = $aCol[3]?$aCol[0].oxLang::getInstance()->getLanguageTag():$aCol[0];
00235             $sQ  .= getViewName( $aCol[1] ) . '.' . $sCol . ' as _' . $iCnt;
00236             $blSep = true;
00237         }
00238 
00239         $aIdentCols = $this->_getIdentColNames();
00240         foreach ( $aIdentCols as $iCnt => $aCol ) {
00241             if ( $blSep )
00242                 $sQ .= ', ';
00243 
00244             // multijanguage
00245             $sCol = $aCol[3]?$aCol[0].oxLang::getInstance()->getLanguageTag():$aCol[0];
00246             $sQ  .= getViewName( $aCol[1] ) . '.' . $sCol . ' as _' . $iCnt;
00247         }
00248 
00249         return " $sQ ";
00250     }
00251 
00257     protected function _getSorting()
00258     {
00259         $aVisibleCols = $this->_getVisibleColNames();
00260         $aSortCol = $aVisibleCols[ $this->_getSortCol() ];
00261         $sCol = $aSortCol[3]?$aSortCol[0].oxLang::getInstance()->getLanguageTag():$aSortCol[0];
00262         return ' order by ' . getViewName( $aSortCol[1] ) . '.' . $sCol . ' '.$this->_getSortDir().' ';
00263     }
00264 
00272     protected function _getLimit( $iStart )
00273     {
00274         $iLimit = (int) oxConfig::getParameter("results");
00275         $iLimit = $iLimit?$iLimit:$this->_iSqlLimit;
00276 
00277         return " limit $iStart, $iLimit ";
00278     }
00279 
00285     protected function _getFilter()
00286     {
00287         $sQ = '';
00288         $aFilter = oxConfig::getParameter( 'aFilter' );
00289         if ( is_array( $aFilter ) && count( $aFilter ) ) {
00290             $aCols = $this->_getVisibleColNames();
00291             $blSep = false;
00292             foreach ( $aFilter as $sCol => $sValue ) {
00293 
00294                 // skipping empty filters
00295                 if ( !$sValue ) {
00296                     continue;
00297                 }
00298 
00299                 $iCol = (int) str_replace( '_', '', $sCol );
00300                 if ( isset( $aCols[ $iCol ] ) ) {
00301                     if ( $sQ )
00302                         $sQ .= ' and ';
00303 
00304                     // escaping special characters
00305                     $sValue = str_replace( array( '%', '_' ), array( '\%', '\_' ), $sValue );
00306 
00307                     // possibility to search in the middle ..
00308                     $sValue = preg_replace( '/^\*/', '%', $sValue );
00309 
00310                     $sCol = $aCols[ $iCol ][3]?$aCols[ $iCol ][0].oxLang::getInstance()->getLanguageTag():$aCols[ $iCol ][0];
00311                     $sQ .= getViewName( $aCols[ $iCol ][1] ) . '.' . $sCol;
00312                     $sQ .= ' like ' . oxDb::getDb()->Quote( $sValue . '%' ). ' ';
00313                 }
00314 
00315             }
00316         }
00317         return $sQ;
00318     }
00319 
00327     protected function _addFilter( $sQ )
00328     {
00329         if ( $sQ && $sFilter = $this->_getFilter() ) {
00330             $sQ .= ( ( stristr( $sQ, 'where' ) === false )?'where':' and ' ) . $sFilter;
00331         }
00332         return $sQ;
00333     }
00334 
00342     protected function _getAll( $sQ )
00343     {
00344         $aReturn = array();
00345         $rs = oxDb::getDb()->Execute( $sQ );
00346         if ($rs != false && $rs->recordCount() > 0) {
00347             while (!$rs->EOF) {
00348                 $aReturn[] = $rs->fields[0];
00349                 $rs->moveNext();
00350             }
00351         }
00352         return $aReturn;
00353     }
00354 
00360     protected function _getSortDir()
00361     {
00362         $sDir = oxConfig::getParameter( 'dir' );
00363         if ( !in_array( $sDir, $this->_aPosDir ) ) {
00364             $sDir = $this->_aPosDir[0];
00365         }
00366 
00367         return $sDir;
00368     }
00369 
00375     protected function _getStartIndex()
00376     {
00377         return (int) oxConfig::getParameter( 'startIndex' );
00378     }
00379 
00387     protected function _getTotalCount( $sQ )
00388     {
00389         // TODO: implement caching here
00390 
00391         // we can cache total count ...
00392 
00393         // $sCountCacheKey = md5( $sQ );
00394 
00395         return (int) oxDb::getDb()->getOne( $sQ );
00396     }
00397 
00405     protected function _getDataFields( $sQ )
00406     {
00407         return oxDb::getDb(true)->getArray( $sQ );
00408     }
00409 
00417     protected function _outputResponse( $aData )
00418     {
00419         // TODO: improve this
00420         if ( is_array( $aData['records'] ) && $iRecSize = count( $aData['records'] ) ) {
00421             $aKeys = array_keys( current( $aData['records'] ) );
00422             $iKeySize = count( $aKeys );
00423             for ( $i = 0; $i < $iRecSize; $i++ ) {
00424                 for ( $c = 0; $c < $iKeySize; $c++ ) {
00425                     $aData['records'][$i][$aKeys[$c]] = iconv("ISO-8859-1", "UTF-8", $aData['records'][$i][$aKeys[$c]] );
00426                 }
00427             }
00428         }
00429 
00430         echo json_encode( $aData );
00431     }
00432 
00441     protected function _getData( $sCountQ, $sQ )
00442     {
00443         $sQ = $this->_addFilter( $sQ );
00444         $sCountQ = $this->_addFilter( $sCountQ );
00445 
00446         $aResponse['startIndex'] = $iStart = $this->_getStartIndex();
00447         $aResponse['sort'] = '_' . $this->_getSortCol();
00448         $aResponse['dir']  = $this->_getSortDir();
00449 
00450         $iDebug = $this->getConfig()->getConfigParam( 'iDebug' );
00451         if ( $iDebug ) {
00452             $aResponse['countsql'] = $sCountQ;
00453         }
00454 
00455         $aResponse['records'] = array();
00456 
00457         // skip further execution if no records were found ...
00458         if ( ( $iTotal = $this->_getTotalCount( $sCountQ ) ) ) {
00459             $sQ .= $this->_getSorting();
00460             $sQ .= $this->_getLimit( $iStart );
00461 
00462             if ( $iDebug ) {
00463                 $aResponse['datasql'] = $sQ;
00464             }
00465 
00466             $aResponse['records'] = $this->_getDataFields( $sQ );
00467         }
00468 
00469         $aResponse['totalRecords'] = $iTotal;
00470 
00471         return $aResponse;
00472     }
00473 
00474 
00475 }
00476 
00477 // processing ..
00478 $blAjaxCall = (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest');
00479 if ( $blAjaxCall ) {
00480 
00481 
00482     // Setting error reporting mode
00483     error_reporting( E_ALL ^ E_NOTICE);
00484 
00485     //setting basic configuration parameters
00486     ini_set('session.name', 'sid' );
00487     ini_set('session.use_cookies', 0 );
00488     ini_set('session.use_trans_sid', 0);
00489     ini_set('url_rewriter.tags', '');
00490     ini_set('magic_quotes_runtime', 0);
00491 
00492     // Generic utility method file.
00493     $sBasePath = getShopBasePath();
00494     include_once $sBasePath . 'core/oxfunctions.php';
00495     include_once $sBasePath . 'core/adodblite/adodb.inc.php';
00496     include_once $sBasePath . 'core/oxconfig.php';
00497     include_once $sBasePath . 'core/oxsupercfg.php';
00498 
00499 
00500     // TODO: check this some day before release :)
00501     include_once $sBasePath . "core/oxutils.php";
00502 
00503     $myConfig = oxConfig::getInstance();
00504 
00505     //TODO change this
00506     // Includes Utility module.
00507     $sUtilModule = $myConfig->getConfigParam( 'sUtilModule' );
00508     if ( $sUtilModule && file_exists( getShopBasePath()."modules/".$sUtilModule ) )
00509         include_once getShopBasePath()."modules/".$sUtilModule;
00510 
00511     $myConfig->setConfigParam( 'blAdmin', true );
00512     $myConfig->setConfigParam( 'blTemplateCaching', false );
00513 
00514     // authorization
00515     if ( !count(oxUtilsServer::getInstance()->getOxCookie()) || !oxUtils::getInstance()->checkAccessRights()) {
00516         header( "location:index.php");
00517         exit();
00518     }
00519 
00520     // must check if function "json_encode" exists !!!
00521 
00522     if ( $sContainer = oxConfig::getParameter( 'container' ) ) {
00523 
00524         $sContainer = strtolower( basename( $sContainer ) );
00525         $aColumns = array();
00526         include_once 'inc/'.$sContainer.'.inc.php';
00527 
00528         $oAjaxComponent = new ajaxcomponent( $aColumns );
00529         $oAjaxComponent->setName( $sContainer );
00530         $oAjaxComponent->processRequest( oxConfig::getParameter( 'fnc' ) );
00531 
00532     } else {
00533 
00534     }
00535 
00536     // closing session handlers
00537     // session_write_close();
00538     return;
00539 }

Generated on Thu Dec 4 12:04:55 2008 for OXID eShop CE by  doxygen 1.5.5