OXID eShop CE  4.10.7
 All Classes Namespaces Files Functions Variables Pages
oxadminlist.php
Go to the documentation of this file.
1 <?php
2 
6 class oxAdminList extends oxAdminView
7 {
8 
14  protected $_sListClass = null;
15 
21  protected $_sListType = 'oxlist';
22 
28  protected $_oList = null;
29 
35  protected $_iCurrListPos = 0;
36 
42  protected $_iListSize = 0;
43 
49  protected $_aWhere = null;
50 
56  protected $_blDesc = false;
57 
63  protected $_blEmployMultilanguage = null;
64 
70  protected $_iOverPos = null;
71 
77  protected $_iViewListSize = 0;
78 
84  protected $_iDefViewListSize = 50;
85 
91  protected $_aCurrSorting = null;
92 
98  protected $_sDefSortField = null;
99 
105  protected $_aListFilter = null;
106 
112  public function getListSorting()
113  {
114  if ($this->_aCurrSorting === null) {
115  $this->_aCurrSorting = oxRegistry::getConfig()->getRequestParameter('sort');
116 
117  if (!$this->_aCurrSorting && $this->_sDefSortField && ($oBaseObject = $this->getItemListBaseObject())) {
118  $this->_aCurrSorting[$oBaseObject->getCoreTableName()] = array($this->_sDefSortField => "asc");
119  }
120  }
121 
122  return $this->_aCurrSorting;
123  }
124 
130  public function getListFilter()
131  {
132  if ($this->_aListFilter === null) {
133  $this->_aListFilter = oxRegistry::getConfig()->getRequestParameter("where");
134  }
135 
136  return $this->_aListFilter;
137  }
138 
144  protected function _getViewListSize()
145  {
146  if (!$this->_iViewListSize) {
147  $myConfig = $this->getConfig();
148  if ($aProfile = oxRegistry::getSession()->getVariable('profile')) {
149  if (isset($aProfile[1])) {
150  $myConfig->setConfigParam('iAdminListSize', (int) $aProfile[1]);
151  }
152  }
153 
154  $this->_iViewListSize = (int) $myConfig->getConfigParam('iAdminListSize');
155  if (!$this->_iViewListSize) {
156  $this->_iViewListSize = 10;
157  $myConfig->setConfigParam('iAdminListSize', $this->_iViewListSize);
158  }
159  }
160 
161  return $this->_iViewListSize;
162  }
163 
169  public function getViewListSize()
170  {
171  return $this->_getViewListSize();
172  }
173 
179  protected function _getUserDefListSize()
180  {
181  if (!$this->_iViewListSize) {
182  if (!($iViewListSize = (int) oxRegistry::getConfig()->getRequestParameter('viewListSize'))) {
183  $iViewListSize = $this->_iDefViewListSize;
184  }
185  $this->_iViewListSize = $iViewListSize;
186  }
187 
188  return $this->_iViewListSize;
189  }
190 
196  public function render()
197  {
198  $sReturn = parent::render();
199 
200  // assign our list
201  $this->_aViewData['mylist'] = $this->getItemList();
202 
203  // set navigation parameters
204  $this->_setListNavigationParams();
205 
206  return $sReturn;
207  }
208 
214  public function deleteEntry()
215  {
216  $oDelete = oxNew($this->_sListClass);
217 
218 
219  $blDelete = $oDelete->delete($this->getEditObjectId());
220 
221  // #A - we must reset object ID
222  if ($blDelete && isset($_POST['oxid'])) {
223  $_POST['oxid'] = -1;
224  }
225 
226  $this->resetContentCache();
227 
228  $this->init();
229  }
230 
236  protected function _calcListItemsCount($sSql)
237  {
238  $oStr = getStr();
239 
240  // count SQL
241  $sSql = $oStr->preg_replace('/select .* from/i', 'select count(*) from ', $sSql);
242 
243  // removing order by
244  $sSql = $oStr->preg_replace('/order by .*$/i', '', $sSql);
245 
246  // con of list items which fits current search conditions
247  $this->_iListSize = oxDb::getDb()->getOne($sSql, false, false);
248 
249  // set it into session that other frames know about size of DB
250  oxRegistry::getSession()->setVariable('iArtCnt', $this->_iListSize);
251  }
252 
258  protected function _setCurrentListPosition($sPage = null)
259  {
260  $iAdminListSize = $this->_getViewListSize();
261 
262  $iJumpTo = $sPage ? ((int) $sPage) : ((int) ((int) oxRegistry::getConfig()->getRequestParameter('lstrt')) / $iAdminListSize);
263  $iJumpTo = ($sPage && $iJumpTo) ? ($iJumpTo - 1) : $iJumpTo;
264 
265  $iJumpTo = $iJumpTo * $iAdminListSize;
266  if ($iJumpTo < 1) {
267  $iJumpTo = 0;
268  } elseif ($iJumpTo >= $this->_iListSize) {
269  $iJumpTo = floor($this->_iListSize / $iAdminListSize - 1) * $iAdminListSize;
270  }
271 
272  $this->_iCurrListPos = $this->_iOverPos = (int) $iJumpTo;
273  }
274 
282  protected function _prepareOrderByQuery($sSql = null)
283  {
284  // sorting
285  $aSortFields = $this->getListSorting();
286 
287  if (is_array($aSortFields) && count($aSortFields)) {
288 
289  // only add order by at full sql not for count(*)
290  $sSql .= ' order by ';
291  $blSep = false;
292 
293  $oListItem = $this->getItemListBaseObject();
294  $iLangId = $oListItem->isMultilang() ? $oListItem->getLanguage() : oxRegistry::getLang()->getBaseLanguage();
295 
296  $blSortDesc = oxRegistry::getConfig()->getRequestParameter('adminorder');
297  $blSortDesc = $blSortDesc !== null ? (bool) $blSortDesc : $this->_blDesc;
298 
299  foreach ($aSortFields as $sTable => $aFieldData) {
300 
301  $sTable = $sTable ? (getViewName($sTable, $iLangId) . '.') : '';
302  foreach ($aFieldData as $sColumn => $sSortDir) {
303 
304  $sField = $sTable . $sColumn;
305 
306  //add table name to column name if no table name found attached to column name
307  $sSql .= ((($blSep) ? ', ' : '')) . oxDb::getInstance()->escapeString($sField);
308 
309  //V oxActive field search always DESC
310  if ($blSortDesc || $sColumn == "oxactive" || strcasecmp($sSortDir, 'desc') == 0) {
311  $sSql .= ' desc ';
312  }
313 
314  $blSep = true;
315  }
316  }
317  }
318 
319  return $sSql;
320  }
321 
329  protected function _buildSelectString($oListObject = null)
330  {
331  return $oListObject !== null ? $oListObject->buildSelectString(null) : "";
332  }
333 
334 
344  protected function _processFilter($sFieldValue)
345  {
346  $oStr = getStr();
347 
348  //removing % symbols
349  $sFieldValue = $oStr->preg_replace("/^%|%$/", "", trim($sFieldValue));
350 
351  return $oStr->preg_replace("/\s+/", " ", $sFieldValue);
352  }
353 
362  protected function _buildFilter($sVal, $blIsSearchValue)
363  {
364  if ($blIsSearchValue) {
365  //is search string, using LIKE
366  $sQ = " like " . oxDb::getDb()->quote('%' . $sVal . '%') . " ";
367  } else {
368  //not search string, values must be equal
369  $sQ = " = " . oxDb::getDb()->quote($sVal) . " ";
370  }
371 
372  return $sQ;
373  }
374 
382  protected function _isSearchValue($sFieldValue)
383  {
384  //check if this is search string (contains % sign at beginning and end of string)
385  $blIsSearchValue = false;
386  $oStr = getStr();
387  if ($oStr->preg_match('/^%/', $sFieldValue) && $oStr->preg_match('/%$/', $sFieldValue)) {
388  $blIsSearchValue = true;
389  }
390 
391  //removing % symbols
392  return $blIsSearchValue;
393  }
394 
405  protected function _prepareWhereQuery($aWhere, $sqlFull)
406  {
407  if (count($aWhere)) {
408  $myUtilsString = oxRegistry::get("oxUtilsString");
409  while (list($sFieldName, $sFieldValue) = each($aWhere)) {
410  $sFieldValue = trim($sFieldValue);
411 
412  //check if this is search string (contains % sign at beginning and end of string)
413  $blIsSearchValue = $this->_isSearchValue($sFieldValue);
414 
415  //removing % symbols
416  $sFieldValue = $this->_processFilter($sFieldValue);
417 
418  if (strlen($sFieldValue)) {
419  $aVal = explode(' ', $sFieldValue);
420 
421  //for each search field using AND action
422  $sSqlBoolAction = ' and (';
423 
424  foreach ($aVal as $sVal) {
425  // trying to search spec chars in search value
426  // if found, add cleaned search value to search sql
427  $sUml = $myUtilsString->prepareStrForSearch($sVal);
428  if ($sUml) {
429  $sSqlBoolAction .= '(';
430  }
431 
432  $sFieldName = oxDb::getInstance()->escapeString($sFieldName);
433  $sqlFull .= " {$sSqlBoolAction} {$sFieldName} ";
434 
435  //for search in same field for different values using AND
436  $sSqlBoolAction = ' and ';
437 
438  $sqlFull .= $this->_buildFilter($sVal, $blIsSearchValue);
439 
440  if ($sUml) {
441  $sqlFull .= " or {$sFieldName} ";
442 
443  $sqlFull .= $this->_buildFilter($sUml, $blIsSearchValue);
444  $sqlFull .= ')'; // end of OR section
445  }
446  }
447 
448  // end for AND action
449  $sqlFull .= ' ) ';
450  }
451  }
452  }
453 
454  return $sqlFull;
455  }
456 
464  protected function _changeselect($sSql)
465  {
466  return $sSql;
467  }
468 
469 
475  public function buildWhere()
476  {
477  if ($this->_aWhere === null && ($oList = $this->getItemList())) {
478 
479  $this->_aWhere = array();
480  $aFilter = $this->getListFilter();
481  if (is_array($aFilter)) {
482 
483  $oListItem = $this->getItemListBaseObject();
484  $iLangId = $oListItem->isMultilang() ? $oListItem->getLanguage() : oxRegistry::getLang()->getBaseLanguage();
485  $sLocalDateFormat = $this->getConfig()->getConfigParam('sLocalDateFormat');
486 
487  foreach ($aFilter as $sTable => $aFilterData) {
488  foreach ($aFilterData as $sName => $sValue) {
489  if ($sValue || '0' === ( string ) $sValue) {
490 
491  $sField = "{$sTable}__{$sName}";
492 
493  // if no table name attached to field name, add it
494  $sName = $sTable ? getViewName($sTable, $iLangId) . ".{$sName}" : $sName;
495 
496  // #M1260: if field is date
497  if ($sLocalDateFormat && $sLocalDateFormat != 'ISO' && isset($oListItem->$sField)) {
498  $sFldType = $oListItem->{$sField}->fldtype;
499  if ("datetime" == $sFldType || "date" == $sFldType) {
500  $sValue = $this->_convertToDBDate($sValue, $sFldType);
501  }
502  }
503 
504  $this->_aWhere[$sName] = "%{$sValue}%";
505  }
506  }
507  }
508  }
509  }
510 
511  return $this->_aWhere;
512  }
513 
522  protected function _convertToDBDate($sValue, $sFldType)
523  {
524  $oConvObject = new oxField();
525  $oConvObject->setValue($sValue);
526  if ($sFldType == "datetime") {
527  if (strlen($sValue) == 10 || strlen($sValue) == 22 || (strlen($sValue) == 19 && !stripos($sValue, "m"))) {
528  oxRegistry::get("oxUtilsDate")->convertDBDateTime($oConvObject, true);
529  } else {
530  if (strlen($sValue) > 10) {
531  return $this->_convertTime($sValue);
532  } else {
533  return $this->_convertDate($sValue);
534  }
535  }
536  } elseif ($sFldType == "date") {
537  if (strlen($sValue) == 10) {
538  oxRegistry::get("oxUtilsDate")->convertDBDate($oConvObject, true);
539  } else {
540  return $this->_convertDate($sValue);
541  }
542  }
543 
544  return $oConvObject->value;
545  }
546 
554  protected function _convertDate($sDate)
555  {
556  // regexps to validate input
557  $aDatePatterns = array("/^([0-9]{2})\.([0-9]{4})/" => "EUR2", // MM.YYYY
558  "/^([0-9]{2})\.([0-9]{2})/" => "EUR1", // DD.MM
559  "/^([0-9]{2})\/([0-9]{4})/" => "USA2", // MM.YYYY
560  "/^([0-9]{2})\/([0-9]{2})/" => "USA1" // DD.MM
561  );
562 
563  // date/time formatting rules
564  $aDFormats = array("EUR1" => array(2, 1),
565  "EUR2" => array(2, 1),
566  "USA1" => array(1, 2),
567  "USA2" => array(2, 1)
568  );
569 
570  // looking for date field
571  $aDateMatches = array();
572  $oStr = getStr();
573  foreach ($aDatePatterns as $sPattern => $sType) {
574  if ($oStr->preg_match($sPattern, $sDate, $aDateMatches)) {
575  $sDate = $aDateMatches[$aDFormats[$sType][0]] . "-" . $aDateMatches[$aDFormats[$sType][1]];
576  break;
577  }
578  }
579 
580  return $sDate;
581  }
582 
590  protected function _convertTime($sFullDate)
591  {
592  $sDate = substr($sFullDate, 0, 10);
593  $oConvObject = new oxField();
594  $oConvObject->setValue($sDate);
595  oxRegistry::get("oxUtilsDate")->convertDBDate($oConvObject, true);
596  $oStr = getStr();
597 
598  // looking for time field
599  $sTime = substr($sFullDate, 11);
600  if ($oStr->preg_match("/([0-9]{2}):([0-9]{2}) ([AP]{1}[M]{1})$/", $sTime, $aTimeMatches)) {
601  if ($aTimeMatches[3] == "PM") {
602  $iIntVal = (int) $aTimeMatches[1];
603  if ($iIntVal < 13) {
604  $sTime = ($iIntVal + 12) . ":" . $aTimeMatches[2];
605  }
606  } else {
607  $sTime = $aTimeMatches[1] . ":" . $aTimeMatches[2];
608  }
609  } elseif ($oStr->preg_match("/([0-9]{2}) ([AP]{1}[M]{1})$/", $sTime, $aTimeMatches)) {
610  if ($aTimeMatches[2] == "PM") {
611  $iIntVal = (int) $aTimeMatches[1];
612  if ($iIntVal < 13) {
613  $sTime = ($iIntVal + 12);
614  }
615  } else {
616  $sTime = $aTimeMatches[1];
617  }
618  } else {
619  $sTime = str_replace(".", ":", $sTime);
620  }
621 
622  return $oConvObject->value . " " . $sTime;
623  }
624 
628  protected function _setListNavigationParams()
629  {
630  $myConfig = $this->getConfig();
631 
632  // list navigation
633  $blShowNavigation = false;
634  $iAdminListSize = $this->_getViewListSize();
635  if ($this->_iListSize > $iAdminListSize) {
636  // yes, we need to build the navigation object
637  $pageNavigation = new stdClass();
638  $pageNavigation->pages = round((($this->_iListSize - 1) / $iAdminListSize) + 0.5, 0);
639  $pageNavigation->actpage = ($pageNavigation->actpage > $pageNavigation->pages) ? $pageNavigation->pages : round(($this->_iCurrListPos / $iAdminListSize) + 0.5, 0);
640  $pageNavigation->lastlink = ($pageNavigation->pages - 1) * $iAdminListSize;
641  $pageNavigation->nextlink = null;
642  $pageNavigation->backlink = null;
643 
644  $iPos = $this->_iCurrListPos + $iAdminListSize;
645  if ($iPos < $this->_iListSize) {
646  $pageNavigation->nextlink = $iPos = $this->_iCurrListPos + $iAdminListSize;
647  }
648 
649  if (($this->_iCurrListPos - $iAdminListSize) >= 0) {
650  $pageNavigation->backlink = $iPos = $this->_iCurrListPos - $iAdminListSize;
651  }
652 
653  // calculating list start position
654  $iStart = $pageNavigation->actpage - 5;
655  $iStart = ($iStart <= 0) ? 1 : $iStart;
656 
657  // calculating list end position
658  $iEnd = $pageNavigation->actpage + 5;
659  $iEnd = ($iEnd < $iStart + 10) ? $iStart + 10 : $iEnd;
660  $iEnd = ($iEnd > $pageNavigation->pages) ? $pageNavigation->pages : $iEnd;
661 
662  // once again adjusting start pos ..
663  $iStart = ($iEnd - 10 > 0) ? $iEnd - 10 : $iStart;
664  $iStart = ($pageNavigation->pages <= 11) ? 1 : $iStart;
665 
666  // navigation urls
667  for ($i = $iStart; $i <= $iEnd; $i++) {
668  $page = new stdclass();
669  $page->selected = 0;
670  if ($i == $pageNavigation->actpage) {
671  $page->selected = 1;
672  }
673  $pageNavigation->changePage[$i] = $page;
674  }
675 
676  $this->_aViewData['pagenavi'] = $pageNavigation;
677 
678  if (isset($this->_iOverPos)) {
679  $iPos = $this->_iOverPos;
680  $this->_iOverPos = null;
681  } else {
682  $iPos = oxRegistry::getConfig()->getRequestParameter('lstrt');
683  }
684 
685  if (!$iPos) {
686  $iPos = 0;
687  }
688 
689  $this->_aViewData['lstrt'] = $iPos;
690  $this->_aViewData['listsize'] = $this->_iListSize;
691  $blShowNavigation = true;
692  }
693 
694  // determine not used space in List
695  $iShowListSize = $this->_iListSize - $this->_iCurrListPos;
696  $iAdminListSize = $this->_getViewListSize();
697  $iNotUsed = $iAdminListSize - min($iShowListSize, $iAdminListSize);
698  $iSpace = $iNotUsed * 15;
699 
700  if (!$blShowNavigation) {
701  $iSpace += 20;
702  }
703 
704  $this->_aViewData['iListFillsize'] = $iSpace;
705  }
706 
712  protected function _setupNavigation($sNode)
713  {
714  // navigation according to class
715  if ($sNode) {
716 
717  $myAdminNavigation = $this->getNavigation();
718 
719  $sOxId = $this->getEditObjectId();
720 
721  if ($sOxId == -1) {
722  //on first call or when pressed creating new item button, reseting active tab
723  $iActTab = $this->_iDefEdit;
724  } else {
725  // active tab
726  $iActTab = oxRegistry::getConfig()->getRequestParameter('actedit');
727  $iActTab = $iActTab ? $iActTab : $this->_iDefEdit;
728  }
729 
730  // tabs
731  $this->_aViewData['editnavi'] = $myAdminNavigation->getTabs($sNode, $iActTab);
732 
733  // active tab
734  $this->_aViewData['actlocation'] = $myAdminNavigation->getActiveTab($sNode, $iActTab);
735 
736  // default tab
737  $this->_aViewData['default_edit'] = $myAdminNavigation->getActiveTab($sNode, $this->_iDefEdit);
738 
739  // assign active tab number
740  $this->_aViewData['actedit'] = $iActTab;
741  }
742  }
743 
749  public function getItemList()
750  {
751  if ($this->_oList === null && $this->_sListClass) {
752 
753  $this->_oList = oxNew($this->_sListType);
754  $this->_oList->clear();
755  $this->_oList->init($this->_sListClass);
756 
757  $aWhere = $this->buildWhere();
758 
759  $oListObject = $this->_oList->getBaseObject();
760 
761  oxRegistry::getSession()->setVariable('tabelle', $this->_sListClass);
762  $this->_aViewData['listTable'] = getViewName($oListObject->getCoreTableName());
763  $this->getConfig()->setGlobalParameter('ListCoreTable', $oListObject->getCoreTableName());
764 
765  if ($oListObject->isMultilang()) {
766  // is the object multilingual?
767  $oListObject->setLanguage(oxRegistry::getLang()->getBaseLanguage());
768 
769  if (isset($this->_blEmployMultilanguage)) {
770  $oListObject->setEnableMultilang($this->_blEmployMultilanguage);
771  }
772  }
773 
774  $sSql = $this->_buildSelectString($oListObject);
775  $sSql = $this->_prepareWhereQuery($aWhere, $sSql);
776  $sSql = $this->_prepareOrderByQuery($sSql);
777  $sSql = $this->_changeselect($sSql);
778 
779  // calculates count of list items
780  $this->_calcListItemsCount($sSql);
781 
782  // setting current list position (page)
783  $this->_setCurrentListPosition(oxRegistry::getConfig()->getRequestParameter('jumppage'));
784 
785  // setting addition params for list: current list size
786  $this->_oList->setSqlLimit($this->_iCurrListPos, $this->_getViewListSize());
787 
788  $this->_oList->selectString($sSql);
789  }
790 
791  return $this->_oList;
792  }
793 
797  public function clearItemList()
798  {
799  $this->_oList = null;
800  }
801 
807  public function getItemListBaseObject()
808  {
809  if (($oList = $this->getItemList())) {
810  return $oList->getBaseObject();
811  }
812  }
813 }