oxadminlist.php

Go to the documentation of this file.
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() ) { // is the object multilingual?
00167                 $oListObject->setLanguage( oxLang::getInstance()->getBaseLanguage() );
00168 
00169                 if ( isset( $this->_blEmployMultilanguage ) ) {
00170                     $oListObject->setEnableMultilang( $this->_blEmployMultilanguage );
00171                 }
00172             }
00173 
00174             $sSql = $this->_buildSelectString( $oListObject );
00175             $sSql = $this->_prepareWhereQuery( $aWhere, $sSql );
00176             $sSql = $this->_prepareOrderByQuery( $sSql );
00177             $sSql = $this->_changeselect( $sSql );
00178 
00179             // calculates count of list items
00180             $this->_calcListItemsCount( $sSql );
00181 
00182             // setting current list position (page)
00183             $this->_setCurrentListPosition( oxConfig::getParameter( 'jumppage' ) );
00184 
00185             // settting additioan params for list: current list size
00186             $this->_oList->setSqlLimit( $this->_iCurrListPos, $this->_getViewListSize() );
00187 
00188             $this->_oList->selectString( $sSql );
00189         }
00190     }
00191 
00197     public function render()
00198     {
00199         $sReturn = parent::render();
00200 
00201         // assign our list
00202         if ( $this->_oList ) {
00203             $this->_aViewData['mylist'] = $this->_oList;
00204         }
00205 
00206         // setting filter data back to view
00207         $this->_setFilterParams();
00208 
00209         // set navigation parameters
00210         $this->_setListNavigationParams();
00211 
00212         // sorting
00213         $this->_aSort[0] = oxConfig::getParameter( 'sort' );
00214         if ( !isset( $this->_aSort[0]) || !$this->_aSort[0] )
00215             $this->_aSort[0] = $this->_sDefSort;
00216 
00217         $this->_aViewData['sort'] = $this->_aSort[0];
00218 
00219         return $sReturn;
00220     }
00221 
00231     protected function _setFilterParams()
00232     {
00233         // build where
00234         if ( is_array( $aWhere = oxConfig::getParameter( 'where' ) ) ) {
00235 
00236             $myConfig = $this->getConfig();
00237             $myUtils  = oxUtils::getInstance();
00238             $sListTable = $myConfig->getGlobalParameter( 'ListCoreTable' );
00239 
00240             $oSearchKeys = new oxStdClass();
00241             $sWhereParam = "";
00242 
00243             while ( list( $sName, $sValue ) = each( $aWhere ) ) {
00244                 $sWhereParam .= "&amp;where[".$sName."]=".$sValue;
00245                 $sFieldName = str_replace( getViewName( $sListTable ) . '.', $sListTable . '.', $sName );
00246                 $sFieldName = $myUtils->getArrFldName( $sFieldName );
00247                 $oSearchKeys->$sFieldName = $sValue;
00248             }
00249 
00250             $this->_aViewData['where'] = $oSearchKeys;
00251 
00252             //#M430: Pagination in admin list loses category parameter
00253             if ( $sChosenCat  = oxConfig::getParameter( "art_category") ) {
00254                 $sWhereParam .= "&amp;art_category=".$sChosenCat;
00255             }
00256             $this->_aViewData['whereparam'] = $sWhereParam;
00257         }
00258     }
00259 
00265     public function deleteEntry()
00266     {
00267         $oDelete = oxNew( $this->_sListClass );
00268 
00269 
00270         $blDelete = $oDelete->delete( oxConfig::getParameter( 'oxid' ) );
00271 
00272         // #A - we must reset object ID
00273         if ( $blDelete && isset( $_POST['oxid'] ) ) {
00274             $_POST['oxid'] = -1;
00275         }
00276 
00277 
00278         $this->init();
00279     }
00280 
00288     protected function _calcListItemsCount( $sSql )
00289     {
00290         // count SQL
00291         $sSql = preg_replace( '/select .* from/', 'select count(*) from ', $sSql );
00292 
00293         // removing order by
00294         $sSql = preg_replace( '/order by .*$/', '', $sSql );
00295 
00296         // con of list items which fits current search conditions
00297         $this->_iListSize = oxDb::getDb()->getOne( $sSql );
00298 
00299         // set it into session that other frames know about size of DB
00300         oxSession::setVar( 'iArtCnt', $this->_iListSize );
00301     }
00302 
00310     protected function _setCurrentListPosition( $sPage = null )
00311     {
00312         $iAdminListSize = $this->_getViewListSize();
00313 
00314         $iJumpTo = $sPage?( (int) $sPage):( (int) ( (int) oxConfig::getParameter( 'lstrt' ) ) / $iAdminListSize );
00315         $iJumpTo = ( $sPage && $iJumpTo )?( $iJumpTo - 1 ):$iJumpTo;
00316 
00317         $iJumpTo = $iJumpTo * $iAdminListSize;
00318         if ( $iJumpTo < 1 ) {
00319             $iJumpTo = 0;
00320         } elseif ( $iJumpTo >= $this->_iListSize ) {
00321             $iJumpTo = floor( $this->_iListSize / $iAdminListSize - 1 ) * $iAdminListSize;
00322         }
00323 
00324         $this->_iCurrListPos = $this->_iOverPos = (int) $iJumpTo;
00325     }
00326 
00334     protected function _prepareOrderByQuery( $sSql = null )
00335     {
00336         // sorting
00337         $this->_aSort[0] = oxConfig::getParameter( 'sort' );
00338 
00339         if ( !isset( $this->_aSort[0]) || !$this->_aSort[0] )
00340             $this->_aSort[0] = $this->_sDefSort;
00341 
00342         $blSortDesc = $this->_blDesc;
00343 
00344         // add sorting
00345         if ( $this->_aSort && isset($this->_aSort[0] ) ) {
00346 
00347             $blSortDesc = oxConfig::getParameter( 'adminorder' );
00348 
00349             if ( !isset( $blSortDesc ) ) {
00350                 $blSortDesc = $this->_blDesc;
00351             }
00352 
00353             // only add order by at full sql not for count(*)
00354             $sSql .= ' order by ';
00355             $blSep = false;
00356             $iLang = null;
00357 
00358             $oListObject = $this->_oList->getBaseObject();
00359 
00360             if ( $oListObject->isMultilang() ) {
00361                 $iLang = $oListObject->getLanguage();
00362             }
00363 
00364             $sTable = $oListObject->getCoreTableName();
00365             $sViewName = getViewName( $sTable );
00366 
00367             foreach ( $this->_aSort as $orderColumn ) {
00368 
00369                 //V oxactive field search always DESC
00370                 if ( $orderColumn == "oxactive" ) {
00371                     $blSortDesc = true;
00372                 }
00373 
00374                 // multilanguage sorting
00375                 if ( $iLang ) {
00376                     $sObjectField = "{$sTable}__{$orderColumn}";
00377                     if ( $oListObject instanceof oxI18n ) {
00378                         if ( $oListObject->isMultilingualField($orderColumn) ) {
00379                             $orderColumn .= "_{$iLang}";
00380                         }
00381                     }
00382                 }
00383 
00384                 //add table name to column name if no table name found attached to column name
00385                 if ( strpos( $orderColumn, '.' ) === false ) {
00386                     $orderColumn = $sViewName . '.' . $orderColumn;
00387                 }
00388 
00389                 $sSql  .= ( ( ( $blSep ) ? ', ' : '' ) ) . oxDb::getInstance()->escapeString( $orderColumn );
00390                 $blSep  = true;
00391             }
00392 
00393             if ( $blSortDesc ) {
00394                 $sSql .= ' desc ';
00395             }
00396         }
00397 
00398         return $sSql;
00399     }
00400 
00408     protected function _buildSelectString( $oListObject = null )
00409     {
00410         $sSql = '';
00411 
00412         if ( $oListObject ) {
00413             $oBase = oxNew( 'oxBase' );
00414             $oBase->init( $oListObject->getCoreTableName() );
00415             $sSql = $oBase->buildSelectString( null );
00416         }
00417 
00418         return $sSql;
00419     }
00420 
00421 
00432     protected function _processFilter( $sFieldValue )
00433     {
00434         //removing % symbols
00435         $sFieldValue = preg_replace( "/^%|%$/", "", trim( $sFieldValue ) );
00436         return preg_replace( "/\s+/", " ", $sFieldValue);
00437     }
00438 
00447     protected function _buildFilter( $sVal, $blIsSearchValue )
00448     {
00449         if ( $blIsSearchValue ) {
00450             //is search string, using LIKE
00451             $sQ = " like '%{$sVal}%' ";
00452         } else {
00453             //not search string, values must be equal
00454             $sQ = " = ".oxDb::getDb()->quote( $sVal )." ";
00455         }
00456 
00457         return $sQ;
00458     }
00459 
00467     protected function _isSearchValue( $sFieldValue )
00468     {
00469         //check if this is search string (conatains % sign at begining and end of string)
00470         $blIsSearchValue = false;
00471         if ( preg_match( '/^%/', $sFieldValue ) && preg_match( '/%$/', $sFieldValue ) ) {
00472             $blIsSearchValue = true;
00473         }
00474 
00475         //removing % symbols
00476         return $blIsSearchValue;
00477     }
00478 
00489     protected function _prepareWhereQuery( $aWhere, $sqlFull )
00490     {
00491         if ( count($aWhere) ) {
00492             $myUtilsString = oxUtilsString::getInstance();
00493             while ( list($sFieldName, $sFieldValue) = each( $aWhere ) ) {
00494                 $sFieldValue = trim( $sFieldValue );
00495 
00496                 //check if this is search string (conatains % sign at begining and end of string)
00497                 $blIsSearchValue = $this->_isSearchValue( $sFieldValue );
00498 
00499                 //removing % symbols
00500                 $sFieldValue = $this->_processFilter( $sFieldValue );
00501 
00502                 if ( strlen($sFieldValue) ) {
00503                     $aVal = explode( ' ', $sFieldValue );
00504 
00505                     //for each search field using AND anction
00506                     $sSqlBoolAction = ' and (';
00507 
00508                     foreach ( $aVal as $sVal) {
00509 
00510                         $sFieldName = oxDb::getInstance()->escapeString( $sFieldName );
00511                         $sqlFull .= " {$sSqlBoolAction} {$sFieldName} ";
00512 
00513                         //for search in same field for different values using AND
00514                         $sSqlBoolAction = ' and ';
00515 
00516                         $sqlFull .= $this->_buildFilter( $sVal, $blIsSearchValue );
00517 
00518                         // trying to search spec chars in search value
00519                         // if found, add cleaned search value to search sql
00520                         $sUml = $myUtilsString->prepareStrForSearch( $sVal );
00521                         if ( $sUml ) {
00522                             $sqlFull .= " or {$sFieldName} ";
00523 
00524                             $sqlFull .= $this->_buildFilter( $sUml, $blIsSearchValue );
00525                         }
00526                     }
00527 
00528                     // end for AND action
00529                     $sqlFull .= ' ) ';
00530                 }
00531             }
00532         }
00533 
00534         return $sqlFull;
00535     }
00536 
00544     protected function _changeselect( $sSql )
00545     {
00546         return $sSql;
00547     }
00548 
00549 
00555     public function buildWhere()
00556     {
00557         $myConfig = $this->getConfig();
00558         $this->_aWhere = array();
00559 
00560         $iLanguage = oxLang::getInstance()->getBaseLanguage();
00561         $aWhere    = oxConfig::getParameter( 'where' );
00562 
00563         if ( !$this->_oList )
00564             return $this->_aWhere;
00565 
00566         $oListObject = $this->_oList->getBaseObject();
00567         $sTable = $oListObject->getCoreTableName();
00568         $sViewName = getViewName( $sTable );
00569 
00570         if ( $this->_oList && is_array( $aWhere ) ) {
00571 
00572             foreach ( $aWhere as $sName => $sValue ) {
00573                 if ( $sValue || '0' === ( string ) $sValue ) {
00574 
00575                     if ( strpos( $sName, '.' ) === false ) {
00576                         // if no table name attached to field name, add it
00577                         $sName = "{$sTable}.{$sName}";
00578                     }
00579 
00580 
00581                     // test if field is multilang
00582                     if ( $oListObject instanceof oxI18n ) {
00583                         $sFldName = strtolower( preg_replace('/(.+)\./', '', $sName ) );
00584                         if ( $oListObject->isMultilingualField( $sFldName ) && $iLanguage ) {
00585                             $sName .=  "_$iLanguage";
00586                         }
00587                     }
00588 
00589                     // #M1260: if field is date
00590                     $sLocalDateFormat = $this->getConfig()->getConfigParam( 'sLocalDateFormat' );
00591                     if ( $sLocalDateFormat && $sLocalDateFormat != 'ISO') {
00592                         $sFldName = strtolower( preg_replace('/(.+)\./', '', $sName ) );
00593                         $sLongName = $sTable."__".$sFldName;
00594                         $sFldType = $oListObject->$sLongName->fldtype;
00595                         if ( $sFldType && ( $sFldType == "datetime" || $sFldType == "date" ) ) {
00596                             $sValue = $this->_convertToDBDate( $sValue, $sFldType );
00597                         }
00598                     }
00599 
00600                     $this->_aWhere[$sName] = "%{$sValue}%";
00601                 }
00602             }
00603         }
00604 
00605         return $this->_aWhere;
00606     }
00607 
00616     protected function _convertToDBDate( $sValue, $sFldType )
00617     {
00618         $oConvObject = new oxField();
00619         $oConvObject->setValue($sValue);
00620         if ( $sFldType == "datetime" ) {
00621             if ( strlen($sValue) == 10 || strlen($sValue) == 22 || ( strlen($sValue) == 19 && !stripos( $sValue, "m" ) ) ) {
00622                 oxDb::getInstance()->convertDBDateTime( $oConvObject, true );
00623             } else {
00624                 if ( strlen($sValue) > 10 ) {
00625                     return $this->_convertTime( $sValue );
00626                 } else {
00627                     return $this->_convertDate( $sValue );
00628                 }
00629             }
00630         } elseif ( $sFldType == "date" ) {
00631             if ( strlen($sValue) == 10 ) {
00632                 oxDb::getInstance()->convertDBDate( $oConvObject, true);
00633             } else {
00634                 return $this->_convertDate( $sValue );
00635             }
00636         }
00637         return $oConvObject->value;
00638     }
00639 
00647     protected function _convertDate( $sDate )
00648     {
00649         // regexps to validate input
00650         $aDatePatterns = array("/^([0-9]{2})\.([0-9]{4})/" => "EUR2",    // MM.YYYY
00651                                "/^([0-9]{2})\.([0-9]{2})/" => "EUR1",    // DD.MM
00652                                "/^([0-9]{2})\/([0-9]{4})/" => "USA2",    // MM.YYYY
00653                                "/^([0-9]{2})\/([0-9]{2})/" => "USA1"     // DD.MM
00654                               );
00655 
00656         // date/time formatting rules
00657         $aDFormats  = array("EUR1" => array(2, 1),
00658                             "EUR2" => array(2, 1),
00659                             "USA1" => array(1, 2),
00660                             "USA2" => array(2, 1)
00661                            );
00662 
00663         // looking for date field
00664         $aDateMatches = array();
00665         foreach ( $aDatePatterns as $sPattern => $sType) {
00666             if ( preg_match( $sPattern, $sDate, $aDateMatches)) {
00667                 $sDate = $aDateMatches[$aDFormats[$sType][0]] . "-" . $aDateMatches[$aDFormats[$sType][1]];
00668                 break;
00669             }
00670         }
00671 
00672         return $sDate;
00673     }
00674 
00682     protected function _convertTime( $sFullDate )
00683     {
00684         $sDate = substr( $sFullDate, 0, 10 );
00685         $oConvObject = new oxField();
00686         $oConvObject->setValue($sDate);
00687         oxDb::getInstance()->convertDBDate( $oConvObject, true);
00688 
00689         // looking for time field
00690         $sTime = substr( $sFullDate, 11);
00691         if ( preg_match( "/([0-9]{2}):([0-9]{2}) ([AP]{1}[M]{1})$/", $sTime, $aTimeMatches ) ) {
00692             if ( $aTimeMatches[3] == "PM") {
00693                 $iIntVal = (int) $aTimeMatches[1];
00694                 if ( $iIntVal < 13) {
00695                     $sTime = ($iIntVal + 12) . ":" . $aTimeMatches[2];
00696                 }
00697             } else {
00698                 $sTime = $aTimeMatches[1] . ":" . $aTimeMatches[2];
00699             }
00700         } elseif ( preg_match( "/([0-9]{2}) ([AP]{1}[M]{1})$/", $sTime, $aTimeMatches ) ) {
00701             if ( $aTimeMatches[2] == "PM") {
00702                 $iIntVal = (int) $aTimeMatches[1];
00703                 if ( $iIntVal < 13) {
00704                     $sTime = ($iIntVal + 12);
00705                 }
00706             } else {
00707                 $sTime = $aTimeMatches[1];
00708             }
00709         } else {
00710             $sTime = str_replace(".", ":", $sTime);
00711         }
00712 
00713         return $oConvObject->value . " " . $sTime;
00714     }
00715 
00721     protected function _setListNavigationParams()
00722     {
00723         $myConfig  = $this->getConfig();
00724 
00725         // list navigation
00726         $blShowNavigation = false;
00727         $iAdminListSize = $this->_getViewListSize();
00728         if ( $this->_iListSize > $iAdminListSize ) {
00729             // yes, we need to build the navigation object
00730             $pagenavigation = new oxStdClass();
00731             $pagenavigation->pages    = round( ( ( $this->_iListSize - 1 ) / $iAdminListSize ) + 0.5, 0 );
00732             $pagenavigation->actpage  = ($pagenavigation->actpage > $pagenavigation->pages)? $pagenavigation->pages : round( ( $this->_iCurrListPos / $iAdminListSize ) + 0.5, 0 );
00733             $pagenavigation->lastlink = ( $pagenavigation->pages - 1 ) * $iAdminListSize;
00734             $pagenavigation->nextlink = null;
00735             $pagenavigation->backlink = null;
00736 
00737             $iPos = $this->_iCurrListPos + $iAdminListSize;
00738             if ( $iPos < $this->_iListSize ) {
00739                 $pagenavigation->nextlink = $iPos = $this->_iCurrListPos + $iAdminListSize;
00740             }
00741 
00742             if ( ( $this->_iCurrListPos - $iAdminListSize ) >= 0 ) {
00743                 $pagenavigation->backlink = $iPos = $this->_iCurrListPos - $iAdminListSize;
00744             }
00745 
00746             // calculating list start position
00747             $iStart = $pagenavigation->actpage - 5;
00748             $iStart = ( $iStart <= 0 ) ? 1 : $iStart;
00749 
00750             // calculating list end position
00751             $iEnd = $pagenavigation->actpage + 5;
00752             $iEnd = ( $iEnd < $iStart + 10) ? $iStart + 10 : $iEnd;
00753             $iEnd = ( $iEnd > $pagenavigation->pages ) ? $pagenavigation->pages : $iEnd;
00754 
00755             // once again adjusting start pos ..
00756             $iStart = ( $iEnd - 10 > 0 ) ? $iEnd - 10 : $iStart;
00757             $iStart = ( $pagenavigation->pages <= 11) ? 1 : $iStart;
00758 
00759             // navigation urls
00760             for ( $i = $iStart; $i <= $iEnd; $i++ ) {
00761                 $page = new Oxstdclass();
00762                 $page->selected = 0;
00763                 if ( $i == $pagenavigation->actpage ) {
00764                     $page->selected = 1;
00765                 }
00766                 $pagenavigation->changePage[$i] = $page;
00767             }
00768 
00769             $this->_aViewData['pagenavi'] = $pagenavigation;
00770 
00771             if ( isset( $this->_iOverPos)) {
00772                 $iPos = $this->_iOverPos;
00773                 $this->_iOverPos = null;
00774             } else {
00775                 $iPos = oxConfig::getParameter( 'lstrt' );
00776             }
00777 
00778             if ( !$iPos ) {
00779                 $iPos = 0;
00780             }
00781 
00782             $this->_aViewData['lstrt']    = $iPos;
00783             $this->_aViewData['listsize'] = $this->_iListSize;
00784             $blShowNavigation = true;
00785         }
00786 
00787         // determine not used space in List
00788         $iShowListSize  = $this->_iListSize - $this->_iCurrListPos;
00789         $iAdminListSize = $this->_getViewListSize();
00790         $iNotUsed = $iAdminListSize - min( $iShowListSize, $iAdminListSize );
00791         $iSpace = $iNotUsed * 15;
00792 
00793         if ( !$blShowNavigation ) {
00794             $iSpace += 20;
00795         }
00796 
00797         $this->_aViewData['iListFillsize'] = $iSpace;
00798     }
00799 
00807     protected function _setupNavigation( $sNode )
00808     {
00809         // navigation according to class
00810         if ( $sNode ) {
00811 
00812             $myAdminNavig = $this->getNavigation();
00813 
00814             $sOxId = oxConfig::getParameter( 'oxid' );
00815 
00816             if( $sOxId == -1) {
00817                 //on first call or when pressed creating new item button, reseting active tab
00818                 $iActTab = $this->_iDefEdit;
00819             } else {
00820                 // active tab
00821                 $iActTab = oxConfig::getParameter( 'actedit' );
00822                 $iActTab = $iActTab ? $iActTab : $this->_iDefEdit;
00823             }
00824 
00825             // tabs
00826             $this->_aViewData['editnavi'] = $myAdminNavig->getTabs( $sNode, $iActTab );
00827 
00828             // active tab
00829             $this->_aViewData['actlocation'] = $myAdminNavig->getActiveTab( $sNode, $iActTab );
00830 
00831             // default tab
00832             $this->_aViewData['default_edit'] = $myAdminNavig->getActiveTab( $sNode, $this->_iDefEdit );
00833 
00834             // passign active tab number
00835             $this->_aViewData['actedit'] = $iActTab;
00836         }
00837     }
00838 }

Generated on Tue Sep 29 16:45:12 2009 for OXID eShop CE by  doxygen 1.5.5