oxvarianthandler.php

Go to the documentation of this file.
00001 <?php
00002 
00007 class oxVariantHandler extends oxSuperCfg
00008 {
00014     protected $_oArticles = null;
00015 
00021     protected $_sMdSeparator = " | ";
00022 
00028     protected $_oMdVariants = null;
00029 
00037     public function init( $oArticles )
00038     {
00039         $this->_oArticles = $oArticles;
00040     }
00041 
00050     public function buildMdVariants( $oVariants, $sParentId )
00051     {
00052         $oMdVariants = oxNew( "OxMdVariant" );
00053         $oMdVariants->setParentId( $sParentId );
00054         $oMdVariants->setName( "_parent_product_" );
00055         foreach ( $oVariants as $sKey => $oVariant ) {
00056             $aNames = explode( trim( $this->_sMdSeparator ), $oVariant->oxarticles__oxvarselect->value );
00057             foreach ( $aNames as $sNameKey => $sName ) {
00058                 $aNames[$sNameKey] = trim($sName);
00059             }
00060             $oMdVariants->addNames( $sKey,
00061                                     $aNames,
00062                                     $oVariant->getPrice()->getBruttoPrice(),
00063                                     $oVariant->getLink() );
00064         }
00065 
00066         return $oMdVariants;
00067     }
00068 
00077     public function genVariantFromSell( $aSels, $oArticle )
00078     {
00079         $oVariants = $oArticle->getAdminVariants();
00080         $myConfig  = $this->getConfig();
00081         $myUtils   = oxUtils::getInstance();
00082         $myLang    = oxLang::getInstance();
00083         $aConfLanguages = $myLang->getLanguageIds();
00084 
00085         foreach ($aSels as $sSelId) {
00086             $oSel = oxNew("oxbase");
00087             $oSel->init( 'oxselectlist' );
00088             $oSel->load( $sSelId );
00089             $sVarNameUpdate = "";
00090             foreach ($aConfLanguages as $sKey => $sLang) {
00091                 $sPrefix = $myLang->getLanguageTag($sKey);
00092                 $aSelValues = $myUtils->assignValuesFromText($oSel->{"oxselectlist__oxvaldesc".$sPrefix}->value );
00093                 foreach ($aSelValues as $sI => $oValue ) {
00094                     $aValues[$sI][$sKey] = $oValue;
00095                 }
00096                 $aSelTitle[$sKey] = $oSel->{"oxselectlist__oxtitle".$sPrefix}->value;
00097                 $sMdSeparator = ($oArticle->oxarticles__oxvarname->value) ? $this->_sMdSeparator: '';
00098                 if ( $sVarNameUpdate ) {
00099                     $sVarNameUpdate .= ", ";
00100                 }
00101                 $sVarName = oxDb::getDb()->quote($sMdSeparator.$aSelTitle[$sKey]);
00102                 $sVarNameUpdate .= "oxvarname".$sPrefix." = CONCAT(oxvarname".$sPrefix.", ".$sVarName.")";
00103             }
00104             $oMDVariants = $this->_assignValues( $aValues, $oVariants, $oArticle, $aConfLanguages);
00105             if ( $myConfig->getConfigParam( 'blUseMultidimensionVariants' ) ) {
00106                 $oAttribute = oxNew("oxattribute");
00107                 $oAttribute->assignVarToAttribute( $oMDVariants, $aSelTitle );
00108             }
00109             $this->_updateArticleVarName( $sVarNameUpdate, $oArticle->oxarticles__oxid->value );
00110         }
00111     }
00112 
00123     protected function _assignValues( $aValues, $oVariants, $oArticle, $aConfLanguages)
00124     {
00125         $myConfig = $this->getConfig();
00126         $myLang    = oxLang::getInstance();
00127         $iCounter = 0;
00128         $aVarselect = array(); //multilanguage names of existing variants
00129         //iterating through all select list values (eg. $oValue->name = S, M, X, XL)
00130         for ( $i=0; $i<count($aValues); $i++ ) {
00131             $oValue = $aValues[$i][0];
00132             $dPriceMod = $this->_getValuePrice( $oValue, $oArticle->oxarticles__oxprice->value);
00133             if ( $oVariants->count() > 0 ) {
00134                 //if we have any existing variants then copying each variant with $oValue->name
00135                 foreach ( $oVariants as $oSimpleVariant ) {
00136                     if ( !$iCounter ) {
00137                         //we just update the first variant
00138                         //cannot use setEnableMultilang() at this place
00139                         //$oVariant = oxNew("oxarticle");
00140                         //$oVariant->setEnableMultilang(false);
00141                         $oVariant = oxNew("oxarticle");
00142                         $oVariant->setEnableMultilang(false);
00143                         $oVariant->load($oSimpleVariant->oxarticles__oxid->value);
00144                         $oVariant->oxarticles__oxprice->setValue( $oVariant->oxarticles__oxprice->value + $dPriceMod );
00145                         //assign for all languages
00146                         foreach ( $aConfLanguages as $sKey => $sLang ) {
00147                             $oValue = $aValues[$i][$sKey];
00148                             $sPrefix = $myLang->getLanguageTag($sKey);
00149                             $aVarselect[$oSimpleVariant->oxarticles__oxid->value][$sKey] = $oVariant->{"oxarticles__oxvarselect".$sPrefix}->value;
00150                             $oVariant->{'oxarticles__oxvarselect'.$sPrefix}->setValue($oVariant->{"oxarticles__oxvarselect".$sPrefix}->value.$this->_sMdSeparator.$oValue->name);
00151                         }
00152                         $oVariant->oxarticles__oxsort->setValue($oVariant->oxarticles__oxsort->value * 10);
00153                         $oVariant->save();
00154                         $sVarId = $oSimpleVariant->oxarticles__oxid->value;
00155                     } else {
00156                         //we create new variants
00157                         foreach ($aVarselect[$oSimpleVariant->oxarticles__oxid->value] as $sKey => $sVarselect) {
00158                             $oValue = $aValues[$i][$sKey];
00159                             $sPrefix = $myLang->getLanguageTag($sKey);
00160                             $aParams['oxarticles__oxvarselect'.$sPrefix] = $sVarselect.$this->_sMdSeparator.$oValue->name;
00161                         }
00162                         $aParams['oxarticles__oxartnum'] = $oSimpleVariant->oxarticles__oxartnum->value . "-" . $iCounter;
00163                         $aParams['oxarticles__oxprice'] = $oSimpleVariant->oxarticles__oxprice->value + $dPriceMod;
00164                         $aParams['oxarticles__oxsort'] = $oSimpleVariant->oxarticles__oxsort->value*10 + 10*$iCounter;
00165                         $aParams['oxarticles__oxstock'] = 0;
00166                         $aParams['oxarticles__oxstockflag'] = $oSimpleVariant->oxarticles__oxstockflag->value;
00167                         $sVarId = $this->_craeteNewVariant( $aParams, $oArticle->oxarticles__oxid->value );
00168                         if ( $myConfig->getConfigParam( 'blUseMultidimensionVariants' ) ) {
00169                             $oAttrList = oxNew('oxattribute');
00170                             $aIds = $oAttrList->getAttributeAssigns( $oSimpleVariant->oxarticles__oxid->value);
00171                             $aMDVariants["mdvar_".$sVarId] = $aIds;
00172                         }
00173                     }
00174                     if ( $myConfig->getConfigParam( 'blUseMultidimensionVariants' ) ) {
00175                         $aMDVariants[$sVarId] = $aValues[$i];
00176                     }
00177                 }
00178                 $iCounter++;
00179             } else {
00180                 //in case we don't have any variants then we just create variant(s) with $oValue->name
00181                 $iCounter++;
00182                 foreach ($aConfLanguages as $sKey => $sLang) {
00183                     $oValue = $aValues[$i][$sKey];
00184                     $sPrefix = $myLang->getLanguageTag($sKey);
00185                     $aParams['oxarticles__oxvarselect'.$sPrefix] = $oValue->name;
00186                 }
00187                 $aParams['oxarticles__oxartnum'] = $oArticle->oxarticles__oxartnum->value . "-" . $iCounter ;
00188                 $aParams['oxarticles__oxprice'] = $oArticle->oxarticles__oxprice->value + $dPriceMod;
00189                 $aParams['oxarticles__oxsort'] = 5000 + $iCounter * 1000;
00190                 $aParams['oxarticles__oxstock'] = 0;
00191                 $aParams['oxarticles__oxstockflag'] = $oArticle->oxarticles__oxstockflag->value;
00192                 $sVarId = $this->_craeteNewVariant( $aParams, $oArticle->oxarticles__oxid->value );
00193                 if ( $myConfig->getConfigParam( 'blUseMultidimensionVariants' ) ) {
00194                     $aMDVariants[$sVarId] = $aValues[$i];
00195                 }
00196             }
00197         }
00198         return $aMDVariants;
00199     }
00200 
00209     protected function _getValuePrice( $oValue, $dParentPrice)
00210     {
00211         $myConfig = $this->getConfig();
00212         $dPriceMod = 0;
00213         if ( $myConfig->getConfigParam( 'bl_perfLoadSelectLists' ) && $myConfig->getConfigParam( 'bl_perfUseSelectlistPrice' ) ) {
00214             if ($oValue->priceUnit == 'abs') {
00215                 $dPriceMod = $oValue->price;
00216             } elseif ($oValue->priceUnit == '%') {
00217                 $dPriceModPerc = abs($oValue->price)*$dParentPrice/100.0;
00218                 if (($oValue->price) >= 0.0) {
00219                     $dPriceMod = $dPriceModPerc;
00220                 } else {
00221                     $dPriceMod = -$dPriceModPerc;
00222                 }
00223             }
00224         }
00225         return $dPriceMod;
00226     }
00227 
00236     protected function _craeteNewVariant( $aParams = null, $sParentId = null)
00237     {
00238         // checkbox handling
00239         $aParams['oxarticles__oxactive'] = 0;
00240 
00241             // shopid
00242             $sShopID = oxSession::getVar( "actshop");
00243             $aParams['oxarticles__oxshopid'] = $sShopID;
00244 
00245         // varianthandling
00246         $aParams['oxarticles__oxparentid'] = $sParentId;
00247 
00248         $oArticle = oxNew("oxbase");
00249         $oArticle->init( 'oxarticles' );
00250         $oArticle->assign( $aParams);
00251 
00252             //echo $aParams['oxarticles__oxartnum']."---";
00253             $oArticle->save();
00254 
00255         return $oArticle->getId();
00256     }
00257 
00266     protected function _updateArticleVarName( $sUpdate, $sArtId )
00267     {
00268         $sUpdate = "update oxarticles set " . $sUpdate . " where oxid = '" . $sArtId . "'";
00269 
00270         oxDb::getDb()->Execute( $sUpdate);
00271     }
00272 
00280     public function isMdVariant( $oArticle )
00281     {
00282         if ( $this->getConfig()->getConfigParam( 'blUseMultidimensionVariants' ) ) {
00283             if ( strpos( $oArticle->oxarticles__oxvarselect->value, trim($this->_sMdSeparator) ) !== false ) {
00284                 return true;
00285             }
00286         }
00287 
00288         return false;
00289     }
00290 
00291 }