OXID eShop CE  4.9.6
 All Classes Files Functions Variables Pages
ajaxlistcomponent.php
Go to the documentation of this file.
1 <?php
2 
3 
8 {
9 
15  protected $_aPosDir = array('asc', 'desc');
16 
22  protected $_aColumns = array();
23 
29  protected $_iSqlLimit = 2500;
30 
36  protected $_sContainer = null;
37 
44  protected $_blAllowExtColumns = false;
45 
53  public function init($aColumns = null)
54  {
55  if (!is_null($aColumns)) {
56  $this->setColumns($aColumns);
57  }
58  }
59 
65  public function getColumns()
66  {
67  return $this->_aColumns;
68  }
69 
75  public function setColumns($aColumns)
76  {
77  $this->_aColumns = $aColumns;
78  }
79 
89  protected function _getActionIds($sId)
90  {
91  $aColumns = $this->_getColNames();
92  foreach ($aColumns as $iPos => $aCol) {
93  if (isset($aCol[4]) && $aCol[4] == 1 && $sId == $aCol[1] . '.' . $aCol[0]) {
94  return oxRegistry::getConfig()->getRequestParameter('_' . $iPos);
95  }
96  }
97  }
98 
104  public function setName($sName)
105  {
106  $this->_sContainer = $sName;
107  }
108 
114  protected function _getQuery()
115  {
116  return '';
117  }
118 
126  protected function _getDataQuery($sQ)
127  {
128  return 'select ' . $this->_getQueryCols() . $sQ;
129  }
130 
138  protected function _getCountQuery($sQ)
139  {
140  return 'select count( * ) ' . $sQ;
141  }
142 
148  public function processRequest($sFunction = null)
149  {
150  if ($sFunction) {
151  $this->$sFunction();
152 
153 
154  } else {
155  $sQAdd = $this->_getQuery();
156 
157  // formatting SQL queries
158  $sQ = $this->_getDataQuery($sQAdd);
159  $sCountQ = $this->_getCountQuery($sQAdd);
160 
161  $this->_outputResponse($this->_getData($sCountQ, $sQ));
162  }
163  }
164 
170  protected function _getSortCol()
171  {
172  $aVisibleNames = $this->_getVisibleColNames();
173  $iCol = oxRegistry::getConfig()->getRequestParameter('sort');
174  $iCol = $iCol ? (( int ) str_replace('_', '', $iCol)) : 0;
175  $iCol = (!isset($aVisibleNames[$iCol])) ? 0 : $iCol;
176 
177  return $iCol;
178  }
179 
180 
189  protected function _getColNames($sId = null)
190  {
191  if ($sId === null) {
192  $sId = oxRegistry::getConfig()->getRequestParameter('cmpid');
193  }
194 
195  if ($sId && isset($this->_aColumns[$sId])) {
196  return $this->_aColumns[$sId];
197  }
198 
199  return $this->_aColumns;
200  }
201 
208  protected function _getIdentColNames()
209  {
210  $aColNames = $this->_getColNames();
211  $aCols = array();
212  foreach ($aColNames as $iKey => $aCol) {
213  // ident ?
214  if ($aCol[4]) {
215  $aCols[$iKey] = $aCol;
216  }
217  }
218 
219  return $aCols;
220  }
221 
227  protected function _getVisibleColNames()
228  {
229  $aColNames = $this->_getColNames();
230  $aUserCols = oxRegistry::getConfig()->getRequestParameter('aCols');
231  $aVisibleCols = array();
232 
233  // user defined some cols to load ?
234  if (is_array($aUserCols)) {
235  foreach ($aUserCols as $iKey => $sCol) {
236  $iCol = ( int ) str_replace('_', '', $sCol);
237  if (isset($aColNames[$iCol]) && !$aColNames[$iCol][4]) {
238  $aVisibleCols[$iCol] = $aColNames[$iCol];
239  }
240  }
241  }
242 
243  // no user defined valid cols ? setting defauls ..
244  if (!count($aVisibleCols)) {
245  foreach ($aColNames as $sName => $aCol) {
246  // visible ?
247  if ($aCol[1] && !$aColNames[$sName][4]) {
248  $aVisibleCols[$sName] = $aCol;
249  }
250  }
251  }
252 
253  return $aVisibleCols;
254  }
255 
262  protected function _getQueryCols()
263  {
264  $sQ = $this->_buildColsQuery($this->_getVisibleColNames(), false) . ", ";
265  $sQ .= $this->_buildColsQuery($this->_getIdentColNames());
266 
267  return " $sQ ";
268  }
269 
278  protected function _buildColsQuery($aIdentCols, $blIdentCols = true)
279  {
280  $sQ = '';
281  foreach ($aIdentCols as $iCnt => $aCol) {
282  if ($sQ) {
283  $sQ .= ', ';
284  }
285 
286  $sViewTable = $this->_getViewName($aCol[1]);
287  if (!$blIdentCols && $this->_isExtendedColumn($aCol[0])) {
288  $sQ .= $this->_getExtendedColQuery($sViewTable, $aCol[0], $iCnt);
289  } else {
290  $sQ .= $sViewTable . '.' . $aCol[0] . ' as _' . $iCnt;
291  }
292  }
293 
294  return $sQ;
295  }
296 
305  protected function _isExtendedColumn($sColumn)
306  {
307  $blBuild = false;
308  $blVariantsSelectionParameter = oxRegistry::getConfig()->getConfigParam('blVariantsSelection');
309  if ($this->_blAllowExtColumns && $blVariantsSelectionParameter && $sColumn == 'oxtitle') {
310  $blBuild = true;
311  }
312 
313  return $blBuild;
314  }
315 
326  protected function _getExtendedColQuery($sViewTable, $sColumn, $iCnt)
327  {
328  // multilanguage
329  $sVarSelect = "$sViewTable.oxvarselect";
330 
331  $sSql = " IF( {$sViewTable}.{$sColumn} != '', {$sViewTable}.{$sColumn}, CONCAT((select oxart.{$sColumn} " .
332  "from {$sViewTable} as oxart " .
333  "where oxart.oxid = {$sViewTable}.oxparentid),', ',{$sVarSelect})) as _{$iCnt}";
334 
335  return $sSql;
336  }
337 
343  protected function _getSorting()
344  {
345  return ' order by _' . $this->_getSortCol() . ' ' . $this->_getSortDir() . ' ';
346  }
347 
355  protected function _getLimit($iStart)
356  {
357  $iLimit = (int) oxRegistry::getConfig()->getRequestParameter("results");
358  $iLimit = $iLimit ? $iLimit : $this->_iSqlLimit;
359 
360  return " limit $iStart, $iLimit ";
361  }
362 
368  protected function _getFilter()
369  {
370  $sQ = '';
371  $oConfig = $this->getConfig();
372  $aFilter = $oConfig->getRequestParameter('aFilter');
373  if (is_array($aFilter) && count($aFilter)) {
374  $aCols = $this->_getVisibleColNames();
375  $oDb = oxDb::getDb();
376  $oLang = oxRegistry::getLang();
377  $oStr = getStr();
378 
379  $blIsUtf = $oConfig->isUtf();
380  $sCharset = $oLang->translateString("charset");
381 
382  foreach ($aFilter as $sCol => $sValue) {
383 
384  // skipping empty filters
385  if ($sValue === '') {
386  continue;
387  }
388 
389  $iCol = (int) str_replace('_', '', $sCol);
390  if (isset($aCols[$iCol])) {
391  if ($sQ) {
392  $sQ .= ' and ';
393  }
394 
395  if (!$blIsUtf) {
396  $sValue = iconv('UTF-8', $sCharset, $sValue);
397  }
398 
399  // escaping special characters
400  $sValue = str_replace(array('%', '_'), array('\%', '\_'), $sValue);
401 
402  // possibility to search in the middle ..
403  $sValue = $oStr->preg_replace('/^\*/', '%', $sValue);
404 
405  $sQ .= $this->_getViewName($aCols[$iCol][1]) . '.' . $aCols[$iCol][0];
406  $sQ .= ' like ' . $oDb->Quote($sValue . '%') . ' ';
407  }
408 
409  }
410  }
411 
412  return $sQ;
413  }
414 
422  protected function _addFilter($sQ)
423  {
424  if ($sQ && ($sFilter = $this->_getFilter())) {
425  $sQ .= ((stristr($sQ, 'where') === false) ? 'where' : ' and ') . $sFilter;
426  }
427 
428  return $sQ;
429  }
430 
438  protected function _getAll($sQ)
439  {
440  $aReturn = array();
441  $rs = oxDb::getDb()->execute($sQ);
442  if ($rs != false && $rs->recordCount() > 0) {
443  while (!$rs->EOF) {
444  $aReturn[] = $rs->fields[0];
445  $rs->moveNext();
446  }
447  }
448 
449  return $aReturn;
450  }
451 
457  protected function _getSortDir()
458  {
459  $sDir = oxRegistry::getConfig()->getRequestParameter('dir');
460  if (!in_array($sDir, $this->_aPosDir)) {
461  $sDir = $this->_aPosDir[0];
462  }
463 
464  return $sDir;
465  }
466 
472  protected function _getStartIndex()
473  {
474  return (int) oxRegistry::getConfig()->getRequestParameter('startIndex');
475  }
476 
484  protected function _getTotalCount($sQ)
485  {
486  // TODO: implement caching here
487 
488  // we can cache total count ...
489 
490  // $sCountCacheKey = md5( $sQ );
491 
492  return (int) oxDb::getDb()->getOne($sQ, false, false);
493  }
494 
502  protected function _getDataFields($sQ)
503  {
504  return oxDb::getDb(oxDB::FETCH_MODE_ASSOC)->getArray($sQ, false, false);
505  }
506 
512  protected function _outputResponse($aData)
513  {
514  if (!$this->getConfig()->isUtf()) {
515  // TODO: improve this
516  if (is_array($aData['records']) && ($iRecSize = count($aData['records']))) {
517  $aKeys = array_keys(current($aData['records']));
518  $iKeySize = count($aKeys);
519  $sCharset = oxRegistry::getLang()->translateString("charset");
520  for ($i = 0; $i < $iRecSize; $i++) {
521  for ($c = 0; $c < $iKeySize; $c++) {
522  $aData['records'][$i][$aKeys[$c]] =
523  iconv($sCharset, "UTF-8", $aData['records'][$i][$aKeys[$c]]);
524  }
525  }
526  }
527  }
528 
529  $this->_output(json_encode($aData));
530  }
531 
537  protected function _output($sOut)
538  {
539  echo $sOut;
540  }
541 
549  protected function _getViewName($sTable)
550  {
551  return getViewName($sTable, oxRegistry::getConfig()->getRequestParameter('editlanguage'));
552  }
553 
562  protected function _getData($sCountQ, $sQ)
563  {
564  $sQ = $this->_addFilter($sQ);
565  $sCountQ = $this->_addFilter($sCountQ);
566 
567  $aResponse['startIndex'] = $iStart = $this->_getStartIndex();
568  $aResponse['sort'] = '_' . $this->_getSortCol();
569  $aResponse['dir'] = $this->_getSortDir();
570 
571  $iDebug = $this->getConfig()->getConfigParam('iDebug');
572  if ($iDebug) {
573  $aResponse['countsql'] = $sCountQ;
574  }
575 
576  $aResponse['records'] = array();
577 
578  // skip further execution if no records were found ...
579  if (($iTotal = $this->_getTotalCount($sCountQ))) {
580  $sQ .= $this->_getSorting();
581  $sQ .= $this->_getLimit($iStart);
582 
583  if ($iDebug) {
584  $aResponse['datasql'] = $sQ;
585  }
586 
587  $aResponse['records'] = $this->_getDataFields($sQ);
588  }
589 
590  $aResponse['totalRecords'] = $iTotal;
591 
592  return $aResponse;
593  }
594 
603  public function resetArtSeoUrl($aArtIds, $aCatIds = null)
604  {
605  if (empty($aArtIds)) {
606  return;
607  }
608 
609  if (!is_array($aArtIds)) {
610  $aArtIds = array($aArtIds);
611  }
612 
613  $sShopId = $this->getConfig()->getShopId();
614  foreach ($aArtIds as $sArtId) {
616  oxRegistry::get("oxSeoEncoder")->markAsExpired($sArtId, $sShopId, 1, null, "oxtype='oxarticle'");
617  }
618  }
619 
623  public function resetContentCache()
624  {
625  $blDeleteCacheOnLogout = $this->getConfig()->getConfigParam('blClearCacheOnLogout');
626 
627  if (!$blDeleteCacheOnLogout) {
628 
629  oxRegistry::getUtils()->oxResetFileCache();
630  }
631  }
632 
640  public function resetCounter($sCounterType, $sValue = null)
641  {
642  $blDeleteCacheOnLogout = $this->getConfig()->getConfigParam('blClearCacheOnLogout');
643 
644  if (!$blDeleteCacheOnLogout) {
645  $myUtilsCount = oxRegistry::get("oxUtilsCount");
646  switch ($sCounterType) {
647  case 'priceCatArticle':
648  $myUtilsCount->resetPriceCatArticleCount($sValue);
649  break;
650  case 'catArticle':
651  $myUtilsCount->resetCatArticleCount($sValue);
652  break;
653  case 'vendorArticle':
654  $myUtilsCount->resetVendorArticleCount($sValue);
655  break;
656  case 'manufacturerArticle':
657  $myUtilsCount->resetManufacturerArticleCount($sValue);
658  break;
659  }
660 
661  $this->_resetContentCache();
662  }
663  }
664 
668  protected function _resetContentCache()
669  {
670  }
671 }