00001 <?php
00002
00003
00009 class oxCategoryList extends oxList
00010 {
00016 protected $_sObjectsInListName = 'oxcategory';
00017
00023 protected $_blHideEmpty = false;
00024
00030 protected $_blForseFull = false;
00031
00037 protected $_iForseLevel = 1;
00038
00044 protected $_sActCat = null;
00045
00051 protected $_aPath = array();
00052
00060 public function __construct( $sObjectsInListName = 'oxcategory')
00061 {
00062 $this->_blHideEmpty = $this->getConfig()->getConfigParam('blDontShowEmptyCategories');
00063 parent::__construct( $sObjectsInListName );
00064 }
00065
00073 protected function _getSelectString($blReverse = false)
00074 {
00075 $oBaseObject = $this->getBaseObject();
00076 $sViewName = $oBaseObject->getViewName();
00077
00078
00079 $sLangSuffix = oxLang::getInstance()->getLanguageTag();
00080 $sFieldList = "oxid, oxactive$sLangSuffix as oxactive, oxhidden, oxparentid, oxdefsort, oxdefsortmode, oxleft, oxright, oxrootid, oxsort, oxtitle$sLangSuffix as oxtitle, oxpricefrom, oxpriceto, oxicon ";
00081 $sWhere = $this->_getDepthSqlSnippet();
00082
00083 $sOrdDir = $blReverse?'desc':'asc';
00084 $sOrder = "$sViewName.oxrootid $sOrdDir, $sViewName.oxleft $sOrdDir";
00085
00086 $sFieldList.= ",not $sViewName.".$oBaseObject->getSqlFieldName( 'oxactive' )." as remove";
00087
00088
00089 return "select $sFieldList from $sViewName where $sWhere order by $sOrder";
00090 }
00091
00098 protected function _getDepthSqlSnippet()
00099 {
00100 $sDepthSnippet = ' 1 ';
00101
00102
00103 if (!$this->isAdmin() && !$this->_blHideEmpty && !$this->_blForseFull) {
00104 $sViewName = $this->getBaseObject()->getViewName();
00105 $sDepthSnippet = ' ( 0';
00106
00107 $oCat = oxNew('oxcategory');
00108 $blLoaded = $oCat->load($this->_sActCat);
00109
00110 if ($blLoaded && $this->_sActCat && $sActRootID = $oCat->oxcategories__oxrootid->value) {
00111 $sDepthSnippet .= " or ("
00112 ." $sViewName.oxparentid in"
00113 ." (select subcats.oxparentid from $sViewName as subcats"
00114 ." where subcats.oxrootid = '{$oCat->oxcategories__oxrootid->value}'"
00115 ." and subcats.oxleft <= {$oCat->oxcategories__oxleft->value}"
00116 ." and subcats.oxright >= {$oCat->oxcategories__oxright->value})"
00117 ." or ($sViewName.oxparentid = '{$oCat->oxcategories__oxid->value}')"
00118 .")";
00119 } else {
00120 $this->_sActCat = null;
00121 }
00122
00123
00124 if ($this->_iForseLevel >= 1) {
00125 $sDepthSnippet .= " or $sViewName.oxparentid = 'oxrootid'";
00126 }
00127
00128
00129 if ($this->_iForseLevel >= 2) {
00130 $sDepthSnippet .= " or $sViewName.oxrootid = $sViewName.oxparentid or $sViewName.oxid = $sViewName.oxrootid";
00131 }
00132
00133 $sDepthSnippet .= ' ) ';
00134 }
00135 return $sDepthSnippet;
00136 }
00137
00138
00151 public function buildTree($sActCat, $blLoadFullTree, $blPerfLoadTreeForSearch, $blTopNaviLayout)
00152 {
00153 startProfile("buildTree");
00154
00155 $this->_sActCat = $sActCat;
00156 $this->_blForseFull = $blLoadFullTree || $blPerfLoadTreeForSearch;
00157 $this->_iForseLevel = $blTopNaviLayout?2:1;
00158
00159 $sSelect = $this->_getSelectString(true);
00160 $this->selectString($sSelect);
00161
00162
00163 if ( !$this->isAdmin() ) {
00164
00165 $this->_ppRemoveInactiveCategories();
00166
00167
00168 $this->_ppLoadFullCategory($sActCat);
00169
00170
00171 $this->_ppAddPathInfo();
00172
00173
00174 $this->_ppAddContentCategories();
00175
00176
00177 $this->_ppBuildTree();
00178 }
00179
00180 stopProfile("buildTree");
00181 }
00182
00188 protected function _ppLoadFullCategory($sId)
00189 {
00190 $oNewCat = oxNew('oxcategory');
00191 if ($oNewCat->load($sId)) {
00192
00193 $this->_aArray[$sId] = $oNewCat;
00194 }
00195 }
00196
00204 public function buildList($blLoad)
00205 {
00206
00207 if (!$blLoad) {
00208 return;
00209 }
00210
00211 startProfile('buildCategoryList');
00212
00213 $this->_blForseFull = true;
00214 $this->selectString($this->_getSelectString(false));
00215
00216
00217
00218 $this->_ppAddDepthInformation();
00219 stopProfile('buildCategoryList');
00220 }
00221
00229 public function setShopID($sShopID)
00230 {
00231 $this->_sShopID = $sShopID;
00232 }
00233
00239 public function getPath()
00240 {
00241 return $this->_aPath;
00242 }
00243
00249 public function getHtmlPath()
00250 {
00251 $sHtmlCatTree = '';
00252 $sSep = '';
00253 foreach ( $this->_aPath as $oCategory ) {
00254 $sHtmlCatTree .= " $sSep<a href='".$oCategory->getLink()."'>".$oCategory->oxcategories__oxtitle->value."</a>";
00255 $sSep = '/ ';
00256 }
00257 return $sHtmlCatTree;
00258 }
00259
00265 public function getClickCat()
00266 {
00267 if (count($this->_aPath)) {
00268 return end($this->_aPath);
00269 }
00270 }
00271
00277 public function getClickRoot()
00278 {
00279 if (count($this->_aPath)) {
00280 return array(reset($this->_aPath));
00281 }
00282 }
00283
00289 protected function _ppRemoveInactiveCategories()
00290 {
00291
00292 $aRemoveList = array();
00293 foreach ($this->_aArray as $oCat) {
00294 if ($oCat->oxcategories__remove->value) {
00295 if (isset($aRemoveList[$oCat->oxcategories__oxrootid->value])) {
00296 $aRemoveRange = $aRemoveList[$oCat->oxcategories__oxrootid->value];
00297 } else {
00298 $aRemoveRange = array();
00299 }
00300 $aRemoveList[$oCat->oxcategories__oxrootid->value] = array_merge(range($oCat->oxcategories__oxleft->value, $oCat->oxcategories__oxright->value), $aRemoveRange);
00301 }
00302 unset($oCat->oxcategories__remove);
00303 }
00304
00305
00306 foreach ($this->_aArray as $oCat) {
00307 if (isset($aRemoveList[$oCat->oxcategories__oxrootid->value]) && in_array($oCat->oxcategories__oxleft->value, $aRemoveList[$oCat->oxcategories__oxrootid->value])) {
00308 unset( $this->_aArray[$oCat->oxcategories__oxid->value] );
00309 }
00310 }
00311 }
00312
00318 protected function _ppAddPathInfo()
00319 {
00320
00321 if (is_null($this->_sActCat)) {
00322 return;
00323 }
00324
00325 $aPath = array();
00326 $sCurrentCat = $this->_sActCat;
00327
00328 while ($sCurrentCat != 'oxrootid' && isset($this[$sCurrentCat])) {
00329 $oCat = $this[$sCurrentCat];
00330 $oCat->setExpanded(true);
00331 $aPath[$sCurrentCat] = $oCat;
00332 $sCurrentCat = $oCat->oxcategories__oxparentid->value;
00333 }
00334
00335 $this->_aPath = array_reverse($aPath);
00336 }
00337
00343 protected function _ppAddContentCategories()
00344 {
00345
00346 $oContentList = oxNew( "oxcontentlist" );
00347 $oContentList->loadCatMenues();
00348
00349 foreach ($oContentList as $sCatId => $aContent) {
00350 if (array_key_exists($sCatId, $this->_aArray)) {
00351 $this[$sCatId]->setContentCats($aContent);
00352
00353 }
00354 }
00355 }
00356
00362 protected function _ppBuildTree()
00363 {
00364 startProfile("_sortCats");
00365 $aIds = $this->sortCats();
00366 stopProfile("_sortCats");
00367 $aTree = array();
00368 foreach ($this->_aArray as $oCat) {
00369 $sParentId = $oCat->oxcategories__oxparentid->value;
00370 if ( $sParentId != 'oxrootid') {
00371 $this->_aArray[$sParentId]->setSortingIds( $aIds );
00372 $this->_aArray[$sParentId]->setSubCat($oCat, $oCat->getId());
00373 } else {
00374 $aTree[$oCat->getId()] = $oCat;
00375 }
00376 }
00377
00378
00379 $oCategory = oxNew('oxcategory');
00380 $oCategory->setSortingIds( $aIds );
00381 uasort($aTree, array( $oCategory, 'cmpCat' ) );
00382
00383 $this->assign($aTree);
00384 }
00385
00391 public function sortCats()
00392 {
00393 $oDB = oxDb::getDb();
00394 $sViewName = getViewName('oxcategories');
00395 $sSortSql = "SELECT oxid FROM $sViewName ORDER BY oxparentid, oxsort, oxtitle";
00396 $aIds = array();
00397 $rs = $oDB->execute($sSortSql);
00398 $cnt = 0;
00399 if ($rs != false && $rs->recordCount() > 0) {
00400 while (!$rs->EOF) {
00401 $aIds[$rs->fields[0]] = $cnt;
00402 $cnt++;
00403 $rs->moveNext();
00404 }
00405 }
00406
00407 return $aIds;
00408 }
00409
00416 protected function _ppAddDepthInformation()
00417 {
00418
00419 $aStack = array();
00420 $iDepth = 0;
00421 $sPrevParent = '';
00422
00423 foreach ($this->_aArray as $oCat) {
00424
00425 $sParentId = $oCat->oxcategories__oxparentid->value;
00426 if ( $oCat->oxcategories__oxparentid->value == 'oxrootid' ) {
00427 $iDepth = 1;
00428 $aStack = array($sParentId => '0');
00429 }
00430
00431 if ($sPrevParent != $sParentId && isset($aStack[$sParentId]) ) {
00432 $iDepth -= count($aStack)- $aStack[$sParentId];
00433 $aStack = array_slice($aStack, 0, $iDepth-1, true);
00434 }
00435
00436 if ( !isset($aStack[$sParentId])) {
00437 $aStack[$sParentId] = $iDepth;
00438 $iDepth++;
00439 }
00440
00441 $oCat->oxcategories__oxtitle->setValue(str_repeat('-', $iDepth-1).' '.$oCat->oxcategories__oxtitle->value);
00442 $sPrevParent = $sParentId;
00443 }
00444
00445 }
00446
00455 public function updateCategoryTree($blVerbose = true, $sShopID = null)
00456 {
00457 $oDB = oxDb::getDb();
00458 $sWhere = '1';
00459
00460
00461 $oDB->execute("update oxcategories set oxleft = 0, oxright = 0 where $sWhere");
00462 $oDB->execute("update oxcategories set oxleft = 1, oxright = 2 where oxparentid = 'oxrootid' and $sWhere");
00463
00464
00465 $rs = $oDB->execute("select oxid, oxtitle from oxcategories where oxparentid = 'oxrootid' and $sWhere order by oxsort");
00466 if ($rs != false && $rs->recordCount() > 0) {
00467 while (!$rs->EOF) {
00468 if ($blVerbose) {
00469 echo( "<b>Processing : ".$rs->fields[1]."</b>(".$rs->fields[0].")<br>");
00470 }
00471 $oxRootId = $rs->fields[0];
00472
00473 $updn = $this->_updateNodes($oxRootId, true, $oxRootId);
00474 $rs->moveNext();
00475 }
00476 }
00477 }
00478
00488 protected function _updateNodes($oxRootId, $isroot, $thisRoot)
00489 {
00490 $oDB = oxDb::getDb();
00491
00492 if ($isroot) {
00493 $thisRoot = $oxRootId;
00494 }
00495
00496
00497 $rs = $oDB->execute("update oxcategories set oxrootid = '$thisRoot' where oxparentid = '$oxRootId'");
00498 $rs = $oDB->execute("select oxid, oxparentid from oxcategories where oxparentid = '$oxRootId' order by oxsort");
00499
00500 if ($rs != false && $rs->recordCount() > 0) {
00501 while (!$rs->EOF) {
00502 $parentId = $rs->fields[1];
00503 $actOxid = $rs->fields[0];
00504
00505
00506 $rs3 = $oDB->execute("select oxrootid, oxright from oxcategories where oxid = '$parentId'");
00507 while (!$rs3->EOF) {
00508 $parentOxRootId = $rs3->fields[0];
00509 $parentRight = $rs3->fields[1];
00510 $rs3->moveNext();
00511 }
00512 $oDB->execute("update oxcategories set oxleft = oxleft + 2 where oxrootid = '$parentOxRootId' and oxleft > '$parentRight' and oxright >= '$parentRight' and oxid != '$actOxid'");
00513 $oDB->execute("update oxcategories set oxright = oxright + 2 where oxrootid = '$parentOxRootId' and oxright >= '$parentRight' and oxid != '$actOxid'");
00514 $oDB->execute("update oxcategories set oxleft = $parentRight, oxright = ($parentRight + 1) where oxid = '$actOxid'");
00515 $this->_updateNodes($actOxid, false, $thisRoot);
00516 $rs->moveNext();
00517 }
00518 }
00519 }
00520
00528 public function __get($sName)
00529 {
00530 switch ($sName) {
00531 case 'aPath':
00532 case 'aFullPath':
00533 return $this->getPath();
00534 }
00535 return parent::__get($sName);
00536 }
00537
00538 }