OXID eShop CE  4.8.10
 All Classes Files Functions Variables Pages
oxarticle.php
Go to the documentation of this file.
1 <?php
2 
3 // defining supported link types
4 define( 'OXARTICLE_LINKTYPE_CATEGORY', 0 );
5 define( 'OXARTICLE_LINKTYPE_VENDOR', 1 );
6 define( 'OXARTICLE_LINKTYPE_MANUFACTURER', 2 );
7 define( 'OXARTICLE_LINKTYPE_PRICECATEGORY', 3 );
8 define( 'OXARTICLE_LINKTYPE_TAG', 4 );
9 define( 'OXARTICLE_LINKTYPE_RECOMM', 5 );
10 
17 class oxArticle extends oxI18n implements oxIArticle, oxIUrl
18 {
19 
25  protected $_sClassName = 'oxarticle';
26 
32  protected $_blUseLazyLoading = true;
33 
39  protected $_sItemKey;
40 
46  protected $_blCalcPrice = true;
47 
52  protected $_oPrice = null;
53 
54 
60  protected $_dVarMinPrice = null;
61 
67  protected $_dVarMaxPrice = null;
68 
74  protected $_dArticleVat = null;
75 
81  protected $_aPersistParam = null;
82 
88  protected $_blNotBuyable = false;
89 
96  protected $_blLoadVariants = true;
97 
103  protected $_aVariants = null;
104 
111 
120  protected $_blNotBuyableParent = false;
121 
122 
126  protected $_blHasVariants = false;
127 
131  protected $_blHasMdVariants = false;
132 
137  protected $_blIsOnComparisonList = false;
138 
143  protected $_oUser = null;
144 
150  protected $_blLoadPrice = true;
151 
158  protected $_blSkipAbPrice = false;
159 
166  protected $_fPricePerUnit = null;
167 
171  protected $_blLoadParentData = false;
172 
176  protected $_blAllowEmptyParentId = false;
177 
181  protected $_blSkipAssign = false;
182 
188  protected $_blSkipDiscounts = null;
189 
194  protected $_oAttributeList = null;
195 
196 
202  protected $_blIsRangePrice = null;
203 
209  protected $_aMediaUrls = null;
210 
216  static protected $_aLoadedParents;
217 
223  static protected $_aSelList;
224 
230  protected $_aDispSelList;
231 
237  protected $_blIsSeoObject = true;
238 
244  protected $_oAmountPriceList = null;
245 
254  protected $_iLinkType = 0;
255 
261  protected $_aStdUrls = array();
262 
268  protected $_aSeoUrls = array();
269 
275  protected $_aSeoAddParams = array();
276 
282  protected $_aStdAddParams = array();
283 
289  protected $_sDynImageDir = null;
290 
296  protected $_sMoreDetailLink = null;
297 
303  protected $_sToBasketLink = null;
304 
310  protected $_iStockStatusOnLoad = null;
311 
317  protected $_iStockStatus = null;
318 
324  protected $_oTPrice = null;
325 
331  protected $_oAmountPriceInfo = null;
332 
338  protected $_dAmountPrice = null;
339 
345  protected static $_aArticleManufacturers = array();
346 
352  protected static $_aArticleVendors = array();
353 
359  protected static $_aArticleCats = array();
360 
366  protected $_aNonCopyParentFields = array('oxarticles__oxinsert',
367  'oxarticles__oxtimestamp',
368  'oxarticles__oxnid',
369  'oxarticles__oxid',
370  'oxarticles__oxparentid');
371 
377  protected $_aCopyParentField = array('oxarticles__oxnonmaterial',
378  'oxarticles__oxfreeshipping',
379  'oxarticles__oxisdownloadable',
380  'oxarticles__oxshowcustomagreement');
381 
387  protected $_oMdVariants = null;
388 
394  protected $_oLongDesc = null;
395 
403  protected $_aVariantSelections = array();
404 
409  protected static $_aSelections = array();
410 
415  protected static $_aCategoryCache = null;
416 
421  protected static $_blHasAmountPrice = null;
422 
427  protected $_aArticleFiles = null;
428 
433  protected $_blCanUpdateAnyField = null;
434 
443  public function __construct($aParams = null)
444  {
445  if ( $aParams && is_array($aParams)) {
446  foreach ( $aParams as $sParam => $mValue) {
447  $this->$sParam = $mValue;
448  }
449  }
451  $this->init( 'oxarticles' );
452  }
453 
462  public function __get($sName)
463  {
464  $this->$sName = parent::__get($sName);
465  if ( $this->$sName ) {
466  // since the field could have been loaded via lazy loading
467  $this->_assignParentFieldValue($sName);
468  }
469 
470  return $this->$sName;
471  }
472 
481  public function __set( $sName, $sValue )
482  {
483  parent::__set( $sName, $sValue );
484  }
485 
492  public function isInList()
493  {
494  return $this->_isInList();
495  }
496 
504  public function setId( $sId = null )
505  {
506  $sId = parent::setId( $sId );
507 
508  // TODO: in oxbase::setId make it to check if exists and update, not recreate, then delete this overload
509  $this->oxarticles__oxnid = $this->oxarticles__oxid;
510 
511  return $sId;
512  }
513 
523  public function getActiveCheckQuery( $blForceCoreTable = null )
524  {
525  $sTable = $this->getViewName( $blForceCoreTable );
526 
527  // check if article is still active
528  $sQ = " $sTable.oxactive = 1 ";
529 
530  // enabled time range check ?
531  if ( $this->getConfig()->getConfigParam( 'blUseTimeCheck' ) ) {
532  $sDate = date( 'Y-m-d H:i:s', oxRegistry::get("oxUtilsDate")->getTime() );
533  $sQ = "( $sQ or ( $sTable.oxactivefrom < '$sDate' and $sTable.oxactiveto > '$sDate' ) ) ";
534  }
535 
536  return $sQ;
537  }
538 
552  public function getStockCheckQuery( $blForceCoreTable = null )
553  {
554  $myConfig = $this->getConfig();
555  $sTable = $this->getViewName( $blForceCoreTable );
556 
557  $sQ = "";
558 
559  //do not check for variants
560  if ( $myConfig->getConfigParam( 'blUseStock' ) ) {
561  $sQ = " and ( $sTable.oxstockflag != 2 or ( $sTable.oxstock + $sTable.oxvarstock ) > 0 ) ";
562  //V #M513: When Parent article is not purchasable, it's visibility should be displayed in shop only if any of Variants is available.
563  if ( !$myConfig->getConfigParam( 'blVariantParentBuyable' ) ) {
564  $sTimeCheckQ = '';
565  if ( $myConfig->getConfigParam( 'blUseTimeCheck' ) ) {
566  $sDate = date( 'Y-m-d H:i:s', oxRegistry::get("oxUtilsDate")->getTime() );
567  $sTimeCheckQ = " or ( art.oxactivefrom < '$sDate' and art.oxactiveto > '$sDate' )";
568  }
569  $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 ) ) ";
570  }
571  }
572 
573  return $sQ;
574  }
575 
587  public function getVariantsQuery( $blRemoveNotOrderables, $blForceCoreTable = null )
588  {
589  $sTable = $this->getViewName( $blForceCoreTable );
590  $sQ = " and $sTable.oxparentid = '".$this->getId()."' ";
591 
592  //checking if variant is active and stock status
593  if ( $this->getConfig()->getConfigParam( 'blUseStock' ) ) {
594  $sQ .= " and ( $sTable.oxstock > 0 or ( $sTable.oxstock <= 0 and $sTable.oxstockflag != 2 ";
595  if ( $blRemoveNotOrderables ) {
596  $sQ .= " and $sTable.oxstockflag != 3 ";
597  }
598  $sQ .= " ) ) ";
599  }
600 
601  return $sQ;
602  }
603 
611  public function getSqlActiveSnippet( $blForceCoreTable = null )
612  {
613  $myConfig = $this->getConfig();
614 
615  // check if article is still active
616  $sQ = $this->getActiveCheckQuery( $blForceCoreTable );
617 
618  // stock and variants check
619  $sQ .= $this->getStockCheckQuery( $blForceCoreTable );
620 
621 
622  return "( $sQ ) ";
623  }
624 
632  public function setSkipAssign($blSkipAssign)
633  {
634  $this->_blSkipAssign = $blSkipAssign;
635  }
636 
642  public function disablePriceLoad()
643  {
644  $this->_blLoadPrice = false;
645  }
646 
652  public function enablePriceLoad()
653  {
654  $this->_blLoadPrice = true;
655  }
656 
662  public function getItemKey()
663  {
664  return $this->_sItemKey;
665  }
666 
674  public function setItemKey($sItemKey)
675  {
676  $this->_sItemKey = $sItemKey;
677  }
678 
686  public function setNoVariantLoading( $blLoadVariants )
687  {
688  $this->_blLoadVariants = !$blLoadVariants;
689  }
690 
696  public function isBuyable()
697  {
698  if ($this->_blNotBuyableParent) {
699  return false;
700  }
701 
702  return !$this->_blNotBuyable;
703  }
704 
710  public function getPersParams()
711  {
712  return $this->_aPersistParam;
713  }
714 
720  public function isOnComparisonList()
721  {
723  }
724 
732  public function setOnComparisonList( $blOnList )
733  {
734  $this->_blIsOnComparisonList = $blOnList;
735  }
736 
744  public function setLoadParentData($blLoadParentData)
745  {
746  $this->_blLoadParentData = $blLoadParentData;
747  }
748 
754  public function getLoadParentData()
755  {
757  }
758 
768  public function setSkipAbPrice( $blSkipAbPrice = null )
769  {
770  $this->_blSkipAbPrice = $blSkipAbPrice;
771  }
772 
773 
781  public function isMultilingualField($sFieldName)
782  {
783  switch ($sFieldName) {
784  case "oxlongdesc":
785  case "oxtags":
786  return true;
787  }
788 
789  return parent::isMultilingualField($sFieldName);
790  }
791 
797  public function isVisible()
798  {
799 
800  // admin preview mode
801  if ( ( $blCanPreview = oxRegistry::getUtils()->canPreview() ) !== null ) {
802  return $blCanPreview;
803  }
804 
805  // active ?
806  $sNow = date('Y-m-d H:i:s');
807  if ( !$this->oxarticles__oxactive->value &&
808  ( $this->oxarticles__oxactivefrom->value > $sNow ||
809  $this->oxarticles__oxactiveto->value < $sNow
810  )) {
811  return false;
812  }
813 
814  // stock flags
815  if ( $this->getConfig()->getConfigParam( 'blUseStock' ) && $this->oxarticles__oxstockflag->value == 2) {
816  $iOnStock = $this->oxarticles__oxstock->value + $this->oxarticles__oxvarstock->value;
817  if ($this->getConfig()->getConfigParam( 'blPsBasketReservationEnabled' )) {
818  $iOnStock += $this->getSession()->getBasketReservations()->getReservedAmount($this->getId());
819  }
820  if ( $iOnStock <= 0 ) {
821  return false;
822  }
823  }
824 
825  return true;
826  }
827 
836  public function assign( $aRecord )
837  {
838 
839  startProfile('articleAssign');
840 
841  // load object from database
842  parent::assign( $aRecord );
843 
844  $this->oxarticles__oxnid = $this->oxarticles__oxid;
845 
846  // check for simple article.
847  if ($this->_blSkipAssign) {
848  return null;
849  }
850 
851  $this->_assignParentFieldValues();
852  $this->_assignNotBuyableParent();
853 
854 
855  $this->_assignStock();
856  $this->_assignPersistentParam();
857  $this->_assignDynImageDir();
858  $this->_assignComparisonListFlag();
859 
860 
861  stopProfile('articleAssign');
862 
863  return null;
864  }
865 
866 
874  protected function _loadFromDb( $sOXID )
875  {
876 
877  $sSelect = $this->buildSelectString( array( $this->getViewName().".oxid" => $sOXID ));
878 
879 
880  $aData = oxDb::getDb( oxDb::FETCH_MODE_ASSOC )->getRow( $sSelect );
881 
882  return $aData;
883  }
884 
885 
896  public function load( $sOXID )
897  {
898  // A. #1325 resetting to avoid problems when reloading (details etc)
899  $this->_blNotBuyableParent = false;
900 
901 
902  $aData = $this->_loadFromDb( $sOXID );
903 
904  if ( $aData ) {
905  $this->assign( $aData );
906  // convert date's to international format
907 
908  $this->_iStockStatusOnLoad = $this->_iStockStatus;
909 
910  $this->_isLoaded = true;
911  return true;
912  }
913 
914  return false;
915  }
916 
917 
925  public function addToRatingAverage( $iRating )
926  {
927  $dOldRating = $this->oxarticles__oxrating->value;
928  $dOldCnt = $this->oxarticles__oxratingcnt->value;
929  $this->oxarticles__oxrating->setValue(( $dOldRating * $dOldCnt + $iRating ) / ($dOldCnt + 1));
930  $this->oxarticles__oxratingcnt->setValue($dOldCnt + 1);
931  $dRating = ( $dOldRating * $dOldCnt + $iRating ) / ($dOldCnt + 1);
932  $dRatingCnt = (int) ($dOldCnt + 1);
933  // oxarticles.oxtimestamp = oxarticles.oxtimestamp to keep old timestamp value
934  $oDb = oxDb::getDb();
935  $oDb->execute( 'update oxarticles set oxarticles.oxrating = '.$dRating.',oxarticles.oxratingcnt = '.$dRatingCnt.', oxarticles.oxtimestamp = oxarticles.oxtimestamp where oxarticles.oxid = '.$oDb->quote( $this->getId() ) );
936 
937  }
938 
946  public function setRatingAverage( $iRating )
947  {
948  $this->oxarticles__oxrating = new oxField( $iRating );
949  }
950 
958  public function setRatingCount( $iRatingCnt )
959  {
960  $this->oxarticles__oxratingcnt = new oxField( $iRatingCnt );
961  }
962 
970  public function getArticleRatingAverage( $blIncludeVariants = false )
971  {
972  if ( !$blIncludeVariants ) {
973  return round( $this->oxarticles__oxrating->value, 1);
974  } else {
975  $oRating = oxNew( 'oxRating' );
976  return $oRating->getRatingAverage( $this->getId(), 'oxarticle', $this->_getVariantsIds() );
977  }
978  }
979 
987  public function getArticleRatingCount( $blIncludeVariants = false )
988  {
989  if ( !$blIncludeVariants ) {
990  return $this->oxarticles__oxratingcnt->value;
991  } else {
992  $oRating = oxNew( 'oxRating' );
993  return $oRating->getRatingCount( $this->getId(), 'oxarticle', $this->_getVariantsIds() );
994  }
995  }
996 
997 
1003  public function getReviews()
1004  {
1005  $aIds = array($this->getId());
1006 
1007  if ( $this->oxarticles__oxparentid->value ) {
1008  $aIds[] = $this->oxarticles__oxparentid->value;
1009  }
1010 
1011  // showing variant reviews ..
1012  if ( $this->getConfig()->getConfigParam( 'blShowVariantReviews' ) ) {
1013  $aAdd = $this->_getVariantsIds();
1014  if (is_array($aAdd)) {
1015  $aIds = array_merge($aIds, $aAdd);
1016  }
1017  }
1018 
1019  $oReview = oxNew('oxreview');
1020  $oRevs = $oReview->loadList('oxarticle', $aIds);
1021 
1022  //if no review found, return null
1023  if ( $oRevs->count() < 1 ) {
1024  return null;
1025  }
1026 
1027  return $oRevs;
1028  }
1029 
1035  public function getCrossSelling()
1036  {
1037  $oCrosslist = oxNew( "oxarticlelist");
1038  $oCrosslist->loadArticleCrossSell($this->oxarticles__oxid->value);
1039  if ( $oCrosslist->count() ) {
1040  return $oCrosslist;
1041  }
1042 
1043  return null;
1044  }
1045 
1051  public function getAccessoires()
1052  {
1053  $myConfig = $this->getConfig();
1054 
1055  // Performance
1056  if ( !$myConfig->getConfigParam( 'bl_perfLoadAccessoires' ) ) {
1057  return null;
1058  }
1059 
1060  $oAcclist = oxNew( "oxarticlelist");
1061  $oAcclist->setSqlLimit( 0, $myConfig->getConfigParam( 'iNrofCrossellArticles' ));
1062  $oAcclist->loadArticleAccessoires($this->oxarticles__oxid->value);
1063 
1064  if ( $oAcclist->count()) {
1065  return $oAcclist;
1066  }
1067 
1068  return null;
1069  }
1070 
1076  public function getSimilarProducts()
1077  {
1078  // Performance
1079  $myConfig = $this->getConfig();
1080  if ( !$myConfig->getConfigParam( 'bl_perfLoadSimilar' ) ) {
1081  return null;
1082  }
1083 
1084  $sArticleTable = $this->getViewName();
1085 
1086  $sAttribs = '';
1087  $iCnt = 0;
1088  $this->_getAttribsString($sAttribs, $iCnt);
1089 
1090  if ( !$sAttribs) {
1091  return null;
1092  }
1093 
1094  $aList = $this->_getSimList($sAttribs, $iCnt);
1095 
1096  if ( count( $aList ) ) {
1097  uasort( $aList, 'cmpart');
1098 
1099  $sSearch = $this->_generateSimListSearchStr($sArticleTable, $aList);
1100 
1101  $oSimilarlist = oxNew( 'oxarticlelist' );
1102  $oSimilarlist->setSqlLimit( 0, $myConfig->getConfigParam( 'iNrofSimilarArticles' ));
1103  $oSimilarlist->selectString( $sSearch);
1104 
1105  return $oSimilarlist;
1106  }
1107 
1108  return null;
1109  }
1110 
1117  {
1118  // Performance
1119  $myConfig = $this->getConfig();
1120  if ( !$myConfig->getConfigParam( 'bl_perfLoadCustomerWhoBoughtThis' ) ) {
1121  return null;
1122  }
1123 
1124  // selecting products that fits
1125  $sQ = $this->_generateSearchStrForCustomerBought();
1126 
1127  $oArticles = oxNew( 'oxarticlelist' );
1128  $oArticles->setSqlLimit( 0, $myConfig->getConfigParam( 'iNrofCustomerWhoArticles' ));
1129  $oArticles->selectString( $sQ );
1130  if ( $oArticles->count() ) {
1131  return $oArticles;
1132  }
1133 
1134  return null;
1135  }
1136 
1143  public function loadAmountPriceInfo()
1144  {
1145  $myConfig = $this->getConfig();
1146  if ( !$myConfig->getConfigParam( 'bl_perfLoadPrice' ) || !$this->_blLoadPrice || !$this->_blCalcPrice || !$this->hasAmountPrice() ) {
1147  return array();
1148  }
1149 
1150  if ( $this->_oAmountPriceInfo === null ) {
1151  $this->_oAmountPriceInfo = array();
1152  if ( count( ( $aAmPriceList = $this->_getAmountPriceList()->getArray() ) ) ) {
1153  $this->_oAmountPriceInfo = $this->_fillAmountPriceList( $aAmPriceList );
1154  }
1155  }
1156  return $this->_oAmountPriceInfo;
1157  }
1158 
1166  public function getSelectLists($sKeyPrefix = null)
1167  {
1168  //#1468C - more then one article in basket with different selectlist...
1169  //optionall function parameter $sKeyPrefix added, used only in basket.php
1170  $sKey = $this->getId();
1171  if ( isset( $sKeyPrefix ) ) {
1172  $sKey = $sKeyPrefix.'__'.$sKey;
1173  }
1174 
1175  if ( !isset( self::$_aSelList[$sKey] ) ) {
1176  $oDb = oxDb::getDb();
1177  $sSLViewName = getViewName( 'oxselectlist' );
1178 
1179  $sQ = "select {$sSLViewName}.* from oxobject2selectlist join {$sSLViewName} on $sSLViewName.oxid=oxobject2selectlist.oxselnid
1180  where oxobject2selectlist.oxobjectid=%s order by oxobject2selectlist.oxsort";
1181 
1182  // all selectlists this article has
1183  $oLists = oxNew( 'oxlist' );
1184  $oLists->init( 'oxselectlist' );
1185  $oLists->selectString( sprintf( $sQ, $oDb->quote( $this->getId() ) ) );
1186 
1187  //#1104S if this is variant ant it has no selectlists, trying with parent
1188  if ( $oLists->count() == 0 && $this->oxarticles__oxparentid->value ) {
1189  $oLists->selectString( sprintf( $sQ, $oDb->quote( $this->oxarticles__oxparentid->value ) ) );
1190  }
1191 
1192  // We do not need to calculate price here as there are method to get current article vat
1193  /*if ( $this->getPrice() != null ) {
1194  $dVat = $this->getPrice()->getVat();
1195  }*/
1196  $dVat = $this->getArticleVat();
1197 
1198  $iCnt = 0;
1199  self::$_aSelList[$sKey] = array();
1200  foreach ( $oLists as $oSelectlist ) {
1201  self::$_aSelList[$sKey][$iCnt] = $oSelectlist->getFieldList( $dVat );
1202  self::$_aSelList[$sKey][$iCnt]['name'] = $oSelectlist->oxselectlist__oxtitle->value;
1203  $iCnt++;
1204  }
1205  }
1206  return self::$_aSelList[$sKey];
1207  }
1208 
1216  protected function _hasAnyVariant( $blForceCoreTable = null )
1217  {
1218  $blHas = false;
1219  if ( ( $sId = $this->getId() ) ) {
1220  if ( $this->oxarticles__oxshopid->value == $this->getConfig()->getShopId() ) {
1221  $blHas = (bool) $this->oxarticles__oxvarcount->value;
1222  } else {
1223  $sArticleTable = $this->getViewName( $blForceCoreTable );
1224  $blHas = (bool) oxDb::getDb()->getOne( "select 1 from $sArticleTable where oxparentid='{$sId}'" );
1225  }
1226  }
1227  return $blHas;
1228  }
1229 
1235  public function getVariantsCount()
1236  {
1237  return $this->oxarticles__oxvarcount->value;
1238  }
1239 
1245  public function hasMdVariants()
1246  {
1247  return $this->_blHasMdVariants;
1248  }
1249 
1255  public function hasIntangibleAgreement()
1256  {
1257  return $this->oxarticles__oxshowcustomagreement->value && $this->oxarticles__oxnonmaterial->value && !$this->hasDownloadableAgreement();
1258  }
1259 
1265  public function hasDownloadableAgreement()
1266  {
1267  return $this->oxarticles__oxshowcustomagreement->value && $this->oxarticles__oxisdownloadable->value;
1268  }
1269 
1279  public function getVariantSelections( $aFilterIds = null, $sActVariantId = null, $iLimit = 0 )
1280  {
1281  $iLimit = (int) $iLimit;
1282  if ( !isset( $this->_aVariantSelections[$iLimit] ) ) {
1283  $aVariantSelections = false;
1284  if ( $this->oxarticles__oxvarcount->value ) {
1285  $oVariants = $this->getVariants( false );
1286  $aVariantSelections = oxNew( "oxVariantHandler" )->buildVariantSelections( $this->oxarticles__oxvarname->getRawValue(), $oVariants, $aFilterIds, $sActVariantId, $iLimit );
1287 
1288  if ( !empty($oVariants) && empty( $aVariantSelections['rawselections'] ) ) {
1289  $aVariantSelections = false;
1290  }
1291  }
1292  $this->_aVariantSelections[$iLimit] = $aVariantSelections;
1293  }
1294 
1295  return $this->_aVariantSelections[$iLimit];
1296  }
1297 
1306  public function getSelections( $iLimit = null, $aFilter = null )
1307  {
1308  $sId = $this->getId() . ( (int) $iLimit );
1309  if ( !array_key_exists( $sId, self::$_aSelections ) ) {
1310 
1311  $oDb = oxDb::getDb();
1312  $sSLViewName = getViewName( 'oxselectlist' );
1313 
1314  $sQ = "select {$sSLViewName}.* from oxobject2selectlist join {$sSLViewName} on $sSLViewName.oxid=oxobject2selectlist.oxselnid
1315  where oxobject2selectlist.oxobjectid=%s order by oxobject2selectlist.oxsort";
1316 
1317  if ( ( $iLimit = (int) $iLimit ) ) {
1318  $sQ .= " limit $iLimit ";
1319  }
1320 
1321  // vat value for price
1322  $dVat = 0;
1323  if ( ( $oPrice = $this->getPrice() ) != null ) {
1324  $dVat = $oPrice->getVat();
1325  }
1326 
1327  // all selectlists this article has
1328  $oList = oxNew( 'oxlist' );
1329  $oList->init( 'oxselectlist' );
1330  $oList->getBaseObject()->setVat( $dVat );
1331  $oList->selectString( sprintf( $sQ, $oDb->quote( $this->getId() ) ) );
1332 
1333  //#1104S if this is variant and it has no selectlists, trying with parent
1334  if ( $oList->count() == 0 && $this->oxarticles__oxparentid->value ) {
1335  $oList->selectString( sprintf( $sQ, $oDb->quote( $this->oxarticles__oxparentid->value ) ) );
1336  }
1337 
1338  self::$_aSelections[$sId] = $oList->count() ? $oList : false;
1339  }
1340 
1341  if ( self::$_aSelections[$sId] ) {
1342  // marking active from filter
1343  $aFilter = ( $aFilter === null ) ? oxConfig::getParameter( "sel" ) : $aFilter;
1344  if ( $aFilter ) {
1345  $iSelIdx = 0;
1346  foreach ( self::$_aSelections[$sId] as $oSelection ) {
1347  if ( isset( $aFilter[$iSelIdx] ) ) {
1348  $oSelection->setActiveSelectionByIndex( $aFilter[$iSelIdx] );
1349  }
1350  $iSelIdx++;
1351  }
1352  }
1353  }
1354 
1355  return self::$_aSelections[$sId];
1356  }
1357 
1367  protected function _loadVariantList( $blSimple, $blRemoveNotOrderables = true, $blForceCoreTable = null )
1368  {
1369  $oVariants = array();
1370  $sId = $this->getId();
1371 
1372  if (!$sId) {
1373  return $oVariants;
1374  }
1375 
1376  //do not load me as a parent later
1377  self::$_aLoadedParents[$sId . "_" . $this->getLanguage()] = $this;
1378 
1379  $myConfig = $this->getConfig();
1380 
1381  if ( !$this->_blLoadVariants ||
1382  ( !$this->isAdmin() && !$myConfig->getConfigParam( 'blLoadVariants') ) ||
1383  ( !$this->isAdmin() && !$this->oxarticles__oxvarcount->value ) ) {
1384  return $oVariants;
1385  }
1386 
1387  // cache
1388  $sCacheKey = $blSimple ? "simple" : "full";
1389  if ( $blRemoveNotOrderables ) {
1390  if ( isset( $this->_aVariants[$sCacheKey] ) ) {
1391  return $this->_aVariants[$sCacheKey];
1392  } else {
1393  $this->_aVariants[$sCacheKey] = & $oVariants;
1394  }
1395  } elseif ( !$blRemoveNotOrderables ) {
1396  if ( isset( $this->_aVariantsWithNotOrderables[$sCacheKey] ) ) {
1397  return $this->_aVariantsWithNotOrderables[$sCacheKey];
1398  } else {
1399  $this->_aVariantsWithNotOrderables[$sCacheKey] = & $oVariants;
1400  }
1401  }
1402 
1403  if ( ( $this->_blHasVariants = $this->_hasAnyVariant( $blForceCoreTable ) ) ) {
1404 
1405  $oVariants = $this->loadSimpleVariantsForLists( $blSimple );
1406 
1407  $oVariants = $this->_selectVariants( $blRemoveNotOrderables, $blForceCoreTable, $oVariants );
1408  }
1409 
1410  //if we have variants then depending on config option the parent may be non buyable
1411  if ( !$myConfig->getConfigParam( 'blVariantParentBuyable' ) && $this->_blHasVariants ) {
1412  $this->_blNotBuyableParent = true;
1413  }
1414 
1415  //if we have variants, but all variants are incative means article may be non buyable (depends on config option)
1416  if ( !$myConfig->getConfigParam( 'blVariantParentBuyable' ) && count( $oVariants ) == 0 && $this->_blHasVariants ) {
1417  $this->_blNotBuyable = true;
1418  }
1419 
1420  return $oVariants;
1421  }
1422 
1431  public function getFullVariants( $blRemoveNotOrderables = true, $blForceCoreTable = null )
1432  {
1433  return $this->_loadVariantList( false, $blRemoveNotOrderables, $blForceCoreTable );
1434  }
1435 
1444  public function getVariants( $blRemoveNotOrderables = true, $blForceCoreTable = null )
1445  {
1446  return $this->_loadVariantList( $this->_isInList(), $blRemoveNotOrderables, $blForceCoreTable );
1447  }
1448 
1454  public function getSimpleVariants()
1455  {
1456  if ( $this->oxarticles__oxvarcount->value) {
1457  return $this->getVariants();
1458  }
1459 
1460  return null;
1461  }
1462 
1471  public function getAdminVariants( $sLanguage = null )
1472  {
1473  $oVariants = oxNew( 'oxarticlelist');
1474  if ( ( $sId = $this->getId() ) ) {
1475 
1476  $oBaseObj = $oVariants->getBaseObject();
1477 
1478  if ( is_null( $sLanguage ) ) {
1479  $oBaseObj->setLanguage( oxRegistry::getLang()->getBaseLanguage() );
1480  } else {
1481  $oBaseObj->setLanguage( $sLanguage );
1482  }
1483 
1484  $sSql = "select * from ".$oBaseObj->getViewName()." where oxparentid = '{$sId}' order by oxsort ";
1485  $oVariants->selectString( $sSql );
1486 
1487  //if we have variants then depending on config option the parent may be non buyable
1488  if ( !$this->getConfig()->getConfigParam( 'blVariantParentBuyable' ) && ( $oVariants->count() > 0 ) ) {
1489  //$this->blNotBuyable = true;
1490  $this->_blNotBuyableParent = true;
1491  }
1492  }
1493 
1494  return $oVariants;
1495  }
1496 
1504  public function getCategory()
1505  {
1506  $oCategory = oxNew( 'oxcategory' );
1507  $oCategory->setLanguage( $this->getLanguage() );
1508 
1509  // variant handling
1510  $sOXID = $this->getId();
1511  if ( isset( $this->oxarticles__oxparentid->value ) && $this->oxarticles__oxparentid->value ) {
1512  $sOXID = $this->oxarticles__oxparentid->value;
1513  }
1514 
1515  if ( $sOXID ) {
1516  // if the oxcategory instance of this article is not cached
1517  if ( !isset( $this->_aCategoryCache[ $sOXID ] ) ) {
1518  startPRofile( 'getCategory' );
1519  $oStr = getStr();
1520  $sWhere = $oCategory->getSqlActiveSnippet();
1521  $sSelect = $this->_generateSearchStr( $sOXID );
1522  $sSelect .= ( $oStr->strstr( $sSelect, 'where' )?' and ':' where ') . $sWhere . " order by oxobject2category.oxtime limit 1";
1523 
1524  // category not found ?
1525  if ( !$oCategory->assignRecord( $sSelect ) ) {
1526 
1527  $sSelect = $this->_generateSearchStr( $sOXID, true );
1528  $sSelect .= ( $oStr->strstr( $sSelect, 'where' )?' and ':' where ') . $sWhere . " limit 1";
1529 
1530  // looking for price category
1531  if ( !$oCategory->assignRecord( $sSelect ) ) {
1532  $oCategory = null;
1533  }
1534  }
1535  // add the category instance to cache
1536  $this->_aCategoryCache[ $sOXID ] = $oCategory;
1537  stopPRofile( 'getCategory' );
1538  } else {
1539  // if the oxcategory instance is cached
1540  $oCategory = $this->_aCategoryCache[ $sOXID ];
1541  }
1542  }
1543 
1544  return $oCategory;
1545  }
1546 
1555  public function getCategoryIds( $blActCats = false, $blSkipCache = false )
1556  {
1557  if ( isset( self::$_aArticleCats[$this->getId()] ) && !$blSkipCache ) {
1558  return self::$_aArticleCats[$this->getId()];
1559  }
1560 
1561  // variant handling
1562  $sOXID = $this->getId();
1563 
1564  $aRet = $this->_getArticleCategories( $sOXID, $blActCats );
1565 
1566  if (isset( $this->oxarticles__oxparentid->value) && $this->oxarticles__oxparentid->value) {
1567  $aRet = array_merge( $aRet, $this->_getArticleCategories( $this->oxarticles__oxparentid->value, $blActCats ) );
1568  }
1569 
1570  // adding price categories if such exists
1571  $sSql = $this->getSqlForPriceCategories();
1572 
1574  $rs = $oDb->select( $sSql );
1575 
1576 
1577  if ($rs != false && $rs->recordCount() > 0) {
1578  while (!$rs->EOF) {
1579 
1580  if ( is_array( $rs->fields ) ) {
1581  $rs->fields = array_change_key_case( $rs->fields, CASE_LOWER );
1582  }
1583 
1584 
1585  if ( !$aRet[$rs->fields['oxid']] ) {
1586  $aRet[] = $rs->fields['oxid'];
1587  }
1588  $rs->moveNext();
1589  }
1590  }
1591 
1592  return self::$_aArticleCats[$this->getId()] = array_unique($aRet);
1593  }
1594 
1603  protected function _getArticleCategories( $sOXID, $blActCats = false )
1604  {
1605  // we do not use lists here as we don't need this overhead right now
1606  $sSql = $this->_getSelectCatIds( $sOXID, $blActCats );
1608  $rs = $oDb->select( $sSql );
1609 
1610 
1611  $aRet = array();
1612 
1613  if ($rs != false && $rs->recordCount() > 0) {
1614  while (!$rs->EOF) {
1615  $aRet[] = $rs->fields['oxcatnid'];
1616  $rs->moveNext();
1617  }
1618  }
1619 
1620  return $aRet;
1621  }
1622 
1631  protected function _getSelectCatIds( $sOXID, $blActCats = false )
1632  {
1633  $sO2CView = $this->_getObjectViewName('oxobject2category');
1634  $sCatView = $this->_getObjectViewName('oxcategories');
1635  $sSelect = "select oxobject2category.oxcatnid as oxcatnid from $sO2CView as oxobject2category left join $sCatView as oxcategories on oxcategories.oxid = oxobject2category.oxcatnid ";
1636  $sSelect .= 'where oxobject2category.oxobjectid='.oxDb::getDb()->quote($sOXID).' and oxcategories.oxid is not null and oxcategories.oxactive = 1 ';
1637  if ( $blActCats ) {
1638  $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 ";
1639  }
1640  $sSelect .= 'order by oxobject2category.oxtime ';
1641  return $sSelect;
1642  }
1643 
1653  public function getVendor( $blShopCheck = true )
1654  {
1655  if ( ( $sVendorId = $this->getVendorId() ) ) {
1656  $oVendor = oxNew( 'oxvendor' );
1657  } elseif ( !$blShopCheck && $this->oxarticles__oxvendorid->value ) {
1658  $oVendor = oxNew( 'oxi18n' );
1659  $oVendor->init('oxvendor');
1660  $oVendor->setReadOnly( true );
1661  $sVendorId = $this->oxarticles__oxvendorid->value;
1662  }
1663  if ( $sVendorId && $oVendor->load( $sVendorId ) && $oVendor->oxvendor__oxactive->value ) {
1664 
1665  return $oVendor;
1666  }
1667  return null;
1668  }
1669 
1677  public function getVendorId( $blForceReload = false )
1678  {
1679  $sVendorId = false;
1680  if ( $this->oxarticles__oxvendorid->value ) {
1681  $sVendorId = $this->oxarticles__oxvendorid->value;
1682 
1683  }
1684  return $sVendorId;
1685  }
1686 
1694  public function getManufacturerId( $blForceReload = false )
1695  {
1696  $sManufacturerId = false;
1697  if ( $this->oxarticles__oxmanufacturerid->value ) {
1698 
1699  $sManufacturerId = $this->oxarticles__oxmanufacturerid->value;
1700 
1701  }
1702  return $sManufacturerId;
1703  }
1704 
1714  public function getManufacturer( $blShopCheck = true )
1715  {
1716  $oManufacturer = oxNew( 'oxmanufacturer' );;
1717  if ( !( $sManufacturerId = $this->getManufacturerId() ) &&
1718  !$blShopCheck && $this->oxarticles__oxmanufacturerid->value ) {
1719  $oManufacturer->setReadOnly( true );
1720  $sManufacturerId = $this->oxarticles__oxmanufacturerid->value;
1721  }
1722 
1723  if ( $sManufacturerId && $oManufacturer->load( $sManufacturerId ) ) {
1724  if ( !$this->getConfig()->getConfigParam( 'bl_perfLoadManufacturerTree' ) ) {
1725  $oManufacturer->setReadOnly( true );
1726  }
1727  $oManufacturer = $oManufacturer->oxmanufacturers__oxactive->value ? $oManufacturer : null;
1728  } else {
1729  $oManufacturer = null;
1730  }
1731 
1732  return $oManufacturer;
1733  }
1734 
1742  public function inCategory( $sCatNid)
1743  {
1744  return in_array( $sCatNid, $this->getCategoryIds());
1745  }
1746 
1755  public function isAssignedToCategory( $sCatId )
1756  {
1757  // variant handling
1758  $sOXID = $this->getId();
1759  if ( isset( $this->oxarticles__oxparentid->value) && $this->oxarticles__oxparentid->value) {
1760  $sOXID = $this->oxarticles__oxparentid->value;
1761  }
1762 
1763  $oDb = oxDb::getDb();
1764  $sSelect = $this->_generateSelectCatStr( $sOXID, $sCatId);
1765  $sOXID = $oDb->getOne( $sSelect );
1766  // article is assigned to passed category!
1767  if ( isset( $sOXID) && $sOXID) {
1768  return true;
1769  }
1770 
1771  // maybe this category is price category ?
1772  if ( $this->getConfig()->getConfigParam( 'bl_perfLoadPrice' ) && $this->_blLoadPrice ) {
1773  $dPriceFromTo = $this->getPrice()->getBruttoPrice();
1774  if ( $dPriceFromTo > 0) {
1775  $sSelect = $this->_generateSelectCatStr( $sOXID, $sCatId, $dPriceFromTo);
1776  $sOXID = $oDb->getOne( $sSelect );
1777  // article is assigned to passed category!
1778  if ( isset( $sOXID) && $sOXID) {
1779  return true;
1780  }
1781  }
1782  }
1783  return false;
1784  }
1785 
1791  public function getTPrice()
1792  {
1793  if ( !$this->getConfig()->getConfigParam( 'bl_perfLoadPrice' ) || !$this->_blLoadPrice ) {
1794  return null;
1795  }
1796 
1797  // return cached result, since oPrice is created ONLY in this function [or function of EQUAL level]
1798  if ( $this->_oTPrice !== null ) {
1799  return $this->_oTPrice;
1800  }
1801 
1802  $oPrice = $this->_getPriceObject();
1803 
1804  $dBasePrice = $this->oxarticles__oxtprice->value;
1805  $dBasePrice = $this->_preparePrice( $dBasePrice, $this->getArticleVat() );
1806 
1807  $oPrice->setPrice( $dBasePrice );
1808 
1809  $this->_applyVat( $oPrice, $this->getArticleVat() );
1810  $this->_applyCurrency( $oPrice );
1811 
1812  if ( $this->isParentNotBuyable() ) {
1813  // if parent article is not buyable then compare agains min article variant price
1814  $oPrice2 = $this->getVarMinPrice();
1815  } else {
1816  // else compare against article price
1817  $oPrice2 = $this->getPrice();
1818  }
1819 
1820  if ( $oPrice->getPrice() <= $oPrice2->getPrice() ) {
1821  // if RRP price is less or equal to comparable price then return
1822  return null;
1823  }
1824 
1825  $this->_oTPrice = $oPrice;
1826 
1827  return $this->_oTPrice;
1828  }
1829 
1835  public function skipDiscounts()
1836  {
1837  // already loaded skip discounts config
1838  if ( $this->_blSkipDiscounts !== null ) {
1839  return $this->_blSkipDiscounts;
1840  }
1841 
1842  if ( $this->oxarticles__oxskipdiscounts->value ) {
1843  return true;
1844  }
1845 
1846 
1847  $this->_blSkipDiscounts = false;
1848  if ( oxRegistry::get("oxDiscountList")->hasSkipDiscountCategories() ) {
1849 
1850  $oDb = oxDb::getDb();
1851  $sO2CView = getViewName( 'oxobject2category', $this->getLanguage() );
1852  $sViewName = getViewName( 'oxcategories', $this->getLanguage() );
1853  $sSelect = "select 1 from $sO2CView as $sO2CView left join {$sViewName} on {$sViewName}.oxid = $sO2CView.oxcatnid
1854  where $sO2CView.oxobjectid=".$oDb->quote( $this->getId() )." and {$sViewName}.oxactive = 1 and {$sViewName}.oxskipdiscounts = '1' ";
1855  $this->_blSkipDiscounts = ( $oDb->getOne( $sSelect ) == 1 );
1856  }
1857 
1858  return $this->_blSkipDiscounts;
1859  }
1860 
1868  public function setPrice(oxPrice $oPrice)
1869  {
1870  $this->_oPrice = $oPrice;
1871  }
1872 
1881  public function getBasePrice( $dAmount = 1 )
1882  {
1883  // override this function if you want e.g. different prices
1884  // for diff. user groups.
1885 
1886  // Performance
1887  $myConfig = $this->getConfig();
1888  if( !$myConfig->getConfigParam( 'bl_perfLoadPrice' ) || !$this->_blLoadPrice )
1889  return null;
1890 
1891  // GroupPrice or DB price ajusted by AmountPrice
1892  $dPrice = $this->_getAmountPrice( $dAmount );
1893 
1894 
1895  return $dPrice;
1896  }
1897 
1905  public function getPrice( $dAmount = 1 )
1906  {
1907  $myConfig = $this->getConfig();
1908  // Performance
1909  if ( !$myConfig->getConfigParam( 'bl_perfLoadPrice' ) || !$this->_blLoadPrice ) {
1910  return null;
1911  }
1912 
1913  // return cached result, since oPrice is created ONLY in this function [or function of EQUAL level]
1914  if ( $dAmount != 1 || $this->_oPrice === null ) {
1915 
1916  // module
1917  $dBasePrice = $this->getBasePrice( $dAmount );
1918  $dBasePrice = $this->_preparePrice( $dBasePrice, $this->getArticleVat() );
1919 
1920  $oPrice = $this->_getPriceObject();
1921 
1922  $oPrice->setPrice( $dBasePrice );
1923 
1924  // price handling
1925  if ( !$this->_blCalcPrice && $dAmount == 1 ) {
1926  return $this->_oPrice = $oPrice;
1927  }
1928 
1929  $this->_calculatePrice( $oPrice );
1930  if ( $dAmount != 1 ) {
1931  return $oPrice;
1932  }
1933 
1934  $this->_oPrice = $oPrice;
1935  }
1936  return $this->_oPrice;
1937  }
1938 
1947  protected function _calculatePrice( $oPrice, $dVat = null )
1948  {
1949  // apply VAT only if configuration requires it
1950  if ( isset( $dVat ) || !$this->getConfig()->getConfigParam( 'bl_perfCalcVatOnlyForBasketOrder' ) ) {
1951  $this->_applyVAT( $oPrice, isset( $dVat ) ? $dVat : $this->getArticleVat() );
1952  }
1953 
1954  // apply currency
1955  $this->_applyCurrency( $oPrice );
1956  // apply discounts
1957  if ( !$this->skipDiscounts() ) {
1958  $oDiscountList = oxRegistry::get("oxDiscountList");
1959  $aDiscounts = $oDiscountList->getArticleDiscounts( $this, $this->getArticleUser() );
1960 
1961  reset( $aDiscounts );
1962  foreach ( $aDiscounts as $oDiscount ) {
1963  $oPrice->setDiscount($oDiscount->getAddSum(), $oDiscount->getAddSumType());
1964  }
1965  $oPrice->calculateDiscount();
1966  }
1967 
1968  return $oPrice;
1969  }
1970 
1978  public function setArticleUser($oUser)
1979  {
1980  $this->_oUser = $oUser;
1981  }
1982 
1988  public function getArticleUser()
1989  {
1990  if ($this->_oUser) {
1991  return $this->_oUser;
1992  }
1993  return $this->getUser();
1994  }
1995 
2005  public function getBasketPrice( $dAmount, $aSelList, $oBasket )
2006  {
2007  $oUser = $oBasket->getBasketUser();
2008  $this->setArticleUser( $oUser );
2009 
2010  $oBasketPrice = $this->_getPriceObject( $oBasket->isCalculationModeNetto() );
2011 
2012  // get base price
2013  $dBasePrice = $this->getBasePrice( $dAmount );
2014 
2015  $dBasePrice = $this->_modifySelectListPrice( $dBasePrice, $aSelList );
2016  $dBasePrice = $this->_preparePrice( $dBasePrice, $this->getArticleVat(), $oBasket->isCalculationModeNetto() );
2017 
2018  // applying select list price
2019 
2020  // setting price
2021  $oBasketPrice->setPrice( $dBasePrice );
2022 
2023  $dVat = oxRegistry::get("oxVatSelector")->getBasketItemVat( $this, $oBasket );
2024  $this->_calculatePrice( $oBasketPrice, $dVat );
2025 
2026  // returning final price object
2027  return $oBasketPrice;
2028  }
2029 
2038  public function delete( $sOXID = null )
2039  {
2040  if ( !$sOXID ) {
2041  $sOXID = $this->getId();
2042  }
2043  if ( !$sOXID ) {
2044  return false;
2045  }
2046 
2047 
2048 
2049  // #2339 delete first variants before deleting parent product
2050  $this->_deleteVariantRecords( $sOXID );
2051  $this->load( $sOXID );
2052  $this->_deletePics();
2053  $this->_onChangeResetCounts( $sOXID, $this->oxarticles__oxvendorid->value, $this->oxarticles__oxmanufacturerid->value );
2054 
2055  // delete self
2056  parent::delete( $sOXID );
2057 
2058  $rs = $this->_deleteRecords( $sOXID );
2059 
2060  oxRegistry::get("oxSeoEncoderArticle")->onDeleteArticle($this);
2061 
2062  $this->onChange( ACTION_DELETE, $sOXID, $this->oxarticles__oxparentid->value );
2063 
2064  return $rs->EOF;
2065  }
2066 
2075  public function reduceStock($dAmount, $blAllowNegativeStock = false)
2076  {
2077  $this->beforeUpdate();
2078 
2079  $iStockCount = $this->oxarticles__oxstock->value - $dAmount;
2080  if (!$blAllowNegativeStock && ($iStockCount < 0)) {
2081  $dAmount += $iStockCount;
2082  $iStockCount = 0;
2083  }
2084  $this->oxarticles__oxstock = new oxField($iStockCount);
2085 
2086  $oDb = oxDb::getDb();
2087  $oDb->execute( 'update oxarticles set oxarticles.oxstock = '.$oDb->quote( $iStockCount ).' where oxarticles.oxid = '.$oDb->quote( $this->getId() ) );
2088  $this->onChange( ACTION_UPDATE_STOCK );
2089  return $dAmount;
2090  }
2091 
2100  public function updateSoldAmount( $dAmount = 0 )
2101  {
2102  if ( !$dAmount ) {
2103  return null;
2104  }
2105 
2106  // article is not variant - should be updated current amount
2107  if ( !$this->oxarticles__oxparentid->value ) {
2108  //updating by SQL query, due to wrong behaviour if saving article using not admin mode
2109  $dAmount = (double) $dAmount;
2110  $oDb = oxDb::getDb();
2111  $rs = $oDb->execute( "update oxarticles set oxarticles.oxsoldamount = oxarticles.oxsoldamount + $dAmount where oxarticles.oxid = ".$oDb->quote($this->oxarticles__oxid->value));
2112  } elseif ( $this->oxarticles__oxparentid->value) {
2113  // article is variant - should be updated this article parent amount
2114  $oUpdateArticle = $this->getParentArticle();
2115  $oUpdateArticle->updateSoldAmount( $dAmount );
2116  }
2117 
2118  return $rs;
2119  }
2120 
2126  public function disableReminder()
2127  {
2128  $oDb = oxDb::getDb();
2129  return $oDb->execute( "update oxarticles set oxarticles.oxremindactive = 2 where oxarticles.oxid = ".$oDb->quote($this->oxarticles__oxid->value));
2130  }
2131 
2137  public function save()
2138  {
2139  $this->_assignParentDependFields();
2140 
2141  if ( ( $blRet = parent::save() ) ) {
2142  // saving long description
2143  $this->_saveArtLongDesc();
2144  }
2145 
2146  return $blRet;
2147  }
2148 
2154  public function resetParent()
2155  {
2156  $sParentId = $this->oxarticles__oxparentid->value;
2157  $this->oxarticles__oxparentid = new oxField( '', oxField::T_RAW );
2158  $this->_blAllowEmptyParentId = true;
2159  $this->save();
2160  $this->_blAllowEmptyParentId = false;
2161 
2162  if ( $sParentId !== '' ) {
2163  $this->onChange( ACTION_UPDATE, null, $sParentId );
2164  }
2165  }
2166 
2167 
2174  public function getPictureGallery()
2175  {
2176  $myConfig = $this->getConfig();
2177 
2178  //initialize
2179  $blMorePic = false;
2180  $aArtPics = array();
2181  $aArtIcons = array();
2182  $iActPicId = 1;
2183  $sActPic = $this->getPictureUrl( $iActPicId );
2184 
2185  if ( oxConfig::getParameter( 'actpicid' ) ) {
2186  $iActPicId = oxConfig::getParameter('actpicid');
2187  }
2188 
2189  $oStr = getStr();
2190  $iCntr = 0;
2191  $iPicCount = $myConfig->getConfigParam( 'iPicCount' );
2192  $blCheckActivePicId = true;
2193 
2194  for ( $i = 1; $i <= $iPicCount; $i++) {
2195  $sPicVal = $this->getPictureUrl( $i );
2196  $sIcoVal = $this->getIconUrl( $i );
2197  if ( !$oStr->strstr($sIcoVal, 'nopic_ico.jpg') && !$oStr->strstr($sIcoVal, 'nopic.jpg') &&
2198  !$oStr->strstr($sPicVal, 'nopic_ico.jpg') && !$oStr->strstr($sPicVal, 'nopic.jpg') ) {
2199  if ($iCntr) {
2200  $blMorePic = true;
2201  }
2202  $aArtIcons[$i]= $sIcoVal;
2203  $aArtPics[$i]= $sPicVal;
2204  $iCntr++;
2205 
2206  if ($iActPicId == $i) {
2207  $sActPic = $sPicVal;
2208  $blCheckActivePicId = false;
2209  }
2210 
2211  } else if ( $blCheckActivePicId && $iActPicId <= $i) {
2212  // if picture is empty, setting active pic id to next
2213  // picture
2214  $iActPicId++;
2215  }
2216  }
2217 
2218  $blZoomPic = false;
2219  $aZoomPics = array();
2220  $iZoomPicCount = $myConfig->getConfigParam( 'iPicCount' );
2221 
2222  for ( $j = 1,$c = 1; $j <= $iZoomPicCount; $j++) {
2223  $sVal = $this->getZoomPictureUrl($j);
2224 
2225  if ( $sVal && !$oStr->strstr($sVal, 'nopic.jpg')) {
2226  $blZoomPic = true;
2227  $aZoomPics[$c]['id'] = $c;
2228  $aZoomPics[$c]['file'] = $sVal;
2229  //anything is better than empty name, because <img src=""> calls shop once more = x2 SLOW.
2230  if (!$sVal) {
2231  $aZoomPics[$c]['file'] = "nopic.jpg";
2232  }
2233  $c++;
2234  }
2235  }
2236 
2237  $aPicGallery = array('ActPicID' => $iActPicId,
2238  'ActPic' => $sActPic,
2239  'MorePics' => $blMorePic,
2240  'Pics' => $aArtPics,
2241  'Icons' => $aArtIcons,
2242  'ZoomPic' => $blZoomPic,
2243  'ZoomPics' => $aZoomPics);
2244 
2245  return $aPicGallery;
2246  }
2247 
2261  public function onChange($sAction = null, $sOXID = null, $sParentID = null)
2262  {
2263  $myConfig = $this->getConfig();
2264 
2265  if (!isset($sOXID)) {
2266  if ( $this->getId()) {
2267  $sOXID = $this->getId();
2268  }
2269  if (!isset ($sOXID)) {
2270  $sOXID = $this->oxarticles__oxid->value;
2271  }
2272  if ($this->oxarticles__oxparentid->value) {
2273  $sParentID = $this->oxarticles__oxparentid->value;
2274  }
2275  }
2276  if (!isset($sOXID)) {
2277  return;
2278  }
2279 
2280  //if (isset($sOXID) && !$myConfig->blVariantParentBuyable && $myConfig->blUseStock)
2281  if ( $myConfig->getConfigParam( 'blUseStock' ) ) {
2282  //if article has variants then updating oxvarstock field
2283  //getting parent id
2284  if (!isset($sParentID)) {
2285  $oDb = oxDb::getDb();
2286  $sQ = 'select oxparentid from oxarticles where oxid = '.$oDb->quote($sOXID);
2287  $sParentID = $oDb->getOne( $sQ );
2288  }
2289  //if we have parent id then update stock
2290  if ($sParentID) {
2291  $this->_onChangeUpdateStock($sParentID);
2292  }
2293  }
2294  //if we have parent id then update count
2295  //update count even if blUseStock is not active
2296  if ($sParentID) {
2297  $this->_onChangeUpdateVarCount($sParentID);
2298  }
2299 
2300  $sId = ( $sParentID ) ? $sParentID : $sOXID;
2301  $this->_setVarMinMaxPrice( $sId );
2302 
2303  $this->_updateParentDependFields();
2304 
2305  // resetting articles count cache if stock has changed and some
2306  // articles goes offline (M:1448)
2307  if ( $sAction === ACTION_UPDATE_STOCK ) {
2308  $this->_assignStock();
2309  $this->_onChangeStockResetCount( $sOXID );
2310  }
2311 
2312  }
2313 
2320  public function getCustomVAT()
2321  {
2322  if ( isset($this->oxarticles__oxvat->value) ) {
2323  return $this->oxarticles__oxvat->value;
2324  }
2325 
2326  return null;
2327  }
2328 
2337  public function checkForStock( $dAmount, $dArtStockAmount = 0 )
2338  {
2339  $myConfig = $this->getConfig();
2340  if ( !$myConfig->getConfigParam( 'blUseStock' ) ) {
2341  return true;
2342  }
2343 
2345  // fetching DB info as its up-to-date
2346  $sQ = 'select oxstock, oxstockflag from oxarticles where oxid = '.$oDb->quote( $this->getId() );
2347  $rs = $oDb->select( $sQ );
2348 
2349  $iOnStock = 0;
2350  $iStockFlag = 0;
2351  if ( $rs !== false && $rs->recordCount() > 0 ) {
2352  $iOnStock = $rs->fields['oxstock'] - $dArtStockAmount;
2353  $iStockFlag = $rs->fields['oxstockflag'];
2354 
2355  // foreign stock is also always considered as on stock
2356  if ( $iStockFlag == 1 || $iStockFlag == 4) {
2357  return true;
2358  }
2359  if ( !$myConfig->getConfigParam( 'blAllowUnevenAmounts' ) ) {
2360  $iOnStock = floor( $iOnStock );
2361  }
2362  }
2363  if ($this->getConfig()->getConfigParam( 'blPsBasketReservationEnabled' )) {
2364  $iOnStock += $this->getSession()->getBasketReservations()->getReservedAmount($this->getId());
2365  }
2366  if ( $iOnStock >= $dAmount ) {
2367  return true;
2368  } else {
2369  if ( $iOnStock > 0 ) {
2370  return $iOnStock;
2371  } else {
2372  $oEx = oxNew( 'oxArticleInputException' );
2373  $oEx->setMessage( 'ERROR_MESSAGE_ARTICLE_ARTICLE_NOT_BUYABLE' );
2374  oxRegistry::get("oxUtilsView")->addErrorToDisplay( $oEx );
2375  return false;
2376  }
2377  }
2378  }
2379 
2380 
2386  public function getLongDescription()
2387  {
2388  if ( $this->_oLongDesc === null ) {
2389  // initializing
2390  $this->_oLongDesc = new oxField();
2391 
2392 
2393  // choosing which to get..
2394  $sOxid = $this->getId();
2395  $sViewName = getViewName( 'oxartextends', $this->getLanguage() );
2396 
2397  $oDb = oxDb::getDb();
2398  $sDbValue = $oDb->getOne( "select oxlongdesc from {$sViewName} where oxid = " . $oDb->quote( $sOxid ) );
2399 
2400  if ( $sDbValue != false ) {
2401  $this->_oLongDesc->setValue( $sDbValue, oxField::T_RAW );
2402  } elseif ( $this->oxarticles__oxparentid->value ) {
2403  if ( !$this->isAdmin() || $this->_blLoadParentData ) {
2404  $this->_oLongDesc->setValue( $this->getParentArticle()->getLongDescription()->getRawValue(), oxField::T_RAW );
2405  }
2406  }
2407  }
2408  return $this->_oLongDesc;
2409  }
2410 
2417  public function getLongDesc()
2418  {
2419  return oxRegistry::get("oxUtilsView")->parseThroughSmarty( $this->getLongDescription()->getRawValue(), $this->getId().$this->getLanguage(), null, true );
2420  }
2421 
2429  public function setArticleLongDesc( $sDesc )
2430  {
2431 
2432  // setting current value
2433  $this->_oLongDesc = new oxField( $sDesc, oxField::T_RAW );
2434  $this->oxarticles__oxlongdesc = new oxField( $sDesc, oxField::T_RAW );
2435 
2436  return null;
2437  }
2438 
2444  public function getAttributes()
2445  {
2446  if ( $this->_oAttributeList === null ) {
2447  $this->_oAttributeList = oxNew( 'oxattributelist' );
2448  $this->_oAttributeList->loadAttributes( $this->getId(), $this->getParentId() );
2449  }
2450 
2451  return $this->_oAttributeList;
2452  }
2453 
2460  {
2461  if ( $this->_oAttributeList === null ) {
2462  $this->_oAttributeList = oxNew( 'oxattributelist' );
2463  $this->_oAttributeList->loadAttributesDisplayableInBasket( $this->getId(), $this->getParentId() );
2464  }
2465 
2466  return $this->_oAttributeList;
2467  }
2468 
2469 
2478  public function appendLink( $sAddParams, $iLang = null )
2479  {
2480  if ( $sAddParams ) {
2481  if ( $iLang === null ) {
2482  $iLang = $this->getLanguage();
2483  }
2484 
2485  $this->_aSeoAddParams[$iLang] = isset( $this->_aSeoAddParams[$iLang] ) ? $this->_aSeoAddParams[$iLang] . "&amp;" : "";
2486  $this->_aSeoAddParams[$iLang] .= $sAddParams;
2487  }
2488  }
2489 
2498  public function getBaseSeoLink( $iLang, $blMain = false )
2499  {
2500  $oEncoder = oxRegistry::get("oxSeoEncoderArticle");
2501  if ( !$blMain ) {
2502  return $oEncoder->getArticleUrl( $this, $iLang, $this->getLinkType() );
2503  }
2504  return $oEncoder->getArticleMainUrl( $this, $iLang );
2505  }
2506 
2515  public function getLink( $iLang = null, $blMain = false )
2516  {
2517  if ( !oxRegistry::getUtils()->seoIsActive() ) {
2518  return $this->getStdLink( $iLang );
2519  }
2520 
2521  if ( $iLang === null ) {
2522  $iLang = $this->getLanguage();
2523  }
2524 
2525  $iLinkType = $this->getLinkType();
2526  if ( !isset( $this->_aSeoUrls[$iLang][$iLinkType] ) ) {
2527  $this->_aSeoUrls[$iLang][$iLinkType] = $this->getBaseSeoLink( $iLang, $blMain );
2528  }
2529 
2530  $sUrl = $this->_aSeoUrls[$iLang][$iLinkType];
2531  if ( isset($this->_aSeoAddParams[$iLang])) {
2532  $sUrl .= ( ( strpos( $sUrl.$this->_aSeoAddParams[$iLang], '?' ) === false ) ? '?' : '&amp;' ).$this->_aSeoAddParams[$iLang];
2533  }
2534 
2535  return $sUrl;
2536  }
2537 
2546  public function getMainLink( $iLang = null )
2547  {
2548  return $this->getLink( $iLang, true );
2549  }
2550 
2558  public function setLinkType( $iType )
2559  {
2560  // resetting details link, to force new
2561  $this->_sDetailLink = null;
2562 
2563  // setting link type
2564  $this->_iLinkType = (int) $iType;
2565  }
2566 
2572  public function getLinkType()
2573  {
2574  return $this->_iLinkType;
2575  }
2576 
2585  public function appendStdLink( $sAddParams, $iLang = null )
2586  {
2587  if ( $sAddParams ) {
2588  if ( $iLang === null ) {
2589  $iLang = $this->getLanguage();
2590  }
2591 
2592  $this->_aStdAddParams[$iLang] = isset( $this->_aStdAddParams[$iLang] ) ? $this->_aStdAddParams[$iLang] . "&amp;" : "";
2593  $this->_aStdAddParams[$iLang] .= $sAddParams;
2594  }
2595  }
2596 
2606  public function getBaseStdLink( $iLang, $blAddId = true, $blFull = true )
2607  {
2608  $sUrl = '';
2609  if ( $blFull ) {
2610  //always returns shop url, not admin
2611  $sUrl = $this->getConfig()->getShopUrl( $iLang, false );
2612  }
2613 
2614  $sUrl .= 'index.php?cl=details' . ( $blAddId ? '&amp;anid=' . urlencode($this->getId()) : '' );
2615  return $sUrl . ( isset( $this->_aStdAddParams[$iLang] ) ? '&amp;' . $this->_aStdAddParams[$iLang] : '' );
2616  }
2617 
2626  public function getStdLink( $iLang = null, $aParams = array() )
2627  {
2628  if ( $iLang === null ) {
2629  $iLang = $this->getLanguage();
2630  }
2631 
2632  if ( !isset( $this->_aStdUrls[$iLang] ) ) {
2633  $this->_aStdUrls[$iLang] = $this->getBaseStdLink( $iLang );
2634  }
2635 
2636  return oxRegistry::get("oxUtilsUrl")->processUrl( $this->_aStdUrls[$iLang], true, $aParams, $iLang );
2637  }
2638 
2648  public function getStdTagLink( $sTag )
2649  {
2650  $oArticleTags = oxNew('oxarticletaglist');
2651  $oArticleTags->setArticleId( $this->getId() );
2652  return $oArticleTags->getStdTagLink($sTag);
2653  }
2654 
2662  public function getTags()
2663  {
2664  $oArticleTags = oxNew('oxarticletaglist');
2665  $oArticleTags->load( $this->getId() );
2666  return $oArticleTags->get()->__toString();
2667  }
2668 
2678  public function saveTags($sTags)
2679  {
2680  //do not allow derived update
2681  if ( !$this->allowDerivedUpdate() ) {
2682  return false;
2683  }
2684  $oArticleTags = oxNew('oxarticletaglist');
2685  $oArticleTags->setArticleId( $this->getId() );
2686  $oArticleTags->set( $sTags );
2687  $oArticleTags->save();
2688  }
2689 
2699  public function addTag($sTag)
2700  {
2701  $oArticleTags = oxNew('oxarticletaglist');
2702  $oArticleTags->load( $this->getId() );
2703  $oArticleTags->addTag( $sTag );
2704  if ( $oArticleTags->save() ) {
2705  return true;
2706  }
2707  return false;
2708  }
2709 
2715  public function getMediaUrls()
2716  {
2717  if ( $this->_aMediaUrls === null ) {
2718  $this->_aMediaUrls = oxNew("oxlist");
2719  $this->_aMediaUrls->init("oxmediaurl");
2720  $this->_aMediaUrls->getBaseObject()->setLanguage( $this->getLanguage() );
2721 
2722  $sViewName = getViewName( "oxmediaurls", $this->getLanguage() );
2723  $sQ = "select * from {$sViewName} where oxobjectid = '".$this->getId()."'";
2724  $this->_aMediaUrls->selectString($sQ);
2725  }
2726  return $this->_aMediaUrls;
2727  }
2728 
2734  public function getDynImageDir()
2735  {
2736  return $this->_sDynImageDir;
2737  }
2738 
2744  public function getDispSelList()
2745  {
2746  if ($this->_aDispSelList === null) {
2747  if ( $this->getConfig()->getConfigParam( 'bl_perfLoadSelectLists' ) && $this->getConfig()->getConfigParam( 'bl_perfLoadSelectListsInAList' ) ) {
2748  $this->_aDispSelList = $this->getSelectLists();
2749  }
2750  }
2751  return $this->_aDispSelList;
2752  }
2753 
2759  public function getMoreDetailLink()
2760  {
2761  if ( $this->_sMoreDetailLink == null ) {
2762 
2763  // and assign special article values
2764  $this->_sMoreDetailLink = $this->getConfig()->getShopHomeURL() . 'cl=moredetails';
2765 
2766  // not always it is okey, as not all the time active category is the same as primary article cat.
2767  if ( $sActCat = oxConfig::getParameter( 'cnid' ) ) {
2768  $this->_sMoreDetailLink .= '&amp;cnid='.$sActCat;
2769  }
2770  $this->_sMoreDetailLink .= '&amp;anid='.$this->getId();
2771  $this->_sMoreDetailLink = $this->_sMoreDetailLink;
2772  }
2773 
2774  return $this->_sMoreDetailLink;
2775  }
2776 
2782  public function getToBasketLink()
2783  {
2784  if ( $this->_sToBasketLink == null ) {
2785  $myConfig = $this->getConfig();
2786 
2787  if ( oxRegistry::getUtils()->isSearchEngine() ) {
2788  $this->_sToBasketLink = $this->getLink();
2789  } else {
2790  // and assign special article values
2791  $this->_sToBasketLink = $myConfig->getShopHomeURL();
2792 
2793  // override some classes as these should never showup
2794  $sActClass = oxConfig::getParameter( 'cl' );
2795  if ( $sActClass == 'thankyou') {
2796  $sActClass = 'basket';
2797  }
2798  $this->_sToBasketLink .= 'cl='.$sActClass;
2799 
2800  // this is not very correct
2801  if ( $sActCat = oxConfig::getParameter( 'cnid' ) ) {
2802  $this->_sToBasketLink .= '&amp;cnid='.$sActCat;
2803  }
2804 
2805  $this->_sToBasketLink .= '&amp;fnc=tobasket&amp;aid='.$this->getId().'&amp;anid='.$this->getId();
2806 
2807  if ( $sTpl = basename( oxConfig::getParameter( 'tpl' ) ) ) {
2808  $this->_sToBasketLink .= '&amp;tpl='.$sTpl;
2809  }
2810  }
2811  }
2812 
2813  return $this->_sToBasketLink;
2814  }
2815 
2821  public function getStockStatus()
2822  {
2823  return $this->_iStockStatus;
2824  }
2825 
2831  protected function _isStockStatusChanged()
2832  {
2833  return $this->_iStockStatus != $this->_iStockStatusOnLoad;
2834  }
2835 
2841  protected function _isVisibilityChanged()
2842  {
2843  return $this->_isStockStatusChanged() && ($this->_iStockStatus == -1 || $this->_iStockStatusOnLoad == -1);
2844  }
2845 
2851  public function getDeliveryDate()
2852  {
2853  if ( $this->oxarticles__oxdelivery->value != '0000-00-00') {
2854  return oxRegistry::get("oxUtilsDate")->formatDBDate( $this->oxarticles__oxdelivery->value);
2855  }
2856  return false;
2857  }
2858 
2866  public function getFTPrice()
2867  {
2868  // module
2869  if ( $oPrice = $this->getTPrice() ) {
2870  if ( $dPrice = $this->_getPriceForView( $oPrice ) ) {
2871  return oxRegistry::getLang()->formatCurrency( $dPrice );
2872  }
2873  }
2874  }
2875 
2883  public function getFPrice()
2884  {
2885  if ( $oPrice = $this->getPrice() ) {
2886  $dPrice = $this->_getPriceForView( $oPrice );
2887  return oxRegistry::getLang()->formatCurrency( $dPrice );
2888  }
2889  }
2890 
2897  public function resetRemindStatus()
2898  {
2899  if ( $this->oxarticles__oxremindactive->value == 2 &&
2900  $this->oxarticles__oxremindamount->value <= $this->oxarticles__oxstock->value ) {
2901  $this->oxarticles__oxremindactive->value = 1;
2902  }
2903 
2904  return null;
2905  }
2906 
2914  public function getFNetPrice()
2915  {
2916  if ( $oPrice = $this->getPrice() ) {
2917  return oxRegistry::getLang()->formatCurrency( $oPrice->getNettoPrice() );
2918  }
2919  }
2920 
2928  public function getPricePerUnit()
2929  {
2930  return $this->getFUnitPrice();
2931  }
2932 
2938  public function isParentNotBuyable()
2939  {
2941  }
2942 
2948  public function isNotBuyable()
2949  {
2950  return $this->_blNotBuyable;
2951  }
2952 
2960  public function setBuyableState( $blBuyable = false )
2961  {
2962  $this->_blNotBuyable = !$blBuyable;
2963  }
2964 
2972  public function setSelectlist( $aSelList )
2973  {
2974  $this->_aDispSelList = $aSelList;
2975  }
2976 
2984  public function getPictureUrl( $iIndex = 1 )
2985  {
2986  if ( $iIndex ) {
2987  $sImgName = false;
2988  if ( !$this->_isFieldEmpty( "oxarticles__oxpic".$iIndex ) ) {
2989  $sImgName = basename( $this->{"oxarticles__oxpic$iIndex"}->value );
2990  }
2991 
2992  $sSize = $this->getConfig()->getConfigParam( 'aDetailImageSizes' );
2993  return oxRegistry::get("oxPictureHandler")->getProductPicUrl( "product/{$iIndex}/", $sImgName, $sSize, 'oxpic'.$iIndex );
2994  }
2995  }
2996 
3005  public function getIconUrl( $iIndex = 0 )
3006  {
3007  $sImgName = false;
3008  $sDirname = "product/1/";
3009  if ( $iIndex && !$this->_isFieldEmpty( "oxarticles__oxpic{$iIndex}" ) ) {
3010  $sImgName = basename( $this->{"oxarticles__oxpic$iIndex"}->value );
3011  $sDirname = "product/{$iIndex}/";
3012  } elseif ( !$this->_isFieldEmpty( "oxarticles__oxicon" ) ) {
3013  $sImgName = basename( $this->oxarticles__oxicon->value );
3014  $sDirname = "product/icon/";
3015  } elseif ( !$this->_isFieldEmpty( "oxarticles__oxpic1" ) ) {
3016  $sImgName = basename( $this->oxarticles__oxpic1->value );
3017  }
3018 
3019  $sSize = $this->getConfig()->getConfigParam( 'sIconsize' );
3020  return oxRegistry::get("oxPictureHandler")->getProductPicUrl( $sDirname, $sImgName, $sSize, $iIndex );
3021  }
3022 
3030  public function getThumbnailUrl( $bSsl = null )
3031  {
3032  $sImgName = false;
3033  $sDirname = "product/1/";
3034  if ( !$this->_isFieldEmpty( "oxarticles__oxthumb" ) ) {
3035  $sImgName = basename( $this->oxarticles__oxthumb->value );
3036  $sDirname = "product/thumb/";
3037  } elseif ( !$this->_isFieldEmpty( "oxarticles__oxpic1" ) ) {
3038  $sImgName = basename( $this->oxarticles__oxpic1->value );
3039  }
3040 
3041  $sSize = $this->getConfig()->getConfigParam( 'sThumbnailsize' );
3042  return oxRegistry::get("oxPictureHandler")->getProductPicUrl( $sDirname, $sImgName, $sSize, 0, $bSsl );
3043  }
3044 
3052  public function getZoomPictureUrl( $iIndex = '' )
3053  {
3054  $iIndex = (int) $iIndex;
3055  if ( $iIndex > 0 && !$this->_isFieldEmpty( "oxarticles__oxpic".$iIndex ) ) {
3056  $sImgName = basename( $this->{"oxarticles__oxpic".$iIndex}->value );
3057  $sSize = $this->getConfig()->getConfigParam( "sZoomImageSize" );
3058  return oxRegistry::get("oxPictureHandler")->getProductPicUrl( "product/{$iIndex}/", $sImgName, $sSize, 'oxpic'.$iIndex );
3059  }
3060  }
3061 
3069  public function getFileUrl()
3070  {
3071  return $this->getConfig()->getPictureUrl( 'media/' );
3072  }
3073 
3081  public function getPriceFromPrefix()
3082  {
3083  $sPricePrefix = '';
3084  if ( $this->_blIsRangePrice) {
3085  $sPricePrefix = oxLang::getInstance()->translateString('PRICE_FROM').' ';
3086  }
3087 
3088  return $sPricePrefix;
3089  }
3090 
3096  protected function _saveArtLongDesc()
3097  {
3098  $myConfig = $this->getConfig();
3099  $sShopId = $myConfig->getShopID();
3100  if (in_array("oxlongdesc", $this->_aSkipSaveFields)) {
3101  return;
3102  }
3103 
3104  if ($this->_blEmployMultilanguage) {
3105  $sValue = $this->getLongDescription()->getRawValue();
3106  if ( $sValue !== null ) {
3107  $oArtExt = oxNew('oxI18n');
3108  $oArtExt->init('oxartextends');
3109  $oArtExt->setLanguage((int) $this->getLanguage());
3110  if (!$oArtExt->load($this->getId())) {
3111  $oArtExt->setId($this->getId());
3112  }
3113  $oArtExt->oxartextends__oxlongdesc = new oxField($sValue, oxField::T_RAW);
3114  $oArtExt->save();
3115  }
3116  } else {
3117  $oArtExt = oxNew('oxI18n');
3118  $oArtExt->setEnableMultilang(false);
3119  $oArtExt->init('oxartextends');
3120  $aObjFields = $oArtExt->_getAllFields(true);
3121  if (!$oArtExt->load($this->getId())) {
3122  $oArtExt->setId($this->getId());
3123  }
3124 
3125  foreach ($aObjFields as $sKey => $sValue ) {
3126  if ( preg_match('/^oxlongdesc(_(\d{1,2}))?$/', $sKey) ) {
3127  $sField = $this->_getFieldLongName($sKey);
3128 
3129  if (isset($this->$sField)) {
3130  $sLongDesc = null;
3131  if ($this->$sField instanceof oxField) {
3132  $sLongDesc = $this->$sField->getRawValue();
3133  } elseif (is_object($this->$sField)) {
3134  $sLongDesc = $this->$sField->value;
3135  }
3136  if (isset($sLongDesc)) {
3137  $sAEField = $oArtExt->_getFieldLongName($sKey);
3138  $oArtExt->$sAEField = new oxField($sLongDesc, oxField::T_RAW);
3139  }
3140  }
3141  }
3142  }
3143  $oArtExt->save();
3144  }
3145  }
3146 
3152  protected function _skipSaveFields()
3153  {
3154  $myConfig = $this->getConfig();
3155 
3156  $this->_aSkipSaveFields = array();
3157 
3158  $this->_aSkipSaveFields[] = 'oxtimestamp';
3159  // $this->_aSkipSaveFields[] = 'oxlongdesc';
3160  $this->_aSkipSaveFields[] = 'oxinsert';
3161 
3162  if ( !$this->_blAllowEmptyParentId && (!isset( $this->oxarticles__oxparentid->value) || $this->oxarticles__oxparentid->value == '') ) {
3163  $this->_aSkipSaveFields[] = 'oxparentid';
3164  }
3165 
3166  }
3167 
3177  protected function _mergeDiscounts( $aDiscounts, $aItemDiscounts)
3178  {
3179  foreach ( $aItemDiscounts as $sKey => $oDiscount ) {
3180  // add prices of the same discounts
3181  if ( array_key_exists ($sKey, $aDiscounts) ) {
3182  $aDiscounts[$sKey]->dDiscount += $oDiscount->dDiscount;
3183  } else {
3184  $aDiscounts[$sKey] = $oDiscount;
3185  }
3186  }
3187  return $aDiscounts;
3188  }
3189 
3195  protected function _getGroupPrice()
3196  {
3197  $sPriceSufix = $this->_getUserPriceSufix();
3198  $sVarName = "oxarticles__oxprice{$sPriceSufix}";
3199  $dPrice = $this->$sVarName->value;
3200 
3201  // #1437/1436C - added config option, and check for zero A,B,C price values
3202  if ( $this->getConfig()->getConfigParam( 'blOverrideZeroABCPrices' ) && (double) $dPrice == 0 ) {
3203  $dPrice = $this->oxarticles__oxprice->value;
3204  }
3205 
3206  return $dPrice;
3207  }
3208 
3217  protected function _getAmountPrice($dAmount = 1)
3218  {
3219  startProfile( "_getAmountPrice" );
3220 
3221  $dPrice = $this->_getGroupPrice();
3222  $oAmtPrices = $this->_getAmountPriceList();
3223  foreach ($oAmtPrices as $oAmPrice) {
3224  if ($oAmPrice->oxprice2article__oxamount->value <= $dAmount
3225  && $dAmount <= $oAmPrice->oxprice2article__oxamountto->value
3226  && $dPrice > $oAmPrice->oxprice2article__oxaddabs->value ) {
3227  $dPrice = $oAmPrice->oxprice2article__oxaddabs->value;
3228  }
3229  }
3230 
3231  stopProfile( "_getAmountPrice" );
3232  return $dPrice;
3233  }
3234 
3243  protected function _modifySelectListPrice( $dPrice, $aChosenList = null )
3244  {
3245  $myConfig = $this->getConfig();
3246  // #690
3247  if ( $myConfig->getConfigParam( 'bl_perfLoadSelectLists' ) && $myConfig->getConfigParam( 'bl_perfUseSelectlistPrice' ) ) {
3248 
3249  $aSelLists = $this->getSelectLists();
3250 
3251  foreach ( $aSelLists as $key => $aSel) {
3252  if ( isset( $aChosenList[$key]) && isset($aSel[$aChosenList[$key]] ) ) {
3253  $oSel = $aSel[$aChosenList[$key]];
3254  if ( $oSel->priceUnit =='abs' ) {
3255  $dPrice += $oSel->price;
3256  } elseif ( $oSel->priceUnit =='%' ) {
3257  $dPrice += oxPrice::percent( $dPrice, $oSel->price );
3258  }
3259  }
3260  }
3261  }
3262  return $dPrice;
3263  }
3264 
3265 
3273  protected function _fillAmountPriceList($aAmPriceList)
3274  {
3275  $oLang = oxRegistry::getLang();
3276 
3277  // trying to find lowest price value
3278  foreach ($aAmPriceList as $sId => $oItem) {
3279 
3280  $oItemPrice = $this->_getPriceObject();
3281  if ( $oItem->oxprice2article__oxaddabs->value ) {
3282 
3283  $dBasePrice = $oItem->oxprice2article__oxaddabs->value;
3284  $dBasePrice = $this->_preparePrice( $dBasePrice, $this->getArticleVat() );
3285 
3286  $oItemPrice->setPrice( $dBasePrice );
3287  $this->_calculatePrice( $oItemPrice );
3288 
3289  } else {
3290  $dBasePrice = $this->_getGroupPrice();
3291  $dBasePrice = $this->_preparePrice( $dBasePrice, $this->getArticleVat() );
3292  $oItemPrice->setPrice( $dBasePrice );
3293  $oItemPrice->subtractPercent( $oItem->oxprice2article__oxaddperc->value );
3294  }
3295 
3296 
3297  $aAmPriceList[$sId]->fbrutprice = $oLang->formatCurrency( $this->_getPriceForView( $oItemPrice ) );
3298  }
3299 
3300  return $aAmPriceList;
3301  }
3302 
3303 
3309  protected function _getVariantsIds()
3310  {
3311  $aSelect = array();
3312  if ( ( $sId = $this->getId() ) ) {
3314  $sQ = "select oxid from " . $this->getViewName( true ) . " where oxparentid = ".$oDb->quote( $sId )." and " .
3315  $this->getSqlActiveSnippet( true ) . " order by oxsort";
3316  $oRs = $oDb->select( $sQ );
3317  if ( $oRs != false && $oRs->recordCount() > 0 ) {
3318  while (!$oRs->EOF) {
3319  $aSelect[] = reset( $oRs->fields );
3320  $oRs->moveNext();
3321  }
3322  }
3323  }
3324  return $aSelect;
3325  }
3326 
3332  public function getArticleVat()
3333  {
3334  if (!isset($this->_dArticleVat)) {
3335  $this->_dArticleVat = oxRegistry::get("oxVatSelector")->getArticleVat( $this );
3336  }
3337  return $this->_dArticleVat;
3338  }
3339 
3348  protected function _applyVAT( oxPrice $oPrice, $dVat )
3349  {
3350  startProfile(__FUNCTION__);
3351  $oPrice->setVAT( $dVat );
3353  $oVatSelector = oxRegistry::get("oxVatSelector");
3354  if ( ($dVat = $oVatSelector->getArticleUserVat($this)) !== false ) {
3355  $oPrice->setUserVat( $dVat );
3356  }
3357  stopProfile(__FUNCTION__);
3358  }
3359 
3367  public function applyVats( oxPrice $oPrice )
3368  {
3369  $this->_applyVAT($oPrice, $this->getArticleVat() );
3370  }
3371 
3379  public function applyDiscountsForVariant( $oPrice )
3380  {
3381  // apply discounts
3382  if ( !$this->skipDiscounts() ) {
3383  $oDiscountList = oxRegistry::get("oxDiscountList");
3384  $aDiscounts = $oDiscountList->getArticleDiscounts( $this, $this->getArticleUser() );
3385 
3386  reset( $aDiscounts );
3387  foreach ( $aDiscounts as $oDiscount ) {
3388  $oPrice->setDiscount($oDiscount->getAddSum(), $oDiscount->getAddSumType());
3389  }
3390  $oPrice->calculateDiscount();
3391  }
3392  }
3393 
3402  protected function _applyCurrency(oxPrice $oPrice, $oCur = null )
3403  {
3404  if ( !$oCur ) {
3405  $oCur = $this->getConfig()->getActShopCurrencyObject();
3406  }
3407 
3408  $oPrice->multiply($oCur->rate);
3409  }
3410 
3411 
3420  protected function _getAttribsString(&$sAttribs, &$iCnt)
3421  {
3422  // we do not use lists here as we dont need this overhead right now
3424  $sSelect = 'select oxattrid from oxobject2attribute where oxobject2attribute.oxobjectid='.$oDb->quote( $this->getId() );
3425  $sAttribs = '';
3426  $blSep = false;
3427  $rs = $oDb->select( $sSelect);
3428  $iCnt = 0;
3429  if ($rs != false && $rs->recordCount() > 0) {
3430  while (!$rs->EOF) {
3431  if ( $blSep) {
3432  $sAttribs .= ' or ';
3433  }
3434  $sAttribs .= 't1.oxattrid = '.$oDb->quote($rs->fields['oxattrid']).' ';
3435  $blSep = true;
3436  $iCnt++;
3437  $rs->moveNext();
3438  }
3439  }
3440  }
3441 
3450  protected function _getSimList($sAttribs, $iCnt)
3451  {
3452  $myConfig = $this->getConfig();
3454 
3455  // #523A
3456  $iAttrPercent = $myConfig->getConfigParam( 'iAttributesPercent' )/100;
3457  // 70% same attributes
3458  if ( !$iAttrPercent || $iAttrPercent < 0 || $iAttrPercent > 1) {
3459  $iAttrPercent = 0.70;
3460  }
3461  // #1137V iAttributesPercent = 100 doesnt work
3462  $iHitMin = ceil( $iCnt * $iAttrPercent );
3463 
3464  // we do not use lists here as we don't need this overhead right now
3465  $aList= array();
3466  $sSelect = "select oxobjectid, count(*) as cnt from oxobject2attribute as t1 where
3467  ( $sAttribs )
3468  and t1.oxobjectid != ".$oDb->quote( $this->oxarticles__oxid->value )."
3469  group by t1.oxobjectid having count(*) >= $iHitMin ";
3470 
3471  $rs = $oDb->selectLimit( $sSelect, 20, 0 );
3472  if ($rs != false && $rs->recordCount() > 0) {
3473  while (!$rs->EOF) {
3474  $oTemp = new stdClass(); // #663
3475  $oTemp->cnt = $rs->fields['cnt'];
3476  $oTemp->id = $rs->fields['oxobjectid'];
3477  $aList[] = $oTemp;
3478  $rs->moveNext();
3479  }
3480  }
3481  return $aList;
3482  }
3483 
3492  protected function _generateSimListSearchStr($sArticleTable, $aList)
3493  {
3494  $myConfig = $this->getConfig();
3495  $sFieldList = $this->getSelectFields();
3496  $sSearch = "select $sFieldList from $sArticleTable where ".$this->getSqlActiveSnippet()." and $sArticleTable.oxissearch = 1 and $sArticleTable.oxid in ( ";
3497  $blSep = false;
3498  $iCnt = 0;
3499  $oDb = oxDb::getDb();
3500  foreach ( $aList as $oTemp) {
3501  if ( $blSep) {
3502  $sSearch .= ',';
3503  }
3504  $sSearch .= $oDb->quote($oTemp->id);
3505  $blSep = true;
3506  if ( $iCnt >= $myConfig->getConfigParam( 'iNrofSimilarArticles' ) ) {
3507  break;
3508  }
3509  $iCnt++;
3510  }
3511 
3512  //#1741T
3513  //$sSearch .= ") and $sArticleTable.oxparentid = '' ";
3514  $sSearch .= ') ';
3515 
3516  // #524A -- randomizing articles in attribute list
3517  $sSearch .= ' order by rand() ';
3518 
3519  return $sSearch;
3520  }
3521 
3530  protected function _generateSearchStr($sOXID, $blSearchPriceCat = false )
3531  {
3532 
3533  $sCatView = getViewName( 'oxcategories', $this->getLanguage() );
3534  $sO2CView = getViewName( 'oxobject2category' );
3535 
3536  // we do not use lists here as we don't need this overhead right now
3537  if ( !$blSearchPriceCat ) {
3538  $sSelect = "select {$sCatView}.* from {$sO2CView} as oxobject2category left join {$sCatView} on
3539  {$sCatView}.oxid = oxobject2category.oxcatnid
3540  where oxobject2category.oxobjectid=".oxDb::getDb()->quote($sOXID)." and {$sCatView}.oxid is not null ";
3541  } else {
3542  $sSelect = "select {$sCatView}.* from {$sCatView} where
3543  '{$this->oxarticles__oxprice->value}' >= {$sCatView}.oxpricefrom and
3544  '{$this->oxarticles__oxprice->value}' <= {$sCatView}.oxpriceto ";
3545  }
3546  return $sSelect;
3547  }
3548 
3555  {
3556  $sArtTable = $this->getViewName();
3557  $sOrderArtTable = getViewName( 'oxorderarticles' );
3558 
3559  // fetching filter params
3560  $sIn = " '{$this->oxarticles__oxid->value}' ";
3561  if ( $this->oxarticles__oxparentid->value ) {
3562 
3563  // adding article parent
3564  $sIn .= ", '{$this->oxarticles__oxparentid->value}' ";
3565  $sParentIdForVariants = $this->oxarticles__oxparentid->value;
3566 
3567  } else {
3568  $sParentIdForVariants = $this->getId();
3569  }
3570 
3571  // adding variants
3573  $oRs = $oDb->select( "select oxid from {$sArtTable} where oxparentid = ".$oDb->quote($sParentIdForVariants)." and oxid != ".$oDb->quote($this->oxarticles__oxid->value) );
3574  if ( $oRs != false && $oRs->recordCount() > 0) {
3575  while ( !$oRs->EOF ) {
3576  $sIn .= ", ".$oDb->quote(current( $oRs->fields ))." ";
3577  $oRs->moveNext();
3578  }
3579  }
3580 
3581  $iLimit = (int) $this->getConfig()->getConfigParam( 'iNrofCustomerWhoArticles' );
3582  $iLimit = $iLimit?( $iLimit * 10 ): 50;
3583 
3584  // building sql (optimized)
3585  $sQ = "select distinct {$sArtTable}.* from (
3586  select d.oxorderid as suborderid from {$sOrderArtTable} as d use index ( oxartid ) where d.oxartid in ( {$sIn} ) limit {$iLimit}
3587  ) as suborder
3588  left join {$sOrderArtTable} force index ( oxorderid ) on suborder.suborderid = {$sOrderArtTable}.oxorderid
3589  left join {$sArtTable} on {$sArtTable}.oxid = {$sOrderArtTable}.oxartid
3590  where {$sArtTable}.oxid not in ( {$sIn} )
3591  and ( {$sArtTable}.oxissearch = 1 or {$sArtTable}.oxparentid <> '' ) and ".$this->getSqlActiveSnippet();
3592 
3593  /* non optimized, but could be used if index forcing is not supported
3594  // building sql
3595  $sQ = "select distinct {$sArtTable}.* from {$sOrderArtTable}, {$sArtTable} where {$sOrderArtTable}.oxorderid in (
3596  select {$sOrderArtTable}.oxorderid from {$sOrderArtTable} where {$sOrderArtTable}.oxartid in ( {$sIn} )
3597  ) and {$sArtTable}.oxid = {$sOrderArtTable}.oxartid and {$sArtTable}.oxid not in ( {$sIn} )
3598  and ( {$sArtTable}.oxissearch = 1 or {$sArtTable}.oxparentid <> '' )
3599  and ".$this->getSqlActiveSnippet();
3600  */
3601 
3602  return $sQ;
3603  }
3604 
3614  protected function _generateSelectCatStr($sOXID, $sCatId, $dPriceFromTo = false)
3615  {
3616  $sCategoryView = getViewName('oxcategories');
3617  $sO2CView = getViewName('oxobject2category');
3618 
3619  $oDb = oxDb::getDb();
3620  $sOXID = $oDb->quote($sOXID);
3621  $sCatId = $oDb->quote($sCatId);
3622 
3623  if (!$dPriceFromTo) {
3624  $sSelect = "select oxobject2category.oxcatnid from $sO2CView as oxobject2category ";
3625  $sSelect .= "left join $sCategoryView as oxcategories on oxcategories.oxid = oxobject2category.oxcatnid ";
3626  $sSelect .= "where oxobject2category.oxcatnid=$sCatId and oxobject2category.oxobjectid=$sOXID ";
3627  $sSelect .= "and oxcategories.oxactive = 1 order by oxobject2category.oxtime ";
3628  } else {
3629  $dPriceFromTo = $oDb->quote($dPriceFromTo);
3630  $sSelect = "select oxcategories.oxid from $sCategoryView as oxcategories where ";
3631  $sSelect .= "oxcategories.oxid=$sCatId and $dPriceFromTo >= oxcategories.oxpricefrom and ";
3632  $sSelect .= "$dPriceFromTo <= oxcategories.oxpriceto ";
3633  }
3634  return $sSelect;
3635  }
3636 
3642  protected function _getAmountPriceList()
3643  {
3644  if ( $this->_oAmountPriceList === null ) {
3645  $oAmPriceList = oxNew( 'oxAmountPricelist' );
3646 
3647  if ( !$this->skipDiscounts() ) {
3648  //collecting assigned to article amount-price list
3649  $oAmPriceList->load( $this );
3650 
3651  // prepare abs prices if currently having percentages
3652  $oBasePrice = $this->_getGroupPrice();
3653  foreach ( $oAmPriceList as $oAmPrice ) {
3654  if ( $oAmPrice->oxprice2article__oxaddperc->value ) {
3655  $oAmPrice->oxprice2article__oxaddabs = new oxField(oxPrice::percent( $oBasePrice, 100 - $oAmPrice->oxprice2article__oxaddperc->value ), oxField::T_RAW );
3656  }
3657  }
3658 
3659  }
3660 
3661  $this->_oAmountPriceList = $oAmPriceList;
3662  }
3663 
3664  return $this->_oAmountPriceList;
3665  }
3666 
3674  protected function _isFieldEmpty( $sFieldName )
3675  {
3676  $mValue = $this->$sFieldName->value;
3677 
3678  if ( is_null( $mValue ) ) {
3679  return true;
3680  }
3681 
3682  if ( $mValue === '' ) {
3683  return true;
3684  }
3685 
3686  // certain fields with zero value treat as empty
3687  $aZeroValueFields = array('oxarticles__oxprice', 'oxarticles__oxvat', 'oxarticles__oxunitquantity');
3688 
3689  if (!$mValue && in_array( $sFieldName, $aZeroValueFields ) ) {
3690  return true;
3691  }
3692 
3693 
3694  if (!strcmp($mValue, '0000-00-00 00:00:00') || !strcmp($mValue, '0000-00-00')) {
3695  return true;
3696  }
3697 
3698  $sFieldName = strtolower($sFieldName);
3699 
3700  if ( $sFieldName == 'oxarticles__oxicon' && ( strpos($mValue, "nopic_ico.jpg") !== false || strpos($mValue, "nopic.jpg") !== false ) ) {
3701  return true;
3702  }
3703 
3704  if ( strpos($mValue, "nopic.jpg") !== false && ($sFieldName == 'oxarticles__oxthumb' || substr($sFieldName, 0, 17) == 'oxarticles__oxpic' || substr($sFieldName, 0, 18) == 'oxarticles__oxzoom') ) {
3705  return true;
3706  }
3707 
3708  return false;
3709  }
3710 
3718  protected function _isImageField($sFieldName)
3719  {
3720  $blIsImageField = ( stristr($sFieldName, '_oxthumb') || stristr($sFieldName, '_oxicon') || stristr($sFieldName, '_oxzoom') || stristr($sFieldName, '_oxpic') );
3721  return $blIsImageField;
3722  }
3723 
3731  protected function _assignParentFieldValue($sFieldName)
3732  {
3733  if (!($oParentArticle = $this->getParentArticle())) {
3734  return;
3735  }
3736 
3737  $sCopyFieldName = $this->_getFieldLongName($sFieldName);
3738 
3739  // assigning only these which parent article has
3740  if ( $oParentArticle->$sCopyFieldName != null ) {
3741 
3742  // only overwrite database values
3743  if ( substr( $sCopyFieldName, 0, 12) != 'oxarticles__') {
3744  return;
3745  }
3746 
3747  //do not copy certain fields
3748  if (in_array($sCopyFieldName, $this->_aNonCopyParentFields)) {
3749  return;
3750  }
3751 
3752  //skip picture parent value assignment in case master image is set for variant
3753  if ($this->_isFieldEmpty($sCopyFieldName) && $this->_isImageField($sCopyFieldName) && $this->_hasMasterImage( 1 )) {
3754  return;
3755  }
3756 
3757  //COPY THE VALUE
3758  if ( $this->_isFieldEmpty($sCopyFieldName) ) {
3759  $this->$sCopyFieldName = clone $oParentArticle->$sCopyFieldName;
3760  }
3761  }
3762  }
3763 
3769  public function getParentArticle()
3770  {
3771  if ( ( $sParentId = $this->oxarticles__oxparentid->value ) ) {
3772  $sIndex = $sParentId . "_" . $this->getLanguage();
3773  if ( !isset( self::$_aLoadedParents[$sIndex] ) ) {
3774  self::$_aLoadedParents[$sIndex] = oxNew( 'oxarticle' );
3775  self::$_aLoadedParents[$sIndex]->_blLoadPrice = false;
3776  self::$_aLoadedParents[$sIndex]->_blLoadVariants = false;
3777  self::$_aLoadedParents[$sIndex]->loadInLang( $this->getLanguage(), $sParentId );
3778  }
3779  return self::$_aLoadedParents[$sIndex];
3780  }
3781  }
3782 
3788  protected function _assignParentFieldValues()
3789  {
3790  startProfile('articleAssignParentInternal');
3791  if ( $this->oxarticles__oxparentid->value ) {
3792  // yes, we are in fact a variant
3793  if ( !$this->isAdmin() || ( $this->_blLoadParentData && $this->isAdmin() ) ) {
3794  foreach ( $this->_aFieldNames as $sFieldName => $sVal ) {
3795  $this->_assignParentFieldValue( $sFieldName );
3796  }
3797  }
3798  }
3799  stopProfile('articleAssignParentInternal');
3800  }
3801 
3807  protected function _assignNotBuyableParent()
3808  {
3809  if ( !$this->getConfig()->getConfigParam( 'blVariantParentBuyable' ) &&
3810  ($this->_blHasVariants || $this->oxarticles__oxvarstock->value || $this->oxarticles__oxvarcount->value )) {
3811  $this->_blNotBuyableParent = true;
3812 
3813  }
3814  }
3815 
3821  protected function _assignStock()
3822  {
3823  $myConfig = $this->getConfig();
3824  // -----------------------------------
3825  // stock
3826  // -----------------------------------
3827 
3828  // #1125 A. must round (using floor()) value taken from database and cast to int
3829  if (!$myConfig->getConfigParam( 'blAllowUnevenAmounts' ) && !$this->isAdmin() ) {
3830  $this->oxarticles__oxstock = new oxField((int) floor($this->oxarticles__oxstock->value));
3831  }
3832  //GREEN light
3833  $this->_iStockStatus = 0;
3834 
3835  // if we have flag /*1 or*/ 4 - we show always green light
3836  if ( $myConfig->getConfigParam( 'blUseStock' ) && /*$this->oxarticles__oxstockflag->value != 1 && */ $this->oxarticles__oxstockflag->value != 4) {
3837  //ORANGE light
3838  $iStock = $this->oxarticles__oxstock->value;
3839 
3840  if ($this->_blNotBuyableParent) {
3841  $iStock = $this->oxarticles__oxvarstock->value;
3842  }
3843 
3844 
3845  if ( $iStock <= $myConfig->getConfigParam( 'sStockWarningLimit' ) && $iStock > 0) {
3846  $this->_iStockStatus = 1;
3847  }
3848 
3849  //RED light
3850  if ($iStock <= 0) {
3851  $this->_iStockStatus = -1;
3852  }
3853  }
3854 
3855 
3856  // stock
3857  if ( $myConfig->getConfigParam( 'blUseStock' ) && ($this->oxarticles__oxstockflag->value == 3 || $this->oxarticles__oxstockflag->value == 2)) {
3858  $iOnStock = $this->oxarticles__oxstock->value;
3859  if ($this->getConfig()->getConfigParam( 'blPsBasketReservationEnabled' )) {
3860  $iOnStock += $this->getSession()->getBasketReservations()->getReservedAmount($this->getId());
3861  }
3862  if ($iOnStock <= 0) {
3863  $this->setBuyableState( false );
3864  }
3865  }
3866 
3867  //exceptional handling for variant parent stock:
3868  if ($this->_blNotBuyable && $this->oxarticles__oxvarstock->value ) {
3869  $this->setBuyableState( true );
3870  //but then at least setting notBuaybleParent to true
3871  $this->_blNotBuyableParent = true;
3872  }
3873 
3874  //special treatment for lists when blVariantParentBuyable config option is set to false
3875  //then we just hide "to basket" button.
3876  //if variants are not loaded in the list and this article has variants and parent is not buyable then this article is not buyable
3877  if ( !$myConfig->getConfigParam( 'blVariantParentBuyable' ) && !$myConfig->getConfigParam( 'blLoadVariants' ) && $this->oxarticles__oxvarstock->value) {
3878  $this->setBuyableState( false );
3879  }
3880 
3881  //setting to non buyable when variant list is empty (for example not loaded or inactive) and $this is non buyable parent
3882  if (!$this->_blNotBuyable && $this->_blNotBuyableParent && $this->oxarticles__oxvarcount->value == 0) {
3883  $this->setBuyableState( false );
3884  }
3885  }
3886 
3894  protected function _assignPrices()
3895  {
3896  $myConfig = $this->getConfig();
3897 
3898  // Performance
3899  if ( !$myConfig->getConfigParam( 'bl_perfLoadPrice' ) || !$this->_blLoadPrice ) {
3900  return;
3901  }
3902 
3903  //getting min and max prices of variants
3904  if ( $this->_hasAnyVariant() ) {
3905  $this->_applyRangePrice();
3906  }
3907  }
3908 
3914  protected function _assignPersistentParam()
3915  {
3916  // Persistent Parameter Handling
3917  $aPersParam = oxSession::getVar( 'persparam');
3918  if ( isset( $aPersParam) && isset( $aPersParam[$this->getId()])) {
3919  $this->_aPersistParam = $aPersParam[$this->getId()];
3920  }
3921  }
3922 
3928  protected function _assignDynImageDir()
3929  {
3930  $myConfig = $this->getConfig();
3931 
3932  $sThisShop = $this->oxarticles__oxshopid->value;
3933 
3934  $this->_sDynImageDir = $myConfig->getPictureUrl( null, false );
3935  $this->dabsimagedir = $myConfig->getPictureDir( false ); //$sThisShop
3936  $this->nossl_dimagedir = $myConfig->getPictureUrl( null, false, false, null, $sThisShop ); //$sThisShop
3937  $this->ssl_dimagedir = $myConfig->getPictureUrl( null, false, true, null, $sThisShop ); //$sThisShop
3938  }
3939 
3945  protected function _assignComparisonListFlag()
3946  {
3947  // #657 add a flag if article is on comparisonlist
3948 
3949  $aItems = oxSession::getVar('aFiltcompproducts');
3950  if ( isset( $aItems[$this->getId()])) {
3951  $this->_blIsOnComparisonList = true;
3952  }
3953  }
3954 
3955 
3963  protected function _insert()
3964  {
3965  // set oxinsert
3966  $sNow = date('Y-m-d H:i:s', oxRegistry::get("oxUtilsDate")->getTime());
3967  $this->oxarticles__oxinsert = new oxField( $sNow );
3968  if ( !is_object($this->oxarticles__oxsubclass) || $this->oxarticles__oxsubclass->value == '') {
3969  $this->oxarticles__oxsubclass = new oxField('oxarticle');
3970  }
3971 
3972  return parent::_insert();
3973  }
3974 
3980  protected function _update()
3981  {
3982 
3983  $this->setUpdateSeo(true);
3984  $this->_setUpdateSeoOnFieldChange('oxtitle');
3985 
3986  $this->_skipSaveFields();
3987 
3988  $myConfig = $this->getConfig();
3989 
3990 
3991  $blRes = parent::_update();
3992 
3993 
3994  return $blRes;
3995  }
3996 
4002  public function updateVariantsRemind()
4003  {
4004  // check if it is parent article
4005  if ( !$this->isVariant() && $this->_hasAnyVariant()) {
4006  $oDb = oxDb::getDb();
4007  $sOxId = $oDb->quote($this->getId());
4008  $sOxShopId = $oDb->quote($this->getShopId());
4009  $iRemindActive = $oDb->quote($this->oxarticles__oxremindactive->value);
4010  $sUpdate = "
4011  update oxarticles
4012  set oxremindactive = $iRemindActive
4013  where oxparentid = $sOxId and
4014  oxshopid = $sOxShopId
4015  ";
4016  $oDb->execute( $sUpdate );
4017  }
4018  }
4019 
4027  protected function _deleteRecords($sOXID)
4028  {
4029  $oDb = oxDb::getDb();
4030 
4031  $sOXID = $oDb->quote($sOXID);
4032 
4033  //remove other records
4034  $sDelete = 'delete from oxobject2article where oxarticlenid = '.$sOXID.' or oxobjectid = '.$sOXID.' ';
4035  $oDb->execute( $sDelete);
4036 
4037  $sDelete = 'delete from oxobject2attribute where oxobjectid = '.$sOXID.' ';
4038  $oDb->execute( $sDelete);
4039 
4040  $sDelete = 'delete from oxobject2category where oxobjectid = '.$sOXID.' ';
4041  $oDb->execute( $sDelete);
4042 
4043  $sDelete = 'delete from oxobject2selectlist where oxobjectid = '.$sOXID.' ';
4044  $oDb->execute( $sDelete);
4045 
4046  $sDelete = 'delete from oxprice2article where oxartid = '.$sOXID.' ';
4047  $oDb->execute( $sDelete);
4048 
4049  $sDelete = 'delete from oxreviews where oxtype="oxarticle" and oxobjectid = '.$sOXID.' ';
4050  $oDb->execute( $sDelete);
4051 
4052  $sDelete = 'delete from oxratings where oxobjectid = '.$sOXID.' ';
4053  $rs = $oDb->execute( $sDelete );
4054 
4055  $sDelete = 'delete from oxaccessoire2article where oxobjectid = '.$sOXID.' or oxarticlenid = '.$sOXID.' ';
4056  $oDb->execute( $sDelete);
4057 
4058  //#1508C - deleting oxobject2delivery entries added
4059  $sDelete = 'delete from oxobject2delivery where oxobjectid = '.$sOXID.' and oxtype=\'oxarticles\' ';
4060  $oDb->execute( $sDelete);
4061 
4062  $sDelete = 'delete from oxartextends where oxid = '.$sOXID.' ';
4063  $oDb->execute( $sDelete);
4064 
4065  //delete the record
4066  foreach ( $this->_getLanguageSetTables( "oxartextends" ) as $sSetTbl ) {
4067  $oDb->execute( "delete from $sSetTbl where oxid = {$sOXID}" );
4068  }
4069 
4070  $sDelete = 'delete from oxactions2article where oxartid = '.$sOXID.' ';
4071  $rs = $oDb->execute( $sDelete );
4072 
4073  $sDelete = 'delete from oxobject2list where oxobjectid = '.$sOXID.' ';
4074  $rs = $oDb->execute( $sDelete );
4075 
4076 
4077  return $rs;
4078  }
4079 
4087  protected function _deleteVariantRecords( $sOXID )
4088  {
4089  if ( $sOXID ) {
4090  $oDb = oxDb::getDb();
4091  //collect variants to remove recursively
4092  $sQ = 'select oxid from '.$this->getViewName().' where oxparentid = '.$oDb->quote( $sOXID );
4093  $rs = $oDb->select( $sQ, false, false );
4094  $oArticle = oxNew("oxArticle");
4095  if ($rs != false && $rs->recordCount() > 0) {
4096  while (!$rs->EOF) {
4097  $oArticle->setId($rs->fields[0]);
4098  $oArticle->delete();
4099  $rs->moveNext();
4100  }
4101  }
4102  }
4103  }
4104 
4110  protected function _deletePics()
4111  {
4112  $myUtilsPic = oxRegistry::get("oxUtilsPic");
4113  $myConfig = $this->getConfig();
4114  $oPictureHandler = oxRegistry::get("oxPictureHandler");
4115 
4116  //deleting custom main icon
4117  $oPictureHandler->deleteMainIcon( $this );
4118 
4119  //deleting custom thumbnail
4120  $oPictureHandler->deleteThumbnail( $this );
4121 
4122  $sAbsDynImageDir = $myConfig->getPictureDir(false);
4123 
4124  // deleting master image and all generated images
4125  $iPicCount = $myConfig->getConfigParam( 'iPicCount' );
4126  for ( $i = 1; $i <= $iPicCount; $i++ ) {
4127  $oPictureHandler->deleteArticleMasterPicture( $this, $i );
4128  }
4129  }
4130 
4140  protected function _onChangeResetCounts( $sOxid, $sVendorId = null, $sManufacturerId = null )
4141  {
4142 
4143  $myUtilsCount = oxRegistry::get("oxUtilsCount");
4144 
4145  if ( $sVendorId ) {
4146  $myUtilsCount->resetVendorArticleCount( $sVendorId );
4147  }
4148 
4149  if ( $sManufacturerId ) {
4150  $myUtilsCount->resetManufacturerArticleCount( $sManufacturerId );
4151  }
4152 
4153  //also reseting category counts
4154  $oDb = oxDb::getDb();
4155  $sQ = "select oxcatnid from oxobject2category where oxobjectid = ".$oDb->quote($sOxid);
4156  $oRs = $oDb->select( $sQ, false, false );
4157  if ( $oRs !== false && $oRs->recordCount() > 0) {
4158  while ( !$oRs->EOF ) {
4159  $myUtilsCount->resetCatArticleCount( $oRs->fields[0] );
4160  $oRs->moveNext();
4161  }
4162  }
4163  }
4164 
4172  protected function _onChangeUpdateStock( $sParentID )
4173  {
4174  if ( $sParentID ) {
4175  $oDb = oxDb::getDb();
4176  $sParentIdQuoted = $oDb->quote($sParentID);
4177  $sQ = 'select oxstock, oxvendorid, oxmanufacturerid from oxarticles where oxid = '.$sParentIdQuoted;
4178  $rs = $oDb->select( $sQ, false, false );
4179  $iOldStock = $rs->fields[0];
4180  $iVendorID = $rs->fields[1];
4181  $iManufacturerID = $rs->fields[2];
4182 
4183  $sQ = 'select sum(oxstock) from '.$this->getViewName(true).' where oxparentid = '.$sParentIdQuoted.' and '. $this->getSqlActiveSnippet( true ).' and oxstock > 0 ';
4184  $iStock = (float) $oDb->getOne( $sQ, false, false );
4185 
4186  $sQ = 'update oxarticles set oxvarstock = '.$iStock.' where oxid = '.$sParentIdQuoted;
4187  $oDb->execute( $sQ );
4188 
4189  //now lets update category counts
4190  //first detect stock status change for this article (to or from 0)
4191  if ( $iStock < 0 ) {
4192  $iStock = 0;
4193  }
4194  if ( $iOldStock < 0 ) {
4195  $iOldStock = 0;
4196  }
4197  if ( $this->oxarticles__oxstockflag->value == 2 && $iOldStock xor $iStock ) {
4198  //means the stock status could be changed (oxstock turns from 0 to 1 or from 1 to 0)
4199  // so far we leave it like this but later we could move all count resets to one or two functions
4200  $this->_onChangeResetCounts( $sParentID, $iVendorID, $iManufacturerID );
4201  }
4202  }
4203  }
4204 
4212  protected function _onChangeStockResetCount( $sOxid )
4213  {
4214  $myConfig = $this->getConfig();
4215 
4216  if ( $myConfig->getConfigParam( 'blUseStock' ) && $this->oxarticles__oxstockflag->value == 2 &&
4217  ( $this->oxarticles__oxstock->value + $this->oxarticles__oxvarstock->value ) <= 0 ) {
4218 
4219  $this->_onChangeResetCounts( $sOxid, $this->oxarticles__oxvendorid->value, $this->oxarticles__oxmanufacturerid->value );
4220  }
4221  }
4222 
4230  protected function _onChangeUpdateVarCount( $sParentID )
4231  {
4232  if ( $sParentID ) {
4233  $oDb = oxDb::getDb();
4234  $sParentIdQuoted = $oDb->quote( $sParentID );
4235  $sQ = "select count(*) as varcount from oxarticles where oxparentid = {$sParentIdQuoted}";
4236  $iVarCount = (int) $oDb->getOne( $sQ, false, false );
4237 
4238  $sQ = "update oxarticles set oxvarcount = {$iVarCount} where oxid = {$sParentIdQuoted}";
4239  $oDb->execute( $sQ );
4240  }
4241  }
4242 
4250  protected function _setVarMinMaxPrice( $sParentId )
4251  {
4252  if ( $sParentId ) {
4254  $sQ = '
4255  SELECT
4256  MIN( IF( `oxarticles`.`oxprice` > 0, `oxarticles`.`oxprice`, `p`.`oxprice` ) ) AS `varminprice`,
4257  MAX( IF( `oxarticles`.`oxprice` > 0, `oxarticles`.`oxprice`, `p`.`oxprice` ) ) AS `varmaxprice`
4258  FROM '. $this->getViewName(true) . ' AS `oxarticles`
4259  LEFT JOIN '. $this->getViewName(true) . ' AS `p` ON ( `p`.`oxid` = `oxarticles`.`oxparentid` AND `p`.`oxprice` > 0 )
4260  WHERE '. $this->getSqlActiveSnippet(true) .'
4261  AND ( `oxarticles`.`oxparentid` = '. $oDb->quote( $sParentId ) .' )';
4262  $oDb->setFetchMode( oxDb::FETCH_MODE_ASSOC );
4263  $aPrices = $oDb->getRow( $sQ, false, false );
4264  if ( !is_null( $aPrices['varminprice'] ) || !is_null( $aPrices['varmaxprice'] ) ) {
4265  $sQ = '
4266  UPDATE `oxarticles`
4267  SET
4268  `oxvarminprice` = '. $oDb->quote( $aPrices['varminprice'] ) .',
4269  `oxvarmaxprice` = '. $oDb->quote( $aPrices['varmaxprice'] ) .'
4270  WHERE
4271  `oxid` = ' . $oDb->quote( $sParentId );
4272  } else {
4273  $sQ = '
4274  UPDATE `oxarticles`
4275  SET
4276  `oxvarminprice` = `oxprice`,
4277  `oxvarmaxprice` = `oxprice`
4278  WHERE
4279  `oxid` = ' . $oDb->quote( $sParentId );
4280  }
4281  $oDb->execute( $sQ );
4282  }
4283  }
4284 
4285 
4295  protected function _onChangeUpdateMinVarPrice( $sParentID )
4296  {
4297  if ( $sParentID ) {
4298  $oDb = oxDb::getDb();
4299  $sParentIdQuoted = $oDb->quote($sParentID);
4300  //#M0000883 (Sarunas)
4301  $sQ = 'select min(oxprice) as varminprice from '.$this->getViewName(true).' where '.$this->getSqlActiveSnippet(true).' and (oxparentid = '.$sParentIdQuoted.')';
4302  $dVarMinPrice = $oDb->getOne( $sQ, false, false );
4303 
4304  $dParentPrice = $oDb->getOne( "select oxprice from oxarticles where oxid = $sParentIdQuoted ", false, false );
4305 
4306  $blParentBuyable = $this->getConfig()->getConfigParam( 'blVariantParentBuyable' );
4307 
4308  if ($dVarMinPrice) {
4309  if ($blParentBuyable) {
4310  $dVarMinPrice = min($dVarMinPrice, $dParentPrice);
4311  }
4312  } else {
4313  $dVarMinPrice = $dParentPrice;
4314  }
4315 
4316  if ( $dVarMinPrice ) {
4317  $sQ = 'update oxarticles set oxvarminprice = '.$dVarMinPrice.' where oxid = '.$sParentIdQuoted;
4318  $oDb->execute($sQ);
4319  }
4320  }
4321  }
4322 
4323 
4331  protected function _applyRangePrice()
4332  {
4333  //#buglist_413 if bl_perfLoadPriceForAddList variant price shouldn't be loaded too
4334  if ( !$this->getConfig()->getConfigParam( 'bl_perfLoadPrice' ) || !$this->_blLoadPrice ) {
4335  return;
4336  }
4337 
4338  $this->_blIsRangePrice = false;
4339 
4340  // if parent is buyable - do not apply range price calculations
4341  if ($this->_blSkipFromPrice || !$this->_blNotBuyableParent) {
4342  return;
4343  }
4344 
4345  if ( $this->isParentNotBuyable() && !$this->getConfig()->getConfigParam( 'blLoadVariants' )) {
4346 
4347  $dPrice = $this->_preparePrice( $this->oxarticles__oxvarminprice->value, $this->getArticleVat() );
4348  $this->getPrice()->setPrice($dPrice);
4349  $this->_blIsRangePrice = true;
4350  $this->_calculatePrice( $this->getPrice() );
4351  return;
4352 
4353  }
4354 
4355  $aPrices = array();
4356 
4357  if (!$this->_blNotBuyableParent) {
4358  $aPrices[] = $this->getPrice()->getPrice();
4359  }
4360 
4361  $aVariants = $this->getVariants(false);
4362 
4363  if (count($aVariants)) {
4364  foreach ($aVariants as $sKey => $oVariant) {
4365  $aPrices[] = $oVariant->getPrice()->getPrice();
4366  }
4367  }
4368 
4369  if ( count( $aPrices ) ) {
4370  $dMinPrice = min( $aPrices );
4371  $dMaxPrice = max( $aPrices );
4372  }
4373 
4374  if ($this->_blNotBuyableParent && isset($dMinPrice) && $dMinPrice == $dMaxPrice) {
4375  $this->getPrice()->setPrice($dMinPrice);
4376  }
4377 
4378  if (isset($dMinPrice) && $dMinPrice != $dMaxPrice) {
4379  $this->getPrice()->setPrice($dMinPrice);
4380  $this->_blIsRangePrice = true;
4381  }
4382  }
4383 
4390  public function getProductId()
4391  {
4392  return $this->getId();
4393  }
4394 
4402  public function getProductParentId()
4403  {
4404  return $this->getParentId();
4405  }
4406 
4412  public function getParentId()
4413  {
4414  return $this->oxarticles__oxparentid->value;
4415  }
4416 
4422  public function isOrderArticle()
4423  {
4424  return false;
4425  }
4426 
4432  public function isVariant()
4433  {
4434  return (bool) ( isset( $this->oxarticles__oxparentid ) ? $this->oxarticles__oxparentid->value : false );
4435  }
4436 
4442  public function isMdVariant()
4443  {
4444  $oMdVariant = oxNew( "oxVariantHandler" );
4445 
4446  return $oMdVariant->isMdVariant($this);
4447  }
4448 
4456  public function getSqlForPriceCategories($sFields = '')
4457  {
4458  if (!$sFields) {
4459  $sFields = 'oxid';
4460  }
4461  $sSelectWhere = "select $sFields from ".$this->_getObjectViewName('oxcategories')." where";
4462  $sQuotedPrice = oxDb::getDb()->quote( $this->oxarticles__oxprice->value );
4463  return "$sSelectWhere oxpricefrom != 0 and oxpriceto != 0 and oxpricefrom <= $sQuotedPrice and oxpriceto >= $sQuotedPrice"
4464  ." union $sSelectWhere oxpricefrom != 0 and oxpriceto = 0 and oxpricefrom <= $sQuotedPrice"
4465  ." union $sSelectWhere oxpricefrom = 0 and oxpriceto != 0 and oxpriceto >= $sQuotedPrice";
4466  }
4467 
4475  public function inPriceCategory( $sCatNid )
4476  {
4477  $oDb = oxDb::getDb();
4478 
4479  $sQuotedPrice = $oDb->quote( $this->oxarticles__oxprice->value );
4480  $sQuotedCnid = $oDb->quote( $sCatNid );
4481  return (bool) $oDb->getOne(
4482  "select 1 from ".$this->_getObjectViewName('oxcategories')." where oxid=$sQuotedCnid and"
4483  ."( (oxpricefrom != 0 and oxpriceto != 0 and oxpricefrom <= $sQuotedPrice and oxpriceto >= $sQuotedPrice)"
4484  ." or (oxpricefrom != 0 and oxpriceto = 0 and oxpricefrom <= $sQuotedPrice)"
4485  ." or (oxpricefrom = 0 and oxpriceto != 0 and oxpriceto >= $sQuotedPrice)"
4486  .")"
4487  );
4488  }
4489 
4495  public function getMdVariants()
4496  {
4497  if ( $this->_oMdVariants ) {
4498  return $this->_oMdVariants;
4499  }
4500 
4501  $oParentArticle = $this->getParentArticle();
4502  if ( $oParentArticle ) {
4503  $oVariants = $oParentArticle->getVariants();
4504  } else {
4505  $oVariants = $this->getVariants();
4506  }
4507 
4508  $oVariantHandler = oxNew( "oxVariantHandler" );
4509  $this->_oMdVariants = $oVariantHandler->buildMdVariants( $oVariants, $this->getId() );
4510 
4511  return $this->_oMdVariants;
4512  }
4513 
4519  public function getMdSubvariants()
4520  {
4521  return $this->getMdVariants()->getMdSubvariants();
4522  }
4523 
4531  protected function _hasMasterImage( $iIndex )
4532  {
4533  $sPicName = basename($this->{"oxarticles__oxpic" . $iIndex}->value);
4534 
4535  if ( $sPicName == "nopic.jpg" || $sPicName == "" ) {
4536  return false;
4537  }
4538  if ( $this->isVariant() && $this->getParentArticle()->{"oxarticles__oxpic".$iIndex}->value == $this->{"oxarticles__oxpic".$iIndex}->value ) {
4539  return false;
4540  }
4541 
4542  $sMasterPic = 'product/'.$iIndex . "/" . $sPicName;
4543 
4544  if ( $this->getConfig()->getMasterPicturePath( $sMasterPic ) ) {
4545  return true;
4546  }
4547 
4548  return false;
4549  }
4550 
4559  public function getPictureFieldValue( $sFieldName, $iIndex = null )
4560  {
4561  if ( $sFieldName ) {
4562  $sFieldName = "oxarticles__" . $sFieldName . $iIndex;
4563  return $this->$sFieldName->value;
4564  }
4565  }
4566 
4574  public function getMasterZoomPictureUrl( $iIndex )
4575  {
4576  $sPicUrl = false;
4577  $sPicName = basename( $this->{"oxarticles__oxpic" . $iIndex}->value );
4578 
4579  if ( $sPicName && $sPicName != "nopic.jpg" ) {
4580  $sPicUrl = $this->getConfig()->getPictureUrl( "master/product/" . $iIndex . "/" . $sPicName );
4581  if ( !$sPicUrl || basename( $sPicUrl ) == "nopic.jpg" ) {
4582  $sPicUrl = false;
4583  }
4584  }
4585 
4586  return $sPicUrl;
4587  }
4588 
4594  public function getUnitName()
4595  {
4596  if ( $this->oxarticles__oxunitname->value ) {
4597  return oxRegistry::getLang()->translateString( $this->oxarticles__oxunitname->value );
4598  }
4599  }
4600 
4608  public function getArticleFiles( $blAddFromParent=false )
4609  {
4610  if ( $this->_aArticleFiles === null) {
4611 
4612  $this->_aArticleFiles = false;
4613 
4614  $sQ = "SELECT * FROM `oxfiles` WHERE `oxartid` = '".$this->getId()."'";
4615 
4616  if ( !$this->getConfig()->getConfigParam( 'blVariantParentBuyable' ) && $blAddFromParent ) {
4617  $sQ .= " OR `oxartId` = '". $this->oxarticles__oxparentid->value . "'";
4618  }
4619 
4620  $oArticleFiles = oxNew("oxlist");
4621  $oArticleFiles->init("oxfile");
4622  $oArticleFiles->selectString( $sQ );
4623  $this->_aArticleFiles = $oArticleFiles;
4624 
4625  }
4626 
4627  return $this->_aArticleFiles;
4628  }
4629 
4635  public function isDownloadable()
4636  {
4637  return $this->oxarticles__oxisdownloadable->value;
4638  }
4639 
4645  public function hasAmountPrice()
4646  {
4647  if ( self::$_blHasAmountPrice === null ) {
4648 
4649  self::$_blHasAmountPrice = false;
4650 
4651  $oDb = oxDb::getDb();
4652  $sQ = "SELECT 1 FROM `oxprice2article` LIMIT 1";
4653 
4654  if ( $oDb->getOne( $sQ ) ) {
4655  self::$_blHasAmountPrice = true;
4656  }
4657  }
4658 
4659  return self::$_blHasAmountPrice;
4660  }
4661 
4662 
4668  protected function _isPriceViewModeNetto()
4669  {
4670  $blResult = (bool) $this->getConfig()->getConfigParam('blShowNetPrice');
4671  $oUser = $this->getArticleUser();
4672  if ( $oUser ) {
4673  $blResult = $oUser->isPriceViewModeNetto();
4674  }
4675 
4676  return $blResult;
4677  }
4678 
4679 
4687  protected function _getPriceObject( $blCalculationModeNetto = null )
4688  {
4689  $oPrice = oxNew( 'oxPrice' );
4690 
4691  if ( $blCalculationModeNetto === null ) {
4692  $blCalculationModeNetto = $this->_isPriceViewModeNetto();
4693  }
4694 
4695  if ( $blCalculationModeNetto ) {
4696  $oPrice->setNettoPriceMode();
4697  } else {
4698  $oPrice->setBruttoPriceMode();
4699  }
4700 
4701  return $oPrice;
4702  }
4703 
4704 
4712  protected function _getPriceForView( $oPrice )
4713  {
4714  if ( $this->_isPriceViewModeNetto() ) {
4715  $dPrice = $oPrice->getNettoPrice();
4716  } else {
4717  $dPrice = $oPrice->getBruttoPrice();
4718  }
4719 
4720  return $dPrice;
4721  }
4722 
4723 
4733  protected function _preparePrice( $dPrice, $dVat, $blCalculationModeNetto = null )
4734  {
4735  if ( $blCalculationModeNetto === null ) {
4736  $blCalculationModeNetto = $this->_isPriceViewModeNetto();
4737  }
4738 
4739  $oCurrency = $this->getConfig()->getActShopCurrencyObject();
4740 
4741  $blEnterNetPrice = $this->getConfig()->getConfigParam('blEnterNetPrice');
4742  if ( $blCalculationModeNetto && !$blEnterNetPrice ) {
4743  $dPrice = round( oxPrice::brutto2Netto( $dPrice, $dVat ), $oCurrency->decimal );
4744  } elseif ( !$blCalculationModeNetto && $blEnterNetPrice ) {
4745  $dPrice = round( oxPrice::netto2Brutto( $dPrice, $dVat ), $oCurrency->decimal );
4746  }
4747 
4748  return $dPrice;
4749  }
4750 
4751 
4758  public function getFUnitPrice()
4759  {
4760  if ($this->_fPricePerUnit == null) {
4761  if ( $oPrice = $this->getUnitPrice() ) {
4762  if ( $dPrice = $this->_getPriceForView( $oPrice ) ) {
4763  $this->_fPricePerUnit = oxRegistry::getLang()->formatCurrency( $dPrice );
4764  }
4765  }
4766  }
4767 
4768  return $this->_fPricePerUnit;
4769  }
4770 
4771 
4777  public function getUnitPrice()
4778  {
4779  // Performance
4780  if ( !$this->getConfig()->getConfigParam( 'bl_perfLoadPrice' ) || !$this->_blLoadPrice ) {
4781  return;
4782  }
4783 
4784  $oPrice = null;
4785  if ( (double) $this->getUnitQuantity() && $this->oxarticles__oxunitname->value ) {
4786  $oPrice = clone $this->getPrice();
4787  $oPrice->divide( (double) $this->getUnitQuantity() );
4788  }
4789 
4790  return $oPrice;
4791  }
4792 
4793 
4801  public function getFMinPrice()
4802  {
4803  $sPrice = '';
4804  if ( $oPrice = $this->getMinPrice() ) {
4805  $dPrice = $this->_getPriceForView( $oPrice );
4806  $sPrice = oxRegistry::getLang()->formatCurrency( $dPrice );
4807  }
4808 
4809  return $sPrice;
4810  }
4811 
4819  public function getFVarMinPrice()
4820  {
4821  $sPrice = '';
4822  if ( $oPrice = $this->getVarMinPrice() ) {
4823  $dPrice = $this->_getPriceForView( $oPrice );
4824  $sPrice = oxRegistry::getLang()->formatCurrency( $dPrice );
4825  }
4826 
4827  return $sPrice;
4828  }
4829 
4835  public function getVarMinPrice()
4836  {
4837  if ( !$this->getConfig()->getConfigParam( 'bl_perfLoadPrice' ) || !$this->_blLoadPrice ) {
4838  return;
4839  }
4840 
4841  $oPrice = null;
4842  $dPrice = $this->_getVarMinPrice();
4843 
4844  $dPrice = $this->_preparePrice( $dPrice, $this->getArticleVat() );
4845 
4846 
4847  $oPrice = $this->_getPriceObject();
4848  $oPrice->setPrice( $dPrice );
4849  $this->_calculatePrice( $oPrice );
4850 
4851 
4852  return $oPrice;
4853  }
4854 
4855 
4861  public function getMinPrice()
4862  {
4863  if ( !$this->getConfig()->getConfigParam( 'bl_perfLoadPrice' ) || !$this->_blLoadPrice ) {
4864  return;
4865  }
4866 
4867  $oPrice = null;
4868  $dPrice = $this->_getPrice();
4869  if ( $this->_getVarMinPrice() !== null && $dPrice > $this->_getVarMinPrice() ) {
4870  $dPrice = $this->_getVarMinPrice();
4871  }
4872 
4873  $dPrice = $this->_preparePrice( $dPrice, $this->getArticleVat() );
4874 
4875 
4876  $oPrice = $this->_getPriceObject();
4877  $oPrice->setPrice( $dPrice );
4878  $this->_calculatePrice( $oPrice );
4879 
4880  return $oPrice;
4881  }
4882 
4883 
4889  public function isRangePrice()
4890  {
4891  if ( $this->_blIsRangePrice === null ) {
4892 
4893  $this->setRangePrice( false );
4894 
4895  if ( $this->_hasAnyVariant() ) {
4896  $dPrice = $this->_getPrice();
4897  $dMinPrice = $this->_getVarMinPrice();
4898  $dMaxPrice = $this->_getVarMaxPrice();
4899 
4900  if ( $dMinPrice != $dMaxPrice ) {
4901  $this->setRangePrice();
4902  } elseif ( !$this->isParentNotBuyable() && $dMinPrice != $dPrice ) {
4903  $this->setRangePrice();
4904  }
4905  }
4906  }
4907 
4908  return $this->_blIsRangePrice;
4909  }
4910 
4911 
4919  public function setRangePrice( $blIsRangePrice = true )
4920  {
4921  return $this->_blIsRangePrice = $blIsRangePrice;
4922  }
4923 
4929  protected function _getUserPriceSufix()
4930  {
4931  $sPriceSufix = '';
4932  $oUser = $this->getArticleUser();
4933 
4934  if ( $oUser ) {
4935  if ( $oUser->inGroup( 'oxidpricea' ) ) {
4936  $sPriceSufix = 'a';
4937  } elseif ( $oUser->inGroup( 'oxidpriceb' ) ) {
4938  $sPriceSufix = 'b';
4939  } elseif ( $oUser->inGroup( 'oxidpricec' ) ) {
4940  $sPriceSufix = 'c';
4941  }
4942  }
4943 
4944  return $sPriceSufix;
4945  }
4946 
4947 
4953  protected function _getPrice()
4954  {
4955  $sPriceSufix = $this->_getUserPriceSufix();
4956  if ( $sPriceSufix === '') {
4957  $dPrice = $this->oxarticles__oxprice->value;
4958  } else {
4959  if ( $this->getConfig()->getConfigParam( 'blOverrideZeroABCPrices' ) ) {
4960  $dPrice = ($this->{oxarticles__oxprice.$sPriceSufix}->value !=0 )? $this->{oxarticles__oxprice.$sPriceSufix}->value : $this->oxarticles__oxprice->value;
4961  } else {
4962  $dPrice = $this->{oxarticles__oxprice.$sPriceSufix}->value;
4963  }
4964  }
4965  return $dPrice;
4966  }
4967 
4968 
4974  protected function _getVarMinPrice()
4975  {
4976  if ( $this->_dVarMinPrice === null) {
4977 
4978  $sPriceSufix = $this->_getUserPriceSufix();
4979  if ( $dPrice === null ) {
4980  if ( $sPriceSufix === '' ) {
4981  $dPrice = $this->oxarticles__oxvarminprice->value;
4982  } else {
4983  $sSql = 'SELECT ';
4984  if ( $this->getConfig()->getConfigParam( 'blOverrideZeroABCPrices' ) ) {
4985  $sSql .= 'MIN( IF(`oxprice'.$sPriceSufix.'` = 0, `oxprice`, `oxprice'.$sPriceSufix.'`) ) AS `varminprice` ';
4986  } else {
4987  $sSql .= 'MIN(`oxprice'.$sPriceSufix.'`) AS `varminprice` ';
4988  }
4989 
4990  $sSql .= ' FROM ' . $this->getViewName(true) . '
4991  WHERE ' .$this->getSqlActiveSnippet(true) . '
4992  AND ( `oxparentid` = ' . oxDb::getDb()->quote( $this->getId() ) . ' )';
4993 
4994  $dPrice = oxDb::getDb()->getOne( $sSql );
4995  }
4996  }
4997  $this->_dVarMinPrice = $dPrice;
4998  }
4999 
5000  return $this->_dVarMinPrice;
5001  }
5002 
5008  protected function _getSubShopVarMinPrice()
5009  {
5010  $myConfig = $this->getConfig();
5011  $sShopId = $myConfig->getShopId();
5012  if ($this->getConfig()->getConfigParam( 'blMallCustomPrice' ) && $sShopId != $this->oxarticles__oxshopid->value ) {
5013  $sPriceSufix = $this->_getUserPriceSufix();
5014  $sSql = 'SELECT ';
5015  if ( $sPriceSufix != '' && $this->getConfig()->getConfigParam( 'blOverrideZeroABCPrices' ) ) {
5016  $sSql .= 'MIN(IF(`oxfield2shop`.`oxprice'.$sPriceSufix.'` = 0, `oxfield2shop`.`oxprice`, `oxfield2shop`.`oxprice'.$sPriceSufix.'`)) AS `varminprice` ';
5017  } else {
5018  $sSql .= 'MIN(`oxfield2shop`.`oxprice'.$sPriceSufix.'`) AS `varminprice` ';
5019  }
5020  $sSql .= ' FROM ' . getViewName('oxfield2shop') . ' AS oxfield2shop
5021  INNER JOIN ' . $this->getViewName(true) . ' AS oxarticles ON `oxfield2shop`.`oxartid` = `oxarticles`.`oxid`
5022  WHERE ' .$this->getSqlActiveSnippet(true) . '
5023  AND ( `oxarticles`.`oxparentid` = ' . oxDb::getDb()->quote( $this->getId() ) . ' )
5024  AND ( `oxfield2shop`.`oxshopid` = ' . oxDb::getDb()->quote( $sShopId ) . ' )';
5025  $dPrice = oxDb::getDb()->getOne( $sSql );
5026  }
5027  return $dPrice;
5028  }
5029 
5035  protected function _getVarMaxPrice()
5036  {
5037  if ( $this->_dVarMaxPrice === null ) {
5038 
5039  $sPriceSufix = $this->_getUserPriceSufix();
5040  if ( $dPrice === null ) {
5041  if ( $sPriceSufix === '') {
5042  $dPrice = $this->oxarticles__oxvarmaxprice->value;
5043  } else {
5044  $sSql = 'SELECT ';
5045  if ( $this->getConfig()->getConfigParam( 'blOverrideZeroABCPrices' ) ) {
5046  $sSql .= 'MAX( IF(`oxprice'.$sPriceSufix.'` = 0, `oxprice`, `oxprice'.$sPriceSufix.'`) ) AS `varmaxprice` ';
5047  } else {
5048  $sSql .= 'MAX(`oxprice'.$sPriceSufix.'`) AS `varmaxprice` ';
5049  }
5050 
5051  $sSql .= ' FROM ' . $this->getViewName(true) . '
5052  WHERE ' .$this->getSqlActiveSnippet(true) . '
5053  AND ( `oxparentid` = ' . oxDb::getDb()->quote( $this->getId() ) . ' )';
5054 
5055  $dPrice = oxDb::getDb()->getOne( $sSql );
5056  }
5057  }
5058  $this->_dVarMaxPrice = $dPrice;
5059  }
5060 
5061  return $this->_dVarMaxPrice;
5062  }
5063 
5069  protected function _getSubShopVarMaxPrice()
5070  {
5071  $myConfig = $this->getConfig();
5072  $sShopId = $myConfig->getShopId();
5073  if ($this->getConfig()->getConfigParam( 'blMallCustomPrice' ) && $sShopId != $this->oxarticles__oxshopid->value ) {
5074  $sPriceSufix = $this->_getUserPriceSufix();
5075  $sSql = 'SELECT ';
5076  if ( $sPriceSufix != '' && $this->getConfig()->getConfigParam( 'blOverrideZeroABCPrices' ) ) {
5077  $sSql .= 'MAX(IF(`oxfield2shop`.`oxprice'.$sPriceSufix.'` = 0, `oxfield2shop`.`oxprice`, `oxfield2shop`.`oxprice'.$sPriceSufix.'`)) AS `varmaxprice` ';
5078  } else {
5079  $sSql .= 'MAX(`oxfield2shop`.`oxprice'.$sPriceSufix.'`) AS `varmaxprice` ';
5080  }
5081  $sSql .= ' FROM ' . getViewName('oxfield2shop') . ' AS oxfield2shop
5082  INNER JOIN ' . $this->getViewName(true) . ' AS oxarticles ON `oxfield2shop`.`oxartid` = `oxarticles`.`oxid`
5083  WHERE ' .$this->getSqlActiveSnippet(true) . '
5084  AND ( `oxarticles`.`oxparentid` = ' . oxDb::getDb()->quote( $this->getId() ) . ' )
5085  AND ( `oxfield2shop`.`oxshopid` = ' . oxDb::getDb()->quote( $sShopId ) . ' )';
5086  $dPrice = oxDb::getDb()->getOne( $sSql );
5087  }
5088  return $dPrice;
5089  }
5090 
5096  public function getUnitQuantity()
5097  {
5098  return $this->oxarticles__oxunitquantity->value;
5099  }
5100 
5106  public function getSize()
5107  {
5108  $dSize = $this->oxarticles__oxlength->value *
5109  $this->oxarticles__oxwidth->value *
5110  $this->oxarticles__oxheight->value;
5111 
5112  return $dSize;
5113  }
5114 
5120  public function getWeight()
5121  {
5122  return $this->oxarticles__oxweight->value;
5123  }
5124 
5130  protected function _updateParentDependFields()
5131  {
5132  $oDb = oxDb::getDb();
5133 
5134  foreach ( $this->_getCopyParentFields() as $sField ) {
5135  $sValue = isset( $this->$sField->value ) ? $this->$sField->value : 0;
5136  $sSqlSets[] = '`' . str_replace( 'oxarticles__', '', $sField ) . '` = ' . $oDb->quote( $sValue );
5137  }
5138 
5139  $sSql = "UPDATE `oxarticles` SET ";
5140  $sSql .= implode(', ', $sSqlSets) . '';
5141  $sSql .= " WHERE `oxparentid` = " . $oDb->quote( $this->getId() );
5142 
5143  return $oDb->execute( $sSql );
5144  }
5145 
5146 
5152  protected function _getCopyParentFields()
5153  {
5154  return $this->_aCopyParentField;
5155  }
5156 
5162  protected function _assignParentDependFields()
5163  {
5164  $sParent = $this->getParentArticle();
5165  if ( $sParent ) {
5166  foreach ( $this->_getCopyParentFields() as $sField ) {
5167  $this->$sField = new oxField ( $sParent->$sField->value );
5168  }
5169  }
5170  }
5171 
5179  protected function loadSimpleVariantsForLists( $blSimple )
5180  {
5181  if ( $blSimple ) {
5182  $oVariants = oxNew( 'oxsimplevariantlist' );
5183  $oVariants->setParent( $this );
5184  } else {
5185  $oVariants = oxNew( 'oxarticlelist' );
5186  $oVariants->getBaseObject()->modifyCacheKey( '_variants' );
5187  }
5188 
5189  return $oVariants;
5190  }
5191 
5201  protected function _selectVariants( $blRemoveNotOrderables, $blForceCoreTable, $oVariants )
5202  {
5203  $myConfig = $this->getConfig();
5204 
5205  startProfile( "selectVariants" );
5206 
5207  $blUseCoreTable = (bool) $blForceCoreTable;
5208  $oBaseObject = $oVariants->getBaseObject();
5209  $oBaseObject->setLanguage( $this->getLanguage() );
5210 
5211 
5212  $sArticleTable = $this->getViewName( $blUseCoreTable );
5213 
5214  $sSelect = "select " . $oBaseObject->getSelectFields( $blUseCoreTable ) . " from $sArticleTable where " .
5215  $this->getActiveCheckQuery( $blUseCoreTable ) .
5216  $this->getVariantsQuery( $blRemoveNotOrderables, $blUseCoreTable ) .
5217  " order by $sArticleTable.oxsort";
5218 
5219 
5220  $oVariants->selectString( $sSelect );
5221 
5222  //if this is multidimensional variants, make additional processing
5223  if ( $myConfig->getConfigParam( 'blUseMultidimensionVariants' ) ) {
5224  $oMdVariants = oxNew( "oxVariantHandler" );
5225  $this->_blHasMdVariants = $oMdVariants->isMdVariant( $oVariants->current() );
5226  }
5227 
5228  stopProfile( "selectVariants" );
5229 
5230  return $oVariants;
5231  }
5232 
5233 }