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         // build tree structure
00289         $this->_ppBuildTree();
00290 
00291         // PostProcessing
00292         // add tree depth info
00293         $this->_ppAddDepthInformation();
00294         stopProfile('buildCategoryList');
00295     }
00296 
00304     public function setShopID($sShopID)
00305     {
00306         $this->_sShopID = $sShopID;
00307     }
00308 
00314     public function getPath()
00315     {
00316         return $this->_aPath;
00317     }
00318 
00324     public function getHtmlPath()
00325     {
00326         $sHtmlCatTree = '';
00327         $sSep         = '';
00328         foreach ( $this->_aPath as $oCategory ) {
00329             $sHtmlCatTree .= " $sSep<a href='".$oCategory->getLink()."'>".$oCategory->oxcategories__oxtitle->value."</a>";
00330             $sSep = '/ ';
00331         }
00332         return $sHtmlCatTree;
00333     }
00334 
00340     public function getClickCat()
00341     {
00342         if (count($this->_aPath)) {
00343             return end($this->_aPath);
00344         }
00345     }
00346 
00352     public function getClickRoot()
00353     {
00354         if (count($this->_aPath)) {
00355             return array(reset($this->_aPath));
00356         }
00357     }
00358 
00364     protected function _ppRemoveInactiveCategories()
00365     {
00366         // Colect all items whitch must be remove
00367         $aRemoveList = array();
00368         foreach ($this->_aArray as $oCat) {
00369             if ($oCat->oxcategories__oxppremove->value) {
00370                 if (isset($aRemoveList[$oCat->oxcategories__oxrootid->value])) {
00371                     $aRemoveRange = $aRemoveList[$oCat->oxcategories__oxrootid->value];
00372                 } else {
00373                     $aRemoveRange = array();
00374                 }
00375                 $aRemoveList[$oCat->oxcategories__oxrootid->value] = array_merge(range($oCat->oxcategories__oxleft->value, $oCat->oxcategories__oxright->value), $aRemoveRange);
00376             }
00377             unset($oCat->oxcategories__oxppremove);
00378         }
00379 
00380         // Remove colected items from list.
00381         foreach ($this->_aArray as $oCat) {
00382             if (isset($aRemoveList[$oCat->oxcategories__oxrootid->value]) && in_array($oCat->oxcategories__oxleft->value, $aRemoveList[$oCat->oxcategories__oxrootid->value])) {
00383                 unset( $this->_aArray[$oCat->oxcategories__oxid->value] );
00384             }
00385         }
00386     }
00387 
00393     protected function _ppAddPathInfo()
00394     {
00395 
00396         if (is_null($this->_sActCat)) {
00397             return;
00398         }
00399 
00400         $aPath = array();
00401         $sCurrentCat  = $this->_sActCat;
00402 
00403         while ($sCurrentCat != 'oxrootid' && isset($this[$sCurrentCat])) {
00404             $oCat = $this[$sCurrentCat];
00405             $oCat->setExpanded(true);
00406             $aPath[$sCurrentCat] = $oCat;
00407             $sCurrentCat = $oCat->oxcategories__oxparentid->value;
00408         }
00409 
00410         $this->_aPath = array_reverse($aPath);
00411     }
00412 
00418     protected function _ppAddContentCategories()
00419     {
00420         // load content pages for adding them into menue tree
00421         $oContentList = oxNew( "oxcontentlist" );
00422         $oContentList->loadCatMenues();
00423 
00424         foreach ($oContentList as $sCatId => $aContent) {
00425             if (array_key_exists($sCatId, $this->_aArray)) {
00426                 $this[$sCatId]->setContentCats($aContent);
00427 
00428             }
00429         }
00430     }
00431 
00437     protected function _ppBuildTree()
00438     {
00439         startProfile("_sortCats");
00440         $aIds = $this->sortCats();
00441         stopProfile("_sortCats");
00442         $aTree = array();
00443         foreach ($this->_aArray as $oCat) {
00444             $sParentId = $oCat->oxcategories__oxparentid->value;
00445             if ( $sParentId != 'oxrootid') {
00446                 if (isset($this->_aArray[$sParentId])) {
00447                     $this->_aArray[$sParentId]->setSortingIds( $aIds );
00448                     $this->_aArray[$sParentId]->setSubCat($oCat, $oCat->getId(), true);
00449                 }
00450             } else {
00451                 $aTree[$oCat->getId()] = $oCat;
00452             }
00453         }
00454 
00455         // run sorting only once on each parent
00456         $aParents = array();
00457         foreach ($this->_aArray as $oCat) {
00458             $aParents[$oCat->oxcategories__oxparentid->value] = true;
00459         }
00460         if ($aParents['oxrootid']) {
00461             unset($aParents['oxrootid']);
00462         }
00463         foreach (array_keys($aParents) as $sParent) {
00464             if (isset($this->_aArray[$sParent])) {
00465                 $this->_aArray[$sParent]->sortSubCats();
00466             }
00467         }
00468 
00469         // Sort root categories
00470         $oCategory = oxNew('oxcategory');
00471         $oCategory->setSortingIds( $aIds );
00472         uasort($aTree, array( $oCategory, 'cmpCat' ) );
00473 
00474         $this->assign($aTree);
00475     }
00476 
00482     public function sortCats()
00483     {
00484         $sViewName  = getViewName('oxcategories');
00485         $sSortSql = $this->_getSelectString(false, array('oxid', 'oxparentid', 'oxsort', 'oxtitle'), 'oxparentid, oxsort, oxtitle');
00486         $aIds = array();
00487         $oDB = oxDb::getDb(true);
00488         $rs = $oDB->execute($sSortSql);
00489         $cnt = 0;
00490         if ($rs != false && $rs->recordCount() > 0) {
00491             while (!$rs->EOF) {
00492                 $aIds[$rs->fields['oxid']] = $cnt;
00493                 $cnt++;
00494                 $rs->moveNext();
00495             }
00496         }
00497         return $aIds;
00498     }
00499 
00506     protected function _ppAddDepthInformation()
00507     {
00508 
00509         $aStack = array();
00510         $iDepth = 0;
00511         $sPrevParent = '';
00512 
00513         $aTree = array();
00514         foreach ($this->_aArray as $oCat) {
00515 
00516             $aTree[$oCat->getId()] = $oCat;
00517             $aSubCats = $oCat->getSubCats();
00518             if ( count($aSubCats) > 0 ) {
00519                 foreach ($aSubCats as $oSubCat) {
00520                     $aTree = $this->_addDepthInfo($aTree, $oSubCat);
00521                 }
00522             }
00523         }
00524         $this->assign($aTree);
00525 
00526     }
00527 
00537     protected function _addDepthInfo($aTree, $oCat, $sDepth = "")
00538     {
00539         $sDepth .= "-";
00540         $oCat->oxcategories__oxtitle->setValue($sDepth.' '.$oCat->oxcategories__oxtitle->value);
00541         $aTree[$oCat->getId()] = $oCat;
00542         $aSubCats = $oCat->getSubCats();
00543         if ( count($aSubCats) > 0 ) {
00544             foreach ($aSubCats as $oSubCat) {
00545                 $aTree = $this->_addDepthInfo($aTree, $oSubCat, $sDepth);
00546             }
00547         }
00548         return $aTree;
00549     }
00558     public function updateCategoryTree($blVerbose = true, $sShopID = null)
00559     {
00560         $oDB = oxDb::getDb();
00561         $sWhere = '1';
00562 
00563 
00564         $oDB->execute("update oxcategories set oxleft = 0, oxright = 0 where $sWhere");
00565         $oDB->execute("update oxcategories set oxleft = 1, oxright = 2 where oxparentid = 'oxrootid' and $sWhere");
00566 
00567         // Get all root categories
00568         $rs = $oDB->execute("select oxid, oxtitle from oxcategories where oxparentid = 'oxrootid' and $sWhere order by oxsort");
00569         if ($rs != false && $rs->recordCount() > 0) {
00570             while (!$rs->EOF) {
00571                 $this->_aUpdateInfo[] = "<b>Processing : ".$rs->fields[1]."</b>(".$rs->fields[0].")<br>";
00572                 if ( $blVerbose ) {
00573                     echo next( $this->_aUpdateInfo );
00574                 }
00575                 $oxRootId = $rs->fields[0];
00576 
00577                 $updn = $this->_updateNodes($oxRootId, true, $oxRootId);
00578                 $rs->moveNext();
00579             }
00580         }
00581     }
00582 
00588     public function getUpdateInfo()
00589     {
00590         return $this->_aUpdateInfo;
00591     }
00592 
00602     protected function _updateNodes($oxRootId, $isroot, $thisRoot)
00603     {
00604         $oDB = oxDb::getDb();
00605 
00606         if ($isroot) {
00607             $thisRoot = $oxRootId;
00608         }
00609 
00610         // Get sub categories of root categorie
00611         $rs = $oDB->execute("update oxcategories set oxrootid = ".$oDB->quote($thisRoot)." where oxparentid = ".$oDB->quote($oxRootId));
00612         $rs = $oDB->execute("select oxid, oxparentid from oxcategories where oxparentid = ".$oDB->quote($oxRootId)." order by oxsort");
00613         // If there are sub categories
00614         if ($rs != false && $rs->recordCount() > 0) {
00615             while (!$rs->EOF) {
00616                 $parentId = $rs->fields[1];
00617                 $actOxid = $rs->fields[0];
00618                 $sActOxidQuoted = $oDB->quote($actOxid);
00619 
00620                 // Get the data of the parent category to the current Cat
00621                 $rs3 = $oDB->execute("select oxrootid, oxright from oxcategories where oxid = ".$oDB->quote($parentId));
00622                 while (!$rs3->EOF) {
00623                     $parentOxRootId = $rs3->fields[0];
00624                     $parentRight    = (int)$rs3->fields[1];
00625                     $rs3->moveNext();
00626                 }
00627                 $sParentOxRootIdQuoted = $oDB->quote($parentOxRootId);
00628                 $oDB->execute("update oxcategories set oxleft = oxleft + 2 where oxrootid = $sParentOxRootIdQuoted and oxleft > '$parentRight' and oxright >= '$parentRight' and oxid != $sActOxidQuoted");
00629                 $oDB->execute("update oxcategories set oxright = oxright + 2 where oxrootid = $sParentOxRootIdQuoted and oxright >= '$parentRight' and oxid != $sActOxidQuoted");
00630                 $oDB->execute("update oxcategories set oxleft = $parentRight, oxright = ($parentRight + 1) where oxid = $sActOxidQuoted");
00631                 $this->_updateNodes($actOxid, false, $thisRoot);
00632                 $rs->moveNext();
00633             }
00634         }
00635     }
00636 
00644     public function __get($sName)
00645     {
00646         switch ($sName) {
00647             case 'aPath':
00648             case 'aFullPath':
00649                 return $this->getPath();
00650                 break;
00651         }
00652         return parent::__get($sName);
00653     }
00654 
00655 }