oxarticle.php

Go to the documentation of this file.
00001 <?php
00002 
00003 // defining supported link types
00004 define( 'OXARTICLE_LINKTYPE_CATEGORY', 0 );
00005 define( 'OXARTICLE_LINKTYPE_VENDOR', 1 );
00006 define( 'OXARTICLE_LINKTYPE_MANUFACTURER', 2 );
00007 define( 'OXARTICLE_LINKTYPE_PRICECATEGORY', 3 );
00008 define( 'OXARTICLE_LINKTYPE_TAG', 4 );
00009 define( 'OXARTICLE_LINKTYPE_RECOMM', 5 );
00010 
00017 class oxArticle extends oxI18n implements oxIArticle, oxIUrl
00018 {
00024     protected $_sClassName = 'oxarticle';
00025 
00031     protected $_blUseLazyLoading = true;
00032 
00038     protected $_sItemKey;
00039 
00045     protected $_blCalcPrice    = true;
00046 
00051     protected $_oPrice      = null;
00052 
00058     protected $_dArticleVat = null;
00059 
00065     protected $_aPersistParam  = null;
00066 
00072     protected $_blNotBuyable   = false;
00073 
00080     protected $_blLoadVariants = true;
00081 
00087     protected $_aVariants = null;
00088 
00094     protected $_aVariantsWithNotOrderables = null;
00095 
00104     protected $_blNotBuyableParent  = false;
00105 
00106 
00110     protected $_blHasVariants = false;
00111 
00115     protected $_blHasMdVariants = false;
00116 
00121     protected $_blIsOnComparisonList = false;
00122 
00127     protected $_oUser = null;
00128 
00134     protected $_blLoadPrice = true;
00135 
00139     protected $_blSkipAbPrice = false;
00140 
00147     protected $_fPricePerUnit = null;
00148 
00152     protected $_blLoadParentData = false;
00153 
00157     protected $_blSkipAssign = false;
00158 
00164     protected $_blSkipDiscounts = null;
00165 
00170     protected $_oAttributeList = null;
00171 
00172 
00178     protected $_blIsRangePrice = false;
00179 
00185     protected $_aMediaUrls = null;
00186 
00192     static protected $_aLoadedParents;
00193 
00199     static protected $_aSelList;
00200 
00206     protected $_aDispSelList;
00207 
00213     protected $_blIsSeoObject = true;
00214 
00220     protected $_oAmountPriceList = null;
00221 
00230     protected $_iLinkType = 0;
00231 
00237     protected $_aStdUrls = array();
00238 
00244     protected $_aSeoUrls = array();
00245 
00251     protected $_aSeoAddParams = array();
00252 
00258     protected $_aStdAddParams = array();
00259 
00265     protected $_sDynImageDir = null;
00266 
00272     protected $_sMoreDetailLink = null;
00273 
00279     protected $_sToBasketLink = null;
00280 
00286     protected $_iStockStatus = null;
00287 
00293     protected $_oTPrice = null;
00294 
00300     protected $_oAmountPriceInfo = null;
00301 
00307     protected $_dAmountPrice = null;
00308 
00314     protected static $_aArticleManufacturers = array();
00315 
00321     protected static $_aArticleVendors = array();
00322 
00328     protected static $_aArticleCats = array();
00329 
00335     protected $_aNonCopyParentFields = array('oxarticles__oxinsert',
00336                                              'oxarticles__oxtimestamp',
00337                                              'oxarticles__oxnid',
00338                                              'oxarticles__oxid',
00339                                              'oxarticles__oxparentid');
00340 
00346     protected $_aCopyParentField = array('oxarticles__oxnonmaterial',
00347                                          'oxarticles__oxfreeshipping',
00348                                          'oxarticles__oxremindactive',
00349                                          'oxarticles__oxisdownloadable');
00350 
00356     protected $_oMdVariants = null;
00357 
00363     protected $_oLongDesc = null;
00364 
00372     protected $_aVariantSelections = array();
00373 
00378     protected static $_aSelections = array();
00379 
00384     protected static $_aCategoryCache = null;
00385 
00390     protected static $_blHasAmountPrice = null;
00391 
00396     protected $_aArticleFiles = null;
00397 
00398 
00407     public function __construct($aParams = null)
00408     {
00409         if ( $aParams && is_array($aParams)) {
00410             foreach ( $aParams as $sParam => $mValue) {
00411                 $this->$sParam = $mValue;
00412             }
00413         }
00414         parent::__construct();
00415         $this->init( 'oxarticles' );
00416 
00417         $this->_blIsRangePrice = false;
00418     }
00419 
00428     public function __get($sName)
00429     {
00430         $myUtils = oxUtils::getInstance();
00431         // deprecated since 2011.03.10, should be used getLongDescription() / getLongDesc()
00432         if ( strpos( $sName, 'oxarticles__oxlongdesc' ) === 0 ) {
00433             return $this->getLongDescription();
00434         }
00435 
00436         $this->$sName = parent::__get($sName);
00437         if ( $this->$sName ) {
00438             // since the field could have been loaded via lazyloading
00439             $this->_assignParentFieldValue($sName);
00440         }
00441 
00442         return $this->$sName;
00443     }
00444 
00453     public function __set( $sName, $sValue )
00454     {
00455         // deprecated since 2011.03.14, should be used setArticleLongDesc()
00456         if ( strpos( $sName, 'oxarticles__oxlongdesc' ) === 0 ) {
00457             if ($this->_blEmployMultilanguage) {
00458                 $sValue = ( $sValue instanceof oxField ) ? $sValue->getRawValue() : $sValue;
00459                 $this->setArticleLongDesc( $sValue );
00460             } else {
00461                 $this->$sName = $sValue;
00462             }
00463         } else {
00464             parent::__set( $sName, $sValue );
00465         }
00466     }
00467 
00475     public function setId( $sId = null )
00476     {
00477         $sId = parent::setId( $sId );
00478 
00479         // TODO: in oxbase::setId make it to check if exists and update, not recreate, then delete this overload
00480         $this->oxarticles__oxnid = $this->oxarticles__oxid;
00481 
00482         return $sId;
00483     }
00484 
00494     public function getActiveCheckQuery( $blForceCoreTable = null )
00495     {
00496         $sTable = $this->getViewName( $blForceCoreTable );
00497 
00498         // check if article is still active
00499         $sQ = " $sTable.oxactive = 1 ";
00500 
00501         // enabled time range check ?
00502         if ( $this->getConfig()->getConfigParam( 'blUseTimeCheck' ) ) {
00503             $sDate = date( 'Y-m-d H:i:s', oxUtilsDate::getInstance()->getTime() );
00504             $sQ = "( $sQ or ( $sTable.oxactivefrom < '$sDate' and $sTable.oxactiveto > '$sDate' ) ) ";
00505         }
00506 
00507         return $sQ;
00508     }
00509 
00523     public function getStockCheckQuery( $blForceCoreTable = null )
00524     {
00525         $myConfig = $this->getConfig();
00526         $sTable = $this->getViewName( $blForceCoreTable );
00527 
00528         $sQ = "";
00529 
00530         //do not check for variants
00531         if ( $myConfig->getConfigParam( 'blUseStock' ) ) {
00532             $sQ = " and ( $sTable.oxstockflag != 2 or ( $sTable.oxstock + $sTable.oxvarstock ) > 0  ) ";
00533             //V #M513: When Parent article is not purchaseble, it's visibility should be displayed in shop only if any of Variants is available.
00534             if ( !$myConfig->getConfigParam( 'blVariantParentBuyable' ) ) {
00535                 $sTimeCheckQ = '';
00536                 if ( $myConfig->getConfigParam( 'blUseTimeCheck' ) ) {
00537                      $sDate = date( 'Y-m-d H:i:s', oxUtilsDate::getInstance()->getTime() );
00538                      $sTimeCheckQ = " or ( art.oxactivefrom < '$sDate' and art.oxactiveto > '$sDate' )";
00539                 }
00540                 $sQ = " $sQ and IF( $sTable.oxvarcount = 0, 1, ( select 1 from $sTable as art where art.oxparentid=$sTable.oxid and ( art.oxactive = 1 $sTimeCheckQ ) and ( art.oxstockflag != 2 or art.oxstock > 0 ) limit 1 ) ) ";
00541             }
00542         }
00543 
00544         return $sQ;
00545     }
00546 
00558     public function getVariantsQuery( $blRemoveNotOrderables, $blForceCoreTable = null  )
00559     {
00560         $sTable = $this->getViewName( $blForceCoreTable );
00561         $sQ = " and $sTable.oxparentid = '".$this->getId()."' ";
00562 
00563         //checking if variant is active and stock status
00564         if ( $this->getConfig()->getConfigParam( 'blUseStock' ) ) {
00565             $sQ .= " and ( $sTable.oxstock > 0 or ( $sTable.oxstock <= 0 and $sTable.oxstockflag != 2 ";
00566             if ( $blRemoveNotOrderables ) {
00567                 $sQ .= " and $sTable.oxstockflag != 3 ";
00568             }
00569             $sQ .= " ) ) ";
00570         }
00571 
00572         return $sQ;
00573     }
00574 
00582     public function getSqlActiveSnippet( $blForceCoreTable = null )
00583     {
00584         $myConfig = $this->getConfig();
00585 
00586         // check if article is still active
00587         $sQ = $this->getActiveCheckQuery( $blForceCoreTable );
00588 
00589         // stock and variants check
00590         $sQ .= $this->getStockCheckQuery( $blForceCoreTable );
00591 
00592 
00593         return "( $sQ ) ";
00594     }
00595 
00603     public function setSkipAssign($blSkipAssign)
00604     {
00605         $this->_blSkipAssign = $blSkipAssign;
00606     }
00607 
00613     public function disablePriceLoad()
00614     {
00615         $this->_blLoadPrice = false;
00616     }
00617 
00623     public function getItemKey()
00624     {
00625         return $this->_sItemKey;
00626     }
00627 
00635     public function setItemKey($sItemKey)
00636     {
00637         $this->_sItemKey = $sItemKey;
00638     }
00639 
00647     public function setNoVariantLoading( $blLoadVariants )
00648     {
00649         $this->_blLoadVariants = !$blLoadVariants;
00650     }
00651 
00657     public function isBuyable()
00658     {
00659         if ($this->_blNotBuyableParent) {
00660             return false;
00661         }
00662 
00663         return !$this->_blNotBuyable;
00664     }
00665 
00671     public function getPersParams()
00672     {
00673         return $this->_aPersistParam;
00674     }
00675 
00681     public function isOnComparisonList()
00682     {
00683         return $this->_blIsOnComparisonList;
00684     }
00685 
00693     public function setOnComparisonList( $blOnList )
00694     {
00695         $this->_blIsOnComparisonList = $blOnList;
00696     }
00697 
00705     public function setLoadParentData($blLoadParentData)
00706     {
00707         $this->_blLoadParentData = $blLoadParentData;
00708     }
00709 
00717     public function setSkipAbPrice( $blSkipAbPrice = null )
00718     {
00719         $this->_blSkipAbPrice = $blSkipAbPrice;
00720     }
00721 
00729     public function getSearchableFields()
00730     {
00731         $aSkipFields = array("oxblfixedprice", "oxicon", "oxvarselect", "oxamitemid", "oxamtaskid", "oxpixiexport", "oxpixiexported") ;
00732         $aFields = array_diff( array_keys($this->_aFieldNames), $aSkipFields );
00733 
00734         return $aFields;
00735     }
00736 
00737 
00745     public function isMultilingualField($sFieldName)
00746     {
00747         switch ($sFieldName) {
00748             case "oxlongdesc":
00749             case "oxtags":
00750                 return true;
00751         }
00752 
00753         return parent::isMultilingualField($sFieldName);
00754     }
00755 
00761     public function isVisible()
00762     {
00763 
00764         // admin preview mode
00765         if ( ( $blCanPreview = oxUtils::getInstance()->canPreview() ) !== null ) {
00766             return $blCanPreview;
00767         }
00768 
00769         // active ?
00770         $sNow = date('Y-m-d H:i:s');
00771         if ( !$this->oxarticles__oxactive->value &&
00772              (  $this->oxarticles__oxactivefrom->value > $sNow ||
00773                 $this->oxarticles__oxactiveto->value < $sNow
00774              )) {
00775             return false;
00776         }
00777 
00778         // stock flags
00779         if ( $this->getConfig()->getConfigParam( 'blUseStock' ) && $this->oxarticles__oxstockflag->value == 2) {
00780             $iOnStock = $this->oxarticles__oxstock->value + $this->oxarticles__oxvarstock->value;
00781             if ($this->getConfig()->getConfigParam( 'blPsBasketReservationEnabled' )) {
00782                 $iOnStock += $this->getSession()->getBasketReservations()->getReservedAmount($this->getId());
00783             }
00784             if ( $iOnStock <= 0 ) {
00785                 return false;
00786             }
00787         }
00788 
00789         return true;
00790     }
00791 
00800     public function assign( $aRecord)
00801     {
00802         startProfile('articleAssign');
00803 
00804 
00805         // load object from database
00806         parent::assign( $aRecord);
00807 
00808         $this->oxarticles__oxnid = $this->oxarticles__oxid;
00809 
00810         // check for simple article.
00811         if ($this->_blSkipAssign) {
00812             return;
00813         }
00814 
00815         $this->_assignParentFieldValues();
00816         $this->_assignNotBuyableParent();
00817 
00818         $this->_assignStock();
00819         startProfile('articleAssignPrices');
00820         $this->_assignPrices();
00821         stopProfile('articleAssignPrices');
00822         $this->_assignPersistentParam();
00823         $this->_assignDynImageDir();
00824         $this->_assignComparisonListFlag();
00825         $this->_assignAttributes();
00826 
00827 
00828         //$this->_seoAssign();
00829 
00830         stopProfile('articleAssign');
00831     }
00832 
00843     public function load( $oxID)
00844     {
00845         // A. #1325 resetting to avoid problems when reloading (details etc)
00846         $this->_blNotBuyableParent = false;
00847 
00848         $blRet = parent::load( $oxID);
00849 
00850         // convert date's to international format
00851         $this->oxarticles__oxinsert    = new oxField(oxUtilsDate::getInstance()->formatDBDate( $this->oxarticles__oxinsert->value));
00852         $this->oxarticles__oxtimestamp = new oxField(oxUtilsDate::getInstance()->formatDBDate( $this->oxarticles__oxtimestamp->value));
00853 
00854         return $blRet;
00855     }
00856 
00864     public function addToRatingAverage( $iRating )
00865     {
00866         $dOldRating = $this->oxarticles__oxrating->value;
00867         $dOldCnt    = $this->oxarticles__oxratingcnt->value;
00868         $this->oxarticles__oxrating->setValue(( $dOldRating * $dOldCnt + $iRating ) / ($dOldCnt + 1));
00869         $this->oxarticles__oxratingcnt->setValue($dOldCnt + 1);
00870         $dRating = ( $dOldRating * $dOldCnt + $iRating ) / ($dOldCnt + 1);
00871         $dRatingCnt = (int) ($dOldCnt + 1);
00872         // oxarticles.oxtimestamp = oxarticles.oxtimestamp to keep old timestamp value
00873         $oDb = oxDb::getDb();
00874         $oDb->execute( 'update oxarticles set oxarticles.oxrating = '.$dRating.',oxarticles.oxratingcnt = '.$dRatingCnt.', oxarticles.oxtimestamp = oxarticles.oxtimestamp where oxarticles.oxid = '.$oDb->quote( $this->getId() ) );
00875     }
00876 
00884     public function setRatingAverage( $iRating )
00885     {
00886          $this->oxarticles__oxrating = new oxField( $iRating );
00887     }
00888 
00896     public function setRatingCount( $iRatingCnt )
00897     {
00898          $this->oxarticles__oxratingcnt = new oxField( $iRatingCnt );
00899     }
00900 
00908     public function getArticleRatingAverage( $blIncludeVariants = false )
00909     {
00910         if ( !$blIncludeVariants ) {
00911             return round( $this->oxarticles__oxrating->value, 1);
00912         } else {
00913             $oRating = oxNew( 'oxRating' );
00914             return $oRating->getRatingAverage( $this->getId(), 'oxarticle', $this->_getVariantsIds() );
00915         }
00916     }
00917 
00925     public function getArticleRatingCount( $blIncludeVariants = false )
00926     {
00927         if ( !$blIncludeVariants ) {
00928             return $this->oxarticles__oxratingcnt->value;
00929         } else {
00930             $oRating = oxNew( 'oxRating' );
00931             return $oRating->getRatingCount( $this->getId(), 'oxarticle', $this->_getVariantsIds() );
00932         }
00933     }
00934 
00935 
00941     public function getReviews()
00942     {
00943         $aIds = array($this->getId());
00944 
00945         if ( $this->oxarticles__oxparentid->value ) {
00946             $aIds[] = $this->oxarticles__oxparentid->value;
00947         }
00948 
00949         // showing variant reviews ..
00950         if ( $this->getConfig()->getConfigParam( 'blShowVariantReviews' ) ) {
00951             $aAdd = $this->_getVariantsIds();
00952             if (is_array($aAdd)) {
00953                 $aIds = array_merge($aIds, $aAdd);
00954             }
00955         }
00956 
00957         $oReview = oxNew('oxreview');
00958         $oRevs = $oReview->loadList('oxarticle', $aIds);
00959 
00960         //if no review found, return null
00961         if ( $oRevs->count() < 1 ) {
00962             return null;
00963         }
00964 
00965         return $oRevs;
00966     }
00967 
00973     public function getCrossSelling()
00974     {
00975         $oCrosslist = oxNew( "oxarticlelist");
00976         $oCrosslist->loadArticleCrossSell($this->oxarticles__oxid->value);
00977         if ( $oCrosslist->count() ) {
00978             return $oCrosslist;
00979         }
00980     }
00981 
00987     public function getAccessoires()
00988     {
00989         $myConfig = $this->getConfig();
00990 
00991         // Performance
00992         if ( !$myConfig->getConfigParam( 'bl_perfLoadAccessoires' ) ) {
00993             return;
00994         }
00995 
00996         $oAcclist = oxNew( "oxarticlelist");
00997         $oAcclist->setSqlLimit( 0, $myConfig->getConfigParam( 'iNrofCrossellArticles' ));
00998         $oAcclist->loadArticleAccessoires($this->oxarticles__oxid->value);
00999 
01000         if ( $oAcclist->count()) {
01001             return $oAcclist;
01002         }
01003     }
01004 
01010     public function getSimilarProducts()
01011     {
01012         // Performance
01013         $myConfig = $this->getConfig();
01014         if ( !$myConfig->getConfigParam( 'bl_perfLoadSimilar' ) ) {
01015             return;
01016         }
01017 
01018         $sArticleTable = $this->getViewName();
01019 
01020         $sAttribs = '';
01021         $iCnt = 0;
01022         $this->_getAttribsString($sAttribs, $iCnt);
01023 
01024         if ( !$sAttribs) {
01025             return null;
01026         }
01027 
01028         $aList = $this->_getSimList($sAttribs, $iCnt);
01029 
01030         if ( count( $aList ) ) {
01031             uasort( $aList, 'cmpart');
01032 
01033             $sSearch = $this->_generateSimListSearchStr($sArticleTable, $aList);
01034 
01035             $oSimilarlist = oxNew( 'oxarticlelist' );
01036             $oSimilarlist->setSqlLimit( 0, $myConfig->getConfigParam( 'iNrofSimilarArticles' ));
01037             $oSimilarlist->selectString( $sSearch);
01038 
01039             return $oSimilarlist;
01040         }
01041     }
01042 
01048     public function getCustomerAlsoBoughtThisProducts()
01049     {
01050         // Performance
01051         $myConfig = $this->getConfig();
01052         if ( !$myConfig->getConfigParam( 'bl_perfLoadCustomerWhoBoughtThis' ) ) {
01053             return;
01054         }
01055 
01056         // selecting products that fits
01057         $sQ = $this->_generateSearchStrForCustomerBought();
01058 
01059         $oArticles = oxNew( 'oxarticlelist' );
01060         $oArticles->setSqlLimit( 0, $myConfig->getConfigParam( 'iNrofCustomerWhoArticles' ));
01061         $oArticles->selectString( $sQ );
01062         if ( $oArticles->count() ) {
01063             return $oArticles;
01064         }
01065     }
01066 
01073     public function loadAmountPriceInfo()
01074     {
01075         $myConfig = $this->getConfig();
01076         if ( !$myConfig->getConfigParam( 'bl_perfLoadPrice' ) || !$this->_blLoadPrice || !$this->_blCalcPrice || !$this->hasAmountPrice() ) {
01077             return array();
01078         }
01079 
01080         if ( $this->_oAmountPriceInfo === null ) {
01081             $this->_oAmountPriceInfo = array();
01082             if ( count( ( $oAmPriceList = $this->_getAmountPriceList() ) ) ) {
01083                 $this->_oAmountPriceInfo = $this->_fillAmountPriceList( $oAmPriceList );
01084             }
01085         }
01086         return $this->_oAmountPriceInfo;
01087     }
01088 
01096     public function getSelectLists($sKeyPrefix = null)
01097     {
01098         //#1468C - more then one article in basket with different selectlist...
01099         //optionall function parameter $sKeyPrefix added, used only in basket.php
01100         $sKey = $this->getId();
01101         if ( isset( $sKeyPrefix ) ) {
01102             $sKey = $sKeyPrefix.'__'.$this->getId();
01103         }
01104 
01105         if ( !isset( self::$_aSelList[$sKey] ) ) {
01106             $oDb = oxDb::getDb();
01107             $sSLViewName = getViewName( 'oxselectlist' );
01108 
01109             $sQ = "select {$sSLViewName}.* from oxobject2selectlist join {$sSLViewName} on $sSLViewName.oxid=oxobject2selectlist.oxselnid
01110                    where oxobject2selectlist.oxobjectid=%s order by oxobject2selectlist.oxsort";
01111 
01112             // all selectlists this article has
01113             $oLists = oxNew( 'oxlist' );
01114             $oLists->init( 'oxselectlist' );
01115             $oLists->selectString( sprintf( $sQ, $oDb->quote( $this->getId() ) ) );
01116 
01117             //#1104S if this is variant ant it has no selectlists, trying with parent
01118             if ( $oLists->count() == 0 && $this->oxarticles__oxparentid->value ) {
01119                 $oLists->selectString( sprintf( $sQ, $oDb->quote( $this->oxarticles__oxparentid->value ) ) );
01120             }
01121 
01122             $dVat = 0;
01123             if ( $this->getPrice() != null ) {
01124                 $dVat = $this->getPrice()->getVat();
01125             }
01126 
01127             $iCnt = 0;
01128             self::$_aSelList[$sKey] = array();
01129             foreach ( $oLists as $oSelectlist ) {
01130                 self::$_aSelList[$sKey][$iCnt] = $oSelectlist->getFieldList( $dVat );
01131                 self::$_aSelList[$sKey][$iCnt]['name'] = $oSelectlist->oxselectlist__oxtitle->value;
01132                 $iCnt++;
01133             }
01134         }
01135         return self::$_aSelList[$sKey];
01136     }
01137 
01145     protected function _hasAnyVariant( $blForceCoreTable = null )
01146     {
01147         $blHas = false;
01148         if ( ( $sId = $this->getId() ) ) {
01149             if ( $this->oxarticles__oxshopid->value == $this->getConfig()->getShopId() ) {
01150                 $blHas = (bool) $this->oxarticles__oxvarcount->value;
01151             } else {
01152                 $sArticleTable = $this->getViewName( $blForceCoreTable );
01153                 $blHas = (bool) oxDb::getDb()->getOne( "select 1 from $sArticleTable where oxparentid='{$sId}'" );
01154             }
01155 
01156         }
01157         return $blHas;
01158     }
01159 
01165     public function hasMdVariants()
01166     {
01167         return $this->_blHasMdVariants;
01168     }
01169 
01179     public function getVariantSelections( $aFilterIds = null, $sActVariantId = null, $iLimit = 0 )
01180     {
01181 
01182         $iLimit = (int) $iLimit;
01183         if ( !isset( $this->_aVariantSelections[$iLimit] ) ) {
01184             $this->_aVariantSelections[$iLimit] = false;
01185 
01186 
01187 
01188             if ( $this->oxarticles__oxvarcount->value ) {
01189 
01190                 $this->_aVariantSelections[$iLimit] = oxNew( "oxVariantHandler" )->buildVariantSelections( $this->oxarticles__oxvarname->getRawValue(), $this->getVariants(), $aFilterIds, $sActVariantId, $iLimit );
01191             }
01192         }
01193 
01194         return $this->_aVariantSelections[$iLimit];
01195     }
01196 
01205     public function getSelections( $iLimit = null, $aFilter = null )
01206     {
01207         $sId = $this->getId() . ( (int) $iLimit );
01208         if ( !array_key_exists( $sId, self::$_aSelections ) ) {
01209 
01210             $oDb = oxDb::getDb();
01211             $sSLViewName = getViewName( 'oxselectlist' );
01212 
01213             $sQ = "select {$sSLViewName}.* from oxobject2selectlist join {$sSLViewName} on $sSLViewName.oxid=oxobject2selectlist.oxselnid
01214                    where oxobject2selectlist.oxobjectid=%s order by oxobject2selectlist.oxsort";
01215 
01216             if ( ( $iLimit = (int) $iLimit ) ) {
01217                 $sQ .= " limit $iLimit ";
01218             }
01219 
01220             // vat value for price
01221             $dVat = 0;
01222             if ( ( $oPrice = $this->getPrice() ) != null ) {
01223                 $dVat = $oPrice->getVat();
01224             }
01225 
01226             // all selectlists this article has
01227             $oList = oxNew( 'oxlist' );
01228             $oList->init( 'oxselectlist' );
01229             $oList->getBaseObject()->setVat( $dVat );
01230             $oList->selectString( sprintf( $sQ, $oDb->quote( $this->getId() ) ) );
01231 
01232             //#1104S if this is variant and it has no selectlists, trying with parent
01233             if ( $oList->count() == 0 && $this->oxarticles__oxparentid->value ) {
01234                 $oList->selectString( sprintf( $sQ, $oDb->quote( $this->oxarticles__oxparentid->value ) ) );
01235             }
01236 
01237             self::$_aSelections[$sId] = $oList->count() ? $oList : false;
01238         }
01239 
01240         if ( self::$_aSelections[$sId] ) {
01241             // marking active from filter
01242             $aFilter = ( $aFilter === null ) ? oxConfig::getParameter( "sel" ) : $aFilter;
01243             if ( $aFilter ) {
01244                 $iSelIdx = 0;
01245                 foreach ( self::$_aSelections[$sId] as $oSelection ) {
01246                     if ( isset( $aFilter[$iSelIdx] ) ) {
01247                         $oSelection->setActiveSelectionByIndex( $aFilter[$iSelIdx] );
01248                     }
01249                     $iSelIdx++;
01250                 }
01251             }
01252         }
01253 
01254         return self::$_aSelections[$sId];
01255     }
01256 
01266     protected function _loadVariantList( $blSimple, $blRemoveNotOrderables = true, $blForceCoreTable = null )
01267     {
01268         $oVariants = array();
01269         if ( ( $sId = $this->getId() ) ) {
01270             //do not load me as a parent later
01271             self::$_aLoadedParents[$sId] = $this;
01272 
01273             $myConfig = $this->getConfig();
01274 
01275             if ( !$this->_blLoadVariants ||
01276                 ( !$this->isAdmin() && !$myConfig->getConfigParam( 'blLoadVariants') ) ||
01277                 ( !$this->isAdmin() && !$this->oxarticles__oxvarcount->value ) ) {
01278                 return $oVariants;
01279             }
01280 
01281             // cache
01282             $sCacheKey = $blSimple ? "simple" : "full";
01283             if ( $blRemoveNotOrderables ) {
01284                 if ( isset( $this->_aVariants[$sCacheKey] ) ) {
01285                    return $this->_aVariants[$sCacheKey];
01286                 } else {
01287                     $this->_aVariants[$sCacheKey] = & $oVariants;
01288                 }
01289             } elseif ( !$blRemoveNotOrderables ) {
01290                 if ( isset( $this->_aVariantsWithNotOrderables[$sCacheKey] ) ) {
01291                     return $this->_aVariantsWithNotOrderables[$sCacheKey];
01292                 } else {
01293                     $this->_aVariantsWithNotOrderables[$sCacheKey] = & $oVariants;
01294                 }
01295             }
01296 
01297             if ( ( $this->_blHasVariants = $this->_hasAnyVariant( $blForceCoreTable ) ) ) {
01298 
01299                 //load simple variants for lists
01300                 if ( $blSimple ) {
01301                     $oVariants = oxNew( 'oxsimplevariantlist' );
01302                     $oVariants->setParent( $this );
01303                 } else {
01304                     //loading variants
01305                     $oVariants = oxNew( 'oxarticlelist' );
01306                     $oVariants->getBaseObject()->modifyCacheKey( '_variants' );
01307                 }
01308 
01309                 startProfile("selectVariants");
01310                 $blUseCoreTable = (bool) $blForceCoreTable;
01311                 $oBaseObject = $oVariants->getBaseObject();
01312                 $oBaseObject->setLanguage( $this->getLanguage() );
01313 
01314 
01315                 $sArticleTable = $this->getViewName( $blUseCoreTable );
01316 
01317                 $sSelect = "select ".$oBaseObject->getSelectFields( $blUseCoreTable )." from $sArticleTable where " .
01318                            $this->getActiveCheckQuery( $blUseCoreTable ) .
01319                            $this->getVariantsQuery( $blRemoveNotOrderables, $blUseCoreTable ) .
01320                            " order by $sArticleTable.oxsort";
01321 
01322 
01323                 $oVariants->selectString( $sSelect );
01324 
01325                 //if this is multidimensional variants, make additional processing
01326                 if ( $myConfig->getConfigParam( 'blUseMultidimensionVariants' ) ) {
01327                     $oMdVariants = oxNew( "oxVariantHandler" );
01328                     $this->_blHasMdVariants = $oMdVariants->isMdVariant( $oVariants->current() );
01329                 }
01330                 stopProfile("selectVariants");
01331             }
01332 
01333             //if we have variants then depending on config option the parent may be non buyable
01334             if ( !$myConfig->getConfigParam( 'blVariantParentBuyable' ) && $this->_blHasVariants ) {
01335                 $this->_blNotBuyableParent = true;
01336             }
01337 
01338             //if we have variants, but all variants are incative means article may be non buyable (depends on config option)
01339             if ( !$myConfig->getConfigParam( 'blVariantParentBuyable' ) && count( $oVariants ) == 0 && $this->_blHasVariants ) {
01340                 $this->_blNotBuyable = true;
01341             }
01342         }
01343 
01344         return $oVariants;
01345     }
01346 
01355     public function getFullVariants( $blRemoveNotOrderables = true, $blForceCoreTable = null )
01356     {
01357         return $this->_loadVariantList( false, $blRemoveNotOrderables, $blForceCoreTable );
01358     }
01359 
01368     public function getVariants( $blRemoveNotOrderables = true, $blForceCoreTable = null  )
01369     {
01370         return $this->_loadVariantList( $this->_isInList(), $blRemoveNotOrderables, $blForceCoreTable );
01371     }
01372 
01378     public function getSimpleVariants()
01379     {
01380         if ( $this->oxarticles__oxvarcount->value) {
01381             return $this->getVariants();
01382         }
01383     }
01384 
01393     public function getAdminVariants( $sLanguage = null )
01394     {
01395         $oVariants = oxNew( 'oxarticlelist');
01396         if ( ( $sId = $this->getId() ) ) {
01397 
01398             $oBaseObj = $oVariants->getBaseObject();
01399 
01400             if ( is_null( $sLanguage ) ) {
01401                 $oBaseObj->setLanguage( oxLang::getInstance()->getBaseLanguage() );
01402             } else {
01403                 $oBaseObj->setLanguage( $sLanguage );
01404             }
01405 
01406             $sSql = "select * from ".$oBaseObj->getViewName()." where oxparentid = '{$sId}' order by oxsort ";
01407             $oVariants->selectString( $sSql );
01408 
01409             //if we have variants then depending on config option the parent may be non buyable
01410             if ( !$this->getConfig()->getConfigParam( 'blVariantParentBuyable' ) && ( $oVariants->count() > 0 ) ) {
01411                 //$this->blNotBuyable = true;
01412                 $this->_blNotBuyableParent = true;
01413             }
01414         }
01415 
01416         return $oVariants;
01417     }
01418 
01426     public function getCategory()
01427     {
01428         $oCategory = oxNew( 'oxcategory' );
01429         $oCategory->setLanguage( $this->getLanguage() );
01430 
01431         // variant handling
01432         $sOXID = $this->getId();
01433         if ( isset( $this->oxarticles__oxparentid->value ) && $this->oxarticles__oxparentid->value ) {
01434             $sOXID = $this->oxarticles__oxparentid->value;
01435         }
01436 
01437         if ( $sOXID ) {
01438             // if the oxcategory instance of this article is not cached
01439             if ( !isset( $this->_aCategoryCache[ $sOXID ] ) ) {
01440                 startPRofile( 'getCategory' );
01441                 $oStr = getStr();
01442                 $sWhere   = $oCategory->getSqlActiveSnippet();
01443                 $sSelect  = $this->_generateSearchStr( $sOXID );
01444                 $sSelect .= ( $oStr->strstr( $sSelect, 'where' )?' and ':' where ') . $sWhere . " order by oxobject2category.oxtime limit 1";
01445 
01446                 // category not found ?
01447                 if ( !$oCategory->assignRecord( $sSelect ) ) {
01448 
01449                     $sSelect  = $this->_generateSearchStr( $sOXID, true );
01450                     $sSelect .= ( $oStr->strstr( $sSelect, 'where' )?' and ':' where ') . $sWhere . " limit 1";
01451 
01452                     // looking for price category
01453                     if ( !$oCategory->assignRecord( $sSelect ) ) {
01454                         $oCategory = null;
01455                     }
01456                 }
01457                 // add the category instance to cache
01458                 $this->_aCategoryCache[ $sOXID ] = $oCategory;
01459                 stopPRofile( 'getCategory' );
01460             } else {
01461                // if the oxcategory instance is cached
01462                $oCategory = $this->_aCategoryCache[ $sOXID ];
01463             }
01464         }
01465 
01466         return $oCategory;
01467     }
01468 
01477     public function getCategoryIds( $blActCats = false, $blSkipCache = false )
01478     {
01479         $myConfig = $this->getConfig();
01480         if ( isset( self::$_aArticleCats[$this->getId()] ) && !$blSkipCache ) {
01481             return self::$_aArticleCats[$this->getId()];
01482         }
01483 
01484         // variant handling
01485         $sOXID = $this->getId();
01486         if (isset( $this->oxarticles__oxparentid->value) && $this->oxarticles__oxparentid->value) {
01487             $sOXID = $this->oxarticles__oxparentid->value;
01488         }
01489 
01490         // we do not use lists here as we dont need this overhead right now
01491         $sSql = $this->_getSelectCatIds( $sOXID, $blActCats );
01492         $oDb = oxDb::getDb( oxDb::FETCH_MODE_ASSOC );
01493         $rs = $oDb->select( $sSql );
01494 
01495 
01496         $aRet = array();
01497 
01498         if ($rs != false && $rs->recordCount() > 0) {
01499             while (!$rs->EOF) {
01500                 $aRet[] = $rs->fields['oxcatnid'];
01501                 $rs->moveNext();
01502             }
01503         }
01504 
01505         // adding price categories if such exists
01506         $sSql = $this->getSqlForPriceCategories();
01507         $oDb->setFetchMode( oxDb::FETCH_MODE_ASSOC );
01508         $rs = $oDb->select( $sSql );
01509 
01510         if ($rs != false && $rs->recordCount() > 0) {
01511             while (!$rs->EOF) {
01512 
01513                 if ( is_array( $rs->fields ) ) {
01514                    $rs->fields = array_change_key_case( $rs->fields, CASE_LOWER );
01515                 }
01516 
01517 
01518                 if ( !$aRet[$rs->fields['oxid']] ) {
01519                     $aRet[] = $rs->fields['oxid'];
01520                 }
01521                 $rs->moveNext();
01522             }
01523         }
01524 
01525         return self::$_aArticleCats[$this->getId()] = $aRet;
01526     }
01527 
01536     protected function _getSelectCatIds( $sOXID, $blActCats = false )
01537     {
01538         $sO2CView = $this->_getObjectViewName('oxobject2category');
01539         $sCatView = $this->_getObjectViewName('oxcategories');
01540         $sSelect =  "select oxobject2category.oxcatnid as oxcatnid from $sO2CView as oxobject2category left join $sCatView as oxcategories on oxcategories.oxid = oxobject2category.oxcatnid ";
01541         $sSelect .= 'where oxobject2category.oxobjectid='.oxDb::getDb()->quote($sOXID).' and oxcategories.oxid is not null and oxcategories.oxactive = 1 ';
01542         if ( $blActCats ) {
01543             $sSelect .= "and oxcategories.oxhidden = 0 and (select count(cats.oxid) from $sCatView as cats where cats.oxrootid = oxcategories.oxrootid and cats.oxleft < oxcategories.oxleft and cats.oxright > oxcategories.oxright and ( cats.oxhidden = 1 or cats.oxactive = 0 ) ) = 0 ";
01544         }
01545         $sSelect .= 'order by oxobject2category.oxtime ';
01546         return $sSelect;
01547     }
01548 
01558     public function getVendor( $blShopCheck = true )
01559     {
01560         if ( ( $sVendorId = $this->getVendorId() ) ) {
01561             $oVendor = oxNew( 'oxvendor' );
01562         } elseif ( !$blShopCheck && $this->oxarticles__oxvendorid->value ) {
01563                 $oVendor = oxNew( 'oxi18n' );
01564                 $oVendor->init('oxvendor');
01565                 $oVendor->setReadOnly( true );
01566             $sVendorId = $this->oxarticles__oxvendorid->value;
01567         }
01568         if ( $sVendorId && $oVendor->load( $sVendorId ) && $oVendor->oxvendor__oxactive->value ) {
01569 
01570             //@deprecated in v.4.5.7, since 2012-02-15; config option removed bug #0003385
01571             if ( !$this->getConfig()->getConfigParam( 'bl_perfLoadVendorTree' ) ) {
01572                 $oVendor->setReadOnly( true );
01573             }
01574             return $oVendor;
01575         }
01576         return null;
01577     }
01578 
01586     public function getVendorId( $blForceReload = false )
01587     {
01588         $sVendorId = false;
01589         if ( $this->oxarticles__oxvendorid->value ) {
01590                 $sVendorId = $this->oxarticles__oxvendorid->value;
01591 
01592         }
01593         return $sVendorId;
01594     }
01595 
01603     public function getManufacturerId( $blForceReload = false )
01604     {
01605         $sManufacturerId = false;
01606         if ( $this->oxarticles__oxmanufacturerid->value ) {
01607 
01608                 $sManufacturerId = $this->oxarticles__oxmanufacturerid->value;
01609 
01610         }
01611         return $sManufacturerId;
01612     }
01613 
01623     public function getManufacturer( $blShopCheck = true )
01624     {
01625             $oManufacturer = oxNew( 'oxmanufacturer' );;
01626         if ( !( $sManufacturerId = $this->getManufacturerId() ) &&
01627              !$blShopCheck && $this->oxarticles__oxmanufacturerid->value ) {
01628             $oManufacturer->setReadOnly( true );
01629             $sManufacturerId = $this->oxarticles__oxmanufacturerid->value;
01630         }
01631 
01632         if ( $sManufacturerId && $oManufacturer->load( $sManufacturerId ) ) {
01633             if ( !$this->getConfig()->getConfigParam( 'bl_perfLoadManufacturerTree' ) ) {
01634                 $oManufacturer->setReadOnly( true );
01635             }
01636             $oManufacturer = $oManufacturer->oxmanufacturers__oxactive->value ? $oManufacturer : null;
01637         } else {
01638             $oManufacturer = null;
01639         }
01640 
01641         return $oManufacturer;
01642     }
01643 
01651     public function inCategory( $sCatNid)
01652     {
01653         return in_array( $sCatNid, $this->getCategoryIds());
01654     }
01655 
01664     public function isAssignedToCategory( $sCatId )
01665     {
01666         // variant handling
01667         $sOXID = $this->getId();
01668         if ( isset( $this->oxarticles__oxparentid->value) && $this->oxarticles__oxparentid->value) {
01669             $sOXID = $this->oxarticles__oxparentid->value;
01670         }
01671 
01672         $oDb = oxDb::getDb();
01673         $sSelect = $this->_generateSelectCatStr( $sOXID, $sCatId);
01674         $sOXID = $oDb->getOne( $sSelect );
01675         // article is assigned to passed category!
01676         if ( isset( $sOXID) && $sOXID) {
01677             return true;
01678         }
01679 
01680         // maybe this category is price category ?
01681         if ( $this->getConfig()->getConfigParam( 'bl_perfLoadPrice' ) && $this->_blLoadPrice ) {
01682             $dPriceFromTo = $this->getPrice()->getBruttoPrice();
01683             if ( $dPriceFromTo > 0) {
01684                 $sSelect = $this->_generateSelectCatStr( $sOXID, $sCatId, $dPriceFromTo);
01685                 $sOXID = $oDb->getOne( $sSelect );
01686                 // article is assigned to passed category!
01687                 if ( isset( $sOXID) && $sOXID) {
01688                     return true;
01689                 }
01690             }
01691         }
01692         return false;
01693     }
01694 
01700     public function getTPrice()
01701     {
01702         if ( !$this->getConfig()->getConfigParam( 'bl_perfLoadPrice' ) || !$this->_blLoadPrice ) {
01703             return;
01704         }
01705         // return cached result, since oPrice is created ONLY in this function [or function of EQUAL level]
01706         if ( $this->_oTPrice !== null ) {
01707             return $this->_oTPrice;
01708         }
01709 
01710         $this->_oTPrice = oxNew( 'oxPrice' );
01711         $this->_oTPrice->setPrice( $this->oxarticles__oxtprice->value );
01712 
01713         $this->_applyVat( $this->_oTPrice, $this->getArticleVat() );
01714         $this->_applyCurrency( $this->_oTPrice );
01715 
01716         return $this->_oTPrice;
01717     }
01718 
01724     public function skipDiscounts()
01725     {
01726         // allready loaded skip discounts config
01727         if ( $this->_blSkipDiscounts !== null ) {
01728             return $this->_blSkipDiscounts;
01729         }
01730 
01731         if ( $this->oxarticles__oxskipdiscounts->value ) {
01732             return true;
01733         }
01734 
01735 
01736         $this->_blSkipDiscounts = false;
01737         if ( oxDiscountList::getInstance()->hasSkipDiscountCategories() ) {
01738 
01739             $oDb = oxDb::getDb();
01740             $sO2CView  = getViewName( 'oxobject2category', $this->getLanguage() );
01741             $sViewName = getViewName( 'oxcategories', $this->getLanguage() );
01742             $sSelect =  "select 1 from $sO2CView as $sO2CView left join {$sViewName} on {$sViewName}.oxid = $sO2CView.oxcatnid
01743                          where $sO2CView.oxobjectid=".$oDb->quote( $this->getId() )." and {$sViewName}.oxactive = 1 and {$sViewName}.oxskipdiscounts = '1' ";
01744             $this->_blSkipDiscounts = ( $oDb->getOne( $sSelect ) == 1 );
01745         }
01746 
01747         return $this->_blSkipDiscounts;
01748     }
01749 
01757     public function setPrice(oxPrice $oPrice)
01758     {
01759         $this->_oPrice = $oPrice;
01760     }
01761 
01770     public function getBasePrice( $dAmount = 1 )
01771     {
01772         // override this function if you want e.g. different prices
01773         // for diff. usergroups.
01774 
01775         // Performance
01776         $myConfig = $this->getConfig();
01777         if( !$myConfig->getConfigParam( 'bl_perfLoadPrice' ) || !$this->_blLoadPrice )
01778             return;
01779 
01780         // GroupPrice or DB price ajusted by AmountPrice
01781         $dPrice = $this->_getAmountPrice( $dAmount );
01782 
01783 
01784         return $dPrice;
01785     }
01786 
01794     public function getPrice( $dAmount = 1 )
01795     {
01796         $myConfig = $this->getConfig();
01797         // Performance
01798         if ( !$myConfig->getConfigParam( 'bl_perfLoadPrice' ) || !$this->_blLoadPrice ) {
01799             return;
01800         }
01801 
01802         // return cached result, since oPrice is created ONLY in this function [or function of EQUAL level]
01803         if ( $dAmount != 1 || $this->_oPrice === null ) {
01804             $oPrice = oxNew( 'oxPrice' );
01805 
01806             // get base
01807             $oPrice->setPrice( $this->getBasePrice( $dAmount ) );
01808 
01809             // price handling
01810             if ( !$this->_blCalcPrice && $dAmount == 1 ) {
01811                 return $this->_oPrice = $oPrice;
01812             }
01813 
01814             $this->_calculatePrice( $oPrice );
01815             if ( $dAmount != 1 ) {
01816                 return $oPrice;
01817             }
01818 
01819             $this->_oPrice = $oPrice;
01820         }
01821         return $this->_oPrice;
01822     }
01823 
01832     protected function _calculatePrice( $oPrice, $dVat = null )
01833     {
01834         // apply VAT only if configuration requires it
01835         if ( isset( $dVat ) || !$this->getConfig()->getConfigParam( 'bl_perfCalcVatOnlyForBasketOrder' ) ) {
01836             $this->_applyVAT( $oPrice, isset( $dVat ) ? $dVat : $this->getArticleVat() );
01837         }
01838 
01839         // apply currency
01840         $this->_applyCurrency( $oPrice );
01841         // apply discounts
01842         if ( !$this->skipDiscounts() ) {
01843             $oDiscountList = oxDiscountList::getInstance();
01844             $oDiscountList->applyDiscounts( $oPrice, $oDiscountList->getArticleDiscounts( $this, $this->getArticleUser() ) );
01845         }
01846 
01847         return $oPrice;
01848     }
01849 
01857     public function setArticleUser($oUser)
01858     {
01859         $this->_oUser = $oUser;
01860     }
01861 
01867     public function getArticleUser()
01868     {
01869         if ($this->_oUser) {
01870             return $this->_oUser;
01871         }
01872         return $this->getUser();
01873     }
01874 
01884     public function getBasketPrice( $dAmount, $aSelList, $oBasket )
01885     {
01886         $oUser = $oBasket->getBasketUser();
01887         $this->setArticleUser( $oUser );
01888 
01889         $oBasketPrice = oxNew( 'oxPrice' );
01890 
01891         // get base price
01892         $dBasePrice = $this->getBasePrice( $dAmount );
01893 
01894         // applying select list price
01895         $dBasePrice = $this->_modifySelectListPrice( $dBasePrice, $aSelList );
01896 
01897         // setting price
01898         $oBasketPrice->setPrice( $dBasePrice );
01899 
01900         $dVat = oxVatSelector::getInstance()->getBasketItemVat( $this, $oBasket );
01901         $this->_calculatePrice( $oBasketPrice, $dVat );
01902 
01903         // returning final price object
01904         return $oBasketPrice;
01905     }
01906 
01919     public function applyBasketDiscounts(oxPrice $oPrice, $aDiscounts, $dAmount = 1)
01920     {
01921         $oDiscountList = oxDiscountList::getInstance();
01922         return $oDiscountList->applyBasketDiscounts( $oPrice, $aDiscounts, $dAmount );
01923     }
01924 
01933     public function delete( $sOXID = null )
01934     {
01935         if ( !$sOXID ) {
01936             $sOXID = $this->getId();
01937         }
01938         if ( !$sOXID ) {
01939             return false;
01940         }
01941 
01942 
01943         // #2339 delete first variants before deleting parent product
01944         $this->_deleteVariantRecords( $sOXID );
01945         $this->load( $sOXID );
01946         $this->_deletePics();
01947         $this->_onChangeResetCounts( $sOXID, $this->oxarticles__oxvendorid->value, $this->oxarticles__oxmanufacturerid->value );
01948 
01949         // delete self
01950         parent::delete( $sOXID );
01951 
01952         $rs = $this->_deleteRecords( $sOXID );
01953 
01954         oxSeoEncoderArticle::getInstance()->onDeleteArticle($this);
01955 
01956         $this->onChange( ACTION_DELETE, $sOXID, $this->oxarticles__oxparentid->value );
01957 
01958         return $rs->EOF;
01959     }
01960 
01969     public function reduceStock($dAmount, $blAllowNegativeStock = false)
01970     {
01971         $this->beforeUpdate();
01972 
01973         $iStockCount = $this->oxarticles__oxstock->value - $dAmount;
01974         if (!$blAllowNegativeStock && ($iStockCount < 0)) {
01975             $dAmount += $iStockCount;
01976             $iStockCount = 0;
01977         }
01978         $this->oxarticles__oxstock = new oxField($iStockCount);
01979 
01980         $oDb = oxDb::getDb();
01981         $oDb->execute( 'update oxarticles set oxarticles.oxstock = '.$oDb->quote( $iStockCount ).' where oxarticles.oxid = '.$oDb->quote( $this->getId() ) );
01982         $this->onChange( ACTION_UPDATE_STOCK );
01983         return $dAmount;
01984     }
01985 
01994     public function updateSoldAmount( $dAmount = 0 )
01995     {
01996         if ( !$dAmount ) {
01997             return;
01998         }
01999 
02000         $this->beforeUpdate();
02001 
02002         // article is not variant - should be updated current amount
02003         if ( !$this->oxarticles__oxparentid->value ) {
02004             //updating by SQL query, due to wrong behaviour if saving article using not admin mode
02005             $dAmount = (double) $dAmount;
02006             $oDb = oxDb::getDb();
02007             $rs = $oDb->execute( "update oxarticles set oxarticles.oxsoldamount = oxarticles.oxsoldamount + $dAmount where oxarticles.oxid = ".$oDb->quote($this->oxarticles__oxid->value));
02008         } elseif ( $this->oxarticles__oxparentid->value) {
02009             // article is variant - should be updated this article parent amount
02010             $oUpdateArticle = oxNewArticle( $this->oxarticles__oxparentid->value );
02011             $oUpdateArticle->updateSoldAmount( $dAmount );
02012         }
02013 
02014         $this->onChange( ACTION_UPDATE );
02015 
02016         return $rs;
02017     }
02018 
02024     public function disableReminder()
02025     {
02026         $oDb = oxDb::getDb();
02027         return $oDb->execute( "update oxarticles set oxarticles.oxremindactive = 2 where oxarticles.oxid = ".$oDb->quote($this->oxarticles__oxid->value));
02028     }
02029 
02037     public function save()
02038     {
02039         // @deprecated since 20110821. folders are no more written, getters must be user for urls
02040         $this->oxarticles__oxthumb = new oxField( basename( $this->oxarticles__oxthumb->value ), oxField::T_RAW );
02041         $this->oxarticles__oxicon  = new oxField( basename( $this->oxarticles__oxicon->value ), oxField::T_RAW );
02042         $iPicCount = $this->getConfig()->getConfigParam( 'iPicCount' );
02043         for ( $i = 1; $i <= $iPicCount; $i++ ) {
02044             $sFieldName = 'oxarticles__oxpic' . $i;
02045             if ( isset( $this->$sFieldName ) ) {
02046                 $this->_setFieldData( $sFieldName, basename( $this->$sFieldName->value ), oxField::T_RAW );
02047             }
02048         }
02049         // @end deprecated
02050 
02051         if ( ( $blRet = parent::save() ) ) {
02052             // saving long descrition
02053             $this->_saveArtLongDesc();
02054         }
02055 
02056         return $blRet;
02057     }
02058 
02059 
02066     public function getPictureGallery()
02067     {
02068         $myConfig = $this->getConfig();
02069 
02070         //initialize
02071         $blMorePic = false;
02072         $aArtPics  = array();
02073         $aArtIcons = array();
02074         $iActPicId = 1;
02075         $sActPic = $this->getPictureUrl( $iActPicId );
02076 
02077         if ( oxConfig::getParameter( 'actpicid' ) ) {
02078             $iActPicId = oxConfig::getParameter('actpicid');
02079         }
02080 
02081         $oStr = getStr();
02082         $iCntr = 0;
02083         $iPicCount = $myConfig->getConfigParam( 'iPicCount' );
02084         $blCheckActivePicId = true;
02085 
02086         for ( $i = 1; $i <= $iPicCount; $i++) {
02087             $sPicVal = $this->getPictureUrl( $i );
02088             $sIcoVal = $this->getIconUrl( $i );
02089             if ( !$oStr->strstr($sIcoVal, 'nopic_ico.jpg') && !$oStr->strstr($sIcoVal, 'nopic.jpg') &&
02090                  !$oStr->strstr($sPicVal, 'nopic_ico.jpg') && !$oStr->strstr($sPicVal, 'nopic.jpg') ) {
02091                 if ($iCntr) {
02092                     $blMorePic = true;
02093                 }
02094                 $aArtIcons[$i]= $sIcoVal;
02095                 $aArtPics[$i]= $sPicVal;
02096                 $iCntr++;
02097 
02098                 if ($iActPicId == $i) {
02099                     $sActPic = $sPicVal;
02100                     $blCheckActivePicId = false;
02101                 }
02102 
02103             } else if ( $blCheckActivePicId && $iActPicId <= $i) {
02104                 // if picture is empty, setting active pic id to next
02105                 // picture
02106                 $iActPicId++;
02107             }
02108         }
02109 
02110         $blZoomPic  = false;
02111         $aZoomPics = array();
02112         $iZoomPicCount = $myConfig->getConfigParam( 'iPicCount' );
02113 
02114         for ( $j = 1,$c = 1; $j <= $iZoomPicCount; $j++) {
02115             $sVal = $this->getZoomPictureUrl($j);
02116 
02117             if ( $sVal && !$oStr->strstr($sVal, 'nopic.jpg')) {
02118                 $blZoomPic = true;
02119                 $aZoomPics[$c]['id'] = $c;
02120                 $aZoomPics[$c]['file'] = $sVal;
02121                 //anything is better than empty name, because <img src=""> calls shop once more = x2 SLOW.
02122                 if (!$sVal) {
02123                     $aZoomPics[$c]['file'] = "nopic.jpg";
02124                 }
02125                 $c++;
02126             }
02127         }
02128 
02129         $aPicGallery = array('ActPicID' => $iActPicId,
02130                              'ActPic' => $sActPic,
02131                              'MorePics' => $blMorePic,
02132                              'Pics' => $aArtPics,
02133                              'Icons' => $aArtIcons,
02134                              'ZoomPic' => $blZoomPic,
02135                              'ZoomPics' => $aZoomPics);
02136 
02137         return $aPicGallery;
02138     }
02139 
02153     public function onChange($sAction = null, $sOXID = null, $sParentID = null)
02154     {
02155         $myConfig = $this->getConfig();
02156 
02157         if (!isset($sOXID)) {
02158             if ( $this->getId()) {
02159                 $sOXID = $this->getId();
02160             }
02161             if (!isset ($sOXID)) {
02162                 $sOXID = $this->oxarticles__oxid->value;
02163             }
02164             if ($this->oxarticles__oxparentid->value) {
02165                 $sParentID = $this->oxarticles__oxparentid->value;
02166             }
02167         }
02168         if (!isset($sOXID)) {
02169             return;
02170         }
02171 
02172         //if (isset($sOXID) && !$myConfig->blVariantParentBuyable && $myConfig->blUseStock)
02173         if ( $myConfig->getConfigParam( 'blUseStock' ) ) {
02174             //if article has variants then updating oxvarstock field
02175             //getting parent id
02176             if (!isset($sParentID)) {
02177                 $oDb = oxDb::getDb();
02178                 $sQ = 'select oxparentid from oxarticles where oxid = '.$oDb->quote($sOXID);
02179                 $sParentID = $oDb->getOne( $sQ );
02180             }
02181             //if we have parent id then update stock
02182             if ($sParentID) {
02183                 $this->_onChangeUpdateStock($sParentID);
02184             }
02185         }
02186         //if we have parent id then update count
02187         //update count even if blUseStock is not active
02188         if ($sParentID) {
02189             $this->_onChangeUpdateVarCount($sParentID);
02190         }
02191 
02192         $sId = ( $sParentID ) ? $sParentID : $sOXID;
02193         $this->_onChangeUpdateMinVarPrice( $sId );
02194 
02195             // reseting articles count cache if stock has changed and some
02196             // articles goes offline (M:1448)
02197             if ( $sAction === ACTION_UPDATE_STOCK ) {
02198                 $this->_onChangeStockResetCount( $sOXID );
02199             }
02200 
02201     }
02202 
02209     public function getCustomVAT()
02210     {
02211         if ( isset($this->oxarticles__oxvat->value) ) {
02212             return $this->oxarticles__oxvat->value;
02213         }
02214     }
02215 
02224     public function checkForStock( $dAmount, $dArtStockAmount = 0 )
02225     {
02226         $myConfig = $this->getConfig();
02227         if ( !$myConfig->getConfigParam( 'blUseStock' ) ) {
02228             return true;
02229         }
02230 
02231         $oDb = oxDb::getDb( oxDb::FETCH_MODE_ASSOC );
02232         // fetching DB info as its up-to-date
02233         $sQ = 'select oxstock, oxstockflag from oxarticles where oxid = '.$oDb->quote( $this->getId() );
02234         $rs = $oDb->select( $sQ );
02235 
02236         $iOnStock   = 0;
02237         $iStockFlag = 0;
02238         if ( $rs !== false && $rs->recordCount() > 0 ) {
02239             $iOnStock   = $rs->fields['oxstock'] - $dArtStockAmount;
02240             $iStockFlag = $rs->fields['oxstockflag'];
02241 
02242             // dodger : fremdlager is also always considered as on stock
02243             if ( $iStockFlag == 1 || $iStockFlag == 4) {
02244                 return true;
02245             }
02246             if ( !$myConfig->getConfigParam( 'blAllowUnevenAmounts' ) ) {
02247                 $iOnStock = floor( $iOnStock );
02248             }
02249         }
02250         if ($this->getConfig()->getConfigParam( 'blPsBasketReservationEnabled' )) {
02251             $iOnStock += $this->getSession()->getBasketReservations()->getReservedAmount($this->getId());
02252         }
02253         if ( $iOnStock >= $dAmount ) {
02254             return true;
02255         } else {
02256             if ( $iOnStock > 0 ) {
02257                 return $iOnStock;
02258             } else {
02259                 $oEx = oxNew( 'oxArticleInputException' );
02260                 $oEx->setMessage( 'EXCEPTION_ARTICLE_ARTICELNOTBUYABLE' );
02261                 oxUtilsView::getInstance()->addErrorToDisplay( $oEx );
02262                 return false;
02263             }
02264         }
02265     }
02266 
02267 
02277     public function getArticleLongDesc( $sOxid = null )
02278     {
02279         return $this->getLongDescription();
02280     }
02281 
02287     public function getLongDescription()
02288     {
02289         if ( $this->_oLongDesc === null ) {
02290             // initializing
02291             $this->_oLongDesc = new oxField();
02292 
02293 
02294             // choosing which to get..
02295             $sOxid = $this->getId();
02296             $sViewName = getViewName( 'oxartextends', $this->getLanguage() );
02297 
02298             $oDb = oxDb::getDb();
02299             $sDbValue = $oDb->getOne( "select oxlongdesc from {$sViewName} where oxid = " . $oDb->quote( $sOxid ) );
02300 
02301             if ( $sDbValue != false ) {
02302                 $this->_oLongDesc->setValue( $sDbValue, oxField::T_RAW );
02303             } elseif ( $this->oxarticles__oxparentid->value ) {
02304                 if ( !$this->isAdmin() || $this->_blLoadParentData ) {
02305                     $this->_oLongDesc->setValue( $this->getParentArticle()->getLongDescription()->getRawValue(), oxField::T_RAW );
02306                 }
02307             }
02308         }
02309         return $this->_oLongDesc;
02310     }
02311 
02321     protected function _setLongDesc( $sDbValue )
02322     {
02323         $this->setArticleLongDesc( $sDbValue );
02324     }
02325 
02332     public function getLongDesc()
02333     {
02334         return oxUtilsView::getInstance()->parseThroughSmarty( $this->getLongDescription()->getRawValue(), $this->getId().$this->getLanguage() );
02335     }
02336 
02345     public function setArticleLongDesc( $sDesc, $sOrigValue = null )
02346     {
02347 
02348         // setting current value
02349         $this->_oLongDesc = new oxField( $sDesc, oxField::T_RAW );
02350 
02351         // setting original value?
02352         //deprecated since 2012-02-13 in v.4.5.7
02353         if ( $sOrigValue ) {
02354             $this->_oLongDesc->orignalValue = $sOrigValue;
02355         }
02356     }
02357 
02363     public function getAttributes()
02364     {
02365         if ( $this->_oAttributeList === null ) {
02366             $this->_oAttributeList = oxNew( 'oxattributelist' );
02367             $this->_oAttributeList->loadAttributes( $this->getId() );
02368         }
02369 
02370         return $this->_oAttributeList;
02371     }
02372 
02381     public function appendLink( $sAddParams, $iLang = null )
02382     {
02383         if ( $sAddParams ) {
02384             if ( $iLang === null ) {
02385                 $iLang = $this->getLanguage();
02386             }
02387 
02388             $this->_aSeoAddParams[$iLang]  = isset( $this->_aSeoAddParams[$iLang] ) ? $this->_aSeoAddParams[$iLang] . "&amp;" : "";
02389             $this->_aSeoAddParams[$iLang] .= $sAddParams;
02390         }
02391     }
02392 
02401     public function getBaseSeoLink( $iLang, $blMain = false )
02402     {
02403         $oEncoder = oxSeoEncoderArticle::getInstance();
02404         if ( !$blMain ) {
02405             return $oEncoder->getArticleUrl( $this, $iLang, $this->getLinkType() );
02406         }
02407         return $oEncoder->getArticleMainUrl( $this, $iLang );
02408     }
02409 
02418     public function getLink( $iLang = null, $blMain = false  )
02419     {
02420         if ( !oxUtils::getInstance()->seoIsActive() ) {
02421             return $this->getStdLink( $iLang );
02422         }
02423 
02424         if ( $iLang === null ) {
02425             $iLang = $this->getLanguage();
02426         }
02427 
02428         $iLinkType = $this->getLinkType();
02429         if ( !isset( $this->_aSeoUrls[$iLang][$iLinkType] ) ) {
02430             $this->_aSeoUrls[$iLang][$iLinkType] = $this->getBaseSeoLink( $iLang, $blMain );
02431         }
02432 
02433         $sUrl = $this->_aSeoUrls[$iLang][$iLinkType];
02434         if ( isset($this->_aSeoAddParams[$iLang])) {
02435             $sUrl .= ( ( strpos( $sUrl.$this->_aSeoAddParams[$iLang], '?' ) === false ) ? '?' : '&amp;' ).$this->_aSeoAddParams[$iLang];
02436         }
02437 
02438         return $sUrl;
02439     }
02440 
02449     public function getMainLink( $iLang = null )
02450     {
02451         return $this->getLink( $iLang, true );
02452     }
02453 
02461     public function setLinkType( $iType )
02462     {
02463         // resetting detaisl link, to force new
02464         $this->_sDetailLink = null;
02465 
02466         // setting link type
02467         $this->_iLinkType = (int) $iType;
02468     }
02469 
02475     public function getLinkType()
02476     {
02477         return $this->_iLinkType;
02478     }
02479 
02488     public function appendStdLink( $sAddParams, $iLang = null )
02489     {
02490         if ( $sAddParams ) {
02491             if ( $iLang === null ) {
02492                 $iLang = $this->getLanguage();
02493             }
02494 
02495             $this->_aStdAddParams[$iLang]  = isset( $this->_aStdAddParams[$iLang] ) ? $this->_aStdAddParams[$iLang] . "&amp;" : "";
02496             $this->_aStdAddParams[$iLang] .= $sAddParams;
02497         }
02498     }
02499 
02509     public function getBaseStdLink( $iLang, $blAddId = true, $blFull = true )
02510     {
02511         $sUrl = '';
02512         if ( $blFull ) {
02513             //always returns shop url, not admin
02514             $sUrl = $this->getConfig()->getShopUrl( $iLang, false );
02515         }
02516 
02517         $sUrl .= "index.php?cl=details" . ( $blAddId ? "&amp;anid=".$this->getId() : "" );
02518         return $sUrl . ( isset( $this->_aStdAddParams[$iLang] ) ? "&amp;". $this->_aStdAddParams[$iLang] : "" );
02519     }
02520 
02529     public function getStdLink( $iLang = null, $aParams = array() )
02530     {
02531         if ( $iLang === null ) {
02532             $iLang = $this->getLanguage();
02533         }
02534 
02535         if ( !isset( $this->_aStdUrls[$iLang] ) ) {
02536             $this->_aStdUrls[$iLang] = $this->getBaseStdLink( $iLang );
02537         }
02538 
02539         return oxUtilsUrl::getInstance()->processUrl( $this->_aStdUrls[$iLang], true, $aParams, $iLang );
02540     }
02541 
02549     public function getStdTagLink( $sTag )
02550     {
02551         $sStdTagLink = $this->getConfig()->getShopHomeURL( $this->getLanguage(), false );
02552         return $sStdTagLink . "cl=details&amp;anid=".$this->getId()."&amp;listtype=tag&amp;searchtag=".rawurlencode( $sTag );
02553     }
02554 
02560     public function getTags()
02561     {
02562         $oDb = oxDb::getDb();
02563         $sViewName = getViewName( "oxartextends", $this->getLanguage() );
02564         $sQ = "select oxtags from {$sViewName} where oxid = ".$oDb->quote( $this->getId() );
02565         $oTagCloud = oxNew('oxtagcloud');
02566         return $oTagCloud->trimTags( $oDb->getOne( $sQ ) );
02567     }
02568 
02576     public function saveTags($sTags)
02577     {
02578         //do not allow derived update
02579         if ( !$this->allowDerivedUpdate() ) {
02580             return false;
02581         }
02582 
02583 
02584         $oTagCloud = oxNew( 'oxtagcloud' );
02585         $oTagCloud->resetTagCache();
02586         $sTags = oxDb::getInstance()->escapeString( $oTagCloud->prepareTags( $sTags ) );
02587         $oDb = oxDb::getDb();
02588 
02589         $sTable = getLangTableName( 'oxartextends', $this->getLanguage() );
02590         $sLangSuffix = oxLang::getInstance()->getLanguageTag($this->getLanguage());
02591         $sQ = "insert into {$sTable} (oxid, oxtags$sLangSuffix) value (".$oDb->quote( $this->getId() ).", '{$sTags}')
02592                on duplicate key update oxtags$sLangSuffix = '{$sTags}'";
02593         return $oDb->execute( $sQ );
02594     }
02595 
02603     public function addTag($sTag)
02604     {
02605         $oDb = oxDb::getDb();
02606 
02607         $oTagCloud = oxNew('oxtagcloud');
02608         $oTagCloud->resetTagCache();
02609         $sTag = $oTagCloud->prepareTags($sTag);
02610         $sTagSeparator = $this->getConfig()->getConfigParam('sTagSeparator');
02611 
02612         $sTable = getLangTableName( 'oxartextends', $this->getLanguage() );
02613         $sLangSuffix = oxLang::getInstance()->getLanguageTag($this->getLanguage());
02614         if ( $oDb->getOne( "select {$sTable}.OXTAGS$sLangSuffix from {$sTable} where {$sTable}.OXID = ".$oDb->quote( $this->getId() ) ) ) {
02615             $sTailTag = $sTagSeparator . $sTag;
02616         } else {
02617             $sTailTag = $sTag;
02618         }
02619 
02620         $sTag = oxDb::getInstance()->escapeString($sTag);
02621         $sTailTag = oxDb::getInstance()->escapeString($sTailTag);
02622 
02623         $sTag = oxDb::getInstance()->escapeString($sTag);
02624         $sTailTag = oxDb::getInstance()->escapeString($sTailTag);
02625 
02626         $sQ = "insert into {$sTable} ( {$sTable}.OXID, {$sTable}.OXTAGS$sLangSuffix) values (".$oDb->quote( $this->getId() ).", '{$sTag}')
02627                        ON DUPLICATE KEY update {$sTable}.OXTAGS$sLangSuffix = CONCAT(TRIM({$sTable}.OXTAGS$sLangSuffix), '$sTailTag') ";
02628 
02629         return $oDb->execute( $sQ );
02630     }
02631 
02637     public function getMediaUrls()
02638     {
02639         if ( $this->_aMediaUrls === null ) {
02640             $this->_aMediaUrls = oxNew("oxlist");
02641             $this->_aMediaUrls->init("oxmediaurl");
02642             $this->_aMediaUrls->getBaseObject()->setLanguage( $this->getLanguage() );
02643 
02644             $sViewName = getViewName( "oxmediaurls", $this->getLanguage() );
02645             $sQ = "select * from {$sViewName} where oxobjectid = '".$this->getId()."'";
02646             $this->_aMediaUrls->selectString($sQ);
02647         }
02648         return $this->_aMediaUrls;
02649     }
02650 
02656     public function getDynImageDir()
02657     {
02658         return $this->_sDynImageDir;
02659     }
02660 
02666     public function getDispSelList()
02667     {
02668         if ($this->_aDispSelList === null) {
02669             if ( $this->getConfig()->getConfigParam( 'bl_perfLoadSelectLists' ) && $this->getConfig()->getConfigParam( 'bl_perfLoadSelectListsInAList' ) ) {
02670                 $this->_aDispSelList = $this->getSelectLists();
02671             }
02672         }
02673         return $this->_aDispSelList;
02674     }
02675 
02681     public function getMoreDetailLink()
02682     {
02683         if ( $this->_sMoreDetailLink == null ) {
02684 
02685             // and assign special article values
02686             $this->_sMoreDetailLink = $this->getConfig()->getShopHomeURL() . 'cl=moredetails';
02687 
02688             // not always it is okey, as not all the time active category is the same as primary article cat.
02689             if ( $sActCat = oxConfig::getParameter( 'cnid' ) ) {
02690                 $this->_sMoreDetailLink .= '&amp;cnid='.$sActCat;
02691             }
02692             $this->_sMoreDetailLink .= '&amp;anid='.$this->getId();
02693             $this->_sMoreDetailLink = $this->_sMoreDetailLink;
02694         }
02695 
02696         return $this->_sMoreDetailLink;
02697     }
02698 
02704     public function getToBasketLink()
02705     {
02706         if ( $this->_sToBasketLink == null ) {
02707             $myConfig = $this->getConfig();
02708 
02709             if ( oxUtils::getInstance()->isSearchEngine() ) {
02710                 $this->_sToBasketLink = $this->getLink();
02711             } else {
02712                 // and assign special article values
02713                 $this->_sToBasketLink = $myConfig->getShopHomeURL();
02714 
02715                 // override some classes as these should never showup
02716                 $sActClass = oxConfig::getParameter( 'cl' );
02717                 if ( $sActClass == 'thankyou') {
02718                     $sActClass = 'basket';
02719                 }
02720                 $this->_sToBasketLink .= 'cl='.$sActClass;
02721 
02722                 // this is not very correct
02723                 if ( $sActCat = oxConfig::getParameter( 'cnid' ) ) {
02724                     $this->_sToBasketLink .= '&amp;cnid='.$sActCat;
02725                 }
02726 
02727                 $this->_sToBasketLink .= '&amp;fnc=tobasket&amp;aid='.$this->getId().'&amp;anid='.$this->getId();
02728 
02729                 if ( $sTpl = basename( oxConfig::getParameter( 'tpl' ) ) ) {
02730                     $this->_sToBasketLink .= '&amp;tpl='.$sTpl;
02731                 }
02732             }
02733         }
02734 
02735         return $this->_sToBasketLink;
02736     }
02737 
02743     public function getStockStatus()
02744     {
02745         return $this->_iStockStatus;
02746     }
02747 
02753     public function getDeliveryDate()
02754     {
02755         if ( $this->oxarticles__oxdelivery->value != '0000-00-00') {
02756             return oxUtilsDate::getInstance()->formatDBDate( $this->oxarticles__oxdelivery->value);
02757         }
02758         return false;
02759     }
02760 
02766     public function getFTPrice()
02767     {
02768         if ( $oPrice = $this->getTPrice() ) {
02769             if ( $oPrice->getBruttoPrice() ) {
02770                 return oxLang::getInstance()->formatCurrency( oxUtils::getInstance()->fRound($oPrice->getBruttoPrice()));
02771             }
02772         }
02773     }
02774 
02780     public function getFPrice()
02781     {
02782         if ( $oPrice = $this->getPrice() ) {
02783             return $this->getPriceFromPrefix().oxLang::getInstance()->formatCurrency( $oPrice->getBruttoPrice() );
02784         }
02785     }
02786 
02792     public function getFNetPrice()
02793     {
02794         if ( $oPrice = $this->getPrice() ) {
02795             return oxLang::getInstance()->formatCurrency( $oPrice->getNettoPrice() );
02796         }
02797     }
02798 
02804     public function getPricePerUnit()
02805     {
02806         return $this->_fPricePerUnit;
02807     }
02808 
02814     public function isParentNotBuyable()
02815     {
02816         return $this->_blNotBuyableParent;
02817     }
02818 
02824     public function isNotBuyable()
02825     {
02826         return $this->_blNotBuyable;
02827     }
02828 
02836     public function setBuyableState( $blBuyable = false )
02837     {
02838         $this->_blNotBuyable = !$blBuyable;
02839     }
02840 
02849     public function getVariantList()
02850     {
02851         return $this->getVariants();
02852     }
02853 
02861     public function setSelectlist( $aSelList )
02862     {
02863         $this->_aDispSelList = $aSelList;
02864     }
02865 
02873     public function getPictureUrl( $iIndex = 1 )
02874     {
02875         if ( $iIndex ) {
02876             $sImgName = false;
02877             if ( !$this->_isFieldEmpty( "oxarticles__oxpic".$iIndex ) ) {
02878                 $sImgName = basename( $this->{"oxarticles__oxpic$iIndex"}->value );
02879             }
02880 
02881             $sSize = $this->getConfig()->getConfigParam( 'aDetailImageSizes' );
02882             return oxPictureHandler::getInstance()->getProductPicUrl( "product/{$iIndex}/", $sImgName, $sSize, 'oxpic'.$iIndex );
02883         }
02884     }
02885 
02894     public function getIconUrl( $iIndex = 0 )
02895     {
02896         $sImgName = false;
02897         $sDirname = "product/1/";
02898         if ( $iIndex && !$this->_isFieldEmpty( "oxarticles__oxpic{$iIndex}" ) ) {
02899             $sImgName = basename( $this->{"oxarticles__oxpic$iIndex"}->value );
02900             $sDirname = "product/{$iIndex}/";
02901         } elseif ( !$this->_isFieldEmpty( "oxarticles__oxicon" ) ) {
02902             $sImgName = basename( $this->oxarticles__oxicon->value );
02903             $sDirname = "product/icon/";
02904         } elseif ( !$this->_isFieldEmpty( "oxarticles__oxpic1" ) ) {
02905             $sImgName = basename( $this->oxarticles__oxpic1->value );
02906         }
02907 
02908         $sSize = $this->getConfig()->getConfigParam( 'sIconsize' );
02909         return oxPictureHandler::getInstance()->getProductPicUrl( $sDirname, $sImgName, $sSize, $iIndex );
02910     }
02911 
02919     public function getThumbnailUrl( $bSsl = null )
02920     {
02921         $sImgName = false;
02922         $sDirname = "product/1/";
02923         if ( !$this->_isFieldEmpty( "oxarticles__oxthumb" ) ) {
02924             $sImgName = basename( $this->oxarticles__oxthumb->value );
02925             $sDirname = "product/thumb/";
02926         } elseif ( !$this->_isFieldEmpty( "oxarticles__oxpic1" ) ) {
02927             $sImgName = basename( $this->oxarticles__oxpic1->value );
02928         }
02929 
02930         $sSize = $this->getConfig()->getConfigParam( 'sThumbnailsize' );
02931         return oxPictureHandler::getInstance()->getProductPicUrl( $sDirname, $sImgName, $sSize, 0, $bSsl );
02932     }
02933 
02941     public function getZoomPictureUrl( $iIndex = '' )
02942     {
02943         $iIndex = (int) $iIndex;
02944         if ( $iIndex > 0 && !$this->_isFieldEmpty( "oxarticles__oxpic".$iIndex ) ) {
02945             $sImgName = basename( $this->{"oxarticles__oxpic".$iIndex}->value );
02946             $sSize = $this->getConfig()->getConfigParam( "sZoomImageSize" );
02947             return oxPictureHandler::getInstance()->getProductPicUrl( "product/{$iIndex}/", $sImgName, $sSize, 'oxpic'.$iIndex );
02948         }
02949     }
02950 
02956     public function getFileUrl()
02957     {
02958         return $this->getConfig()->getPictureUrl( 'media/' );
02959     }
02960 
02966     public function getPriceFromPrefix()
02967     {
02968         $sPricePrefix = '';
02969         if ( $this->_blIsRangePrice) {
02970             $sPricePrefix = oxLang::getInstance()->translateString('priceFrom').' ';
02971         }
02972 
02973         return $sPricePrefix;
02974     }
02975 
02981     protected function _saveArtLongDesc()
02982     {
02983         $myConfig = $this->getConfig();
02984         $sShopId = $myConfig->getShopID();
02985         if (in_array("oxlongdesc", $this->_aSkipSaveFields)) {
02986             return;
02987         }
02988 
02989         if ($this->_blEmployMultilanguage) {
02990             $sValue = $this->getLongDescription()->getRawValue();
02991             if ( $sValue !== null ) {
02992                 $oArtExt = oxNew('oxI18n');
02993                 $oArtExt->init('oxartextends');
02994                 $oArtExt->setLanguage((int) $this->getLanguage());
02995                 if (!$oArtExt->load($this->getId())) {
02996                     $oArtExt->setId($this->getId());
02997                 }
02998                 $oArtExt->oxartextends__oxlongdesc = new oxField($sValue, oxField::T_RAW);
02999                 $oArtExt->save();
03000             }
03001         } else {
03002             $oArtExt = oxNew('oxI18n');
03003             $oArtExt->setEnableMultilang(false);
03004             $oArtExt->init('oxartextends');
03005             $aObjFields = $oArtExt->_getAllFields(true);
03006             if (!$oArtExt->load($this->getId())) {
03007                 $oArtExt->setId($this->getId());
03008             }
03009 
03010             foreach ($aObjFields as $sKey => $sValue ) {
03011                 if ( preg_match('/^oxlongdesc(_(\d{1,2}))?$/', $sKey) ) {
03012                     $sField = $this->_getFieldLongName($sKey);
03013 
03014                     if (isset($this->$sField)) {
03015                         $sLongDesc = null;
03016                         if ($this->$sField instanceof oxField) {
03017                             $sLongDesc = $this->$sField->getRawValue();
03018                         } elseif (is_object($this->$sField)) {
03019                             $sLongDesc = $this->$sField->value;
03020                         }
03021                         if (isset($sLongDesc)) {
03022                             $sAEField = $oArtExt->_getFieldLongName($sKey);
03023                             $oArtExt->$sAEField = new oxField($sLongDesc, oxField::T_RAW);
03024                         }
03025                     }
03026                 }
03027             }
03028             $oArtExt->save();
03029         }
03030     }
03031 
03037     protected function _skipSaveFields()
03038     {
03039         $myConfig = $this->getConfig();
03040 
03041         $this->_aSkipSaveFields = array();
03042 
03043         $this->_aSkipSaveFields[] = 'oxtimestamp';
03044        // $this->_aSkipSaveFields[] = 'oxlongdesc';
03045         $this->_aSkipSaveFields[] = 'oxinsert';
03046 
03047         if ( !isset( $this->oxarticles__oxparentid->value) || $this->oxarticles__oxparentid->value == '') {
03048             $this->_aSkipSaveFields[] = 'oxparentid';
03049         }
03050 
03051     }
03052 
03062     protected function _mergeDiscounts( $aDiscounts, $aItemDiscounts)
03063     {
03064         foreach ( $aItemDiscounts as $sKey => $oDiscount ) {
03065             // add prices of the same discounts
03066             if ( array_key_exists ($sKey, $aDiscounts) ) {
03067                 $aDiscounts[$sKey]->dDiscount += $oDiscount->dDiscount;
03068             } else {
03069                 $aDiscounts[$sKey] = $oDiscount;
03070             }
03071         }
03072         return $aDiscounts;
03073     }
03074 
03080     protected function _getGroupPrice()
03081     {
03082         $dPrice = $this->oxarticles__oxprice->value;
03083 
03084         $oUser = $this->getArticleUser();
03085         if ( $oUser ) {
03086             if ( $oUser->inGroup( 'oxidpricea' ) ) {
03087                 $dPrice = $this->oxarticles__oxpricea->value;
03088             } elseif ( $oUser->inGroup( 'oxidpriceb' ) ) {
03089                 $dPrice = $this->oxarticles__oxpriceb->value;
03090             } elseif ( $oUser->inGroup( 'oxidpricec' ) ) {
03091                 $dPrice = $this->oxarticles__oxpricec->value;
03092             }
03093         }
03094 
03095         // #1437/1436C - added config option, and check for zero A,B,C price values
03096         if ( $this->getConfig()->getConfigParam( 'blOverrideZeroABCPrices' ) && (double) $dPrice == 0 ) {
03097             $dPrice = $this->oxarticles__oxprice->value;
03098         }
03099 
03100         return $dPrice;
03101     }
03102 
03111     protected function _getAmountPrice($dAmount = 1)
03112     {
03113         $myConfig = $this->getConfig();
03114 
03115         startProfile( "_getAmountPrice" );
03116 
03117         $dPrice = $this->_getGroupPrice();
03118         $oAmtPrices = $this->_getAmountPriceList();
03119         foreach ($oAmtPrices as $oAmPrice) {
03120             if ($oAmPrice->oxprice2article__oxamount->value <= $dAmount
03121                     && $dAmount <= $oAmPrice->oxprice2article__oxamountto->value
03122                     && $dPrice > $oAmPrice->oxprice2article__oxaddabs->value ) {
03123                 $dPrice = $oAmPrice->oxprice2article__oxaddabs->value;
03124             }
03125         }
03126 
03127         stopProfile( "_getAmountPrice" );
03128         return $dPrice;
03129     }
03130 
03139     protected function _modifySelectListPrice( $dPrice, $aChosenList = null )
03140     {
03141         $myConfig = $this->getConfig();
03142         // #690
03143         if ( $myConfig->getConfigParam( 'bl_perfLoadSelectLists' ) && $myConfig->getConfigParam( 'bl_perfUseSelectlistPrice' ) ) {
03144 
03145             $aSelLists = $this->getSelectLists();
03146 
03147             foreach ( $aSelLists as $key => $aSel) {
03148                 if ( isset( $aChosenList[$key]) && isset($aSel[$aChosenList[$key]] ) ) {
03149                     $oSel = $aSel[$aChosenList[$key]];
03150                     if ( $oSel->priceUnit =='abs' ) {
03151                         $dPrice += $oSel->price;
03152                     } elseif ( $oSel->priceUnit =='%' ) {
03153                         $dPrice += oxPrice::percent( $dPrice, $oSel->price );
03154                     }
03155                 }
03156             }
03157         }
03158         return $dPrice;
03159     }
03160 
03161 
03169     protected function _fillAmountPriceList($oAmPriceList)
03170     {
03171         $myConfig = $this->getConfig();
03172         $myUtils  = oxUtils::getInstance();
03173 
03174         //modifying price
03175         $oCur = $myConfig->getActShopCurrencyObject();
03176 
03177         $oUser = $this->getArticleUser();
03178 
03179         $oDiscountList = oxDiscountList::getInstance();
03180         $aDiscountList = $oDiscountList->getArticleDiscounts( $this, $oUser );
03181 
03182         $oLowestPrice = null;
03183 
03184         $dBasePrice = $this->_getGroupPrice();
03185         $oLang = oxLang::getInstance();
03186 
03187         $dArticleVat = null;
03188         if ( !$myConfig->getConfigParam( 'bl_perfCalcVatOnlyForBasketOrder' ) ) {
03189             $dArticleVat = $this->getArticleVat();
03190         }
03191 
03192         // trying to find lowest price value
03193         foreach ($oAmPriceList as $sId => $oItem) {
03194             $oItemPrice = oxNew( 'oxprice' );
03195             if ( $oItem->oxprice2article__oxaddabs->value) {
03196                 $oItemPrice->setPrice( $oItem->oxprice2article__oxaddabs->value );
03197                 $oDiscountList->applyDiscounts( $oItemPrice, $aDiscountList );
03198                 $this->_applyCurrency( $oItemPrice, $oCur );
03199             } else {
03200                 $oItemPrice->setPrice( $dBasePrice );
03201                 $oItemPrice->subtractPercent( $oItem->oxprice2article__oxaddperc->value );
03202             }
03203 
03204             if (isset($dArticleVat)) {
03205                 $this->_applyVAT($oItemPrice, $dArticleVat);
03206             }
03207 
03208             if (!$oLowestPrice) {
03209                 $oLowestPrice = $oItemPrice;
03210             } elseif ($oLowestPrice->getBruttoPrice() > $oItemPrice->getBruttoPrice()) {
03211                 $oLowestPrice = $oItemPrice;
03212             }
03213 
03214             $oAmPriceList[$sId]->oxprice2article__oxaddabs = new oxField( $oLang->formatCurrency( $myUtils->fRound( $oItemPrice->getBruttoPrice(), $oCur ) ) );
03215             $oAmPriceList[$sId]->fnetprice  = $oLang->formatCurrency( $myUtils->fRound($oItemPrice->getNettoPrice(), $oCur ) );
03216             $oAmPriceList[$sId]->fbrutprice = $oLang->formatCurrency( $myUtils->fRound($oItemPrice->getBruttoPrice(), $oCur ) );
03217         }
03218 
03219         $this->_dAmountPrice = $myUtils->fRound( $oLowestPrice->getBruttoPrice() );
03220         return $oAmPriceList;
03221     }
03222 
03228     protected function _getVariantsIds()
03229     {
03230         $aSelect = array();
03231         if ( ( $sId = $this->getId() ) ) {
03232             $oDb = oxDb::getDb( oxDb::FETCH_MODE_ASSOC );
03233             $sQ = "select oxid from " . $this->getViewName( true ) . " where oxparentid = ".$oDb->quote( $sId )." and " .
03234                    $this->getSqlActiveSnippet( true ) . " order by oxsort";
03235             $oRs = $oDb->select( $sQ );
03236             if ( $oRs != false && $oRs->recordCount() > 0 ) {
03237                 while (!$oRs->EOF) {
03238                     $aSelect[] = reset( $oRs->fields );
03239                     $oRs->moveNext();
03240                 }
03241             }
03242         }
03243         return $aSelect;
03244     }
03245 
03251     public function getArticleVat()
03252     {
03253         if (!isset($this->_dArticleVat)) {
03254             $this->_dArticleVat = oxVatSelector::getInstance()->getArticleVat( $this );
03255         }
03256         return $this->_dArticleVat;
03257     }
03258 
03267     protected function _applyVAT( oxPrice $oPrice, $dVat )
03268     {
03269         startProfile(__FUNCTION__);
03270         $oPrice->setVAT( $dVat );
03271         if ( ($dVat = oxVatSelector::getInstance()->getArticleUserVat($this)) !== false ) {
03272             $oPrice->setUserVat( $dVat );
03273         }
03274         stopProfile(__FUNCTION__);
03275     }
03276 
03284     public function applyVats( oxPrice $oPrice )
03285     {
03286         $this->_applyVAT($oPrice, $this->getArticleVat() );
03287     }
03288 
03299     protected function _applyDiscounts( $oPrice, $aDiscounts )
03300     {
03301         $oDiscountList = oxDiscountList::getInstance();
03302         $oDiscountList->applyDiscounts( $oPrice, $aDiscounts );
03303     }
03304 
03312     public function applyDiscountsForVariant( $oPrice )
03313     {
03314         // apply discounts
03315         if ( !$this->skipDiscounts() ) {
03316             $oDiscountList = oxDiscountList::getInstance();
03317             $oDiscountList->applyDiscounts( $oPrice, $oDiscountList->getArticleDiscounts( $this, $this->getArticleUser() ) );
03318         }
03319     }
03320 
03329     protected function _applyCurrency(oxPrice $oPrice, $oCur = null )
03330     {
03331         if ( !$oCur ) {
03332             $oCur = $this->getConfig()->getActShopCurrencyObject();
03333         }
03334 
03335         $oPrice->multiply($oCur->rate);
03336     }
03337 
03338 
03347     protected function _getAttribsString(&$sAttribs, &$iCnt)
03348     {
03349         // we do not use lists here as we dont need this overhead right now
03350         $oDb = oxDb::getDb( oxDb::FETCH_MODE_ASSOC );
03351         $sSelect =  'select oxattrid from oxobject2attribute where oxobject2attribute.oxobjectid='.$oDb->quote( $this->getId() );
03352         $sAttribs = '';
03353         $blSep = false;
03354         $rs = $oDb->select( $sSelect);
03355         $iCnt = 0;
03356         if ($rs != false && $rs->recordCount() > 0) {
03357             while (!$rs->EOF) {
03358                 if ( $blSep) {
03359                     $sAttribs .= ' or ';
03360                 }
03361                 $sAttribs .= 't1.oxattrid = '.$oDb->quote($rs->fields['oxattrid']).' ';
03362                 $blSep = true;
03363                 $iCnt++;
03364                 $rs->moveNext();
03365             }
03366         }
03367     }
03368 
03377     protected function _getSimList($sAttribs, $iCnt)
03378     {
03379         $myConfig = $this->getConfig();
03380         $oDb      = oxDb::getDb( oxDb::FETCH_MODE_ASSOC );
03381 
03382         // #523A
03383         $iAttrPercent = $myConfig->getConfigParam( 'iAttributesPercent' )/100;
03384         // 70% same attributes
03385         if ( !$iAttrPercent || $iAttrPercent < 0 || $iAttrPercent > 1) {
03386             $iAttrPercent = 0.70;
03387         }
03388         // #1137V iAttributesPercent = 100 doesnt work
03389         $iHitMin = ceil( $iCnt * $iAttrPercent );
03390 
03391         // we do not use lists here as we dont need this overhead right now
03392         $aList= array();
03393         $sSelect =  "select oxobjectid, count(*) as cnt from oxobject2attribute as t1 where
03394                     ( $sAttribs )
03395                     and t1.oxobjectid != ".$oDb->quote( $this->oxarticles__oxid->value )."
03396                     group by t1.oxobjectid having count(*) >= $iHitMin ";
03397 
03398         $rs = $oDb->selectLimit( $sSelect, 20, 0 );
03399         if ($rs != false && $rs->recordCount() > 0) {
03400             while (!$rs->EOF) {
03401                 $oTemp = new stdClass();    // #663
03402                 $oTemp->cnt = $rs->fields['cnt'];
03403                 $oTemp->id  = $rs->fields['oxobjectid'];
03404                 $aList[] = $oTemp;
03405                 $rs->moveNext();
03406             }
03407         }
03408         return $aList;
03409     }
03410 
03419     protected function _generateSimListSearchStr($sArticleTable, $aList)
03420     {
03421         $myConfig = $this->getConfig();
03422         $sFieldList = $this->getSelectFields();
03423         $sSearch = "select $sFieldList from $sArticleTable where ".$this->getSqlActiveSnippet()."  and $sArticleTable.oxissearch = 1 and $sArticleTable.oxid in ( ";
03424         $blSep = false;
03425         $iCnt = 0;
03426         $oDb = oxDb::getDb();
03427         foreach ( $aList as $oTemp) {
03428             if ( $blSep) {
03429                 $sSearch .= ',';
03430             }
03431             $sSearch .= $oDb->quote($oTemp->id);
03432             $blSep = true;
03433             if ( $iCnt >= $myConfig->getConfigParam( 'iNrofSimilarArticles' ) ) {
03434                 break;
03435             }
03436             $iCnt++;
03437         }
03438 
03439         //#1741T
03440         //$sSearch .= ") and $sArticleTable.oxparentid = '' ";
03441         $sSearch .= ') ';
03442 
03443         // #524A -- randomizing articles in attribute list
03444         $sSearch .= ' order by rand() ';
03445 
03446         return $sSearch;
03447     }
03448 
03457     protected function _generateSearchStr($sOXID, $blSearchPriceCat = false )
03458     {
03459 
03460         $sCatView = getViewName( 'oxcategories', $this->getLanguage() );
03461         $sO2CView = getViewName( 'oxobject2category' );
03462 
03463         // we do not use lists here as we dont need this overhead right now
03464         if ( !$blSearchPriceCat ) {
03465             $sSelect  = "select {$sCatView}.* from {$sO2CView} as oxobject2category left join {$sCatView} on
03466                          {$sCatView}.oxid = oxobject2category.oxcatnid
03467                          where oxobject2category.oxobjectid=".oxDb::getDb()->quote($sOXID)." and {$sCatView}.oxid is not null ";
03468         } else {
03469             $sSelect  = "select {$sCatView}.* from {$sCatView} where
03470                          '{$this->oxarticles__oxprice->value}' >= {$sCatView}.oxpricefrom and
03471                          '{$this->oxarticles__oxprice->value}' <= {$sCatView}.oxpriceto ";
03472         }
03473         return $sSelect;
03474     }
03475 
03481     protected function _generateSearchStrForCustomerBought()
03482     {
03483         $sArtTable = $this->getViewName();
03484         $sOrderArtTable = getViewName( 'oxorderarticles' );
03485 
03486         // fetching filter params
03487         $sIn = " '{$this->oxarticles__oxid->value}' ";
03488         if ( $this->oxarticles__oxparentid->value ) {
03489 
03490             // adding article parent
03491             $sIn .= ", '{$this->oxarticles__oxparentid->value}' ";
03492             $sParentIdForVariants = $this->oxarticles__oxparentid->value;
03493 
03494         } else {
03495             $sParentIdForVariants = $this->getId();
03496         }
03497 
03498         // adding variants
03499         $oDb = oxDb::getDb( oxDb::FETCH_MODE_ASSOC );
03500         $oRs = $oDb->select( "select oxid from {$sArtTable} where oxparentid = ".$oDb->quote($sParentIdForVariants)." and oxid != ".$oDb->quote($this->oxarticles__oxid->value) );
03501         if ( $oRs != false && $oRs->recordCount() > 0) {
03502             while ( !$oRs->EOF ) {
03503                 $sIn .= ", ".$oDb->quote(current( $oRs->fields ))." ";
03504                 $oRs->moveNext();
03505             }
03506         }
03507 
03508         $iLimit = (int) $this->getConfig()->getConfigParam( 'iNrofCustomerWhoArticles' );
03509         $iLimit = $iLimit?( $iLimit * 10 ): 50;
03510 
03511         // building sql (optimized)
03512         $sQ = "select distinct {$sArtTable}.* from (
03513                    select d.oxorderid as suborderid from {$sOrderArtTable} as d use index ( oxartid ) where d.oxartid in ( {$sIn} ) limit {$iLimit}
03514                ) as suborder
03515                left join {$sOrderArtTable} force index ( oxorderid ) on suborder.suborderid = {$sOrderArtTable}.oxorderid
03516                left join {$sArtTable} on {$sArtTable}.oxid = {$sOrderArtTable}.oxartid
03517                where {$sArtTable}.oxid not in ( {$sIn} )
03518                and ( {$sArtTable}.oxissearch = 1 or {$sArtTable}.oxparentid <> '' ) and ".$this->getSqlActiveSnippet();
03519 
03520         /* non optimized, but could be used if index forcing is not supported
03521         // building sql
03522         $sQ = "select distinct {$sArtTable}.* from {$sOrderArtTable}, {$sArtTable} where {$sOrderArtTable}.oxorderid in (
03523                    select {$sOrderArtTable}.oxorderid from {$sOrderArtTable} where {$sOrderArtTable}.oxartid in ( {$sIn} )
03524                ) and {$sArtTable}.oxid = {$sOrderArtTable}.oxartid and {$sArtTable}.oxid not in ( {$sIn} )
03525                and ( {$sArtTable}.oxissearch = 1 or {$sArtTable}.oxparentid <> '' )
03526                and ".$this->getSqlActiveSnippet();
03527         */
03528 
03529         return $sQ;
03530     }
03531 
03541     protected function _generateSelectCatStr($sOXID, $sCatId, $dPriceFromTo = false)
03542     {
03543         $sCategoryView = getViewName('oxcategories');
03544         $sO2CView = getViewName('oxobject2category');
03545 
03546         $oDb    = oxDb::getDb();
03547         $sOXID  = $oDb->quote($sOXID);
03548         $sCatId = $oDb->quote($sCatId);
03549 
03550         if (!$dPriceFromTo) {
03551             $sSelect  = "select oxobject2category.oxcatnid from $sO2CView as oxobject2category ";
03552             $sSelect .= "left join $sCategoryView as oxcategories on oxcategories.oxid = oxobject2category.oxcatnid ";
03553             $sSelect .= "where oxobject2category.oxcatnid=$sCatId and oxobject2category.oxobjectid=$sOXID ";
03554             $sSelect .= "and oxcategories.oxactive = 1 order by oxobject2category.oxtime ";
03555         } else {
03556             $dPriceFromTo = $oDb->quote($dPriceFromTo);
03557             $sSelect  = "select oxcategories.oxid from $sCategoryView as oxcategories where ";
03558             $sSelect .= "oxcategories.oxid=$sCatId and $dPriceFromTo >= oxcategories.oxpricefrom and ";
03559             $sSelect .= "$dPriceFromTo <= oxcategories.oxpriceto ";
03560         }
03561         return $sSelect;
03562     }
03563 
03569     protected function _getAmountPriceList()
03570     {
03571         if ( $this->_oAmountPriceList === null ) {
03572             $this->_oAmountPriceList = array();
03573             if ( !$this->skipDiscounts() ) {
03574                 $myConfig = $this->getConfig();
03575                 $sArtId   = $this->getId();
03576 
03577                 // #1690C - Scale prices and variants
03578                 if ( !$this->isAdmin() && $myConfig->getConfigParam( 'blVariantInheritAmountPrice' ) && $this->oxarticles__oxparentid->value ) {
03579                     $sArtId = $this->oxarticles__oxparentid->value;
03580                 }
03581 
03582                 //collecting assigned to article amount-price list
03583                 $oAmPriceList = oxNew( 'oxlist' );
03584                 $oAmPriceList->init( 'oxbase', 'oxprice2article' );
03585 
03586                 $sShopID = $myConfig->getShopID();
03587                 if ( $myConfig->getConfigParam( 'blMallInterchangeArticles' ) ) {
03588                     $sShopSelect = '1';
03589                 } else {
03590                     $sShopSelect = " oxshopid =  '$sShopID' ";
03591                 }
03592 
03593                 $oAmPriceList->selectString( "select * from oxprice2article where oxartid = " . oxDb::getDb()->quote( $sArtId ) . " and $sShopSelect order by oxamount ");
03594 
03595                 // prepare abs prices if currently having percentages
03596                 $oBasePrice = $this->_getGroupPrice();
03597                 foreach ( $oAmPriceList as $oAmPrice ) {
03598                     if ( $oAmPrice->oxprice2article__oxaddperc->value ) {
03599                         $oAmPrice->oxprice2article__oxaddabs = new oxField(oxPrice::percent( $oBasePrice, 100 - $oAmPrice->oxprice2article__oxaddperc->value ), oxField::T_RAW );
03600                     }
03601                 }
03602 
03603                 $this->_oAmountPriceList = $oAmPriceList;
03604             }
03605         }
03606 
03607         return $this->_oAmountPriceList;
03608     }
03609 
03617     protected function _isFieldEmpty( $sFieldName )
03618     {
03619         $mValue = $this->$sFieldName->value;
03620 
03621         if ( is_null( $mValue ) ) {
03622             return true;
03623         }
03624 
03625         if ( $mValue === '' ) {
03626             return true;
03627         }
03628 
03629         $aDoubleCopyFields = array('oxarticles__oxprice', 'oxarticles__oxvat');
03630 
03631         if (!$mValue && in_array( $sFieldName, $aDoubleCopyFields ) ) {
03632             return true;
03633         }
03634 
03635 
03636         if (!strcmp($mValue, '0000-00-00 00:00:00') || !strcmp($mValue, '0000-00-00')) {
03637             return true;
03638         }
03639 
03640         $sFieldName = strtolower($sFieldName);
03641 
03642         if ( $sFieldName == 'oxarticles__oxicon' && ( strpos($mValue, "nopic_ico.jpg") !== false || strpos($mValue, "nopic.jpg") !== false ) ) {
03643             return true;
03644         }
03645 
03646         if ( strpos($mValue, "nopic.jpg") !== false && ($sFieldName == 'oxarticles__oxthumb' || substr($sFieldName, 0, 17) == 'oxarticles__oxpic' || substr($sFieldName, 0, 18) == 'oxarticles__oxzoom') ) {
03647             return true;
03648         }
03649 
03650         return false;
03651     }
03652 
03660     protected function _assignParentFieldValue($sFieldName)
03661     {
03662         if (!($oParentArticle = $this->getParentArticle())) {
03663             return;
03664         }
03665 
03666         $sCopyFieldName = $this->_getFieldLongName($sFieldName);
03667 
03668         // assigning only theese which parent article has
03669         if ( $oParentArticle->$sCopyFieldName != null ) {
03670 
03671             // only overwrite database values
03672             if ( substr( $sCopyFieldName, 0, 12) != 'oxarticles__') {
03673                 return;
03674             }
03675 
03676             //do not copy certain fields
03677             if (in_array($sCopyFieldName, $this->_aNonCopyParentFields)) {
03678                 return;
03679             }
03680 
03681             //COPY THE VALUE
03682             // assigning images from parent only if variant has no master image (#1807)
03683             if ( stristr($sCopyFieldName, '_oxthumb') || stristr($sCopyFieldName, '_oxicon') ) {
03684                 if ( $this->_isFieldEmpty( $sCopyFieldName ) && !$this->_hasMasterImage( 1 ) ) {
03685                     $this->$sCopyFieldName = clone $oParentArticle->$sCopyFieldName;
03686                 }
03687             } elseif ( stristr($sCopyFieldName, '_oxzoom') ) {
03688                 // for zoom images checking master image with specified index
03689                 // assign from parent only if no pictures to variant are added
03690                 $iIndex = (int) str_ireplace( "oxarticles__oxzoom", "", $sFieldName );
03691                 if ( $this->_isFieldEmpty( $sCopyFieldName ) && !$this->_hasMasterImage( $iIndex ) && !$this->_hasMasterImage( 1 ) ) {
03692                     $this->$sCopyFieldName = clone $oParentArticle->$sCopyFieldName;
03693                 }
03694             } elseif ( stristr($sCopyFieldName, '_oxpicsgenerated') && $this->{$sCopyFieldName}->value == 0 ) {
03695                 // if no pics generated for variants, load all from
03696                 $this->$sCopyFieldName = clone $oParentArticle->$sCopyFieldName;
03697             } elseif ($this->_isFieldEmpty($sCopyFieldName) || in_array( $sCopyFieldName, $this->_aCopyParentField ) ) {
03698                 $this->$sCopyFieldName = clone $oParentArticle->$sCopyFieldName;
03699             }
03700         }
03701     }
03702 
03708     public function getParentArticle()
03709     {
03710         if ( ( $sParentId = $this->oxarticles__oxparentid->value ) ) {
03711             $sIndex = $sParentId . "_" . $this->getLanguage();
03712             if ( !isset( self::$_aLoadedParents[$sIndex] ) ) {
03713                 self::$_aLoadedParents[$sIndex] = oxNew( 'oxarticle' );
03714                 self::$_aLoadedParents[$sIndex]->_blSkipAbPrice  = true;
03715                 self::$_aLoadedParents[$sIndex]->_blLoadPrice    = false;
03716                 self::$_aLoadedParents[$sIndex]->_blLoadVariants = false;
03717                 self::$_aLoadedParents[$sIndex]->loadInLang( $this->getLanguage(), $sParentId );
03718             }
03719             return self::$_aLoadedParents[$sIndex];
03720         }
03721     }
03722 
03730     protected function _getParentAricle()
03731     {
03732         return $this->getParentArticle();
03733     }
03734 
03740     protected function _assignParentFieldValues()
03741     {
03742         startProfile('articleAssignParentInternal');
03743         if ( $this->oxarticles__oxparentid->value ) {
03744             // yes, we are in fact a variant
03745             if ( !$this->isAdmin() || ( $this->_blLoadParentData && $this->isAdmin() ) ) {
03746                 foreach ( $this->_aFieldNames as $sFieldName => $sVal ) {
03747                     $this->_assignParentFieldValue( $sFieldName );
03748                 }
03749             }
03750         }
03751         stopProfile('articleAssignParentInternal');
03752     }
03753 
03759     protected function _assignNotBuyableParent()
03760     {
03761         if ( !$this->getConfig()->getConfigParam( 'blVariantParentBuyable' ) &&
03762              ($this->_blHasVariants || $this->oxarticles__oxvarstock->value || $this->oxarticles__oxvarcount->value )) {
03763             $this->_blNotBuyableParent = true;
03764 
03765         }
03766     }
03767 
03773     protected function _assignStock()
03774     {
03775         $myConfig = $this->getConfig();
03776         // -----------------------------------
03777         // stock
03778         // -----------------------------------
03779 
03780         // #1125 A. must round (using floor()) value taken from database and cast to int
03781         if (!$myConfig->getConfigParam( 'blAllowUnevenAmounts' ) && !$this->isAdmin() ) {
03782             $this->oxarticles__oxstock = new oxField((int) floor($this->oxarticles__oxstock->value));
03783         }
03784         //GREEN light
03785         $this->_iStockStatus = 0;
03786 
03787         // if we have flag /*1 or*/ 4 - we show always green light
03788         if ( $myConfig->getConfigParam( 'blUseStock' ) && /*$this->oxarticles__oxstockflag->value != 1 && */ $this->oxarticles__oxstockflag->value != 4) {
03789             //ORANGE light
03790             $iStock = $this->oxarticles__oxstock->value;
03791 
03792             if ($this->_blNotBuyableParent) {
03793                 $iStock = $this->oxarticles__oxvarstock->value;
03794             }
03795 
03796 
03797             if ( $iStock <= $myConfig->getConfigParam( 'sStockWarningLimit' ) && $iStock > 0) {
03798                 $this->_iStockStatus = 1;
03799             }
03800 
03801             //RED light
03802             if ($iStock <= 0) {
03803                 $this->_iStockStatus = -1;
03804             }
03805         }
03806 
03807 
03808         // stock
03809         if ( $myConfig->getConfigParam( 'blUseStock' ) && ($this->oxarticles__oxstockflag->value == 3 || $this->oxarticles__oxstockflag->value == 2)) {
03810             $iOnStock = $this->oxarticles__oxstock->value;
03811             if ($this->getConfig()->getConfigParam( 'blPsBasketReservationEnabled' )) {
03812                 $iOnStock += $this->getSession()->getBasketReservations()->getReservedAmount($this->getId());
03813             }
03814             if ($iOnStock <= 0) {
03815                 $this->setBuyableState( false );
03816             }
03817         }
03818 
03819         //exceptional handling for variant parent stock:
03820         if ($this->_blNotBuyable && $this->oxarticles__oxvarstock->value ) {
03821             $this->setBuyableState( true );
03822             //but then at least setting notBuaybleParent to true
03823             $this->_blNotBuyableParent = true;
03824         }
03825 
03826         //special treatment for lists when blVariantParentBuyable config option is set to false
03827         //then we just hide "to basket" button.
03828         //if variants are not loaded in the list and this article has variants and parent is not buyable then this article is not buyable
03829         if ( !$myConfig->getConfigParam( 'blVariantParentBuyable' ) && !$myConfig->getConfigParam( 'blLoadVariants' ) && $this->oxarticles__oxvarstock->value) {
03830             $this->setBuyableState( false );
03831         }
03832 
03833         //setting to non buyable when variant list is empty (for example not loaded or inactive) and $this is non buyable parent
03834         if (!$this->_blNotBuyable && $this->_blNotBuyableParent && $this->oxarticles__oxvarcount->value == 0) {
03835             $this->setBuyableState( false );
03836         }
03837     }
03838 
03844     protected function _assignPrices()
03845     {
03846         $myConfig = $this->getConfig();
03847 
03848         // Performance
03849         if ( !$myConfig->getConfigParam( 'bl_perfLoadPrice' ) || !$this->_blLoadPrice ) {
03850             return;
03851         }
03852 
03853         //price per unit handling
03854         if ((double) $this->oxarticles__oxunitquantity->value && $this->oxarticles__oxunitname->value) {
03855             // compute price
03856             $dPrice = $this->getPrice()->getBruttoPrice();
03857             $oCur = $myConfig->getActShopCurrencyObject();
03858             $this->_fPricePerUnit = oxLang::getInstance()->formatCurrency($dPrice / (double) $this->oxarticles__oxunitquantity->value, $oCur);
03859         }
03860 
03861         //getting min and max prices of variants
03862         if ( $this->_hasAnyVariant() ) {
03863             $this->_applyRangePrice();
03864         }
03865     }
03866 
03872     protected function _assignPersistentParam()
03873     {
03874         // Persistent Parameter Handling
03875         $aPersParam     = oxSession::getVar( 'persparam');
03876         if ( isset( $aPersParam) && isset( $aPersParam[$this->getId()])) {
03877             $this->_aPersistParam = $aPersParam[$this->getId()];
03878         }
03879     }
03880 
03886     protected function _assignDynImageDir()
03887     {
03888         $myConfig = $this->getConfig();
03889 
03890         $sThisShop = $this->oxarticles__oxshopid->value;
03891 
03892         $this->_sDynImageDir   = $myConfig->getPictureUrl( null, false );
03893         $this->dabsimagedir    = $myConfig->getPictureDir( false ); //$sThisShop
03894         $this->nossl_dimagedir = $myConfig->getPictureUrl( null, false, false, null, $sThisShop ); //$sThisShop
03895         $this->ssl_dimagedir   = $myConfig->getPictureUrl( null, false, true, null, $sThisShop ); //$sThisShop
03896     }
03897 
03903     protected function _assignComparisonListFlag()
03904     {
03905         // #657 add a flag if article is on comparisonlist
03906 
03907         $aItems = oxSession::getVar('aFiltcompproducts');
03908         if ( isset( $aItems[$this->getId()])) {
03909             $this->_blIsOnComparisonList = true;
03910         }
03911     }
03912 
03918     protected function _assignAttributes()
03919     {
03920         //#1029T load attributes
03921         //#1078S removed check for module "Produktvergleich"
03922         if ( $this->getConfig()->getConfigParam( 'bl_perfLoadAttributes' ) ) {
03923             $this->getAttributes();
03924         }
03925     }
03926 
03927 
03935     protected function _insert()
03936     {
03937         // set oxinsert
03938         $iInsertTime = time();
03939         $now = date('Y-m-d H:i:s', $iInsertTime);
03940         $this->oxarticles__oxinsert    = new oxField( $now );
03941         $this->oxarticles__oxtimestamp = new oxField( $now );
03942         if ( !is_object($this->oxarticles__oxsubclass) || $this->oxarticles__oxsubclass->value == '') {
03943             $this->oxarticles__oxsubclass = new oxField('oxarticle');
03944         }
03945 
03946         return parent::_insert();
03947     }
03948 
03954     protected function _update()
03955     {
03956 
03957         $this->_skipSaveFields();
03958 
03959         $myConfig = $this->getConfig();
03960 
03961 
03962         return parent::_update();
03963     }
03964 
03972     protected function _deleteRecords($sOXID)
03973     {
03974         $oDb = oxDb::getDb();
03975 
03976         $sOXID = $oDb->quote($sOXID);
03977 
03978         //remove other records
03979         $sDelete = 'delete from oxobject2article where oxarticlenid = '.$sOXID.' or oxobjectid = '.$sOXID.' ';
03980         $oDb->execute( $sDelete);
03981 
03982         $sDelete = 'delete from oxobject2attribute where oxobjectid = '.$sOXID.' ';
03983         $oDb->execute( $sDelete);
03984 
03985         $sDelete = 'delete from oxobject2category where oxobjectid = '.$sOXID.' ';
03986         $oDb->execute( $sDelete);
03987 
03988         $sDelete = 'delete from oxobject2selectlist where oxobjectid = '.$sOXID.' ';
03989         $oDb->execute( $sDelete);
03990 
03991         $sDelete = 'delete from oxprice2article where oxartid = '.$sOXID.' ';
03992         $oDb->execute( $sDelete);
03993 
03994         $sDelete = 'delete from oxreviews where oxtype="oxarticle" and oxobjectid = '.$sOXID.' ';
03995         $oDb->execute( $sDelete);
03996 
03997         $sDelete = 'delete from oxaccessoire2article where oxobjectid = '.$sOXID.' or oxarticlenid = '.$sOXID.' ';
03998         $oDb->execute( $sDelete);
03999 
04000         //#1508C - deleting oxobject2delivery entries added
04001         $sDelete = 'delete from oxobject2delivery where oxobjectid = '.$sOXID.' and oxtype=\'oxarticles\' ';
04002         $oDb->execute( $sDelete);
04003 
04004         $sDelete = 'delete from oxartextends where oxid = '.$sOXID.' ';
04005         $oDb->execute( $sDelete);
04006 
04007         //delete the record
04008         foreach ( $this->_getLanguageSetTables( "oxartextends" ) as $sSetTbl ) {
04009             $oDb->execute( "delete from $sSetTbl where oxid = {$sOXID}" );
04010         }
04011 
04012         $sDelete = 'delete from oxactions2article where oxartid = '.$sOXID.' ';
04013         $rs = $oDb->execute( $sDelete );
04014 
04015         $sDelete = 'delete from oxobject2list where oxobjectid = '.$sOXID.' ';
04016         $rs = $oDb->execute( $sDelete );
04017 
04018 
04019         return $rs;
04020     }
04021 
04029     protected function _deleteVariantRecords( $sOXID )
04030     {
04031         if ( $sOXID ) {
04032             $oDb = oxDb::getDb();
04033             //collect variants to remove recursively
04034             $sQ = 'select oxid from '.$this->getViewName().' where oxparentid = '.$oDb->quote( $sOXID );
04035             $rs = $oDb->select( $sQ, false, false );
04036             if ($rs != false && $rs->recordCount() > 0) {
04037                 while (!$rs->EOF) {
04038                     $this->delete( $rs->fields[0] );
04039                     $rs->moveNext();
04040                 }
04041             }
04042         }
04043     }
04044 
04054     protected function _resetCacheAndArticleCount( $sOxid )
04055     {
04056         $this->_onChangeResetCounts( $sOxid, $this->oxarticles__oxvendorid->value, $this->oxarticles__oxmanufacturerid->value );
04057     }
04058 
04064     protected function _deletePics()
04065     {
04066         $myUtilsPic = oxUtilsPic::getInstance();
04067         $myConfig   = $this->getConfig();
04068         $oPictureHandler = oxPictureHandler::getInstance();
04069 
04070         //deleting custom main icon
04071         $oPictureHandler->deleteMainIcon( $this );
04072 
04073         //deleting custom thumbnail
04074         $oPictureHandler->deleteThumbnail( $this );
04075 
04076         $sAbsDynImageDir = $myConfig->getPictureDir(false);
04077 
04078         // deleting master image and all generated images
04079         $iPicCount = $myConfig->getConfigParam( 'iPicCount' );
04080         for ( $i = 1; $i <= $iPicCount; $i++ ) {
04081             $oPictureHandler->deleteArticleMasterPicture( $this, $i );
04082         }
04083     }
04084 
04094     protected function _onChangeResetCounts( $sOxid, $sVendorId = null, $sManufacturerId = null )
04095     {
04096 
04097         $myUtilsCount = oxUtilsCount::getInstance();
04098 
04099         if ( $sVendorId ) {
04100             $myUtilsCount->resetVendorArticleCount( $sVendorId );
04101         }
04102 
04103         if ( $sManufacturerId ) {
04104             $myUtilsCount->resetManufacturerArticleCount( $sManufacturerId );
04105         }
04106 
04107         //also reseting category counts
04108         $oDb = oxDb::getDb();
04109         $sQ = "select oxcatnid from oxobject2category where oxobjectid = ".$oDb->quote($sOxid);
04110         $oRs = $oDb->select( $sQ, false, false );
04111         if ( $oRs !== false && $oRs->recordCount() > 0) {
04112             while ( !$oRs->EOF ) {
04113                 $myUtilsCount->resetCatArticleCount( $oRs->fields[0] );
04114                 $oRs->moveNext();
04115             }
04116         }
04117     }
04118 
04126     protected function _onChangeUpdateStock( $sParentID )
04127     {
04128         if ( $sParentID ) {
04129             $oDb = oxDb::getDb();
04130             $sParentIdQuoted = $oDb->quote($sParentID);
04131             $sQ = 'select oxstock, oxvendorid, oxmanufacturerid from oxarticles where oxid = '.$sParentIdQuoted;
04132             $rs = $oDb->select( $sQ, false, false );
04133             $iOldStock = $rs->fields[0];
04134             $iVendorID = $rs->fields[1];
04135             $iManufacturerID = $rs->fields[2];
04136 
04137             $sQ = 'select sum(oxstock) from '.$this->getViewName(true).' where oxparentid = '.$sParentIdQuoted.' and '. $this->getSqlActiveSnippet( true ).' and oxstock > 0 ';
04138             $iStock = (float) $oDb->getOne( $sQ, false, false );
04139 
04140             $sQ = 'update oxarticles set oxvarstock = '.$iStock.' where oxid = '.$sParentIdQuoted;
04141             $oDb->execute( $sQ );
04142 
04143                 //now lets update category counts
04144                 //first detect stock status change for this article (to or from 0)
04145                 if ( $iStock < 0 ) {
04146                     $iStock = 0;
04147                 }
04148                 if ( $iOldStock < 0 ) {
04149                     $iOldStock = 0;
04150                 }
04151                 if ( $this->oxarticles__oxstockflag->value == 2 && $iOldStock xor $iStock ) {
04152                     //means the stock status could be changed (oxstock turns from 0 to 1 or from 1 to 0)
04153                     // so far we leave it like this but later we could move all count resets to one or two functions
04154                     $this->_onChangeResetCounts( $sParentID, $iVendorID, $iManufacturerID );
04155                 }
04156         }
04157     }
04158 
04166     protected function _onChangeStockResetCount( $sOxid )
04167     {
04168         $myConfig = $this->getConfig();
04169 
04170         if ( $myConfig->getConfigParam( 'blUseStock' ) && $this->oxarticles__oxstockflag->value == 2 &&
04171            ( $this->oxarticles__oxstock->value + $this->oxarticles__oxvarstock->value ) <= 0 ) {
04172 
04173                $this->_onChangeResetCounts( $sOxid, $this->oxarticles__oxvendorid->value, $this->oxarticles__oxmanufacturerid->value );
04174         }
04175     }
04176 
04184     protected function _onChangeUpdateVarCount( $sParentID )
04185     {
04186         if ( $sParentID ) {
04187             $oDb = oxDb::getDb();
04188             $sParentIdQuoted = $oDb->quote( $sParentID );
04189             $sQ = "select count(*) as varcount from oxarticles where oxparentid = {$sParentIdQuoted}";
04190             $iVarCount = (int) $oDb->getOne( $sQ, false, false );
04191 
04192             $sQ = "update oxarticles set oxvarcount = {$iVarCount} where oxid = {$sParentIdQuoted}";
04193             $oDb->execute( $sQ );
04194         }
04195     }
04196 
04204     protected function _onChangeUpdateMinVarPrice( $sParentID )
04205     {
04206         if ( $sParentID ) {
04207             $oDb = oxDb::getDb();
04208             $sParentIdQuoted = $oDb->quote($sParentID);
04209             //#M0000883 (Sarunas)
04210             $sQ = 'select min(oxprice) as varminprice from '.$this->getViewName(true).' where '.$this->getSqlActiveSnippet(true).' and (oxparentid = '.$sParentIdQuoted.')';
04211             $dVarMinPrice = $oDb->getOne( $sQ, false, false );
04212 
04213             $dParentPrice = $oDb->getOne( "select oxprice from oxarticles where oxid = $sParentIdQuoted ", false, false );
04214 
04215             $blParentBuyable =  $this->getConfig()->getConfigParam( 'blVariantParentBuyable' );
04216 
04217             if ($dVarMinPrice) {
04218                 if ($blParentBuyable) {
04219                     $dVarMinPrice = min($dVarMinPrice, $dParentPrice);
04220                 }
04221 
04222             } else {
04223                 $dVarMinPrice = $dParentPrice;
04224             }
04225 
04226             if ( $dVarMinPrice ) {
04227                 $sQ = 'update oxarticles set oxvarminprice = '.$dVarMinPrice.' where oxid = '.$sParentIdQuoted;
04228                 $oDb->execute($sQ);
04229             }
04230         }
04231     }
04232 
04233 
04239     protected function _applyRangePrice()
04240     {
04241         //#buglist_413 if bl_perfLoadPriceForAddList variant price shouldn't be loaded too
04242         if ( !$this->getConfig()->getConfigParam( 'bl_perfLoadPrice' ) || !$this->_blLoadPrice ) {
04243             return;
04244         }
04245 
04246         $this->_blIsRangePrice = false;
04247 
04248         // if parent is buyable - do not apply range price calcculations
04249         if ($this->_blSkipAbPrice || !$this->_blNotBuyableParent) {
04250             return;
04251         }
04252 
04253         if ( $this->isParentNotBuyable() && !$this->getConfig()->getConfigParam( 'blLoadVariants' )) {
04254             //#2509 we cannot force brutto price here, as netto price can be added to DB
04255             // $this->getPrice()->setBruttoPriceMode();
04256             $dPrice = $this->oxarticles__oxvarminprice->value;
04257             $this->getPrice()->setPrice($dPrice);
04258             $this->_blIsRangePrice = true;
04259             $this->_calculatePrice( $this->getPrice() );
04260             return;
04261         }
04262 
04263         $aPrices = array();
04264 
04265         if (!$this->_blNotBuyableParent) {
04266             $aPrices[] = $this->getPrice()->getBruttoPrice();
04267         }
04268 
04269         $aVariants = $this->getVariants(false);
04270 
04271         if (count($aVariants)) {
04272             foreach ($aVariants as $sKey => $oVariant) {
04273                 $aPrices[] = $oVariant->getPrice()->getBruttoPrice();
04274             }
04275         }
04276 
04277         if ( count( $aPrices ) ) {
04278             $dMinPrice = min( $aPrices );
04279             $dMaxPrice = max( $aPrices );
04280         }
04281 
04282         if ($this->_blNotBuyableParent && isset($dMinPrice) && $dMinPrice == $dMaxPrice) {
04283             $this->getPrice()->setBruttoPriceMode();
04284             $this->getPrice()->setPrice($dMinPrice);
04285         }
04286 
04287         if (isset($dMinPrice) && $dMinPrice != $dMaxPrice) {
04288             $this->getPrice()->setBruttoPriceMode();
04289             $this->getPrice()->setPrice($dMinPrice);
04290             $this->_blIsRangePrice = true;
04291         }
04292     }
04293 
04300     public function getProductId()
04301     {
04302         return $this->getId();
04303     }
04304 
04310     public function getProductParentId()
04311     {
04312         return $this->oxarticles__oxparentid->value;
04313     }
04314 
04320     public function isOrderArticle()
04321     {
04322         return false;
04323     }
04324 
04330     public function isVariant()
04331     {
04332         return (bool) ( isset( $this->oxarticles__oxparentid ) ? $this->oxarticles__oxparentid->value : false );
04333     }
04334 
04340     public function isMdVariant()
04341     {
04342         $oMdVariant = oxNew( "oxVariantHandler" );
04343 
04344         return $oMdVariant->isMdVariant($this);
04345     }
04346 
04354     public function getSqlForPriceCategories($sFields = '')
04355     {
04356         if (!$sFields) {
04357             $sFields = 'oxid';
04358         }
04359         $sSelectWhere = "select $sFields from ".$this->_getObjectViewName('oxcategories')." where";
04360         $sQuotedPrice = oxDb::getDb()->quote( $this->oxarticles__oxprice->value );
04361         return  "$sSelectWhere oxpricefrom != 0 and oxpriceto != 0 and oxpricefrom <= $sQuotedPrice and oxpriceto >= $sQuotedPrice"
04362                ." union $sSelectWhere oxpricefrom != 0 and oxpriceto = 0 and oxpricefrom <= $sQuotedPrice"
04363                ." union $sSelectWhere oxpricefrom = 0 and oxpriceto != 0 and oxpriceto >= $sQuotedPrice";
04364     }
04365 
04373     public function inPriceCategory( $sCatNid )
04374     {
04375         $oDb = oxDb::getDb();
04376 
04377         $sQuotedPrice = $oDb->quote( $this->oxarticles__oxprice->value );
04378         $sQuotedCnid = $oDb->quote( $sCatNid );
04379         return (bool) $oDb->getOne(
04380             "select 1 from ".$this->_getObjectViewName('oxcategories')." where oxid=$sQuotedCnid and"
04381            ."(   (oxpricefrom != 0 and oxpriceto != 0 and oxpricefrom <= $sQuotedPrice and oxpriceto >= $sQuotedPrice)"
04382            ." or (oxpricefrom != 0 and oxpriceto = 0 and oxpricefrom <= $sQuotedPrice)"
04383            ." or (oxpricefrom = 0 and oxpriceto != 0 and oxpriceto >= $sQuotedPrice)"
04384            .")"
04385         );
04386     }
04387 
04393     public function getMdVariants()
04394     {
04395         if ( $this->_oMdVariants ) {
04396             return $this->_oMdVariants;
04397         }
04398 
04399         $oParentArticle = $this->getParentArticle();
04400         if ( $oParentArticle ) {
04401             $oVariants = $oParentArticle->getVariants();
04402         } else {
04403             $oVariants = $this->getVariants();
04404         }
04405 
04406         $oVariantHandler = oxNew( "oxVariantHandler" );
04407         $this->_oMdVariants = $oVariantHandler->buildMdVariants( $oVariants, $this->getId() );
04408 
04409         return $this->_oMdVariants;
04410     }
04411 
04417     public function getMdSubvariants()
04418     {
04419         return $this->getMdVariants()->getMdSubvariants();
04420     }
04421 
04429     protected function _hasMasterImage( $iIndex )
04430     {
04431         $sPicName = basename($this->{"oxarticles__oxpic" . $iIndex}->value);
04432 
04433         if ( $sPicName == "nopic.jpg" || $sPicName == "" ) {
04434             return false;
04435         }
04436         if ( $this->isVariant() && $this->getParentArticle()->{"oxarticles__oxpic".$iIndex}->value == $this->{"oxarticles__oxpic".$iIndex}->value ) {
04437             return false;
04438         }
04439 
04440         $sMasterPic = 'product/'.$iIndex . "/" . $sPicName;
04441 
04442         if ( $this->getConfig()->getMasterPicturePath( $sMasterPic ) ) {
04443             return true;
04444         }
04445 
04446         return false;
04447     }
04448 
04457     public function getPictureFieldValue( $sFieldName, $iIndex = null )
04458     {
04459         if ( $sFieldName ) {
04460             $sFieldName = "oxarticles__" . $sFieldName . $iIndex;
04461             return $this->$sFieldName->value;
04462         }
04463     }
04464 
04472     public function getMasterZoomPictureUrl( $iIndex )
04473     {
04474         $sPicUrl  = false;
04475         $sPicName = basename( $this->{"oxarticles__oxpic" . $iIndex}->value );
04476 
04477         if ( $sPicName && $sPicName != "nopic.jpg" ) {
04478             $sPicUrl = $this->getConfig()->getPictureUrl( "master/product/" . $iIndex . "/" . $sPicName );
04479             if ( !$sPicUrl || basename( $sPicUrl ) == "nopic.jpg" ) {
04480                 $sPicUrl = false;
04481             }
04482         }
04483 
04484         return $sPicUrl;
04485     }
04486 
04492     public function getUnitName()
04493     {
04494         if ( $this->oxarticles__oxunitname->value ) {
04495             return oxLang::getInstance()->translateString( $this->oxarticles__oxunitname->value );
04496         }
04497     }
04498 
04506     public function getArticleFiles( $blAddFromParent=false )
04507     {
04508         if ( $this->_aArticleFiles === null) {
04509 
04510             $this->_aArticleFiles = false;
04511 
04512             $sQ = "SELECT * FROM `oxfiles` WHERE `oxartid` = '".$this->getId()."'";
04513 
04514             if ( !$this->getConfig()->getConfigParam( 'blVariantParentBuyable' ) && $blAddFromParent ) {
04515                 $sQ .= " OR `oxartId` = '". $this->oxarticles__oxparentid->value . "'";
04516             }
04517 
04518             $oArticleFiles = oxNew("oxlist");
04519             $oArticleFiles->init("oxfile");
04520             $oArticleFiles->selectString( $sQ );
04521             $this->_aArticleFiles  = $oArticleFiles;
04522 
04523         }
04524 
04525         return $this->_aArticleFiles;
04526     }
04527 
04533     public function isDownloadable()
04534     {
04535         return $this->oxarticles__oxisdownloadable->value;
04536     }
04537 
04543     public function hasAmountPrice()
04544     {
04545         if ( self::$_blHasAmountPrice === null ) {
04546 
04547             self::$_blHasAmountPrice = false;
04548 
04549             $oDb = oxDb::getDb();
04550             $sQ = "SELECT 1 FROM `oxprice2article` LIMIT 1";
04551 
04552             if ( $oDb->getOne( $sQ ) ) {
04553                 self::$_blHasAmountPrice = true;
04554             }
04555         }
04556 
04557         return self::$_blHasAmountPrice;
04558     }
04559 }