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 $_blDesc = false;
00056 
00062     protected $_blEmployMultilanguage = null;
00063 
00069     protected $_iOverPos = null;
00070 
00076     protected $_iViewListSize = 0;
00077 
00083     protected $_iDefViewListSize = 50;
00084 
00090     protected $_aCurrSorting = null;
00091 
00097     protected $_sDefSortField = null;
00098 
00104     protected $_aListFilter = null;
00105 
00111     public function getListSorting()
00112     {
00113         if ( $this->_aCurrSorting === null ) {
00114             $this->_aCurrSorting = oxConfig::getParameter( 'sort' );
00115 
00116             if ( !$this->_aCurrSorting && $this->_sDefSortField && ( $oBaseObject = $this->getItemListBaseObject() ) ) {
00117                 $this->_aCurrSorting[$oBaseObject->getCoreTableName()] = array( $this->_sDefSortField => "asc" );
00118             }
00119         }
00120 
00121         return $this->_aCurrSorting;
00122     }
00123 
00129     public function getListFilter()
00130     {
00131         if ( $this->_aListFilter === null ) {
00132             $this->_aListFilter = oxConfig::getParameter( "where" );
00133         }
00134 
00135         return $this->_aListFilter;
00136     }
00137 
00143     protected function _getViewListSize()
00144     {
00145         if ( !$this->_iViewListSize ) {
00146             $myConfig = $this->getConfig();
00147             if ( $aProfile = oxSession::getVar( 'profile' ) ) {
00148                 if ( isset( $aProfile[1] ) ) {
00149                     $myConfig->setConfigParam( 'iAdminListSize', (int) $aProfile[1] );
00150                 }
00151             }
00152 
00153             $this->_iViewListSize = (int) $myConfig->getConfigParam( 'iAdminListSize' );
00154             if ( !$this->_iViewListSize ) {
00155                 $this->_iViewListSize = 10;
00156                 $myConfig->setConfigParam( 'iAdminListSize', $this->_iViewListSize );
00157             }
00158         }
00159 
00160         return $this->_iViewListSize;
00161     }
00162 
00168     public function getViewListSize()
00169     {
00170         return $this->_getViewListSize();
00171     }
00172 
00178     protected function _getUserDefListSize()
00179     {
00180         if ( !$this->_iViewListSize ) {
00181             if ( ! ($iViewListSize = (int) oxConfig::getParameter( 'viewListSize' ) ) ) {
00182                 $iViewListSize = $this->_iDefViewListSize;
00183             }
00184             $this->_iViewListSize = $iViewListSize;
00185         }
00186 
00187         return $this->_iViewListSize;
00188     }
00189 
00195     public function render()
00196     {
00197         $sReturn = parent::render();
00198 
00199         // assign our list
00200         $this->_aViewData['mylist'] = $this->getItemList();
00201 
00202         // set navigation parameters
00203         $this->_setListNavigationParams();
00204 
00205         return $sReturn;
00206     }
00207 
00213     public function deleteEntry()
00214     {
00215         $oDelete = oxNew( $this->_sListClass );
00216 
00217 
00218         $blDelete = $oDelete->delete( $this->getEditObjectId() );
00219 
00220         // #A - we must reset object ID
00221         if ( $blDelete && isset( $_POST['oxid'] ) ) {
00222             $_POST['oxid'] = -1;
00223         }
00224 
00225 
00226         $this->init();
00227     }
00228 
00236     protected function _calcListItemsCount( $sSql )
00237     {
00238         $oStr = getStr();
00239 
00240         // count SQL
00241         $sSql = $oStr->preg_replace( '/select .* from/', 'select count(*) from ', $sSql );
00242 
00243         // removing order by
00244         $sSql = $oStr->preg_replace( '/order by .*$/', '', $sSql );
00245 
00246         // con of list items which fits current search conditions
00247         $this->_iListSize = oxDb::getDb()->getOne( $sSql, false, false );
00248 
00249         // set it into session that other frames know about size of DB
00250         oxSession::setVar( 'iArtCnt', $this->_iListSize );
00251     }
00252 
00260     protected function _setCurrentListPosition( $sPage = null )
00261     {
00262         $iAdminListSize = $this->_getViewListSize();
00263 
00264         $iJumpTo = $sPage?( (int) $sPage):( (int) ( (int) oxConfig::getParameter( 'lstrt' ) ) / $iAdminListSize );
00265         $iJumpTo = ( $sPage && $iJumpTo )?( $iJumpTo - 1 ):$iJumpTo;
00266 
00267         $iJumpTo = $iJumpTo * $iAdminListSize;
00268         if ( $iJumpTo < 1 ) {
00269             $iJumpTo = 0;
00270         } elseif ( $iJumpTo >= $this->_iListSize ) {
00271             $iJumpTo = floor( $this->_iListSize / $iAdminListSize - 1 ) * $iAdminListSize;
00272         }
00273 
00274         $this->_iCurrListPos = $this->_iOverPos = (int) $iJumpTo;
00275     }
00276 
00284     protected function _prepareOrderByQuery( $sSql = null )
00285     {
00286         // sorting
00287         $aSortFields = $this->getListSorting();
00288 
00289         if ( is_array( $aSortFields ) && count( $aSortFields ) ) {
00290 
00291             // only add order by at full sql not for count(*)
00292             $sSql .= ' order by ';
00293             $blSep = false;
00294 
00295             $oListItem = $this->getItemListBaseObject();
00296             $iLangId = $oListItem->isMultilang() ? $oListItem->getLanguage() : oxLang::getInstance()->getBaseLanguage();
00297 
00298             $blSortDesc = oxConfig::getParameter( 'adminorder' );
00299             $blSortDesc = $blSortDesc !== null ? (bool) $blSortDesc : $this->_blDesc;
00300 
00301             foreach ( $aSortFields as $sTable => $aFieldData ) {
00302 
00303                 $sTable = $sTable ? ( getViewName( $sTable, $iLangId ) . '.' ) : '';
00304                 foreach ( $aFieldData as $sColumn => $sSortDir ) {
00305 
00306                     $sField = $sTable . $sColumn;
00307 
00308                     //add table name to column name if no table name found attached to column name
00309                     $sSql .= ( ( ( $blSep ) ? ', ' : '' ) ) . oxDb::getInstance()->escapeString( $sField );
00310 
00311                     //V oxactive field search always DESC
00312                     if ( $blSortDesc || $sColumn == "oxactive" || strcasecmp( $sSortDir, 'desc' ) == 0 ) {
00313                         $sSql .= ' desc ';
00314                     }
00315 
00316                     $blSep = true;
00317                 }
00318             }
00319         }
00320 
00321         return $sSql;
00322     }
00323 
00331     protected function _buildSelectString( $oListObject = null )
00332     {
00333         return $oListObject !== null ? $oListObject->buildSelectString( null ) : "";
00334     }
00335 
00336 
00346     protected function _processFilter( $sFieldValue )
00347     {
00348         $oStr = getStr();
00349 
00350         //removing % symbols
00351         $sFieldValue = $oStr->preg_replace( "/^%|%$/", "", trim( $sFieldValue ) );
00352         return $oStr->preg_replace( "/\s+/", " ", $sFieldValue );
00353     }
00354 
00363     protected function _buildFilter( $sVal, $blIsSearchValue )
00364     {
00365         if ( $blIsSearchValue ) {
00366             //is search string, using LIKE
00367             $sQ = " like ".oxDb::getDb()->quote( '%'.$sVal.'%' )." ";
00368         } else {
00369             //not search string, values must be equal
00370             $sQ = " = ".oxDb::getDb()->quote( $sVal )." ";
00371         }
00372 
00373         return $sQ;
00374     }
00375 
00383     protected function _isSearchValue( $sFieldValue )
00384     {
00385         //check if this is search string (conatains % sign at begining and end of string)
00386         $blIsSearchValue = false;
00387         $oStr = getStr();
00388         if ( $oStr->preg_match( '/^%/', $sFieldValue ) && $oStr->preg_match( '/%$/', $sFieldValue ) ) {
00389             $blIsSearchValue = true;
00390         }
00391 
00392         //removing % symbols
00393         return $blIsSearchValue;
00394     }
00395 
00406     protected function _prepareWhereQuery( $aWhere, $sqlFull )
00407     {
00408         if ( count($aWhere) ) {
00409             $myUtilsString = oxUtilsString::getInstance();
00410             while ( list($sFieldName, $sFieldValue) = each( $aWhere ) ) {
00411                 $sFieldValue = trim( $sFieldValue );
00412 
00413                 //check if this is search string (conatains % sign at begining and end of string)
00414                 $blIsSearchValue = $this->_isSearchValue( $sFieldValue );
00415 
00416                 //removing % symbols
00417                 $sFieldValue = $this->_processFilter( $sFieldValue );
00418 
00419                 if ( strlen($sFieldValue) ) {
00420                     $aVal = explode( ' ', $sFieldValue );
00421 
00422                     //for each search field using AND anction
00423                     $sSqlBoolAction = ' and (';
00424 
00425                     foreach ( $aVal as $sVal) {
00426                         // trying to search spec chars in search value
00427                         // if found, add cleaned search value to search sql
00428                         $sUml = $myUtilsString->prepareStrForSearch( $sVal );
00429                         if ($sUml) {
00430                             $sSqlBoolAction .= '(';
00431                         }
00432 
00433                         $sFieldName = oxDb::getInstance()->escapeString( $sFieldName );
00434                         //$sVal = oxDb::getInstance()->escapeString( $sVal );
00435                         $sqlFull .= " {$sSqlBoolAction} {$sFieldName} ";
00436 
00437                         //for search in same field for different values using AND
00438                         $sSqlBoolAction = ' and ';
00439 
00440                         $sqlFull .= $this->_buildFilter( $sVal, $blIsSearchValue );
00441 
00442                         if ( $sUml ) {
00443                             $sqlFull .= " or {$sFieldName} ";
00444 
00445                             $sqlFull .= $this->_buildFilter( $sUml, $blIsSearchValue );
00446                             $sqlFull .= ')'; // end of OR section
00447                         }
00448                     }
00449 
00450                     // end for AND action
00451                     $sqlFull .= ' ) ';
00452                 }
00453             }
00454         }
00455 
00456         return $sqlFull;
00457     }
00458 
00466     protected function _changeselect( $sSql )
00467     {
00468         return $sSql;
00469     }
00470 
00471 
00477     public function buildWhere()
00478     {
00479         if ( $this->_aWhere === null && ( $oList = $this->getItemList() ) ) {
00480 
00481             $this->_aWhere = array();
00482             $aFilter = $this->getListFilter();
00483             if ( is_array( $aFilter ) ) {
00484 
00485                 $oListItem = $this->getItemListBaseObject();
00486                 $iLangId = $oListItem->isMultilang() ? $oListItem->getLanguage() : oxLang::getInstance()->getBaseLanguage();
00487                 $sLocalDateFormat = $this->getConfig()->getConfigParam( 'sLocalDateFormat' );
00488 
00489                 foreach ( $aFilter as $sTable => $aFilterData ) {
00490                     foreach ( $aFilterData as $sName => $sValue ) {
00491                         if ( $sValue || '0' === ( string ) $sValue ) {
00492 
00493                             $sField = "{$sTable}__{$sName}";
00494 
00495                             // if no table name attached to field name, add it
00496                             $sName = $sTable ? getViewName( $sTable, $iLangId ) . ".{$sName}" : $sName;
00497 
00498                             // #M1260: if field is date
00499                             if ( $sLocalDateFormat && $sLocalDateFormat != 'ISO' && isset( $oListItem->$sField ) ) {
00500                                 $sFldType = $oListItem->{$sField}->fldtype;
00501                                 if ( "datetime" == $sFldType || "date" == $sFldType ) {
00502                                     $sValue = $this->_convertToDBDate( $sValue, $sFldType );
00503                                 }
00504                             }
00505 
00506                             $this->_aWhere[$sName] = "%{$sValue}%";
00507                         }
00508                     }
00509                 }
00510             }
00511         }
00512 
00513         return $this->_aWhere;
00514     }
00515 
00524     protected function _convertToDBDate( $sValue, $sFldType )
00525     {
00526         $oConvObject = new oxField();
00527         $oConvObject->setValue($sValue);
00528         if ( $sFldType == "datetime" ) {
00529             if ( strlen($sValue) == 10 || strlen($sValue) == 22 || ( strlen($sValue) == 19 && !stripos( $sValue, "m" ) ) ) {
00530                 oxDb::getInstance()->convertDBDateTime( $oConvObject, true );
00531             } else {
00532                 if ( strlen($sValue) > 10 ) {
00533                     return $this->_convertTime( $sValue );
00534                 } else {
00535                     return $this->_convertDate( $sValue );
00536                 }
00537             }
00538         } elseif ( $sFldType == "date" ) {
00539             if ( strlen($sValue) == 10 ) {
00540                 oxDb::getInstance()->convertDBDate( $oConvObject, true);
00541             } else {
00542                 return $this->_convertDate( $sValue );
00543             }
00544         }
00545         return $oConvObject->value;
00546     }
00547 
00555     protected function _convertDate( $sDate )
00556     {
00557         // regexps to validate input
00558         $aDatePatterns = array("/^([0-9]{2})\.([0-9]{4})/" => "EUR2",    // MM.YYYY
00559                                "/^([0-9]{2})\.([0-9]{2})/" => "EUR1",    // DD.MM
00560                                "/^([0-9]{2})\/([0-9]{4})/" => "USA2",    // MM.YYYY
00561                                "/^([0-9]{2})\/([0-9]{2})/" => "USA1"     // DD.MM
00562                               );
00563 
00564         // date/time formatting rules
00565         $aDFormats  = array("EUR1" => array(2, 1),
00566                             "EUR2" => array(2, 1),
00567                             "USA1" => array(1, 2),
00568                             "USA2" => array(2, 1)
00569                            );
00570 
00571         // looking for date field
00572         $aDateMatches = array();
00573         $oStr = getStr();
00574         foreach ( $aDatePatterns as $sPattern => $sType) {
00575             if ( $oStr->preg_match( $sPattern, $sDate, $aDateMatches)) {
00576                 $sDate = $aDateMatches[$aDFormats[$sType][0]] . "-" . $aDateMatches[$aDFormats[$sType][1]];
00577                 break;
00578             }
00579         }
00580 
00581         return $sDate;
00582     }
00583 
00591     protected function _convertTime( $sFullDate )
00592     {
00593         $sDate = substr( $sFullDate, 0, 10 );
00594         $oConvObject = new oxField();
00595         $oConvObject->setValue($sDate);
00596         oxDb::getInstance()->convertDBDate( $oConvObject, true);
00597         $oStr = getStr();
00598 
00599         // looking for time field
00600         $sTime = substr( $sFullDate, 11);
00601         if ( $oStr->preg_match( "/([0-9]{2}):([0-9]{2}) ([AP]{1}[M]{1})$/", $sTime, $aTimeMatches ) ) {
00602             if ( $aTimeMatches[3] == "PM") {
00603                 $iIntVal = (int) $aTimeMatches[1];
00604                 if ( $iIntVal < 13) {
00605                     $sTime = ($iIntVal + 12) . ":" . $aTimeMatches[2];
00606                 }
00607             } else {
00608                 $sTime = $aTimeMatches[1] . ":" . $aTimeMatches[2];
00609             }
00610         } elseif ( $oStr->preg_match( "/([0-9]{2}) ([AP]{1}[M]{1})$/", $sTime, $aTimeMatches ) ) {
00611             if ( $aTimeMatches[2] == "PM") {
00612                 $iIntVal = (int) $aTimeMatches[1];
00613                 if ( $iIntVal < 13) {
00614                     $sTime = ($iIntVal + 12);
00615                 }
00616             } else {
00617                 $sTime = $aTimeMatches[1];
00618             }
00619         } else {
00620             $sTime = str_replace(".", ":", $sTime);
00621         }
00622 
00623         return $oConvObject->value . " " . $sTime;
00624     }
00625 
00631     protected function _setListNavigationParams()
00632     {
00633         $myConfig  = $this->getConfig();
00634 
00635         // list navigation
00636         $blShowNavigation = false;
00637         $iAdminListSize = $this->_getViewListSize();
00638         if ( $this->_iListSize > $iAdminListSize ) {
00639             // yes, we need to build the navigation object
00640             $pagenavigation = new oxStdClass();
00641             $pagenavigation->pages    = round( ( ( $this->_iListSize - 1 ) / $iAdminListSize ) + 0.5, 0 );
00642             $pagenavigation->actpage  = ($pagenavigation->actpage > $pagenavigation->pages)? $pagenavigation->pages : round( ( $this->_iCurrListPos / $iAdminListSize ) + 0.5, 0 );
00643             $pagenavigation->lastlink = ( $pagenavigation->pages - 1 ) * $iAdminListSize;
00644             $pagenavigation->nextlink = null;
00645             $pagenavigation->backlink = null;
00646 
00647             $iPos = $this->_iCurrListPos + $iAdminListSize;
00648             if ( $iPos < $this->_iListSize ) {
00649                 $pagenavigation->nextlink = $iPos = $this->_iCurrListPos + $iAdminListSize;
00650             }
00651 
00652             if ( ( $this->_iCurrListPos - $iAdminListSize ) >= 0 ) {
00653                 $pagenavigation->backlink = $iPos = $this->_iCurrListPos - $iAdminListSize;
00654             }
00655 
00656             // calculating list start position
00657             $iStart = $pagenavigation->actpage - 5;
00658             $iStart = ( $iStart <= 0 ) ? 1 : $iStart;
00659 
00660             // calculating list end position
00661             $iEnd = $pagenavigation->actpage + 5;
00662             $iEnd = ( $iEnd < $iStart + 10) ? $iStart + 10 : $iEnd;
00663             $iEnd = ( $iEnd > $pagenavigation->pages ) ? $pagenavigation->pages : $iEnd;
00664 
00665             // once again adjusting start pos ..
00666             $iStart = ( $iEnd - 10 > 0 ) ? $iEnd - 10 : $iStart;
00667             $iStart = ( $pagenavigation->pages <= 11) ? 1 : $iStart;
00668 
00669             // navigation urls
00670             for ( $i = $iStart; $i <= $iEnd; $i++ ) {
00671                 $page = new Oxstdclass();
00672                 $page->selected = 0;
00673                 if ( $i == $pagenavigation->actpage ) {
00674                     $page->selected = 1;
00675                 }
00676                 $pagenavigation->changePage[$i] = $page;
00677             }
00678 
00679             $this->_aViewData['pagenavi'] = $pagenavigation;
00680 
00681             if ( isset( $this->_iOverPos)) {
00682                 $iPos = $this->_iOverPos;
00683                 $this->_iOverPos = null;
00684             } else {
00685                 $iPos = oxConfig::getParameter( 'lstrt' );
00686             }
00687 
00688             if ( !$iPos ) {
00689                 $iPos = 0;
00690             }
00691 
00692             $this->_aViewData['lstrt']    = $iPos;
00693             $this->_aViewData['listsize'] = $this->_iListSize;
00694             $blShowNavigation = true;
00695         }
00696 
00697         // determine not used space in List
00698         $iShowListSize  = $this->_iListSize - $this->_iCurrListPos;
00699         $iAdminListSize = $this->_getViewListSize();
00700         $iNotUsed = $iAdminListSize - min( $iShowListSize, $iAdminListSize );
00701         $iSpace = $iNotUsed * 15;
00702 
00703         if ( !$blShowNavigation ) {
00704             $iSpace += 20;
00705         }
00706 
00707         $this->_aViewData['iListFillsize'] = $iSpace;
00708     }
00709 
00717     protected function _setupNavigation( $sNode )
00718     {
00719         // navigation according to class
00720         if ( $sNode ) {
00721 
00722             $myAdminNavig = $this->getNavigation();
00723 
00724             $sOxId = $this->getEditObjectId();
00725 
00726             if ( $sOxId == -1) {
00727                 //on first call or when pressed creating new item button, reseting active tab
00728                 $iActTab = $this->_iDefEdit;
00729             } else {
00730                 // active tab
00731                 $iActTab = oxConfig::getParameter( 'actedit' );
00732                 $iActTab = $iActTab ? $iActTab : $this->_iDefEdit;
00733             }
00734 
00735             // tabs
00736             $this->_aViewData['editnavi'] = $myAdminNavig->getTabs( $sNode, $iActTab );
00737 
00738             // active tab
00739             $this->_aViewData['actlocation'] = $myAdminNavig->getActiveTab( $sNode, $iActTab );
00740 
00741             // default tab
00742             $this->_aViewData['default_edit'] = $myAdminNavig->getActiveTab( $sNode, $this->_iDefEdit );
00743 
00744             // passign active tab number
00745             $this->_aViewData['actedit'] = $iActTab;
00746         }
00747     }
00748 
00754     public function getItemList()
00755     {
00756         if ( $this->_oList === null && $this->_sListClass ) {
00757 
00758             $this->_oList = oxNew( $this->_sListType );
00759             $this->_oList->clear();
00760             $this->_oList->init( $this->_sListClass );
00761 
00762             $aWhere = $this->buildWhere();
00763 
00764             $oListObject = $this->_oList->getBaseObject();
00765 
00766             oxSession::setVar( 'tabelle', $this->_sListClass );
00767             $this->_aViewData['listTable'] = getViewName( $oListObject->getCoreTableName() );
00768             $this->getConfig()->setGlobalParameter( 'ListCoreTable', $oListObject->getCoreTableName() );
00769 
00770             if ( $oListObject->isMultilang() ) {
00771                 // is the object multilingual?
00772                 $oListObject->setLanguage( oxLang::getInstance()->getBaseLanguage() );
00773 
00774                 if ( isset( $this->_blEmployMultilanguage ) ) {
00775                     $oListObject->setEnableMultilang( $this->_blEmployMultilanguage );
00776                 }
00777             }
00778 
00779             $sSql = $this->_buildSelectString( $oListObject );
00780             $sSql = $this->_prepareWhereQuery( $aWhere, $sSql );
00781             $sSql = $this->_prepareOrderByQuery( $sSql );
00782             $sSql = $this->_changeselect( $sSql );
00783 
00784             // calculates count of list items
00785             $this->_calcListItemsCount( $sSql );
00786 
00787             // setting current list position (page)
00788             $this->_setCurrentListPosition( oxConfig::getParameter( 'jumppage' ) );
00789 
00790             // settting additioan params for list: current list size
00791             $this->_oList->setSqlLimit( $this->_iCurrListPos, $this->_getViewListSize() );
00792 
00793             $this->_oList->selectString( $sSql );
00794         }
00795 
00796         return $this->_oList;
00797     }
00798 
00804     public function clearItemList()
00805     {
00806         $this->_oList = null;
00807     }
00808 
00814     public function getItemListBaseObject()
00815     {
00816         if ( ( $oList = $this->getItemList() ) ) {
00817            return $oList->getBaseObject();
00818         }
00819     }
00820 }