00001 <?php
00002
00006 class oxAdminList extends oxAdminView
00007 {
00013 protected $_sListClass = null;
00014
00020 protected $_sListType = 'oxlist';
00021
00027 protected $_oList = null;
00028
00034 protected $_iCurrListPos = 0;
00035
00041 protected $_iListSize = 0;
00042
00048 protected $_aWhere = null;
00049
00055 protected $_sDefSort = null;
00056
00062 protected $_aSort = array();
00063
00069 protected $_blDesc = false;
00070
00076 protected $_blEmployMultilanguage = null;
00077
00083 protected $_iOverPos = null;
00084
00090 protected $_iViewListSize = 0;
00091
00097 protected $_iDefViewListSize = 50;
00098
00104 protected function _getViewListSize()
00105 {
00106 if ( !$this->_iViewListSize ) {
00107 $myConfig = $this->getConfig();
00108 if ( $aProfile = oxSession::getVar( 'profile' ) ) {
00109 if ( isset( $aProfile[1] ) ) {
00110 $myConfig->setConfigParam( 'iAdminListSize', (int) $aProfile[1] );
00111 }
00112 }
00113
00114 $this->_iViewListSize = (int) $myConfig->getConfigParam( 'iAdminListSize' );
00115 if ( !$this->_iViewListSize ) {
00116 $this->_iViewListSize = 10;
00117 $myConfig->setConfigParam( 'iAdminListSize', $this->_iViewListSize );
00118 }
00119 }
00120
00121 return $this->_iViewListSize;
00122 }
00123
00129 protected function _getUserDefListSize()
00130 {
00131 if ( !$this->_iViewListSize ) {
00132 if ( ! ($iViewListSize = (int) oxConfig::getParameter( 'viewListSize' ) ) ) {
00133 $iViewListSize = $this->_iDefViewListSize;
00134 }
00135 $this->_iViewListSize = $iViewListSize;
00136 }
00137
00138 return $this->_iViewListSize;
00139 }
00140
00146 public function init()
00147 {
00148 parent::init();
00149
00150 $myConfig = $this->getConfig();
00151
00152 if ( $this->_sListClass ) {
00153
00154 $this->_oList = oxNew( $this->_sListType, 'core' );
00155 $this->_oList->clear();
00156 $this->_oList->init( $this->_sListClass );
00157
00158 $aWhere = $this->buildWhere();
00159
00160 $oListObject = $this->_oList->getBaseObject();
00161
00162 oxSession::setVar( 'tabelle', $this->_sListClass );
00163 $this->_aViewData['listTable'] = getViewName( $oListObject->getCoreTableName() );
00164 $myConfig->setGlobalParameter( 'ListCoreTable', $oListObject->getCoreTableName() );
00165
00166 if ( $oListObject->isMultilang() ) {
00167
00168 $oListObject->setLanguage( oxLang::getInstance()->getBaseLanguage() );
00169
00170 if ( isset( $this->_blEmployMultilanguage ) ) {
00171 $oListObject->setEnableMultilang( $this->_blEmployMultilanguage );
00172 }
00173 }
00174
00175 $sSql = $this->_buildSelectString( $oListObject );
00176 $sSql = $this->_prepareWhereQuery( $aWhere, $sSql );
00177 $sSql = $this->_prepareOrderByQuery( $sSql );
00178 $sSql = $this->_changeselect( $sSql );
00179
00180
00181 $this->_calcListItemsCount( $sSql );
00182
00183
00184 $this->_setCurrentListPosition( oxConfig::getParameter( 'jumppage' ) );
00185
00186
00187 $this->_oList->setSqlLimit( $this->_iCurrListPos, $this->_getViewListSize() );
00188
00189 $this->_oList->selectString( $sSql );
00190 }
00191 }
00192
00198 public function render()
00199 {
00200 $sReturn = parent::render();
00201
00202
00203 if ( $this->_oList ) {
00204 $this->_aViewData['mylist'] = $this->_oList;
00205 }
00206
00207
00208 $this->_setFilterParams();
00209
00210
00211 $this->_setListNavigationParams();
00212
00213
00214 $this->_aSort[0] = oxConfig::getParameter( 'sort' );
00215 if ( !isset( $this->_aSort[0]) || !$this->_aSort[0] )
00216 $this->_aSort[0] = $this->_sDefSort;
00217
00218 $this->_aViewData['sort'] = $this->_aSort[0];
00219
00220 return $sReturn;
00221 }
00222
00232 protected function _setFilterParams()
00233 {
00234
00235 if ( is_array( $aWhere = oxConfig::getParameter( 'where' ) ) ) {
00236
00237 $myConfig = $this->getConfig();
00238 $myUtils = oxUtils::getInstance();
00239 $sListTable = $myConfig->getGlobalParameter( 'ListCoreTable' );
00240
00241 $oSearchKeys = new oxStdClass();
00242 $sWhereParam = "";
00243
00244 while ( list( $sName, $sValue ) = each( $aWhere ) ) {
00245 $sWhereParam .= "&where[".$sName."]=".$sValue;
00246 $sFieldName = str_replace( getViewName( $sListTable ) . '.', $sListTable . '.', $sName );
00247 $sFieldName = $myUtils->getArrFldName( $sFieldName );
00248 $oSearchKeys->$sFieldName = $sValue;
00249 }
00250
00251 $this->_aViewData['where'] = $oSearchKeys;
00252
00253
00254 if ( $sChosenCat = oxConfig::getParameter( "art_category") ) {
00255 $sWhereParam .= "&art_category=".$sChosenCat;
00256 }
00257 $this->_aViewData['whereparam'] = $sWhereParam;
00258 }
00259 }
00260
00266 public function deleteEntry()
00267 {
00268 $oDelete = oxNew( $this->_sListClass );
00269
00270
00271 $blDelete = $oDelete->delete( oxConfig::getParameter( 'oxid' ) );
00272
00273
00274 if ( $blDelete && isset( $_POST['oxid'] ) ) {
00275 $_POST['oxid'] = -1;
00276 }
00277
00278
00279 $this->init();
00280 }
00281
00289 protected function _calcListItemsCount( $sSql )
00290 {
00291 $oStr = getStr();
00292
00293
00294 $sSql = $oStr->preg_replace( '/select .* from/', 'select count(*) from ', $sSql );
00295
00296
00297 $sSql = $oStr->preg_replace( '/order by .*$/', '', $sSql );
00298
00299
00300 $this->_iListSize = oxDb::getDb()->getOne( $sSql );
00301
00302
00303 oxSession::setVar( 'iArtCnt', $this->_iListSize );
00304 }
00305
00313 protected function _setCurrentListPosition( $sPage = null )
00314 {
00315 $iAdminListSize = $this->_getViewListSize();
00316
00317 $iJumpTo = $sPage?( (int) $sPage):( (int) ( (int) oxConfig::getParameter( 'lstrt' ) ) / $iAdminListSize );
00318 $iJumpTo = ( $sPage && $iJumpTo )?( $iJumpTo - 1 ):$iJumpTo;
00319
00320 $iJumpTo = $iJumpTo * $iAdminListSize;
00321 if ( $iJumpTo < 1 ) {
00322 $iJumpTo = 0;
00323 } elseif ( $iJumpTo >= $this->_iListSize ) {
00324 $iJumpTo = floor( $this->_iListSize / $iAdminListSize - 1 ) * $iAdminListSize;
00325 }
00326
00327 $this->_iCurrListPos = $this->_iOverPos = (int) $iJumpTo;
00328 }
00329
00337 protected function _prepareOrderByQuery( $sSql = null )
00338 {
00339
00340 $this->_aSort[0] = oxConfig::getParameter( 'sort' );
00341
00342 if ( !isset( $this->_aSort[0]) || !$this->_aSort[0] )
00343 $this->_aSort[0] = $this->_sDefSort;
00344
00345 $blSortDesc = $this->_blDesc;
00346
00347
00348 if ( $this->_aSort && isset($this->_aSort[0] ) ) {
00349
00350 $blSortDesc = oxConfig::getParameter( 'adminorder' );
00351
00352 if ( !isset( $blSortDesc ) ) {
00353 $blSortDesc = $this->_blDesc;
00354 }
00355
00356
00357 $sSql .= ' order by ';
00358 $blSep = false;
00359 $iLang = null;
00360
00361 $oListObject = $this->_oList->getBaseObject();
00362
00363 if ( $oListObject->isMultilang() ) {
00364 $iLang = $oListObject->getLanguage();
00365 }
00366
00367 $sTable = $oListObject->getCoreTableName();
00368 $sViewName = getViewName( $sTable );
00369
00370 foreach ( $this->_aSort as $orderColumn ) {
00371
00372
00373 if ( $orderColumn == "oxactive" ) {
00374 $blSortDesc = true;
00375 }
00376
00377
00378 if ( $iLang ) {
00379 $sObjectField = "{$sTable}__{$orderColumn}";
00380 if ( $oListObject instanceof oxI18n ) {
00381 if ( $oListObject->isMultilingualField($orderColumn) ) {
00382 $orderColumn .= "_{$iLang}";
00383 }
00384 }
00385 }
00386
00387
00388 if ( strpos( $orderColumn, '.' ) === false ) {
00389 $orderColumn = $sViewName . '.' . $orderColumn;
00390 }
00391
00392 $sSql .= ( ( ( $blSep ) ? ', ' : '' ) ) . oxDb::getInstance()->escapeString( $orderColumn );
00393 $blSep = true;
00394 }
00395
00396 if ( $blSortDesc ) {
00397 $sSql .= ' desc ';
00398 }
00399 }
00400
00401 return $sSql;
00402 }
00403
00411 protected function _buildSelectString( $oListObject = null )
00412 {
00413 $sSql = '';
00414
00415 if ( $oListObject ) {
00416 $oBase = oxNew( 'oxBase' );
00417 $oBase->init( $oListObject->getCoreTableName() );
00418 $sSql = $oBase->buildSelectString( null );
00419 }
00420
00421 return $sSql;
00422 }
00423
00424
00434 protected function _processFilter( $sFieldValue )
00435 {
00436 $oStr = getStr();
00437
00438
00439 $sFieldValue = $oStr->preg_replace( "/^%|%$/", "", trim( $sFieldValue ) );
00440 return $oStr->preg_replace( "/\s+/", " ", $sFieldValue );
00441 }
00442
00451 protected function _buildFilter( $sVal, $blIsSearchValue )
00452 {
00453 if ( $blIsSearchValue ) {
00454
00455 $sQ = " like '%{$sVal}%' ";
00456 } else {
00457
00458 $sQ = " = ".oxDb::getDb()->quote( $sVal )." ";
00459 }
00460
00461 return $sQ;
00462 }
00463
00471 protected function _isSearchValue( $sFieldValue )
00472 {
00473
00474 $blIsSearchValue = false;
00475 $oStr = getStr();
00476 if ( $oStr->preg_match( '/^%/', $sFieldValue ) && $oStr->preg_match( '/%$/', $sFieldValue ) ) {
00477 $blIsSearchValue = true;
00478 }
00479
00480
00481 return $blIsSearchValue;
00482 }
00483
00494 protected function _prepareWhereQuery( $aWhere, $sqlFull )
00495 {
00496 if ( count($aWhere) ) {
00497 $myUtilsString = oxUtilsString::getInstance();
00498 while ( list($sFieldName, $sFieldValue) = each( $aWhere ) ) {
00499 $sFieldValue = trim( $sFieldValue );
00500
00501
00502 $blIsSearchValue = $this->_isSearchValue( $sFieldValue );
00503
00504
00505 $sFieldValue = $this->_processFilter( $sFieldValue );
00506
00507 if ( strlen($sFieldValue) ) {
00508 $aVal = explode( ' ', $sFieldValue );
00509
00510
00511 $sSqlBoolAction = ' and (';
00512
00513 foreach ( $aVal as $sVal) {
00514
00515 $sFieldName = oxDb::getInstance()->escapeString( $sFieldName );
00516 $sqlFull .= " {$sSqlBoolAction} {$sFieldName} ";
00517
00518
00519 $sSqlBoolAction = ' and ';
00520
00521 $sqlFull .= $this->_buildFilter( $sVal, $blIsSearchValue );
00522
00523
00524
00525 $sUml = $myUtilsString->prepareStrForSearch( $sVal );
00526 if ( $sUml ) {
00527 $sqlFull .= " or {$sFieldName} ";
00528
00529 $sqlFull .= $this->_buildFilter( $sUml, $blIsSearchValue );
00530 }
00531 }
00532
00533
00534 $sqlFull .= ' ) ';
00535 }
00536 }
00537 }
00538
00539 return $sqlFull;
00540 }
00541
00549 protected function _changeselect( $sSql )
00550 {
00551 return $sSql;
00552 }
00553
00554
00560 public function buildWhere()
00561 {
00562 $myConfig = $this->getConfig();
00563 $this->_aWhere = array();
00564
00565 $iLanguage = oxLang::getInstance()->getBaseLanguage();
00566 $aWhere = oxConfig::getParameter( 'where' );
00567
00568 if ( !$this->_oList )
00569 return $this->_aWhere;
00570
00571 $oListObject = $this->_oList->getBaseObject();
00572 $sTable = $oListObject->getCoreTableName();
00573 $sViewName = getViewName( $sTable );
00574
00575 if ( $this->_oList && is_array( $aWhere ) ) {
00576
00577 $oStr = getStr();
00578 foreach ( $aWhere as $sName => $sValue ) {
00579 if ( $sValue || '0' === ( string ) $sValue ) {
00580
00581 if ( strpos( $sName, '.' ) === false ) {
00582
00583 $sName = "{$sTable}.{$sName}";
00584 }
00585
00586
00587
00588 if ( $oListObject instanceof oxI18n ) {
00589 $sFldName = strtolower( $oStr->preg_replace('/(.+)\./', '', $sName ) );
00590 if ( $oListObject->isMultilingualField( $sFldName ) && $iLanguage ) {
00591 $sName .= "_$iLanguage";
00592 }
00593 }
00594
00595
00596 $sLocalDateFormat = $this->getConfig()->getConfigParam( 'sLocalDateFormat' );
00597 if ( $sLocalDateFormat && $sLocalDateFormat != 'ISO') {
00598 $sFldName = strtolower( $oStr->preg_replace('/(.+)\./', '', $sName ) );
00599 $sLongName = $sTable."__".$sFldName;
00600 $sFldType = $oListObject->$sLongName->fldtype;
00601 if ( $sFldType && ( $sFldType == "datetime" || $sFldType == "date" ) ) {
00602 $sValue = $this->_convertToDBDate( $sValue, $sFldType );
00603 }
00604 }
00605
00606 $this->_aWhere[$sName] = "%{$sValue}%";
00607 }
00608 }
00609 }
00610
00611 return $this->_aWhere;
00612 }
00613
00622 protected function _convertToDBDate( $sValue, $sFldType )
00623 {
00624 $oConvObject = new oxField();
00625 $oConvObject->setValue($sValue);
00626 if ( $sFldType == "datetime" ) {
00627 if ( strlen($sValue) == 10 || strlen($sValue) == 22 || ( strlen($sValue) == 19 && !stripos( $sValue, "m" ) ) ) {
00628 oxDb::getInstance()->convertDBDateTime( $oConvObject, true );
00629 } else {
00630 if ( strlen($sValue) > 10 ) {
00631 return $this->_convertTime( $sValue );
00632 } else {
00633 return $this->_convertDate( $sValue );
00634 }
00635 }
00636 } elseif ( $sFldType == "date" ) {
00637 if ( strlen($sValue) == 10 ) {
00638 oxDb::getInstance()->convertDBDate( $oConvObject, true);
00639 } else {
00640 return $this->_convertDate( $sValue );
00641 }
00642 }
00643 return $oConvObject->value;
00644 }
00645
00653 protected function _convertDate( $sDate )
00654 {
00655
00656 $aDatePatterns = array("/^([0-9]{2})\.([0-9]{4})/" => "EUR2",
00657 "/^([0-9]{2})\.([0-9]{2})/" => "EUR1",
00658 "/^([0-9]{2})\/([0-9]{4})/" => "USA2",
00659 "/^([0-9]{2})\/([0-9]{2})/" => "USA1"
00660 );
00661
00662
00663 $aDFormats = array("EUR1" => array(2, 1),
00664 "EUR2" => array(2, 1),
00665 "USA1" => array(1, 2),
00666 "USA2" => array(2, 1)
00667 );
00668
00669
00670 $aDateMatches = array();
00671 $oStr = getStr();
00672 foreach ( $aDatePatterns as $sPattern => $sType) {
00673 if ( $oStr->preg_match( $sPattern, $sDate, $aDateMatches)) {
00674 $sDate = $aDateMatches[$aDFormats[$sType][0]] . "-" . $aDateMatches[$aDFormats[$sType][1]];
00675 break;
00676 }
00677 }
00678
00679 return $sDate;
00680 }
00681
00689 protected function _convertTime( $sFullDate )
00690 {
00691 $sDate = substr( $sFullDate, 0, 10 );
00692 $oConvObject = new oxField();
00693 $oConvObject->setValue($sDate);
00694 oxDb::getInstance()->convertDBDate( $oConvObject, true);
00695 $oStr = getStr();
00696
00697
00698 $sTime = substr( $sFullDate, 11);
00699 if ( $oStr->preg_match( "/([0-9]{2}):([0-9]{2}) ([AP]{1}[M]{1})$/", $sTime, $aTimeMatches ) ) {
00700 if ( $aTimeMatches[3] == "PM") {
00701 $iIntVal = (int) $aTimeMatches[1];
00702 if ( $iIntVal < 13) {
00703 $sTime = ($iIntVal + 12) . ":" . $aTimeMatches[2];
00704 }
00705 } else {
00706 $sTime = $aTimeMatches[1] . ":" . $aTimeMatches[2];
00707 }
00708 } elseif ( $oStr->preg_match( "/([0-9]{2}) ([AP]{1}[M]{1})$/", $sTime, $aTimeMatches ) ) {
00709 if ( $aTimeMatches[2] == "PM") {
00710 $iIntVal = (int) $aTimeMatches[1];
00711 if ( $iIntVal < 13) {
00712 $sTime = ($iIntVal + 12);
00713 }
00714 } else {
00715 $sTime = $aTimeMatches[1];
00716 }
00717 } else {
00718 $sTime = str_replace(".", ":", $sTime);
00719 }
00720
00721 return $oConvObject->value . " " . $sTime;
00722 }
00723
00729 protected function _setListNavigationParams()
00730 {
00731 $myConfig = $this->getConfig();
00732
00733
00734 $blShowNavigation = false;
00735 $iAdminListSize = $this->_getViewListSize();
00736 if ( $this->_iListSize > $iAdminListSize ) {
00737
00738 $pagenavigation = new oxStdClass();
00739 $pagenavigation->pages = round( ( ( $this->_iListSize - 1 ) / $iAdminListSize ) + 0.5, 0 );
00740 $pagenavigation->actpage = ($pagenavigation->actpage > $pagenavigation->pages)? $pagenavigation->pages : round( ( $this->_iCurrListPos / $iAdminListSize ) + 0.5, 0 );
00741 $pagenavigation->lastlink = ( $pagenavigation->pages - 1 ) * $iAdminListSize;
00742 $pagenavigation->nextlink = null;
00743 $pagenavigation->backlink = null;
00744
00745 $iPos = $this->_iCurrListPos + $iAdminListSize;
00746 if ( $iPos < $this->_iListSize ) {
00747 $pagenavigation->nextlink = $iPos = $this->_iCurrListPos + $iAdminListSize;
00748 }
00749
00750 if ( ( $this->_iCurrListPos - $iAdminListSize ) >= 0 ) {
00751 $pagenavigation->backlink = $iPos = $this->_iCurrListPos - $iAdminListSize;
00752 }
00753
00754
00755 $iStart = $pagenavigation->actpage - 5;
00756 $iStart = ( $iStart <= 0 ) ? 1 : $iStart;
00757
00758
00759 $iEnd = $pagenavigation->actpage + 5;
00760 $iEnd = ( $iEnd < $iStart + 10) ? $iStart + 10 : $iEnd;
00761 $iEnd = ( $iEnd > $pagenavigation->pages ) ? $pagenavigation->pages : $iEnd;
00762
00763
00764 $iStart = ( $iEnd - 10 > 0 ) ? $iEnd - 10 : $iStart;
00765 $iStart = ( $pagenavigation->pages <= 11) ? 1 : $iStart;
00766
00767
00768 for ( $i = $iStart; $i <= $iEnd; $i++ ) {
00769 $page = new Oxstdclass();
00770 $page->selected = 0;
00771 if ( $i == $pagenavigation->actpage ) {
00772 $page->selected = 1;
00773 }
00774 $pagenavigation->changePage[$i] = $page;
00775 }
00776
00777 $this->_aViewData['pagenavi'] = $pagenavigation;
00778
00779 if ( isset( $this->_iOverPos)) {
00780 $iPos = $this->_iOverPos;
00781 $this->_iOverPos = null;
00782 } else {
00783 $iPos = oxConfig::getParameter( 'lstrt' );
00784 }
00785
00786 if ( !$iPos ) {
00787 $iPos = 0;
00788 }
00789
00790 $this->_aViewData['lstrt'] = $iPos;
00791 $this->_aViewData['listsize'] = $this->_iListSize;
00792 $blShowNavigation = true;
00793 }
00794
00795
00796 $iShowListSize = $this->_iListSize - $this->_iCurrListPos;
00797 $iAdminListSize = $this->_getViewListSize();
00798 $iNotUsed = $iAdminListSize - min( $iShowListSize, $iAdminListSize );
00799 $iSpace = $iNotUsed * 15;
00800
00801 if ( !$blShowNavigation ) {
00802 $iSpace += 20;
00803 }
00804
00805 $this->_aViewData['iListFillsize'] = $iSpace;
00806 }
00807
00815 protected function _setupNavigation( $sNode )
00816 {
00817
00818 if ( $sNode ) {
00819
00820 $myAdminNavig = $this->getNavigation();
00821
00822 $sOxId = oxConfig::getParameter( 'oxid' );
00823
00824 if ( $sOxId == -1) {
00825
00826 $iActTab = $this->_iDefEdit;
00827 } else {
00828
00829 $iActTab = oxConfig::getParameter( 'actedit' );
00830 $iActTab = $iActTab ? $iActTab : $this->_iDefEdit;
00831 }
00832
00833
00834 $this->_aViewData['editnavi'] = $myAdminNavig->getTabs( $sNode, $iActTab );
00835
00836
00837 $this->_aViewData['actlocation'] = $myAdminNavig->getActiveTab( $sNode, $iActTab );
00838
00839
00840 $this->_aViewData['default_edit'] = $myAdminNavig->getActiveTab( $sNode, $this->_iDefEdit );
00841
00842
00843 $this->_aViewData['actedit'] = $iActTab;
00844 }
00845 }
00846
00852 public function getItemList()
00853 {
00854 return $this->_oList;
00855 }
00856 }