oxcategorylist.php

Go to the documentation of this file.
00001 <?php
00002 
00003 
00009 class oxCategoryList extends oxList
00010 {
00016     protected $_sObjectsInListName = 'oxcategory';
00017 
00023     protected $_blHideEmpty = false;
00024 
00030     protected $_blForceFull = false;
00031 
00037     protected $_iForceLevel = 1;
00038 
00044     protected $_sActCat     = null;
00045 
00051     protected $_aPath = array();
00052 
00058     protected $_aUpdateInfo = array();
00059 
00067     public function __construct( $sObjectsInListName = 'oxcategory')
00068     {
00069         $this->_blHideEmpty = $this->getConfig()->getConfigParam('blDontShowEmptyCategories');
00070         parent::__construct( $sObjectsInListName );
00071     }
00072 
00081     protected function _getSqlSelectFieldsForTree($sTable, $aColumns = null)
00082     {
00083         if ($aColumns && count($aColumns)) {
00084             foreach ($aColumns as $key=>$val) {
00085                 $aColumns[$key].=' as '.$val;
00086             }
00087             return "$sTable.".implode(", $sTable.", $aColumns);
00088         }
00089 
00090         $oBaseObject = $this->getBaseObject();
00091         $sLangSuffix = oxLang::getInstance()->getLanguageTag();
00092 
00093         $sFieldList = "$sTable.oxid as oxid, $sTable.oxactive$sLangSuffix as oxactive,"
00094                     ." $sTable.oxhidden as oxhidden, $sTable.oxparentid as oxparentid,"
00095                     ." $sTable.oxdefsort as oxdefsort, $sTable.oxdefsortmode as oxdefsortmode,"
00096                     ." $sTable.oxleft as oxleft, $sTable.oxright as oxright,"
00097                     ." $sTable.oxrootid as oxrootid, $sTable.oxsort as oxsort,"
00098                     ." $sTable.oxtitle$sLangSuffix as oxtitle, $sTable.oxdesc$sLangSuffix as oxdesc,"
00099                     ." $sTable.oxpricefrom as oxpricefrom, $sTable.oxpriceto as oxpriceto,"
00100                     ." $sTable.oxicon as oxicon, $sTable.oxextlink as oxextlink ";
00101 
00102             $sFieldList.= ",not $sTable.".$oBaseObject->getSqlFieldName( 'oxactive' )." as oxppremove";
00103 
00104 
00105         return $sFieldList;
00106     }
00107 
00117     protected function _getSelectString($blReverse = false, $aColumns = null, $sOrder = null)
00118     {
00119         $sViewName  = $this->getBaseObject()->getViewName();
00120         $sFieldList = $this->_getSqlSelectFieldsForTree($sViewName, $aColumns);
00121 
00122         //excluding long desc
00123         if (!$this->isAdmin() && !$this->_blHideEmpty && !$this->_blForceFull) {
00124             $oCat = oxNew('oxcategory');
00125             if (!($this->_sActCat && $oCat->load($this->_sActCat) && $oCat->oxcategories__oxrootid->value)) {
00126                 $oCat = null;
00127                 $this->_sActCat = null;
00128             }
00129             $sUnion = $this->_getDepthSqlUnion($oCat, $aColumns);
00130             $sWhere = $this->_getDepthSqlSnippet($oCat);
00131         } else {
00132             $sUnion = '';
00133             $sWhere = '1';
00134         }
00135 
00136         if (!$sOrder) {
00137             $sOrdDir    = $blReverse?'desc':'asc';
00138             $sOrder     = "oxrootid $sOrdDir, oxleft $sOrdDir";
00139         }
00140 
00141         return "select $sFieldList from $sViewName where $sWhere $sUnion order by $sOrder";
00142     }
00143 
00153     protected function _getDepthSqlSnippet($oCat)
00154     {
00155         $sViewName  = $this->getBaseObject()->getViewName();
00156         $sDepthSnippet = ' ( 0';
00157 
00158         // load compleate tree of active category, if it exists
00159         if ($oCat) {
00160             // select children here, siblings will be selected from union
00161             $sDepthSnippet .= " or ($sViewName.oxparentid = ".oxDb::getDb()->quote($oCat->oxcategories__oxid->value).")";
00162         }
00163 
00164         // load 1'st category level (roots)
00165         if ($this->_iForceLevel >= 1) {
00166             $sDepthSnippet .= " or $sViewName.oxparentid = 'oxrootid'";
00167         }
00168 
00169         // load 2'nd category level ()
00170         if ($this->_iForceLevel >= 2) {
00171             $sDepthSnippet .= " or $sViewName.oxrootid = $sViewName.oxparentid or $sViewName.oxid = $sViewName.oxrootid";
00172         }
00173 
00174         $sDepthSnippet .= ' ) ';
00175         return $sDepthSnippet;
00176     }
00177 
00188     protected function _getDepthSqlUnion($oCat, $aColumns = null)
00189     {
00190         if (!$oCat) {
00191             return '';
00192         }
00193 
00194         $sViewName = $this->getBaseObject()->getViewName();
00195 
00196         return "UNION SELECT ".$this->_getSqlSelectFieldsForTree('maincats', $aColumns)
00197                 ." FROM oxcategories AS subcats"
00198                 ." LEFT JOIN $sViewName AS maincats on maincats.oxparentid = subcats.oxparentid"
00199                 ." WHERE subcats.oxrootid = ".oxDb::getDb()->quote($oCat->oxcategories__oxrootid->value)
00200                 ." AND subcats.oxleft <= ". (int)$oCat->oxcategories__oxleft->value
00201                 ." AND subcats.oxright >= ".(int)$oCat->oxcategories__oxright->value;
00202     }
00203 
00204 
00217     public function buildTree($sActCat, $blLoadFullTree, $blPerfLoadTreeForSearch, $blTopNaviLayout)
00218     {
00219         startProfile("buildTree");
00220 
00221         $this->_sActCat     = $sActCat;
00222         $this->_blForceFull = $blLoadFullTree || $blPerfLoadTreeForSearch;
00223         $this->_iForceLevel = $blTopNaviLayout?2:1;
00224 
00225         $sSelect = $this->_getSelectString(true);
00226         $this->selectString($sSelect);
00227 
00228         // PostProcessing
00229         if ( !$this->isAdmin() ) {
00230             // remove inactive categories
00231             $this->_ppRemoveInactiveCategories();
00232 
00233             // add active cat as full object
00234             $this->_ppLoadFullCategory($sActCat);
00235 
00236             // builds navigation path
00237             $this->_ppAddPathInfo();
00238 
00239             // add content categories
00240             $this->_ppAddContentCategories();
00241 
00242             // build tree structure
00243             $this->_ppBuildTree();
00244         }
00245 
00246         stopProfile("buildTree");
00247     }
00248 
00256     protected function _ppLoadFullCategory( $sId )
00257     {
00258         if ( isset($this->_aArray[$sId])) {
00259             $oNewCat = oxNew('oxcategory');
00260             if ( $oNewCat->load($sId)) {
00261                 // replace aArray object with fully loaded category
00262                 $this->_aArray[$sId] = $oNewCat;
00263             }
00264         } else {
00265             $this->_sActCat = null;
00266         }
00267     }
00268 
00276     public function buildList($blLoad)
00277     {
00278 
00279         if (!$blLoad) {
00280             return;
00281         }
00282 
00283         startProfile('buildCategoryList');
00284 
00285         $this->_blForceFull = true;
00286         $this->selectString($this->_getSelectString(false));
00287 
00288         // PostProcessing
00289         // add tree depth info
00290         $this->_ppAddDepthInformation();
00291         stopProfile('buildCategoryList');
00292     }
00293 
00301     public function setShopID($sShopID)
00302     {
00303         $this->_sShopID = $sShopID;
00304     }
00305 
00311     public function getPath()
00312     {
00313         return $this->_aPath;
00314     }
00315 
00321     public function getHtmlPath()
00322     {
00323         $sHtmlCatTree = '';
00324         $sSep         = '';
00325         foreach ( $this->_aPath as $oCategory ) {
00326             $sHtmlCatTree .= " $sSep<a href='".$oCategory->getLink()."'>".$oCategory->oxcategories__oxtitle->value."</a>";
00327             $sSep = '/ ';
00328         }
00329         return $sHtmlCatTree;
00330     }
00331 
00337     public function getClickCat()
00338     {
00339         if (count($this->_aPath)) {
00340             return end($this->_aPath);
00341         }
00342     }
00343 
00349     public function getClickRoot()
00350     {
00351         if (count($this->_aPath)) {
00352             return array(reset($this->_aPath));
00353         }
00354     }
00355 
00361     protected function _ppRemoveInactiveCategories()
00362     {
00363         // Colect all items whitch must be remove
00364         $aRemoveList = array();
00365         foreach ($this->_aArray as $oCat) {
00366             if ($oCat->oxcategories__oxppremove->value) {
00367                 if (isset($aRemoveList[$oCat->oxcategories__oxrootid->value])) {
00368                     $aRemoveRange = $aRemoveList[$oCat->oxcategories__oxrootid->value];
00369                 } else {
00370                     $aRemoveRange = array();
00371                 }
00372                 $aRemoveList[$oCat->oxcategories__oxrootid->value] = array_merge(range($oCat->oxcategories__oxleft->value, $oCat->oxcategories__oxright->value), $aRemoveRange);
00373             }
00374             unset($oCat->oxcategories__oxppremove);
00375         }
00376 
00377         // Remove colected items from list.
00378         foreach ($this->_aArray as $oCat) {
00379             if (isset($aRemoveList[$oCat->oxcategories__oxrootid->value]) && in_array($oCat->oxcategories__oxleft->value, $aRemoveList[$oCat->oxcategories__oxrootid->value])) {
00380                 unset( $this->_aArray[$oCat->oxcategories__oxid->value] );
00381             }
00382         }
00383     }
00384 
00390     protected function _ppAddPathInfo()
00391     {
00392 
00393         if (is_null($this->_sActCat)) {
00394             return;
00395         }
00396 
00397         $aPath = array();
00398         $sCurrentCat  = $this->_sActCat;
00399 
00400         while ($sCurrentCat != 'oxrootid' && isset($this[$sCurrentCat])) {
00401             $oCat = $this[$sCurrentCat];
00402             $oCat->setExpanded(true);
00403             $aPath[$sCurrentCat] = $oCat;
00404             $sCurrentCat = $oCat->oxcategories__oxparentid->value;
00405         }
00406 
00407         $this->_aPath = array_reverse($aPath);
00408     }
00409 
00415     protected function _ppAddContentCategories()
00416     {
00417         // load content pages for adding them into menue tree
00418         $oContentList = oxNew( "oxcontentlist" );
00419         $oContentList->loadCatMenues();
00420 
00421         foreach ($oContentList as $sCatId => $aContent) {
00422             if (array_key_exists($sCatId, $this->_aArray)) {
00423                 $this[$sCatId]->setContentCats($aContent);
00424 
00425             }
00426         }
00427     }
00428 
00434     protected function _ppBuildTree()
00435     {
00436         startProfile("_sortCats");
00437         $aIds = $this->sortCats();
00438         stopProfile("_sortCats");
00439         $aTree = array();
00440         foreach ($this->_aArray as $oCat) {
00441             $sParentId = $oCat->oxcategories__oxparentid->value;
00442             if ( $sParentId != 'oxrootid') {
00443                 if (isset($this->_aArray[$sParentId])) {
00444                     $this->_aArray[$sParentId]->setSortingIds( $aIds );
00445                     $this->_aArray[$sParentId]->setSubCat($oCat, $oCat->getId(), true);
00446                 }
00447             } else {
00448                 $aTree[$oCat->getId()] = $oCat;
00449             }
00450         }
00451 
00452         // run sorting only once on each parent
00453         $aParents = array();
00454         foreach ($this->_aArray as $oCat) {
00455             $aParents[$oCat->oxcategories__oxparentid->value] = true;
00456         }
00457         if ($aParents['oxrootid']) {
00458             unset($aParents['oxrootid']);
00459         }
00460         foreach (array_keys($aParents) as $sParent) {
00461             if (isset($this->_aArray[$sParent])) {
00462                 $this->_aArray[$sParent]->sortSubCats();
00463             }
00464         }
00465 
00466         // Sort root categories
00467         $oCategory = oxNew('oxcategory');
00468         $oCategory->setSortingIds( $aIds );
00469         uasort($aTree, array( $oCategory, 'cmpCat' ) );
00470 
00471         $this->assign($aTree);
00472     }
00473 
00479     public function sortCats()
00480     {
00481         $sViewName  = getViewName('oxcategories');
00482         $sSortSql = $this->_getSelectString(false, array('oxid', 'oxparentid', 'oxsort', 'oxtitle'), 'oxparentid, oxsort, oxtitle');
00483         $aIds = array();
00484         $oDB = oxDb::getDb(true);
00485         $rs = $oDB->execute($sSortSql);
00486         $cnt = 0;
00487         if ($rs != false && $rs->recordCount() > 0) {
00488             while (!$rs->EOF) {
00489                 $aIds[$rs->fields['oxid']] = $cnt;
00490                 $cnt++;
00491                 $rs->moveNext();
00492             }
00493         }
00494         return $aIds;
00495     }
00496 
00503     protected function _ppAddDepthInformation()
00504     {
00505 
00506         $aStack = array();
00507         $iDepth = 0;
00508         $sPrevParent = '';
00509 
00510         foreach ($this->_aArray as $oCat) {
00511 
00512             $sParentId = $oCat->oxcategories__oxparentid->value;
00513             if ( $oCat->oxcategories__oxparentid->value == 'oxrootid' ) {
00514                 $iDepth = 1;
00515                 $aStack = array($sParentId => '0');
00516             }
00517 
00518             if ($sPrevParent != $sParentId && isset($aStack[$sParentId]) ) {
00519                 $iDepth -= count($aStack)- $aStack[$sParentId];
00520                 $aStack = array_slice($aStack, 0, $iDepth-1, true);
00521             }
00522 
00523             if ( !isset($aStack[$sParentId])) {
00524                 $aStack[$sParentId] = $iDepth;
00525                 $iDepth++;
00526             }
00527 
00528             $oCat->oxcategories__oxtitle->setValue(str_repeat('-', $iDepth-1).' '.$oCat->oxcategories__oxtitle->value);
00529             $sPrevParent = $sParentId;
00530         }
00531 
00532     }
00533 
00542     public function updateCategoryTree($blVerbose = true, $sShopID = null)
00543     {
00544         $oDB = oxDb::getDb();
00545         $sWhere = '1';
00546 
00547 
00548         $oDB->execute("update oxcategories set oxleft = 0, oxright = 0 where $sWhere");
00549         $oDB->execute("update oxcategories set oxleft = 1, oxright = 2 where oxparentid = 'oxrootid' and $sWhere");
00550 
00551         // Get all root categories
00552         $rs = $oDB->execute("select oxid, oxtitle from oxcategories where oxparentid = 'oxrootid' and $sWhere order by oxsort");
00553         if ($rs != false && $rs->recordCount() > 0) {
00554             while (!$rs->EOF) {
00555                 $this->_aUpdateInfo[] = "<b>Processing : ".$rs->fields[1]."</b>(".$rs->fields[0].")<br>";
00556                 if ( $blVerbose ) {
00557                     echo next( $this->_aUpdateInfo );
00558                 }
00559                 $oxRootId = $rs->fields[0];
00560 
00561                 $updn = $this->_updateNodes($oxRootId, true, $oxRootId);
00562                 $rs->moveNext();
00563             }
00564         }
00565     }
00566 
00572     public function getUpdateInfo()
00573     {
00574         return $this->_aUpdateInfo;
00575     }
00576 
00586     protected function _updateNodes($oxRootId, $isroot, $thisRoot)
00587     {
00588         $oDB = oxDb::getDb();
00589 
00590         if ($isroot) {
00591             $thisRoot = $oxRootId;
00592         }
00593 
00594         // Get sub categories of root categorie
00595         $rs = $oDB->execute("update oxcategories set oxrootid = ".$oDB->quote($thisRoot)." where oxparentid = ".$oDB->quote($oxRootId));
00596         $rs = $oDB->execute("select oxid, oxparentid from oxcategories where oxparentid = ".$oDB->quote($oxRootId)." order by oxsort");
00597         // If there are sub categories
00598         if ($rs != false && $rs->recordCount() > 0) {
00599             while (!$rs->EOF) {
00600                 $parentId = $rs->fields[1];
00601                 $actOxid = $rs->fields[0];
00602                 $sActOxidQuoted = $oDB->quote($actOxid);
00603 
00604                 // Get the data of the parent category to the current Cat
00605                 $rs3 = $oDB->execute("select oxrootid, oxright from oxcategories where oxid = ".$oDB->quote($parentId));
00606                 while (!$rs3->EOF) {
00607                     $parentOxRootId = $rs3->fields[0];
00608                     $parentRight    = (int)$rs3->fields[1];
00609                     $rs3->moveNext();
00610                 }
00611                 $sParentOxRootIdQuoted = $oDB->quote($parentOxRootId);
00612                 $oDB->execute("update oxcategories set oxleft = oxleft + 2 where oxrootid = $sParentOxRootIdQuoted and oxleft > '$parentRight' and oxright >= '$parentRight' and oxid != $sActOxidQuoted");
00613                 $oDB->execute("update oxcategories set oxright = oxright + 2 where oxrootid = $sParentOxRootIdQuoted and oxright >= '$parentRight' and oxid != $sActOxidQuoted");
00614                 $oDB->execute("update oxcategories set oxleft = $parentRight, oxright = ($parentRight + 1) where oxid = $sActOxidQuoted");
00615                 $this->_updateNodes($actOxid, false, $thisRoot);
00616                 $rs->moveNext();
00617             }
00618         }
00619     }
00620 
00628     public function __get($sName)
00629     {
00630         switch ($sName) {
00631             case 'aPath':
00632             case 'aFullPath':
00633                 return $this->getPath();
00634                 break;
00635         }
00636         return parent::__get($sName);
00637     }
00638 
00639 }