OXID eShop CE  4.8.11
 All Classes Files Functions Variables Pages
oxadminlist.php
Go to the documentation of this file.
1 <?php
2 
6 class oxAdminList extends oxAdminView
7 {
13  protected $_sListClass = null;
14 
20  protected $_sListType = 'oxlist';
21 
27  protected $_oList = null;
28 
34  protected $_iCurrListPos = 0;
35 
41  protected $_iListSize = 0;
42 
48  protected $_aWhere = null;
49 
55  protected $_blDesc = false;
56 
62  protected $_blEmployMultilanguage = null;
63 
69  protected $_iOverPos = null;
70 
76  protected $_iViewListSize = 0;
77 
83  protected $_iDefViewListSize = 50;
84 
90  protected $_aCurrSorting = null;
91 
97  protected $_sDefSortField = null;
98 
104  protected $_aListFilter = null;
105 
111  public function getListSorting()
112  {
113  if ( $this->_aCurrSorting === null ) {
114  $this->_aCurrSorting = oxConfig::getParameter( 'sort' );
115 
116  if ( !$this->_aCurrSorting && $this->_sDefSortField && ( $oBaseObject = $this->getItemListBaseObject() ) ) {
117  $this->_aCurrSorting[$oBaseObject->getCoreTableName()] = array( $this->_sDefSortField => "asc" );
118  }
119  }
120 
121  return $this->_aCurrSorting;
122  }
123 
129  public function getListFilter()
130  {
131  if ( $this->_aListFilter === null ) {
132  $this->_aListFilter = oxConfig::getParameter( "where" );
133  }
134 
135  return $this->_aListFilter;
136  }
137 
143  protected function _getViewListSize()
144  {
145  if ( !$this->_iViewListSize ) {
146  $myConfig = $this->getConfig();
147  if ( $aProfile = oxSession::getVar( 'profile' ) ) {
148  if ( isset( $aProfile[1] ) ) {
149  $myConfig->setConfigParam( 'iAdminListSize', (int) $aProfile[1] );
150  }
151  }
152 
153  $this->_iViewListSize = (int) $myConfig->getConfigParam( 'iAdminListSize' );
154  if ( !$this->_iViewListSize ) {
155  $this->_iViewListSize = 10;
156  $myConfig->setConfigParam( 'iAdminListSize', $this->_iViewListSize );
157  }
158  }
159 
160  return $this->_iViewListSize;
161  }
162 
168  public function getViewListSize()
169  {
170  return $this->_getViewListSize();
171  }
172 
178  protected function _getUserDefListSize()
179  {
180  if ( !$this->_iViewListSize ) {
181  if ( ! ($iViewListSize = (int) oxConfig::getParameter( 'viewListSize' ) ) ) {
182  $iViewListSize = $this->_iDefViewListSize;
183  }
184  $this->_iViewListSize = $iViewListSize;
185  }
186 
187  return $this->_iViewListSize;
188  }
189 
195  public function render()
196  {
197  $sReturn = parent::render();
198 
199  // assign our list
200  $this->_aViewData['mylist'] = $this->getItemList();
201 
202  // set navigation parameters
203  $this->_setListNavigationParams();
204 
205  return $sReturn;
206  }
207 
213  public function deleteEntry()
214  {
215  $oDelete = oxNew( $this->_sListClass );
216 
217 
218  $blDelete = $oDelete->delete( $this->getEditObjectId() );
219 
220  // #A - we must reset object ID
221  if ( $blDelete && isset( $_POST['oxid'] ) ) {
222  $_POST['oxid'] = -1;
223  }
224 
225 
226  $this->init();
227  }
228 
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 
260  protected function _setCurrentListPosition( $sPage = null )
261  {
262  $iAdminListSize = $this->_getViewListSize();
263 
264  $iJumpTo = $sPage?( (int) $sPage):( (int) ( (int) oxConfig::getParameter( 'lstrt' ) ) / $iAdminListSize );
265  $iJumpTo = ( $sPage && $iJumpTo )?( $iJumpTo - 1 ):$iJumpTo;
266 
267  $iJumpTo = $iJumpTo * $iAdminListSize;
268  if ( $iJumpTo < 1 ) {
269  $iJumpTo = 0;
270  } elseif ( $iJumpTo >= $this->_iListSize ) {
271  $iJumpTo = floor( $this->_iListSize / $iAdminListSize - 1 ) * $iAdminListSize;
272  }
273 
274  $this->_iCurrListPos = $this->_iOverPos = (int) $iJumpTo;
275  }
276 
284  protected function _prepareOrderByQuery( $sSql = null )
285  {
286  // sorting
287  $aSortFields = $this->getListSorting();
288 
289  if ( is_array( $aSortFields ) && count( $aSortFields ) ) {
290 
291  // only add order by at full sql not for count(*)
292  $sSql .= ' order by ';
293  $blSep = false;
294 
295  $oListItem = $this->getItemListBaseObject();
296  $iLangId = $oListItem->isMultilang() ? $oListItem->getLanguage() : oxRegistry::getLang()->getBaseLanguage();
297 
298  $blSortDesc = oxConfig::getParameter( 'adminorder' );
299  $blSortDesc = $blSortDesc !== null ? (bool) $blSortDesc : $this->_blDesc;
300 
301  foreach ( $aSortFields as $sTable => $aFieldData ) {
302 
303  $sTable = $sTable ? ( getViewName( $sTable, $iLangId ) . '.' ) : '';
304  foreach ( $aFieldData as $sColumn => $sSortDir ) {
305 
306  $sField = $sTable . $sColumn;
307 
308  //add table name to column name if no table name found attached to column name
309  $sSql .= ( ( ( $blSep ) ? ', ' : '' ) ) . oxDb::getInstance()->escapeString( $sField );
310 
311  //V oxActive field search always DESC
312  if ( $blSortDesc || $sColumn == "oxactive" || strcasecmp( $sSortDir, 'desc' ) == 0 ) {
313  $sSql .= ' desc ';
314  }
315 
316  $blSep = true;
317  }
318  }
319  }
320 
321  return $sSql;
322  }
323 
331  protected function _buildSelectString( $oListObject = null )
332  {
333  return $oListObject !== null ? $oListObject->buildSelectString( null ) : "";
334  }
335 
336 
346  protected function _processFilter( $sFieldValue )
347  {
348  $oStr = getStr();
349 
350  //removing % symbols
351  $sFieldValue = $oStr->preg_replace( "/^%|%$/", "", trim( $sFieldValue ) );
352  return $oStr->preg_replace( "/\s+/", " ", $sFieldValue );
353  }
354 
363  protected function _buildFilter( $sVal, $blIsSearchValue )
364  {
365  if ( $blIsSearchValue ) {
366  //is search string, using LIKE
367  $sQ = " like ".oxDb::getDb()->quote( '%'.$sVal.'%' )." ";
368  } else {
369  //not search string, values must be equal
370  $sQ = " = ".oxDb::getDb()->quote( $sVal )." ";
371  }
372 
373  return $sQ;
374  }
375 
383  protected function _isSearchValue( $sFieldValue )
384  {
385  //check if this is search string (contains % sign at beginning and end of string)
386  $blIsSearchValue = false;
387  $oStr = getStr();
388  if ( $oStr->preg_match( '/^%/', $sFieldValue ) && $oStr->preg_match( '/%$/', $sFieldValue ) ) {
389  $blIsSearchValue = true;
390  }
391 
392  //removing % symbols
393  return $blIsSearchValue;
394  }
395 
406  protected function _prepareWhereQuery( $aWhere, $sqlFull )
407  {
408  if ( count($aWhere) ) {
409  $myUtilsString = oxRegistry::get("oxUtilsString");
410  while ( list($sFieldName, $sFieldValue) = each( $aWhere ) ) {
411  $sFieldValue = trim( $sFieldValue );
412 
413  //check if this is search string (contains % sign at beginning and end of string)
414  $blIsSearchValue = $this->_isSearchValue( $sFieldValue );
415 
416  //removing % symbols
417  $sFieldValue = $this->_processFilter( $sFieldValue );
418 
419  if ( strlen($sFieldValue) ) {
420  $aVal = explode( ' ', $sFieldValue );
421 
422  //for each search field using AND action
423  $sSqlBoolAction = ' and (';
424 
425  foreach ( $aVal as $sVal) {
426  // trying to search spec chars in search value
427  // if found, add cleaned search value to search sql
428  $sUml = $myUtilsString->prepareStrForSearch( $sVal );
429  if ($sUml) {
430  $sSqlBoolAction .= '(';
431  }
432 
433  $sFieldName = oxDb::getInstance()->escapeString( $sFieldName );
434  $sqlFull .= " {$sSqlBoolAction} {$sFieldName} ";
435 
436  //for search in same field for different values using AND
437  $sSqlBoolAction = ' and ';
438 
439  $sqlFull .= $this->_buildFilter( $sVal, $blIsSearchValue );
440 
441  if ( $sUml ) {
442  $sqlFull .= " or {$sFieldName} ";
443 
444  $sqlFull .= $this->_buildFilter( $sUml, $blIsSearchValue );
445  $sqlFull .= ')'; // end of OR section
446  }
447  }
448 
449  // end for AND action
450  $sqlFull .= ' ) ';
451  }
452  }
453  }
454 
455  return $sqlFull;
456  }
457 
465  protected function _changeselect( $sSql )
466  {
467  return $sSql;
468  }
469 
470 
476  public function buildWhere()
477  {
478  if ( $this->_aWhere === null && ( $oList = $this->getItemList() ) ) {
479 
480  $this->_aWhere = array();
481  $aFilter = $this->getListFilter();
482  if ( is_array( $aFilter ) ) {
483 
484  $oListItem = $this->getItemListBaseObject();
485  $iLangId = $oListItem->isMultilang() ? $oListItem->getLanguage() : oxRegistry::getLang()->getBaseLanguage();
486  $sLocalDateFormat = $this->getConfig()->getConfigParam( 'sLocalDateFormat' );
487 
488  foreach ( $aFilter as $sTable => $aFilterData ) {
489  foreach ( $aFilterData as $sName => $sValue ) {
490  if ( $sValue || '0' === ( string ) $sValue ) {
491 
492  $sField = "{$sTable}__{$sName}";
493 
494  // if no table name attached to field name, add it
495  $sName = $sTable ? getViewName( $sTable, $iLangId ) . ".{$sName}" : $sName;
496 
497  // #M1260: if field is date
498  if ( $sLocalDateFormat && $sLocalDateFormat != 'ISO' && isset( $oListItem->$sField ) ) {
499  $sFldType = $oListItem->{$sField}->fldtype;
500  if ( "datetime" == $sFldType || "date" == $sFldType ) {
501  $sValue = $this->_convertToDBDate( $sValue, $sFldType );
502  }
503  }
504 
505  $this->_aWhere[$sName] = "%{$sValue}%";
506  }
507  }
508  }
509  }
510  }
511 
512  return $this->_aWhere;
513  }
514 
523  protected function _convertToDBDate( $sValue, $sFldType )
524  {
525  $oConvObject = new oxField();
526  $oConvObject->setValue($sValue);
527  if ( $sFldType == "datetime" ) {
528  if ( strlen($sValue) == 10 || strlen($sValue) == 22 || ( strlen($sValue) == 19 && !stripos( $sValue, "m" ) ) ) {
529  oxRegistry::get("oxUtilsDate")->convertDBDateTime( $oConvObject, true );
530  } else {
531  if ( strlen($sValue) > 10 ) {
532  return $this->_convertTime( $sValue );
533  } else {
534  return $this->_convertDate( $sValue );
535  }
536  }
537  } elseif ( $sFldType == "date" ) {
538  if ( strlen($sValue) == 10 ) {
539  oxRegistry::get("oxUtilsDate")->convertDBDate( $oConvObject, true);
540  } else {
541  return $this->_convertDate( $sValue );
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 
630  protected function _setListNavigationParams()
631  {
632  $myConfig = $this->getConfig();
633 
634  // list navigation
635  $blShowNavigation = false;
636  $iAdminListSize = $this->_getViewListSize();
637  if ( $this->_iListSize > $iAdminListSize ) {
638  // yes, we need to build the navigation object
639  $pageNavigation = new stdClass();
640  $pageNavigation->pages = round( ( ( $this->_iListSize - 1 ) / $iAdminListSize ) + 0.5, 0 );
641  $pageNavigation->actpage = ($pageNavigation->actpage > $pageNavigation->pages)? $pageNavigation->pages : round( ( $this->_iCurrListPos / $iAdminListSize ) + 0.5, 0 );
642  $pageNavigation->lastlink = ( $pageNavigation->pages - 1 ) * $iAdminListSize;
643  $pageNavigation->nextlink = null;
644  $pageNavigation->backlink = null;
645 
646  $iPos = $this->_iCurrListPos + $iAdminListSize;
647  if ( $iPos < $this->_iListSize ) {
648  $pageNavigation->nextlink = $iPos = $this->_iCurrListPos + $iAdminListSize;
649  }
650 
651  if ( ( $this->_iCurrListPos - $iAdminListSize ) >= 0 ) {
652  $pageNavigation->backlink = $iPos = $this->_iCurrListPos - $iAdminListSize;
653  }
654 
655  // calculating list start position
656  $iStart = $pageNavigation->actpage - 5;
657  $iStart = ( $iStart <= 0 ) ? 1 : $iStart;
658 
659  // calculating list end position
660  $iEnd = $pageNavigation->actpage + 5;
661  $iEnd = ( $iEnd < $iStart + 10) ? $iStart + 10 : $iEnd;
662  $iEnd = ( $iEnd > $pageNavigation->pages ) ? $pageNavigation->pages : $iEnd;
663 
664  // once again adjusting start pos ..
665  $iStart = ( $iEnd - 10 > 0 ) ? $iEnd - 10 : $iStart;
666  $iStart = ( $pageNavigation->pages <= 11) ? 1 : $iStart;
667 
668  // navigation urls
669  for ( $i = $iStart; $i <= $iEnd; $i++ ) {
670  $page = new stdclass();
671  $page->selected = 0;
672  if ( $i == $pageNavigation->actpage ) {
673  $page->selected = 1;
674  }
675  $pageNavigation->changePage[$i] = $page;
676  }
677 
678  $this->_aViewData['pagenavi'] = $pageNavigation;
679 
680  if ( isset( $this->_iOverPos)) {
681  $iPos = $this->_iOverPos;
682  $this->_iOverPos = null;
683  } else {
684  $iPos = oxConfig::getParameter( 'lstrt' );
685  }
686 
687  if ( !$iPos ) {
688  $iPos = 0;
689  }
690 
691  $this->_aViewData['lstrt'] = $iPos;
692  $this->_aViewData['listsize'] = $this->_iListSize;
693  $blShowNavigation = true;
694  }
695 
696  // determine not used space in List
697  $iShowListSize = $this->_iListSize - $this->_iCurrListPos;
698  $iAdminListSize = $this->_getViewListSize();
699  $iNotUsed = $iAdminListSize - min( $iShowListSize, $iAdminListSize );
700  $iSpace = $iNotUsed * 15;
701 
702  if ( !$blShowNavigation ) {
703  $iSpace += 20;
704  }
705 
706  $this->_aViewData['iListFillsize'] = $iSpace;
707  }
708 
716  protected function _setupNavigation( $sNode )
717  {
718  // navigation according to class
719  if ( $sNode ) {
720 
721  $myAdminNavigation = $this->getNavigation();
722 
723  $sOxId = $this->getEditObjectId();
724 
725  if ( $sOxId == -1) {
726  //on first call or when pressed creating new item button, reseting active tab
727  $iActTab = $this->_iDefEdit;
728  } else {
729  // active tab
730  $iActTab = oxConfig::getParameter( 'actedit' );
731  $iActTab = $iActTab ? $iActTab : $this->_iDefEdit;
732  }
733 
734  // tabs
735  $this->_aViewData['editnavi'] = $myAdminNavigation->getTabs( $sNode, $iActTab );
736 
737  // active tab
738  $this->_aViewData['actlocation'] = $myAdminNavigation->getActiveTab( $sNode, $iActTab );
739 
740  // default tab
741  $this->_aViewData['default_edit'] = $myAdminNavigation->getActiveTab( $sNode, $this->_iDefEdit );
742 
743  // assign active tab number
744  $this->_aViewData['actedit'] = $iActTab;
745  }
746  }
747 
753  public function getItemList()
754  {
755  if ( $this->_oList === null && $this->_sListClass ) {
756 
757  $this->_oList = oxNew( $this->_sListType );
758  $this->_oList->clear();
759  $this->_oList->init( $this->_sListClass );
760 
761  $aWhere = $this->buildWhere();
762 
763  $oListObject = $this->_oList->getBaseObject();
764 
765  oxSession::setVar( 'tabelle', $this->_sListClass );
766  $this->_aViewData['listTable'] = getViewName( $oListObject->getCoreTableName() );
767  $this->getConfig()->setGlobalParameter( 'ListCoreTable', $oListObject->getCoreTableName() );
768 
769  if ( $oListObject->isMultilang() ) {
770  // is the object multilingual?
771  $oListObject->setLanguage( oxRegistry::getLang()->getBaseLanguage() );
772 
773  if ( isset( $this->_blEmployMultilanguage ) ) {
774  $oListObject->setEnableMultilang( $this->_blEmployMultilanguage );
775  }
776  }
777 
778  $sSql = $this->_buildSelectString( $oListObject );
779  $sSql = $this->_prepareWhereQuery( $aWhere, $sSql );
780  $sSql = $this->_prepareOrderByQuery( $sSql );
781  $sSql = $this->_changeselect( $sSql );
782 
783  // calculates count of list items
784  $this->_calcListItemsCount( $sSql );
785 
786  // setting current list position (page)
787  $this->_setCurrentListPosition( oxConfig::getParameter( 'jumppage' ) );
788 
789  // setting addition params for list: current list size
790  $this->_oList->setSqlLimit( $this->_iCurrListPos, $this->_getViewListSize() );
791 
792  $this->_oList->selectString( $sSql );
793  }
794 
795  return $this->_oList;
796  }
797 
803  public function clearItemList()
804  {
805  $this->_oList = null;
806  }
807 
813  public function getItemListBaseObject()
814  {
815  if ( ( $oList = $this->getItemList() ) ) {
816  return $oList->getBaseObject();
817  }
818  }
819 }