OXID eShop CE  4.8.11
 All Classes Files Functions Variables Pages
ajaxlistcomponent.php
Go to the documentation of this file.
1 <?php
2 
3 
8 {
14  protected $_aPosDir = array( 'asc', 'desc' );
15 
21  protected $_aColumns = array();
22 
28  protected $_iSqlLimit = 2500;
29 
35  protected $_sContainer = null;
36 
43  protected $_blAllowExtColumns = false;
44 
54  public function init( $aColumns = null )
55  {
56  if ( !is_null( $aColumns ) ) {
57  $this->setColumns( $aColumns );
58  }
59  }
60 
66  public function getColumns()
67  {
68  return $this->_aColumns;
69  }
70 
78  public function setColumns( $aColumns )
79  {
80  $this->_aColumns = $aColumns;
81  }
82 
92  protected function _getActionIds( $sId )
93  {
94  $aColumns = $this->_getColNames();
95  foreach ( $aColumns as $iPos => $aCol ) {
96  if ( isset($aCol[4]) && $aCol[4] == 1 && $sId == $aCol[1].'.'.$aCol[0] ) {
97  return oxConfig::getParameter( '_'.$iPos );
98  }
99  }
100  }
101 
109  public function setName( $sName )
110  {
111  $this->_sContainer = $sName;
112  }
113 
119  protected function _getQuery()
120  {
121  return '';
122  }
123 
131  protected function _getDataQuery( $sQ )
132  {
133  return 'select ' . $this->_getQueryCols() . $sQ;
134  }
135 
143  protected function _getCountQuery( $sQ )
144  {
145  return 'select count( * ) ' . $sQ;
146  }
147 
155  public function processRequest( $sFunction = null )
156  {
157  if ( $sFunction ) {
158  $this->$sFunction();
159 
160 
161  } else {
162  $sQAdd = $this->_getQuery();
163 
164  // formatting SQL queries
165  $sQ = $this->_getDataQuery( $sQAdd );
166  $sCountQ = $this->_getCountQuery( $sQAdd );
167 
168  $this->_outputResponse( $this->_getData( $sCountQ, $sQ ) );
169  }
170  }
171 
177  protected function _getSortCol()
178  {
179  $aVisibleNames = $this->_getVisibleColNames();
180  $iCol = oxConfig::getParameter( 'sort' );
181  $iCol = $iCol?( ( int ) str_replace( '_', '', $iCol )):0;
182  $iCol = ( !isset( $aVisibleNames[$iCol] ) )?0:$iCol;
183 
184  return $iCol;
185  }
186 
187 
196  protected function _getColNames( $sId = null )
197  {
198  if ( $sId === null ) {
199  $sId = oxConfig::getParameter( 'cmpid' );
200  }
201 
202  if ( $sId && isset( $this->_aColumns[$sId] ) ) {
203  return $this->_aColumns[$sId];
204  }
205 
206  return $this->_aColumns;
207  }
208 
215  protected function _getIdentColNames()
216  {
217  $aColNames = $this->_getColNames();
218  $aCols = array();
219  foreach ( $aColNames as $iKey => $aCol ) {
220  if ( $aCol[4] ) // ident ?
221  $aCols[$iKey] = $aCol;
222  }
223  return $aCols;
224  }
225 
231  protected function _getVisibleColNames()
232  {
233  $aColNames = $this->_getColNames();
234  $aUserCols = oxConfig::getParameter( 'aCols' );
235  $aVisibleCols = array();
236 
237  // user defined some cols to load ?
238  if ( is_array( $aUserCols ) ) {
239  foreach ( $aUserCols as $iKey => $sCol ) {
240  $iCol = ( int ) str_replace( '_', '', $sCol );
241  if ( isset( $aColNames[$iCol] ) && !$aColNames[$iCol][4] ) {
242  $aVisibleCols[$iCol] = $aColNames[$iCol];
243  }
244  }
245  }
246 
247  // no user defined valid cols ? setting defauls ..
248  if ( !count( $aVisibleCols ) ) {
249  foreach ( $aColNames as $sName => $aCol ) {
250  if ( $aCol[1] && !$aColNames[$sName][4] ) // visible ?
251  $aVisibleCols[$sName] = $aCol;
252  }
253  }
254 
255  return $aVisibleCols;
256  }
257 
264  protected function _getQueryCols()
265  {
266  $sQ = $this->_buildColsQuery( $this->_getVisibleColNames(), false ).", ";
267  $sQ .= $this->_buildColsQuery( $this->_getIdentColNames() );
268 
269  return " $sQ ";
270  }
271 
280  protected function _buildColsQuery( $aIdentCols, $blIdentCols = true )
281  {
282  $sQ = '';
283  foreach ( $aIdentCols as $iCnt => $aCol ) {
284  if ( $sQ ) {
285  $sQ .= ', ';
286  }
287 
288  $sViewTable = $this->_getViewName( $aCol[1] );
289  if ( !$blIdentCols && $this->_isExtendedColumn( $aCol[0] ) ) {
290  $sQ .= $this->_getExtendedColQuery( $sViewTable, $aCol[0], $iCnt );
291  } else {
292  $sQ .= $sViewTable. '.' . $aCol[0] . ' as _' . $iCnt;
293  }
294  }
295 
296  return $sQ;
297  }
298 
307  protected function _isExtendedColumn( $sColumn )
308  {
309  $blBuild = false;
310  if ( $this->_blAllowExtColumns && oxRegistry::getConfig()->getConfigParam( 'blVariantsSelection' ) && $sColumn == 'oxtitle' ) {
311  $blBuild = true;
312  }
313 
314  return $blBuild;
315  }
316 
327  protected function _getExtendedColQuery( $sViewTable, $sColumn, $iCnt )
328  {
329  // multilanguage
330  $sVarSelect = "$sViewTable.oxvarselect";
331  return " IF( {$sViewTable}.{$sColumn} != '', {$sViewTable}.{$sColumn}, CONCAT((select oxart.{$sColumn} from {$sViewTable} as oxart where oxart.oxid = {$sViewTable}.oxparentid),', ',{$sVarSelect})) as _{$iCnt}";
332  }
333 
339  protected function _getSorting()
340  {
341  return ' order by _' . $this->_getSortCol() . ' '.$this->_getSortDir().' ';
342  }
343 
351  protected function _getLimit( $iStart )
352  {
353  $iLimit = (int) oxConfig::getParameter("results");
354  $iLimit = $iLimit?$iLimit:$this->_iSqlLimit;
355 
356  return " limit $iStart, $iLimit ";
357  }
358 
364  protected function _getFilter()
365  {
366  $sQ = '';
367  $oConfig = $this->getConfig();
368  $aFilter = $oConfig->getRequestParameter( 'aFilter' );
369  if ( is_array( $aFilter ) && count( $aFilter ) ) {
370  $aCols = $this->_getVisibleColNames();
371  $oDb = oxDb::getDb();
372  $oLang = oxRegistry::getLang();
373  $oStr = getStr();
374 
375  $blIsUtf = $oConfig->isUtf();
376  $sCharset = $oLang->translateString( "charset" );
377 
378  foreach ( $aFilter as $sCol => $sValue ) {
379 
380  // skipping empty filters
381  if ( $sValue == '' && $sValue !== 0 && $sValue !== '0' ) {
382  continue;
383  }
384 
385  $iCol = (int) str_replace( '_', '', $sCol );
386  if ( isset( $aCols[ $iCol ] ) ) {
387  if ( $sQ )
388  $sQ .= ' and ';
389 
390  if ( !$blIsUtf ) {
391  $sValue = iconv( 'UTF-8', $sCharset, $sValue );
392  }
393 
394  // escaping special characters
395  $sValue = str_replace( array( '%', '_' ), array( '\%', '\_' ), $sValue );
396 
397  // possibility to search in the middle ..
398  $sValue = $oStr->preg_replace( '/^\*/', '%', $sValue );
399 
400  $sQ .= $this->_getViewName( $aCols[ $iCol ][1] ) . '.' . $aCols[ $iCol ][0];
401  $sQ .= ' like ' . $oDb->Quote( $sValue . '%' ). ' ';
402  }
403 
404  }
405  }
406  return $sQ;
407  }
408 
416  protected function _addFilter( $sQ )
417  {
418  if ( $sQ && ( $sFilter = $this->_getFilter() ) ) {
419  $sQ .= ( ( stristr( $sQ, 'where' ) === false )?'where':' and ' ) . $sFilter;
420  }
421  return $sQ;
422  }
423 
431  protected function _getAll( $sQ )
432  {
433  $aReturn = array();
434  $rs = oxDb::getDb()->execute( $sQ );
435  if ($rs != false && $rs->recordCount() > 0) {
436  while (!$rs->EOF) {
437  $aReturn[] = $rs->fields[0];
438  $rs->moveNext();
439  }
440  }
441  return $aReturn;
442  }
443 
449  protected function _getSortDir()
450  {
451  $sDir = oxConfig::getParameter( 'dir' );
452  if ( !in_array( $sDir, $this->_aPosDir ) ) {
453  $sDir = $this->_aPosDir[0];
454  }
455 
456  return $sDir;
457  }
458 
464  protected function _getStartIndex()
465  {
466  return (int) oxConfig::getParameter( 'startIndex' );
467  }
468 
476  protected function _getTotalCount( $sQ )
477  {
478  // TODO: implement caching here
479 
480  // we can cache total count ...
481 
482  // $sCountCacheKey = md5( $sQ );
483 
484  return (int) oxDb::getDb()->getOne( $sQ, false, false );
485  }
486 
494  protected function _getDataFields( $sQ )
495  {
496  return oxDb::getDb( oxDB::FETCH_MODE_ASSOC )->getArray( $sQ, false, false );
497  }
498 
506  protected function _outputResponse( $aData )
507  {
508  if ( !$this->getConfig()->isUtf() ) {
509  // TODO: improve this
510  if ( is_array( $aData['records'] ) && ( $iRecSize = count( $aData['records'] ) ) ) {
511  $aKeys = array_keys( current( $aData['records'] ) );
512  $iKeySize = count( $aKeys );
513  $sCharset = oxRegistry::getLang()->translateString("charset");
514  for ( $i = 0; $i < $iRecSize; $i++ ) {
515  for ( $c = 0; $c < $iKeySize; $c++ ) {
516  $aData['records'][$i][$aKeys[$c]] = iconv( $sCharset, "UTF-8", $aData['records'][$i][$aKeys[$c]] );
517  }
518  }
519  }
520  }
521 
522  $this->_output( json_encode( $aData ) );
523  }
524 
532  protected function _output( $sOut )
533  {
534  echo $sOut;
535  }
536 
544  protected function _getViewName( $sTable )
545  {
546  return getViewName( $sTable, oxConfig::getParameter('editlanguage'));
547  }
548 
557  protected function _getData( $sCountQ, $sQ )
558  {
559  $sQ = $this->_addFilter( $sQ );
560  $sCountQ = $this->_addFilter( $sCountQ );
561 
562  $aResponse['startIndex'] = $iStart = $this->_getStartIndex();
563  $aResponse['sort'] = '_' . $this->_getSortCol();
564  $aResponse['dir'] = $this->_getSortDir();
565 
566  $iDebug = $this->getConfig()->getConfigParam( 'iDebug' );
567  if ( $iDebug ) {
568  $aResponse['countsql'] = $sCountQ;
569  }
570 
571  $aResponse['records'] = array();
572 
573  // skip further execution if no records were found ...
574  if ( ( $iTotal = $this->_getTotalCount( $sCountQ ) ) ) {
575  $sQ .= $this->_getSorting();
576  $sQ .= $this->_getLimit( $iStart );
577 
578  if ( $iDebug ) {
579  $aResponse['datasql'] = $sQ;
580  }
581 
582  $aResponse['records'] = $this->_getDataFields( $sQ );
583  }
584 
585  $aResponse['totalRecords'] = $iTotal;
586 
587  return $aResponse;
588  }
589 
598  public function resetArtSeoUrl( $aArtIds, $aCatIds = null )
599  {
600  if ( empty( $aArtIds ) ) {
601  return;
602  }
603 
604  if ( !is_array( $aArtIds ) ) {
605  $aArtIds = array( $aArtIds );
606  }
607 
608  $blCleanCats = false;
609  if ( $aCatIds ) {
610  if ( !is_array( $aCatIds ) ) {
611  $aCatIds = array( $aCatIds );
612  }
613  $sShopId = $this->getConfig()->getShopId();
614  $sQ = "delete from oxseo where oxtype='oxarticle' and oxobjectid='%s' and
615  oxshopid='{$sShopId}' and oxparams in (" . implode( ",", oxDb::getInstance()->quoteArray( $aCatIds ) ) . ")";
616  $oDb = oxDb::getDb();
617  $blCleanCats = true;
618  }
619 
620  $sShopId = $this->getConfig()->getShopId();
621  foreach ( $aArtIds as $sArtId ) {
622  oxRegistry::get("oxSeoEncoder")->markAsExpired( $sArtId, $sShopId, 1, null, "oxtype='oxarticle'" );
623  if ( $blCleanCats ) {
624  $oDb->execute( sprintf( $sQ, $sArtId ) );
625  }
626  }
627  }
628 
634  public function resetContentCache()
635  {
636  $blDeleteCacheOnLogout = $this->getConfig()->getConfigParam( 'blClearCacheOnLogout' );
637 
638 
639  if ( !$blDeleteCacheOnLogout ) {
640  oxRegistry::getUtils()->oxResetFileCache();
641  }
642  }
643 
653  public function resetCounter( $sCounterType, $sValue = null )
654  {
655  $blDeleteCacheOnLogout = $this->getConfig()->getConfigParam( 'blClearCacheOnLogout' );
656 
657  if ( !$blDeleteCacheOnLogout ) {
658  $myUtilsCount = oxRegistry::get("oxUtilsCount");
659  switch ( $sCounterType ) {
660  case 'priceCatArticle':
661  $myUtilsCount->resetPriceCatArticleCount( $sValue );
662  break;
663  case 'catArticle':
664  $myUtilsCount->resetCatArticleCount( $sValue );
665  break;
666  case 'vendorArticle':
667  $myUtilsCount->resetVendorArticleCount( $sValue );
668  break;
669  case 'manufacturerArticle':
670  $myUtilsCount->resetManufacturerArticleCount( $sValue );
671  break;
672  }
673  }
674  }
675 
676 }