ajaxlistcomponent.php

Go to the documentation of this file.
00001 <?php
00002 
00003 
00007 class ajaxListComponent extends oxSuperCfg
00008 {
00009 
00015     protected $_aPosDir = array('asc', 'desc');
00016 
00022     protected $_aColumns = array();
00023 
00029     protected $_iSqlLimit = 2500;
00030 
00036     protected $_sContainer = null;
00037 
00044     protected $_blAllowExtColumns = false;
00045 
00053     public function init($aColumns = null)
00054     {
00055         if (!is_null($aColumns)) {
00056             $this->setColumns($aColumns);
00057         }
00058     }
00059 
00065     public function getColumns()
00066     {
00067         return $this->_aColumns;
00068     }
00069 
00075     public function setColumns($aColumns)
00076     {
00077         $this->_aColumns = $aColumns;
00078     }
00079 
00089     protected function _getActionIds($sId)
00090     {
00091         $aColumns = $this->_getColNames();
00092         foreach ($aColumns as $iPos => $aCol) {
00093             if (isset($aCol[4]) && $aCol[4] == 1 && $sId == $aCol[1] . '.' . $aCol[0]) {
00094                 return oxRegistry::getConfig()->getRequestParameter('_' . $iPos);
00095             }
00096         }
00097     }
00098 
00104     public function setName($sName)
00105     {
00106         $this->_sContainer = $sName;
00107     }
00108 
00114     protected function _getQuery()
00115     {
00116         return '';
00117     }
00118 
00126     protected function _getDataQuery($sQ)
00127     {
00128         return 'select ' . $this->_getQueryCols() . $sQ;
00129     }
00130 
00138     protected function _getCountQuery($sQ)
00139     {
00140         return 'select count( * ) ' . $sQ;
00141     }
00142 
00148     public function processRequest($sFunction = null)
00149     {
00150         if ($sFunction) {
00151             $this->$sFunction();
00152 
00153 
00154         } else {
00155             $sQAdd = $this->_getQuery();
00156 
00157             // formatting SQL queries
00158             $sQ = $this->_getDataQuery($sQAdd);
00159             $sCountQ = $this->_getCountQuery($sQAdd);
00160 
00161             $this->_outputResponse($this->_getData($sCountQ, $sQ));
00162         }
00163     }
00164 
00170     protected function _getSortCol()
00171     {
00172         $aVisibleNames = $this->_getVisibleColNames();
00173         $iCol = oxRegistry::getConfig()->getRequestParameter('sort');
00174         $iCol = $iCol ? (( int ) str_replace('_', '', $iCol)) : 0;
00175         $iCol = (!isset($aVisibleNames[$iCol])) ? 0 : $iCol;
00176 
00177         return $iCol;
00178     }
00179 
00180 
00189     protected function _getColNames($sId = null)
00190     {
00191         if ($sId === null) {
00192             $sId = oxRegistry::getConfig()->getRequestParameter('cmpid');
00193         }
00194 
00195         if ($sId && isset($this->_aColumns[$sId])) {
00196             return $this->_aColumns[$sId];
00197         }
00198 
00199         return $this->_aColumns;
00200     }
00201 
00208     protected function _getIdentColNames()
00209     {
00210         $aColNames = $this->_getColNames();
00211         $aCols = array();
00212         foreach ($aColNames as $iKey => $aCol) {
00213             // ident ?
00214             if ($aCol[4]) {
00215                 $aCols[$iKey] = $aCol;
00216             }
00217         }
00218 
00219         return $aCols;
00220     }
00221 
00227     protected function _getVisibleColNames()
00228     {
00229         $aColNames = $this->_getColNames();
00230         $aUserCols = oxRegistry::getConfig()->getRequestParameter('aCols');
00231         $aVisibleCols = array();
00232 
00233         // user defined some cols to load ?
00234         if (is_array($aUserCols)) {
00235             foreach ($aUserCols as $iKey => $sCol) {
00236                 $iCol = ( int ) str_replace('_', '', $sCol);
00237                 if (isset($aColNames[$iCol]) && !$aColNames[$iCol][4]) {
00238                     $aVisibleCols[$iCol] = $aColNames[$iCol];
00239                 }
00240             }
00241         }
00242 
00243         // no user defined valid cols ? setting defauls ..
00244         if (!count($aVisibleCols)) {
00245             foreach ($aColNames as $sName => $aCol) {
00246                 // visible ?
00247                 if ($aCol[1] && !$aColNames[$sName][4]) {
00248                     $aVisibleCols[$sName] = $aCol;
00249                 }
00250             }
00251         }
00252 
00253         return $aVisibleCols;
00254     }
00255 
00262     protected function _getQueryCols()
00263     {
00264         $sQ = $this->_buildColsQuery($this->_getVisibleColNames(), false) . ", ";
00265         $sQ .= $this->_buildColsQuery($this->_getIdentColNames());
00266 
00267         return " $sQ ";
00268     }
00269 
00278     protected function _buildColsQuery($aIdentCols, $blIdentCols = true)
00279     {
00280         $sQ = '';
00281         foreach ($aIdentCols as $iCnt => $aCol) {
00282             if ($sQ) {
00283                 $sQ .= ', ';
00284             }
00285 
00286             $sViewTable = $this->_getViewName($aCol[1]);
00287             if (!$blIdentCols && $this->_isExtendedColumn($aCol[0])) {
00288                 $sQ .= $this->_getExtendedColQuery($sViewTable, $aCol[0], $iCnt);
00289             } else {
00290                 $sQ .= $sViewTable . '.' . $aCol[0] . ' as _' . $iCnt;
00291             }
00292         }
00293 
00294         return $sQ;
00295     }
00296 
00305     protected function _isExtendedColumn($sColumn)
00306     {
00307         $blBuild = false;
00308         $blVariantsSelectionParameter = oxRegistry::getConfig()->getConfigParam('blVariantsSelection');
00309         if ($this->_blAllowExtColumns && $blVariantsSelectionParameter && $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 
00331         $sSql = " IF( {$sViewTable}.{$sColumn} != '', {$sViewTable}.{$sColumn}, CONCAT((select oxart.{$sColumn} " .
00332                 "from {$sViewTable} as oxart " .
00333                 "where oxart.oxid = {$sViewTable}.oxparentid),', ',{$sVarSelect})) as _{$iCnt}";
00334 
00335         return $sSql;
00336     }
00337 
00343     protected function _getSorting()
00344     {
00345         return ' order by _' . $this->_getSortCol() . ' ' . $this->_getSortDir() . ' ';
00346     }
00347 
00355     protected function _getLimit($iStart)
00356     {
00357         $iLimit = (int) oxRegistry::getConfig()->getRequestParameter("results");
00358         $iLimit = $iLimit ? $iLimit : $this->_iSqlLimit;
00359 
00360         return " limit $iStart, $iLimit ";
00361     }
00362 
00368     protected function _getFilter()
00369     {
00370         $sQ = '';
00371         $oConfig = $this->getConfig();
00372         $aFilter = $oConfig->getRequestParameter('aFilter');
00373         if (is_array($aFilter) && count($aFilter)) {
00374             $aCols = $this->_getVisibleColNames();
00375             $oDb = oxDb::getDb();
00376             $oLang = oxRegistry::getLang();
00377             $oStr = getStr();
00378 
00379             $blIsUtf = $oConfig->isUtf();
00380             $sCharset = $oLang->translateString("charset");
00381 
00382             foreach ($aFilter as $sCol => $sValue) {
00383 
00384                 // skipping empty filters
00385                 if ($sValue === '') {
00386                     continue;
00387                 }
00388 
00389                 $iCol = (int) str_replace('_', '', $sCol);
00390                 if (isset($aCols[$iCol])) {
00391                     if ($sQ) {
00392                         $sQ .= ' and ';
00393                     }
00394 
00395                     if (!$blIsUtf) {
00396                         $sValue = iconv('UTF-8', $sCharset, $sValue);
00397                     }
00398 
00399                     // escaping special characters
00400                     $sValue = str_replace(array('%', '_'), array('\%', '\_'), $sValue);
00401 
00402                     // possibility to search in the middle ..
00403                     $sValue = $oStr->preg_replace('/^\*/', '%', $sValue);
00404 
00405                     $sQ .= $this->_getViewName($aCols[$iCol][1]) . '.' . $aCols[$iCol][0];
00406                     $sQ .= ' like ' . $oDb->Quote($sValue . '%') . ' ';
00407                 }
00408 
00409             }
00410         }
00411 
00412         return $sQ;
00413     }
00414 
00422     protected function _addFilter($sQ)
00423     {
00424         if ($sQ && ($sFilter = $this->_getFilter())) {
00425             $sQ .= ((stristr($sQ, 'where') === false) ? 'where' : ' and ') . $sFilter;
00426         }
00427 
00428         return $sQ;
00429     }
00430 
00438     protected function _getAll($sQ)
00439     {
00440         $aReturn = array();
00441         $rs = oxDb::getDb()->execute($sQ);
00442         if ($rs != false && $rs->recordCount() > 0) {
00443             while (!$rs->EOF) {
00444                 $aReturn[] = $rs->fields[0];
00445                 $rs->moveNext();
00446             }
00447         }
00448 
00449         return $aReturn;
00450     }
00451 
00457     protected function _getSortDir()
00458     {
00459         $sDir = oxRegistry::getConfig()->getRequestParameter('dir');
00460         if (!in_array($sDir, $this->_aPosDir)) {
00461             $sDir = $this->_aPosDir[0];
00462         }
00463 
00464         return $sDir;
00465     }
00466 
00472     protected function _getStartIndex()
00473     {
00474         return (int) oxRegistry::getConfig()->getRequestParameter('startIndex');
00475     }
00476 
00484     protected function _getTotalCount($sQ)
00485     {
00486         // TODO: implement caching here
00487 
00488         // we can cache total count ...
00489 
00490         // $sCountCacheKey = md5( $sQ );
00491 
00492         return (int) oxDb::getDb()->getOne($sQ, false, false);
00493     }
00494 
00502     protected function _getDataFields($sQ)
00503     {
00504         return oxDb::getDb(oxDB::FETCH_MODE_ASSOC)->getArray($sQ, false, false);
00505     }
00506 
00512     protected function _outputResponse($aData)
00513     {
00514         if (!$this->getConfig()->isUtf()) {
00515             // TODO: improve this
00516             if (is_array($aData['records']) && ($iRecSize = count($aData['records']))) {
00517                 $aKeys = array_keys(current($aData['records']));
00518                 $iKeySize = count($aKeys);
00519                 $sCharset = oxRegistry::getLang()->translateString("charset");
00520                 for ($i = 0; $i < $iRecSize; $i++) {
00521                     for ($c = 0; $c < $iKeySize; $c++) {
00522                         $aData['records'][$i][$aKeys[$c]] =
00523                                                         iconv($sCharset, "UTF-8", $aData['records'][$i][$aKeys[$c]]);
00524                     }
00525                 }
00526             }
00527         }
00528 
00529         $this->_output(json_encode($aData));
00530     }
00531 
00537     protected function _output($sOut)
00538     {
00539         echo $sOut;
00540     }
00541 
00549     protected function _getViewName($sTable)
00550     {
00551         return getViewName($sTable, oxRegistry::getConfig()->getRequestParameter('editlanguage'));
00552     }
00553 
00562     protected function _getData($sCountQ, $sQ)
00563     {
00564         $sQ = $this->_addFilter($sQ);
00565         $sCountQ = $this->_addFilter($sCountQ);
00566 
00567         $aResponse['startIndex'] = $iStart = $this->_getStartIndex();
00568         $aResponse['sort'] = '_' . $this->_getSortCol();
00569         $aResponse['dir'] = $this->_getSortDir();
00570 
00571         $iDebug = $this->getConfig()->getConfigParam('iDebug');
00572         if ($iDebug) {
00573             $aResponse['countsql'] = $sCountQ;
00574         }
00575 
00576         $aResponse['records'] = array();
00577 
00578         // skip further execution if no records were found ...
00579         if (($iTotal = $this->_getTotalCount($sCountQ))) {
00580             $sQ .= $this->_getSorting();
00581             $sQ .= $this->_getLimit($iStart);
00582 
00583             if ($iDebug) {
00584                 $aResponse['datasql'] = $sQ;
00585             }
00586 
00587             $aResponse['records'] = $this->_getDataFields($sQ);
00588         }
00589 
00590         $aResponse['totalRecords'] = $iTotal;
00591 
00592         return $aResponse;
00593     }
00594 
00603     public function resetArtSeoUrl($aArtIds, $aCatIds = null)
00604     {
00605         if (empty($aArtIds)) {
00606             return;
00607         }
00608 
00609         if (!is_array($aArtIds)) {
00610             $aArtIds = array($aArtIds);
00611         }
00612 
00613         $sShopId = $this->getConfig()->getShopId();
00614         foreach ($aArtIds as $sArtId) {
00616             oxRegistry::get("oxSeoEncoder")->markAsExpired($sArtId, $sShopId, 1, null, "oxtype='oxarticle'");
00617         }
00618     }
00619 
00623     public function resetContentCache()
00624     {
00625         $blDeleteCacheOnLogout = $this->getConfig()->getConfigParam('blClearCacheOnLogout');
00626 
00627         if (!$blDeleteCacheOnLogout) {
00628 
00629             oxRegistry::getUtils()->oxResetFileCache();
00630         }
00631     }
00632 
00640     public function resetCounter($sCounterType, $sValue = null)
00641     {
00642         $blDeleteCacheOnLogout = $this->getConfig()->getConfigParam('blClearCacheOnLogout');
00643 
00644         if (!$blDeleteCacheOnLogout) {
00645             $myUtilsCount = oxRegistry::get("oxUtilsCount");
00646             switch ($sCounterType) {
00647                 case 'priceCatArticle':
00648                     $myUtilsCount->resetPriceCatArticleCount($sValue);
00649                     break;
00650                 case 'catArticle':
00651                     $myUtilsCount->resetCatArticleCount($sValue);
00652                     break;
00653                 case 'vendorArticle':
00654                     $myUtilsCount->resetVendorArticleCount($sValue);
00655                     break;
00656                 case 'manufacturerArticle':
00657                     $myUtilsCount->resetManufacturerArticleCount($sValue);
00658                     break;
00659             }
00660 
00661             $this->_resetContentCache();
00662         }
00663     }
00664 
00668     protected function _resetContentCache()
00669     {
00670     }
00671 }