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 ) ? ', ' : '' ) ) . $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                         $sqlFull .= " {$sSqlBoolAction} {$sFieldName} ";
00511 
00512                         //for search in same field for different values using AND
00513                         $sSqlBoolAction = ' and ';
00514 
00515                         $sqlFull .= $this->_buildFilter( $sVal, $blIsSearchValue );
00516 
00517                         // trying to search spec chars in search value
00518                         // if found, add cleaned search value to search sql
00519                         $sUml = $myUtilsString->prepareStrForSearch( $sVal );
00520                         if ( $sUml ) {
00521                             $sqlFull .= " or {$sFieldName} ";
00522 
00523                             $sqlFull .= $this->_buildFilter( $sUml, $blIsSearchValue );
00524                         }
00525                     }
00526 
00527                     // end for AND action
00528                     $sqlFull .= ' ) ';
00529                 }
00530             }
00531         }
00532 
00533         return $sqlFull;
00534     }
00535 
00543     protected function _changeselect( $sSql )
00544     {
00545         return $sSql;
00546     }
00547 
00548 
00554     public function buildWhere()
00555     {
00556         $myConfig = $this->getConfig();
00557         $this->_aWhere = array();
00558 
00559         $iLanguage = oxLang::getInstance()->getBaseLanguage();
00560         $aWhere    = oxConfig::getParameter( 'where' );
00561 
00562         if ( !$this->_oList )
00563             return $this->_aWhere;
00564 
00565         $oListObject = $this->_oList->getBaseObject();
00566         $sTable = $oListObject->getCoreTableName();
00567         $sViewName = getViewName( $sTable );
00568 
00569         if ( $this->_oList && is_array( $aWhere ) ) {
00570 
00571             foreach ( $aWhere as $sName => $sValue ) {
00572                 if ( $sValue || '0' === ( string ) $sValue ) {
00573 
00574                     if ( strpos( $sName, '.' ) === false ) {
00575                         // if no table name attached to field name, add it
00576                         $sName = "{$sTable}.{$sName}";
00577                     }
00578 
00579 
00580                     // test if field is multilang
00581                     if ( $oListObject instanceof oxI18n ) {
00582                         $sFldName = strtolower( preg_replace('/(.+)\./', '', $sName ) );
00583                         if ( $oListObject->isMultilingualField( $sFldName ) && $iLanguage ) {
00584                             $sName .=  "_$iLanguage";
00585                         }
00586                     }
00587 
00588                     $this->_aWhere[$sName] = "%{$sValue}%";
00589                 }
00590             }
00591         }
00592 
00593         return $this->_aWhere;
00594     }
00595 
00601     protected function _setListNavigationParams()
00602     {
00603         $myConfig  = $this->getConfig();
00604 
00605         // list navigation
00606         $blShowNavigation = false;
00607         $iAdminListSize = $this->_getViewListSize();
00608         if ( $this->_iListSize > $iAdminListSize ) {
00609             // yes, we need to build the navigation object
00610             $pagenavigation = new oxStdClass();
00611             $pagenavigation->pages    = round( ( ( $this->_iListSize - 1 ) / $iAdminListSize ) + 0.5, 0 );
00612             $pagenavigation->actpage  = ($pagenavigation->actpage > $pagenavigation->pages)? $pagenavigation->pages : round( ( $this->_iCurrListPos / $iAdminListSize ) + 0.5, 0 );
00613             $pagenavigation->lastlink = ( $pagenavigation->pages - 1 ) * $iAdminListSize;
00614             $pagenavigation->nextlink = null;
00615             $pagenavigation->backlink = null;
00616 
00617             $iPos = $this->_iCurrListPos + $iAdminListSize;
00618             if ( $iPos < $this->_iListSize ) {
00619                 $pagenavigation->nextlink = $iPos = $this->_iCurrListPos + $iAdminListSize;
00620             }
00621 
00622             if ( ( $this->_iCurrListPos - $iAdminListSize ) >= 0 ) {
00623                 $pagenavigation->backlink = $iPos = $this->_iCurrListPos - $iAdminListSize;
00624             }
00625 
00626             // calculating list start position
00627             $iStart = $pagenavigation->actpage - 5;
00628             $iStart = ( $iStart <= 0 ) ? 1 : $iStart;
00629 
00630             // calculating list end position
00631             $iEnd = $pagenavigation->actpage + 5;
00632             $iEnd = ( $iEnd < $iStart + 10) ? $iStart + 10 : $iEnd;
00633             $iEnd = ( $iEnd > $pagenavigation->pages ) ? $pagenavigation->pages : $iEnd;
00634 
00635             // once again adjusting start pos ..
00636             $iStart = ( $iEnd - 10 > 0 ) ? $iEnd - 10 : $iStart;
00637             $iStart = ( $pagenavigation->pages <= 11) ? 1 : $iStart;
00638 
00639             // navigation urls
00640             for ( $i = $iStart; $i <= $iEnd; $i++ ) {
00641                 $page = new Oxstdclass();
00642                 $page->selected = 0;
00643                 if ( $i == $pagenavigation->actpage ) {
00644                     $page->selected = 1;
00645                 }
00646                 $pagenavigation->changePage[$i] = $page;
00647             }
00648 
00649             $this->_aViewData['pagenavi'] = $pagenavigation;
00650 
00651             if ( isset( $this->_iOverPos)) {
00652                 $iPos = $this->_iOverPos;
00653                 $this->_iOverPos = null;
00654             } else {
00655                 $iPos = oxConfig::getParameter( 'lstrt' );
00656             }
00657 
00658             if ( !$iPos ) {
00659                 $iPos = 0;
00660             }
00661 
00662             $this->_aViewData['lstrt']    = $iPos;
00663             $this->_aViewData['listsize'] = $this->_iListSize;
00664             $blShowNavigation = true;
00665         }
00666 
00667         // determine not used space in List
00668         $iShowListSize  = $this->_iListSize - $this->_iCurrListPos;
00669         $iAdminListSize = $this->_getViewListSize();
00670         $iNotUsed = $iAdminListSize - min( $iShowListSize, $iAdminListSize );
00671         $iSpace = $iNotUsed * 15;
00672 
00673         if ( !$blShowNavigation ) {
00674             $iSpace += 20;
00675         }
00676 
00677         $this->_aViewData['iListFillsize'] = $iSpace;
00678     }
00679 
00687     protected function _setupNavigation( $sNode )
00688     {
00689         // navigation according to class
00690         if ( $sNode ) {
00691 
00692             $myAdminNavig = $this->getNavigation();
00693 
00694             $sOxId = oxConfig::getParameter( 'oxid' );
00695 
00696             if( $sOxId == -1) {
00697                 //on first call or when pressed creating new item button, reseting active tab
00698                 $iActTab = $this->_iDefEdit;
00699             } else {
00700                 // active tab
00701                 $iActTab = oxConfig::getParameter( 'actedit' );
00702                 $iActTab = $iActTab ? $iActTab : $this->_iDefEdit;
00703             }
00704 
00705             // tabs
00706             $this->_aViewData['editnavi'] = $myAdminNavig->getTabs( $sNode, $iActTab );
00707 
00708             // active tab
00709             $this->_aViewData['actlocation'] = $myAdminNavig->getActiveTab( $sNode, $iActTab );
00710 
00711             // default tab
00712             $this->_aViewData['default_edit'] = $myAdminNavig->getActiveTab( $sNode, $this->_iDefEdit );
00713 
00714             // passign active tab number
00715             $this->_aViewData['actedit'] = $iActTab;
00716         }
00717     }
00718 }

Generated on Wed Apr 22 12:26:30 2009 for OXID eShop CE by  doxygen 1.5.5