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
00516 $sUml = $myUtilsString->prepareStrForSearch( $sVal );
00517 if ($sUml) {
00518 $sSqlBoolAction .= '(';
00519 }
00520
00521 $sFieldName = oxDb::getInstance()->escapeString( $sFieldName );
00522 $sqlFull .= " {$sSqlBoolAction} {$sFieldName} ";
00523
00524
00525 $sSqlBoolAction = ' and ';
00526
00527 $sqlFull .= $this->_buildFilter( $sVal, $blIsSearchValue );
00528
00529 if ( $sUml ) {
00530 $sqlFull .= " or {$sFieldName} ";
00531
00532 $sqlFull .= $this->_buildFilter( $sUml, $blIsSearchValue );
00533 $sqlFull .= ')';
00534 }
00535 }
00536
00537
00538 $sqlFull .= ' ) ';
00539 }
00540 }
00541 }
00542
00543 return $sqlFull;
00544 }
00545
00553 protected function _changeselect( $sSql )
00554 {
00555 return $sSql;
00556 }
00557
00558
00564 public function buildWhere()
00565 {
00566 $myConfig = $this->getConfig();
00567 $this->_aWhere = array();
00568
00569 $iLanguage = oxLang::getInstance()->getBaseLanguage();
00570 $aWhere = oxConfig::getParameter( 'where' );
00571
00572 if ( !$this->_oList )
00573 return $this->_aWhere;
00574
00575 $oListObject = $this->_oList->getBaseObject();
00576 $sTable = $oListObject->getCoreTableName();
00577 $sViewName = getViewName( $sTable );
00578
00579 if ( $this->_oList && is_array( $aWhere ) ) {
00580
00581 $oStr = getStr();
00582 foreach ( $aWhere as $sName => $sValue ) {
00583 if ( $sValue || '0' === ( string ) $sValue ) {
00584
00585 if ( strpos( $sName, '.' ) === false ) {
00586
00587 $sName = "{$sTable}.{$sName}";
00588 }
00589
00590
00591
00592 if ( $oListObject instanceof oxI18n ) {
00593 $sFldName = strtolower( $oStr->preg_replace('/(.+)\./', '', $sName ) );
00594 if ( $oListObject->isMultilingualField( $sFldName ) && $iLanguage ) {
00595 $sName .= "_$iLanguage";
00596 }
00597 }
00598
00599
00600 $sLocalDateFormat = $this->getConfig()->getConfigParam( 'sLocalDateFormat' );
00601 if ( $sLocalDateFormat && $sLocalDateFormat != 'ISO') {
00602 $sFldName = strtolower( $oStr->preg_replace('/(.+)\./', '', $sName ) );
00603 $sLongName = $sTable."__".$sFldName;
00604 $sFldType = $oListObject->$sLongName->fldtype;
00605 if ( $sFldType && ( $sFldType == "datetime" || $sFldType == "date" ) ) {
00606 $sValue = $this->_convertToDBDate( $sValue, $sFldType );
00607 }
00608 }
00609
00610 $this->_aWhere[$sName] = "%{$sValue}%";
00611 }
00612 }
00613 }
00614
00615 return $this->_aWhere;
00616 }
00617
00626 protected function _convertToDBDate( $sValue, $sFldType )
00627 {
00628 $oConvObject = new oxField();
00629 $oConvObject->setValue($sValue);
00630 if ( $sFldType == "datetime" ) {
00631 if ( strlen($sValue) == 10 || strlen($sValue) == 22 || ( strlen($sValue) == 19 && !stripos( $sValue, "m" ) ) ) {
00632 oxDb::getInstance()->convertDBDateTime( $oConvObject, true );
00633 } else {
00634 if ( strlen($sValue) > 10 ) {
00635 return $this->_convertTime( $sValue );
00636 } else {
00637 return $this->_convertDate( $sValue );
00638 }
00639 }
00640 } elseif ( $sFldType == "date" ) {
00641 if ( strlen($sValue) == 10 ) {
00642 oxDb::getInstance()->convertDBDate( $oConvObject, true);
00643 } else {
00644 return $this->_convertDate( $sValue );
00645 }
00646 }
00647 return $oConvObject->value;
00648 }
00649
00657 protected function _convertDate( $sDate )
00658 {
00659
00660 $aDatePatterns = array("/^([0-9]{2})\.([0-9]{4})/" => "EUR2",
00661 "/^([0-9]{2})\.([0-9]{2})/" => "EUR1",
00662 "/^([0-9]{2})\/([0-9]{4})/" => "USA2",
00663 "/^([0-9]{2})\/([0-9]{2})/" => "USA1"
00664 );
00665
00666
00667 $aDFormats = array("EUR1" => array(2, 1),
00668 "EUR2" => array(2, 1),
00669 "USA1" => array(1, 2),
00670 "USA2" => array(2, 1)
00671 );
00672
00673
00674 $aDateMatches = array();
00675 $oStr = getStr();
00676 foreach ( $aDatePatterns as $sPattern => $sType) {
00677 if ( $oStr->preg_match( $sPattern, $sDate, $aDateMatches)) {
00678 $sDate = $aDateMatches[$aDFormats[$sType][0]] . "-" . $aDateMatches[$aDFormats[$sType][1]];
00679 break;
00680 }
00681 }
00682
00683 return $sDate;
00684 }
00685
00693 protected function _convertTime( $sFullDate )
00694 {
00695 $sDate = substr( $sFullDate, 0, 10 );
00696 $oConvObject = new oxField();
00697 $oConvObject->setValue($sDate);
00698 oxDb::getInstance()->convertDBDate( $oConvObject, true);
00699 $oStr = getStr();
00700
00701
00702 $sTime = substr( $sFullDate, 11);
00703 if ( $oStr->preg_match( "/([0-9]{2}):([0-9]{2}) ([AP]{1}[M]{1})$/", $sTime, $aTimeMatches ) ) {
00704 if ( $aTimeMatches[3] == "PM") {
00705 $iIntVal = (int) $aTimeMatches[1];
00706 if ( $iIntVal < 13) {
00707 $sTime = ($iIntVal + 12) . ":" . $aTimeMatches[2];
00708 }
00709 } else {
00710 $sTime = $aTimeMatches[1] . ":" . $aTimeMatches[2];
00711 }
00712 } elseif ( $oStr->preg_match( "/([0-9]{2}) ([AP]{1}[M]{1})$/", $sTime, $aTimeMatches ) ) {
00713 if ( $aTimeMatches[2] == "PM") {
00714 $iIntVal = (int) $aTimeMatches[1];
00715 if ( $iIntVal < 13) {
00716 $sTime = ($iIntVal + 12);
00717 }
00718 } else {
00719 $sTime = $aTimeMatches[1];
00720 }
00721 } else {
00722 $sTime = str_replace(".", ":", $sTime);
00723 }
00724
00725 return $oConvObject->value . " " . $sTime;
00726 }
00727
00733 protected function _setListNavigationParams()
00734 {
00735 $myConfig = $this->getConfig();
00736
00737
00738 $blShowNavigation = false;
00739 $iAdminListSize = $this->_getViewListSize();
00740 if ( $this->_iListSize > $iAdminListSize ) {
00741
00742 $pagenavigation = new oxStdClass();
00743 $pagenavigation->pages = round( ( ( $this->_iListSize - 1 ) / $iAdminListSize ) + 0.5, 0 );
00744 $pagenavigation->actpage = ($pagenavigation->actpage > $pagenavigation->pages)? $pagenavigation->pages : round( ( $this->_iCurrListPos / $iAdminListSize ) + 0.5, 0 );
00745 $pagenavigation->lastlink = ( $pagenavigation->pages - 1 ) * $iAdminListSize;
00746 $pagenavigation->nextlink = null;
00747 $pagenavigation->backlink = null;
00748
00749 $iPos = $this->_iCurrListPos + $iAdminListSize;
00750 if ( $iPos < $this->_iListSize ) {
00751 $pagenavigation->nextlink = $iPos = $this->_iCurrListPos + $iAdminListSize;
00752 }
00753
00754 if ( ( $this->_iCurrListPos - $iAdminListSize ) >= 0 ) {
00755 $pagenavigation->backlink = $iPos = $this->_iCurrListPos - $iAdminListSize;
00756 }
00757
00758
00759 $iStart = $pagenavigation->actpage - 5;
00760 $iStart = ( $iStart <= 0 ) ? 1 : $iStart;
00761
00762
00763 $iEnd = $pagenavigation->actpage + 5;
00764 $iEnd = ( $iEnd < $iStart + 10) ? $iStart + 10 : $iEnd;
00765 $iEnd = ( $iEnd > $pagenavigation->pages ) ? $pagenavigation->pages : $iEnd;
00766
00767
00768 $iStart = ( $iEnd - 10 > 0 ) ? $iEnd - 10 : $iStart;
00769 $iStart = ( $pagenavigation->pages <= 11) ? 1 : $iStart;
00770
00771
00772 for ( $i = $iStart; $i <= $iEnd; $i++ ) {
00773 $page = new Oxstdclass();
00774 $page->selected = 0;
00775 if ( $i == $pagenavigation->actpage ) {
00776 $page->selected = 1;
00777 }
00778 $pagenavigation->changePage[$i] = $page;
00779 }
00780
00781 $this->_aViewData['pagenavi'] = $pagenavigation;
00782
00783 if ( isset( $this->_iOverPos)) {
00784 $iPos = $this->_iOverPos;
00785 $this->_iOverPos = null;
00786 } else {
00787 $iPos = oxConfig::getParameter( 'lstrt' );
00788 }
00789
00790 if ( !$iPos ) {
00791 $iPos = 0;
00792 }
00793
00794 $this->_aViewData['lstrt'] = $iPos;
00795 $this->_aViewData['listsize'] = $this->_iListSize;
00796 $blShowNavigation = true;
00797 }
00798
00799
00800 $iShowListSize = $this->_iListSize - $this->_iCurrListPos;
00801 $iAdminListSize = $this->_getViewListSize();
00802 $iNotUsed = $iAdminListSize - min( $iShowListSize, $iAdminListSize );
00803 $iSpace = $iNotUsed * 15;
00804
00805 if ( !$blShowNavigation ) {
00806 $iSpace += 20;
00807 }
00808
00809 $this->_aViewData['iListFillsize'] = $iSpace;
00810 }
00811
00819 protected function _setupNavigation( $sNode )
00820 {
00821
00822 if ( $sNode ) {
00823
00824 $myAdminNavig = $this->getNavigation();
00825
00826 $sOxId = oxConfig::getParameter( 'oxid' );
00827
00828 if ( $sOxId == -1) {
00829
00830 $iActTab = $this->_iDefEdit;
00831 } else {
00832
00833 $iActTab = oxConfig::getParameter( 'actedit' );
00834 $iActTab = $iActTab ? $iActTab : $this->_iDefEdit;
00835 }
00836
00837
00838 $this->_aViewData['editnavi'] = $myAdminNavig->getTabs( $sNode, $iActTab );
00839
00840
00841 $this->_aViewData['actlocation'] = $myAdminNavig->getActiveTab( $sNode, $iActTab );
00842
00843
00844 $this->_aViewData['default_edit'] = $myAdminNavig->getActiveTab( $sNode, $this->_iDefEdit );
00845
00846
00847 $this->_aViewData['actedit'] = $iActTab;
00848 }
00849 }
00850
00856 public function getItemList()
00857 {
00858 return $this->_oList;
00859 }
00860 }