00001 <?php
00002
00009 class oxCategory extends oxI18n
00010 {
00015 protected $_aSubCats = array();
00016
00021 protected $_aContentCats = array();
00022
00028 protected $_sClassName = 'oxcategory';
00029
00035 protected $_iNrOfArticles;
00036
00042 protected $_blIsVisible;
00043
00049 protected $_blExpanded;
00050
00056 protected $_blHasSubCats;
00057
00063 protected $_blHasVisibleSubCats;
00064
00070 protected $_blIsSeoObject = true;
00071
00077 protected $_blUseLazyLoading = false;
00078
00084 protected $_sDynImageDir = null;
00085
00089 public function __construct()
00090 {
00091 parent::__construct();
00092 $this->init( 'oxcategories' );
00093 }
00094
00102 public function __get( $sName )
00103 {
00104 switch ( $sName ) {
00105 case 'aSubCats':
00106 return $this->_aSubCats;
00107
00108 case 'aContent':
00109 return $this->_aContentCats;
00110
00111 case 'iArtCnt':
00112 return $this->getNrOfArticles();
00113
00114 case 'isVisible':
00115 return $this->getIsVisible();
00116
00117 case 'expanded':
00118 return $this->getExpanded();
00119
00120 case 'hasSubCats':
00121 return $this->getHasSubCats();
00122
00123 case 'hasVisibleSubCats':
00124 return $this->getHasVisibleSubCats();
00125
00126 case 'openlink':
00127 case 'closelink':
00128 case 'link':
00129
00130
00131 return $this->getLink();
00132 case 'dimagedir':
00133 return $this->getPictureUrl();
00134 }
00135 return parent::__get($sName);
00136 }
00137
00145 public function assign( $dbRecord )
00146 {
00147
00148 parent::assign( $dbRecord );
00149
00150
00151 $myConfig = $this->getConfig();
00152 if ( !$this->isAdmin() && $myConfig->getConfigParam( 'bl_perfParseLongDescinSmarty' ) ) {
00153 $this->oxcategories__oxlongdesc = new oxField( oxUtilsView::getInstance()->parseThroughSmarty( $this->oxcategories__oxlongdesc->value, $this->getId() ), oxField::T_RAW );
00154 }
00155
00156 if ( !$this->isAdmin() && ( $myConfig->getConfigParam( 'bl_perfShowActionCatArticleCnt' ) || $myConfig->getConfigParam('blDontShowEmptyCategories') ) ) {
00157 if ( $this->oxcategories__oxpricefrom->value || $this->oxcategories__oxpriceto->value ) {
00158 $this->_iNrOfArticles = oxUtilsCount::getInstance()->getPriceCatArticleCount( $this->getId(), $this->oxcategories__oxpricefrom->value, $this->oxcategories__oxpriceto->value );
00159 } else {
00160 $this->_iNrOfArticles = oxUtilsCount::getInstance()->getCatArticleCount( $this->getId() );
00161 }
00162 }
00163 }
00164
00172 public function delete( $sOXID = null)
00173 {
00174 if ( !$this->getId() ) {
00175 $this->load( $sOXID);
00176 }
00177
00178
00179 $myConfig = $this->getConfig();
00180 $oDB = oxDb::getDb();
00181 $blRet = false;
00182
00183 if ( $this->oxcategories__oxright->value == ($this->oxcategories__oxleft->value+1) ) {
00184
00185
00186 oxUtilsPic::getInstance()->safePictureDelete($this->oxcategories__oxthumb->value, $myConfig->getAbsDynImageDir().'/0', 'oxcategories', 'oxthumb' );
00187
00188 oxUtilsPic::getInstance()->safePictureDelete($this->oxcategories__oxicon->value, $myConfig->getAbsDynImageDir().'/icon', 'oxcategories', 'oxicon' );
00189
00190 $sAdd = " and oxshopid = '" . $this->getShopId() . "' ";
00191
00192 $oDB->execute( "UPDATE oxcategories SET OXLEFT = OXLEFT - 2
00193 WHERE OXROOTID = '".$this->oxcategories__oxrootid->value."'
00194 AND OXLEFT > ".$this->oxcategories__oxleft->value.$sAdd );
00195
00196 $oDB->execute( "UPDATE oxcategories SET OXRIGHT = OXRIGHT - 2
00197 WHERE OXROOTID = '".$this->oxcategories__oxrootid->value."'
00198 AND OXRIGHT > ".$this->oxcategories__oxright->value.$sAdd );
00199
00200
00201 $blRet = parent::delete();
00202
00203
00204 $oDB->execute( "delete from oxobject2category where oxobject2category.oxcatnid='".$this->oxcategories__oxid->value."' ");
00205
00206
00207 $oDB->execute( "delete from oxcategory2attribute where oxcategory2attribute.oxobjectid='".$this->oxcategories__oxid->value."' ");
00208
00209
00210
00211 $oDB->execute( "delete from oxobject2delivery where oxobject2delivery.oxobjectid='".$this->oxcategories__oxid->value."' ");
00212
00213 $oDB->execute( "delete from oxobject2discount where oxobject2discount.oxobjectid='".$this->oxcategories__oxid->value."' ");
00214 }
00215 return $blRet;
00216 }
00217
00223 public function getSubCats()
00224 {
00225 return $this->_aSubCats;
00226 }
00227
00235 public function getSubCat($sKey)
00236 {
00237 return $this->_aSubCats[$sKey];
00238 }
00239
00247 public function setSubCats( $aCats )
00248 {
00249 $this->_aSubCats = $aCats;
00250
00251 foreach ( $aCats as $oCat ) {
00252 if ( $oCat->getIsVisible() ) {
00253 $this->setHasVisibleSubCats( true );
00254 }
00255 }
00256
00257 $this->sortSubCats();
00258 }
00259
00268 public function setSubCat($oCat, $sKey=null)
00269 {
00270 if ( $sKey ) {
00271 $this->_aSubCats[$sKey] = $oCat;
00272 } else {
00273 $this->_aSubCats[] = $oCat;
00274 }
00275
00276
00277 $oCat->setParentCategory( $this );
00278
00279 if ( $oCat->getIsVisible() ) {
00280 $this->setHasVisibleSubCats( true );
00281 }
00282
00283 $this->sortSubCats();
00284 }
00285
00291 public function sortSubCats()
00292 {
00293 uasort( $this->_aSubCats, array( $this, "cmpCat" ) );
00294 }
00295
00301 public function getContentCats()
00302 {
00303 return $this->_aContentCats;
00304 }
00305
00313 public function setContentCats( $aContent )
00314 {
00315 $this->_aContentCats = $aContent;
00316 }
00317
00326 public function setContentCat( $oContent, $sKey=null )
00327 {
00328 if ( $sKey ) {
00329 $this->_aContentCats[$sKey] = $oContent;
00330 } else {
00331 $this->_aContentCats[] = $oContent;
00332 }
00333 }
00334
00343 public function cmpCat( $a,$b )
00344 {
00345 if ( $a->oxcategories__oxsort->value == $b->oxcategories__oxsort->value ) {
00346 return strcasecmp( $a->oxcategories__oxtitle->value, $b->oxcategories__oxtitle->value );
00347 }
00348
00349 return $a->oxcategories__oxsort->value - $b->oxcategories__oxsort->value;
00350 }
00351
00357 public function getNrOfArticles()
00358 {
00359 if ( !$this->getConfig()->getConfigParam( 'bl_perfShowActionCatArticleCnt' ) && $this->getConfig()->getConfigParam( 'blDontShowEmptyCategories' ) ) {
00360 return 0;
00361 }
00362
00363 return $this->_iNrOfArticles;
00364 }
00365
00373 public function setNrOfArticles( $iNum )
00374 {
00375 $this->_iNrOfArticles = $iNum;
00376 }
00377
00383 public function getIsVisible()
00384 {
00385 if (!isset( $this->_blIsVisible ) ) {
00386
00387 if ( $this->getConfig()->getConfigParam( 'blDontShowEmptyCategories' ) ) {
00388 $blEmpty = ($this->_iNrOfArticles < 1) && !$this->getHasVisibleSubCats();
00389 } else {
00390 $blEmpty = false;
00391 }
00392
00393 $this->_blIsVisible = !($blEmpty || $this->oxcategories__oxhidden->value);
00394 }
00395
00396 return $this->_blIsVisible;
00397 }
00398
00406 public function setIsVisible( $blVisible )
00407 {
00408 $this->_blIsVisible = $blVisible;
00409 }
00410
00416 public function getPictureUrl()
00417 {
00418 if ( $this->_sDynImageDir === null ) {
00419 $sThisShop = $this->oxcategories__oxshopid->value;
00420 $this->_sDynImageDir = $this->getConfig()->getPictureUrl( null, false, null, null, $sThisShop);
00421 }
00422 return $this->_sDynImageDir;
00423 }
00424
00432 public function getLink($iLang = null)
00433 {
00434 if (isset($iLang)) {
00435 $iLang = (int) $iLang;
00436 if ($iLang == (int) $this->getLanguage()) {
00437 $iLang = null;
00438 }
00439 }
00440 if ( ($this->link === null ) || isset($iLang)) {
00441 if ( (!isset( $this->oxcategories__oxextlink->value ) || !$this->oxcategories__oxextlink->value ) &&
00442 oxUtils::getInstance()->seoIsActive() ) {
00443 $link = oxSeoEncoderCategory::getInstance()->getCategoryUrl( $this, $iLang );
00444 } else {
00445 $link = $this->getStdLink($iLang);
00446 }
00447
00448 if (isset($iLang)) {
00449 return $link;
00450 } else {
00451 $this->link = $link;
00452 }
00453 }
00454
00455 return $this->link;
00456 }
00457
00465 public function setLink( $sLink )
00466 {
00467 $this->link = $sLink;
00468 }
00469
00477 public function getSqlActiveSnippet( $blForceCoreTable = false )
00478 {
00479 $sTable = $this->getCoreTableName();
00480
00481 $sQ = parent::getSqlActiveSnippet( $blForceCoreTable );
00482 $sQ .= ( strlen( $sQ )? ' and ' : '' ) . " $sTable.oxhidden = '0' ";
00483
00484
00485 return "( $sQ ) ";
00486 }
00487
00495 public function getStdLink($iLang = null)
00496 {
00497 $sLink = '';
00498 if ( $this->oxcategories__oxextlink->value ) {
00499 return $this->getSession()->url( $this->oxcategories__oxextlink->value );
00500 } else {
00501 $sLink = $this->getConfig()->getShopHomeURL(). "cl=alist&cnid=" . $this->getId();
00502 }
00503
00504 if ( isset($iLang) && !oxUtils::getInstance()->seoIsActive() ) {
00505 $iLang = (int) $iLang;
00506 if ($iLang != (int) $this->getLanguage()) {
00507 $sLink .= "&lang={$iLang}";
00508 }
00509 }
00510
00511 return $sLink;
00512 }
00513
00519 public function getExpanded()
00520 {
00521 if ( !isset( $this->_blExpanded ) ) {
00522 $myConfig = $this->getConfig();
00523 $this->_blExpanded = ( $myConfig->getConfigParam( 'blLoadFullTree' ) && !$myConfig->getConfigParam( 'blTopNaviLayout' ) );
00524 }
00525
00526 return $this->_blExpanded;
00527 }
00528
00536 public function setExpanded( $blExpanded )
00537 {
00538 $this->_blExpanded = $blExpanded;
00539 }
00540
00546 public function getHasSubCats()
00547 {
00548 if ( !isset( $this->_blHasSubCats ) ) {
00549 $this->_blHasSubCats = $this->oxcategories__oxright->value > $this->oxcategories__oxleft->value + 1 ;
00550 }
00551
00552 return $this->_blHasSubCats;
00553 }
00554
00560 public function getHasVisibleSubCats()
00561 {
00562 if ( !isset( $this->_blHasVisibleSubCats ) ) {
00563 $this->_blHasVisibleSubCats = false;
00564 }
00565
00566 return $this->_blHasVisibleSubCats;
00567 }
00568
00576 public function setHasVisibleSubCats( $blHasVisibleSubcats )
00577 {
00578 $this->_blHasVisibleSubCats = $blHasVisibleSubcats;
00579 }
00580
00586 public function getAttributes()
00587 {
00588 $sActCat = $this->sOXID;
00589 $aAttributes = array();
00590 $blActiveFilter = false;
00591
00592 $aSessionFilter = oxSession::getVar( 'session_attrfilter' );
00593
00594 $oArtList = oxNew( "oxarticlelist");
00595 $oArtList->loadCategoryIDs( $sActCat, $aSessionFilter );
00596
00597
00598 if (count($oArtList) > 0 ) {
00599 $sArtIds = implode("','", array_keys($oArtList->getArray()) );
00600 $sAttTbl = getViewName('oxattribute');
00601 $sO2ATbl = getViewName('oxobject2attribute');
00602 $sC2ATbl = getViewName('oxcategory2attribute');
00603 $sLngSuf = oxLang::getInstance()->getLanguageTag($this->getLanguage());
00604
00605 $sSelect = "SELECT DISTINCT att.oxid, att.oxtitle{$sLngSuf}, o2a.oxvalue, o2a.oxvalue{$sLngSuf} ".
00606 "FROM $sAttTbl as att, $sO2ATbl as o2a ,$sC2ATbl as c2a ".
00607 "WHERE att.oxid = o2a.oxattrid AND c2a.oxobjectid = '{$sActCat}' AND c2a.oxattrid = att.oxid AND o2a.oxvalue{$sLngSuf} !='' AND o2a.oxobjectid IN ('$sArtIds') ".
00608 "ORDER BY c2a.oxsort , att.oxpos, att.oxtitle{$sLngSuf}, o2a.oxvalue{$sLngSuf}";
00609
00610 $rs = oxDb::getDb()->Execute( $sSelect);
00611 if ($rs != false && $rs->recordCount() > 0) {
00612 while ( !$rs->EOF && list($sAttId,$sAttTitle,$sAttVid,$sAttValue) = $rs->fields ) {
00613 if ( !isset( $aAttributes[$sAttId])) {
00614 $oAttribute = new stdClass();
00615 $oAttribute->title = $sAttTitle;
00616 $oAttribute->aValues = array();
00617 $aAttributes[$sAttId] = $oAttribute;
00618 }
00619 $oValue = new stdClass();
00620 $oValue->id = htmlspecialchars($sAttValue);
00621 $oValue->value = htmlspecialchars($sAttValue);
00622 $oValue->blSelected = isset($aSessionFilter[$sActCat][$sAttId]) && $aSessionFilter[$sActCat][$sAttId] == $sAttValue;
00623
00624 $blActiveFilter = $blActiveFilter || $oValue->blSelected;
00625 $aAttributes[$sAttId]->aValues[$sAttVid] = $oValue;
00626 $rs->moveNext();
00627 }
00628 }
00629
00630 }
00631
00632 if ( is_array($aSessionFilter[$sActCat]) && !$blActiveFilter ) {
00633 oxSession::setVar( "session_attrfilter", false);
00634 }
00635 return $aAttributes;
00636 }
00637
00645 public function getCatInLang( $oActCategory = null )
00646 {
00647 $oCategoryInDefaultLanguage= oxNew( "oxcategory" );
00648 if ( $this->oxcategories__oxpricefrom->value || $this->oxcategories__oxpriceto->value) {
00649
00650 $oCategoryInDefaultLanguage= oxNew( "oxcategory" );
00651 $oCategoryInDefaultLanguage->loadInLang( 0, $this->getId());
00652 } else {
00653 $oCategoryInDefaultLanguage= oxNew( "oxcategory" );
00654 $oCategoryInDefaultLanguage->loadInLang( 0, $oActCategory->getId());
00655 }
00656 return $oCategoryInDefaultLanguage;
00657 }
00658
00666 public function setParentCategory( $oCategory )
00667 {
00668 $this->_oParent = $oCategory;
00669 }
00670
00676 public function getParentCategory()
00677 {
00678 $oCat = null;
00679
00680
00681 if ( $this->oxcategories__oxparentid->value && $this->oxcategories__oxparentid->value != 'oxrootid' ) {
00682
00683
00684 if ( $this->_oParent ) {
00685 $oCat = $this->_oParent;
00686 } else {
00687 $oCat = oxNew( 'oxcategory' );
00688 if ( !$oCat->loadInLang( $this->getLanguage(), $this->oxcategories__oxparentid->value ) ) {
00689 $oCat = null;
00690 }
00691 }
00692 }
00693 return $oCat;
00694 }
00695
00703 public static function getRootId($sCategoryId)
00704 {
00705 if ( !isset( $sCategoryId ) ) {
00706 return;
00707 }
00708
00709 return oxDb::getDb()->getOne( 'select oxrootid from '.getViewName('oxcategories').' where oxid = ?', array( $sCategoryId ) );
00710 }
00711
00712
00720 public function assignViewableRecord($sSelect)
00721 {
00722 if ( $this->assignRecord( $sSelect ) ) {
00723 return true;
00724 }
00725
00726
00727 return false;
00728 }
00729
00735 protected function _insert()
00736 {
00737
00738
00739 if ( $this->oxcategories__oxparentid->value != "oxrootid") {
00740
00741
00742 $oParent = oxNew( "oxcategory" );
00743
00744 if ( !$oParent->load( $this->oxcategories__oxparentid->value) ) {
00745 return false;
00746 }
00747
00748 $sAdd = " and oxshopid = '" . $this->getShopId() . "' ";
00749
00750
00751 $oDB = oxDb::getDb();
00752 $oDB->execute( "UPDATE oxcategories SET OXLEFT = OXLEFT + 2
00753 WHERE OXROOTID = '".$oParent->oxcategories__oxrootid->value."'
00754 AND OXLEFT > ".$oParent->oxcategories__oxright->value."
00755 AND OXRIGHT >= ".$oParent->oxcategories__oxright->value.$sAdd);
00756
00757
00758 $oDB->execute( "UPDATE oxcategories SET OXRIGHT = OXRIGHT + 2
00759 WHERE OXROOTID = '".$oParent->oxcategories__oxrootid->value."'
00760 AND OXRIGHT >= ".$oParent->oxcategories__oxright->value.$sAdd );
00761
00762
00763
00764
00765
00766 if ( !$this->getId() ) {
00767 $this->setId();
00768 }
00769
00770 $this->oxcategories__oxrootid = new oxField($oParent->oxcategories__oxrootid->value, oxField::T_RAW);
00771 $this->oxcategories__oxleft = new oxField($oParent->oxcategories__oxright->value, oxField::T_RAW);
00772 $this->oxcategories__oxright = new oxField($oParent->oxcategories__oxright->value + 1, oxField::T_RAW);
00773 return parent::_insert();
00774 } else {
00775
00776 if ( !$this->getId() ) {
00777 $this->setId();
00778 }
00779
00780 $this->oxcategories__oxrootid = new oxField($this->getId(), oxField::T_RAW);
00781 $this->oxcategories__oxleft = new oxField(1, oxField::T_RAW);
00782 $this->oxcategories__oxright = new oxField(2, oxField::T_RAW);
00783 return parent::_insert();
00784 }
00785 }
00786
00792 protected function _update()
00793 {
00794
00795 $oDB = oxDb::getDb();
00796
00797 $sOldParentID = $oDB->getOne( "select oxparentid from oxcategories where oxid = '".$this->getId()."'");
00798
00799 if ( $this->_blIsSeoObject && $this->isAdmin() ) {
00800 oxSeoEncoderCategory::getInstance()->markRelatedAsExpired($this);
00801 }
00802
00803 $blRes = parent::_update();
00804
00805
00806
00807
00808
00809
00810
00811
00812
00813
00814 if ( $this->oxcategories__oxparentid->value != $sOldParentID) {
00815 $sOldParentLeft = $this->oxcategories__oxleft->value;
00816 $sOldParentRight = $this->oxcategories__oxright->value;
00817
00818 $iTreeSize = $sOldParentRight-$sOldParentLeft+1;
00819
00820 $sNewRootID = $oDB->getOne( "select oxrootid from oxcategories where oxid = '".$this->oxcategories__oxparentid->value."'");
00821
00822
00823 if ( $sNewRootID == "") {
00824
00825 $sNewRootID = $this->getId();
00826 }
00827
00828 $sNewParentLeft = $oDB->getOne( "select oxleft from oxcategories where oxid = '".$this->oxcategories__oxparentid->value."'");
00829
00830
00831
00832
00833
00834
00835 $iMoveAfter = $sNewParentLeft+1;
00836
00837
00838
00839 if ($sNewParentLeft > $sOldParentLeft && $sNewParentLeft < $sOldParentRight && $this->oxcategories__oxrootid->value == $sNewRootID) {
00840
00841
00842
00843 $sRestoreOld = "UPDATE oxcategories SET OXPARENTID = '".$sOldParentID."' WHERE oxid = '".$this->getId()."'";
00844 $oDB->execute( $sRestoreOld );
00845 return false;
00846 }
00847
00848
00849 if ($sOldParentLeft > $iMoveAfter && $this->oxcategories__oxrootid->value == $sNewRootID) {
00850 $sOldParentLeft += $iTreeSize;
00851 $sOldParentRight += $iTreeSize;
00852 }
00853
00854 $iDelta = $iMoveAfter-$sOldParentLeft;
00855
00856
00857
00858 $sAddOld = " and oxshopid = '" . $this->getShopId() . "' and OXROOTID = '".$this->oxcategories__oxrootid->value."';";
00859 $sAddNew = " and oxshopid = '" . $this->getShopId() . "' and OXROOTID = '".$sNewRootID."';";
00860
00861
00862 $oDB->execute( "UPDATE oxcategories SET OXLEFT = (OXLEFT + ".$iTreeSize.") WHERE OXLEFT >= ".$iMoveAfter.$sAddNew );
00863 $oDB->execute( "UPDATE oxcategories SET OXRIGHT = (OXRIGHT + ".$iTreeSize.") WHERE OXRIGHT >= ".$iMoveAfter.$sAddNew );
00864
00865
00866 $sChangeRootID = "";
00867 if ($this->oxcategories__oxrootid->value != $sNewRootID) {
00868
00869 $sChangeRootID = ", OXROOTID='$sNewRootID'";
00870 }
00871
00872
00873 $oDB->execute( "UPDATE oxcategories SET OXLEFT = (OXLEFT + ".$iDelta."), OXRIGHT = (OXRIGHT + ".$iDelta.") ".$sChangeRootID." WHERE OXLEFT >= ".$sOldParentLeft." AND OXRIGHT <= ".$sOldParentRight.$sAddOld );
00874
00875
00876
00877 $oDB->execute( "UPDATE oxcategories SET OXLEFT = (OXLEFT - ".$iTreeSize.") WHERE OXLEFT >= ".($sOldParentRight+1).$sAddOld );
00878 $oDB->execute( "UPDATE oxcategories SET OXRIGHT = (OXRIGHT - ".$iTreeSize.") WHERE OXRIGHT >= ".($sOldParentRight+1).$sAddOld );
00879
00880 }
00881
00882 if ( $blRes && $this->_blIsSeoObject && $this->isAdmin() ) {
00883 oxSeoEncoderCategory::getInstance()->markRelatedAsExpired($this);
00884 }
00885
00886 return $blRes;
00887 }
00888
00898 protected function _setFieldData( $sFieldName, $sValue, $iDataType = oxField::T_TEXT)
00899 {
00900
00901 if ($sFieldName[2] == 'l' || $sFieldName[2] == 'L' || (isset($sFieldName[16]) && ($sFieldName[16] == 'l' || $sFieldName[16] == 'L') ) ) {
00902 if ('oxlongdesc' === strtolower($sFieldName) || 'oxcategories__oxlongdesc' === strtolower($sFieldName)) {
00903 $iDataType = oxField::T_RAW;
00904 }
00905 }
00906 return parent::_setFieldData($sFieldName, $sValue, $iDataType);
00907 }
00908
00909
00915 public function getIconUrl()
00916 {
00917 return $this->getConfig()->getPictureUrl( 'icon/'.$this->oxcategories__oxicon->value );
00918 }
00919
00920 }