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() ) {
00167                 // is the object multilingual?
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             // calculates count of list items
00181             $this->_calcListItemsCount( $sSql );
00182 
00183             // setting current list position (page)
00184             $this->_setCurrentListPosition( oxConfig::getParameter( 'jumppage' ) );
00185 
00186             // settting additioan params for list: current list size
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         // assign our list
00203         if ( $this->_oList ) {
00204             $this->_aViewData['mylist'] = $this->_oList;
00205         }
00206 
00207         // setting filter data back to view
00208         $this->_setFilterParams();
00209 
00210         // set navigation parameters
00211         $this->_setListNavigationParams();
00212 
00213         // sorting
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         // build where
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 .= "&amp;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             //#M430: Pagination in admin list loses category parameter
00254             if ( $sChosenCat  = oxConfig::getParameter( "art_category") ) {
00255                 $sWhereParam .= "&amp;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         // #A - we must reset object ID
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         // count SQL
00292         $sSql = preg_replace( '/select .* from/', 'select count(*) from ', $sSql );
00293 
00294         // removing order by
00295         $sSql = preg_replace( '/order by .*$/', '', $sSql );
00296 
00297         // con of list items which fits current search conditions
00298         $this->_iListSize = oxDb::getDb()->getOne( $sSql );
00299 
00300         // set it into session that other frames know about size of DB
00301         oxSession::setVar( 'iArtCnt', $this->_iListSize );
00302     }
00303 
00311     protected function _setCurrentListPosition( $sPage = null )
00312     {
00313         $iAdminListSize = $this->_getViewListSize();
00314 
00315         $iJumpTo = $sPage?( (int) $sPage):( (int) ( (int) oxConfig::getParameter( 'lstrt' ) ) / $iAdminListSize );
00316         $iJumpTo = ( $sPage && $iJumpTo )?( $iJumpTo - 1 ):$iJumpTo;
00317 
00318         $iJumpTo = $iJumpTo * $iAdminListSize;
00319         if ( $iJumpTo < 1 ) {
00320             $iJumpTo = 0;
00321         } elseif ( $iJumpTo >= $this->_iListSize ) {
00322             $iJumpTo = floor( $this->_iListSize / $iAdminListSize - 1 ) * $iAdminListSize;
00323         }
00324 
00325         $this->_iCurrListPos = $this->_iOverPos = (int) $iJumpTo;
00326     }
00327 
00335     protected function _prepareOrderByQuery( $sSql = null )
00336     {
00337         // sorting
00338         $this->_aSort[0] = oxConfig::getParameter( 'sort' );
00339 
00340         if ( !isset( $this->_aSort[0]) || !$this->_aSort[0] )
00341             $this->_aSort[0] = $this->_sDefSort;
00342 
00343         $blSortDesc = $this->_blDesc;
00344 
00345         // add sorting
00346         if ( $this->_aSort && isset($this->_aSort[0] ) ) {
00347 
00348             $blSortDesc = oxConfig::getParameter( 'adminorder' );
00349 
00350             if ( !isset( $blSortDesc ) ) {
00351                 $blSortDesc = $this->_blDesc;
00352             }
00353 
00354             // only add order by at full sql not for count(*)
00355             $sSql .= ' order by ';
00356             $blSep = false;
00357             $iLang = null;
00358 
00359             $oListObject = $this->_oList->getBaseObject();
00360 
00361             if ( $oListObject->isMultilang() ) {
00362                 $iLang = $oListObject->getLanguage();
00363             }
00364 
00365             $sTable = $oListObject->getCoreTableName();
00366             $sViewName = getViewName( $sTable );
00367 
00368             foreach ( $this->_aSort as $orderColumn ) {
00369 
00370                 //V oxactive field search always DESC
00371                 if ( $orderColumn == "oxactive" ) {
00372                     $blSortDesc = true;
00373                 }
00374 
00375                 // multilanguage sorting
00376                 if ( $iLang ) {
00377                     $sObjectField = "{$sTable}__{$orderColumn}";
00378                     if ( $oListObject instanceof oxI18n ) {
00379                         if ( $oListObject->isMultilingualField($orderColumn) ) {
00380                             $orderColumn .= "_{$iLang}";
00381                         }
00382                     }
00383                 }
00384 
00385                 //add table name to column name if no table name found attached to column name
00386                 if ( strpos( $orderColumn, '.' ) === false ) {
00387                     $orderColumn = $sViewName . '.' . $orderColumn;
00388                 }
00389 
00390                 $sSql  .= ( ( ( $blSep ) ? ', ' : '' ) ) . oxDb::getInstance()->escapeString( $orderColumn );
00391                 $blSep  = true;
00392             }
00393 
00394             if ( $blSortDesc ) {
00395                 $sSql .= ' desc ';
00396             }
00397         }
00398 
00399         return $sSql;
00400     }
00401 
00409     protected function _buildSelectString( $oListObject = null )
00410     {
00411         $sSql = '';
00412 
00413         if ( $oListObject ) {
00414             $oBase = oxNew( 'oxBase' );
00415             $oBase->init( $oListObject->getCoreTableName() );
00416             $sSql = $oBase->buildSelectString( null );
00417         }
00418 
00419         return $sSql;
00420     }
00421 
00422 
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 
00844     public function getItemList()
00845     {
00846         return $this->_oList;
00847     }
00848 }

Generated by  doxygen 1.6.2