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 {
00019 
00025     protected $_sClassName = 'oxarticle';
00026 
00032     protected $_blUseLazyLoading = true;
00033 
00039     protected $_sItemKey;
00040 
00046     protected $_blCalcPrice    = true;
00047 
00052     protected $_oPrice      = null;
00053 
00054 
00060     protected $_dVarMinPrice = null;
00061 
00067     protected $_dVarMaxPrice = null;
00068 
00074     protected $_dArticleVat = null;
00075 
00081     protected $_aPersistParam  = null;
00082 
00088     protected $_blNotBuyable   = false;
00089 
00096     protected $_blLoadVariants = true;
00097 
00103     protected $_aVariants = null;
00104 
00110     protected $_aVariantsWithNotOrderables = null;
00111 
00120     protected $_blNotBuyableParent  = false;
00121 
00122 
00126     protected $_blHasVariants = false;
00127 
00131     protected $_blHasMdVariants = false;
00132 
00137     protected $_blIsOnComparisonList = false;
00138 
00143     protected $_oUser = null;
00144 
00150     protected $_blLoadPrice = true;
00151 
00158     protected $_blSkipAbPrice = false;
00159 
00166     protected $_fPricePerUnit = null;
00167 
00171     protected $_blLoadParentData = false;
00172 
00176     protected $_blAllowEmptyParentId = false;
00177 
00181     protected $_blSkipAssign = false;
00182 
00188     protected $_blSkipDiscounts = null;
00189 
00194     protected $_oAttributeList = null;
00195 
00196 
00202     protected $_blIsRangePrice = null;
00203 
00209     protected $_aMediaUrls = null;
00210 
00216     static protected $_aLoadedParents;
00217 
00223     static protected $_aSelList;
00224 
00230     protected $_aDispSelList;
00231 
00237     protected $_blIsSeoObject = true;
00238 
00244     protected $_oAmountPriceList = null;
00245 
00254     protected $_iLinkType = 0;
00255 
00261     protected $_aStdUrls = array();
00262 
00268     protected $_aSeoUrls = array();
00269 
00275     protected $_aSeoAddParams = array();
00276 
00282     protected $_aStdAddParams = array();
00283 
00289     protected $_sDynImageDir = null;
00290 
00296     protected $_sMoreDetailLink = null;
00297 
00303     protected $_sToBasketLink = null;
00304 
00310     protected $_iStockStatusOnLoad = null;
00311 
00317     protected $_iStockStatus = null;
00318 
00324     protected $_oTPrice = null;
00325 
00331     protected $_oAmountPriceInfo = null;
00332 
00338     protected $_dAmountPrice = null;
00339 
00345     protected static $_aArticleManufacturers = array();
00346 
00352     protected static $_aArticleVendors = array();
00353 
00359     protected static $_aArticleCats = array();
00360 
00366     protected $_aNonCopyParentFields = array('oxarticles__oxinsert',
00367                                              'oxarticles__oxtimestamp',
00368                                              'oxarticles__oxnid',
00369                                              'oxarticles__oxid',
00370                                              'oxarticles__oxparentid');
00371 
00377     protected $_aCopyParentField = array('oxarticles__oxnonmaterial',
00378                                          'oxarticles__oxfreeshipping',
00379                                          //'oxarticles__oxremindactive',
00380                                          'oxarticles__oxisdownloadable');
00381 
00387     protected $_oMdVariants = null;
00388 
00394     protected $_oLongDesc = null;
00395 
00403     protected $_aVariantSelections = array();
00404 
00409     protected static $_aSelections = array();
00410 
00415     protected static $_aCategoryCache = null;
00416 
00421     protected static $_blHasAmountPrice = null;
00422 
00427     protected $_aArticleFiles = null;
00428 
00433     protected $_blCanUpdateAnyField = null;
00434 
00443     public function __construct($aParams = null)
00444     {
00445         if ( $aParams && is_array($aParams)) {
00446             foreach ( $aParams as $sParam => $mValue) {
00447                 $this->$sParam = $mValue;
00448             }
00449         }
00450         parent::__construct();
00451         $this->init( 'oxarticles' );
00452     }
00453 
00462     public function __get($sName)
00463     {
00464         $this->$sName = parent::__get($sName);
00465         if ( $this->$sName ) {
00466             // since the field could have been loaded via lazy loading
00467             $this->_assignParentFieldValue($sName);
00468         }
00469 
00470         return $this->$sName;
00471     }
00472 
00481     public function __set( $sName, $sValue )
00482     {
00483         parent::__set( $sName, $sValue );
00484     }
00485 
00492     public function isInList()
00493     {
00494         return $this->_isInList();
00495     }
00496 
00504     public function setId( $sId = null )
00505     {
00506         $sId = parent::setId( $sId );
00507 
00508         // TODO: in oxbase::setId make it to check if exists and update, not recreate, then delete this overload
00509         $this->oxarticles__oxnid = $this->oxarticles__oxid;
00510 
00511         return $sId;
00512     }
00513 
00523     public function getActiveCheckQuery( $blForceCoreTable = null )
00524     {
00525         $sTable = $this->getViewName( $blForceCoreTable );
00526 
00527         // check if article is still active
00528         $sQ = " $sTable.oxactive = 1 ";
00529 
00530         // enabled time range check ?
00531         if ( $this->getConfig()->getConfigParam( 'blUseTimeCheck' ) ) {
00532             $sDate = date( 'Y-m-d H:i:s', oxRegistry::get("oxUtilsDate")->getTime() );
00533             $sQ = "( $sQ or ( $sTable.oxactivefrom < '$sDate' and $sTable.oxactiveto > '$sDate' ) ) ";
00534         }
00535 
00536         return $sQ;
00537     }
00538 
00552     public function getStockCheckQuery( $blForceCoreTable = null )
00553     {
00554         $myConfig = $this->getConfig();
00555         $sTable = $this->getViewName( $blForceCoreTable );
00556 
00557         $sQ = "";
00558 
00559         //do not check for variants
00560         if ( $myConfig->getConfigParam( 'blUseStock' ) ) {
00561             $sQ = " and ( $sTable.oxstockflag != 2 or ( $sTable.oxstock + $sTable.oxvarstock ) > 0  ) ";
00562             //V #M513: When Parent article is not purchasable, it's visibility should be displayed in shop only if any of Variants is available.
00563             if ( !$myConfig->getConfigParam( 'blVariantParentBuyable' ) ) {
00564                 $sTimeCheckQ = '';
00565                 if ( $myConfig->getConfigParam( 'blUseTimeCheck' ) ) {
00566                      $sDate = date( 'Y-m-d H:i:s', oxRegistry::get("oxUtilsDate")->getTime() );
00567                      $sTimeCheckQ = " or ( art.oxactivefrom < '$sDate' and art.oxactiveto > '$sDate' )";
00568                 }
00569                 $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 ) ) ";
00570             }
00571         }
00572 
00573         return $sQ;
00574     }
00575 
00587     public function getVariantsQuery( $blRemoveNotOrderables, $blForceCoreTable = null  )
00588     {
00589         $sTable = $this->getViewName( $blForceCoreTable );
00590         $sQ = " and $sTable.oxparentid = '".$this->getId()."' ";
00591 
00592         //checking if variant is active and stock status
00593         if ( $this->getConfig()->getConfigParam( 'blUseStock' ) ) {
00594             $sQ .= " and ( $sTable.oxstock > 0 or ( $sTable.oxstock <= 0 and $sTable.oxstockflag != 2 ";
00595             if ( $blRemoveNotOrderables ) {
00596                 $sQ .= " and $sTable.oxstockflag != 3 ";
00597             }
00598             $sQ .= " ) ) ";
00599         }
00600 
00601         return $sQ;
00602     }
00603 
00611     public function getSqlActiveSnippet( $blForceCoreTable = null )
00612     {
00613         $myConfig = $this->getConfig();
00614 
00615         // check if article is still active
00616         $sQ = $this->getActiveCheckQuery( $blForceCoreTable );
00617 
00618         // stock and variants check
00619         $sQ .= $this->getStockCheckQuery( $blForceCoreTable );
00620 
00621 
00622         return "( $sQ ) ";
00623     }
00624 
00632     public function setSkipAssign($blSkipAssign)
00633     {
00634         $this->_blSkipAssign = $blSkipAssign;
00635     }
00636 
00642     public function disablePriceLoad()
00643     {
00644         $this->_blLoadPrice = false;
00645     }
00646 
00652     public function enablePriceLoad()
00653     {
00654         $this->_blLoadPrice = true;
00655     }
00656 
00662     public function getItemKey()
00663     {
00664         return $this->_sItemKey;
00665     }
00666 
00674     public function setItemKey($sItemKey)
00675     {
00676         $this->_sItemKey = $sItemKey;
00677     }
00678 
00686     public function setNoVariantLoading( $blLoadVariants )
00687     {
00688         $this->_blLoadVariants = !$blLoadVariants;
00689     }
00690 
00696     public function isBuyable()
00697     {
00698         if ($this->_blNotBuyableParent) {
00699             return false;
00700         }
00701 
00702         return !$this->_blNotBuyable;
00703     }
00704 
00710     public function getPersParams()
00711     {
00712         return $this->_aPersistParam;
00713     }
00714 
00720     public function isOnComparisonList()
00721     {
00722         return $this->_blIsOnComparisonList;
00723     }
00724 
00732     public function setOnComparisonList( $blOnList )
00733     {
00734         $this->_blIsOnComparisonList = $blOnList;
00735     }
00736 
00744     public function setLoadParentData($blLoadParentData)
00745     {
00746         $this->_blLoadParentData = $blLoadParentData;
00747     }
00748 
00758     public function setSkipAbPrice( $blSkipAbPrice = null )
00759     {
00760         $this->_blSkipAbPrice = $blSkipAbPrice;
00761     }
00762 
00763 
00771     public function isMultilingualField($sFieldName)
00772     {
00773         switch ($sFieldName) {
00774             case "oxlongdesc":
00775             case "oxtags":
00776                 return true;
00777         }
00778 
00779         return parent::isMultilingualField($sFieldName);
00780     }
00781 
00787     public function isVisible()
00788     {
00789 
00790         // admin preview mode
00791         if ( ( $blCanPreview = oxRegistry::getUtils()->canPreview() ) !== null ) {
00792             return $blCanPreview;
00793         }
00794 
00795         // active ?
00796         $sNow = date('Y-m-d H:i:s');
00797         if ( !$this->oxarticles__oxactive->value &&
00798              (  $this->oxarticles__oxactivefrom->value > $sNow ||
00799                 $this->oxarticles__oxactiveto->value < $sNow
00800              )) {
00801             return false;
00802         }
00803 
00804         // stock flags
00805         if ( $this->getConfig()->getConfigParam( 'blUseStock' ) && $this->oxarticles__oxstockflag->value == 2) {
00806             $iOnStock = $this->oxarticles__oxstock->value + $this->oxarticles__oxvarstock->value;
00807             if ($this->getConfig()->getConfigParam( 'blPsBasketReservationEnabled' )) {
00808                 $iOnStock += $this->getSession()->getBasketReservations()->getReservedAmount($this->getId());
00809             }
00810             if ( $iOnStock <= 0 ) {
00811                 return false;
00812             }
00813         }
00814 
00815         return true;
00816     }
00817 
00826     public function assign( $aRecord )
00827     {
00828 
00829         startProfile('articleAssign');
00830 
00831         // load object from database
00832         parent::assign( $aRecord );
00833 
00834         $this->oxarticles__oxnid = $this->oxarticles__oxid;
00835 
00836         // check for simple article.
00837         if ($this->_blSkipAssign) {
00838             return null;
00839         }
00840 
00841         $this->_assignParentFieldValues();
00842         $this->_assignNotBuyableParent();
00843 
00844 
00845         $this->_assignStock();
00846         $this->_assignPersistentParam();
00847         $this->_assignDynImageDir();
00848         $this->_assignComparisonListFlag();
00849 
00850 
00851         stopProfile('articleAssign');
00852 
00853         return null;
00854     }
00855 
00856 
00864     protected function _loadFromDb( $sOXID )
00865     {
00866         $sSelect = $this->buildSelectString( array( $this->getViewName().".oxid" => $sOXID ));
00867 
00868         $aData = oxDb::getDb( oxDb::FETCH_MODE_ASSOC )->getRow( $sSelect );
00869 
00870         return $aData;
00871     }
00872 
00883     public function load( $sOXID )
00884     {
00885         // A. #1325 resetting to avoid problems when reloading (details etc)
00886         $this->_blNotBuyableParent = false;
00887 
00888 
00889             $aData = $this->_loadFromDb( $sOXID );
00890 
00891         if ( $aData ) {
00892             $this->assign( $aData );
00893             // convert date's to international format
00894 
00895             $this->_iStockStatusOnLoad = $this->_iStockStatus;
00896 
00897             $this->_isLoaded = true;
00898             return true;
00899         }
00900 
00901         return false;
00902     }
00903 
00904 
00912     public function addToRatingAverage( $iRating )
00913     {
00914         $dOldRating = $this->oxarticles__oxrating->value;
00915         $dOldCnt    = $this->oxarticles__oxratingcnt->value;
00916         $this->oxarticles__oxrating->setValue(( $dOldRating * $dOldCnt + $iRating ) / ($dOldCnt + 1));
00917         $this->oxarticles__oxratingcnt->setValue($dOldCnt + 1);
00918         $dRating = ( $dOldRating * $dOldCnt + $iRating ) / ($dOldCnt + 1);
00919         $dRatingCnt = (int) ($dOldCnt + 1);
00920         // oxarticles.oxtimestamp = oxarticles.oxtimestamp to keep old timestamp value
00921         $oDb = oxDb::getDb();
00922         $oDb->execute( 'update oxarticles set oxarticles.oxrating = '.$dRating.',oxarticles.oxratingcnt = '.$dRatingCnt.', oxarticles.oxtimestamp = oxarticles.oxtimestamp where oxarticles.oxid = '.$oDb->quote( $this->getId() ) );
00923 
00924     }
00925 
00933     public function setRatingAverage( $iRating )
00934     {
00935          $this->oxarticles__oxrating = new oxField( $iRating );
00936     }
00937 
00945     public function setRatingCount( $iRatingCnt )
00946     {
00947          $this->oxarticles__oxratingcnt = new oxField( $iRatingCnt );
00948     }
00949 
00957     public function getArticleRatingAverage( $blIncludeVariants = false )
00958     {
00959         if ( !$blIncludeVariants ) {
00960             return round( $this->oxarticles__oxrating->value, 1);
00961         } else {
00962             $oRating = oxNew( 'oxRating' );
00963             return $oRating->getRatingAverage( $this->getId(), 'oxarticle', $this->_getVariantsIds() );
00964         }
00965     }
00966 
00974     public function getArticleRatingCount( $blIncludeVariants = false )
00975     {
00976         if ( !$blIncludeVariants ) {
00977             return $this->oxarticles__oxratingcnt->value;
00978         } else {
00979             $oRating = oxNew( 'oxRating' );
00980             return $oRating->getRatingCount( $this->getId(), 'oxarticle', $this->_getVariantsIds() );
00981         }
00982     }
00983 
00984 
00990     public function getReviews()
00991     {
00992         $aIds = array($this->getId());
00993 
00994         if ( $this->oxarticles__oxparentid->value ) {
00995             $aIds[] = $this->oxarticles__oxparentid->value;
00996         }
00997 
00998         // showing variant reviews ..
00999         if ( $this->getConfig()->getConfigParam( 'blShowVariantReviews' ) ) {
01000             $aAdd = $this->_getVariantsIds();
01001             if (is_array($aAdd)) {
01002                 $aIds = array_merge($aIds, $aAdd);
01003             }
01004         }
01005 
01006         $oReview = oxNew('oxreview');
01007         $oRevs = $oReview->loadList('oxarticle', $aIds);
01008 
01009         //if no review found, return null
01010         if ( $oRevs->count() < 1 ) {
01011             return null;
01012         }
01013 
01014         return $oRevs;
01015     }
01016 
01022     public function getCrossSelling()
01023     {
01024         $oCrosslist = oxNew( "oxarticlelist");
01025         $oCrosslist->loadArticleCrossSell($this->oxarticles__oxid->value);
01026         if ( $oCrosslist->count() ) {
01027             return $oCrosslist;
01028         }
01029 
01030         return null;
01031     }
01032 
01038     public function getAccessoires()
01039     {
01040         $myConfig = $this->getConfig();
01041 
01042         // Performance
01043         if ( !$myConfig->getConfigParam( 'bl_perfLoadAccessoires' ) ) {
01044             return null;
01045         }
01046 
01047         $oAcclist = oxNew( "oxarticlelist");
01048         $oAcclist->setSqlLimit( 0, $myConfig->getConfigParam( 'iNrofCrossellArticles' ));
01049         $oAcclist->loadArticleAccessoires($this->oxarticles__oxid->value);
01050 
01051         if ( $oAcclist->count()) {
01052             return $oAcclist;
01053         }
01054 
01055         return null;
01056     }
01057 
01063     public function getSimilarProducts()
01064     {
01065         // Performance
01066         $myConfig = $this->getConfig();
01067         if ( !$myConfig->getConfigParam( 'bl_perfLoadSimilar' ) ) {
01068             return null;
01069         }
01070 
01071         $sArticleTable = $this->getViewName();
01072 
01073         $sAttribs = '';
01074         $iCnt = 0;
01075         $this->_getAttribsString($sAttribs, $iCnt);
01076 
01077         if ( !$sAttribs) {
01078             return null;
01079         }
01080 
01081         $aList = $this->_getSimList($sAttribs, $iCnt);
01082 
01083         if ( count( $aList ) ) {
01084             uasort( $aList, 'cmpart');
01085 
01086             $sSearch = $this->_generateSimListSearchStr($sArticleTable, $aList);
01087 
01088             $oSimilarlist = oxNew( 'oxarticlelist' );
01089             $oSimilarlist->setSqlLimit( 0, $myConfig->getConfigParam( 'iNrofSimilarArticles' ));
01090             $oSimilarlist->selectString( $sSearch);
01091 
01092             return $oSimilarlist;
01093         }
01094 
01095         return null;
01096     }
01097 
01103     public function getCustomerAlsoBoughtThisProducts()
01104     {
01105         // Performance
01106         $myConfig = $this->getConfig();
01107         if ( !$myConfig->getConfigParam( 'bl_perfLoadCustomerWhoBoughtThis' ) ) {
01108             return null;
01109         }
01110 
01111         // selecting products that fits
01112         $sQ = $this->_generateSearchStrForCustomerBought();
01113 
01114         $oArticles = oxNew( 'oxarticlelist' );
01115         $oArticles->setSqlLimit( 0, $myConfig->getConfigParam( 'iNrofCustomerWhoArticles' ));
01116         $oArticles->selectString( $sQ );
01117         if ( $oArticles->count() ) {
01118             return $oArticles;
01119         }
01120 
01121         return null;
01122     }
01123 
01130     public function loadAmountPriceInfo()
01131     {
01132         $myConfig = $this->getConfig();
01133         if ( !$myConfig->getConfigParam( 'bl_perfLoadPrice' ) || !$this->_blLoadPrice || !$this->_blCalcPrice || !$this->hasAmountPrice() ) {
01134             return array();
01135         }
01136 
01137         if ( $this->_oAmountPriceInfo === null ) {
01138             $this->_oAmountPriceInfo = array();
01139             if ( count( ( $aAmPriceList = $this->_getAmountPriceList()->getArray() ) ) ) {
01140                 $this->_oAmountPriceInfo = $this->_fillAmountPriceList( $aAmPriceList );
01141             }
01142         }
01143         return $this->_oAmountPriceInfo;
01144     }
01145 
01153     public function getSelectLists($sKeyPrefix = null)
01154     {
01155         //#1468C - more then one article in basket with different selectlist...
01156         //optionall function parameter $sKeyPrefix added, used only in basket.php
01157         $sKey = $this->getId();
01158         if ( isset( $sKeyPrefix ) ) {
01159             $sKey = $sKeyPrefix.'__'.$sKey;
01160         }
01161 
01162         if ( !isset( self::$_aSelList[$sKey] ) ) {
01163             $oDb = oxDb::getDb();
01164             $sSLViewName = getViewName( 'oxselectlist' );
01165 
01166             $sQ = "select {$sSLViewName}.* from oxobject2selectlist join {$sSLViewName} on $sSLViewName.oxid=oxobject2selectlist.oxselnid
01167                    where oxobject2selectlist.oxobjectid=%s order by oxobject2selectlist.oxsort";
01168 
01169             // all selectlists this article has
01170             $oLists = oxNew( 'oxlist' );
01171             $oLists->init( 'oxselectlist' );
01172             $oLists->selectString( sprintf( $sQ, $oDb->quote( $this->getId() ) ) );
01173 
01174             //#1104S if this is variant ant it has no selectlists, trying with parent
01175             if ( $oLists->count() == 0 && $this->oxarticles__oxparentid->value ) {
01176                 $oLists->selectString( sprintf( $sQ, $oDb->quote( $this->oxarticles__oxparentid->value ) ) );
01177             }
01178 
01179             // We do not need to calculate price here as there are method to get current article vat
01180             /*if ( $this->getPrice() != null ) {
01181                 $dVat = $this->getPrice()->getVat();
01182             }*/
01183             $dVat = $this->getArticleVat();
01184 
01185             $iCnt = 0;
01186             self::$_aSelList[$sKey] = array();
01187             foreach ( $oLists as $oSelectlist ) {
01188                 self::$_aSelList[$sKey][$iCnt] = $oSelectlist->getFieldList( $dVat );
01189                 self::$_aSelList[$sKey][$iCnt]['name'] = $oSelectlist->oxselectlist__oxtitle->value;
01190                 $iCnt++;
01191             }
01192         }
01193         return self::$_aSelList[$sKey];
01194     }
01195 
01203     protected function _hasAnyVariant( $blForceCoreTable = null )
01204     {
01205         $blHas = false;
01206         if ( ( $sId = $this->getId() ) ) {
01207             if ( $this->oxarticles__oxshopid->value == $this->getConfig()->getShopId() ) {
01208                 $blHas = (bool) $this->oxarticles__oxvarcount->value;
01209             } else {
01210                 $sArticleTable = $this->getViewName( $blForceCoreTable );
01211                 $blHas = (bool) oxDb::getDb()->getOne( "select 1 from $sArticleTable where oxparentid='{$sId}'" );
01212             }
01213         }
01214         return $blHas;
01215     }
01216 
01222     public function getVariantsCount()
01223     {
01224         return $this->oxarticles__oxvarcount->value;
01225     }
01226 
01232     public function hasMdVariants()
01233     {
01234         return $this->_blHasMdVariants;
01235     }
01236 
01246     public function getVariantSelections( $aFilterIds = null, $sActVariantId = null, $iLimit = 0 )
01247     {
01248         $iLimit = (int) $iLimit;
01249         if ( !isset( $this->_aVariantSelections[$iLimit] ) ) {
01250             $aVariantSelections = false;
01251             if ( $this->oxarticles__oxvarcount->value ) {
01252                 $oVariants = $this->getVariants( false );
01253                 $aVariantSelections = oxNew( "oxVariantHandler" )->buildVariantSelections( $this->oxarticles__oxvarname->getRawValue(), $oVariants, $aFilterIds, $sActVariantId, $iLimit );
01254 
01255                 if ( !empty($oVariants) && empty( $aVariantSelections['rawselections'] ) ) {
01256                     $aVariantSelections = false;
01257                 }
01258             }
01259             $this->_aVariantSelections[$iLimit] = $aVariantSelections;
01260         }
01261 
01262         return $this->_aVariantSelections[$iLimit];
01263     }
01264 
01273     public function getSelections( $iLimit = null, $aFilter = null )
01274     {
01275         $sId = $this->getId() . ( (int) $iLimit );
01276         if ( !array_key_exists( $sId, self::$_aSelections ) ) {
01277 
01278             $oDb = oxDb::getDb();
01279             $sSLViewName = getViewName( 'oxselectlist' );
01280 
01281             $sQ = "select {$sSLViewName}.* from oxobject2selectlist join {$sSLViewName} on $sSLViewName.oxid=oxobject2selectlist.oxselnid
01282                    where oxobject2selectlist.oxobjectid=%s order by oxobject2selectlist.oxsort";
01283 
01284             if ( ( $iLimit = (int) $iLimit ) ) {
01285                 $sQ .= " limit $iLimit ";
01286             }
01287 
01288             // vat value for price
01289             $dVat = 0;
01290             if ( ( $oPrice = $this->getPrice() ) != null ) {
01291                 $dVat = $oPrice->getVat();
01292             }
01293 
01294             // all selectlists this article has
01295             $oList = oxNew( 'oxlist' );
01296             $oList->init( 'oxselectlist' );
01297             $oList->getBaseObject()->setVat( $dVat );
01298             $oList->selectString( sprintf( $sQ, $oDb->quote( $this->getId() ) ) );
01299 
01300             //#1104S if this is variant and it has no selectlists, trying with parent
01301             if ( $oList->count() == 0 && $this->oxarticles__oxparentid->value ) {
01302                 $oList->selectString( sprintf( $sQ, $oDb->quote( $this->oxarticles__oxparentid->value ) ) );
01303             }
01304 
01305             self::$_aSelections[$sId] = $oList->count() ? $oList : false;
01306         }
01307 
01308         if ( self::$_aSelections[$sId] ) {
01309             // marking active from filter
01310             $aFilter = ( $aFilter === null ) ? oxConfig::getParameter( "sel" ) : $aFilter;
01311             if ( $aFilter ) {
01312                 $iSelIdx = 0;
01313                 foreach ( self::$_aSelections[$sId] as $oSelection ) {
01314                     if ( isset( $aFilter[$iSelIdx] ) ) {
01315                         $oSelection->setActiveSelectionByIndex( $aFilter[$iSelIdx] );
01316                     }
01317                     $iSelIdx++;
01318                 }
01319             }
01320         }
01321 
01322         return self::$_aSelections[$sId];
01323     }
01324 
01334     protected function _loadVariantList( $blSimple, $blRemoveNotOrderables = true, $blForceCoreTable = null )
01335     {
01336         $oVariants = array();
01337         $sId = $this->getId();
01338 
01339         if (!$sId) {
01340             return $oVariants;
01341         }
01342 
01343         //do not load me as a parent later
01344         self::$_aLoadedParents[$sId . "_" . $this->getLanguage()] = $this;
01345 
01346         $myConfig = $this->getConfig();
01347 
01348         if ( !$this->_blLoadVariants ||
01349             ( !$this->isAdmin() && !$myConfig->getConfigParam( 'blLoadVariants') ) ||
01350             ( !$this->isAdmin() && !$this->oxarticles__oxvarcount->value ) ) {
01351             return $oVariants;
01352         }
01353 
01354         // cache
01355         $sCacheKey = $blSimple ? "simple" : "full";
01356         if ( $blRemoveNotOrderables ) {
01357             if ( isset( $this->_aVariants[$sCacheKey] ) ) {
01358                return $this->_aVariants[$sCacheKey];
01359             } else {
01360                 $this->_aVariants[$sCacheKey] = & $oVariants;
01361             }
01362         } elseif ( !$blRemoveNotOrderables ) {
01363             if ( isset( $this->_aVariantsWithNotOrderables[$sCacheKey] ) ) {
01364                 return $this->_aVariantsWithNotOrderables[$sCacheKey];
01365             } else {
01366                 $this->_aVariantsWithNotOrderables[$sCacheKey] = & $oVariants;
01367             }
01368         }
01369 
01370         if ( ( $this->_blHasVariants = $this->_hasAnyVariant( $blForceCoreTable ) ) ) {
01371 
01372             $oVariants = $this->loadSimpleVariantsForLists( $blSimple );
01373 
01374             $oVariants = $this->_selectVariants( $blRemoveNotOrderables, $blForceCoreTable, $oVariants );
01375         }
01376 
01377         //if we have variants then depending on config option the parent may be non buyable
01378         if ( !$myConfig->getConfigParam( 'blVariantParentBuyable' ) && $this->_blHasVariants ) {
01379             $this->_blNotBuyableParent = true;
01380         }
01381 
01382         //if we have variants, but all variants are incative means article may be non buyable (depends on config option)
01383         if ( !$myConfig->getConfigParam( 'blVariantParentBuyable' ) && count( $oVariants ) == 0 && $this->_blHasVariants ) {
01384             $this->_blNotBuyable = true;
01385         }
01386 
01387         return $oVariants;
01388     }
01389 
01398     public function getFullVariants( $blRemoveNotOrderables = true, $blForceCoreTable = null )
01399     {
01400         return $this->_loadVariantList( false, $blRemoveNotOrderables, $blForceCoreTable );
01401     }
01402 
01411     public function getVariants( $blRemoveNotOrderables = true, $blForceCoreTable = null  )
01412     {
01413         return $this->_loadVariantList( $this->_isInList(), $blRemoveNotOrderables, $blForceCoreTable );
01414     }
01415 
01421     public function getSimpleVariants()
01422     {
01423         if ( $this->oxarticles__oxvarcount->value) {
01424             return $this->getVariants();
01425         }
01426 
01427         return null;
01428     }
01429 
01438     public function getAdminVariants( $sLanguage = null )
01439     {
01440         $oVariants = oxNew( 'oxarticlelist');
01441         if ( ( $sId = $this->getId() ) ) {
01442 
01443             $oBaseObj = $oVariants->getBaseObject();
01444 
01445             if ( is_null( $sLanguage ) ) {
01446                 $oBaseObj->setLanguage( oxRegistry::getLang()->getBaseLanguage() );
01447             } else {
01448                 $oBaseObj->setLanguage( $sLanguage );
01449             }
01450 
01451             $sSql = "select * from ".$oBaseObj->getViewName()." where oxparentid = '{$sId}' order by oxsort ";
01452             $oVariants->selectString( $sSql );
01453 
01454             //if we have variants then depending on config option the parent may be non buyable
01455             if ( !$this->getConfig()->getConfigParam( 'blVariantParentBuyable' ) && ( $oVariants->count() > 0 ) ) {
01456                 //$this->blNotBuyable = true;
01457                 $this->_blNotBuyableParent = true;
01458             }
01459         }
01460 
01461         return $oVariants;
01462     }
01463 
01471     public function getCategory()
01472     {
01473         $oCategory = oxNew( 'oxcategory' );
01474         $oCategory->setLanguage( $this->getLanguage() );
01475 
01476         // variant handling
01477         $sOXID = $this->getId();
01478         if ( isset( $this->oxarticles__oxparentid->value ) && $this->oxarticles__oxparentid->value ) {
01479             $sOXID = $this->oxarticles__oxparentid->value;
01480         }
01481 
01482         if ( $sOXID ) {
01483             // if the oxcategory instance of this article is not cached
01484             if ( !isset( $this->_aCategoryCache[ $sOXID ] ) ) {
01485                 startPRofile( 'getCategory' );
01486                 $oStr = getStr();
01487                 $sWhere   = $oCategory->getSqlActiveSnippet();
01488                 $sSelect  = $this->_generateSearchStr( $sOXID );
01489                 $sSelect .= ( $oStr->strstr( $sSelect, 'where' )?' and ':' where ') . $sWhere . " order by oxobject2category.oxtime limit 1";
01490 
01491                 // category not found ?
01492                 if ( !$oCategory->assignRecord( $sSelect ) ) {
01493 
01494                     $sSelect  = $this->_generateSearchStr( $sOXID, true );
01495                     $sSelect .= ( $oStr->strstr( $sSelect, 'where' )?' and ':' where ') . $sWhere . " limit 1";
01496 
01497                     // looking for price category
01498                     if ( !$oCategory->assignRecord( $sSelect ) ) {
01499                         $oCategory = null;
01500                     }
01501                 }
01502                 // add the category instance to cache
01503                 $this->_aCategoryCache[ $sOXID ] = $oCategory;
01504                 stopPRofile( 'getCategory' );
01505             } else {
01506                // if the oxcategory instance is cached
01507                $oCategory = $this->_aCategoryCache[ $sOXID ];
01508             }
01509         }
01510 
01511         return $oCategory;
01512     }
01513 
01522     public function getCategoryIds( $blActCats = false, $blSkipCache = false )
01523     {
01524         if ( isset( self::$_aArticleCats[$this->getId()] ) && !$blSkipCache ) {
01525             return self::$_aArticleCats[$this->getId()];
01526         }
01527 
01528         // variant handling
01529         $sOXID = $this->getId();
01530 
01531         $aRet = $this->_getArticleCategories( $sOXID, $blActCats );
01532 
01533         if (isset( $this->oxarticles__oxparentid->value) && $this->oxarticles__oxparentid->value) {
01534             $aRet = array_merge( $aRet, $this->_getArticleCategories( $this->oxarticles__oxparentid->value, $blActCats ) );
01535         }
01536 
01537         // adding price categories if such exists
01538         $sSql = $this->getSqlForPriceCategories();
01539 
01540         $oDb = oxDb::getDb( oxDb::FETCH_MODE_ASSOC );
01541         $rs = $oDb->select( $sSql );
01542 
01543 
01544         if ($rs != false && $rs->recordCount() > 0) {
01545             while (!$rs->EOF) {
01546 
01547                 if ( is_array( $rs->fields ) ) {
01548                    $rs->fields = array_change_key_case( $rs->fields, CASE_LOWER );
01549                 }
01550 
01551 
01552                 if ( !$aRet[$rs->fields['oxid']] ) {
01553                     $aRet[] = $rs->fields['oxid'];
01554                 }
01555                 $rs->moveNext();
01556             }
01557         }
01558 
01559         return self::$_aArticleCats[$this->getId()] = $aRet;
01560     }
01561 
01570     protected function _getArticleCategories( $sOXID, $blActCats = false )
01571     {
01572         // we do not use lists here as we don't need this overhead right now
01573         $sSql = $this->_getSelectCatIds( $sOXID, $blActCats );
01574         $oDb = oxDb::getDb( oxDb::FETCH_MODE_ASSOC );
01575         $rs = $oDb->select( $sSql );
01576 
01577 
01578         $aRet = array();
01579 
01580         if ($rs != false && $rs->recordCount() > 0) {
01581             while (!$rs->EOF) {
01582                 $aRet[] = $rs->fields['oxcatnid'];
01583                 $rs->moveNext();
01584             }
01585         }
01586 
01587         return $aRet;
01588     }
01589 
01598     protected function _getSelectCatIds( $sOXID, $blActCats = false )
01599     {
01600         $sO2CView = $this->_getObjectViewName('oxobject2category');
01601         $sCatView = $this->_getObjectViewName('oxcategories');
01602         $sSelect =  "select oxobject2category.oxcatnid as oxcatnid from $sO2CView as oxobject2category left join $sCatView as oxcategories on oxcategories.oxid = oxobject2category.oxcatnid ";
01603         $sSelect .= 'where oxobject2category.oxobjectid='.oxDb::getDb()->quote($sOXID).' and oxcategories.oxid is not null and oxcategories.oxactive = 1 ';
01604         if ( $blActCats ) {
01605             $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 ";
01606         }
01607         $sSelect .= 'order by oxobject2category.oxtime ';
01608         return $sSelect;
01609     }
01610 
01620     public function getVendor( $blShopCheck = true )
01621     {
01622         if ( ( $sVendorId = $this->getVendorId() ) ) {
01623             $oVendor = oxNew( 'oxvendor' );
01624         } elseif ( !$blShopCheck && $this->oxarticles__oxvendorid->value ) {
01625                 $oVendor = oxNew( 'oxi18n' );
01626                 $oVendor->init('oxvendor');
01627                 $oVendor->setReadOnly( true );
01628             $sVendorId = $this->oxarticles__oxvendorid->value;
01629         }
01630         if ( $sVendorId && $oVendor->load( $sVendorId ) && $oVendor->oxvendor__oxactive->value ) {
01631 
01632             return $oVendor;
01633         }
01634         return null;
01635     }
01636 
01644     public function getVendorId( $blForceReload = false )
01645     {
01646         $sVendorId = false;
01647         if ( $this->oxarticles__oxvendorid->value ) {
01648                 $sVendorId = $this->oxarticles__oxvendorid->value;
01649 
01650         }
01651         return $sVendorId;
01652     }
01653 
01661     public function getManufacturerId( $blForceReload = false )
01662     {
01663         $sManufacturerId = false;
01664         if ( $this->oxarticles__oxmanufacturerid->value ) {
01665 
01666                 $sManufacturerId = $this->oxarticles__oxmanufacturerid->value;
01667 
01668         }
01669         return $sManufacturerId;
01670     }
01671 
01681     public function getManufacturer( $blShopCheck = true )
01682     {
01683             $oManufacturer = oxNew( 'oxmanufacturer' );;
01684         if ( !( $sManufacturerId = $this->getManufacturerId() ) &&
01685              !$blShopCheck && $this->oxarticles__oxmanufacturerid->value ) {
01686             $oManufacturer->setReadOnly( true );
01687             $sManufacturerId = $this->oxarticles__oxmanufacturerid->value;
01688         }
01689 
01690         if ( $sManufacturerId && $oManufacturer->load( $sManufacturerId ) ) {
01691             if ( !$this->getConfig()->getConfigParam( 'bl_perfLoadManufacturerTree' ) ) {
01692                 $oManufacturer->setReadOnly( true );
01693             }
01694             $oManufacturer = $oManufacturer->oxmanufacturers__oxactive->value ? $oManufacturer : null;
01695         } else {
01696             $oManufacturer = null;
01697         }
01698 
01699         return $oManufacturer;
01700     }
01701 
01709     public function inCategory( $sCatNid)
01710     {
01711         return in_array( $sCatNid, $this->getCategoryIds());
01712     }
01713 
01722     public function isAssignedToCategory( $sCatId )
01723     {
01724         // variant handling
01725         $sOXID = $this->getId();
01726         if ( isset( $this->oxarticles__oxparentid->value) && $this->oxarticles__oxparentid->value) {
01727             $sOXID = $this->oxarticles__oxparentid->value;
01728         }
01729 
01730         $oDb = oxDb::getDb();
01731         $sSelect = $this->_generateSelectCatStr( $sOXID, $sCatId);
01732         $sOXID = $oDb->getOne( $sSelect );
01733         // article is assigned to passed category!
01734         if ( isset( $sOXID) && $sOXID) {
01735             return true;
01736         }
01737 
01738         // maybe this category is price category ?
01739         if ( $this->getConfig()->getConfigParam( 'bl_perfLoadPrice' ) && $this->_blLoadPrice ) {
01740             $dPriceFromTo = $this->getPrice()->getBruttoPrice();
01741             if ( $dPriceFromTo > 0) {
01742                 $sSelect = $this->_generateSelectCatStr( $sOXID, $sCatId, $dPriceFromTo);
01743                 $sOXID = $oDb->getOne( $sSelect );
01744                 // article is assigned to passed category!
01745                 if ( isset( $sOXID) && $sOXID) {
01746                     return true;
01747                 }
01748             }
01749         }
01750         return false;
01751     }
01752 
01758     public function getTPrice()
01759     {
01760         if ( !$this->getConfig()->getConfigParam( 'bl_perfLoadPrice' ) || !$this->_blLoadPrice ) {
01761             return null;
01762         }
01763 
01764         // return cached result, since oPrice is created ONLY in this function [or function of EQUAL level]
01765         if ( $this->_oTPrice !== null ) {
01766             return $this->_oTPrice;
01767         }
01768 
01769         $oPrice = $this->_getPriceObject();
01770 
01771         $dBasePrice = $this->oxarticles__oxtprice->value;
01772         $dBasePrice = $this->_preparePrice( $dBasePrice, $this->getArticleVat() );
01773 
01774         $oPrice->setPrice( $dBasePrice );
01775 
01776         $this->_applyVat( $oPrice, $this->getArticleVat() );
01777         $this->_applyCurrency( $oPrice );
01778 
01779         if ( $this->isParentNotBuyable() ) {
01780             // if parent article is not buyable then compare agains min article variant price
01781             $oPrice2 = $this->getVarMinPrice();
01782         } else {
01783             // else compare against article price
01784             $oPrice2 = $this->getPrice();
01785         }
01786 
01787         if ( $oPrice->getPrice() <= $oPrice2->getPrice() ) {
01788             // if RRP price is less or equal to comparable price then return
01789             return null;
01790         }
01791 
01792         $this->_oTPrice = $oPrice;
01793 
01794         return $this->_oTPrice;
01795     }
01796 
01802     public function skipDiscounts()
01803     {
01804         // already loaded skip discounts config
01805         if ( $this->_blSkipDiscounts !== null ) {
01806             return $this->_blSkipDiscounts;
01807         }
01808 
01809         if ( $this->oxarticles__oxskipdiscounts->value ) {
01810             return true;
01811         }
01812 
01813 
01814         $this->_blSkipDiscounts = false;
01815         if ( oxRegistry::get("oxDiscountList")->hasSkipDiscountCategories() ) {
01816 
01817             $oDb = oxDb::getDb();
01818             $sO2CView  = getViewName( 'oxobject2category', $this->getLanguage() );
01819             $sViewName = getViewName( 'oxcategories', $this->getLanguage() );
01820             $sSelect =  "select 1 from $sO2CView as $sO2CView left join {$sViewName} on {$sViewName}.oxid = $sO2CView.oxcatnid
01821                          where $sO2CView.oxobjectid=".$oDb->quote( $this->getId() )." and {$sViewName}.oxactive = 1 and {$sViewName}.oxskipdiscounts = '1' ";
01822             $this->_blSkipDiscounts = ( $oDb->getOne( $sSelect ) == 1 );
01823         }
01824 
01825         return $this->_blSkipDiscounts;
01826     }
01827 
01835     public function setPrice(oxPrice $oPrice)
01836     {
01837         $this->_oPrice = $oPrice;
01838     }
01839 
01848     public function getBasePrice( $dAmount = 1 )
01849     {
01850         // override this function if you want e.g. different prices
01851         // for diff. user groups.
01852 
01853         // Performance
01854         $myConfig = $this->getConfig();
01855         if( !$myConfig->getConfigParam( 'bl_perfLoadPrice' ) || !$this->_blLoadPrice )
01856             return null;
01857 
01858         // GroupPrice or DB price ajusted by AmountPrice
01859         $dPrice = $this->_getAmountPrice( $dAmount );
01860 
01861 
01862         return $dPrice;
01863     }
01864 
01872     public function getPrice( $dAmount = 1 )
01873     {
01874         $myConfig = $this->getConfig();
01875         // Performance
01876         if ( !$myConfig->getConfigParam( 'bl_perfLoadPrice' ) || !$this->_blLoadPrice ) {
01877             return null;
01878         }
01879 
01880         // return cached result, since oPrice is created ONLY in this function [or function of EQUAL level]
01881         if ( $dAmount != 1 || $this->_oPrice === null ) {
01882 
01883             // module
01884             $dBasePrice = $this->getBasePrice( $dAmount );
01885             $dBasePrice = $this->_preparePrice( $dBasePrice, $this->getArticleVat() );
01886 
01887             $oPrice = $this->_getPriceObject();
01888 
01889             $oPrice->setPrice( $dBasePrice );
01890 
01891             // price handling
01892             if ( !$this->_blCalcPrice && $dAmount == 1 ) {
01893                 return $this->_oPrice = $oPrice;
01894             }
01895 
01896             $this->_calculatePrice( $oPrice );
01897             if ( $dAmount != 1 ) {
01898                 return $oPrice;
01899             }
01900 
01901             $this->_oPrice = $oPrice;
01902         }
01903         return $this->_oPrice;
01904     }
01905 
01914     protected function _calculatePrice( $oPrice, $dVat = null )
01915     {
01916         // apply VAT only if configuration requires it
01917         if ( isset( $dVat ) || !$this->getConfig()->getConfigParam( 'bl_perfCalcVatOnlyForBasketOrder' ) ) {
01918             $this->_applyVAT( $oPrice, isset( $dVat ) ? $dVat : $this->getArticleVat() );
01919         }
01920 
01921         // apply currency
01922         $this->_applyCurrency( $oPrice );
01923         // apply discounts
01924         if ( !$this->skipDiscounts() ) {
01925             $oDiscountList = oxRegistry::get("oxDiscountList");
01926             $aDiscounts = $oDiscountList->getArticleDiscounts( $this, $this->getArticleUser() );
01927 
01928             reset( $aDiscounts );
01929             foreach ( $aDiscounts as $oDiscount ) {
01930                 $oPrice->setDiscount($oDiscount->getAddSum(), $oDiscount->getAddSumType());
01931             }
01932             $oPrice->calculateDiscount();
01933         }
01934 
01935         return $oPrice;
01936     }
01937 
01945     public function setArticleUser($oUser)
01946     {
01947         $this->_oUser = $oUser;
01948     }
01949 
01955     public function getArticleUser()
01956     {
01957         if ($this->_oUser) {
01958             return $this->_oUser;
01959         }
01960         return $this->getUser();
01961     }
01962 
01972     public function getBasketPrice( $dAmount, $aSelList, $oBasket )
01973     {
01974         $oUser = $oBasket->getBasketUser();
01975         $this->setArticleUser( $oUser );
01976 
01977         $oBasketPrice = $this->_getPriceObject( $oBasket->isCalculationModeNetto() );
01978 
01979         // get base price
01980         $dBasePrice = $this->getBasePrice( $dAmount );
01981 
01982         $dBasePrice = $this->_modifySelectListPrice( $dBasePrice, $aSelList );
01983         $dBasePrice = $this->_preparePrice( $dBasePrice, $this->getArticleVat(), $oBasket->isCalculationModeNetto() );
01984 
01985         // applying select list price
01986 
01987         // setting price
01988         $oBasketPrice->setPrice( $dBasePrice );
01989 
01990         $dVat = oxRegistry::get("oxVatSelector")->getBasketItemVat( $this, $oBasket );
01991         $this->_calculatePrice( $oBasketPrice, $dVat );
01992 
01993         // returning final price object
01994         return $oBasketPrice;
01995     }
01996 
02005     public function delete( $sOXID = null )
02006     {
02007         if ( !$sOXID ) {
02008             $sOXID = $this->getId();
02009         }
02010         if ( !$sOXID ) {
02011             return false;
02012         }
02013 
02014 
02015 
02016         // #2339 delete first variants before deleting parent product
02017         $this->_deleteVariantRecords( $sOXID );
02018         $this->load( $sOXID );
02019         $this->_deletePics();
02020         $this->_onChangeResetCounts( $sOXID, $this->oxarticles__oxvendorid->value, $this->oxarticles__oxmanufacturerid->value );
02021 
02022         // delete self
02023         parent::delete( $sOXID );
02024 
02025         $rs = $this->_deleteRecords( $sOXID );
02026 
02027         oxRegistry::get("oxSeoEncoderArticle")->onDeleteArticle($this);
02028 
02029         $this->onChange( ACTION_DELETE, $sOXID, $this->oxarticles__oxparentid->value );
02030 
02031         return $rs->EOF;
02032     }
02033 
02042     public function reduceStock($dAmount, $blAllowNegativeStock = false)
02043     {
02044         $this->beforeUpdate();
02045 
02046         $iStockCount = $this->oxarticles__oxstock->value - $dAmount;
02047         if (!$blAllowNegativeStock && ($iStockCount < 0)) {
02048             $dAmount += $iStockCount;
02049             $iStockCount = 0;
02050         }
02051         $this->oxarticles__oxstock = new oxField($iStockCount);
02052 
02053         $oDb = oxDb::getDb();
02054         $oDb->execute( 'update oxarticles set oxarticles.oxstock = '.$oDb->quote( $iStockCount ).' where oxarticles.oxid = '.$oDb->quote( $this->getId() ) );
02055         $this->onChange( ACTION_UPDATE_STOCK );
02056         return $dAmount;
02057     }
02058 
02067     public function updateSoldAmount( $dAmount = 0 )
02068     {
02069         if ( !$dAmount ) {
02070             return null;
02071         }
02072 
02073         // article is not variant - should be updated current amount
02074         if ( !$this->oxarticles__oxparentid->value ) {
02075             //updating by SQL query, due to wrong behaviour if saving article using not admin mode
02076             $dAmount = (double) $dAmount;
02077             $oDb = oxDb::getDb();
02078             $rs = $oDb->execute( "update oxarticles set oxarticles.oxsoldamount = oxarticles.oxsoldamount + $dAmount where oxarticles.oxid = ".$oDb->quote($this->oxarticles__oxid->value));
02079         } elseif ( $this->oxarticles__oxparentid->value) {
02080             // article is variant - should be updated this article parent amount
02081             $oUpdateArticle = $this->getParentArticle();
02082             $oUpdateArticle->updateSoldAmount( $dAmount );
02083         }
02084 
02085         return $rs;
02086     }
02087 
02093     public function disableReminder()
02094     {
02095         $oDb = oxDb::getDb();
02096         return $oDb->execute( "update oxarticles set oxarticles.oxremindactive = 2 where oxarticles.oxid = ".$oDb->quote($this->oxarticles__oxid->value));
02097     }
02098 
02104     public function save()
02105     {
02106         $this->_assignParentDependFields();
02107 
02108         if ( ( $blRet = parent::save() ) ) {
02109             // saving long description
02110             $this->_saveArtLongDesc();
02111         }
02112 
02113         return $blRet;
02114     }
02115 
02121     public function resetParent()
02122     {
02123         $sParentId = $this->oxarticles__oxparentid->value;
02124         $this->oxarticles__oxparentid = new oxField( '', oxField::T_RAW );
02125         $this->_blAllowEmptyParentId = true;
02126         $this->save();
02127         $this->_blAllowEmptyParentId = false;
02128 
02129         if ( $sParentId !== '' ) {
02130             $this->onChange( ACTION_UPDATE, null, $sParentId );
02131         }
02132     }
02133 
02134 
02141     public function getPictureGallery()
02142     {
02143         $myConfig = $this->getConfig();
02144 
02145         //initialize
02146         $blMorePic = false;
02147         $aArtPics  = array();
02148         $aArtIcons = array();
02149         $iActPicId = 1;
02150         $sActPic = $this->getPictureUrl( $iActPicId );
02151 
02152         if ( oxConfig::getParameter( 'actpicid' ) ) {
02153             $iActPicId = oxConfig::getParameter('actpicid');
02154         }
02155 
02156         $oStr = getStr();
02157         $iCntr = 0;
02158         $iPicCount = $myConfig->getConfigParam( 'iPicCount' );
02159         $blCheckActivePicId = true;
02160 
02161         for ( $i = 1; $i <= $iPicCount; $i++) {
02162             $sPicVal = $this->getPictureUrl( $i );
02163             $sIcoVal = $this->getIconUrl( $i );
02164             if ( !$oStr->strstr($sIcoVal, 'nopic_ico.jpg') && !$oStr->strstr($sIcoVal, 'nopic.jpg') &&
02165                  !$oStr->strstr($sPicVal, 'nopic_ico.jpg') && !$oStr->strstr($sPicVal, 'nopic.jpg') ) {
02166                 if ($iCntr) {
02167                     $blMorePic = true;
02168                 }
02169                 $aArtIcons[$i]= $sIcoVal;
02170                 $aArtPics[$i]= $sPicVal;
02171                 $iCntr++;
02172 
02173                 if ($iActPicId == $i) {
02174                     $sActPic = $sPicVal;
02175                     $blCheckActivePicId = false;
02176                 }
02177 
02178             } else if ( $blCheckActivePicId && $iActPicId <= $i) {
02179                 // if picture is empty, setting active pic id to next
02180                 // picture
02181                 $iActPicId++;
02182             }
02183         }
02184 
02185         $blZoomPic  = false;
02186         $aZoomPics = array();
02187         $iZoomPicCount = $myConfig->getConfigParam( 'iPicCount' );
02188 
02189         for ( $j = 1,$c = 1; $j <= $iZoomPicCount; $j++) {
02190             $sVal = $this->getZoomPictureUrl($j);
02191 
02192             if ( $sVal && !$oStr->strstr($sVal, 'nopic.jpg')) {
02193                 $blZoomPic = true;
02194                 $aZoomPics[$c]['id'] = $c;
02195                 $aZoomPics[$c]['file'] = $sVal;
02196                 //anything is better than empty name, because <img src=""> calls shop once more = x2 SLOW.
02197                 if (!$sVal) {
02198                     $aZoomPics[$c]['file'] = "nopic.jpg";
02199                 }
02200                 $c++;
02201             }
02202         }
02203 
02204         $aPicGallery = array('ActPicID' => $iActPicId,
02205                              'ActPic' => $sActPic,
02206                              'MorePics' => $blMorePic,
02207                              'Pics' => $aArtPics,
02208                              'Icons' => $aArtIcons,
02209                              'ZoomPic' => $blZoomPic,
02210                              'ZoomPics' => $aZoomPics);
02211 
02212         return $aPicGallery;
02213     }
02214 
02228     public function onChange($sAction = null, $sOXID = null, $sParentID = null)
02229     {
02230         $myConfig = $this->getConfig();
02231 
02232         if (!isset($sOXID)) {
02233             if ( $this->getId()) {
02234                 $sOXID = $this->getId();
02235             }
02236             if (!isset ($sOXID)) {
02237                 $sOXID = $this->oxarticles__oxid->value;
02238             }
02239             if ($this->oxarticles__oxparentid->value) {
02240                 $sParentID = $this->oxarticles__oxparentid->value;
02241             }
02242         }
02243         if (!isset($sOXID)) {
02244             return;
02245         }
02246 
02247         //if (isset($sOXID) && !$myConfig->blVariantParentBuyable && $myConfig->blUseStock)
02248         if ( $myConfig->getConfigParam( 'blUseStock' ) ) {
02249             //if article has variants then updating oxvarstock field
02250             //getting parent id
02251             if (!isset($sParentID)) {
02252                 $oDb = oxDb::getDb();
02253                 $sQ = 'select oxparentid from oxarticles where oxid = '.$oDb->quote($sOXID);
02254                 $sParentID = $oDb->getOne( $sQ );
02255             }
02256             //if we have parent id then update stock
02257             if ($sParentID) {
02258                 $this->_onChangeUpdateStock($sParentID);
02259             }
02260         }
02261         //if we have parent id then update count
02262         //update count even if blUseStock is not active
02263         if ($sParentID) {
02264             $this->_onChangeUpdateVarCount($sParentID);
02265         }
02266 
02267         $sId = ( $sParentID ) ? $sParentID : $sOXID;
02268         $this->_setVarMinMaxPrice( $sId );
02269 
02270         $this->_updateParentDependFields();
02271 
02272         // resetting articles count cache if stock has changed and some
02273         // articles goes offline (M:1448)
02274         if ( $sAction === ACTION_UPDATE_STOCK ) {
02275             $this->_assignStock();
02276             $this->_onChangeStockResetCount( $sOXID );
02277         }
02278 
02279     }
02280 
02287     public function getCustomVAT()
02288     {
02289         if ( isset($this->oxarticles__oxvat->value) ) {
02290             return $this->oxarticles__oxvat->value;
02291         }
02292 
02293         return null;
02294     }
02295 
02304     public function checkForStock( $dAmount, $dArtStockAmount = 0 )
02305     {
02306         $myConfig = $this->getConfig();
02307         if ( !$myConfig->getConfigParam( 'blUseStock' ) ) {
02308             return true;
02309         }
02310 
02311         $oDb = oxDb::getDb( oxDb::FETCH_MODE_ASSOC );
02312         // fetching DB info as its up-to-date
02313         $sQ = 'select oxstock, oxstockflag from oxarticles where oxid = '.$oDb->quote( $this->getId() );
02314         $rs = $oDb->select( $sQ );
02315 
02316         $iOnStock   = 0;
02317         $iStockFlag = 0;
02318         if ( $rs !== false && $rs->recordCount() > 0 ) {
02319             $iOnStock   = $rs->fields['oxstock'] - $dArtStockAmount;
02320             $iStockFlag = $rs->fields['oxstockflag'];
02321 
02322             // foreign stock is also always considered as on stock
02323             if ( $iStockFlag == 1 || $iStockFlag == 4) {
02324                 return true;
02325             }
02326             if ( !$myConfig->getConfigParam( 'blAllowUnevenAmounts' ) ) {
02327                 $iOnStock = floor( $iOnStock );
02328             }
02329         }
02330         if ($this->getConfig()->getConfigParam( 'blPsBasketReservationEnabled' )) {
02331             $iOnStock += $this->getSession()->getBasketReservations()->getReservedAmount($this->getId());
02332         }
02333         if ( $iOnStock >= $dAmount ) {
02334             return true;
02335         } else {
02336             if ( $iOnStock > 0 ) {
02337                 return $iOnStock;
02338             } else {
02339                 $oEx = oxNew( 'oxArticleInputException' );
02340                 $oEx->setMessage( 'ERROR_MESSAGE_ARTICLE_ARTICLE_NOT_BUYABLE' );
02341                 oxRegistry::get("oxUtilsView")->addErrorToDisplay( $oEx );
02342                 return false;
02343             }
02344         }
02345     }
02346 
02347 
02353     public function getLongDescription()
02354     {
02355         if ( $this->_oLongDesc === null ) {
02356             // initializing
02357             $this->_oLongDesc = new oxField();
02358 
02359 
02360             // choosing which to get..
02361             $sOxid = $this->getId();
02362             $sViewName = getViewName( 'oxartextends', $this->getLanguage() );
02363 
02364             $oDb = oxDb::getDb();
02365             $sDbValue = $oDb->getOne( "select oxlongdesc from {$sViewName} where oxid = " . $oDb->quote( $sOxid ) );
02366 
02367             if ( $sDbValue != false ) {
02368                 $this->_oLongDesc->setValue( $sDbValue, oxField::T_RAW );
02369             } elseif ( $this->oxarticles__oxparentid->value ) {
02370                 if ( !$this->isAdmin() || $this->_blLoadParentData ) {
02371                     $this->_oLongDesc->setValue( $this->getParentArticle()->getLongDescription()->getRawValue(), oxField::T_RAW );
02372                 }
02373             }
02374         }
02375         return $this->_oLongDesc;
02376     }
02377 
02384     public function getLongDesc()
02385     {
02386         return oxRegistry::get("oxUtilsView")->parseThroughSmarty( $this->getLongDescription()->getRawValue(), $this->getId().$this->getLanguage(), null, true );
02387     }
02388 
02396     public function setArticleLongDesc( $sDesc )
02397     {
02398 
02399         // setting current value
02400         $this->_oLongDesc = new oxField( $sDesc, oxField::T_RAW );
02401         $this->oxarticles__oxlongdesc = new oxField( $sDesc, oxField::T_RAW );
02402 
02403         return null;
02404     }
02405 
02411     public function getAttributes()
02412     {
02413         if ( $this->_oAttributeList === null ) {
02414             $this->_oAttributeList = oxNew( 'oxattributelist' );
02415             $this->_oAttributeList->loadAttributes( $this->getId(), $this->getParentId() );
02416         }
02417 
02418         return $this->_oAttributeList;
02419     }
02420 
02426     public function getAttributesDisplayableInBasket()
02427     {
02428         if ( $this->_oAttributeList === null ) {
02429             $this->_oAttributeList = oxNew( 'oxattributelist' );
02430             $this->_oAttributeList->loadAttributesDisplayableInBasket( $this->getId(), $this->getParentId() );
02431         }
02432 
02433         return $this->_oAttributeList;
02434     }
02435 
02436 
02445     public function appendLink( $sAddParams, $iLang = null )
02446     {
02447         if ( $sAddParams ) {
02448             if ( $iLang === null ) {
02449                 $iLang = $this->getLanguage();
02450             }
02451 
02452             $this->_aSeoAddParams[$iLang]  = isset( $this->_aSeoAddParams[$iLang] ) ? $this->_aSeoAddParams[$iLang] . "&amp;" : "";
02453             $this->_aSeoAddParams[$iLang] .= $sAddParams;
02454         }
02455     }
02456 
02465     public function getBaseSeoLink( $iLang, $blMain = false )
02466     {
02467         $oEncoder = oxRegistry::get("oxSeoEncoderArticle");
02468         if ( !$blMain ) {
02469             return $oEncoder->getArticleUrl( $this, $iLang, $this->getLinkType() );
02470         }
02471         return $oEncoder->getArticleMainUrl( $this, $iLang );
02472     }
02473 
02482     public function getLink( $iLang = null, $blMain = false  )
02483     {
02484         if ( !oxRegistry::getUtils()->seoIsActive() ) {
02485             return $this->getStdLink( $iLang );
02486         }
02487 
02488         if ( $iLang === null ) {
02489             $iLang = $this->getLanguage();
02490         }
02491 
02492         $iLinkType = $this->getLinkType();
02493         if ( !isset( $this->_aSeoUrls[$iLang][$iLinkType] ) ) {
02494             $this->_aSeoUrls[$iLang][$iLinkType] = $this->getBaseSeoLink( $iLang, $blMain );
02495         }
02496 
02497         $sUrl = $this->_aSeoUrls[$iLang][$iLinkType];
02498         if ( isset($this->_aSeoAddParams[$iLang])) {
02499             $sUrl .= ( ( strpos( $sUrl.$this->_aSeoAddParams[$iLang], '?' ) === false ) ? '?' : '&amp;' ).$this->_aSeoAddParams[$iLang];
02500         }
02501 
02502         return $sUrl;
02503     }
02504 
02513     public function getMainLink( $iLang = null )
02514     {
02515         return $this->getLink( $iLang, true );
02516     }
02517 
02525     public function setLinkType( $iType )
02526     {
02527         // resetting details link, to force new
02528         $this->_sDetailLink = null;
02529 
02530         // setting link type
02531         $this->_iLinkType = (int) $iType;
02532     }
02533 
02539     public function getLinkType()
02540     {
02541         return $this->_iLinkType;
02542     }
02543 
02552     public function appendStdLink( $sAddParams, $iLang = null )
02553     {
02554         if ( $sAddParams ) {
02555             if ( $iLang === null ) {
02556                 $iLang = $this->getLanguage();
02557             }
02558 
02559             $this->_aStdAddParams[$iLang]  = isset( $this->_aStdAddParams[$iLang] ) ? $this->_aStdAddParams[$iLang] . "&amp;" : "";
02560             $this->_aStdAddParams[$iLang] .= $sAddParams;
02561         }
02562     }
02563 
02573     public function getBaseStdLink( $iLang, $blAddId = true, $blFull = true )
02574     {
02575         $sUrl = '';
02576         if ( $blFull ) {
02577             //always returns shop url, not admin
02578             $sUrl = $this->getConfig()->getShopUrl( $iLang, false );
02579         }
02580 
02581         $sUrl .= 'index.php?cl=details' . ( $blAddId ? '&amp;anid=' . urlencode($this->getId()) : '' );
02582         return $sUrl . ( isset( $this->_aStdAddParams[$iLang] ) ? '&amp;' . $this->_aStdAddParams[$iLang] : '' );
02583     }
02584 
02593     public function getStdLink( $iLang = null, $aParams = array() )
02594     {
02595         if ( $iLang === null ) {
02596             $iLang = $this->getLanguage();
02597         }
02598 
02599         if ( !isset( $this->_aStdUrls[$iLang] ) ) {
02600             $this->_aStdUrls[$iLang] = $this->getBaseStdLink( $iLang );
02601         }
02602 
02603         return oxRegistry::get("oxUtilsUrl")->processUrl( $this->_aStdUrls[$iLang], true, $aParams, $iLang );
02604     }
02605 
02615     public function getStdTagLink( $sTag )
02616     {
02617         $oArticleTags = oxNew('oxarticletaglist');
02618         $oArticleTags->setArticleId( $this->getId() );
02619         return $oArticleTags->getStdTagLink($sTag);
02620     }
02621 
02629     public function getTags()
02630     {
02631         $oArticleTags = oxNew('oxarticletaglist');
02632         $oArticleTags->load( $this->getId() );
02633         return $oArticleTags->get()->__toString();
02634     }
02635 
02645     public function saveTags($sTags)
02646     {
02647         //do not allow derived update
02648         if ( !$this->allowDerivedUpdate() ) {
02649             return false;
02650         }
02651         $oArticleTags = oxNew('oxarticletaglist');
02652         $oArticleTags->setArticleId( $this->getId() );
02653         $oArticleTags->set( $sTags );
02654         $oArticleTags->save();
02655     }
02656 
02666     public function addTag($sTag)
02667     {
02668         $oArticleTags = oxNew('oxarticletaglist');
02669         $oArticleTags->load( $this->getId() );
02670         $oArticleTags->addTag( $sTag );
02671         if ( $oArticleTags->save() ) {
02672             return true;
02673         }
02674         return false;
02675     }
02676 
02682     public function getMediaUrls()
02683     {
02684         if ( $this->_aMediaUrls === null ) {
02685             $this->_aMediaUrls = oxNew("oxlist");
02686             $this->_aMediaUrls->init("oxmediaurl");
02687             $this->_aMediaUrls->getBaseObject()->setLanguage( $this->getLanguage() );
02688 
02689             $sViewName = getViewName( "oxmediaurls", $this->getLanguage() );
02690             $sQ = "select * from {$sViewName} where oxobjectid = '".$this->getId()."'";
02691             $this->_aMediaUrls->selectString($sQ);
02692         }
02693         return $this->_aMediaUrls;
02694     }
02695 
02701     public function getDynImageDir()
02702     {
02703         return $this->_sDynImageDir;
02704     }
02705 
02711     public function getDispSelList()
02712     {
02713         if ($this->_aDispSelList === null) {
02714             if ( $this->getConfig()->getConfigParam( 'bl_perfLoadSelectLists' ) && $this->getConfig()->getConfigParam( 'bl_perfLoadSelectListsInAList' ) ) {
02715                 $this->_aDispSelList = $this->getSelectLists();
02716             }
02717         }
02718         return $this->_aDispSelList;
02719     }
02720 
02726     public function getMoreDetailLink()
02727     {
02728         if ( $this->_sMoreDetailLink == null ) {
02729 
02730             // and assign special article values
02731             $this->_sMoreDetailLink = $this->getConfig()->getShopHomeURL() . 'cl=moredetails';
02732 
02733             // not always it is okey, as not all the time active category is the same as primary article cat.
02734             if ( $sActCat = oxConfig::getParameter( 'cnid' ) ) {
02735                 $this->_sMoreDetailLink .= '&amp;cnid='.$sActCat;
02736             }
02737             $this->_sMoreDetailLink .= '&amp;anid='.$this->getId();
02738             $this->_sMoreDetailLink = $this->_sMoreDetailLink;
02739         }
02740 
02741         return $this->_sMoreDetailLink;
02742     }
02743 
02749     public function getToBasketLink()
02750     {
02751         if ( $this->_sToBasketLink == null ) {
02752             $myConfig = $this->getConfig();
02753 
02754             if ( oxRegistry::getUtils()->isSearchEngine() ) {
02755                 $this->_sToBasketLink = $this->getLink();
02756             } else {
02757                 // and assign special article values
02758                 $this->_sToBasketLink = $myConfig->getShopHomeURL();
02759 
02760                 // override some classes as these should never showup
02761                 $sActClass = oxConfig::getParameter( 'cl' );
02762                 if ( $sActClass == 'thankyou') {
02763                     $sActClass = 'basket';
02764                 }
02765                 $this->_sToBasketLink .= 'cl='.$sActClass;
02766 
02767                 // this is not very correct
02768                 if ( $sActCat = oxConfig::getParameter( 'cnid' ) ) {
02769                     $this->_sToBasketLink .= '&amp;cnid='.$sActCat;
02770                 }
02771 
02772                 $this->_sToBasketLink .= '&amp;fnc=tobasket&amp;aid='.$this->getId().'&amp;anid='.$this->getId();
02773 
02774                 if ( $sTpl = basename( oxConfig::getParameter( 'tpl' ) ) ) {
02775                     $this->_sToBasketLink .= '&amp;tpl='.$sTpl;
02776                 }
02777             }
02778         }
02779 
02780         return $this->_sToBasketLink;
02781     }
02782 
02788     public function getStockStatus()
02789     {
02790         return $this->_iStockStatus;
02791     }
02792 
02798     protected function _isStockStatusChanged()
02799     {
02800         return $this->_iStockStatus != $this->_iStockStatusOnLoad;
02801     }
02802 
02808     protected function _isVisibilityChanged()
02809     {
02810         return $this->_isStockStatusChanged() && ($this->_iStockStatus == -1 || $this->_iStockStatusOnLoad == -1);
02811     }
02812 
02818     public function getDeliveryDate()
02819     {
02820         if ( $this->oxarticles__oxdelivery->value != '0000-00-00') {
02821             return oxRegistry::get("oxUtilsDate")->formatDBDate( $this->oxarticles__oxdelivery->value);
02822         }
02823         return false;
02824     }
02825 
02833     public function getFTPrice()
02834     {
02835         // module
02836         if ( $oPrice = $this->getTPrice() ) {
02837             if ( $dPrice = $this->_getPriceForView( $oPrice ) ) {
02838                 return oxRegistry::getLang()->formatCurrency( $dPrice );
02839             }
02840         }
02841     }
02842 
02850     public function getFPrice()
02851     {
02852         if ( $oPrice = $this->getPrice() ) {
02853             $dPrice = $this->_getPriceForView( $oPrice );
02854             return oxRegistry::getLang()->formatCurrency( $dPrice );
02855         }
02856     }
02857 
02864     public function resetRemindStatus()
02865     {
02866         if ( $this->oxarticles__oxremindactive->value == 2 &&
02867             $this->oxarticles__oxremindamount->value <= $this->oxarticles__oxstock->value ) {
02868             $this->oxarticles__oxremindactive->value = 1;
02869         }
02870 
02871         return null;
02872     }
02873 
02881     public function getFNetPrice()
02882     {
02883         if ( $oPrice = $this->getPrice() ) {
02884             return oxRegistry::getLang()->formatCurrency( $oPrice->getNettoPrice() );
02885         }
02886     }
02887 
02895     public function getPricePerUnit()
02896     {
02897         return $this->getFUnitPrice();
02898     }
02899 
02905     public function isParentNotBuyable()
02906     {
02907         return $this->_blNotBuyableParent;
02908     }
02909 
02915     public function isNotBuyable()
02916     {
02917         return $this->_blNotBuyable;
02918     }
02919 
02927     public function setBuyableState( $blBuyable = false )
02928     {
02929         $this->_blNotBuyable = !$blBuyable;
02930     }
02931 
02939     public function setSelectlist( $aSelList )
02940     {
02941         $this->_aDispSelList = $aSelList;
02942     }
02943 
02951     public function getPictureUrl( $iIndex = 1 )
02952     {
02953         if ( $iIndex ) {
02954             $sImgName = false;
02955             if ( !$this->_isFieldEmpty( "oxarticles__oxpic".$iIndex ) ) {
02956                 $sImgName = basename( $this->{"oxarticles__oxpic$iIndex"}->value );
02957             }
02958 
02959             $sSize = $this->getConfig()->getConfigParam( 'aDetailImageSizes' );
02960             return oxRegistry::get("oxPictureHandler")->getProductPicUrl( "product/{$iIndex}/", $sImgName, $sSize, 'oxpic'.$iIndex );
02961         }
02962     }
02963 
02972     public function getIconUrl( $iIndex = 0 )
02973     {
02974         $sImgName = false;
02975         $sDirname = "product/1/";
02976         if ( $iIndex && !$this->_isFieldEmpty( "oxarticles__oxpic{$iIndex}" ) ) {
02977             $sImgName = basename( $this->{"oxarticles__oxpic$iIndex"}->value );
02978             $sDirname = "product/{$iIndex}/";
02979         } elseif ( !$this->_isFieldEmpty( "oxarticles__oxicon" ) ) {
02980             $sImgName = basename( $this->oxarticles__oxicon->value );
02981             $sDirname = "product/icon/";
02982         } elseif ( !$this->_isFieldEmpty( "oxarticles__oxpic1" ) ) {
02983             $sImgName = basename( $this->oxarticles__oxpic1->value );
02984         }
02985 
02986         $sSize = $this->getConfig()->getConfigParam( 'sIconsize' );
02987         return oxRegistry::get("oxPictureHandler")->getProductPicUrl( $sDirname, $sImgName, $sSize, $iIndex );
02988     }
02989 
02997     public function getThumbnailUrl( $bSsl = null )
02998     {
02999         $sImgName = false;
03000         $sDirname = "product/1/";
03001         if ( !$this->_isFieldEmpty( "oxarticles__oxthumb" ) ) {
03002             $sImgName = basename( $this->oxarticles__oxthumb->value );
03003             $sDirname = "product/thumb/";
03004         } elseif ( !$this->_isFieldEmpty( "oxarticles__oxpic1" ) ) {
03005             $sImgName = basename( $this->oxarticles__oxpic1->value );
03006         }
03007 
03008         $sSize = $this->getConfig()->getConfigParam( 'sThumbnailsize' );
03009         return oxRegistry::get("oxPictureHandler")->getProductPicUrl( $sDirname, $sImgName, $sSize, 0, $bSsl );
03010     }
03011 
03019     public function getZoomPictureUrl( $iIndex = '' )
03020     {
03021         $iIndex = (int) $iIndex;
03022         if ( $iIndex > 0 && !$this->_isFieldEmpty( "oxarticles__oxpic".$iIndex ) ) {
03023             $sImgName = basename( $this->{"oxarticles__oxpic".$iIndex}->value );
03024             $sSize = $this->getConfig()->getConfigParam( "sZoomImageSize" );
03025             return oxRegistry::get("oxPictureHandler")->getProductPicUrl( "product/{$iIndex}/", $sImgName, $sSize, 'oxpic'.$iIndex );
03026         }
03027     }
03028 
03036     public function getFileUrl()
03037     {
03038         return $this->getConfig()->getPictureUrl( 'media/' );
03039     }
03040 
03048     public function getPriceFromPrefix()
03049     {
03050         $sPricePrefix = '';
03051         if ( $this->_blIsRangePrice) {
03052             $sPricePrefix = oxLang::getInstance()->translateString('PRICE_FROM').' ';
03053         }
03054 
03055         return $sPricePrefix;
03056     }
03057 
03063     protected function _saveArtLongDesc()
03064     {
03065         $myConfig = $this->getConfig();
03066         $sShopId = $myConfig->getShopID();
03067         if (in_array("oxlongdesc", $this->_aSkipSaveFields)) {
03068             return;
03069         }
03070 
03071         if ($this->_blEmployMultilanguage) {
03072             $sValue = $this->getLongDescription()->getRawValue();
03073             if ( $sValue !== null ) {
03074                 $oArtExt = oxNew('oxI18n');
03075                 $oArtExt->init('oxartextends');
03076                 $oArtExt->setLanguage((int) $this->getLanguage());
03077                 if (!$oArtExt->load($this->getId())) {
03078                     $oArtExt->setId($this->getId());
03079                 }
03080                 $oArtExt->oxartextends__oxlongdesc = new oxField($sValue, oxField::T_RAW);
03081                 $oArtExt->save();
03082             }
03083         } else {
03084             $oArtExt = oxNew('oxI18n');
03085             $oArtExt->setEnableMultilang(false);
03086             $oArtExt->init('oxartextends');
03087             $aObjFields = $oArtExt->_getAllFields(true);
03088             if (!$oArtExt->load($this->getId())) {
03089                 $oArtExt->setId($this->getId());
03090             }
03091 
03092             foreach ($aObjFields as $sKey => $sValue ) {
03093                 if ( preg_match('/^oxlongdesc(_(\d{1,2}))?$/', $sKey) ) {
03094                     $sField = $this->_getFieldLongName($sKey);
03095 
03096                     if (isset($this->$sField)) {
03097                         $sLongDesc = null;
03098                         if ($this->$sField instanceof oxField) {
03099                             $sLongDesc = $this->$sField->getRawValue();
03100                         } elseif (is_object($this->$sField)) {
03101                             $sLongDesc = $this->$sField->value;
03102                         }
03103                         if (isset($sLongDesc)) {
03104                             $sAEField = $oArtExt->_getFieldLongName($sKey);
03105                             $oArtExt->$sAEField = new oxField($sLongDesc, oxField::T_RAW);
03106                         }
03107                     }
03108                 }
03109             }
03110             $oArtExt->save();
03111         }
03112     }
03113 
03119     protected function _skipSaveFields()
03120     {
03121         $myConfig = $this->getConfig();
03122 
03123         $this->_aSkipSaveFields = array();
03124 
03125         $this->_aSkipSaveFields[] = 'oxtimestamp';
03126        // $this->_aSkipSaveFields[] = 'oxlongdesc';
03127         $this->_aSkipSaveFields[] = 'oxinsert';
03128 
03129         if ( !$this->_blAllowEmptyParentId && (!isset( $this->oxarticles__oxparentid->value) || $this->oxarticles__oxparentid->value == '') ) {
03130             $this->_aSkipSaveFields[] = 'oxparentid';
03131         }
03132 
03133     }
03134 
03144     protected function _mergeDiscounts( $aDiscounts, $aItemDiscounts)
03145     {
03146         foreach ( $aItemDiscounts as $sKey => $oDiscount ) {
03147             // add prices of the same discounts
03148             if ( array_key_exists ($sKey, $aDiscounts) ) {
03149                 $aDiscounts[$sKey]->dDiscount += $oDiscount->dDiscount;
03150             } else {
03151                 $aDiscounts[$sKey] = $oDiscount;
03152             }
03153         }
03154         return $aDiscounts;
03155     }
03156 
03162     protected function _getGroupPrice()
03163     {
03164         $sPriceSufix = $this->_getUserPriceSufix();
03165         $sVarName = "oxarticles__oxprice{$sPriceSufix}";
03166         $dPrice = $this->$sVarName->value;
03167 
03168         // #1437/1436C - added config option, and check for zero A,B,C price values
03169         if ( $this->getConfig()->getConfigParam( 'blOverrideZeroABCPrices' ) && (double) $dPrice == 0 ) {
03170             $dPrice = $this->oxarticles__oxprice->value;
03171         }
03172 
03173         return $dPrice;
03174     }
03175 
03184     protected function _getAmountPrice($dAmount = 1)
03185     {
03186         startProfile( "_getAmountPrice" );
03187 
03188         $dPrice = $this->_getGroupPrice();
03189         $oAmtPrices = $this->_getAmountPriceList();
03190         foreach ($oAmtPrices as $oAmPrice) {
03191             if ($oAmPrice->oxprice2article__oxamount->value <= $dAmount
03192                     && $dAmount <= $oAmPrice->oxprice2article__oxamountto->value
03193                     && $dPrice > $oAmPrice->oxprice2article__oxaddabs->value ) {
03194                 $dPrice = $oAmPrice->oxprice2article__oxaddabs->value;
03195             }
03196         }
03197 
03198         stopProfile( "_getAmountPrice" );
03199         return $dPrice;
03200     }
03201 
03210     protected function _modifySelectListPrice( $dPrice, $aChosenList = null )
03211     {
03212         $myConfig = $this->getConfig();
03213         // #690
03214         if ( $myConfig->getConfigParam( 'bl_perfLoadSelectLists' ) && $myConfig->getConfigParam( 'bl_perfUseSelectlistPrice' ) ) {
03215 
03216             $aSelLists = $this->getSelectLists();
03217 
03218             foreach ( $aSelLists as $key => $aSel) {
03219                 if ( isset( $aChosenList[$key]) && isset($aSel[$aChosenList[$key]] ) ) {
03220                     $oSel = $aSel[$aChosenList[$key]];
03221                     if ( $oSel->priceUnit =='abs' ) {
03222                         $dPrice += $oSel->price;
03223                     } elseif ( $oSel->priceUnit =='%' ) {
03224                         $dPrice += oxPrice::percent( $dPrice, $oSel->price );
03225                     }
03226                 }
03227             }
03228         }
03229         return $dPrice;
03230     }
03231 
03232 
03240     protected function _fillAmountPriceList($aAmPriceList)
03241     {
03242         $oLang = oxRegistry::getLang();
03243 
03244         // trying to find lowest price value
03245         foreach ($aAmPriceList as $sId => $oItem) {
03246 
03247             $oItemPrice = $this->_getPriceObject();
03248             if ( $oItem->oxprice2article__oxaddabs->value ) {
03249 
03250                 $dBasePrice = $oItem->oxprice2article__oxaddabs->value;
03251                 $dBasePrice = $this->_preparePrice( $dBasePrice, $this->getArticleVat() );
03252 
03253                 $oItemPrice->setPrice( $dBasePrice );
03254                 $this->_calculatePrice( $oItemPrice );
03255 
03256             } else {
03257                 $dBasePrice = $this->_getGroupPrice();
03258                 $dBasePrice = $this->_preparePrice( $dBasePrice, $this->getArticleVat() );
03259                 $oItemPrice->setPrice( $dBasePrice );
03260                 $oItemPrice->subtractPercent( $oItem->oxprice2article__oxaddperc->value );
03261             }
03262 
03263 
03264             $aAmPriceList[$sId]->fbrutprice = $oLang->formatCurrency( $this->_getPriceForView( $oItemPrice ) );
03265         }
03266 
03267         return $aAmPriceList;
03268     }
03269 
03270 
03276     protected function _getVariantsIds()
03277     {
03278         $aSelect = array();
03279         if ( ( $sId = $this->getId() ) ) {
03280             $oDb = oxDb::getDb( oxDb::FETCH_MODE_ASSOC );
03281             $sQ = "select oxid from " . $this->getViewName( true ) . " where oxparentid = ".$oDb->quote( $sId )." and " .
03282                    $this->getSqlActiveSnippet( true ) . " order by oxsort";
03283             $oRs = $oDb->select( $sQ );
03284             if ( $oRs != false && $oRs->recordCount() > 0 ) {
03285                 while (!$oRs->EOF) {
03286                     $aSelect[] = reset( $oRs->fields );
03287                     $oRs->moveNext();
03288                 }
03289             }
03290         }
03291         return $aSelect;
03292     }
03293 
03299     public function getArticleVat()
03300     {
03301         if (!isset($this->_dArticleVat)) {
03302             $this->_dArticleVat = oxRegistry::get("oxVatSelector")->getArticleVat( $this );
03303         }
03304         return $this->_dArticleVat;
03305     }
03306 
03315     protected function _applyVAT( oxPrice $oPrice, $dVat )
03316     {
03317         startProfile(__FUNCTION__);
03318         $oPrice->setVAT( $dVat );
03319         if ( ($dVat = oxRegistry::get("oxVatSelector")->getArticleUserVat($this)) !== false ) {
03320             $oPrice->setUserVat( $dVat );
03321         }
03322         stopProfile(__FUNCTION__);
03323     }
03324 
03332     public function applyVats( oxPrice $oPrice )
03333     {
03334         $this->_applyVAT($oPrice, $this->getArticleVat() );
03335     }
03336 
03344     public function applyDiscountsForVariant( $oPrice )
03345     {
03346         // apply discounts
03347         if ( !$this->skipDiscounts() ) {
03348             $oDiscountList = oxRegistry::get("oxDiscountList");
03349             $aDiscounts = $oDiscountList->getArticleDiscounts( $this, $this->getArticleUser() );
03350 
03351             reset( $aDiscounts );
03352             foreach ( $aDiscounts as $oDiscount ) {
03353                 $oPrice->setDiscount($oDiscount->getAddSum(), $oDiscount->getAddSumType());
03354             }
03355             $oPrice->calculateDiscount();
03356         }
03357     }
03358 
03367     protected function _applyCurrency(oxPrice $oPrice, $oCur = null )
03368     {
03369         if ( !$oCur ) {
03370             $oCur = $this->getConfig()->getActShopCurrencyObject();
03371         }
03372 
03373         $oPrice->multiply($oCur->rate);
03374     }
03375 
03376 
03385     protected function _getAttribsString(&$sAttribs, &$iCnt)
03386     {
03387         // we do not use lists here as we dont need this overhead right now
03388         $oDb = oxDb::getDb( oxDb::FETCH_MODE_ASSOC );
03389         $sSelect =  'select oxattrid from oxobject2attribute where oxobject2attribute.oxobjectid='.$oDb->quote( $this->getId() );
03390         $sAttribs = '';
03391         $blSep = false;
03392         $rs = $oDb->select( $sSelect);
03393         $iCnt = 0;
03394         if ($rs != false && $rs->recordCount() > 0) {
03395             while (!$rs->EOF) {
03396                 if ( $blSep) {
03397                     $sAttribs .= ' or ';
03398                 }
03399                 $sAttribs .= 't1.oxattrid = '.$oDb->quote($rs->fields['oxattrid']).' ';
03400                 $blSep = true;
03401                 $iCnt++;
03402                 $rs->moveNext();
03403             }
03404         }
03405     }
03406 
03415     protected function _getSimList($sAttribs, $iCnt)
03416     {
03417         $myConfig = $this->getConfig();
03418         $oDb      = oxDb::getDb( oxDb::FETCH_MODE_ASSOC );
03419 
03420         // #523A
03421         $iAttrPercent = $myConfig->getConfigParam( 'iAttributesPercent' )/100;
03422         // 70% same attributes
03423         if ( !$iAttrPercent || $iAttrPercent < 0 || $iAttrPercent > 1) {
03424             $iAttrPercent = 0.70;
03425         }
03426         // #1137V iAttributesPercent = 100 doesnt work
03427         $iHitMin = ceil( $iCnt * $iAttrPercent );
03428 
03429         // we do not use lists here as we don't need this overhead right now
03430         $aList= array();
03431         $sSelect =  "select oxobjectid, count(*) as cnt from oxobject2attribute as t1 where
03432                     ( $sAttribs )
03433                     and t1.oxobjectid != ".$oDb->quote( $this->oxarticles__oxid->value )."
03434                     group by t1.oxobjectid having count(*) >= $iHitMin ";
03435 
03436         $rs = $oDb->selectLimit( $sSelect, 20, 0 );
03437         if ($rs != false && $rs->recordCount() > 0) {
03438             while (!$rs->EOF) {
03439                 $oTemp = new stdClass();    // #663
03440                 $oTemp->cnt = $rs->fields['cnt'];
03441                 $oTemp->id  = $rs->fields['oxobjectid'];
03442                 $aList[] = $oTemp;
03443                 $rs->moveNext();
03444             }
03445         }
03446         return $aList;
03447     }
03448 
03457     protected function _generateSimListSearchStr($sArticleTable, $aList)
03458     {
03459         $myConfig = $this->getConfig();
03460         $sFieldList = $this->getSelectFields();
03461         $sSearch = "select $sFieldList from $sArticleTable where ".$this->getSqlActiveSnippet()."  and $sArticleTable.oxissearch = 1 and $sArticleTable.oxid in ( ";
03462         $blSep = false;
03463         $iCnt = 0;
03464         $oDb = oxDb::getDb();
03465         foreach ( $aList as $oTemp) {
03466             if ( $blSep) {
03467                 $sSearch .= ',';
03468             }
03469             $sSearch .= $oDb->quote($oTemp->id);
03470             $blSep = true;
03471             if ( $iCnt >= $myConfig->getConfigParam( 'iNrofSimilarArticles' ) ) {
03472                 break;
03473             }
03474             $iCnt++;
03475         }
03476 
03477         //#1741T
03478         //$sSearch .= ") and $sArticleTable.oxparentid = '' ";
03479         $sSearch .= ') ';
03480 
03481         // #524A -- randomizing articles in attribute list
03482         $sSearch .= ' order by rand() ';
03483 
03484         return $sSearch;
03485     }
03486 
03495     protected function _generateSearchStr($sOXID, $blSearchPriceCat = false )
03496     {
03497 
03498         $sCatView = getViewName( 'oxcategories', $this->getLanguage() );
03499         $sO2CView = getViewName( 'oxobject2category' );
03500 
03501         // we do not use lists here as we don't need this overhead right now
03502         if ( !$blSearchPriceCat ) {
03503             $sSelect  = "select {$sCatView}.* from {$sO2CView} as oxobject2category left join {$sCatView} on
03504                          {$sCatView}.oxid = oxobject2category.oxcatnid
03505                          where oxobject2category.oxobjectid=".oxDb::getDb()->quote($sOXID)." and {$sCatView}.oxid is not null ";
03506         } else {
03507             $sSelect  = "select {$sCatView}.* from {$sCatView} where
03508                          '{$this->oxarticles__oxprice->value}' >= {$sCatView}.oxpricefrom and
03509                          '{$this->oxarticles__oxprice->value}' <= {$sCatView}.oxpriceto ";
03510         }
03511         return $sSelect;
03512     }
03513 
03519     protected function _generateSearchStrForCustomerBought()
03520     {
03521         $sArtTable = $this->getViewName();
03522         $sOrderArtTable = getViewName( 'oxorderarticles' );
03523 
03524         // fetching filter params
03525         $sIn = " '{$this->oxarticles__oxid->value}' ";
03526         if ( $this->oxarticles__oxparentid->value ) {
03527 
03528             // adding article parent
03529             $sIn .= ", '{$this->oxarticles__oxparentid->value}' ";
03530             $sParentIdForVariants = $this->oxarticles__oxparentid->value;
03531 
03532         } else {
03533             $sParentIdForVariants = $this->getId();
03534         }
03535 
03536         // adding variants
03537         $oDb = oxDb::getDb( oxDb::FETCH_MODE_ASSOC );
03538         $oRs = $oDb->select( "select oxid from {$sArtTable} where oxparentid = ".$oDb->quote($sParentIdForVariants)." and oxid != ".$oDb->quote($this->oxarticles__oxid->value) );
03539         if ( $oRs != false && $oRs->recordCount() > 0) {
03540             while ( !$oRs->EOF ) {
03541                 $sIn .= ", ".$oDb->quote(current( $oRs->fields ))." ";
03542                 $oRs->moveNext();
03543             }
03544         }
03545 
03546         $iLimit = (int) $this->getConfig()->getConfigParam( 'iNrofCustomerWhoArticles' );
03547         $iLimit = $iLimit?( $iLimit * 10 ): 50;
03548 
03549         // building sql (optimized)
03550         $sQ = "select distinct {$sArtTable}.* from (
03551                    select d.oxorderid as suborderid from {$sOrderArtTable} as d use index ( oxartid ) where d.oxartid in ( {$sIn} ) limit {$iLimit}
03552                ) as suborder
03553                left join {$sOrderArtTable} force index ( oxorderid ) on suborder.suborderid = {$sOrderArtTable}.oxorderid
03554                left join {$sArtTable} on {$sArtTable}.oxid = {$sOrderArtTable}.oxartid
03555                where {$sArtTable}.oxid not in ( {$sIn} )
03556                and ( {$sArtTable}.oxissearch = 1 or {$sArtTable}.oxparentid <> '' ) and ".$this->getSqlActiveSnippet();
03557 
03558         /* non optimized, but could be used if index forcing is not supported
03559         // building sql
03560         $sQ = "select distinct {$sArtTable}.* from {$sOrderArtTable}, {$sArtTable} where {$sOrderArtTable}.oxorderid in (
03561                    select {$sOrderArtTable}.oxorderid from {$sOrderArtTable} where {$sOrderArtTable}.oxartid in ( {$sIn} )
03562                ) and {$sArtTable}.oxid = {$sOrderArtTable}.oxartid and {$sArtTable}.oxid not in ( {$sIn} )
03563                and ( {$sArtTable}.oxissearch = 1 or {$sArtTable}.oxparentid <> '' )
03564                and ".$this->getSqlActiveSnippet();
03565         */
03566 
03567         return $sQ;
03568     }
03569 
03579     protected function _generateSelectCatStr($sOXID, $sCatId, $dPriceFromTo = false)
03580     {
03581         $sCategoryView = getViewName('oxcategories');
03582         $sO2CView = getViewName('oxobject2category');
03583 
03584         $oDb    = oxDb::getDb();
03585         $sOXID  = $oDb->quote($sOXID);
03586         $sCatId = $oDb->quote($sCatId);
03587 
03588         if (!$dPriceFromTo) {
03589             $sSelect  = "select oxobject2category.oxcatnid from $sO2CView as oxobject2category ";
03590             $sSelect .= "left join $sCategoryView as oxcategories on oxcategories.oxid = oxobject2category.oxcatnid ";
03591             $sSelect .= "where oxobject2category.oxcatnid=$sCatId and oxobject2category.oxobjectid=$sOXID ";
03592             $sSelect .= "and oxcategories.oxactive = 1 order by oxobject2category.oxtime ";
03593         } else {
03594             $dPriceFromTo = $oDb->quote($dPriceFromTo);
03595             $sSelect  = "select oxcategories.oxid from $sCategoryView as oxcategories where ";
03596             $sSelect .= "oxcategories.oxid=$sCatId and $dPriceFromTo >= oxcategories.oxpricefrom and ";
03597             $sSelect .= "$dPriceFromTo <= oxcategories.oxpriceto ";
03598         }
03599         return $sSelect;
03600     }
03601 
03607     protected function _getAmountPriceList()
03608     {
03609         if ( $this->_oAmountPriceList === null ) {
03610             $oAmPriceList = oxNew( 'oxAmountPricelist' );
03611 
03612             if ( !$this->skipDiscounts() ) {
03613                 //collecting assigned to article amount-price list
03614                 $oAmPriceList->load( $this );
03615 
03616                 // prepare abs prices if currently having percentages
03617                 $oBasePrice = $this->_getGroupPrice();
03618                 foreach ( $oAmPriceList as $oAmPrice ) {
03619                     if ( $oAmPrice->oxprice2article__oxaddperc->value ) {
03620                         $oAmPrice->oxprice2article__oxaddabs = new oxField(oxPrice::percent( $oBasePrice, 100 - $oAmPrice->oxprice2article__oxaddperc->value ), oxField::T_RAW );
03621                     }
03622                 }
03623 
03624             }
03625 
03626             $this->_oAmountPriceList = $oAmPriceList;
03627         }
03628 
03629         return $this->_oAmountPriceList;
03630     }
03631 
03639     protected function _isFieldEmpty( $sFieldName )
03640     {
03641         $mValue = $this->$sFieldName->value;
03642 
03643         if ( is_null( $mValue ) ) {
03644             return true;
03645         }
03646 
03647         if ( $mValue === '' ) {
03648             return true;
03649         }
03650 
03651         // certain fields with zero value treat as empty
03652         $aZeroValueFields = array('oxarticles__oxprice', 'oxarticles__oxvat', 'oxarticles__oxunitquantity');
03653 
03654         if (!$mValue && in_array( $sFieldName, $aZeroValueFields ) ) {
03655             return true;
03656         }
03657 
03658 
03659         if (!strcmp($mValue, '0000-00-00 00:00:00') || !strcmp($mValue, '0000-00-00')) {
03660             return true;
03661         }
03662 
03663         $sFieldName = strtolower($sFieldName);
03664 
03665         if ( $sFieldName == 'oxarticles__oxicon' && ( strpos($mValue, "nopic_ico.jpg") !== false || strpos($mValue, "nopic.jpg") !== false ) ) {
03666             return true;
03667         }
03668 
03669         if ( strpos($mValue, "nopic.jpg") !== false && ($sFieldName == 'oxarticles__oxthumb' || substr($sFieldName, 0, 17) == 'oxarticles__oxpic' || substr($sFieldName, 0, 18) == 'oxarticles__oxzoom') ) {
03670             return true;
03671         }
03672 
03673         return false;
03674     }
03675 
03683     protected function _isImageField($sFieldName)
03684     {
03685         $blIsImageField = ( stristr($sFieldName, '_oxthumb') || stristr($sFieldName, '_oxicon') || stristr($sFieldName, '_oxzoom') || stristr($sFieldName, '_oxpic') );
03686         return $blIsImageField;
03687     }
03688 
03696     protected function _assignParentFieldValue($sFieldName)
03697     {
03698         if (!($oParentArticle = $this->getParentArticle())) {
03699             return;
03700         }
03701 
03702         $sCopyFieldName = $this->_getFieldLongName($sFieldName);
03703 
03704         // assigning only these which parent article has
03705         if ( $oParentArticle->$sCopyFieldName != null ) {
03706 
03707             // only overwrite database values
03708             if ( substr( $sCopyFieldName, 0, 12) != 'oxarticles__') {
03709                 return;
03710             }
03711 
03712             //do not copy certain fields
03713             if (in_array($sCopyFieldName, $this->_aNonCopyParentFields)) {
03714                 return;
03715             }
03716 
03717             //skip picture parent value assignment in case master image is set for variant
03718             if ($this->_isFieldEmpty($sCopyFieldName) && $this->_isImageField($sCopyFieldName) && $this->_hasMasterImage( 1 )) {
03719                 return;
03720             }
03721 
03722             //COPY THE VALUE
03723             if ( $this->_isFieldEmpty($sCopyFieldName) ) {
03724                 $this->$sCopyFieldName = clone $oParentArticle->$sCopyFieldName;
03725             }
03726         }
03727     }
03728 
03734     public function getParentArticle()
03735     {
03736         if ( ( $sParentId = $this->oxarticles__oxparentid->value ) ) {
03737             $sIndex = $sParentId . "_" . $this->getLanguage();
03738             if ( !isset( self::$_aLoadedParents[$sIndex] ) ) {
03739                 self::$_aLoadedParents[$sIndex] = oxNew( 'oxarticle' );
03740                 self::$_aLoadedParents[$sIndex]->_blLoadPrice    = false;
03741                 self::$_aLoadedParents[$sIndex]->_blLoadVariants = false;
03742                 self::$_aLoadedParents[$sIndex]->loadInLang( $this->getLanguage(), $sParentId );
03743             }
03744             return self::$_aLoadedParents[$sIndex];
03745         }
03746     }
03747 
03753     protected function _assignParentFieldValues()
03754     {
03755         startProfile('articleAssignParentInternal');
03756         if ( $this->oxarticles__oxparentid->value ) {
03757             // yes, we are in fact a variant
03758             if ( !$this->isAdmin() || ( $this->_blLoadParentData && $this->isAdmin() ) ) {
03759                 foreach ( $this->_aFieldNames as $sFieldName => $sVal ) {
03760                     $this->_assignParentFieldValue( $sFieldName );
03761                 }
03762             }
03763         }
03764         stopProfile('articleAssignParentInternal');
03765     }
03766 
03772     protected function _assignNotBuyableParent()
03773     {
03774         if ( !$this->getConfig()->getConfigParam( 'blVariantParentBuyable' ) &&
03775              ($this->_blHasVariants || $this->oxarticles__oxvarstock->value || $this->oxarticles__oxvarcount->value )) {
03776             $this->_blNotBuyableParent = true;
03777 
03778         }
03779     }
03780 
03786     protected function _assignStock()
03787     {
03788         $myConfig = $this->getConfig();
03789         // -----------------------------------
03790         // stock
03791         // -----------------------------------
03792 
03793         // #1125 A. must round (using floor()) value taken from database and cast to int
03794         if (!$myConfig->getConfigParam( 'blAllowUnevenAmounts' ) && !$this->isAdmin() ) {
03795             $this->oxarticles__oxstock = new oxField((int) floor($this->oxarticles__oxstock->value));
03796         }
03797         //GREEN light
03798         $this->_iStockStatus = 0;
03799 
03800         // if we have flag /*1 or*/ 4 - we show always green light
03801         if ( $myConfig->getConfigParam( 'blUseStock' ) && /*$this->oxarticles__oxstockflag->value != 1 && */ $this->oxarticles__oxstockflag->value != 4) {
03802             //ORANGE light
03803             $iStock = $this->oxarticles__oxstock->value;
03804 
03805             if ($this->_blNotBuyableParent) {
03806                 $iStock = $this->oxarticles__oxvarstock->value;
03807             }
03808 
03809 
03810             if ( $iStock <= $myConfig->getConfigParam( 'sStockWarningLimit' ) && $iStock > 0) {
03811                 $this->_iStockStatus = 1;
03812             }
03813 
03814             //RED light
03815             if ($iStock <= 0) {
03816                 $this->_iStockStatus = -1;
03817             }
03818         }
03819 
03820 
03821         // stock
03822         if ( $myConfig->getConfigParam( 'blUseStock' ) && ($this->oxarticles__oxstockflag->value == 3 || $this->oxarticles__oxstockflag->value == 2)) {
03823             $iOnStock = $this->oxarticles__oxstock->value;
03824             if ($this->getConfig()->getConfigParam( 'blPsBasketReservationEnabled' )) {
03825                 $iOnStock += $this->getSession()->getBasketReservations()->getReservedAmount($this->getId());
03826             }
03827             if ($iOnStock <= 0) {
03828                 $this->setBuyableState( false );
03829             }
03830         }
03831 
03832         //exceptional handling for variant parent stock:
03833         if ($this->_blNotBuyable && $this->oxarticles__oxvarstock->value ) {
03834             $this->setBuyableState( true );
03835             //but then at least setting notBuaybleParent to true
03836             $this->_blNotBuyableParent = true;
03837         }
03838 
03839         //special treatment for lists when blVariantParentBuyable config option is set to false
03840         //then we just hide "to basket" button.
03841         //if variants are not loaded in the list and this article has variants and parent is not buyable then this article is not buyable
03842         if ( !$myConfig->getConfigParam( 'blVariantParentBuyable' ) && !$myConfig->getConfigParam( 'blLoadVariants' ) && $this->oxarticles__oxvarstock->value) {
03843             $this->setBuyableState( false );
03844         }
03845 
03846         //setting to non buyable when variant list is empty (for example not loaded or inactive) and $this is non buyable parent
03847         if (!$this->_blNotBuyable && $this->_blNotBuyableParent && $this->oxarticles__oxvarcount->value == 0) {
03848             $this->setBuyableState( false );
03849         }
03850     }
03851 
03859     protected function _assignPrices()
03860     {
03861         $myConfig = $this->getConfig();
03862 
03863         // Performance
03864         if ( !$myConfig->getConfigParam( 'bl_perfLoadPrice' ) || !$this->_blLoadPrice ) {
03865             return;
03866         }
03867 
03868         //getting min and max prices of variants
03869         if ( $this->_hasAnyVariant() ) {
03870             $this->_applyRangePrice();
03871         }
03872     }
03873 
03879     protected function _assignPersistentParam()
03880     {
03881         // Persistent Parameter Handling
03882         $aPersParam     = oxSession::getVar( 'persparam');
03883         if ( isset( $aPersParam) && isset( $aPersParam[$this->getId()])) {
03884             $this->_aPersistParam = $aPersParam[$this->getId()];
03885         }
03886     }
03887 
03893     protected function _assignDynImageDir()
03894     {
03895         $myConfig = $this->getConfig();
03896 
03897         $sThisShop = $this->oxarticles__oxshopid->value;
03898 
03899         $this->_sDynImageDir   = $myConfig->getPictureUrl( null, false );
03900         $this->dabsimagedir    = $myConfig->getPictureDir( false ); //$sThisShop
03901         $this->nossl_dimagedir = $myConfig->getPictureUrl( null, false, false, null, $sThisShop ); //$sThisShop
03902         $this->ssl_dimagedir   = $myConfig->getPictureUrl( null, false, true, null, $sThisShop ); //$sThisShop
03903     }
03904 
03910     protected function _assignComparisonListFlag()
03911     {
03912         // #657 add a flag if article is on comparisonlist
03913 
03914         $aItems = oxSession::getVar('aFiltcompproducts');
03915         if ( isset( $aItems[$this->getId()])) {
03916             $this->_blIsOnComparisonList = true;
03917         }
03918     }
03919 
03920 
03928     protected function _insert()
03929     {
03930         // set oxinsert
03931         $sNow = date('Y-m-d H:i:s', oxRegistry::get("oxUtilsDate")->getTime());
03932         $this->oxarticles__oxinsert    = new oxField( $sNow );
03933         if ( !is_object($this->oxarticles__oxsubclass) || $this->oxarticles__oxsubclass->value == '') {
03934             $this->oxarticles__oxsubclass = new oxField('oxarticle');
03935         }
03936 
03937         return parent::_insert();
03938     }
03939 
03945     protected function _update()
03946     {
03947 
03948         $this->_skipSaveFields();
03949 
03950         $myConfig = $this->getConfig();
03951 
03952 
03953         $blRes =  parent::_update();
03954 
03955 
03956         return $blRes;
03957     }
03958 
03964     public function updateVariantsRemind()
03965     {
03966         // check if it is parent article
03967         if ( !$this->isVariant() && $this->_hasAnyVariant()) {
03968             $oDb = oxDb::getDb();
03969             $sOxId = $oDb->quote($this->getId());
03970             $sOxShopId = $oDb->quote($this->getShopId());
03971             $iRemindActive = $oDb->quote($this->oxarticles__oxremindactive->value);
03972             $sUpdate = "
03973                 update oxarticles
03974                     set oxremindactive = $iRemindActive
03975                     where oxparentid = $sOxId and
03976                           oxshopid = $sOxShopId
03977             ";
03978             $oDb->execute( $sUpdate );
03979         }
03980     }
03981 
03989     protected function _deleteRecords($sOXID)
03990     {
03991         $oDb = oxDb::getDb();
03992 
03993         $sOXID = $oDb->quote($sOXID);
03994 
03995         //remove other records
03996         $sDelete = 'delete from oxobject2article where oxarticlenid = '.$sOXID.' or oxobjectid = '.$sOXID.' ';
03997         $oDb->execute( $sDelete);
03998 
03999         $sDelete = 'delete from oxobject2attribute where oxobjectid = '.$sOXID.' ';
04000         $oDb->execute( $sDelete);
04001 
04002         $sDelete = 'delete from oxobject2category where oxobjectid = '.$sOXID.' ';
04003         $oDb->execute( $sDelete);
04004 
04005         $sDelete = 'delete from oxobject2selectlist where oxobjectid = '.$sOXID.' ';
04006         $oDb->execute( $sDelete);
04007 
04008         $sDelete = 'delete from oxprice2article where oxartid = '.$sOXID.' ';
04009         $oDb->execute( $sDelete);
04010 
04011         $sDelete = 'delete from oxreviews where oxtype="oxarticle" and oxobjectid = '.$sOXID.' ';
04012         $oDb->execute( $sDelete);
04013 
04014         $sDelete = 'delete from oxratings where oxobjectid = '.$sOXID.' ';
04015         $rs = $oDb->execute( $sDelete );
04016 
04017         $sDelete = 'delete from oxaccessoire2article where oxobjectid = '.$sOXID.' or oxarticlenid = '.$sOXID.' ';
04018         $oDb->execute( $sDelete);
04019 
04020         //#1508C - deleting oxobject2delivery entries added
04021         $sDelete = 'delete from oxobject2delivery where oxobjectid = '.$sOXID.' and oxtype=\'oxarticles\' ';
04022         $oDb->execute( $sDelete);
04023 
04024         $sDelete = 'delete from oxartextends where oxid = '.$sOXID.' ';
04025         $oDb->execute( $sDelete);
04026 
04027         //delete the record
04028         foreach ( $this->_getLanguageSetTables( "oxartextends" ) as $sSetTbl ) {
04029             $oDb->execute( "delete from $sSetTbl where oxid = {$sOXID}" );
04030         }
04031 
04032         $sDelete = 'delete from oxactions2article where oxartid = '.$sOXID.' ';
04033         $rs = $oDb->execute( $sDelete );
04034 
04035         $sDelete = 'delete from oxobject2list where oxobjectid = '.$sOXID.' ';
04036         $rs = $oDb->execute( $sDelete );
04037 
04038 
04039         return $rs;
04040     }
04041 
04049     protected function _deleteVariantRecords( $sOXID )
04050     {
04051         if ( $sOXID ) {
04052             $oDb = oxDb::getDb();
04053             //collect variants to remove recursively
04054             $sQ = 'select oxid from '.$this->getViewName().' where oxparentid = '.$oDb->quote( $sOXID );
04055             $rs = $oDb->select( $sQ, false, false );
04056             $oArticle = oxNew("oxArticle");
04057             if ($rs != false && $rs->recordCount() > 0) {
04058                 while (!$rs->EOF) {
04059                     $oArticle->setId($rs->fields[0]);
04060                     $oArticle->delete();
04061                     $rs->moveNext();
04062                 }
04063             }
04064         }
04065     }
04066 
04072     protected function _deletePics()
04073     {
04074         $myUtilsPic = oxRegistry::get("oxUtilsPic");
04075         $myConfig   = $this->getConfig();
04076         $oPictureHandler = oxRegistry::get("oxPictureHandler");
04077 
04078         //deleting custom main icon
04079         $oPictureHandler->deleteMainIcon( $this );
04080 
04081         //deleting custom thumbnail
04082         $oPictureHandler->deleteThumbnail( $this );
04083 
04084         $sAbsDynImageDir = $myConfig->getPictureDir(false);
04085 
04086         // deleting master image and all generated images
04087         $iPicCount = $myConfig->getConfigParam( 'iPicCount' );
04088         for ( $i = 1; $i <= $iPicCount; $i++ ) {
04089             $oPictureHandler->deleteArticleMasterPicture( $this, $i );
04090         }
04091     }
04092 
04102     protected function _onChangeResetCounts( $sOxid, $sVendorId = null, $sManufacturerId = null )
04103     {
04104 
04105         $myUtilsCount = oxRegistry::get("oxUtilsCount");
04106 
04107         if ( $sVendorId ) {
04108             $myUtilsCount->resetVendorArticleCount( $sVendorId );
04109         }
04110 
04111         if ( $sManufacturerId ) {
04112             $myUtilsCount->resetManufacturerArticleCount( $sManufacturerId );
04113         }
04114 
04115         //also reseting category counts
04116         $oDb = oxDb::getDb();
04117         $sQ = "select oxcatnid from oxobject2category where oxobjectid = ".$oDb->quote($sOxid);
04118         $oRs = $oDb->select( $sQ, false, false );
04119         if ( $oRs !== false && $oRs->recordCount() > 0) {
04120             while ( !$oRs->EOF ) {
04121                 $myUtilsCount->resetCatArticleCount( $oRs->fields[0] );
04122                 $oRs->moveNext();
04123             }
04124         }
04125     }
04126 
04134     protected function _onChangeUpdateStock( $sParentID )
04135     {
04136         if ( $sParentID ) {
04137             $oDb = oxDb::getDb();
04138             $sParentIdQuoted = $oDb->quote($sParentID);
04139             $sQ = 'select oxstock, oxvendorid, oxmanufacturerid from oxarticles where oxid = '.$sParentIdQuoted;
04140             $rs = $oDb->select( $sQ, false, false );
04141             $iOldStock = $rs->fields[0];
04142             $iVendorID = $rs->fields[1];
04143             $iManufacturerID = $rs->fields[2];
04144 
04145             $sQ = 'select sum(oxstock) from '.$this->getViewName(true).' where oxparentid = '.$sParentIdQuoted.' and '. $this->getSqlActiveSnippet( true ).' and oxstock > 0 ';
04146             $iStock = (float) $oDb->getOne( $sQ, false, false );
04147 
04148             $sQ = 'update oxarticles set oxvarstock = '.$iStock.' where oxid = '.$sParentIdQuoted;
04149             $oDb->execute( $sQ );
04150 
04151             //now lets update category counts
04152             //first detect stock status change for this article (to or from 0)
04153             if ( $iStock < 0 ) {
04154                 $iStock = 0;
04155             }
04156             if ( $iOldStock < 0 ) {
04157                 $iOldStock = 0;
04158             }
04159             if ( $this->oxarticles__oxstockflag->value == 2 && $iOldStock xor $iStock ) {
04160                 //means the stock status could be changed (oxstock turns from 0 to 1 or from 1 to 0)
04161                 // so far we leave it like this but later we could move all count resets to one or two functions
04162                 $this->_onChangeResetCounts( $sParentID, $iVendorID, $iManufacturerID );
04163             }
04164         }
04165     }
04166 
04174     protected function _onChangeStockResetCount( $sOxid )
04175     {
04176         $myConfig = $this->getConfig();
04177 
04178         if ( $myConfig->getConfigParam( 'blUseStock' ) && $this->oxarticles__oxstockflag->value == 2 &&
04179            ( $this->oxarticles__oxstock->value + $this->oxarticles__oxvarstock->value ) <= 0 ) {
04180 
04181                $this->_onChangeResetCounts( $sOxid, $this->oxarticles__oxvendorid->value, $this->oxarticles__oxmanufacturerid->value );
04182         }
04183     }
04184 
04192     protected function _onChangeUpdateVarCount( $sParentID )
04193     {
04194         if ( $sParentID ) {
04195             $oDb = oxDb::getDb();
04196             $sParentIdQuoted = $oDb->quote( $sParentID );
04197             $sQ = "select count(*) as varcount from oxarticles where oxparentid = {$sParentIdQuoted}";
04198             $iVarCount = (int) $oDb->getOne( $sQ, false, false );
04199 
04200             $sQ = "update oxarticles set oxvarcount = {$iVarCount} where oxid = {$sParentIdQuoted}";
04201             $oDb->execute( $sQ );
04202         }
04203     }
04204 
04212     protected function _setVarMinMaxPrice( $sParentId )
04213     {
04214         if ( $sParentId ) {
04215             $oDb = oxDb::getDb( oxDb::FETCH_MODE_ASSOC );
04216             $sQ = '
04217                 SELECT
04218                     MIN( IF( `oxarticles`.`oxprice` > 0, `oxarticles`.`oxprice`, `p`.`oxprice` ) ) AS `varminprice`,
04219                     MAX( IF( `oxarticles`.`oxprice` > 0, `oxarticles`.`oxprice`, `p`.`oxprice` ) ) AS `varmaxprice`
04220                 FROM '. $this->getViewName(true) . ' AS `oxarticles`
04221                     LEFT JOIN '. $this->getViewName(true) . ' AS `p` ON ( `p`.`oxid` = `oxarticles`.`oxparentid` AND `p`.`oxprice` > 0 )
04222                 WHERE '. $this->getSqlActiveSnippet(true) .'
04223                     AND ( `oxarticles`.`oxparentid` = '. $oDb->quote( $sParentId ) .' )';
04224             $oDb->setFetchMode( oxDb::FETCH_MODE_ASSOC );
04225             $aPrices = $oDb->getRow( $sQ, false, false );
04226             if ( !is_null( $aPrices['varminprice'] ) || !is_null( $aPrices['varmaxprice'] ) ) {
04227                 $sQ = '
04228                     UPDATE `oxarticles`
04229                     SET
04230                         `oxvarminprice` = '. $oDb->quote( $aPrices['varminprice'] ) .',
04231                         `oxvarmaxprice` = '. $oDb->quote( $aPrices['varmaxprice'] ) .'
04232                     WHERE
04233                         `oxid` = ' . $oDb->quote( $sParentId );
04234             } else {
04235                  $sQ = '
04236                     UPDATE `oxarticles`
04237                     SET
04238                         `oxvarminprice` = `oxprice`,
04239                         `oxvarmaxprice` = `oxprice`
04240                     WHERE
04241                         `oxid` = ' . $oDb->quote( $sParentId );
04242             }
04243             $oDb->execute( $sQ );
04244         }
04245     }
04246 
04247 
04257     protected function _onChangeUpdateMinVarPrice( $sParentID )
04258     {
04259         if ( $sParentID ) {
04260             $oDb = oxDb::getDb();
04261             $sParentIdQuoted = $oDb->quote($sParentID);
04262             //#M0000883 (Sarunas)
04263             $sQ = 'select min(oxprice) as varminprice from '.$this->getViewName(true).' where '.$this->getSqlActiveSnippet(true).' and (oxparentid = '.$sParentIdQuoted.')';
04264             $dVarMinPrice = $oDb->getOne( $sQ, false, false );
04265 
04266             $dParentPrice = $oDb->getOne( "select oxprice from oxarticles where oxid = $sParentIdQuoted ", false, false );
04267 
04268             $blParentBuyable =  $this->getConfig()->getConfigParam( 'blVariantParentBuyable' );
04269 
04270             if ($dVarMinPrice) {
04271                 if ($blParentBuyable) {
04272                     $dVarMinPrice = min($dVarMinPrice, $dParentPrice);
04273                 }
04274             } else {
04275                 $dVarMinPrice = $dParentPrice;
04276             }
04277 
04278             if ( $dVarMinPrice ) {
04279                 $sQ = 'update oxarticles set oxvarminprice = '.$dVarMinPrice.' where oxid = '.$sParentIdQuoted;
04280                 $oDb->execute($sQ);
04281             }
04282         }
04283     }
04284 
04285 
04293     protected function _applyRangePrice()
04294     {
04295         //#buglist_413 if bl_perfLoadPriceForAddList variant price shouldn't be loaded too
04296         if ( !$this->getConfig()->getConfigParam( 'bl_perfLoadPrice' ) || !$this->_blLoadPrice ) {
04297             return;
04298         }
04299 
04300         $this->_blIsRangePrice = false;
04301 
04302         // if parent is buyable - do not apply range price calculations
04303         if ($this->_blSkipFromPrice || !$this->_blNotBuyableParent) {
04304             return;
04305         }
04306 
04307         if ( $this->isParentNotBuyable() && !$this->getConfig()->getConfigParam( 'blLoadVariants' )) {
04308 
04309             $dPrice = $this->_preparePrice( $this->oxarticles__oxvarminprice->value, $this->getArticleVat() );
04310             $this->getPrice()->setPrice($dPrice);
04311             $this->_blIsRangePrice = true;
04312             $this->_calculatePrice( $this->getPrice() );
04313             return;
04314 
04315         }
04316 
04317         $aPrices = array();
04318 
04319         if (!$this->_blNotBuyableParent) {
04320             $aPrices[] = $this->getPrice()->getPrice();
04321         }
04322 
04323         $aVariants = $this->getVariants(false);
04324 
04325         if (count($aVariants)) {
04326             foreach ($aVariants as $sKey => $oVariant) {
04327                 $aPrices[] = $oVariant->getPrice()->getPrice();
04328             }
04329         }
04330 
04331         if ( count( $aPrices ) ) {
04332             $dMinPrice = min( $aPrices );
04333             $dMaxPrice = max( $aPrices );
04334         }
04335 
04336         if ($this->_blNotBuyableParent && isset($dMinPrice) && $dMinPrice == $dMaxPrice) {
04337             $this->getPrice()->setPrice($dMinPrice);
04338         }
04339 
04340         if (isset($dMinPrice) && $dMinPrice != $dMaxPrice) {
04341             $this->getPrice()->setPrice($dMinPrice);
04342             $this->_blIsRangePrice = true;
04343         }
04344     }
04345 
04352     public function getProductId()
04353     {
04354         return $this->getId();
04355     }
04356 
04364     public function getProductParentId()
04365     {
04366         return $this->getParentId();
04367     }
04368 
04374     public function getParentId()
04375     {
04376         return $this->oxarticles__oxparentid->value;
04377     }
04378 
04384     public function isOrderArticle()
04385     {
04386         return false;
04387     }
04388 
04394     public function isVariant()
04395     {
04396         return (bool) ( isset( $this->oxarticles__oxparentid ) ? $this->oxarticles__oxparentid->value : false );
04397     }
04398 
04404     public function isMdVariant()
04405     {
04406         $oMdVariant = oxNew( "oxVariantHandler" );
04407 
04408         return $oMdVariant->isMdVariant($this);
04409     }
04410 
04418     public function getSqlForPriceCategories($sFields = '')
04419     {
04420         if (!$sFields) {
04421             $sFields = 'oxid';
04422         }
04423         $sSelectWhere = "select $sFields from ".$this->_getObjectViewName('oxcategories')." where";
04424         $sQuotedPrice = oxDb::getDb()->quote( $this->oxarticles__oxprice->value );
04425         return  "$sSelectWhere oxpricefrom != 0 and oxpriceto != 0 and oxpricefrom <= $sQuotedPrice and oxpriceto >= $sQuotedPrice"
04426                ." union $sSelectWhere oxpricefrom != 0 and oxpriceto = 0 and oxpricefrom <= $sQuotedPrice"
04427                ." union $sSelectWhere oxpricefrom = 0 and oxpriceto != 0 and oxpriceto >= $sQuotedPrice";
04428     }
04429 
04437     public function inPriceCategory( $sCatNid )
04438     {
04439         $oDb = oxDb::getDb();
04440 
04441         $sQuotedPrice = $oDb->quote( $this->oxarticles__oxprice->value );
04442         $sQuotedCnid = $oDb->quote( $sCatNid );
04443         return (bool) $oDb->getOne(
04444             "select 1 from ".$this->_getObjectViewName('oxcategories')." where oxid=$sQuotedCnid and"
04445            ."(   (oxpricefrom != 0 and oxpriceto != 0 and oxpricefrom <= $sQuotedPrice and oxpriceto >= $sQuotedPrice)"
04446            ." or (oxpricefrom != 0 and oxpriceto = 0 and oxpricefrom <= $sQuotedPrice)"
04447            ." or (oxpricefrom = 0 and oxpriceto != 0 and oxpriceto >= $sQuotedPrice)"
04448            .")"
04449         );
04450     }
04451 
04457     public function getMdVariants()
04458     {
04459         if ( $this->_oMdVariants ) {
04460             return $this->_oMdVariants;
04461         }
04462 
04463         $oParentArticle = $this->getParentArticle();
04464         if ( $oParentArticle ) {
04465             $oVariants = $oParentArticle->getVariants();
04466         } else {
04467             $oVariants = $this->getVariants();
04468         }
04469 
04470         $oVariantHandler = oxNew( "oxVariantHandler" );
04471         $this->_oMdVariants = $oVariantHandler->buildMdVariants( $oVariants, $this->getId() );
04472 
04473         return $this->_oMdVariants;
04474     }
04475 
04481     public function getMdSubvariants()
04482     {
04483         return $this->getMdVariants()->getMdSubvariants();
04484     }
04485 
04493     protected function _hasMasterImage( $iIndex )
04494     {
04495         $sPicName = basename($this->{"oxarticles__oxpic" . $iIndex}->value);
04496 
04497         if ( $sPicName == "nopic.jpg" || $sPicName == "" ) {
04498             return false;
04499         }
04500         if ( $this->isVariant() && $this->getParentArticle()->{"oxarticles__oxpic".$iIndex}->value == $this->{"oxarticles__oxpic".$iIndex}->value ) {
04501             return false;
04502         }
04503 
04504         $sMasterPic = 'product/'.$iIndex . "/" . $sPicName;
04505 
04506         if ( $this->getConfig()->getMasterPicturePath( $sMasterPic ) ) {
04507             return true;
04508         }
04509 
04510         return false;
04511     }
04512 
04521     public function getPictureFieldValue( $sFieldName, $iIndex = null )
04522     {
04523         if ( $sFieldName ) {
04524             $sFieldName = "oxarticles__" . $sFieldName . $iIndex;
04525             return $this->$sFieldName->value;
04526         }
04527     }
04528 
04536     public function getMasterZoomPictureUrl( $iIndex )
04537     {
04538         $sPicUrl  = false;
04539         $sPicName = basename( $this->{"oxarticles__oxpic" . $iIndex}->value );
04540 
04541         if ( $sPicName && $sPicName != "nopic.jpg" ) {
04542             $sPicUrl = $this->getConfig()->getPictureUrl( "master/product/" . $iIndex . "/" . $sPicName );
04543             if ( !$sPicUrl || basename( $sPicUrl ) == "nopic.jpg" ) {
04544                 $sPicUrl = false;
04545             }
04546         }
04547 
04548         return $sPicUrl;
04549     }
04550 
04556     public function getUnitName()
04557     {
04558         if ( $this->oxarticles__oxunitname->value ) {
04559             return oxRegistry::getLang()->translateString( $this->oxarticles__oxunitname->value );
04560         }
04561     }
04562 
04570     public function getArticleFiles( $blAddFromParent=false )
04571     {
04572         if ( $this->_aArticleFiles === null) {
04573 
04574             $this->_aArticleFiles = false;
04575 
04576             $sQ = "SELECT * FROM `oxfiles` WHERE `oxartid` = '".$this->getId()."'";
04577 
04578             if ( !$this->getConfig()->getConfigParam( 'blVariantParentBuyable' ) && $blAddFromParent ) {
04579                 $sQ .= " OR `oxartId` = '". $this->oxarticles__oxparentid->value . "'";
04580             }
04581 
04582             $oArticleFiles = oxNew("oxlist");
04583             $oArticleFiles->init("oxfile");
04584             $oArticleFiles->selectString( $sQ );
04585             $this->_aArticleFiles  = $oArticleFiles;
04586 
04587         }
04588 
04589         return $this->_aArticleFiles;
04590     }
04591 
04597     public function isDownloadable()
04598     {
04599         return $this->oxarticles__oxisdownloadable->value;
04600     }
04601 
04607     public function hasAmountPrice()
04608     {
04609         if ( self::$_blHasAmountPrice === null ) {
04610 
04611             self::$_blHasAmountPrice = false;
04612 
04613             $oDb = oxDb::getDb();
04614             $sQ = "SELECT 1 FROM `oxprice2article` LIMIT 1";
04615 
04616             if ( $oDb->getOne( $sQ ) ) {
04617                 self::$_blHasAmountPrice = true;
04618             }
04619         }
04620 
04621         return self::$_blHasAmountPrice;
04622     }
04623 
04624 
04630     protected function _isPriceViewModeNetto()
04631     {
04632         $blResult = (bool) $this->getConfig()->getConfigParam('blShowNetPrice');
04633         $oUser = $this->getArticleUser();
04634         if ( $oUser ) {
04635             $blResult = $oUser->isPriceViewModeNetto();
04636         }
04637 
04638         return $blResult;
04639     }
04640 
04641 
04649     protected function _getPriceObject( $blCalculationModeNetto = null )
04650     {
04651         $oPrice = oxNew( 'oxPrice' );
04652 
04653         if ( $blCalculationModeNetto === null ) {
04654             $blCalculationModeNetto = $this->_isPriceViewModeNetto();
04655         }
04656 
04657         if ( $blCalculationModeNetto ) {
04658             $oPrice->setNettoPriceMode();
04659         } else {
04660             $oPrice->setBruttoPriceMode();
04661         }
04662 
04663         return $oPrice;
04664     }
04665 
04666 
04674     protected function _getPriceForView( $oPrice )
04675     {
04676         if ( $this->_isPriceViewModeNetto() ) {
04677             $dPrice = $oPrice->getNettoPrice();
04678         } else {
04679             $dPrice = $oPrice->getBruttoPrice();
04680         }
04681 
04682         return $dPrice;
04683     }
04684 
04685 
04695     protected function _preparePrice( $dPrice, $dVat, $blCalculationModeNetto = null )
04696     {
04697         if ( $blCalculationModeNetto === null ) {
04698             $blCalculationModeNetto = $this->_isPriceViewModeNetto();
04699         }
04700 
04701         $oCurrency = $this->getConfig()->getActShopCurrencyObject();
04702 
04703         $blEnterNetPrice = $this->getConfig()->getConfigParam('blEnterNetPrice');
04704         if ( $blCalculationModeNetto && !$blEnterNetPrice ) {
04705             $dPrice = round( oxPrice::brutto2Netto( $dPrice, $dVat ), $oCurrency->decimal );
04706         } elseif ( !$blCalculationModeNetto && $blEnterNetPrice ) {
04707             $dPrice = round( oxPrice::netto2Brutto( $dPrice, $dVat ), $oCurrency->decimal );
04708         }
04709 
04710         return $dPrice;
04711     }
04712 
04713 
04720     public function getFUnitPrice()
04721     {
04722         if ($this->_fPricePerUnit == null) {
04723             if ( $oPrice = $this->getUnitPrice() ) {
04724                 if ( $dPrice = $this->_getPriceForView( $oPrice ) ) {
04725                     $this->_fPricePerUnit = oxRegistry::getLang()->formatCurrency( $dPrice );
04726                 }
04727             }
04728         }
04729 
04730         return $this->_fPricePerUnit;
04731     }
04732 
04733 
04739     public function getUnitPrice()
04740     {
04741         // Performance
04742         if ( !$this->getConfig()->getConfigParam( 'bl_perfLoadPrice' ) || !$this->_blLoadPrice ) {
04743             return;
04744         }
04745 
04746         $oPrice = null;
04747         if ( (double) $this->getUnitQuantity() && $this->oxarticles__oxunitname->value ) {
04748             $oPrice = clone $this->getPrice();
04749             $oPrice->divide( (double) $this->getUnitQuantity() );
04750         }
04751 
04752         return $oPrice;
04753     }
04754 
04755 
04763     public function getFMinPrice()
04764     {
04765         $sPrice = '';
04766         if ( $oPrice = $this->getMinPrice() ) {
04767             $dPrice = $this->_getPriceForView( $oPrice );
04768             $sPrice = oxRegistry::getLang()->formatCurrency( $dPrice );
04769         }
04770 
04771         return $sPrice;
04772     }
04773 
04781     public function getFVarMinPrice()
04782     {
04783         $sPrice = '';
04784         if ( $oPrice = $this->getVarMinPrice() ) {
04785             $dPrice = $this->_getPriceForView( $oPrice );
04786             $sPrice = oxRegistry::getLang()->formatCurrency( $dPrice );
04787         }
04788 
04789         return $sPrice;
04790     }
04791 
04797     public function getVarMinPrice()
04798     {
04799         if ( !$this->getConfig()->getConfigParam( 'bl_perfLoadPrice' ) || !$this->_blLoadPrice ) {
04800             return;
04801         }
04802 
04803         $oPrice = null;
04804         $dPrice = $this->_getVarMinPrice();
04805 
04806         $dPrice = $this->_preparePrice( $dPrice, $this->getArticleVat() );
04807 
04808 
04809         $oPrice = $this->_getPriceObject();
04810         $oPrice->setPrice( $dPrice );
04811         $this->_calculatePrice( $oPrice );
04812 
04813 
04814         return $oPrice;
04815     }
04816 
04817 
04823     public function getMinPrice()
04824     {
04825         if ( !$this->getConfig()->getConfigParam( 'bl_perfLoadPrice' ) || !$this->_blLoadPrice ) {
04826             return;
04827         }
04828 
04829         $oPrice = null;
04830         $dPrice = $this->_getPrice();
04831         if ( $this->_getVarMinPrice() !== null && $dPrice > $this->_getVarMinPrice() ) {
04832             $dPrice = $this->_getVarMinPrice();
04833         }
04834 
04835         $dPrice = $this->_preparePrice( $dPrice, $this->getArticleVat() );
04836 
04837 
04838         $oPrice = $this->_getPriceObject();
04839         $oPrice->setPrice( $dPrice );
04840         $this->_calculatePrice( $oPrice );
04841 
04842         return $oPrice;
04843     }
04844 
04845 
04851     public function isRangePrice()
04852     {
04853         if ( $this->_blIsRangePrice === null ) {
04854 
04855             $this->setRangePrice( false );
04856 
04857             if ( $this->_hasAnyVariant() ) {
04858                 $dPrice = $this->_getPrice();
04859                 $dMinPrice = $this->_getVarMinPrice();
04860                 $dMaxPrice = $this->_getVarMaxPrice();
04861 
04862                 if ( $dMinPrice != $dMaxPrice ) {
04863                     $this->setRangePrice();
04864                 } elseif ( !$this->isParentNotBuyable() &&  $dMinPrice != $dPrice ) {
04865                     $this->setRangePrice();
04866                 }
04867             }
04868         }
04869 
04870         return $this->_blIsRangePrice;
04871     }
04872 
04873 
04881     public function setRangePrice( $blIsRangePrice = true )
04882     {
04883         return $this->_blIsRangePrice = $blIsRangePrice;
04884     }
04885 
04891     protected function _getUserPriceSufix()
04892     {
04893         $sPriceSufix = '';
04894         $oUser = $this->getArticleUser();
04895 
04896         if ( $oUser ) {
04897             if ( $oUser->inGroup( 'oxidpricea' ) ) {
04898                 $sPriceSufix = 'a';
04899             } elseif ( $oUser->inGroup( 'oxidpriceb' ) ) {
04900                 $sPriceSufix = 'b';
04901             } elseif ( $oUser->inGroup( 'oxidpricec' ) ) {
04902                 $sPriceSufix = 'c';
04903             }
04904         }
04905 
04906         return $sPriceSufix;
04907     }
04908 
04909 
04915     protected function _getPrice()
04916     {
04917             $sPriceSufix = $this->_getUserPriceSufix();
04918             if ( $sPriceSufix === '') {
04919                 $dPrice = $this->oxarticles__oxprice->value;
04920             } else {
04921                 if ( $this->getConfig()->getConfigParam( 'blOverrideZeroABCPrices' ) ) {
04922                     $dPrice = ($this->{oxarticles__oxprice.$sPriceSufix}->value !=0 )? $this->{oxarticles__oxprice.$sPriceSufix}->value : $this->oxarticles__oxprice->value;
04923                 } else {
04924                     $dPrice = $this->{oxarticles__oxprice.$sPriceSufix}->value;
04925                 }
04926             }
04927         return $dPrice;
04928     }
04929 
04930 
04936     protected function _getVarMinPrice()
04937     {
04938         if ( $this->_dVarMinPrice === null) {
04939 
04940             $sPriceSufix = $this->_getUserPriceSufix();
04941             if ( $dPrice === null ) {
04942                 if ( $sPriceSufix === '' ) {
04943                     $dPrice = $this->oxarticles__oxvarminprice->value;
04944                 } else {
04945                     $sSql = 'SELECT ';
04946                     if ( $this->getConfig()->getConfigParam( 'blOverrideZeroABCPrices' ) ) {
04947                         $sSql .=  'MIN( IF(`oxprice'.$sPriceSufix.'` = 0, `oxprice`, `oxprice'.$sPriceSufix.'`) ) AS `varminprice` ';
04948                     } else {
04949                         $sSql .=  'MIN(`oxprice'.$sPriceSufix.'`) AS `varminprice` ';
04950                     }
04951 
04952                     $sSql .=  ' FROM ' . $this->getViewName(true) . '
04953                         WHERE ' .$this->getSqlActiveSnippet(true) . '
04954                             AND ( `oxparentid` = ' . oxDb::getDb()->quote( $this->getId() ) . ' )';
04955 
04956                     $dPrice = oxDb::getDb()->getOne( $sSql );
04957                 }
04958             }
04959             $this->_dVarMinPrice = $dPrice;
04960         }
04961 
04962         return $this->_dVarMinPrice;
04963     }
04964 
04970     protected function _getSubShopVarMinPrice()
04971     {
04972         $myConfig = $this->getConfig();
04973         $sShopId = $myConfig->getShopId();
04974         if ($this->getConfig()->getConfigParam( 'blMallCustomPrice' ) && $sShopId != $this->oxarticles__oxshopid->value ) {
04975             $sPriceSufix = $this->_getUserPriceSufix();
04976             $sSql = 'SELECT ';
04977             if ( $sPriceSufix != '' && $this->getConfig()->getConfigParam( 'blOverrideZeroABCPrices' ) ) {
04978                 $sSql .=  'MIN(IF(`oxfield2shop`.`oxprice'.$sPriceSufix.'` = 0, `oxfield2shop`.`oxprice`, `oxfield2shop`.`oxprice'.$sPriceSufix.'`)) AS `varminprice` ';
04979             } else {
04980                 $sSql .=  'MIN(`oxfield2shop`.`oxprice'.$sPriceSufix.'`) AS `varminprice` ';
04981             }
04982             $sSql .=  ' FROM ' . getViewName('oxfield2shop') . ' AS oxfield2shop
04983                         INNER JOIN ' . $this->getViewName(true) . ' AS oxarticles ON `oxfield2shop`.`oxartid` = `oxarticles`.`oxid`
04984                         WHERE ' .$this->getSqlActiveSnippet(true) . '
04985                             AND ( `oxarticles`.`oxparentid` = ' . oxDb::getDb()->quote( $this->getId() ) . ' )
04986                             AND ( `oxfield2shop`.`oxshopid` = ' . oxDb::getDb()->quote( $sShopId ) . ' )';
04987             $dPrice = oxDb::getDb()->getOne( $sSql );
04988         }
04989         return $dPrice;
04990     }
04991 
04997     protected function _getVarMaxPrice()
04998     {
04999         if ( $this->_dVarMaxPrice === null ) {
05000 
05001             $sPriceSufix = $this->_getUserPriceSufix();
05002             if ( $dPrice === null ) {
05003                 if ( $sPriceSufix === '') {
05004                     $dPrice = $this->oxarticles__oxvarmaxprice->value;
05005                 } else {
05006                     $sSql = 'SELECT ';
05007                     if ( $this->getConfig()->getConfigParam( 'blOverrideZeroABCPrices' ) ) {
05008                         $sSql .=  'MAX( IF(`oxprice'.$sPriceSufix.'` = 0, `oxprice`, `oxprice'.$sPriceSufix.'`) ) AS `varmaxprice` ';
05009                     } else {
05010                         $sSql .=  'MAX(`oxprice'.$sPriceSufix.'`) AS `varmaxprice` ';
05011                     }
05012 
05013                     $sSql .=  ' FROM ' . $this->getViewName(true) . '
05014                         WHERE ' .$this->getSqlActiveSnippet(true) . '
05015                             AND ( `oxparentid` = ' . oxDb::getDb()->quote( $this->getId() ) . ' )';
05016 
05017                     $dPrice = oxDb::getDb()->getOne( $sSql );
05018                 }
05019             }
05020             $this->_dVarMaxPrice = $dPrice;
05021         }
05022 
05023         return $this->_dVarMaxPrice;
05024     }
05025 
05031     protected function _getSubShopVarMaxPrice()
05032     {
05033         $myConfig = $this->getConfig();
05034         $sShopId = $myConfig->getShopId();
05035         if ($this->getConfig()->getConfigParam( 'blMallCustomPrice' ) && $sShopId != $this->oxarticles__oxshopid->value ) {
05036             $sPriceSufix = $this->_getUserPriceSufix();
05037             $sSql = 'SELECT ';
05038             if ( $sPriceSufix != '' && $this->getConfig()->getConfigParam( 'blOverrideZeroABCPrices' ) ) {
05039                 $sSql .=  'MAX(IF(`oxfield2shop`.`oxprice'.$sPriceSufix.'` = 0, `oxfield2shop`.`oxprice`, `oxfield2shop`.`oxprice'.$sPriceSufix.'`)) AS `varmaxprice` ';
05040             } else {
05041                 $sSql .=  'MAX(`oxfield2shop`.`oxprice'.$sPriceSufix.'`) AS `varmaxprice` ';
05042             }
05043             $sSql .=  ' FROM ' . getViewName('oxfield2shop') . ' AS oxfield2shop
05044                         INNER JOIN ' . $this->getViewName(true) . ' AS oxarticles ON `oxfield2shop`.`oxartid` = `oxarticles`.`oxid`
05045                         WHERE ' .$this->getSqlActiveSnippet(true) . '
05046                             AND ( `oxarticles`.`oxparentid` = ' . oxDb::getDb()->quote( $this->getId() ) . ' )
05047                             AND ( `oxfield2shop`.`oxshopid` = ' . oxDb::getDb()->quote( $sShopId ) . ' )';
05048             $dPrice = oxDb::getDb()->getOne( $sSql );
05049         }
05050         return $dPrice;
05051     }
05052 
05058     public function getUnitQuantity()
05059     {
05060         return $this->oxarticles__oxunitquantity->value;
05061     }
05062 
05068     public function getSize()
05069     {
05070         $dSize = $this->oxarticles__oxlength->value *
05071             $this->oxarticles__oxwidth->value *
05072             $this->oxarticles__oxheight->value;
05073 
05074         return $dSize;
05075     }
05076 
05082     public function getWeight()
05083     {
05084         return $this->oxarticles__oxweight->value;
05085     }
05086 
05092     protected function _updateParentDependFields()
05093     {
05094         $oDb = oxDb::getDb();
05095 
05096         foreach ( $this->_getCopyParentFields() as $sField ) {
05097             $sValue = isset( $this->$sField->value ) ? $this->$sField->value : 0;
05098             $sSqlSets[] = '`' . str_replace( 'oxarticles__', '', $sField )  . '` = ' . $oDb->quote( $sValue );
05099         }
05100 
05101         $sSql = "UPDATE `oxarticles` SET ";
05102         $sSql .= implode(', ', $sSqlSets) . '';
05103         $sSql .= " WHERE `oxparentid` = " . $oDb->quote( $this->getId() );
05104 
05105         return $oDb->execute( $sSql );
05106     }
05107 
05108 
05114     protected function _getCopyParentFields()
05115     {
05116         return $this->_aCopyParentField;
05117     }
05118 
05124     protected function _assignParentDependFields()
05125     {
05126         $sParent = $this->getParentArticle();
05127         if ( $sParent ) {
05128             foreach ( $this->_getCopyParentFields() as $sField ) {
05129                 $this->$sField = new oxField ( $sParent->$sField->value );
05130             }
05131         }
05132     }
05133 
05141     protected function loadSimpleVariantsForLists( $blSimple )
05142     {
05143         if ( $blSimple ) {
05144             $oVariants = oxNew( 'oxsimplevariantlist' );
05145             $oVariants->setParent( $this );
05146         } else {
05147             $oVariants = oxNew( 'oxarticlelist' );
05148             $oVariants->getBaseObject()->modifyCacheKey( '_variants' );
05149         }
05150 
05151         return $oVariants;
05152     }
05153 
05163     protected function _selectVariants( $blRemoveNotOrderables, $blForceCoreTable, $oVariants )
05164     {
05165         $myConfig = $this->getConfig();
05166 
05167         startProfile( "selectVariants" );
05168 
05169         $blUseCoreTable = (bool) $blForceCoreTable;
05170         $oBaseObject    = $oVariants->getBaseObject();
05171         $oBaseObject->setLanguage( $this->getLanguage() );
05172 
05173 
05174         $sArticleTable = $this->getViewName( $blUseCoreTable );
05175 
05176         $sSelect = "select " . $oBaseObject->getSelectFields( $blUseCoreTable ) . " from $sArticleTable where " .
05177                    $this->getActiveCheckQuery( $blUseCoreTable ) .
05178                    $this->getVariantsQuery( $blRemoveNotOrderables, $blUseCoreTable ) .
05179                    " order by $sArticleTable.oxsort";
05180 
05181 
05182         $oVariants->selectString( $sSelect );
05183 
05184         //if this is multidimensional variants, make additional processing
05185         if ( $myConfig->getConfigParam( 'blUseMultidimensionVariants' ) ) {
05186             $oMdVariants            = oxNew( "oxVariantHandler" );
05187             $this->_blHasMdVariants = $oMdVariants->isMdVariant( $oVariants->current() );
05188         }
05189 
05190         stopProfile( "selectVariants" );
05191 
05192         return $oVariants;
05193     }
05194 
05195 }