37 public function init( $oArticles )
39 $this->_oArticles = $oArticles;
52 $oMdVariants =
oxNew(
"OxMdVariant" );
53 $oMdVariants->setParentId( $sParentId );
54 $oMdVariants->setName(
"_parent_product_" );
55 foreach ( $oVariants as $sKey => $oVariant ) {
56 $aNames = explode( trim( $this->_sMdSeparator ), $oVariant->oxarticles__oxvarselect->value );
57 foreach ( $aNames as $sNameKey => $sName ) {
58 $aNames[$sNameKey] = trim($sName);
60 $oMdVariants->addNames( $sKey,
62 ( $this->
getConfig()->getConfigParam(
'bl_perfLoadPrice' ) ) ? $oVariant->getPrice()->getBruttoPrice() : null,
63 $oVariant->getLink() );
79 $oVariants = $oArticle->getAdminVariants();
83 $aConfLanguages = $myLang->getLanguageIds();
85 foreach ($aSels as $sSelId) {
86 $oSel =
oxNew(
"oxi18n");
87 $oSel->setEnableMultilang(
false );
88 $oSel->init(
'oxselectlist' );
89 $oSel->load( $sSelId );
91 foreach ($aConfLanguages as $sKey => $sLang) {
92 $sPrefix = $myLang->getLanguageTag($sKey);
93 $aSelValues = $myUtils->assignValuesFromText($oSel->{
"oxselectlist__oxvaldesc".$sPrefix}->value );
94 foreach ($aSelValues as $sI => $oValue ) {
95 $aValues[$sI][$sKey] = $oValue;
97 $aSelTitle[$sKey] = $oSel->{
"oxselectlist__oxtitle".$sPrefix}->value;
98 $sMdSeparator = ($oArticle->oxarticles__oxvarname->value) ? $this->_sMdSeparator:
'';
99 if ( $sVarNameUpdate ) {
100 $sVarNameUpdate .=
", ";
102 $sVarName =
oxDb::getDb()->quote($sMdSeparator.$aSelTitle[$sKey]);
103 $sVarNameUpdate .=
"oxvarname".$sPrefix.
" = CONCAT(oxvarname".$sPrefix.
", ".$sVarName.
")";
105 $oMDVariants = $this->
_assignValues( $aValues, $oVariants, $oArticle, $aConfLanguages);
106 if (
$myConfig->getConfigParam(
'blUseMultidimensionVariants' ) ) {
107 $oAttribute =
oxNew(
"oxattribute");
108 $oAttribute->assignVarToAttribute( $oMDVariants, $aSelTitle );
124 protected function _assignValues( $aValues, $oVariants, $oArticle, $aConfLanguages)
129 $aVarselect = array();
131 for ( $i=0; $i<count($aValues); $i++ ) {
132 $oValue = $aValues[$i][0];
133 $dPriceMod = $this->_getValuePrice( $oValue, $oArticle->oxarticles__oxprice->value);
134 if ( $oVariants->count() > 0 ) {
136 foreach ( $oVariants as $oSimpleVariant ) {
139 $oVariant =
oxNew(
"oxarticle");
140 $oVariant->setEnableMultilang(
false);
141 $oVariant->load($oSimpleVariant->oxarticles__oxid->value);
142 $oVariant->oxarticles__oxprice->setValue( $oVariant->oxarticles__oxprice->value + $dPriceMod );
144 foreach ( $aConfLanguages as $sKey => $sLang ) {
145 $oValue = $aValues[$i][$sKey];
146 $sPrefix = $myLang->getLanguageTag($sKey);
147 $aVarselect[$oSimpleVariant->oxarticles__oxid->value][$sKey] = $oVariant->{
"oxarticles__oxvarselect".$sPrefix}->value;
148 $oVariant->{
'oxarticles__oxvarselect'.$sPrefix}->setValue($oVariant->{
"oxarticles__oxvarselect".$sPrefix}->value.$this->_sMdSeparator.$oValue->name);
150 $oVariant->oxarticles__oxsort->setValue($oVariant->oxarticles__oxsort->value * 10);
152 $sVarId = $oSimpleVariant->oxarticles__oxid->value;
155 foreach ($aVarselect[$oSimpleVariant->oxarticles__oxid->value] as $sKey => $sVarselect) {
156 $oValue = $aValues[$i][$sKey];
157 $sPrefix = $myLang->getLanguageTag($sKey);
158 $aParams[
'oxarticles__oxvarselect'.$sPrefix] = $sVarselect.$this->_sMdSeparator.$oValue->name;
160 $aParams[
'oxarticles__oxartnum'] = $oSimpleVariant->oxarticles__oxartnum->value .
"-" . $iCounter;
161 $aParams[
'oxarticles__oxprice'] = $oSimpleVariant->oxarticles__oxprice->value + $dPriceMod;
162 $aParams[
'oxarticles__oxsort'] = $oSimpleVariant->oxarticles__oxsort->value*10 + 10*$iCounter;
163 $aParams[
'oxarticles__oxstock'] = 0;
164 $aParams[
'oxarticles__oxstockflag'] = $oSimpleVariant->oxarticles__oxstockflag->value;
165 $aParams[
'oxarticles__oxisconfigurable'] = $oSimpleVariant->oxarticles__oxisconfigurable->value;
166 $sVarId = $this->_createNewVariant( $aParams, $oArticle->oxarticles__oxid->value );
167 if (
$myConfig->getConfigParam(
'blUseMultidimensionVariants' ) ) {
168 $oAttrList =
oxNew(
'oxattribute');
169 $aIds = $oAttrList->getAttributeAssigns( $oSimpleVariant->oxarticles__oxid->value);
170 $aMDVariants[
"mdvar_".$sVarId] = $aIds;
173 if (
$myConfig->getConfigParam(
'blUseMultidimensionVariants' ) ) {
174 $aMDVariants[$sVarId] = $aValues[$i];
181 foreach ($aConfLanguages as $sKey => $sLang) {
182 $oValue = $aValues[$i][$sKey];
183 $sPrefix = $myLang->getLanguageTag($sKey);
184 $aParams[
'oxarticles__oxvarselect'.$sPrefix] = $oValue->name;
186 $aParams[
'oxarticles__oxartnum'] = $oArticle->oxarticles__oxartnum->value .
"-" . $iCounter ;
187 $aParams[
'oxarticles__oxprice'] = $oArticle->oxarticles__oxprice->value + $dPriceMod;
188 $aParams[
'oxarticles__oxsort'] = $iCounter * 100;
189 $aParams[
'oxarticles__oxstock'] = 0;
190 $aParams[
'oxarticles__oxstockflag'] = $oArticle->oxarticles__oxstockflag->value;
191 $aParams[
'oxarticles__oxisconfigurable'] = $oArticle->oxarticles__oxisconfigurable->value;
192 $sVarId = $this->_createNewVariant( $aParams, $oArticle->oxarticles__oxid->value );
193 if (
$myConfig->getConfigParam(
'blUseMultidimensionVariants' ) ) {
194 $aMDVariants[$sVarId] = $aValues[$i];
209 protected function _getValuePrice( $oValue, $dParentPrice)
213 if (
$myConfig->getConfigParam(
'bl_perfLoadSelectLists' ) &&
$myConfig->getConfigParam(
'bl_perfUseSelectlistPrice' ) ) {
214 if ($oValue->priceUnit ==
'abs') {
215 $dPriceMod = $oValue->price;
216 } elseif ($oValue->priceUnit ==
'%') {
217 $dPriceModPerc = abs($oValue->price)*$dParentPrice/100.0;
218 if (($oValue->price) >= 0.0) {
219 $dPriceMod = $dPriceModPerc;
221 $dPriceMod = -$dPriceModPerc;
236 protected function _createNewVariant( $aParams = null, $sParentId = null)
239 $aParams[
'oxarticles__oxactive'] = 0;
243 $aParams[
'oxarticles__oxshopid'] = $sShopID;
246 $aParams[
'oxarticles__oxparentid'] = $sParentId;
248 $oArticle =
oxNew(
"oxi18n");
249 $oArticle->setEnableMultilang(
false);
250 $oArticle->init(
'oxarticles' );
251 $oArticle->assign( $aParams);
256 return $oArticle->getId();
267 protected function _updateArticleVarName( $sUpdate, $sArtId )
270 $sUpdate =
"update oxarticles set " . $sUpdate .
" where oxid = " . $oDb->quote( $sArtId );
271 $oDb->Execute( $sUpdate );
281 public function isMdVariant( $oArticle )
283 if ( $this->getConfig()->getConfigParam(
'blUseMultidimensionVariants' ) ) {
284 if ( strpos( $oArticle->oxarticles__oxvarselect->value, trim($this->_sMdSeparator) ) !==
false ) {
302 protected function _fillVariantSelections( $oVariantList, $iVarSelCnt, &$aFilter, $sActVariantId )
304 $aSelections = array();
307 foreach ( $oVariantList as $oVariant ) {
309 $aNames = $this->_getSelections( $oVariant->oxarticles__oxvarselect->getRawValue() );
310 $blActive = ( $sActVariantId === $oVariant->getId() ) ?
true :
false;
311 for ( $i = 0; $i < $iVarSelCnt; $i++ ) {
312 $sName = isset( $aNames[$i] ) ? trim($aNames[$i]) :
false;
314 $sHash = md5($sName );
318 $aFilter[$i] = $sHash;
321 $aSelections[$oVariant->getId()][$i] = array(
'name' => $sName,
'disabled' => null,
'active' =>
false,
'hash' => $sHash );
335 protected function _cleanFilter( $aFilter )
337 $aCleanFilter =
false;
338 if ( is_array( $aFilter ) && count( $aFilter ) ) {
339 foreach ( $aFilter as $iKey => $sFilter ) {
341 $aCleanFilter[$iKey] = $sFilter;
346 return $aCleanFilter;
357 protected function _applyVariantSelectionsFilter( $aSelections, $aFilter )
359 $iMaxActiveCount = 0;
360 $sMostSuitableVariantId = null;
361 $blPerfectFit =
false;
363 if ( ( $aFilter = $this->_cleanFilter( $aFilter ) ) ) {
364 $aFilterKeys = array_keys( $aFilter );
365 $iFilterKeysCount = count( $aFilter );
366 foreach ( $aSelections as $sVariantId => &$aLineSelections ) {
368 foreach ( $aFilter as $iKey => $sVal ) {
369 if ( strcmp( $aLineSelections[$iKey][
'hash'], $sVal ) === 0 ) {
370 $aLineSelections[$iKey][
'active'] =
true;
373 foreach ($aLineSelections as $iOtherKey => &$aLineOtherVariant) {
374 if ( $iKey != $iOtherKey ) {
375 $aLineOtherVariant[
'disabled'] =
true;
380 foreach ($aLineSelections as $iOtherKey => &$aLineOtherVariant) {
381 if ( !in_array( $iOtherKey, $aFilterKeys ) ) {
382 $aLineOtherVariant[
'disabled'] = !($iFilterKeysCount == $iActive);
386 $blFitsAll = $iActive && (count($aLineSelections) == $iActive) && ($iFilterKeysCount == $iActive);
387 if (($iActive > $iMaxActiveCount) || (!$blPerfectFit && $blFitsAll)) {
388 $blPerfectFit = $blFitsAll;
389 $sMostSuitableVariantId = $sVariantId;
390 $iMaxActiveCount = $iActive;
393 unset( $aLineSelections );
396 return array($aSelections, $sMostSuitableVariantId, $blPerfectFit);
407 protected function _buildVariantSelectionsList( $aVarSelects, $aSelections )
410 foreach ( $aVarSelects as $iKey => $sLabel ) {
411 $aVariantSelections[$iKey] =
oxNew(
"oxVariantSelectList", $sLabel, $iKey );
415 foreach ( $aSelections as $aLineSelections ) {
416 foreach ( $aLineSelections as $oPos => $aLine ) {
417 $aVariantSelections[$oPos]->addVariant( $aLine[
'name'], $aLine[
'hash'], $aLine[
'disabled'], $aLine[
'active'] );
421 return $aVariantSelections;
432 protected function _getSelections( $sTitle )
435 if ( $this->getConfig()->getConfigParam(
'blUseMultidimensionVariants' ) ) {
436 $aSelections = explode( $this->_sMdSeparator, $sTitle );
438 $aSelections = array( $sTitle );
455 public function buildVariantSelections( $sVarName, $oVariantList, $aFilter, $sActVariantId, $iLimit = 0 )
461 $aVarSelects = $this->_getSelections( $sVarName );
464 $aVarSelects = array_slice($aVarSelects, 0, $iLimit);
466 if ( ( $iVarSelCnt = count( $aVarSelects ) ) ) {
469 $aRawVariantSelections = $this->_fillVariantSelections( $oVariantList, $iVarSelCnt, $aFilter, $sActVariantId );
472 list($aRawVariantSelections, $sActVariantId, $blPerfectFit) = $this->_applyVariantSelectionsFilter( $aRawVariantSelections, $aFilter );
474 $aVariantSelections = $this->_buildVariantSelectionsList( $aVarSelects, $aRawVariantSelections );
476 $oCurrentVariant = null;
477 if ($sActVariantId) {
478 $oCurrentVariant = $oVariantList[$sActVariantId];
484 'selections' => $aVariantSelections,
485 'rawselections' => $aRawVariantSelections,
486 'oActiveVariant' => $oCurrentVariant,
487 'blPerfectFit' => $blPerfectFit