oxadminlist.php

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