00001 <?php
00002
00008 class oxCategory extends oxI18n implements oxIUrl
00009 {
00014 protected $_aSubCats = array();
00015
00020 protected $_aContentCats = array();
00021
00027 protected $_sClassName = 'oxcategory';
00028
00034 protected $_iNrOfArticles;
00035
00041 protected $_blIsVisible;
00042
00048 protected $_blExpanded;
00049
00055 protected $_blHasSubCats;
00056
00062 protected $_blHasVisibleSubCats;
00063
00069 protected $_blIsSeoObject = true;
00070
00076 protected $_blUseLazyLoading = false;
00077
00083 protected $_sDynImageDir = null;
00084
00090 protected $_blTopCategory = null;
00091
00099 protected $_aIds = array();
00100
00106 protected $_aStdUrls = array();
00107
00113 protected $_aSeoUrls = array();
00114
00118 public function __construct()
00119 {
00120 parent::__construct();
00121 $this->init( 'oxcategories' );
00122 }
00123
00131 public function __get( $sName )
00132 {
00133 switch ( $sName ) {
00134 case 'aSubCats':
00135 return $this->_aSubCats;
00136 break;
00137 case 'aContent':
00138 return $this->_aContentCats;
00139 break;
00140 case 'iArtCnt':
00141 return $this->getNrOfArticles();
00142 break;
00143 case 'isVisible':
00144 return $this->getIsVisible();
00145 break;
00146 case 'expanded':
00147 return $this->getExpanded();
00148 break;
00149 case 'hasSubCats':
00150 return $this->getHasSubCats();
00151 break;
00152 case 'hasVisibleSubCats':
00153 return $this->getHasVisibleSubCats();
00154 break;
00155 case 'openlink':
00156 case 'closelink':
00157 case 'link':
00158
00159
00160 return $this->getLink();
00161 break;
00162 case 'dimagedir':
00163 return $this->getPictureUrl();
00164 break;
00165 }
00166 return parent::__get($sName);
00167 }
00168
00176 public function assign( $dbRecord )
00177 {
00178 $this->_iNrOfArticles = null;
00179 return parent::assign( $dbRecord );
00180 }
00181
00189 public function delete( $sOXID = null)
00190 {
00191 if ( !$this->getId() ) {
00192 $this->load( $sOXID);
00193 }
00194
00195
00196 $myConfig = $this->getConfig();
00197 $oDB = oxDb::getDb();
00198 $blRet = false;
00199
00200 if ( $this->oxcategories__oxright->value == ($this->oxcategories__oxleft->value+1) ) {
00201 $myUtilsPic = oxUtilsPic::getInstance();
00202
00203
00204
00205 $myUtilsPic->safePictureDelete($this->oxcategories__oxthumb->value, $myConfig->getPictureDir(false).'/0', 'oxcategories', 'oxthumb' );
00206
00207 $myUtilsPic->safePictureDelete($this->oxcategories__oxicon->value, $myConfig->getPictureDir(false).'/icon', 'oxcategories', 'oxicon' );
00208
00209 $myUtilsPic->safePictureDelete($this->oxcategories__oxpromoicon->value, $myConfig->getPictureDir(false).'/icon', 'oxcategories', 'oxpromoicon' );
00210
00211 $sAdd = " and oxshopid = '" . $this->getShopId() . "' ";
00212
00213 $oDB->execute( "UPDATE oxcategories SET OXLEFT = OXLEFT - 2
00214 WHERE OXROOTID = ".$oDB->quote($this->oxcategories__oxrootid->value)."
00215 AND OXLEFT > ".((int) $this->oxcategories__oxleft->value).$sAdd );
00216
00217 $oDB->execute( "UPDATE oxcategories SET OXRIGHT = OXRIGHT - 2
00218 WHERE OXROOTID = ".$oDB->quote($this->oxcategories__oxrootid->value)."
00219 AND OXRIGHT > ".((int) $this->oxcategories__oxright->value).$sAdd );
00220
00221
00222 $blRet = parent::delete();
00223
00224 $sOxidQuoted = $oDB->quote($this->oxcategories__oxid->value);
00225
00226 $oDB->execute( "delete from oxobject2category where oxobject2category.oxcatnid=$sOxidQuoted ");
00227
00228
00229 $oDB->execute( "delete from oxcategory2attribute where oxcategory2attribute.oxobjectid=$sOxidQuoted ");
00230
00231
00232
00233 $oDB->execute( "delete from oxobject2delivery where oxobject2delivery.oxobjectid=$sOxidQuoted ");
00234
00235 $oDB->execute( "delete from oxobject2discount where oxobject2discount.oxobjectid=$sOxidQuoted ");
00236
00237 oxSeoEncoderCategory::getInstance()->onDeleteCategory($this);
00238 }
00239 return $blRet;
00240 }
00241
00247 public function getSubCats()
00248 {
00249 return $this->_aSubCats;
00250 }
00251
00259 public function getSubCat($sKey)
00260 {
00261 return $this->_aSubCats[$sKey];
00262 }
00263
00271 public function setSubCats( $aCats )
00272 {
00273 $this->_aSubCats = $aCats;
00274
00275 foreach ( $aCats as $oCat ) {
00276 if ( $oCat->getIsVisible() ) {
00277 $this->setHasVisibleSubCats( true );
00278 }
00279 }
00280 }
00281
00291 public function setSubCat($oCat, $sKey=null, $blSkipSorting = false)
00292 {
00293 if ( $sKey ) {
00294 $this->_aSubCats[$sKey] = $oCat;
00295 } else {
00296 $this->_aSubCats[] = $oCat;
00297 }
00298
00299
00300 $oCat->setParentCategory( $this );
00301
00302 if ( $oCat->getIsVisible() ) {
00303 $this->setHasVisibleSubCats( true );
00304 }
00305 }
00306
00314 public function sortSubCats()
00315 {
00316 if ( count( $this->_aIds ) > 0 ) {
00317 uasort($this->_aSubCats, array( $this, 'cmpCat' ) );
00318 }
00319 }
00320
00332 public function cmpCat( $a,$b )
00333 {
00334 if ( count( $this->_aIds ) == 0 ) {
00335 return;
00336 }
00337
00338 $sNumA = $this->_aIds[$a->oxcategories__oxid->value];
00339 $sNumB = $this->_aIds[$b->oxcategories__oxid->value];
00340
00341 if ($sNumA < $sNumB ) {
00342 return -1;
00343 } if ( $sNumA == $sNumB) {
00344 return 0;
00345 }
00346 return 1;
00347 }
00348
00358 public function setSortingIds( $aSorIds )
00359 {
00360 $this->_aIds = $aSorIds;
00361 }
00362
00368 public function getContentCats()
00369 {
00370 return $this->_aContentCats;
00371 }
00372
00380 public function setContentCats( $aContent )
00381 {
00382 $this->_aContentCats = $aContent;
00383 }
00384
00393 public function setContentCat( $oContent, $sKey=null )
00394 {
00395 if ( $sKey ) {
00396 $this->_aContentCats[$sKey] = $oContent;
00397 } else {
00398 $this->_aContentCats[] = $oContent;
00399 }
00400 }
00401
00407 public function getNrOfArticles()
00408 {
00409 $myConfig = $this->getConfig();
00410
00411 if ( !isset($this->_iNrOfArticles)
00412 && !$this->isAdmin()
00413 && (
00414 $myConfig->getConfigParam( 'bl_perfShowActionCatArticleCnt' )
00415 || $myConfig->getConfigParam('blDontShowEmptyCategories')
00416 ) ) {
00417
00418 if ( $this->isPriceCategory() ) {
00419 $this->_iNrOfArticles = oxUtilsCount::getInstance()->getPriceCatArticleCount( $this->getId(), $this->oxcategories__oxpricefrom->value, $this->oxcategories__oxpriceto->value );
00420 } else {
00421 $this->_iNrOfArticles = oxUtilsCount::getInstance()->getCatArticleCount( $this->getId() );
00422 }
00423 }
00424
00425 return (int)$this->_iNrOfArticles;
00426 }
00427
00435 public function setNrOfArticles( $iNum )
00436 {
00437 $this->_iNrOfArticles = $iNum;
00438 }
00439
00445 public function getIsVisible()
00446 {
00447 if (!isset( $this->_blIsVisible ) ) {
00448
00449 if ( $this->getConfig()->getConfigParam( 'blDontShowEmptyCategories' ) ) {
00450 $blEmpty = ($this->getNrOfArticles() < 1) && !$this->getHasVisibleSubCats();
00451 } else {
00452 $blEmpty = false;
00453 }
00454
00455 $this->_blIsVisible = !($blEmpty || $this->oxcategories__oxhidden->value);
00456 }
00457
00458 return $this->_blIsVisible;
00459 }
00460
00468 public function setIsVisible( $blVisible )
00469 {
00470 $this->_blIsVisible = $blVisible;
00471 }
00472
00478 public function getPictureUrl()
00479 {
00480 if ( $this->_sDynImageDir === null ) {
00481 $sThisShop = $this->oxcategories__oxshopid->value;
00482 $this->_sDynImageDir = $this->getConfig()->getPictureUrl( null, false, null, null, $sThisShop);
00483 }
00484 return $this->_sDynImageDir;
00485 }
00486
00495 public function getBaseSeoLink( $iLang, $iPage = 0 )
00496 {
00497 $oEncoder = oxSeoEncoderCategory::getInstance();
00498 if ( !$iPage ) {
00499 return $oEncoder->getCategoryUrl( $this, $iLang );
00500 }
00501 return $oEncoder->getCategoryPageUrl( $this, $iPage, $iLang );
00502 }
00503
00511 public function getLink( $iLang = null )
00512 {
00513 if ( !oxUtils::getInstance()->seoIsActive() ||
00514 ( isset( $this->oxcategories__oxextlink ) && $this->oxcategories__oxextlink->value ) ) {
00515 return $this->getStdLink( $iLang );
00516 }
00517
00518 if ( $iLang === null ) {
00519 $iLang = $this->getLanguage();
00520 }
00521
00522 if ( !isset( $this->_aSeoUrls[$iLang] ) ) {
00523 $this->_aSeoUrls[$iLang] = $this->getBaseSeoLink( $iLang );
00524 }
00525 return $this->_aSeoUrls[$iLang];
00526 }
00527
00535 public function setLink( $sLink )
00536 {
00537 $iLang = $this->getLanguage();
00538 if ( oxUtils::getInstance()->seoIsActive() ) {
00539 $this->_aSeoUrls[$iLang] = $sLink;
00540 } else {
00541 $this->_aStdUrls[$iLang] = $sLink;
00542 }
00543 }
00544
00552 public function getSqlActiveSnippet( $blForceCoreTable = null )
00553 {
00554 $sQ = parent::getSqlActiveSnippet( $blForceCoreTable );
00555
00556 $sTable = $this->getViewName($blForceCoreTable);
00557 $sQ .= ( strlen( $sQ )? ' and ' : '' ) . " $sTable.oxhidden = '0' ";
00558
00559
00560 return "( $sQ ) ";
00561 }
00562
00572 public function getBaseStdLink( $iLang, $blAddId = true, $blFull = true )
00573 {
00574 if ( isset( $this->oxcategories__oxextlink ) && $this->oxcategories__oxextlink->value ) {
00575 return $this->oxcategories__oxextlink->value;
00576 }
00577
00578 $sUrl = '';
00579 if ( $blFull ) {
00580
00581 $sUrl = $this->getConfig()->getShopUrl( $iLang, false );
00582 }
00583
00584
00585 return $sUrl . "index.php?cl=alist" . ( $blAddId ? "&cnid=".$this->getId() : "" );
00586 }
00587
00596 public function getStdLink( $iLang = null, $aParams = array() )
00597 {
00598 if ( isset( $this->oxcategories__oxextlink ) && $this->oxcategories__oxextlink->value ) {
00599 return oxUtilsUrl::getInstance()->processUrl( $this->oxcategories__oxextlink->value, false );
00600 }
00601
00602 if ( $iLang === null ) {
00603 $iLang = $this->getLanguage();
00604 }
00605
00606 if ( !isset( $this->_aStdUrls[$iLang] ) ) {
00607 $this->_aStdUrls[$iLang] = $this->getBaseStdLink( $iLang );
00608 }
00609
00610 return oxUtilsUrl::getInstance()->processUrl( $this->_aStdUrls[$iLang], true, $aParams, $iLang );
00611 }
00612
00618 public function getExpanded()
00619 {
00620 if ( !isset( $this->_blExpanded ) ) {
00621 $myConfig = $this->getConfig();
00622 $this->_blExpanded = ( $myConfig->getConfigParam( 'blLoadFullTree' ) && !$myConfig->getConfigParam( 'blTopNaviLayout' ) );
00623 }
00624
00625 return $this->_blExpanded;
00626 }
00627
00635 public function setExpanded( $blExpanded )
00636 {
00637 $this->_blExpanded = $blExpanded;
00638 }
00639
00645 public function getHasSubCats()
00646 {
00647 if ( !isset( $this->_blHasSubCats ) ) {
00648 $this->_blHasSubCats = $this->oxcategories__oxright->value > $this->oxcategories__oxleft->value + 1 ;
00649 }
00650
00651 return $this->_blHasSubCats;
00652 }
00653
00659 public function getHasVisibleSubCats()
00660 {
00661 if ( !isset( $this->_blHasVisibleSubCats ) ) {
00662 $this->_blHasVisibleSubCats = false;
00663 }
00664
00665 return $this->_blHasVisibleSubCats;
00666 }
00667
00675 public function setHasVisibleSubCats( $blHasVisibleSubcats )
00676 {
00677 $this->_blHasVisibleSubCats = $blHasVisibleSubcats;
00678 }
00679
00685 public function getAttributes()
00686 {
00687 $sActCat = $this->getId();
00688
00689 $oAttrList = oxNew( "oxAttributeList" );
00690 $oAttrList->getCategoryAttributes( $sActCat, $this->getLanguage() );
00691
00692 return $oAttrList;
00693 }
00694
00702 public function getCatInLang( $oActCategory = null )
00703 {
00704 $oCategoryInDefaultLanguage= oxNew( "oxcategory" );
00705 if ( $this->isPriceCategory() ) {
00706
00707 $oCategoryInDefaultLanguage= oxNew( "oxcategory" );
00708 $oCategoryInDefaultLanguage->loadInLang( 0, $this->getId());
00709 } else {
00710 $oCategoryInDefaultLanguage= oxNew( "oxcategory" );
00711 $oCategoryInDefaultLanguage->loadInLang( 0, $oActCategory->getId());
00712 }
00713 return $oCategoryInDefaultLanguage;
00714 }
00715
00723 public function setParentCategory( $oCategory )
00724 {
00725 $this->_oParent = $oCategory;
00726 }
00727
00733 public function getParentCategory()
00734 {
00735 $oCat = null;
00736
00737
00738 if ( $this->oxcategories__oxparentid->value && $this->oxcategories__oxparentid->value != 'oxrootid' ) {
00739
00740
00741 if ( $this->_oParent ) {
00742 $oCat = $this->_oParent;
00743 } else {
00744 $oCat = oxNew( 'oxcategory' );
00745 if ( !$oCat->loadInLang( $this->getLanguage(), $this->oxcategories__oxparentid->value ) ) {
00746 $oCat = null;
00747 }
00748 }
00749 }
00750 return $oCat;
00751 }
00752
00760 public static function getRootId($sCategoryId)
00761 {
00762 if ( !isset( $sCategoryId ) ) {
00763 return;
00764 }
00765
00766 return oxDb::getDb()->getOne( 'select oxrootid from '.getViewName('oxcategories').' where oxid = ?', array( $sCategoryId ) );
00767 }
00768
00769
00777 public function assignViewableRecord($sSelect)
00778 {
00779 if ( $this->assignRecord( $sSelect ) ) {
00780 return true;
00781 }
00782
00783
00784 return false;
00785 }
00786
00792 protected function _insert()
00793 {
00794
00795
00796 if ( $this->oxcategories__oxparentid->value != "oxrootid") {
00797
00798
00799 $oParent = oxNew( "oxcategory" );
00800
00801 if ( !$oParent->load( $this->oxcategories__oxparentid->value) ) {
00802 return false;
00803 }
00804
00805 $sAdd = " and oxshopid = '" . $this->getShopId() . "' ";
00806
00807
00808 $oDB = oxDb::getDb();
00809 $oDB->execute( "UPDATE oxcategories SET OXLEFT = OXLEFT + 2
00810 WHERE OXROOTID = ".$oDB->quote($oParent->oxcategories__oxrootid->value)."
00811 AND OXLEFT > ".((int) $oParent->oxcategories__oxright->value)."
00812 AND OXRIGHT >= ".((int) $oParent->oxcategories__oxright->value).$sAdd);
00813
00814
00815 $oDB->execute( "UPDATE oxcategories SET OXRIGHT = OXRIGHT + 2
00816 WHERE OXROOTID = ".$oDB->quote($oParent->oxcategories__oxrootid->value)."
00817 AND OXRIGHT >= ".((int) $oParent->oxcategories__oxright->value).$sAdd );
00818
00819
00820
00821
00822
00823 if ( !$this->getId() ) {
00824 $this->setId();
00825 }
00826
00827 $this->oxcategories__oxrootid = new oxField($oParent->oxcategories__oxrootid->value, oxField::T_RAW);
00828 $this->oxcategories__oxleft = new oxField($oParent->oxcategories__oxright->value, oxField::T_RAW);
00829 $this->oxcategories__oxright = new oxField($oParent->oxcategories__oxright->value + 1, oxField::T_RAW);
00830 return parent::_insert();
00831 } else {
00832
00833 if ( !$this->getId() ) {
00834 $this->setId();
00835 }
00836
00837 $this->oxcategories__oxrootid = new oxField($this->getId(), oxField::T_RAW);
00838 $this->oxcategories__oxleft = new oxField(1, oxField::T_RAW);
00839 $this->oxcategories__oxright = new oxField(2, oxField::T_RAW);
00840 return parent::_insert();
00841 }
00842 }
00843
00849 protected function _update()
00850 {
00851
00852 $oDB = oxDb::getDb();
00853
00854 $sOldParentID = $oDB->getOne( "select oxparentid from oxcategories where oxid = '".$this->getId()."'");
00855
00856 if ( $this->_blIsSeoObject && $this->isAdmin() ) {
00857 oxSeoEncoderCategory::getInstance()->markRelatedAsExpired($this);
00858 }
00859
00860 $blRes = parent::_update();
00861
00862
00863
00864
00865
00866
00867
00868
00869
00870
00871 if ( $this->oxcategories__oxparentid->value != $sOldParentID) {
00872 $sOldParentLeft = $this->oxcategories__oxleft->value;
00873 $sOldParentRight = $this->oxcategories__oxright->value;
00874
00875 $iTreeSize = $sOldParentRight-$sOldParentLeft+1;
00876
00877 $sNewRootID = $oDB->getOne( "select oxrootid from oxcategories where oxid = ".$oDB->quote($this->oxcategories__oxparentid->value));
00878
00879
00880 if ( $sNewRootID == "") {
00881
00882 $sNewRootID = $this->getId();
00883 }
00884
00885 $sNewParentLeft = $oDB->getOne( "select oxleft from oxcategories where oxid = ".$oDB->quote($this->oxcategories__oxparentid->value));
00886
00887
00888
00889
00890
00891
00892 $iMoveAfter = $sNewParentLeft+1;
00893
00894
00895
00896 if ($sNewParentLeft > $sOldParentLeft && $sNewParentLeft < $sOldParentRight && $this->oxcategories__oxrootid->value == $sNewRootID) {
00897
00898
00899
00900 $sRestoreOld = "UPDATE oxcategories SET OXPARENTID = ".$oDB->quote($sOldParentID)." WHERE oxid = '".$this->getId()."'";
00901 $oDB->execute( $sRestoreOld );
00902 return false;
00903 }
00904
00905
00906 if ($sOldParentLeft > $iMoveAfter && $this->oxcategories__oxrootid->value == $sNewRootID) {
00907 $sOldParentLeft += $iTreeSize;
00908 $sOldParentRight += $iTreeSize;
00909 }
00910
00911 $iDelta = $iMoveAfter-$sOldParentLeft;
00912
00913
00914
00915 $sAddOld = " and oxshopid = '" . $this->getShopId() . "' and OXROOTID = ".$oDB->quote($this->oxcategories__oxrootid->value).";";
00916 $sAddNew = " and oxshopid = '" . $this->getShopId() . "' and OXROOTID = ".$oDB->quote($sNewRootID).";";
00917
00918
00919 $oDB->execute( "UPDATE oxcategories SET OXLEFT = (OXLEFT + ".$iTreeSize.") WHERE OXLEFT >= ".$iMoveAfter.$sAddNew );
00920 $oDB->execute( "UPDATE oxcategories SET OXRIGHT = (OXRIGHT + ".$iTreeSize.") WHERE OXRIGHT >= ".$iMoveAfter.$sAddNew );
00921
00922
00923 $sChangeRootID = "";
00924 if ($this->oxcategories__oxrootid->value != $sNewRootID) {
00925
00926 $sChangeRootID = ", OXROOTID=".$oDB->quote($sNewRootID);
00927 }
00928
00929
00930 $oDB->execute( "UPDATE oxcategories SET OXLEFT = (OXLEFT + ".$iDelta."), OXRIGHT = (OXRIGHT + ".$iDelta.") ".$sChangeRootID." WHERE OXLEFT >= ".$sOldParentLeft." AND OXRIGHT <= ".$sOldParentRight.$sAddOld );
00931
00932
00933
00934 $oDB->execute( "UPDATE oxcategories SET OXLEFT = (OXLEFT - ".$iTreeSize.") WHERE OXLEFT >= ".($sOldParentRight+1).$sAddOld );
00935 $oDB->execute( "UPDATE oxcategories SET OXRIGHT = (OXRIGHT - ".$iTreeSize.") WHERE OXRIGHT >= ".($sOldParentRight+1).$sAddOld );
00936
00937 }
00938
00939 if ( $blRes && $this->_blIsSeoObject && $this->isAdmin() ) {
00940 oxSeoEncoderCategory::getInstance()->markRelatedAsExpired($this);
00941 }
00942
00943 return $blRes;
00944 }
00945
00955 protected function _setFieldData( $sFieldName, $sValue, $iDataType = oxField::T_TEXT)
00956 {
00957
00958 if ($sFieldName[2] == 'l' || $sFieldName[2] == 'L' || (isset($sFieldName[16]) && ($sFieldName[16] == 'l' || $sFieldName[16] == 'L') ) ) {
00959 if ('oxlongdesc' === strtolower($sFieldName) || 'oxcategories__oxlongdesc' === strtolower($sFieldName)) {
00960 $iDataType = oxField::T_RAW;
00961 }
00962 }
00963 return parent::_setFieldData($sFieldName, $sValue, $iDataType);
00964 }
00965
00966
00972 public function getIconUrl()
00973 {
00974 return $this->getPictureUrlForType( $this->oxcategories__oxicon->value, 'icon');
00975 }
00976
00982 public function getThumbUrl()
00983 {
00984 return $this->getPictureUrlForType( $this->oxcategories__oxthumb->value, '0' );
00985 }
00986
00992 public function getPromotionIconUrl()
00993 {
00994 return $this->getPictureUrlForType( $this->oxcategories__oxpromoicon->value, 'icon');
00995 }
00996
01005 public function getPictureUrlForType($sPicName, $sPicType)
01006 {
01007
01008 if ( $sPicName ) {
01009 return $this->getPictureUrl() . $sPicType . '/' . $sPicName;
01010 } else {
01011 return false;
01012 }
01013 }
01014
01020 public function isTopCategory()
01021 {
01022 if ( $this->_blTopCategory == null ) {
01023 $this->_blTopCategory = $this->oxcategories__oxparentid->value == 'oxrootid';
01024 }
01025 return $this->_blTopCategory;
01026 }
01027
01033 public function isPriceCategory()
01034 {
01035 return (bool) ( $this->oxcategories__oxpricefrom->value || $this->oxcategories__oxpriceto->value );
01036 }
01037
01044 public function getLongDesc()
01045 {
01046 return oxUtilsView::getInstance()->parseThroughSmarty( $this->oxcategories__oxlongdesc->getRawValue(), $this->getId().$this->getLanguage() );
01047 }
01048 }