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                         $oVariant = oxNew("oxarticle");
00139                         $oVariant->setEnableMultilang(false);
00140                         $oVariant->load($oSimpleVariant->oxarticles__oxid->value);
00141                         $oVariant->oxarticles__oxprice->setValue( $oVariant->oxarticles__oxprice->value + $dPriceMod );
00142                         //assign for all languages
00143                         foreach ( $aConfLanguages as $sKey => $sLang ) {
00144                             $oValue = $aValues[$i][$sKey];
00145                             $sPrefix = $myLang->getLanguageTag($sKey);
00146                             $aVarselect[$oSimpleVariant->oxarticles__oxid->value][$sKey] = $oVariant->{"oxarticles__oxvarselect".$sPrefix}->value;
00147                             $oVariant->{'oxarticles__oxvarselect'.$sPrefix}->setValue($oVariant->{"oxarticles__oxvarselect".$sPrefix}->value.$this->_sMdSeparator.$oValue->name);
00148                         }
00149                         $oVariant->oxarticles__oxsort->setValue($oVariant->oxarticles__oxsort->value * 10);
00150                         $oVariant->save();
00151                         $sVarId = $oSimpleVariant->oxarticles__oxid->value;
00152                     } else {
00153                         //we create new variants
00154                         foreach ($aVarselect[$oSimpleVariant->oxarticles__oxid->value] as $sKey => $sVarselect) {
00155                             $oValue = $aValues[$i][$sKey];
00156                             $sPrefix = $myLang->getLanguageTag($sKey);
00157                             $aParams['oxarticles__oxvarselect'.$sPrefix] = $sVarselect.$this->_sMdSeparator.$oValue->name;
00158                         }
00159                         $aParams['oxarticles__oxartnum'] = $oSimpleVariant->oxarticles__oxartnum->value . "-" . $iCounter;
00160                         $aParams['oxarticles__oxprice'] = $oSimpleVariant->oxarticles__oxprice->value + $dPriceMod;
00161                         $aParams['oxarticles__oxsort'] = $oSimpleVariant->oxarticles__oxsort->value*10 + 10*$iCounter;
00162                         $aParams['oxarticles__oxstock'] = 0;
00163                         $aParams['oxarticles__oxstockflag'] = $oSimpleVariant->oxarticles__oxstockflag->value;
00164                         $sVarId = $this->_craeteNewVariant( $aParams, $oArticle->oxarticles__oxid->value );
00165                         if ( $myConfig->getConfigParam( 'blUseMultidimensionVariants' ) ) {
00166                             $oAttrList = oxNew('oxattribute');
00167                             $aIds = $oAttrList->getAttributeAssigns( $oSimpleVariant->oxarticles__oxid->value);
00168                             $aMDVariants["mdvar_".$sVarId] = $aIds;
00169                         }
00170                     }
00171                     if ( $myConfig->getConfigParam( 'blUseMultidimensionVariants' ) ) {
00172                         $aMDVariants[$sVarId] = $aValues[$i];
00173                     }
00174                 }
00175                 $iCounter++;
00176             } else {
00177                 //in case we don't have any variants then we just create variant(s) with $oValue->name
00178                 $iCounter++;
00179                 foreach ($aConfLanguages as $sKey => $sLang) {
00180                     $oValue = $aValues[$i][$sKey];
00181                     $sPrefix = $myLang->getLanguageTag($sKey);
00182                     $aParams['oxarticles__oxvarselect'.$sPrefix] = $oValue->name;
00183                 }
00184                 $aParams['oxarticles__oxartnum'] = $oArticle->oxarticles__oxartnum->value . "-" . $iCounter ;
00185                 $aParams['oxarticles__oxprice'] = $oArticle->oxarticles__oxprice->value + $dPriceMod;
00186                 $aParams['oxarticles__oxsort'] = 5000 + $iCounter * 1000;
00187                 $aParams['oxarticles__oxstock'] = 0;
00188                 $aParams['oxarticles__oxstockflag'] = $oArticle->oxarticles__oxstockflag->value;
00189                 $sVarId = $this->_craeteNewVariant( $aParams, $oArticle->oxarticles__oxid->value );
00190                 if ( $myConfig->getConfigParam( 'blUseMultidimensionVariants' ) ) {
00191                     $aMDVariants[$sVarId] = $aValues[$i];
00192                 }
00193             }
00194         }
00195         return $aMDVariants;
00196     }
00197 
00206     protected function _getValuePrice( $oValue, $dParentPrice)
00207     {
00208         $myConfig = $this->getConfig();
00209         $dPriceMod = 0;
00210         if ( $myConfig->getConfigParam( 'bl_perfLoadSelectLists' ) && $myConfig->getConfigParam( 'bl_perfUseSelectlistPrice' ) ) {
00211             if ($oValue->priceUnit == 'abs') {
00212                 $dPriceMod = $oValue->price;
00213             } elseif ($oValue->priceUnit == '%') {
00214                 $dPriceModPerc = abs($oValue->price)*$dParentPrice/100.0;
00215                 if (($oValue->price) >= 0.0) {
00216                     $dPriceMod = $dPriceModPerc;
00217                 } else {
00218                     $dPriceMod = -$dPriceModPerc;
00219                 }
00220             }
00221         }
00222         return $dPriceMod;
00223     }
00224 
00233     protected function _craeteNewVariant( $aParams = null, $sParentId = null)
00234     {
00235         // checkbox handling
00236         $aParams['oxarticles__oxactive'] = 0;
00237 
00238             // shopid
00239             $sShopID = oxSession::getVar( "actshop");
00240             $aParams['oxarticles__oxshopid'] = $sShopID;
00241 
00242         // varianthandling
00243         $aParams['oxarticles__oxparentid'] = $sParentId;
00244 
00245         $oArticle = oxNew("oxbase");
00246         $oArticle->init( 'oxarticles' );
00247         $oArticle->assign( $aParams);
00248 
00249             //echo $aParams['oxarticles__oxartnum']."---";
00250             $oArticle->save();
00251 
00252         return $oArticle->getId();
00253     }
00254 
00263     protected function _updateArticleVarName( $sUpdate, $sArtId )
00264     {
00265         $sUpdate = "update oxarticles set " . $sUpdate . " where oxid = '" . $sArtId . "'";
00266 
00267         oxDb::getDb()->Execute( $sUpdate);
00268     }
00269 
00277     public function isMdVariant( $oArticle )
00278     {
00279         if ( $this->getConfig()->getConfigParam( 'blUseMultidimensionVariants' ) ) {
00280             if ( strpos( $oArticle->oxarticles__oxvarselect->value, trim($this->_sMdSeparator) ) !== false ) {
00281                 return true;
00282             }
00283         }
00284 
00285         return false;
00286     }
00287 
00288 }