oxcategorylist.php

Go to the documentation of this file.
00001 <?php
00002 
00003 
00010 class oxCategoryList extends oxList
00011 {
00017     protected $_sObjectsInListName = 'oxcategory';
00018 
00024     protected $_blHideEmpty = false;
00025 
00031     protected $_blForseFull = false;
00032 
00038     protected $_iForseLevel = 1;
00039 
00045     protected $_sActCat     = null;
00046 
00052     protected $_aPath = array();
00053 
00061     public function __construct( $sObjectsInListName = 'oxcategory')
00062     {
00063         $this->_blHideEmpty = $this->getConfig()->getConfigParam('blDontShowEmptyCategories');
00064         parent::__construct( $sObjectsInListName );
00065     }
00066 
00074     protected function _getSelectString($blReverse = false)
00075     {
00076         $sViewName  = $this->getBaseObject()->getViewName();
00077         $sFieldList = $this->getBaseObject()->getSelectFields();
00078         //$sFieldList = "oxid, oxparentid, oxleft, oxright, oxrootid, oxsort, oxtitle, oxpricefrom, oxpriceto, oxicon ";
00079         $sWhere     = $this->_getDepthSqlSnippet();
00080 
00081         $sOrdDir    = $blReverse?'desc':'asc';
00082         $sOrder     = "$sViewName.oxrootid $sOrdDir, $sViewName.oxleft $sOrdDir";
00083 
00084             $sFieldList.= ",not $sViewName.oxactive as remove";
00085 
00086 
00087         return "select $sFieldList from $sViewName where $sWhere order by $sOrder";
00088     }
00089 
00096     protected function _getDepthSqlSnippet()
00097     {
00098         $sDepthSnippet = ' 1 ';
00099 
00100         // complex tree depth loading optimization ...
00101         if (!$this->isAdmin() && !$this->_blHideEmpty && !$this->_blForseFull) {
00102             $sViewName  = $this->getBaseObject()->getViewName();
00103             $sDepthSnippet = ' ( 0';
00104 
00105             $oCat = oxNew('oxcategory');
00106             $blLoaded = $oCat->load($this->_sActCat);
00107             // load compleate tree of active category, if it exists
00108             if ($blLoaded && $this->_sActCat && $sActRootID = $oCat->oxcategories__oxrootid->value) {
00109                 $sDepthSnippet .= " or ("
00110                     ." $sViewName.oxparentid in"
00111                         ." (select subcats.oxparentid from $sViewName as subcats"
00112                         ." where subcats.oxrootid = '{$oCat->oxcategories__oxrootid->value}'"
00113                         ." and subcats.oxleft <= {$oCat->oxcategories__oxleft->value}"
00114                         ." and subcats.oxright >= {$oCat->oxcategories__oxright->value})"
00115                     ." or ($sViewName.oxparentid = '{$oCat->oxcategories__oxid->value}')"
00116                 .")";
00117             } else {
00118                 $this->_sActCat = null;
00119             }
00120 
00121             // load 1'st category level (roots)
00122             if ($this->_iForseLevel >= 1) {
00123                 $sDepthSnippet .= " or $sViewName.oxparentid = 'oxrootid'";
00124             }
00125 
00126             // load 2'nd category level ()
00127             if ($this->_iForseLevel >= 2) {
00128                 $sDepthSnippet .= " or $sViewName.oxrootid = $sViewName.oxparentid or $sViewName.oxid = $sViewName.oxrootid";
00129             }
00130 
00131             $sDepthSnippet .= ' ) ';
00132         }
00133         return $sDepthSnippet;
00134     }
00135 
00136 
00149     public function buildTree($sActCat, $blLoadFullTree, $blPerfLoadTreeForSearch, $blTopNaviLayout)
00150     {
00151         startProfile("buildTree");
00152 
00153         $this->_sActCat     = $sActCat;
00154         $this->_blForseFull = $blLoadFullTree || $blPerfLoadTreeForSearch;
00155         $this->_iForseLevel = $blTopNaviLayout?2:1;
00156 
00157         $sSelect = $this->_getSelectString(true);
00158         $this->selectString($sSelect);
00159 
00160         // PostProcessing
00161         if ( !$this->isAdmin() ) {
00162             // remove inactive categories
00163             $this->_ppRemoveInactiveCategories();
00164 
00165             // builds navigation path
00166             $this->_ppAddPathInfo();
00167 
00168             // add content categories
00169             $this->_ppAddContentCategories();
00170 
00171             // build tree structure
00172             $this->_ppBuildTree();
00173         }
00174 
00175         stopProfile("buildTree");
00176     }
00177 
00185     public function buildList($blLoad)
00186     {
00187 
00188         if (!$blLoad) {
00189             return;
00190         }
00191 
00192         startProfile('buildCategoryList');
00193 
00194         $this->_blForseFull = true;
00195         $this->selectString($this->_getSelectString(false));
00196 
00197         // PostProcessing
00198         // add tree depth info
00199         $this->_ppAddDepthInformation();
00200         stopProfile('buildCategoryList');
00201     }
00202 
00210     public function setShopID($sShopID)
00211     {
00212         $this->_sShopID = $sShopID;
00213     }
00214 
00220     public function getPath()
00221     {
00222         return $this->_aPath;
00223     }
00224 
00230     public function getHtmlPath()
00231     {
00232         $sHtmlCatTree = '';
00233         $sSep         = '';
00234         foreach ( $this->_aPath as $oCategory ) {
00235             $sHtmlCatTree .= " $sSep<a href='".$oCategory->getLink()."'>".$oCategory->oxcategories__oxtitle->value."</a>";
00236             $sSep = '/ ';
00237         }
00238         return $sHtmlCatTree;
00239     }
00240 
00246     public function getClickCat()
00247     {
00248         if (count($this->_aPath)) {
00249             return end($this->_aPath);
00250         }
00251     }
00252 
00258     public function getClickRoot()
00259     {
00260         if (count($this->_aPath)) {
00261             return array(reset($this->_aPath));
00262         }
00263     }
00264 
00270     protected function _ppRemoveInactiveCategories()
00271     {
00272         // Colect all items whitch must be remove
00273         $aRemoveList = array();
00274         foreach ($this->_aArray as $oCat) {
00275             if ($oCat->oxcategories__remove->value) {
00276                 if (isset($aRemoveList[$oCat->oxcategories__oxrootid->value])) {
00277                     $aRemoveRange = $aRemoveList[$oCat->oxcategories__oxrootid->value];
00278                 } else {
00279                     $aRemoveRange = array();
00280                 }
00281                 $aRemoveList[$oCat->oxcategories__oxrootid->value] = array_merge(range($oCat->oxcategories__oxleft->value, $oCat->oxcategories__oxright->value), $aRemoveRange);
00282             }
00283             unset($oCat->oxcategories__remove);
00284         }
00285 
00286         // Remove colected items from list.
00287         foreach ($this->_aArray as $oCat) {
00288             if (isset($aRemoveList[$oCat->oxcategories__oxrootid->value]) && in_array($oCat->oxcategories__oxleft->value, $aRemoveList[$oCat->oxcategories__oxrootid->value])) {
00289                 unset( $this->_aArray[$oCat->oxcategories__oxid->value] );
00290             }
00291         }
00292     }
00293 
00299     protected function _ppAddPathInfo()
00300     {
00301 
00302         if (is_null($this->_sActCat)) {
00303             return;
00304         }
00305 
00306         $aPath = array();
00307         $sCurrentCat  = $this->_sActCat;
00308 
00309         while ($sCurrentCat != 'oxrootid' && isset($this[$sCurrentCat])) {
00310             $oCat = $this[$sCurrentCat];
00311             $oCat->setExpanded(true);
00312             $aPath[$sCurrentCat] = $oCat;
00313             $sCurrentCat = $oCat->oxcategories__oxparentid->value;
00314         }
00315 
00316         $this->_aPath = array_reverse($aPath);
00317     }
00318 
00324     protected function _ppAddContentCategories()
00325     {
00326         // load content pages for adding them into menue tree
00327         $oContentList = oxNew( "oxcontentlist" );
00328         $oContentList->loadCatMenues();
00329 
00330         foreach ($oContentList as $sCatId => $aContent) {
00331             if (array_key_exists($sCatId, $this->_aArray)) {
00332                 $this[$sCatId]->setContentCats($aContent);
00333 
00334             }
00335         }
00336     }
00337 
00343     protected function _ppBuildTree()
00344     {
00345 
00346         $aTree = array();
00347         foreach ($this->_aArray as $oCat) {
00348             $sParentId = $oCat->oxcategories__oxparentid->value;
00349             if ( $sParentId != 'oxrootid') {
00350                 $this->_aArray[$sParentId]->setSubCat($oCat, $oCat->getId());
00351             } else {
00352                 $aTree[$oCat->getId()] = $oCat;
00353             }
00354         }
00355 
00356         // Sort root categories
00357         uasort($aTree, array(oxNew('oxCategory'), "cmpCat"));
00358 
00359         $this->assign($aTree);
00360     }
00361 
00368     protected function _ppAddDepthInformation()
00369     {
00370 
00371         $aStack = array();
00372         $iDepth = 0;
00373         $sPrevParent = '';
00374 
00375         foreach ($this->_aArray as $oCat) {
00376 
00377             $sParentId = $oCat->oxcategories__oxparentid->value;
00378             if ( $oCat->oxcategories__oxparentid->value == 'oxrootid' ) {
00379                 $iDepth = 1;
00380                 $aStack = array($sParentId => '0');
00381             }
00382 
00383             if ($sPrevParent != $sParentId && isset($aStack[$sParentId]) ) {
00384                 $iDepth -= count($aStack)- $aStack[$sParentId];
00385                 $aStack = array_slice($aStack, 0, $iDepth-1, true);
00386             }
00387 
00388             if ( !isset($aStack[$sParentId])) {
00389                 $aStack[$sParentId] = $iDepth;
00390                 $iDepth++;
00391             }
00392 
00393             $oCat->oxcategories__oxtitle->setValue(str_repeat('-', $iDepth-1).' '.$oCat->oxcategories__oxtitle->value);
00394             $sPrevParent = $sParentId;
00395         }
00396 
00397     }
00398 
00407     public function updateCategoryTree($blVerbose = true, $sShopID = null)
00408     {
00409         $oDB = oxDb::getDb();
00410         $sWhere = '1';
00411 
00412 
00413         $oDB->execute("update oxcategories set oxleft = 0, oxright = 0 where $sWhere");
00414         $oDB->execute("update oxcategories set oxleft = 1, oxright = 2 where oxparentid = 'oxrootid' and $sWhere");
00415 
00416         // Get all root categories
00417         $rs = $oDB->execute("select oxid, oxtitle from oxcategories where oxparentid = 'oxrootid' and $sWhere order by oxsort");
00418         if ($rs != false && $rs->recordCount() > 0) {
00419             while (!$rs->EOF) {
00420                 if ($blVerbose) {
00421                     echo( "<b>Processing : ".$rs->fields[1]."</b>(".$rs->fields[0].")<br>");
00422                 }
00423                 $oxRootId = $rs->fields[0];
00424 
00425                 $updn = $this->_updateNodes($oxRootId, true, $oxRootId);
00426                 $rs->moveNext();
00427             }
00428         }
00429     }
00430 
00440     protected function _updateNodes($oxRootId, $isroot, $thisRoot)
00441     {
00442         $oDB = oxDb::getDb();
00443 
00444         if ($isroot) {
00445             $thisRoot = $oxRootId;
00446         }
00447 
00448         // Get sub categories of root categorie
00449         $rs = $oDB->execute("update oxcategories set oxrootid = '$thisRoot' where oxparentid = '$oxRootId'");
00450         $rs = $oDB->execute("select oxid, oxparentid from oxcategories where oxparentid = '$oxRootId' order by oxsort");
00451         // If there are sub categories
00452         if ($rs != false && $rs->recordCount() > 0) {
00453             while (!$rs->EOF) {
00454                 $parentId = $rs->fields[1];
00455                 $actOxid = $rs->fields[0];
00456 
00457                 // Get the data of the parent category to the current Cat
00458                 $rs3 = $oDB->execute("select oxrootid, oxright from oxcategories where oxid = '$parentId'");
00459                 while (!$rs3->EOF) {
00460                     $parentOxRootId = $rs3->fields[0];
00461                     $parentRight = $rs3->fields[1];
00462                     $rs3->moveNext();
00463                 }
00464                 $oDB->execute("update oxcategories set oxleft = oxleft + 2 where oxrootid = '$parentOxRootId' and oxleft > '$parentRight' and oxright >= '$parentRight' and oxid != '$actOxid'");
00465                 $oDB->execute("update oxcategories set oxright = oxright + 2 where oxrootid = '$parentOxRootId' and oxright >= '$parentRight' and oxid != '$actOxid'");
00466                 $oDB->execute("update oxcategories set oxleft = $parentRight, oxright = ($parentRight + 1) where oxid = '$actOxid'");
00467                 $this->_updateNodes($actOxid, false, $thisRoot);
00468                 $rs->moveNext();
00469             }
00470         }
00471     }
00472 
00480     public function __get($sName)
00481     {
00482         switch ($sName) {
00483             case 'aPath':
00484             case 'aFullPath':
00485                 return $this->getPath();
00486             }
00487         return parent::__get($sName);
00488     }
00489 
00490 }

Generated on Thu Dec 4 12:04:56 2008 for OXID eShop CE by  doxygen 1.5.5