ajaxlistcomponent.php

Go to the documentation of this file.
00001 <?php
00002 
00003 
00007 class ajaxListComponent extends oxSuperCfg
00008 {
00014     protected $_aPosDir = array( 'asc', 'desc' );
00015 
00021     protected $_aColumns = array();
00022 
00028     protected $_iSqlLimit = 2500;
00029 
00035     protected $_sContainer = null;
00036 
00043     protected $_blAllowExtColumns = false;
00044 
00054     public function init( $aColumns = null )
00055     {
00056         if ( !is_null( $aColumns ) ) {
00057             $this->setColumns( $aColumns );
00058         }
00059     }
00060 
00066     public function getColumns()
00067     {
00068         return $this->_aColumns;
00069     }
00070 
00078     public function setColumns( $aColumns )
00079     {
00080         $this->_aColumns = $aColumns;
00081     }
00082 
00092     protected function _getActionIds( $sId )
00093     {
00094         $aColumns = $this->_getColNames();
00095         foreach ( $aColumns as $iPos => $aCol ) {
00096             if ( isset($aCol[4]) && $aCol[4] == 1 && $sId == $aCol[1].'.'.$aCol[0] ) {
00097                 return oxConfig::getParameter( '_'.$iPos );
00098             }
00099         }
00100     }
00101 
00109     public function setName( $sName )
00110     {
00111         $this->_sContainer = $sName;
00112     }
00113 
00119     protected function _getQuery()
00120     {
00121         return '';
00122     }
00123 
00131     protected function _getDataQuery( $sQ )
00132     {
00133         return 'select ' . $this->_getQueryCols() . $sQ;
00134     }
00135 
00143     protected function _getCountQuery( $sQ )
00144     {
00145         return 'select count( * ) ' . $sQ;
00146     }
00147 
00155     public function processRequest( $sFunction = null )
00156     {
00157         if ( $sFunction ) {
00158             $this->$sFunction();
00159 
00160 
00161         } else {
00162             $sQAdd = $this->_getQuery();
00163 
00164             // formatting SQL queries
00165             $sQ      = $this->_getDataQuery( $sQAdd );
00166             $sCountQ = $this->_getCountQuery( $sQAdd );
00167 
00168             $this->_outputResponse( $this->_getData( $sCountQ, $sQ ) );
00169         }
00170     }
00171 
00177     protected function _getSortCol()
00178     {
00179         $aVisibleNames = $this->_getVisibleColNames();
00180         $iCol = oxConfig::getParameter( 'sort' );
00181         $iCol = $iCol?( ( int ) str_replace( '_', '', $iCol )):0;
00182         $iCol = ( !isset( $aVisibleNames[$iCol] ) )?0:$iCol;
00183 
00184         return $iCol;
00185     }
00186 
00187 
00196     protected function _getColNames( $sId = null )
00197     {
00198         if ( $sId === null ) {
00199             $sId = oxConfig::getParameter( 'cmpid' );
00200         }
00201 
00202         if ( $sId && isset( $this->_aColumns[$sId] ) ) {
00203             return $this->_aColumns[$sId];
00204         }
00205 
00206         return $this->_aColumns;
00207     }
00208 
00215     protected function _getIdentColNames()
00216     {
00217         $aColNames = $this->_getColNames();
00218         $aCols = array();
00219         foreach ( $aColNames as $iKey =>  $aCol ) {
00220             if ( $aCol[4] ) // ident ?
00221                 $aCols[$iKey] = $aCol;
00222         }
00223         return $aCols;
00224     }
00225 
00231     protected function _getVisibleColNames()
00232     {
00233         $aColNames = $this->_getColNames();
00234         $aUserCols = oxConfig::getParameter( 'aCols' );
00235         $aVisibleCols = array();
00236 
00237         // user defined some cols to load ?
00238         if ( is_array( $aUserCols ) ) {
00239             foreach ( $aUserCols as $iKey => $sCol ) {
00240                 $iCol = ( int ) str_replace( '_', '', $sCol );
00241                 if ( isset( $aColNames[$iCol] ) && !$aColNames[$iCol][4] ) {
00242                     $aVisibleCols[$iCol] = $aColNames[$iCol];
00243                 }
00244             }
00245         }
00246 
00247         // no user defined valid cols ? setting defauls ..
00248         if ( !count( $aVisibleCols ) ) {
00249             foreach ( $aColNames as $sName => $aCol ) {
00250                 if ( $aCol[1] && !$aColNames[$sName][4]  ) // visible ?
00251                     $aVisibleCols[$sName] = $aCol;
00252             }
00253         }
00254 
00255         return $aVisibleCols;
00256     }
00257 
00264     protected function _getQueryCols()
00265     {
00266         $sQ  = $this->_buildColsQuery( $this->_getVisibleColNames(), false ).", ";
00267         $sQ .= $this->_buildColsQuery( $this->_getIdentColNames() );
00268 
00269         return " $sQ ";
00270     }
00271 
00280     protected function _buildColsQuery( $aIdentCols, $blIdentCols = true )
00281     {
00282         $sQ = '';
00283         foreach ( $aIdentCols as $iCnt => $aCol ) {
00284             if ( $sQ ) {
00285                 $sQ .= ', ';
00286             }
00287 
00288             $sViewTable = $this->_getViewName( $aCol[1] );
00289             if ( !$blIdentCols && $this->_isExtendedColumn( $aCol[0] ) ) {
00290                 $sQ .= $this->_getExtendedColQuery( $sViewTable, $aCol[0], $iCnt );
00291             } else {
00292                 $sQ .=  $sViewTable. '.' . $aCol[0] . ' as _' . $iCnt;
00293             }
00294         }
00295 
00296         return $sQ;
00297     }
00298 
00307     protected function _isExtendedColumn( $sColumn )
00308     {
00309         $blBuild = false;
00310         if ( $this->_blAllowExtColumns && oxRegistry::getConfig()->getConfigParam( 'blVariantsSelection' ) && $sColumn == 'oxtitle' ) {
00311             $blBuild = true;
00312         }
00313 
00314         return $blBuild;
00315     }
00316 
00327     protected function _getExtendedColQuery( $sViewTable, $sColumn, $iCnt )
00328     {
00329         // multilanguage
00330         $sVarSelect = "$sViewTable.oxvarselect";
00331         return " IF( {$sViewTable}.{$sColumn} != '', {$sViewTable}.{$sColumn}, CONCAT((select oxart.{$sColumn} from {$sViewTable} as oxart where oxart.oxid = {$sViewTable}.oxparentid),', ',{$sVarSelect})) as _{$iCnt}";
00332     }
00333 
00339     protected function _getSorting()
00340     {
00341         return ' order by _' . $this->_getSortCol() . ' '.$this->_getSortDir().' ';
00342     }
00343 
00351     protected function _getLimit( $iStart )
00352     {
00353         $iLimit = (int) oxConfig::getParameter("results");
00354         $iLimit = $iLimit?$iLimit:$this->_iSqlLimit;
00355 
00356         return " limit $iStart, $iLimit ";
00357     }
00358 
00364     protected function _getFilter()
00365     {
00366         $sQ = '';
00367         $aFilter = oxConfig::getParameter( 'aFilter' );
00368         if ( is_array( $aFilter ) && count( $aFilter ) ) {
00369             $aCols = $this->_getVisibleColNames();
00370             $blSep = false;
00371             $oDb = oxDb::getDb();
00372             $oLang = oxRegistry::getLang();
00373             $oStr = getStr();
00374 
00375             $blIsUtf  = $this->getConfig()->isUtf();
00376             $sCharset = $oLang->translateString( "charset" );
00377 
00378             foreach ( $aFilter as $sCol => $sValue ) {
00379 
00380                 // skipping empty filters
00381                 if ( !$sValue ) {
00382                     continue;
00383                 }
00384 
00385                 $iCol = (int) str_replace( '_', '', $sCol );
00386                 if ( isset( $aCols[ $iCol ] ) ) {
00387                     if ( $sQ )
00388                         $sQ .= ' and ';
00389 
00390                     if ( !$blIsUtf ) {
00391                         $sValue = iconv( 'UTF-8', $sCharset, $sValue );
00392                     }
00393 
00394                     // escaping special characters
00395                     $sValue = str_replace( array( '%', '_' ), array( '\%', '\_' ), $sValue );
00396 
00397                     // possibility to search in the middle ..
00398                     $sValue = $oStr->preg_replace( '/^\*/', '%', $sValue );
00399 
00400                     $sQ .= $this->_getViewName( $aCols[ $iCol ][1] ) . '.' . $aCols[ $iCol ][0];
00401                     $sQ .= ' like ' . $oDb->Quote( $sValue . '%' ). ' ';
00402                 }
00403 
00404             }
00405         }
00406         return $sQ;
00407     }
00408 
00416     protected function _addFilter( $sQ )
00417     {
00418         if ( $sQ && ( $sFilter = $this->_getFilter() ) ) {
00419             $sQ .= ( ( stristr( $sQ, 'where' ) === false )?'where':' and ' ) . $sFilter;
00420         }
00421         return $sQ;
00422     }
00423 
00431     protected function _getAll( $sQ )
00432     {
00433         $aReturn = array();
00434         $rs = oxDb::getDb()->execute( $sQ );
00435         if ($rs != false && $rs->recordCount() > 0) {
00436             while (!$rs->EOF) {
00437                 $aReturn[] = $rs->fields[0];
00438                 $rs->moveNext();
00439             }
00440         }
00441         return $aReturn;
00442     }
00443 
00449     protected function _getSortDir()
00450     {
00451         $sDir = oxConfig::getParameter( 'dir' );
00452         if ( !in_array( $sDir, $this->_aPosDir ) ) {
00453             $sDir = $this->_aPosDir[0];
00454         }
00455 
00456         return $sDir;
00457     }
00458 
00464     protected function _getStartIndex()
00465     {
00466         return (int) oxConfig::getParameter( 'startIndex' );
00467     }
00468 
00476     protected function _getTotalCount( $sQ )
00477     {
00478         // TODO: implement caching here
00479 
00480         // we can cache total count ...
00481 
00482         // $sCountCacheKey = md5( $sQ );
00483 
00484         return (int) oxDb::getDb()->getOne( $sQ, false, false );
00485     }
00486 
00494     protected function _getDataFields( $sQ )
00495     {
00496         return oxDb::getDb( oxDB::FETCH_MODE_ASSOC )->getArray( $sQ, false, false );
00497     }
00498 
00506     protected function _outputResponse( $aData )
00507     {
00508         if ( !$this->getConfig()->isUtf() ) {
00509             // TODO: improve this
00510             if ( is_array( $aData['records'] ) && ( $iRecSize = count( $aData['records'] ) ) ) {
00511                 $aKeys = array_keys( current( $aData['records'] ) );
00512                 $iKeySize = count( $aKeys );
00513                 $sCharset = oxRegistry::getLang()->translateString("charset");
00514                 for ( $i = 0; $i < $iRecSize; $i++ ) {
00515                     for ( $c = 0; $c < $iKeySize; $c++ ) {
00516                         $aData['records'][$i][$aKeys[$c]] = iconv( $sCharset, "UTF-8", $aData['records'][$i][$aKeys[$c]] );
00517                     }
00518                 }
00519             }
00520         }
00521 
00522         $this->_output( json_encode( $aData ) );
00523     }
00524 
00532     protected function _output( $sOut )
00533     {
00534         echo $sOut;
00535     }
00536 
00544     protected function _getViewName( $sTable )
00545     {
00546         return getViewName( $sTable, oxConfig::getParameter('editlanguage'));
00547     }
00548 
00557     protected function _getData( $sCountQ, $sQ )
00558     {
00559         $sQ = $this->_addFilter( $sQ );
00560         $sCountQ = $this->_addFilter( $sCountQ );
00561 
00562         $aResponse['startIndex'] = $iStart = $this->_getStartIndex();
00563         $aResponse['sort'] = '_' . $this->_getSortCol();
00564         $aResponse['dir']  = $this->_getSortDir();
00565 
00566         $iDebug = $this->getConfig()->getConfigParam( 'iDebug' );
00567         if ( $iDebug ) {
00568             $aResponse['countsql'] = $sCountQ;
00569         }
00570 
00571         $aResponse['records'] = array();
00572 
00573         // skip further execution if no records were found ...
00574         if ( ( $iTotal = $this->_getTotalCount( $sCountQ ) ) ) {
00575             $sQ .= $this->_getSorting();
00576             $sQ .= $this->_getLimit( $iStart );
00577 
00578             if ( $iDebug ) {
00579                 $aResponse['datasql'] = $sQ;
00580             }
00581 
00582             $aResponse['records'] = $this->_getDataFields( $sQ );
00583         }
00584 
00585         $aResponse['totalRecords'] = $iTotal;
00586 
00587         return $aResponse;
00588     }
00589 
00598     public function resetArtSeoUrl( $aArtIds, $aCatIds = null )
00599     {
00600         if ( empty( $aArtIds ) ) {
00601             return;
00602         }
00603 
00604         if ( !is_array( $aArtIds ) ) {
00605             $aArtIds = array( $aArtIds );
00606         }
00607 
00608         $blCleanCats = false;
00609         if ( $aCatIds ) {
00610             if ( !is_array( $aCatIds ) ) {
00611                 $aCatIds = array( $aCatIds );
00612             }
00613             $sShopId = $this->getConfig()->getShopId();
00614             $sQ = "delete from oxseo where oxtype='oxarticle' and oxobjectid='%s' and
00615                    oxshopid='{$sShopId}' and oxparams in (" . implode( ",", oxDb::getInstance()->quoteArray( $aCatIds ) ) . ")";
00616             $oDb = oxDb::getDb();
00617             $blCleanCats = true;
00618         }
00619 
00620         $sShopId = $this->getConfig()->getShopId();
00621         foreach ( $aArtIds as $sArtId ) {
00622             oxRegistry::get("oxSeoEncoder")->markAsExpired( $sArtId, $sShopId, 1, null, "oxtype='oxarticle'" );
00623             if ( $blCleanCats ) {
00624                 $oDb->execute( sprintf( $sQ, $sArtId ) );
00625             }
00626         }
00627     }
00628 
00634     public function resetContentCache()
00635     {
00636         $blDeleteCacheOnLogout = $this->getConfig()->getConfigParam( 'blClearCacheOnLogout' );
00637 
00638 
00639             if ( !$blDeleteCacheOnLogout ) {
00640                 oxRegistry::getUtils()->oxResetFileCache();
00641             }
00642     }
00643 
00653     public function resetCounter( $sCounterType, $sValue = null )
00654     {
00655         $blDeleteCacheOnLogout = $this->getConfig()->getConfigParam( 'blClearCacheOnLogout' );
00656 
00657         if ( !$blDeleteCacheOnLogout ) {
00658             $myUtilsCount = oxRegistry::get("oxUtilsCount");
00659             switch ( $sCounterType ) {
00660                 case 'priceCatArticle':
00661                     $myUtilsCount->resetPriceCatArticleCount( $sValue );
00662                     break;
00663                 case 'catArticle':
00664                     $myUtilsCount->resetCatArticleCount( $sValue );
00665                     break;
00666                 case 'vendorArticle':
00667                     $myUtilsCount->resetVendorArticleCount( $sValue );
00668                     break;
00669                 case 'manufacturerArticle':
00670                     $myUtilsCount->resetManufacturerArticleCount( $sValue );
00671                     break;
00672             }
00673         }
00674     }
00675 
00676 }