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/i', 'select count(*) from ', $sSql );
00242 
00243         // removing order by
00244         $sSql = $oStr->preg_replace( '/order by .*$/i', '', $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         oxRegistry::getSession()->setVariable( '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() : oxRegistry::getLang()->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 (contains % sign at beginning 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 = oxRegistry::get("oxUtilsString");
00410             while ( list($sFieldName, $sFieldValue) = each( $aWhere ) ) {
00411                 $sFieldValue = trim( $sFieldValue );
00412 
00413                 //check if this is search string (contains % sign at beginning 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 action
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                         $sqlFull .= " {$sSqlBoolAction} {$sFieldName} ";
00435 
00436                         //for search in same field for different values using AND
00437                         $sSqlBoolAction = ' and ';
00438 
00439                         $sqlFull .= $this->_buildFilter( $sVal, $blIsSearchValue );
00440 
00441                         if ( $sUml ) {
00442                             $sqlFull .= " or {$sFieldName} ";
00443 
00444                             $sqlFull .= $this->_buildFilter( $sUml, $blIsSearchValue );
00445                             $sqlFull .= ')'; // end of OR section
00446                         }
00447                     }
00448 
00449                     // end for AND action
00450                     $sqlFull .= ' ) ';
00451                 }
00452             }
00453         }
00454 
00455         return $sqlFull;
00456     }
00457 
00465     protected function _changeselect( $sSql )
00466     {
00467         return $sSql;
00468     }
00469 
00470 
00476     public function buildWhere()
00477     {
00478         if ( $this->_aWhere === null && ( $oList = $this->getItemList() ) ) {
00479 
00480             $this->_aWhere = array();
00481             $aFilter = $this->getListFilter();
00482             if ( is_array( $aFilter ) ) {
00483 
00484                 $oListItem = $this->getItemListBaseObject();
00485                 $iLangId = $oListItem->isMultilang() ? $oListItem->getLanguage() : oxRegistry::getLang()->getBaseLanguage();
00486                 $sLocalDateFormat = $this->getConfig()->getConfigParam( 'sLocalDateFormat' );
00487 
00488                 foreach ( $aFilter as $sTable => $aFilterData ) {
00489                     foreach ( $aFilterData as $sName => $sValue ) {
00490                         if ( $sValue || '0' === ( string ) $sValue ) {
00491 
00492                             $sField = "{$sTable}__{$sName}";
00493 
00494                             // if no table name attached to field name, add it
00495                             $sName = $sTable ? getViewName( $sTable, $iLangId ) . ".{$sName}" : $sName;
00496 
00497                             // #M1260: if field is date
00498                             if ( $sLocalDateFormat && $sLocalDateFormat != 'ISO' && isset( $oListItem->$sField ) ) {
00499                                 $sFldType = $oListItem->{$sField}->fldtype;
00500                                 if ( "datetime" == $sFldType || "date" == $sFldType ) {
00501                                     $sValue = $this->_convertToDBDate( $sValue, $sFldType );
00502                                 }
00503                             }
00504 
00505                             $this->_aWhere[$sName] = "%{$sValue}%";
00506                         }
00507                     }
00508                 }
00509             }
00510         }
00511 
00512         return $this->_aWhere;
00513     }
00514 
00523     protected function _convertToDBDate( $sValue, $sFldType )
00524     {
00525         $oConvObject = new oxField();
00526         $oConvObject->setValue($sValue);
00527         if ( $sFldType == "datetime" ) {
00528             if ( strlen($sValue) == 10 || strlen($sValue) == 22 || ( strlen($sValue) == 19 && !stripos( $sValue, "m" ) ) ) {
00529                 oxRegistry::get("oxUtilsDate")->convertDBDateTime( $oConvObject, true );
00530             } else {
00531                 if ( strlen($sValue) > 10 ) {
00532                     return $this->_convertTime( $sValue );
00533                 } else {
00534                     return $this->_convertDate( $sValue );
00535                 }
00536             }
00537         } elseif ( $sFldType == "date" ) {
00538             if ( strlen($sValue) == 10 ) {
00539                 oxRegistry::get("oxUtilsDate")->convertDBDate( $oConvObject, true);
00540             } else {
00541                 return $this->_convertDate( $sValue );
00542             }
00543         }
00544         return $oConvObject->value;
00545     }
00546 
00554     protected function _convertDate( $sDate )
00555     {
00556         // regexps to validate input
00557         $aDatePatterns = array("/^([0-9]{2})\.([0-9]{4})/" => "EUR2",    // MM.YYYY
00558                                "/^([0-9]{2})\.([0-9]{2})/" => "EUR1",    // DD.MM
00559                                "/^([0-9]{2})\/([0-9]{4})/" => "USA2",    // MM.YYYY
00560                                "/^([0-9]{2})\/([0-9]{2})/" => "USA1"     // DD.MM
00561                               );
00562 
00563         // date/time formatting rules
00564         $aDFormats  = array("EUR1" => array(2, 1),
00565                             "EUR2" => array(2, 1),
00566                             "USA1" => array(1, 2),
00567                             "USA2" => array(2, 1)
00568                            );
00569 
00570         // looking for date field
00571         $aDateMatches = array();
00572         $oStr = getStr();
00573         foreach ( $aDatePatterns as $sPattern => $sType) {
00574             if ( $oStr->preg_match( $sPattern, $sDate, $aDateMatches)) {
00575                 $sDate = $aDateMatches[$aDFormats[$sType][0]] . "-" . $aDateMatches[$aDFormats[$sType][1]];
00576                 break;
00577             }
00578         }
00579 
00580         return $sDate;
00581     }
00582 
00590     protected function _convertTime( $sFullDate )
00591     {
00592         $sDate = substr( $sFullDate, 0, 10 );
00593         $oConvObject = new oxField();
00594         $oConvObject->setValue($sDate);
00595         oxRegistry::get("oxUtilsDate")->convertDBDate( $oConvObject, true);
00596         $oStr = getStr();
00597 
00598         // looking for time field
00599         $sTime = substr( $sFullDate, 11);
00600         if ( $oStr->preg_match( "/([0-9]{2}):([0-9]{2}) ([AP]{1}[M]{1})$/", $sTime, $aTimeMatches ) ) {
00601             if ( $aTimeMatches[3] == "PM") {
00602                 $iIntVal = (int) $aTimeMatches[1];
00603                 if ( $iIntVal < 13) {
00604                     $sTime = ($iIntVal + 12) . ":" . $aTimeMatches[2];
00605                 }
00606             } else {
00607                 $sTime = $aTimeMatches[1] . ":" . $aTimeMatches[2];
00608             }
00609         } elseif ( $oStr->preg_match( "/([0-9]{2}) ([AP]{1}[M]{1})$/", $sTime, $aTimeMatches ) ) {
00610             if ( $aTimeMatches[2] == "PM") {
00611                 $iIntVal = (int) $aTimeMatches[1];
00612                 if ( $iIntVal < 13) {
00613                     $sTime = ($iIntVal + 12);
00614                 }
00615             } else {
00616                 $sTime = $aTimeMatches[1];
00617             }
00618         } else {
00619             $sTime = str_replace(".", ":", $sTime);
00620         }
00621 
00622         return $oConvObject->value . " " . $sTime;
00623     }
00624 
00630     protected function _setListNavigationParams()
00631     {
00632         $myConfig  = $this->getConfig();
00633 
00634         // list navigation
00635         $blShowNavigation = false;
00636         $iAdminListSize = $this->_getViewListSize();
00637         if ( $this->_iListSize > $iAdminListSize ) {
00638             // yes, we need to build the navigation object
00639             $pageNavigation = new stdClass();
00640             $pageNavigation->pages    = round( ( ( $this->_iListSize - 1 ) / $iAdminListSize ) + 0.5, 0 );
00641             $pageNavigation->actpage  = ($pageNavigation->actpage > $pageNavigation->pages)? $pageNavigation->pages : round( ( $this->_iCurrListPos / $iAdminListSize ) + 0.5, 0 );
00642             $pageNavigation->lastlink = ( $pageNavigation->pages - 1 ) * $iAdminListSize;
00643             $pageNavigation->nextlink = null;
00644             $pageNavigation->backlink = null;
00645 
00646             $iPos = $this->_iCurrListPos + $iAdminListSize;
00647             if ( $iPos < $this->_iListSize ) {
00648                 $pageNavigation->nextlink = $iPos = $this->_iCurrListPos + $iAdminListSize;
00649             }
00650 
00651             if ( ( $this->_iCurrListPos - $iAdminListSize ) >= 0 ) {
00652                 $pageNavigation->backlink = $iPos = $this->_iCurrListPos - $iAdminListSize;
00653             }
00654 
00655             // calculating list start position
00656             $iStart = $pageNavigation->actpage - 5;
00657             $iStart = ( $iStart <= 0 ) ? 1 : $iStart;
00658 
00659             // calculating list end position
00660             $iEnd = $pageNavigation->actpage + 5;
00661             $iEnd = ( $iEnd < $iStart + 10) ? $iStart + 10 : $iEnd;
00662             $iEnd = ( $iEnd > $pageNavigation->pages ) ? $pageNavigation->pages : $iEnd;
00663 
00664             // once again adjusting start pos ..
00665             $iStart = ( $iEnd - 10 > 0 ) ? $iEnd - 10 : $iStart;
00666             $iStart = ( $pageNavigation->pages <= 11) ? 1 : $iStart;
00667 
00668             // navigation urls
00669             for ( $i = $iStart; $i <= $iEnd; $i++ ) {
00670                 $page = new stdclass();
00671                 $page->selected = 0;
00672                 if ( $i == $pageNavigation->actpage ) {
00673                     $page->selected = 1;
00674                 }
00675                 $pageNavigation->changePage[$i] = $page;
00676             }
00677 
00678             $this->_aViewData['pagenavi'] = $pageNavigation;
00679 
00680             if ( isset( $this->_iOverPos)) {
00681                 $iPos = $this->_iOverPos;
00682                 $this->_iOverPos = null;
00683             } else {
00684                 $iPos = oxConfig::getParameter( 'lstrt' );
00685             }
00686 
00687             if ( !$iPos ) {
00688                 $iPos = 0;
00689             }
00690 
00691             $this->_aViewData['lstrt']    = $iPos;
00692             $this->_aViewData['listsize'] = $this->_iListSize;
00693             $blShowNavigation = true;
00694         }
00695 
00696         // determine not used space in List
00697         $iShowListSize  = $this->_iListSize - $this->_iCurrListPos;
00698         $iAdminListSize = $this->_getViewListSize();
00699         $iNotUsed = $iAdminListSize - min( $iShowListSize, $iAdminListSize );
00700         $iSpace = $iNotUsed * 15;
00701 
00702         if ( !$blShowNavigation ) {
00703             $iSpace += 20;
00704         }
00705 
00706         $this->_aViewData['iListFillsize'] = $iSpace;
00707     }
00708 
00716     protected function _setupNavigation( $sNode )
00717     {
00718         // navigation according to class
00719         if ( $sNode ) {
00720 
00721             $myAdminNavigation = $this->getNavigation();
00722 
00723             $sOxId = $this->getEditObjectId();
00724 
00725             if ( $sOxId == -1) {
00726                 //on first call or when pressed creating new item button, reseting active tab
00727                 $iActTab = $this->_iDefEdit;
00728             } else {
00729                 // active tab
00730                 $iActTab = oxConfig::getParameter( 'actedit' );
00731                 $iActTab = $iActTab ? $iActTab : $this->_iDefEdit;
00732             }
00733 
00734             // tabs
00735             $this->_aViewData['editnavi'] = $myAdminNavigation->getTabs( $sNode, $iActTab );
00736 
00737             // active tab
00738             $this->_aViewData['actlocation'] = $myAdminNavigation->getActiveTab( $sNode, $iActTab );
00739 
00740             // default tab
00741             $this->_aViewData['default_edit'] = $myAdminNavigation->getActiveTab( $sNode, $this->_iDefEdit );
00742 
00743             // assign active tab number
00744             $this->_aViewData['actedit'] = $iActTab;
00745         }
00746     }
00747 
00753     public function getItemList()
00754     {
00755         if ( $this->_oList === null && $this->_sListClass ) {
00756 
00757             $this->_oList = oxNew( $this->_sListType );
00758             $this->_oList->clear();
00759             $this->_oList->init( $this->_sListClass );
00760 
00761             $aWhere = $this->buildWhere();
00762 
00763             $oListObject = $this->_oList->getBaseObject();
00764 
00765             oxSession::setVar( 'tabelle', $this->_sListClass );
00766             $this->_aViewData['listTable'] = getViewName( $oListObject->getCoreTableName() );
00767             $this->getConfig()->setGlobalParameter( 'ListCoreTable', $oListObject->getCoreTableName() );
00768 
00769             if ( $oListObject->isMultilang() ) {
00770                 // is the object multilingual?
00771                 $oListObject->setLanguage( oxRegistry::getLang()->getBaseLanguage() );
00772 
00773                 if ( isset( $this->_blEmployMultilanguage ) ) {
00774                     $oListObject->setEnableMultilang( $this->_blEmployMultilanguage );
00775                 }
00776             }
00777 
00778             $sSql = $this->_buildSelectString( $oListObject );
00779             $sSql = $this->_prepareWhereQuery( $aWhere, $sSql );
00780             $sSql = $this->_prepareOrderByQuery( $sSql );
00781             $sSql = $this->_changeselect( $sSql );
00782 
00783             // calculates count of list items
00784             $this->_calcListItemsCount( $sSql );
00785 
00786             // setting current list position (page)
00787             $this->_setCurrentListPosition( oxConfig::getParameter( 'jumppage' ) );
00788 
00789             // setting addition params for list: current list size
00790             $this->_oList->setSqlLimit( $this->_iCurrListPos, $this->_getViewListSize() );
00791 
00792             $this->_oList->selectString( $sSql );
00793         }
00794 
00795         return $this->_oList;
00796     }
00797 
00803     public function clearItemList()
00804     {
00805         $this->_oList = null;
00806     }
00807 
00813     public function getItemListBaseObject()
00814     {
00815         if ( ( $oList = $this->getItemList() ) ) {
00816            return $oList->getBaseObject();
00817         }
00818     }
00819 }