oxarticle.php

Go to the documentation of this file.
00001 <?php
00002 
00003 // defining supported link types
00004 define( 'OXARTICLE_LINKTYPE_CATEGORY', 0 );
00005 define( 'OXARTICLE_LINKTYPE_VENDOR', 1 );
00006 define( 'OXARTICLE_LINKTYPE_MANUFACTURER', 2 );
00007 define( 'OXARTICLE_LINKTYPE_PRICECATEGORY', 3 );
00008 define( 'OXARTICLE_LINKTYPE_TAG', 4 );
00009 define( 'OXARTICLE_LINKTYPE_RECOMM', 5 );
00010 
00017 class oxArticle extends oxI18n implements oxIArticle, oxIUrl
00018 {
00024     protected $_sCoreTbl = 'oxarticles';
00025 
00031     protected $_sClassName = 'oxarticle';
00032 
00038     protected $_blUseLazyLoading = true;
00039 
00045     protected $_sItemKey;
00046 
00052     protected $_blCalcPrice    = true;
00053 
00058     protected $_oPrice      = null;
00059 
00065     protected $_dArticleVat = null;
00066 
00072     protected $_aPersistParam  = null;
00073 
00079     protected $_blNotBuyable   = false;
00080 
00087     protected $_blLoadVariants = true;
00088 
00094     protected $_aVariants = null;
00095 
00101     protected $_aVariantsWithNotOrderables = null;
00102 
00111     protected $_blNotBuyableParent  = false;
00112 
00116     protected $_blHasVariants = false;
00117 
00121     protected $_blHasMdVariants = false;
00122 
00127     protected $_blIsOnComparisonList = false;
00128 
00133     protected $_oUser = null;
00134 
00140     protected $_blLoadPrice = true;
00141 
00145     protected $_blSkipAbPrice = false;
00146 
00153     protected $_fPricePerUnit = null;
00154 
00158     protected $_blLoadParentData = false;
00159 
00163     protected $_blSkipAssign = false;
00164 
00170     protected $_blSkipDiscounts = null;
00171 
00176     protected $_oAttributeList = null;
00177 
00178 
00184     protected $_blIsRangePrice = false;
00185 
00191     protected $_aMediaUrls = null;
00192 
00198     static protected $_aLoadedParents;
00199 
00205     static protected $_aSelList;
00206 
00212     protected $_aDispSelList;
00213 
00219     protected $_blIsSeoObject = true;
00220 
00226     protected $_oAmountPriceList = null;
00227 
00236     protected $_iLinkType = 0;
00237 
00243     protected $_aStdUrls = array();
00244 
00250     protected $_aSeoUrls = array();
00251 
00257     protected $_aSeoAddParams = array();
00258 
00264     protected $_aStdAddParams = array();
00265 
00271     protected $_sDynImageDir = null;
00272 
00278     protected $_sMoreDetailLink = null;
00279 
00285     protected $_sToBasketLink = null;
00286 
00292     protected $_iStockStatus = null;
00293 
00299     protected $_oTPrice = null;
00300 
00306     protected $_oAmountPriceInfo = null;
00307 
00313     protected $_dAmountPrice = null;
00314 
00320     protected static $_aArticleManufacturers = array();
00321 
00327     protected static $_aArticleVendors = array();
00328 
00334     protected static $_aArticleCats = array();
00335 
00341     protected $_aNonCopyParentFields = array('oxarticles__oxinsert',
00342                                              'oxarticles__oxtimestamp',
00343                                              'oxarticles__oxnid',
00344                                              'oxarticles__oxid',
00345                                              'oxarticles__oxparentid');
00346 
00352     protected $_aCopyParentField = array('oxarticles__oxnonmaterial',
00353                                          'oxarticles__oxfreeshipping',
00354                                          'oxarticles__oxremindactive');
00355 
00361     protected $_oMdVariants = null;
00362 
00371     public function __construct($aParams = null)
00372     {
00373         if ( $aParams && is_array($aParams)) {
00374             foreach ( $aParams as $sParam => $mValue) {
00375                 $this->$sParam = $mValue;
00376             }
00377         }
00378         parent::__construct();
00379         $this->init( 'oxarticles' );
00380 
00381         $this->_blIsRangePrice = false;
00382     }
00383 
00391     public function __isset( $sName )
00392     {
00393         if ( $sName == 'oxarticles__oxlongdesc' ) {
00394             //get empty oxlongdesc field
00395             $this->getArticleLongDesc();
00396             return true;
00397         }
00398         return isset( $this->$sName );
00399     }
00400 
00409     public function __get($sName)
00410     {
00411         $myUtils = oxUtils::getInstance();
00412         switch ($sName) {
00413             // NOT using caching here, because of these params should be used in templates ONLY
00414             // and in template files they are used not very much [once most of the time]
00415 
00416             // price related
00417             case 'netprice':
00418             case 'netPrice':
00419                 $mVal = $myUtils->fRound($this->getPrice()->getNettoPrice());
00420                 break;
00421             case 'brutPrice':
00422                 return $myUtils->fRound($this->getPrice()->getBruttoPrice());
00423                 break;
00424             case 'vatPercent':
00425                 return $this->getPrice()->getVAT();
00426                 break;
00427             case 'vat':
00428                 return $this->getPrice()->getVATValue();
00429                 break;
00430             case 'fnetprice':
00431                 return oxLang::getInstance()->formatCurrency( $myUtils->fRound($this->getPrice()->getNettoPrice()));
00432                 break;
00433             case 'fprice':
00434                 return $this->getFPrice();
00435                 break;
00436 
00437             // t price related
00438             case 'dtprice':
00439                 if ( $oPrice = $this->getTPrice() ) {
00440                     return $myUtils->fRound($oPrice->getBruttoPrice());
00441                     break;
00442                 } else {
00443                     return null;
00444                     break;
00445                 }
00446             case 'tvat':
00447                 if ( $oPrice = $this->getTPrice() ) {
00448                     return $oPrice->getVATValue();
00449                     break;
00450                 } else {
00451                     return null;
00452                     break;
00453                 }
00454             case 'ftprice':
00455                 if ( $oPrice = $this->getTPrice() ) {
00456                     return oxLang::getInstance()->formatCurrency( $myUtils->fRound($oPrice->getBruttoPrice()) );
00457                     break;
00458                 } else {
00459                     return null;
00460                     break;
00461                 }
00462             case 'oxarticles__oxlongdesc':
00463                 return $this->getArticleLongDesc($this->getId());
00464                 break;
00465             case 'foxdelivery':
00466                 return $this->getDeliveryDate();
00467                 break;
00468             case 'sItemKey':
00469                 return $this->getItemKey();
00470                 break;
00471             case 'selectlist':
00472                 return $this->_aDispSelList = $this->getDispSelList();
00473                 break;
00474             case 'blNotBuyable':
00475                 return $this->isNotBuyable();
00476                 break;
00477             case 'blNotBuyableParent':
00478                 return $this->isParentNotBuyable();
00479                 break;
00480             case 'blIsOnComparisonList':
00481                 return $this->isOnComparisonList();
00482                 break;
00483             case 'oVariantlist' :
00484                 return $this->getVariants();
00485                 break;
00486             case 'fPricePerUnit' :
00487                 return $this->getPricePerUnit();
00488                 break;
00489             case 'dimagedir' :
00490                 return $this->_sDynImageDir = $this->getDynImageDir();
00491                 break;
00492             case 'oxmoredetaillink' :
00493                 return $this->_sMoreDetailLink = $this->getMoreDetailLink();
00494                 break;
00495             case 'amountpricelist' :
00496                 return $this->loadAmountPriceInfo();
00497                 break;
00498             case 'stockstatus' :
00499                 return $this->getStockStatus();
00500                 break;
00501             case 'tobasketlink' :
00502                 return $this->getToBasketLink();
00503                 break;
00504             case 'oxdetaillink' :
00505                 return $this->getLink();
00506                 break;
00507             case 'aSelectlist' :
00508                 if ( $this->getConfig()->getConfigParam( 'bl_perfLoadSelectLists' ) ) {
00509                     return $this->aSelectlist = $this->getSelectLists();
00510                 }
00511                 return;
00512                 break;
00513             /*case 'oxarticles__oxnid':
00514                 return $this->getId();*/
00515         }
00516 
00517         $this->$sName = parent::__get($sName);
00518         if ( $this->$sName ) {
00519             $this->_assignParentFieldValue($sName);
00520         }
00521 
00522         //checking for picture information
00523         if ($sName == "oxarticles__oxthumb" || $sName == "oxarticles__oxicon" || (strpos($sName, "oxarticles__oxpic") === 0 && $sName != "oxarticles__oxpicsgenerated") || strpos($sName, "oxarticles__oxzoom") === 0) {
00524             $this->_assignPictureValues( $sName );
00525             return $this->$sName;
00526         }
00527 
00528         return $this->$sName;
00529     }
00530 
00538     public function setId( $sId = null )
00539     {
00540         $sId = parent::setId( $sId );
00541 
00542         // TODO: in oxbase::setId make it to check if exists and update, not recreate, then delete this overload
00543         $this->oxarticles__oxnid = $this->oxarticles__oxid;
00544 
00545         return $sId;
00546     }
00547 
00555     public function getTableNameForActiveSnippet( $blForceCoreTable = false )
00556     {
00557             $sTable = $this->getCoreTableName();
00558 
00559         return $sTable;
00560     }
00561 
00571     public function getActiveCheckQuery( $blForceCoreTable = false )
00572     {
00573         $sTable = $this->getTableNameForActiveSnippet( $blForceCoreTable );
00574 
00575         // check if article is still active
00576         $sQ = " $sTable.oxactive = 1 ";
00577 
00578         // enabled time range check ?
00579         if ( $this->getConfig()->getConfigParam( 'blUseTimeCheck' ) ) {
00580             $sDate = date( 'Y-m-d H:i:s', oxUtilsDate::getInstance()->getTime() );
00581             $sQ = "( $sQ or ( $sTable.oxactivefrom < '$sDate' and $sTable.oxactiveto > '$sDate' ) ) ";
00582         }
00583 
00584         return $sQ;
00585     }
00586 
00600     public function getStockCheckQuery( $blForceCoreTable = false )
00601     {
00602         $myConfig = $this->getConfig();
00603         $sTable = $this->getTableNameForActiveSnippet( $blForceCoreTable );
00604 
00605         $sQ = "";
00606 
00607         //do not check for variants
00608         if ( $myConfig->getConfigParam( 'blUseStock' ) ) {
00609             $sQ = " and ( $sTable.oxstockflag != 2 or ( $sTable.oxstock + $sTable.oxvarstock ) > 0  ) ";
00610             //V #M513: When Parent article is not purchaseble, it's visibility should be displayed in shop only if any of Variants is available.
00611             if ( !$myConfig->getConfigParam( 'blVariantParentBuyable' ) ) {
00612                 $sTimeCheckQ = '';
00613                 if ( $myConfig->getConfigParam( 'blUseTimeCheck' ) ) {
00614                      $sDate = date( 'Y-m-d H:i:s', oxUtilsDate::getInstance()->getTime() );
00615                      $sTimeCheckQ = " or ( art.oxactivefrom < '$sDate' and art.oxactiveto > '$sDate' )";
00616                 }
00617                 $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 ) ) ";
00618             }
00619         }
00620 
00621         return $sQ;
00622     }
00623 
00635     public function getVariantsQuery( $blRemoveNotOrderables, $blForceCoreTable = false  )
00636     {
00637         $sTable = $this->getTableNameForActiveSnippet( $blForceCoreTable );
00638         $sQ = " and $sTable.oxparentid = '".$this->getId()."' ";
00639 
00640         //checking if variant is active and stock status
00641         if ( $this->getConfig()->getConfigParam( 'blUseStock' ) ) {
00642             $sQ .= " and ( $sTable.oxstock > 0 or ( $sTable.oxstock <= 0 and $sTable.oxstockflag != 2 ";
00643             if ( $blRemoveNotOrderables ) {
00644                 $sQ .= " and $sTable.oxstockflag != 3 ";
00645             }
00646             $sQ .= " ) ) ";
00647         }
00648 
00649         return $sQ;
00650     }
00651 
00659     public function getSqlActiveSnippet( $blForceCoreTable = false )
00660     {
00661         $myConfig = $this->getConfig();
00662 
00663         // check if article is still active
00664         $sQ = $this->getActiveCheckQuery( $blForceCoreTable );
00665 
00666         // stock and variants check
00667         $sQ .= $this->getStockCheckQuery( $blForceCoreTable );
00668 
00669 
00670         return "( $sQ ) ";
00671     }
00672 
00680     public function setSkipAssign($blSkipAssign)
00681     {
00682         $this->_blSkipAssign = $blSkipAssign;
00683     }
00684 
00692     public function disablePriceLoad( $oArticle )
00693     {
00694         $oArticle->_blLoadPrice = false;
00695     }
00696 
00702     public function getItemKey()
00703     {
00704         return $this->_sItemKey;
00705     }
00706 
00714     public function setItemKey($sItemKey)
00715     {
00716         $this->_sItemKey = $sItemKey;
00717     }
00718 
00726     public function setNoVariantLoading( $blLoadVariants )
00727     {
00728         $this->_blLoadVariants = !$blLoadVariants;
00729     }
00730 
00736     public function isBuyable()
00737     {
00738         if ($this->_blNotBuyableParent) {
00739             return false;
00740         }
00741 
00742         return !$this->_blNotBuyable;
00743     }
00744 
00750     public function getPersParams()
00751     {
00752         return $this->_aPersistParam;
00753     }
00754 
00760     public function isOnComparisonList()
00761     {
00762         return $this->_blIsOnComparisonList;
00763     }
00764 
00772     public function setOnComparisonList( $blOnList )
00773     {
00774         $this->_blIsOnComparisonList = $blOnList;
00775     }
00776 
00784     public function setLoadParentData($blLoadParentData)
00785     {
00786         $this->_blLoadParentData = $blLoadParentData;
00787     }
00788 
00796     public function setSkipAbPrice( $blSkipAbPrice = null )
00797     {
00798         $this->_blSkipAbPrice = $blSkipAbPrice;
00799     }
00800 
00806     public function getSearchableFields()
00807     {
00808         $aSkipFields = array("oxblfixedprice", "oxicon", "oxvarselect", "oxamitemid", "oxamtaskid", "oxpixiexport", "oxpixiexported") ;
00809         $aFields = array_diff( array_keys($this->_aFieldNames), $aSkipFields );
00810 
00811         return $aFields;
00812     }
00813 
00814 
00822     public function isMultilingualField($sFieldName)
00823     {
00824         switch ($sFieldName) {
00825             case "oxlongdesc":
00826             case "oxtags":
00827                 return true;
00828         }
00829 
00830         return parent::isMultilingualField($sFieldName);
00831     }
00832 
00838     public function isVisible()
00839     {
00840 
00841         // admin preview mode
00842         $myConfig  = $this->getConfig();
00843         if ( ( $sPrevId = oxConfig::getParameter( 'preview' ) ) &&
00844              ( $sAdminSid = oxUtilsServer::getInstance()->getOxCookie( 'admin_sid' ) ) ) {
00845 
00846             $oDb = oxDb::getDb();
00847             $sPrevId   = $oDb->quote( $sPrevId );
00848             $sAdminSid = $oDb->quote( $sAdminSid );
00849             $sTable    = getViewName( 'oxuser' );
00850 
00851             return (bool) $oDb->getOne( "select 1 from $sTable where MD5( CONCAT( {$sAdminSid}, {$sTable}.oxid, {$sTable}.oxpassword, {$sTable}.oxrights ) ) = $sPrevId" );
00852         }
00853 
00854         // active ?
00855         $sNow = date('Y-m-d H:i:s');
00856         if ( !$this->oxarticles__oxactive->value &&
00857              (  $this->oxarticles__oxactivefrom->value > $sNow ||
00858                 $this->oxarticles__oxactiveto->value < $sNow
00859              )) {
00860             return false;
00861         }
00862 
00863         // stock flags
00864         if ( $myConfig->getConfigParam( 'blUseStock' ) && $this->oxarticles__oxstockflag->value == 2) {
00865             $iOnStock = $this->oxarticles__oxstock->value + $this->oxarticles__oxvarstock->value;
00866             if ($this->getConfig()->getConfigParam( 'blPsBasketReservationEnabled' )) {
00867                 $iOnStock += $this->getSession()->getBasketReservations()->getReservedAmount($this->getId());
00868             }
00869             if ( $iOnStock <= 0 ) {
00870                 return false;
00871             }
00872         }
00873 
00874         return true;
00875     }
00876 
00885     public function assign( $aRecord)
00886     {
00887         startProfile('articleAssign');
00888 
00889 
00890         // load object from database
00891         parent::assign( $aRecord);
00892 
00893         $this->oxarticles__oxnid = $this->oxarticles__oxid;
00894 
00895         // check for simple article.
00896         if ($this->_blSkipAssign) {
00897             return;
00898         }
00899 
00900         $this->_assignParentFieldValues();
00901         $this->_assignNotBuyableParent();
00902 
00903         $this->_assignAllPictureValues();
00904 
00905         $this->_assignStock();
00906         startProfile('articleAssignPrices');
00907         $this->_assignPrices();
00908         stopProfile('articleAssignPrices');
00909         $this->_assignPersistentParam();
00910         $this->_assignDynImageDir();
00911         $this->_assignComparisonListFlag();
00912         $this->_assignAttributes();
00913 
00914 
00915         //$this->_seoAssign();
00916 
00917         stopProfile('articleAssign');
00918     }
00919 
00930     public function load( $oxID)
00931     {
00932         // A. #1325 resetting to avoid problems when reloading (details etc)
00933         $this->_blNotBuyableParent = false;
00934 
00935         $blRet = parent::load( $oxID);
00936 
00937         // convert date's to international format
00938         $this->oxarticles__oxinsert    = new oxField(oxUtilsDate::getInstance()->formatDBDate( $this->oxarticles__oxinsert->value));
00939         $this->oxarticles__oxtimestamp = new oxField(oxUtilsDate::getInstance()->formatDBDate( $this->oxarticles__oxtimestamp->value));
00940 
00941         return $blRet;
00942     }
00943 
00951     public function addToRatingAverage( $iRating)
00952     {
00953         $dOldRating = $this->oxarticles__oxrating->value;
00954         $dOldCnt    = $this->oxarticles__oxratingcnt->value;
00955         $this->oxarticles__oxrating->setValue(( $dOldRating * $dOldCnt + $iRating ) / ($dOldCnt + 1));
00956         $this->oxarticles__oxratingcnt->setValue($dOldCnt + 1);
00957         $dRating = ( $dOldRating * $dOldCnt + $iRating ) / ($dOldCnt + 1);
00958         $dRatingCnt = (int) ($dOldCnt + 1);
00959         // oxarticles.oxtimestamp = oxarticles.oxtimestamp to keep old timestamp value
00960         oxDb::getDb()->execute( 'update oxarticles set oxarticles.oxrating = '.$dRating.',oxarticles.oxratingcnt = '.$dRatingCnt.', oxarticles.oxtimestamp = oxarticles.oxtimestamp where oxarticles.oxid = "'.$this->getId().'" ' );
00961     }
00962 
00968     public function getArticleRatingAverage()
00969     {
00970         return round( $this->oxarticles__oxrating->value, 1);
00971     }
00972 
00978     public function getReviews()
00979     {
00980         $myConfig  = $this->getConfig();
00981 
00982         $aIds = array($this->getId());
00983 
00984         if ( $this->oxarticles__oxparentid->value ) {
00985                 $aIds[] = $this->oxarticles__oxparentid->value;
00986         }
00987 
00988         // showing variant reviews ..
00989         if ( $myConfig->getConfigParam( 'blShowVariantReviews' ) ) {
00990             $aAdd = $this->_getVariantsIds();
00991             if (is_array($aAdd)) {
00992                 $aIds = array_merge($aIds, $aAdd);
00993             }
00994         }
00995 
00996         $oReview = oxNew('oxreview');
00997         $oRevs = $oReview->loadList('oxarticle', $aIds);
00998 
00999         //if no review found, return null
01000         if ( $oRevs->count() < 1 ) {
01001             return null;
01002         }
01003 
01004         return $oRevs;
01005     }
01006 
01012     public function getCrossSelling()
01013     {
01014         $oCrosslist = oxNew( "oxarticlelist");
01015         $oCrosslist->loadArticleCrossSell($this->oxarticles__oxid->value);
01016         if ( $oCrosslist->count() ) {
01017             return $oCrosslist;
01018         }
01019     }
01020 
01026     public function getAccessoires()
01027     {
01028         $myConfig = $this->getConfig();
01029 
01030         // Performance
01031         if ( !$myConfig->getConfigParam( 'bl_perfLoadAccessoires' ) ) {
01032             return;
01033         }
01034 
01035         $oAcclist = oxNew( "oxarticlelist");
01036         $oAcclist->setSqlLimit( 0, $myConfig->getConfigParam( 'iNrofCrossellArticles' ));
01037         $oAcclist->loadArticleAccessoires($this->oxarticles__oxid->value);
01038 
01039         if ( $oAcclist->count()) {
01040             return $oAcclist;
01041         }
01042     }
01043 
01049     public function getSimilarProducts()
01050     {
01051         // Performance
01052         $myConfig = $this->getConfig();
01053         if ( !$myConfig->getConfigParam( 'bl_perfLoadSimilar' ) ) {
01054             return;
01055         }
01056 
01057         $sArticleTable = $this->_getObjectViewName('oxarticles');
01058 
01059         $sAttribs = '';
01060         $iCnt = 0;
01061         $this->_getAttribsString($sAttribs, $iCnt);
01062 
01063         if ( !$sAttribs) {
01064             return null;
01065         }
01066 
01067         $aList = $this->_getSimList($sAttribs, $iCnt);
01068 
01069         if ( count( $aList ) ) {
01070             uasort( $aList, 'cmpart');
01071 
01072             $sSearch = $this->_generateSimListSearchStr($sArticleTable, $aList);
01073 
01074             $oSimilarlist = oxNew( 'oxarticlelist' );
01075             $oSimilarlist->setSqlLimit( 0, $myConfig->getConfigParam( 'iNrofSimilarArticles' ));
01076             $oSimilarlist->selectString( $sSearch);
01077 
01078             return $oSimilarlist;
01079         }
01080     }
01081 
01087     public function getCustomerAlsoBoughtThisProducts()
01088     {
01089         // Performance
01090         $myConfig = $this->getConfig();
01091         if ( !$myConfig->getConfigParam( 'bl_perfLoadCustomerWhoBoughtThis' ) ) {
01092             return;
01093         }
01094 
01095         // selecting products that fits
01096         $sQ = $this->_generateSearchStrForCustomerBought();
01097 
01098         $oArticles = oxNew( 'oxarticlelist' );
01099         $oArticles->setSqlLimit( 0, $myConfig->getConfigParam( 'iNrofCustomerWhoArticles' ));
01100         $oArticles->selectString( $sQ );
01101         if ( $oArticles->count() ) {
01102             return $oArticles;
01103         }
01104     }
01105 
01112     public function loadAmountPriceInfo()
01113     {
01114         $myConfig = $this->getConfig();
01115         if ( !$myConfig->getConfigParam( 'bl_perfLoadPrice' ) || !$this->_blLoadPrice || !$this->_blCalcPrice) {
01116             return array();
01117         }
01118 
01119         if ( $this->_oAmountPriceInfo === null ) {
01120             $this->_oAmountPriceInfo = array();
01121             if ( count( ( $oAmPriceList = $this->_getAmountPriceList() ) ) ) {
01122                 $this->_oAmountPriceInfo = $this->_fillAmountPriceList( $oAmPriceList );
01123 
01124             }
01125         }
01126         return $this->_oAmountPriceInfo;
01127     }
01128 
01136     public function getSelectLists($sKeyPrefix = null)
01137     {
01138         //#1468C - more then one article in basket with different selectlist...
01139         //optionall function parameter $sKeyPrefix added, used only in basket.php
01140         $sKey = $this->getId();
01141         if ( isset( $sKeyPrefix ) ) {
01142             $sKey = $sKeyPrefix.'__'.$this->getId();
01143         }
01144 
01145         if ( !isset( self::$_aSelList[$sKey] ) ) {
01146             // all selectlists this article has
01147             $oLists = oxNew( 'oxlist' );
01148             $oLists->init('oxselectlist');
01149             $sSLViewName = getViewName('oxselectlist');
01150             $sSelect  = "select $sSLViewName.* from oxobject2selectlist left join $sSLViewName on $sSLViewName.oxid=oxobject2selectlist.oxselnid ";
01151             $sSelect .= 'where oxobject2selectlist.oxobjectid=\''.$this->getId().'\' ';
01152             //sorting
01153             $sSelect .= ' order by oxobject2selectlist.oxsort';
01154 
01155             $oLists->selectString( $sSelect );
01156 
01157             //#1104S if this is variant ant it has no selectlists, trying with parent
01158             if ( $this->oxarticles__oxparentid->value && $oLists->count() == 0 ) {
01159                 $sParentQuoted = oxDb::getDb()->quote($this->oxarticles__oxparentid->value);
01160                 //#1496C - select fixed ( * => $sSLViewName.*)
01161                 $sSelect  = "select $sSLViewName.* from oxobject2selectlist left join $sSLViewName on $sSLViewName.oxid=oxobject2selectlist.oxselnid ";
01162                 $sSelect .= "where oxobject2selectlist.oxobjectid=$sParentQuoted ";
01163                 //sorting
01164                 $sSelect .= ' order by oxobject2selectlist.oxsort';
01165                 $oLists->selectString( $sSelect);
01166             }
01167 
01168             $dVat = 0;
01169             if ( $this->getPrice() != null ) {
01170                 $dVat = $this->getPrice()->getVat();
01171             }
01172 
01173             $iCnt = 0;
01174             self::$_aSelList[$sKey] = array();
01175             foreach ( $oLists as $oSelectlist ) {
01176                 self::$_aSelList[$sKey][$iCnt] = $oSelectlist->getFieldList( $dVat );
01177                 self::$_aSelList[$sKey][$iCnt]['name'] = $oSelectlist->oxselectlist__oxtitle->value;
01178                 $iCnt++;
01179             }
01180         }
01181         return self::$_aSelList[$sKey];
01182     }
01183 
01191     protected function _hasAnyVariant( $blForceCoreTable = false )
01192     {
01193         $sArticleTable = $this->getTableNameForActiveSnippet( $blForceCoreTable );
01194         return (bool) oxDb::getDb()->getOne( "select 1 from $sArticleTable where oxparentid='".$this->getId()."'" );
01195     }
01196 
01202     public function hasMdVariants()
01203     {
01204         return $this->_blHasMdVariants;
01205     }
01206 
01215     public function getVariants( $blRemoveNotOrderables = true, $blForceCoreTable = false  )
01216     {
01217         if ( $blRemoveNotOrderables && $this->_aVariants ) {
01218             return $this->_aVariants;
01219         } elseif ( !$blRemoveNotOrderables && $this->_aVariantsWithNotOrderables ) {
01220             return $this->_aVariantsWithNotOrderables;
01221         }
01222 
01223         $myConfig = $this->getConfig();
01224         if ( !$this->_blLoadVariants ||
01225             ( !$this->isAdmin() && !$myConfig->getConfigParam( 'blLoadVariants') ) ||
01226             ( !$this->isAdmin() && !$this->oxarticles__oxvarcount->value ) ) {
01227             return array();
01228         }
01229 
01230         //do not load me as a parent later
01231         self::$_aLoadedParents[$this->getId()] = $this;
01232 
01233         //load simple variants for lists
01234         if ( $this->_isInList() ) {
01235             $oVariants = oxNew( 'oxsimplevariantlist' );
01236             $oVariants->setParent( $this );
01237         } else {
01238             //loading variants
01239             $oVariants = oxNew( 'oxarticlelist' );
01240             $oVariants->getBaseObject()->modifyCacheKey( '_variants' );
01241         }
01242 
01243         if ( $this->_blHasVariants = $this->_hasAnyVariant( $blForceCoreTable ) ) {
01244 
01245             startProfile("selectVariants");
01246             $blUseCoreTable = $blForceCoreTable;
01247             $oBaseObject = $oVariants->getBaseObject();
01248             $sArticleTable = $this->getTableNameForActiveSnippet( $blUseCoreTable );
01249 
01250             $sSelect = "select ".$oBaseObject->getSelectFields()." from $sArticleTable where " .
01251                        $this->getActiveCheckQuery( $blUseCoreTable ) .
01252                        $this->getVariantsQuery( $blRemoveNotOrderables, $blUseCoreTable ) .
01253                        " order by $sArticleTable.oxsort";
01254 
01255             $oVariants->selectString( $sSelect );
01256 
01257             //if this is multidimensional variants, make additional processing
01258             if ( $myConfig->getConfigParam( 'blUseMultidimensionVariants' ) ) {
01259                 $oMdVariants = oxNew( "oxVariantHandler" );
01260                 $this->_blHasMdVariants = $oMdVariants->isMdVariant( $oVariants->current() );
01261             }
01262             stopProfile("selectVariants");
01263         }
01264 
01265         //if we have variants then depending on config option the parent may be non buyable
01266         if ( !$myConfig->getConfigParam( 'blVariantParentBuyable' ) && $this->_blHasVariants ) {
01267             $this->_blNotBuyableParent = true;
01268         }
01269 
01270         //if we have variants, but all variants are incative means article may be non buyable (depends on config option)
01271         if ( !$myConfig->getConfigParam( 'blVariantParentBuyable' ) && $oVariants->count() == 0 && $this->_blHasVariants ) {
01272             $this->_blNotBuyable = true;
01273         }
01274 
01275         // cache
01276         if ( $blRemoveNotOrderables ) {
01277             $this->_aVariants = $oVariants;
01278         } else {
01279             $this->_aVariantsWithNotOrderables = $oVariants;
01280         }
01281 
01282         return $oVariants;
01283     }
01284 
01290     public function getSimpleVariants()
01291     {
01292         if ( $this->oxarticles__oxvarcount->value) {
01293             return $this->getVariants();
01294         }
01295     }
01296 
01305     public function getAdminVariants( $sLanguage = null )
01306     {
01307         $myConfig = $this->getConfig();
01308 
01309         $oVariants = oxNew( 'oxarticlelist');
01310 
01311         if ( is_null($sLanguage) ) {
01312             $oVariants->getBaseObject()->setLanguage(oxLang::getInstance()->getBaseLanguage());
01313         } else {
01314             $oVariants->getBaseObject()->setLanguage($sLanguage);
01315         }
01316 
01317         $sSql = 'select * from oxarticles where oxparentid = "'.$this->getId().'" order by oxsort ';
01318 
01319         $oVariants->selectString( $sSql);
01320 
01321         //if we have variants then depending on config option the parent may be non buyable
01322         if (!$myConfig->getConfigParam( 'blVariantParentBuyable' ) && ($oVariants->count() > 0)) {
01323             //$this->blNotBuyable = true;
01324             $this->_blNotBuyableParent = true;
01325         }
01326 
01327         return $oVariants;
01328     }
01329 
01337     public function getCategory()
01338     {
01339         startPRofile( 'getCategory' );
01340 
01341         $oCategory = oxNew( 'oxcategory' );
01342         $oCategory->setLanguage( $this->getLanguage() );
01343 
01344         // variant handling
01345         $sOXID = $this->getId();
01346         if ( isset( $this->oxarticles__oxparentid->value ) && $this->oxarticles__oxparentid->value ) {
01347             $sOXID = $this->oxarticles__oxparentid->value;
01348         }
01349 
01350         $oStr = getStr();
01351         $sWhere   = $oCategory->getSqlActiveSnippet();
01352         $sSelect  = $this->_generateSearchStr( $sOXID );
01353         $sSelect .= ( $oStr->strstr( $sSelect, 'where' )?' and ':' where ') . $sWhere . " order by oxobject2category.oxtime limit 1";
01354 
01355         // category not found ?
01356         if ( !$oCategory->assignRecord( $sSelect ) ) {
01357 
01358             $sSelect  = $this->_generateSearchStr( $sOXID, true );
01359             $sSelect .= ( $oStr->strstr( $sSelect, 'where' )?' and ':' where ') . $sWhere . " limit 1";
01360 
01361             // looking for price category
01362             if ( !$oCategory->assignRecord( $sSelect ) ) {
01363                 $oCategory = null;
01364             }
01365         }
01366 
01367         stopPRofile( 'getCategory' );
01368         return $oCategory;
01369     }
01370 
01379     public function getCategoryIds( $blActCats = false, $blSkipCache = false )
01380     {
01381         $myConfig = $this->getConfig();
01382         if ( isset( self::$_aArticleCats[$this->getId()] ) && !$blSkipCache ) {
01383             return self::$_aArticleCats[$this->getId()];
01384         }
01385 
01386         // variant handling
01387         $sOXID = $this->getId();
01388         if (isset( $this->oxarticles__oxparentid->value) && $this->oxarticles__oxparentid->value) {
01389             $sOXID = $this->oxarticles__oxparentid->value;
01390         }
01391 
01392         // we do not use lists here as we dont need this overhead right now
01393         $sSql = $this->_getSelectCatIds( $sOXID, $blActCats );
01394         $oDB = oxDb::getDb(true);
01395         $rs = $oDB->execute( $sSql );
01396 
01397 
01398         $aRet = array();
01399 
01400         if ($rs != false && $rs->recordCount() > 0) {
01401             while (!$rs->EOF) {
01402                 $aRet[] = $rs->fields['oxcatnid'];
01403                 $rs->moveNext();
01404             }
01405         }
01406 
01407         // adding price categories if such exists
01408         $sSql = $this->getSqlForPriceCategories();
01409 
01410         $oDB = oxDb::getDb( true );
01411         $rs = $oDB->execute( $sSql );
01412 
01413         if ($rs != false && $rs->recordCount() > 0) {
01414             while (!$rs->EOF) {
01415 
01416                 if ( is_array( $rs->fields ) ) {
01417                    $rs->fields = array_change_key_case( $rs->fields, CASE_LOWER );
01418                 }
01419 
01420 
01421                 if ( !$aRet[$rs->fields['oxid']] ) {
01422                     $aRet[] = $rs->fields['oxid'];
01423                 }
01424                 $rs->moveNext();
01425             }
01426         }
01427 
01428         return self::$_aArticleCats[$this->getId()] = $aRet;
01429     }
01430 
01439     protected function _getSelectCatIds( $sOXID, $blActCats = false )
01440     {
01441         $sO2CView = $this->_getObjectViewName('oxobject2category');
01442         $sCatView = $this->_getObjectViewName('oxcategories');
01443         $sSelect =  "select oxobject2category.oxcatnid as oxcatnid from $sO2CView as oxobject2category left join $sCatView as oxcategories on oxcategories.oxid = oxobject2category.oxcatnid ";
01444         $sSelect .= 'where oxobject2category.oxobjectid='.oxDb::getDb()->quote($sOXID).' and oxcategories.oxid is not null and oxcategories.oxactive'.(($this->getLanguage())?'_'.$this->getLanguage():'').' = 1 ';
01445         if ( $blActCats ) {
01446             $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".(($this->getLanguage())?"_".$this->getLanguage():"")." = 0 ) ) = 0 ";
01447         }
01448         $sSelect .= 'order by oxobject2category.oxtime ';
01449         return $sSelect;
01450     }
01451 
01461     public function getVendor( $blShopCheck = true )
01462     {
01463         if ( ( $sVendorId = $this->getVendorId() ) ) {
01464             $oVendor = oxNew( 'oxvendor' );
01465         } elseif ( !$blShopCheck && $this->oxarticles__oxvendorid->value ) {
01466             $oVendor = oxNew( 'oxi18n' );
01467             $oVendor->init('oxvendor');
01468             $oVendor->setReadOnly( true );
01469             $sVendorId = $this->oxarticles__oxvendorid->value;
01470         }
01471         if ( $sVendorId && $oVendor->load( $sVendorId ) && $oVendor->oxvendor__oxactive->value ) {
01472             if ( !$this->getConfig()->getConfigParam( 'bl_perfLoadVendorTree' ) ) {
01473                 $oVendor->setReadOnly( true );
01474             }
01475             return $oVendor;
01476         }
01477         return null;
01478     }
01479 
01487     public function getVendorId( $blForceReload = false )
01488     {
01489         $sVendorId = false;
01490         if ( $this->oxarticles__oxvendorid->value ) {
01491             if ( !$blForceReload && isset( self::$_aArticleVendors[$this->getId()] ) ) {
01492                 return self::$_aArticleVendors[$this->getId()];
01493             }
01494             $oDb = oxDb::getDb();
01495             $sVendorIdQuoted = $oDb->quote($this->oxarticles__oxvendorid->value);
01496             $sQ = "select oxid from ".getViewName('oxvendor')." where oxid=$sVendorIdQuoted";
01497             self::$_aArticleVendors[$this->getId()] = $sVendorId = $oDb->getOne( $sQ );
01498         }
01499         return $sVendorId;
01500     }
01501 
01509     public function getManufacturerId( $blForceReload = false )
01510     {
01511         $sManufacturerId = false;
01512         if ( $this->oxarticles__oxmanufacturerid->value ) {
01513             if ( !$blForceReload && isset( self::$_aArticleManufacturers[$this->getId()])) {
01514                 return self::$_aArticleManufacturers[$this->getId()];
01515             }
01516             $oDb = oxDb::getDb();
01517             $sQ = "select oxid from ".getViewName('oxmanufacturers')." where oxid=".$oDb->quote($this->oxarticles__oxmanufacturerid->value);
01518             self::$_aArticleManufacturers[$this->getId()] = $sManufacturerId = $oDb->getOne( $sQ );
01519         }
01520         return $sManufacturerId;
01521     }
01522 
01532     public function getManufacturer( $blShopCheck = true )
01533     {
01534         $oManufacturer = oxNew( 'oxmanufacturer' );;
01535         if ( !( $sManufacturerId = $this->getManufacturerId() ) &&
01536              !$blShopCheck && $this->oxarticles__oxmanufacturerid->value ) {
01537             $oManufacturer->setReadOnly( true );
01538             $sManufacturerId = $this->oxarticles__oxmanufacturerid->value;
01539         }
01540 
01541         if ( $sManufacturerId && $oManufacturer->load( $sManufacturerId ) ) {
01542             if ( !$this->getConfig()->getConfigParam( 'bl_perfLoadManufacturerTree' ) ) {
01543                 $oManufacturer->setReadOnly( true );
01544             }
01545             $oManufacturer = $oManufacturer->oxmanufacturers__oxactive->value ? $oManufacturer : null;
01546         } else {
01547             $oManufacturer = null;
01548         }
01549 
01550         return $oManufacturer;
01551     }
01552 
01560     public function inCategory( $sCatNid)
01561     {
01562         return in_array( $sCatNid, $this->getCategoryIds());
01563     }
01564 
01573     public function isAssignedToCategory( $sCatId )
01574     {
01575         // variant handling
01576         $sOXID = $this->getId();
01577         if ( isset( $this->oxarticles__oxparentid->value) && $this->oxarticles__oxparentid->value) {
01578             $sOXID = $this->oxarticles__oxparentid->value;
01579         }
01580 
01581         $oDB = oxDb::getDb();
01582         $sSelect = $this->_generateSelectCatStr( $sOXID, $sCatId);
01583         $sOXID = $oDB->getOne( $sSelect);
01584         // article is assigned to passed category!
01585         if ( isset( $sOXID) && $sOXID) {
01586             return true;
01587         }
01588 
01589         // maybe this category is price category ?
01590         if ( $this->getConfig()->getConfigParam( 'bl_perfLoadPrice' ) && $this->_blLoadPrice ) {
01591             $dPriceFromTo = $this->getPrice()->getBruttoPrice();
01592             if ( $dPriceFromTo > 0) {
01593                 $sSelect = $this->_generateSelectCatStr( $sOXID, $sCatId, $dPriceFromTo);
01594                 $sOXID = $oDB->getOne( $sSelect);
01595                 // article is assigned to passed category!
01596                 if ( isset( $sOXID) && $sOXID) {
01597                     return true;
01598                 }
01599             }
01600         }
01601         return false;
01602     }
01603 
01609     public function getTPrice()
01610     {
01611         if ( !$this->getConfig()->getConfigParam( 'bl_perfLoadPrice' ) || !$this->_blLoadPrice ) {
01612             return;
01613         }
01614         // return cached result, since oPrice is created ONLY in this function [or function of EQUAL level]
01615         if ( $this->_oTPrice !== null ) {
01616             return $this->_oTPrice;
01617         }
01618 
01619         $this->_oTPrice = oxNew( 'oxPrice' );
01620         $this->_oTPrice->setPrice( $this->oxarticles__oxtprice->value );
01621 
01622         $this->_applyVat( $this->_oTPrice, $this->getArticleVat() );
01623         $this->_applyCurrency( $this->_oTPrice );
01624 
01625         return $this->_oTPrice;
01626     }
01627 
01633     public function skipDiscounts()
01634     {
01635         // allready loaded skip discounts config
01636         if ( $this->_blSkipDiscounts !== null )
01637             return $this->_blSkipDiscounts;
01638 
01639         if ( $this->oxarticles__oxskipdiscounts->value )
01640             return true;
01641 
01642         $sO2CView = getViewName('oxobject2category');
01643         $sSelect =  "select 1 from $sO2CView as oxobject2category left join oxcategories on oxcategories.oxid = oxobject2category.oxcatnid ";
01644         $sSelect .= 'where oxobject2category.oxobjectid="'.$this->getId().'" and oxcategories.oxactive'.(($this->getLanguage())?'_'.$this->getLanguage():'').' = 1 and oxcategories.oxskipdiscounts = "1" ';
01645 
01646         return $this->_blSkipDiscounts = ( oxDb::getDb()->getOne($sSelect) == 1 );
01647     }
01648 
01656     public function setPrice(oxPrice $oPrice)
01657     {
01658         $this->_oPrice = $oPrice;
01659     }
01660 
01669     public function getBasePrice( $dAmount = 1 )
01670     {
01671         // override this function if you want e.g. different prices
01672         // for diff. usergroups.
01673 
01674         // Performance
01675         $myConfig = $this->getConfig();
01676         if( !$myConfig->getConfigParam( 'bl_perfLoadPrice' ) || !$this->_blLoadPrice )
01677             return;
01678 
01679         // GroupPrice or DB price ajusted by AmountPrice
01680         $dPrice = $this->_getAmountPrice( $dAmount );
01681 
01682 
01683         return $dPrice;
01684     }
01685 
01693     public function getPrice( $dAmount = 1 )
01694     {
01695         $myConfig = $this->getConfig();
01696         // Performance
01697         if ( !$myConfig->getConfigParam( 'bl_perfLoadPrice' ) || !$this->_blLoadPrice ) {
01698             return;
01699         }
01700 
01701         // return cached result, since oPrice is created ONLY in this function [or function of EQUAL level]
01702         if ( $dAmount != 1 || $this->_oPrice === null ) {
01703             $oPrice = oxNew( 'oxPrice' );
01704 
01705             // get base
01706             $oPrice->setPrice( $this->getBasePrice( $dAmount ) );
01707 
01708             // price handling
01709             if ( !$this->_blCalcPrice && $dAmount == 1 ) {
01710                 return $this->_oPrice = $oPrice;
01711             }
01712 
01713             $this->_calculatePrice( $oPrice );
01714             if ( $dAmount != 1 ) {
01715                 return $oPrice;
01716             }
01717 
01718             $this->_oPrice = $oPrice;
01719         }
01720         return $this->_oPrice;
01721     }
01722 
01730     protected function _calculatePrice( $oPrice )
01731     {
01732         // apply VAT only if configuration requires it
01733         if ( !$this->getConfig()->getConfigParam( 'bl_perfCalcVatOnlyForBasketOrder' ) ) {
01734             $this->_applyVAT( $oPrice, $this->getArticleVat() );
01735         }
01736 
01737         // apply currency
01738         $this->_applyCurrency( $oPrice );
01739         // apply discounts
01740         if ( !$this->skipDiscounts() ) {
01741             $oDiscountList = oxDiscountList::getInstance();
01742             $oDiscountList->applyDiscounts( $oPrice, $oDiscountList->getArticleDiscounts($this, $this->getArticleUser() ) );
01743         }
01744 
01745         return $oPrice;
01746     }
01747 
01755     public function setArticleUser($oUser)
01756     {
01757         $this->_oUser = $oUser;
01758     }
01759 
01765     public function getArticleUser()
01766     {
01767         if ($this->_oUser) {
01768             return $this->_oUser;
01769         }
01770         return $this->getUser();
01771     }
01772 
01782     public function getBasketPrice( $dAmount, $aSelList, $oBasket )
01783     {
01784         $oUser = $oBasket->getBasketUser();
01785         $this->setArticleUser($oUser);
01786 
01787         $oBasketPrice = oxNew( 'oxPrice' );
01788 
01789         // get base price
01790         $dBasePrice = $this->getBasePrice( $dAmount );
01791 
01792         // applying select list price
01793         $dBasePrice = $this->_modifySelectListPrice( $dBasePrice, $aSelList );
01794 
01795         // setting price
01796         $oBasketPrice->setPrice( $dBasePrice );
01797 
01798         // apply VAT
01799         $this->_applyVat( $oBasketPrice, oxVatSelector::getInstance()->getBasketItemVat( $this, $oBasket ) );
01800 
01801         // apply currency
01802         $this->_applyCurrency( $oBasketPrice );
01803 
01804         // apply discounts
01805         if ( !$this->skipDiscounts() ) {
01806             // apply general discounts
01807             $oDiscountList = oxDiscountList::getInstance();
01808             $oDiscountList->applyDiscounts( $oBasketPrice, $oDiscountList->getArticleDiscounts( $this, $oUser ) );
01809         }
01810 
01811         // returning final price object
01812         return $oBasketPrice;
01813     }
01814 
01827     public function applyBasketDiscounts(oxPrice $oPrice, $aDiscounts, $dAmount = 1)
01828     {
01829         $oDiscountList = oxDiscountList::getInstance();
01830         return $oDiscountList->applyBasketDiscounts( $oPrice, $aDiscounts, $dAmount );
01831     }
01832 
01841     public function delete( $sOXID = null )
01842     {
01843         if ( !$sOXID ) {
01844             $sOXID = $this->getId();
01845         }
01846         if ( !$sOXID ) {
01847             return false;
01848         }
01849 
01850 
01851         $this->load( $sOXID );
01852         $this->_deletePics();
01853         $this->_onChangeResetCounts( $sOXID, $this->oxarticles__oxvendorid->value, $this->oxarticles__oxmanufacturerid->value );
01854         $this->_deleteVariantRecords( $sOXID );
01855         $rs = $this->_deleteRecords( $sOXID );
01856 
01857         oxSeoEncoderArticle::getInstance()->onDeleteArticle($this);
01858 
01859         $this->onChange( ACTION_DELETE, $sOXID, $this->oxarticles__oxparentid->value );
01860 
01861         return $rs->EOF;
01862     }
01863 
01872     public function reduceStock($dAmount, $blAllowNegativeStock = false)
01873     {
01874         $this->beforeUpdate();
01875 
01876         $iStockCount = $this->oxarticles__oxstock->value - $dAmount;
01877         if (!$blAllowNegativeStock && ($iStockCount < 0)) {
01878             $dAmount += $iStockCount;
01879             $iStockCount = 0;
01880         }
01881         $this->oxarticles__oxstock = new oxField($iStockCount);
01882 
01883         $oDb = oxDb::getDb();
01884         $oDb->execute( 'update oxarticles set oxarticles.oxstock = '.$oDb->quote( $iStockCount ).' where oxarticles.oxid = '.$oDb->quote( $this->getId() ) );
01885         $this->onChange( ACTION_UPDATE_STOCK );
01886         return $dAmount;
01887     }
01888 
01897     public function updateSoldAmount( $dAmount = 0 )
01898     {
01899         if ( !$dAmount ) {
01900             return;
01901         }
01902 
01903         $this->beforeUpdate();
01904 
01905         // article is not variant - should be updated current amount
01906         if ( !$this->oxarticles__oxparentid->value ) {
01907             //updating by SQL query, due to wrong behaviour if saving article using not admin mode
01908             $dAmount = (double) $dAmount;
01909             $oDb = oxDb::getDb();
01910             $rs = $oDb->execute( "update oxarticles set oxarticles.oxsoldamount = oxarticles.oxsoldamount + $dAmount where oxarticles.oxid = ".$oDb->quote($this->oxarticles__oxid->value));
01911         } elseif ( $this->oxarticles__oxparentid->value) {
01912             // article is variant - should be updated this article parent amount
01913             $oUpdateArticle = oxNewArticle( $this->oxarticles__oxparentid->value );
01914             $oUpdateArticle->updateSoldAmount( $dAmount );
01915         }
01916 
01917         $this->onChange( ACTION_UPDATE );
01918 
01919         return $rs;
01920     }
01921 
01927     public function disableReminder()
01928     {
01929         $oDB = oxDb::getDb(true);
01930         return $oDB->execute( "update oxarticles set oxarticles.oxremindactive = 2 where oxarticles.oxid = ".$oDB->quote($this->oxarticles__oxid->value));
01931     }
01932 
01939     public function save()
01940     {
01941         $myConfig = $this->getConfig();
01942 
01943         $this->oxarticles__oxthumb = new oxField(basename($this->oxarticles__oxthumb->value), oxField::T_RAW);
01944         $this->oxarticles__oxicon  = new oxField(basename($this->oxarticles__oxicon->value), oxField::T_RAW);
01945         $iPicCount = $myConfig->getConfigParam( 'iPicCount');
01946         for ($i=1; $i <= $iPicCount; $i++) {
01947             if ( isset($this->{'oxarticles__oxpic'.$i}) ) {
01948                 $this->{'oxarticles__oxpic'.$i}->setValue(basename($this->{'oxarticles__oxpic'.$i}->value));
01949             }
01950         }
01951 
01952         $blRet = parent::save();
01953 
01954         // save article long description
01955         $this->setArticleLongDesc();
01956 
01957         // load article images after save
01958         $this->_assignAllPictureValues();
01959 
01960         return $blRet;
01961     }
01962 
01963 
01970     public function getPictureGallery()
01971     {
01972         $myConfig = $this->getConfig();
01973 
01974         //initialize
01975         $blMorePic = false;
01976         $aArtPics  = array();
01977         $aArtIcons = array();
01978         $iActPicId = 1;
01979         $sActPic = $this->getPictureUrl( $iActPicId );
01980 
01981         if ( oxConfig::getParameter( 'actpicid' ) ) {
01982             $iActPicId = oxConfig::getParameter('actpicid');
01983         }
01984 
01985         $oStr = getStr();
01986         $iCntr = 0;
01987         $iPicCount = $myConfig->getConfigParam( 'iPicCount' );
01988         $blCheckActivePicId = true;
01989 
01990         for ( $i = 1; $i <= $iPicCount; $i++) {
01991             $sPicVal = $this->getPictureUrl( $i );
01992             $sIcoVal = $this->getIconUrl( $i );
01993             if ( !$oStr->strstr($sIcoVal, 'nopic_ico.jpg') && !$oStr->strstr($sIcoVal, 'nopic.jpg') ) {
01994                 if ($iCntr) {
01995                     $blMorePic = true;
01996                 }
01997                 $aArtIcons[$i]= $sIcoVal;
01998                 $aArtPics[$i]= $sPicVal;
01999                 $iCntr++;
02000 
02001                 if ($iActPicId == $i) {
02002                     $sActPic = $sPicVal;
02003                     $blCheckActivePicId = false;
02004                 }
02005 
02006             } else if ( $blCheckActivePicId && $iActPicId <= $i) {
02007                 // if picture is empty, setting active pic id to next
02008                 // picture
02009                 $iActPicId++;
02010             }
02011         }
02012 
02013         $blZoomPic  = false;
02014         $aZoomPics = array();
02015         $iZoomPicCount = $myConfig->getConfigParam( 'iPicCount' );
02016 
02017         for ( $j = 1,$c = 1; $j <= $iZoomPicCount; $j++) {
02018             $sVal = $this->getZoomPictureUrl($j);
02019 
02020             if ( !$oStr->strstr($sVal, 'nopic.jpg')) {
02021                 if ($this->getConfig()->getConfigParam('blFormerTplSupport')) {
02022                     $sVal = $this->_sDynImageDir."/".$sVal;
02023                 }
02024                 $blZoomPic = true;
02025                 $aZoomPics[$c]['id'] = $c;
02026                 $aZoomPics[$c]['file'] = $sVal;
02027                 //anything is better than empty name, because <img src=""> calls shop once more = x2 SLOW.
02028                 if (!$sVal) {
02029                     $aZoomPics[$c]['file'] = "nopic.jpg";
02030                 }
02031                 $c++;
02032             }
02033         }
02034 
02035         $aPicGallery = array('ActPicID' => $iActPicId,
02036                              'ActPic' => $sActPic,
02037                              'MorePics' => $blMorePic,
02038                              'Pics' => $aArtPics,
02039                              'Icons' => $aArtIcons,
02040                              'ZoomPic' => $blZoomPic,
02041                              'ZoomPics' => $aZoomPics);
02042 
02043         return $aPicGallery;
02044     }
02045 
02059     public function onChange($sAction = null, $sOXID = null, $sParentID = null)
02060     {
02061         $myConfig = $this->getConfig();
02062 
02063         if (!isset($sOXID)) {
02064             if ( $this->getId()) {
02065                 $sOXID = $this->getId();
02066             }
02067             if (!isset ($sOXID)) {
02068                 $sOXID = $this->oxarticles__oxid->value;
02069             }
02070             if ($this->oxarticles__oxparentid->value) {
02071                 $sParentID = $this->oxarticles__oxparentid->value;
02072             }
02073         }
02074         if (!isset($sOXID)) {
02075             return;
02076         }
02077 
02078         //if (isset($sOXID) && !$myConfig->blVariantParentBuyable && $myConfig->blUseStock)
02079         if ( $myConfig->getConfigParam( 'blUseStock' ) ) {
02080             //if article has variants then updating oxvarstock field
02081             //getting parent id
02082             if (!isset($sParentID)) {
02083                 $oDb = oxDb::getDb();
02084                 $sQ = 'select oxparentid from oxarticles where oxid = '.$oDb->quote($sOXID);
02085                 $sParentID = $oDb->getOne($sQ);
02086             }
02087             //if we have parent id then update stock
02088             if ($sParentID) {
02089                 $this->_onChangeUpdateStock($sParentID);
02090             }
02091         }
02092         //if we have parent id then update count
02093         //update count even if blUseStock is not active
02094         if ($sParentID) {
02095             $this->_onChangeUpdateVarCount($sParentID);
02096         }
02097 
02098         $sId = ( $sParentID ) ? $sParentID : $sOXID;
02099         $this->_onChangeUpdateMinVarPrice( $sId );
02100 
02101             // reseting articles count cache if stock has changed and some
02102             // articles goes offline (M:1448)
02103             if ( $sAction === ACTION_UPDATE_STOCK ) {
02104                 $this->_onChangeStockResetCount( $sOXID );
02105             }
02106 
02107     }
02108 
02115     public function getCustomVAT()
02116     {
02117         if ( isset($this->oxarticles__oxvat->value) ) {
02118             return $this->oxarticles__oxvat->value;
02119         }
02120     }
02121 
02130     public function checkForStock( $dAmount, $dArtStockAmount = 0 )
02131     {
02132         $myConfig = $this->getConfig();
02133         if ( !$myConfig->getConfigParam( 'blUseStock' ) ) {
02134             return true;
02135         }
02136 
02137         // fetching DB info as its up-to-date
02138         $sQ = 'select oxstock, oxstockflag from oxarticles where oxid = "'.$this->getId().'" ';
02139         $rs = oxDb::getDb(true)->Execute( $sQ );
02140 
02141         $iOnStock   = 0;
02142         $iStockFlag = 0;
02143         if ( $rs !== false && $rs->recordCount() > 0 ) {
02144             $iOnStock   = $rs->fields['oxstock'] - $dArtStockAmount;
02145             $iStockFlag = $rs->fields['oxstockflag'];
02146 
02147             // dodger : fremdlager is also always considered as on stock
02148             if ( $iStockFlag == 1 || $iStockFlag == 4) {
02149                 return true;
02150             }
02151             if ( !$myConfig->getConfigParam( 'blAllowUnevenAmounts' ) ) {
02152                 $iOnStock = floor( $iOnStock );
02153             }
02154         }
02155         if ($this->getConfig()->getConfigParam( 'blPsBasketReservationEnabled' )) {
02156             $iOnStock += $this->getSession()->getBasketReservations()->getReservedAmount($this->getId());
02157         }
02158         if ( $iOnStock >= $dAmount ) {
02159             return true;
02160         } else {
02161             if ( $iOnStock > 0 ) {
02162                 return $iOnStock;
02163             } else {
02164                 $oEx = oxNew( 'oxArticleInputException' );
02165                 $oEx->setMessage( 'EXCEPTION_ARTICLE_ARTICELNOTBUYABLE' );
02166                 oxUtilsView::getInstance()->addErrorToDisplay( $oEx );
02167                 return false;
02168             }
02169         }
02170     }
02171 
02172 
02180     public function getArticleLongDesc($sOXID = null)
02181     {
02182 
02183         if ( !$sOXID ) {
02184             $sOXID = $this->oxarticles__oxid->value;
02185         }
02186 
02187         if ($sOXID == $this->oxarticles__oxid->value) {
02188             if (isset($this->oxarticles__oxlongdesc) && ($this->oxarticles__oxlongdesc instanceof oxField) && $this->oxarticles__oxlongdesc->value) {
02189                 return $this->oxarticles__oxlongdesc;
02190             }
02191         }
02192 
02193         $myConfig = $this->getConfig();
02194 
02195         if ( $sOXID ) {
02196             $sLangField = oxLang::getInstance()->getLanguageTag($this->getLanguage());
02197             $oDb = oxDb::getDb();
02198             $this->_setLongDesc($oDb->getOne( "select oxlongdesc{$sLangField} from oxartextends where oxid = ".$oDb->quote($sOXID) ));
02199         } else {
02200             // TODO: check if keeping fldname is needed in non-admin mode
02201             $this->oxarticles__oxlongdesc = new oxField();
02202             $this->oxarticles__oxlongdesc->fldname = 'oxlongdesc';
02203             $this->oxarticles__oxlongdesc->table   = 'oxarticles';
02204             $this->oxarticles__oxlongdesc->fldtype = 'text';
02205         }
02206 
02207         return $this->oxarticles__oxlongdesc;
02208     }
02209 
02217     protected function _setLongDesc($sDbValue)
02218     {
02219         // TODO: the code below is redundant, optimize it, assignments should go smooth without conversions
02220         // hack, if editor screws up text, htmledit tends to do so
02221         $sDbValue = str_replace( '&amp;nbsp;', '&nbsp;', $sDbValue );
02222         $sDbValue = str_replace( '&amp;', '&', $sDbValue );
02223         $sDbValue = str_replace( '&quot;', '"', $sDbValue );
02224         $sDbValue = str_replace( '&lang=', '&amp;lang=', $sDbValue);
02225         //
02226 
02227         $oStr = getStr();
02228         $blHasSmarty = $oStr->strstr( $sDbValue, '[{' );
02229         $blHasPhp = $oStr->strstr( $sDbValue, '<?' );
02230         $myConfig = oxConfig::getInstance();
02231         if ( ( $blHasSmarty || $blHasPhp ) && ($myConfig->getConfigParam( 'blExport' ) || !$this->isAdmin() ) && $myConfig->getConfigParam( 'bl_perfParseLongDescinSmarty' ) ) {
02232             $sDbValue = oxUtilsView::getInstance()->parseThroughSmarty( $sDbValue, $this->getId() );
02233         }
02234         $this->oxarticles__oxlongdesc = new oxField($sDbValue, oxField::T_RAW);
02235         // TODO: check if keeping fldname is needed in non-admin mode
02236         $this->oxarticles__oxlongdesc->fldname = 'oxlongdesc';
02237         $this->oxarticles__oxlongdesc->table   = 'oxarticles';
02238         $this->oxarticles__oxlongdesc->fldtype = 'text';
02239     }
02240 
02246     public function setArticleLongDesc()
02247     {
02248 
02249         if ( $this->_blEmployMultilanguage ) {
02250             // update or insert article long description
02251             if ($this->oxarticles__oxlongdesc instanceof oxField) {
02252                 $sLongDesc = $this->oxarticles__oxlongdesc->getRawValue();
02253             } else {
02254                 $sLongDesc = $this->oxarticles__oxlongdesc->value;
02255             }
02256             $this->_saveArtLongDesc($this->getLanguage(), $sLongDesc);
02257         } else {
02258             $oArtExt = oxNew('oxi18n');
02259             $oArtExt->init('oxartextends');
02260             $aObjFields = $oArtExt->_getAllFields(true);
02261             foreach ($aObjFields as $sKey => $sValue ) {
02262                 if ( preg_match('/^oxlongdesc(_(\d{1,2}))?$/', $sKey) ) {
02263                     $sField = $this->_getFieldLongName($sKey);
02264                     if (isset($this->$sField)) {
02265                         $iLang = $oArtExt->_getFieldLang($sKey);
02266                         $sLongDesc = null;
02267                         if ($this->$sField instanceof oxField) {
02268                             $sLongDesc = $this->$sField->getRawValue();
02269                         } elseif (is_object($this->$sField)) {
02270                             $sLongDesc = $this->$sField->value;
02271                         }
02272                         if (isset($sLongDesc)) {
02273                             $this->_saveArtLongDesc($iLang, $sLongDesc);
02274                         }
02275                     }
02276                 }
02277             }
02278         }
02279     }
02280 
02286     public function getAttributes()
02287     {
02288         if ( $this->_oAttributeList === null ) {
02289             $this->_oAttributeList = oxNew( 'oxattributelist' );
02290             $this->_oAttributeList->loadAttributes( $this->getId() );
02291         }
02292 
02293         return $this->_oAttributeList;
02294     }
02295 
02304     public function appendLink( $sAddParams, $iLang = null )
02305     {
02306         if ( $sAddParams ) {
02307             if ( $iLang === null ) {
02308                 $iLang = $this->getLanguage();
02309             }
02310 
02311             $this->_aSeoAddParams[$iLang]  = isset( $this->_aSeoAddParams[$iLang] ) ? $this->_aSeoAddParams[$iLang] . "&amp;" : "";
02312             $this->_aSeoAddParams[$iLang] .= $sAddParams;
02313         }
02314     }
02315 
02324     public function getBaseSeoLink( $iLang, $blMain = false )
02325     {
02326         $oEncoder = oxSeoEncoderArticle::getInstance();
02327         if ( !$blMain ) {
02328             return $oEncoder->getArticleUrl( $this, $iLang, $this->getLinkType() );
02329         }
02330         return $oEncoder->getArticleMainUrl( $this, $iLang );
02331     }
02332 
02341     public function getLink( $iLang = null, $blMain = false  )
02342     {
02343         if ( !oxUtils::getInstance()->seoIsActive() ) {
02344             return $this->getStdLink( $iLang );
02345         }
02346 
02347         if ( $iLang === null ) {
02348             $iLang = $this->getLanguage();
02349         }
02350 
02351         $iLinkType = $this->getLinkType();
02352         if ( !isset( $this->_aSeoUrls[$iLang][$iLinkType] ) ) {
02353             $this->_aSeoUrls[$iLang][$iLinkType] = $this->getBaseSeoLink( $iLang, $blMain );
02354         }
02355 
02356         $sUrl = $this->_aSeoUrls[$iLang][$iLinkType];
02357         if ( isset($this->_aSeoAddParams[$iLang])) {
02358             $sUrl .= ( ( strpos( $sUrl.$this->_aSeoAddParams[$iLang], '?' ) === false ) ? '?' : '&amp;' ).$this->_aSeoAddParams[$iLang];
02359         }
02360 
02361         return $sUrl;
02362     }
02363 
02372     public function getMainLink( $iLang = null )
02373     {
02374         return $this->getLink( $iLang, true );
02375     }
02376 
02384     public function setLinkType( $iType )
02385     {
02386         // resetting detaisl link, to force new
02387         $this->_sDetailLink = null;
02388 
02389         // setting link type
02390         $this->_iLinkType = (int) $iType;
02391     }
02392 
02398     public function getLinkType()
02399     {
02400         return $this->_iLinkType;
02401     }
02402 
02411     public function appendStdLink( $sAddParams, $iLang = null )
02412     {
02413         if ( $sAddParams ) {
02414             if ( $iLang === null ) {
02415                 $iLang = $this->getLanguage();
02416             }
02417 
02418             $this->_aStdAddParams[$iLang]  = isset( $this->_aStdAddParams[$iLang] ) ? $this->_aStdAddParams[$iLang] . "&amp;" : "";
02419             $this->_aStdAddParams[$iLang] .= $sAddParams;
02420         }
02421     }
02422 
02432     public function getBaseStdLink( $iLang, $blAddId = true, $blFull = true )
02433     {
02434         $sUrl = '';
02435         if ( $blFull ) {
02436             //always returns shop url, not admin
02437             $sUrl = $this->getConfig()->getShopUrl( $iLang, false );
02438         }
02439 
02440         $sUrl .= "index.php?cl=details" . ( $blAddId ? "&amp;anid=".$this->getId() : "" );
02441         return $sUrl . ( isset( $this->_aStdAddParams[$iLang] ) ? "&amp;". $this->_aStdAddParams[$iLang] : "" );
02442     }
02443 
02452     public function getStdLink( $iLang = null, $aParams = array() )
02453     {
02454         if ( $iLang === null ) {
02455             $iLang = $this->getLanguage();
02456         }
02457 
02458         if ( !isset( $this->_aStdUrls[$iLang] ) ) {
02459             $this->_aStdUrls[$iLang] = $this->getBaseStdLink( $iLang );
02460         }
02461 
02462         return oxUtilsUrl::getInstance()->processStdUrl( $this->_aStdUrls[$iLang], $aParams, $iLang, $iLang != $this->getLanguage() );
02463     }
02464 
02472     public function getStdTagLink( $sTag )
02473     {
02474         $sStdTagLink = $this->getConfig()->getShopHomeURL( $this->getLanguage(), false );
02475         return $sStdTagLink . "cl=details&amp;anid=".$this->getId()."&amp;listtype=tag&amp;searchtag=".rawurlencode( $sTag );
02476     }
02477 
02483     public function getTags()
02484     {
02485         $sTagField = "oxtags".oxLang::getInstance()->getLanguageTag($this->getLanguage());
02486         $sQ = "select $sTagField from oxartextends where oxid = '".$this->getId()."'";
02487         $sTags = oxDb::getDb(true)->getOne($sQ);
02488         $oTagCloud = oxNew('oxtagcloud');
02489         $sTags = $oTagCloud->trimTags($sTags);
02490         return $sTags;
02491     }
02492 
02500     public function saveTags($sTags)
02501     {
02502         $sTags = mysql_real_escape_string($sTags);
02503         $oTagCloud = oxNew('oxtagcloud');
02504         $oTagCloud->resetTagCache();
02505         $sTags = $oTagCloud->prepareTags($sTags);
02506         $sTagField = "oxtags".oxLang::getInstance()->getLanguageTag($this->getLanguage());
02507         $sQ = "update oxartextends set $sTagField = '$sTags'  where oxid = '".$this->getId()."'";
02508         return oxDb::getDb()->execute($sQ);
02509 
02510     }
02511 
02519     public function addTag($sTag)
02520     {
02521         $sTag = mysql_real_escape_string($sTag);
02522 
02523         $oTagCloud = oxNew('oxtagcloud');
02524         $oTagCloud->resetTagCache();
02525         $sTag = $oTagCloud->prepareTags($sTag);
02526         $sTagSeparator = $this->getConfig()->getConfigParam('sTagSeparator');
02527         $sTailTag = $sTagSeparator.$sTag;
02528 
02529         $sField = "oxartextends.OXTAGS".oxLang::getInstance()->getLanguageTag();
02530         $sQ = "insert into oxartextends (oxartextends.OXID, $sField) values ('".$this->getId()."', '{$sTag}')
02531                        ON DUPLICATE KEY update $sField = CONCAT(TRIM($sField), '$sTailTag') ";
02532 
02533         return oxDb::getDb()->Execute($sQ);
02534     }
02535 
02541     public function getMediaUrls()
02542     {
02543         if ( $this->_aMediaUrls === null ) {
02544             $this->_aMediaUrls = oxNew("oxlist");
02545             $this->_aMediaUrls->init("oxmediaurl");
02546             $this->_aMediaUrls->getBaseObject()->setLanguage( $this->getLanguage() );
02547 
02548             $sQ = "select * from oxmediaurls where oxobjectid = '".$this->getId()."'";
02549             $this->_aMediaUrls->selectString($sQ);
02550         }
02551         return $this->_aMediaUrls;
02552     }
02553 
02559     public function getDynImageDir()
02560     {
02561         return $this->_sDynImageDir;
02562     }
02563 
02569     public function getDispSelList()
02570     {
02571         if ($this->_aDispSelList === null) {
02572             if ( $this->getConfig()->getConfigParam( 'bl_perfLoadSelectLists' ) && $this->getConfig()->getConfigParam( 'bl_perfLoadSelectListsInAList' ) ) {
02573                 $this->_aDispSelList = $this->getSelectLists();
02574             }
02575         }
02576         return $this->_aDispSelList;
02577     }
02578 
02584     public function getMoreDetailLink()
02585     {
02586         if ( $this->_sMoreDetailLink == null ) {
02587 
02588             // and assign special article values
02589             $this->_sMoreDetailLink = $this->getConfig()->getShopHomeURL() . 'cl=moredetails';
02590 
02591             // not always it is okey, as not all the time active category is the same as primary article cat.
02592             if ( $sActCat = oxConfig::getParameter( 'cnid' ) ) {
02593                 $this->_sMoreDetailLink .= '&amp;cnid='.$sActCat;
02594             }
02595             $this->_sMoreDetailLink .= '&amp;anid='.$this->getId();
02596             $this->_sMoreDetailLink = $this->_sMoreDetailLink;
02597         }
02598 
02599         return $this->_sMoreDetailLink;
02600     }
02601 
02607     public function getToBasketLink()
02608     {
02609         if ( $this->_sToBasketLink == null ) {
02610             $myConfig = $this->getConfig();
02611 
02612             if ( oxUtils::getInstance()->isSearchEngine() ) {
02613                 $this->_sToBasketLink = $this->getLink();
02614             } else {
02615                 // and assign special article values
02616                 $this->_sToBasketLink = $myConfig->getShopHomeURL();
02617 
02618                 // override some classes as these should never showup
02619                 $sActClass = oxConfig::getParameter( 'cl' );
02620                 if ( $sActClass == 'thankyou') {
02621                     $sActClass = 'basket';
02622                 }
02623                 $this->_sToBasketLink .= 'cl='.$sActClass;
02624 
02625                 // this is not very correct
02626                 if ( $sActCat = oxConfig::getParameter( 'cnid' ) ) {
02627                     $this->_sToBasketLink .= '&amp;cnid='.$sActCat;
02628                 }
02629 
02630                 $this->_sToBasketLink .= '&amp;fnc=tobasket&amp;aid='.$this->getId().'&amp;anid='.$this->getId();
02631 
02632                 if ( $sTpl = basename( oxConfig::getParameter( 'tpl' ) ) ) {
02633                     $this->_sToBasketLink .= '&amp;tpl='.$sTpl;
02634                 }
02635             }
02636         }
02637 
02638         return $this->_sToBasketLink;
02639     }
02640 
02646     public function getStockStatus()
02647     {
02648         return $this->_iStockStatus;
02649     }
02650 
02656     public function getDeliveryDate()
02657     {
02658         if ( $this->oxarticles__oxdelivery->value != '0000-00-00') {
02659             return oxUtilsDate::getInstance()->formatDBDate( $this->oxarticles__oxdelivery->value);
02660         }
02661         return false;
02662     }
02663 
02669     public function getFTPrice()
02670     {
02671         if ( $oPrice = $this->getTPrice() ) {
02672             if ( $oPrice->getBruttoPrice() ) {
02673                 return oxLang::getInstance()->formatCurrency( oxUtils::getInstance()->fRound($oPrice->getBruttoPrice()));
02674             }
02675         }
02676     }
02677 
02683     public function getFPrice()
02684     {
02685         if ( $oPrice = $this->getPrice() ) {
02686             return $this->getPriceFromPrefix().oxLang::getInstance()->formatCurrency( $oPrice->getBruttoPrice() );
02687         }
02688     }
02689 
02695     public function getPricePerUnit()
02696     {
02697         return $this->_fPricePerUnit;
02698     }
02699 
02705     public function isParentNotBuyable()
02706     {
02707         return $this->_blNotBuyableParent;
02708     }
02709 
02715     public function isNotBuyable()
02716     {
02717         return $this->_blNotBuyable;
02718     }
02719 
02727     public function setBuyableState( $blBuyable = false )
02728     {
02729         $this->_blNotBuyable = !$blBuyable;
02730     }
02731 
02740     public function getVariantList()
02741     {
02742         return $this->getVariants();
02743     }
02744 
02752     public function setSelectlist( $aSelList )
02753     {
02754         $this->_aDispSelList = $aSelList;
02755     }
02756 
02764     public function getPictureUrl( $iIndex = '' )
02765     {
02766         if ( $iIndex ) {
02767 
02768             if ( !$this->_hasGeneratedImage( $iIndex ) ) {
02769                 $this->_generateImages( $iIndex );
02770             }
02771 
02772             $sPic = $iIndex . "/" . $this->_getPictureName( $iIndex );
02773 
02774             return $this->getConfig()->getPictureUrl( $sPic, $this->isAdmin() );
02775         }
02776     }
02777 
02785     protected function _getPictureName( $iIndex = '' )
02786     {
02787         if ( !$this->_isFieldEmpty( "oxarticles__oxpic".$iIndex ) ) {
02788             $sPicName = basename($this->{"oxarticles__oxpic".$iIndex}->value);
02789         } else {
02790             $sPicName = "nopic.jpg";
02791         }
02792 
02793         return $sPicName;
02794     }
02795 
02804     public function getIconUrl( $iIndex = '')
02805     {
02806         if ( $this->_isFieldEmpty( "oxarticles__oxicon" ) ) {
02807 
02808             $iIconIndex = ( $iIndex ) ? $iIndex : 1;
02809             //generating new images if needed
02810             if ( !$this->_hasGeneratedImage( $iIconIndex ) ) {
02811                 $this->_generateImages( $iIconIndex );
02812             }
02813         }
02814 
02815         $sIconName = $this->_getIconName( $iIndex );
02816 
02817         if ( !$iIndex ) {
02818             $sPic = "icon/" . basename( $sIconName );
02819         } else {
02820             $sPic = $iIndex . "/" . basename( $sIconName );
02821         }
02822 
02823         return $this->getConfig()->getIconUrl( $sPic, $this->isAdmin() );
02824     }
02825 
02834     protected function _getIconName( $iIndex = '' )
02835     {
02836         $oPictureHandler = oxPictureHandler::getInstance();
02837         $sIconName = "nopic_ico.jpg";
02838 
02839         if ( !$iIndex ) {
02840             if ( !$this->_isFieldEmpty( "oxarticles__oxicon" ) ) {
02841                 $sIconName = basename( $this->oxarticles__oxicon->value );
02842             } elseif ( $this->_hasGeneratedImage( 1 ) && $this->_hasMasterImage( 1 )  && $this->oxarticles__oxpic1->value ) {
02843                 $sIconName = $oPictureHandler->getMainIconName( $this->oxarticles__oxpic1->value );
02844             }
02845         } else {
02846             if ( $this->_hasGeneratedImage( $iIndex ) && $this->{"oxarticles__oxpic".$iIndex}->value ) {
02847                 $sIconName = $oPictureHandler->getIconName( $this->{"oxarticles__oxpic".$iIndex}->value );
02848             }
02849         }
02850 
02851         return $sIconName;
02852     }
02853 
02859     public function getThumbnailUrl()
02860     {
02861         if ( $this->_isFieldEmpty( "oxarticles__oxthumb" ) ) {
02862             //generating new images if needed
02863             if ( !$this->_hasGeneratedImage( 1 ) ) {
02864                 $this->_generateImages( 1 );
02865             }
02866         }
02867 
02868         $sPic = "0/" . $this->_getThumbnailName();
02869 
02870         return $this->getConfig()->getPictureUrl( $sPic, $this->isAdmin() );
02871     }
02872 
02878     protected function _getThumbnailName()
02879     {
02880         if ( !$this->_isFieldEmpty( "oxarticles__oxthumb" ) ) {
02881             $sThumbName = basename($this->oxarticles__oxthumb->value);
02882         } else {
02883             if ( $this->_hasGeneratedImage( 1 ) && $this->_hasMasterImage( 1 ) && $this->oxarticles__oxpic1->value ) {
02884                 $oPictureHandler = oxPictureHandler::getInstance();
02885                 $sThumbName = $oPictureHandler->getThumbName( $this->oxarticles__oxpic1->value );
02886             } else {
02887                 $sThumbName = "nopic.jpg";
02888             }
02889         }
02890 
02891         return $sThumbName;
02892     }
02893 
02901     public function getZoomPictureUrl( $iIndex = '' )
02902     {
02903         $iIndex = (int) $iIndex;
02904         if ( $iIndex > 0) {
02905             //generating new images if needed
02906             if ( $this->_isFieldEmpty( "oxarticles__oxzoom" . $iIndex ) ) {
02907                 //generating new images if needed
02908                 if ( !$this->_hasGeneratedImage( $iIndex ) ) {
02909                     $this->_generateImages( $iIndex );
02910                 }
02911             }
02912 
02913             $sPic = "z{$iIndex}/" . $this->_getZoomPictureName( $iIndex );
02914 
02915             return $this->getConfig()->getPictureUrl( $sPic, $this->isAdmin() );
02916         }
02917     }
02918 
02926     protected function _getZoomPictureName( $iIndex = '' )
02927     {
02928         $sZoomField = "oxarticles__oxzoom" . $iIndex;
02929 
02930         if ( !$this->_isFieldEmpty( $sZoomField ) ) {
02931             $sZoomName = basename( $this->$sZoomField->value );
02932         } else {
02933             if ( $this->_hasGeneratedImage( $iIndex ) && $this->_hasMasterImage( $iIndex ) && $this->{"oxarticles__oxpic".$iIndex}->value ) {
02934                 $oPictureHandler = oxPictureHandler::getInstance();
02935                 $sZoomName = $oPictureHandler->getZoomName( $this->{"oxarticles__oxpic".$iIndex}->value, $iIndex );
02936             } else {
02937                 $sZoomName = "nopic.jpg";
02938             }
02939         }
02940 
02941         return $sZoomName;
02942     }
02943 
02949     public function getFileUrl()
02950     {
02951         return $this->getConfig()->getPictureUrl( '0/' );
02952     }
02953 
02959     public function getPriceFromPrefix()
02960     {
02961         $sPricePrefics = '';
02962         if ( $this->_blIsRangePrice) {
02963             $sPricePrefics = oxLang::getInstance()->translateString('priceFrom').' ';
02964         }
02965 
02966         return $sPricePrefics;
02967     }
02968 
02977     protected function _saveArtLongDesc($iLang, $sValue)
02978     {
02979         $oDB = oxDb::getDb();
02980         $iLang = (int) $iLang;
02981         $sLangField = ($iLang > '0') ? '_'.$iLang : '';
02982         $sLongDesc = $oDB->quote($sValue);
02983         $sLongDescSQL = "insert into oxartextends (oxartextends.OXID, oxartextends.OXLONGDESC{$sLangField})
02984                        VALUES ('".$this->getId()."', {$sLongDesc})
02985                        ON DUPLICATE KEY update oxartextends.OXLONGDESC{$sLangField} = {$sLongDesc} ";
02986 
02987         $oDB->execute($sLongDescSQL);
02988     }
02989 
02995     protected function _skipSaveFields()
02996     {
02997         $myConfig = $this->getConfig();
02998 
02999         $this->_aSkipSaveFields = array();
03000 
03001         $this->_aSkipSaveFields[] = 'oxtimestamp';
03002         $this->_aSkipSaveFields[] = 'oxlongdesc';
03003         $this->_aSkipSaveFields[] = 'oxinsert';
03004 
03005         if ( !isset( $this->oxarticles__oxparentid->value) || $this->oxarticles__oxparentid->value == '') {
03006             $this->_aSkipSaveFields[] = 'oxparentid';
03007         }
03008 
03009     }
03010 
03020     protected function _mergeDiscounts( $aDiscounts, $aItemDiscounts)
03021     {
03022         foreach ( $aItemDiscounts as $sKey => $oDiscount ) {
03023             // add prices of the same discounts
03024             if ( array_key_exists ($sKey, $aDiscounts) ) {
03025                 $aDiscounts[$sKey]->dDiscount += $oDiscount->dDiscount;
03026             } else {
03027                 $aDiscounts[$sKey] = $oDiscount;
03028             }
03029         }
03030         return $aDiscounts;
03031     }
03032 
03038     protected function _getGroupPrice()
03039     {
03040         $dPrice = $this->oxarticles__oxprice->value;
03041 
03042         $oUser = $this->getArticleUser();
03043         if ( $oUser ) {
03044             if ( $oUser->inGroup( 'oxidpricea' ) ) {
03045                 $dPrice = $this->oxarticles__oxpricea->value;
03046             } elseif ( $oUser->inGroup( 'oxidpriceb' ) ) {
03047                 $dPrice = $this->oxarticles__oxpriceb->value;
03048             } elseif ( $oUser->inGroup( 'oxidpricec' ) ) {
03049                 $dPrice = $this->oxarticles__oxpricec->value;
03050             }
03051         }
03052 
03053         // #1437/1436C - added config option, and check for zero A,B,C price values
03054         if ( $this->getConfig()->getConfigParam( 'blOverrideZeroABCPrices' ) && (double) $dPrice == 0 ) {
03055             $dPrice = $this->oxarticles__oxprice->value;
03056         }
03057 
03058         return $dPrice;
03059     }
03060 
03069     protected function _getAmountPrice($dAmount = 1)
03070     {
03071         $myConfig = $this->getConfig();
03072 
03073         startProfile( "_getAmountPrice" );
03074 
03075         $dPrice = $this->_getGroupPrice();
03076         $oAmtPrices = $this->_getAmountPriceList();
03077         foreach ($oAmtPrices as $oAmPrice) {
03078             if ($oAmPrice->oxprice2article__oxamount->value <= $dAmount
03079                     && $dAmount <= $oAmPrice->oxprice2article__oxamountto->value
03080                     && $dPrice > $oAmPrice->oxprice2article__oxaddabs->value ) {
03081                 $dPrice = $oAmPrice->oxprice2article__oxaddabs->value;
03082             }
03083         }
03084 
03085         stopProfile( "_getAmountPrice" );
03086         return $dPrice;
03087     }
03088 
03097     protected function _modifySelectListPrice( $dPrice, $aChosenList = null )
03098     {
03099         $myConfig = $this->getConfig();
03100         // #690
03101         if ( $myConfig->getConfigParam( 'bl_perfLoadSelectLists' ) && $myConfig->getConfigParam( 'bl_perfUseSelectlistPrice' ) ) {
03102 
03103             $aSelLists = $this->getSelectLists();
03104             foreach ( $aSelLists as $key => $aSel) {
03105                 if ( isset( $aChosenList[$key]) && isset($aSel[$aChosenList[$key]] ) ) {
03106                     $oSel = $aSel[$aChosenList[$key]];
03107                     if ( $oSel->priceUnit =='abs' ) {
03108                         $dPrice += $oSel->price;
03109                     } elseif ( $oSel->priceUnit =='%' ) {
03110                         $dPrice += oxPrice::percent( $dPrice, $oSel->price );
03111                     }
03112                 }
03113             }
03114         }
03115         return $dPrice;
03116     }
03117 
03118 
03126     protected function _fillAmountPriceList($oAmPriceList)
03127     {
03128         $myConfig = $this->getConfig();
03129         $myUtils  = oxUtils::getInstance();
03130 
03131         //modifying price
03132         $oCur = $myConfig->getActShopCurrencyObject();
03133 
03134         $oUser = $this->getArticleUser();
03135 
03136         $oDiscountList = oxDiscountList::getInstance();
03137         $aDiscountList = $oDiscountList->getArticleDiscounts( $this, $oUser );
03138 
03139         $oLowestPrice = null;
03140 
03141         $dBasePrice = $this->_getGroupPrice();
03142         $oLang = oxLang::getInstance();
03143 
03144         $dArticleVat = null;
03145         if ( !$myConfig->getConfigParam( 'bl_perfCalcVatOnlyForBasketOrder' ) ) {
03146             $dArticleVat = $this->getArticleVat();
03147         }
03148 
03149         // trying to find lowest price value
03150         foreach ($oAmPriceList as $sId => $oItem) {
03151             $oItemPrice = oxNew( 'oxprice' );
03152             if ( $oItem->oxprice2article__oxaddabs->value) {
03153                 $oItemPrice->setPrice( $oItem->oxprice2article__oxaddabs->value );
03154                 $oDiscountList->applyDiscounts( $oItemPrice, $aDiscountList );
03155                 $this->_applyCurrency( $oItemPrice, $oCur );
03156             } else {
03157                 $oItemPrice->setPrice( $dBasePrice );
03158                 $oItemPrice->subtractPercent( $oItem->oxprice2article__oxaddperc->value );
03159             }
03160 
03161             if (isset($dArticleVat)) {
03162                 $this->_applyVAT($oItemPrice, $dArticleVat);
03163             }
03164 
03165             if (!$oLowestPrice) {
03166                 $oLowestPrice = $oItemPrice;
03167             } elseif ($oLowestPrice->getBruttoPrice() > $oItemPrice->getBruttoPrice()) {
03168                 $oLowestPrice = $oItemPrice;
03169             }
03170 
03171             $oAmPriceList[$sId]->oxprice2article__oxaddabs = new oxField( $oLang->formatCurrency( $myUtils->fRound( $oItemPrice->getBruttoPrice(), $oCur ) ) );
03172             $oAmPriceList[$sId]->fnetprice  = $oLang->formatCurrency( $myUtils->fRound($oItemPrice->getNettoPrice(), $oCur ) );
03173             $oAmPriceList[$sId]->fbrutprice = $oLang->formatCurrency( $myUtils->fRound($oItemPrice->getBruttoPrice(), $oCur ) );
03174         }
03175 
03176         $this->_dAmountPrice = $myUtils->fRound( $oLowestPrice->getBruttoPrice() );
03177         return $oAmPriceList;
03178     }
03179 
03185     protected function _getVariantsIds()
03186     {
03187         $aSelect = array();
03188         $oRs = oxDb::getDb(true)->execute( "select oxid from oxarticles where oxparentid = '".$this->getId()."' " );
03189         if ( $oRs != false && $oRs->recordCount() > 0 ) {
03190             while (!$oRs->EOF) {
03191                 $aSelect[] = $oRs->fields['oxid'];
03192                 $oRs->moveNext();
03193             }
03194         }
03195         return $aSelect;
03196     }
03197 
03203     public function getArticleVat()
03204     {
03205         if (!isset($this->_dArticleVat)) {
03206             $this->_dArticleVat = oxVatSelector::getInstance()->getArticleVat( $this );
03207         }
03208         return $this->_dArticleVat;
03209     }
03210 
03219     protected function _applyVAT( oxPrice $oPrice, $dVat )
03220     {
03221         startProfile(__FUNCTION__);
03222         $oPrice->setVAT( $dVat );
03223         if ( ($dVat = oxVatSelector::getInstance()->getArticleUserVat($this)) !== false ) {
03224             $oPrice->setUserVat( $dVat );
03225         }
03226         stopProfile(__FUNCTION__);
03227     }
03228 
03236     public function applyVats( oxPrice $oPrice )
03237     {
03238         $this->_applyVAT($oPrice, $this->getArticleVat() );
03239     }
03240 
03251     protected function _applyDiscounts( $oPrice, $aDiscounts )
03252     {
03253         $oDiscountList = oxDiscountList::getInstance();
03254         $oDiscountList->applyDiscounts( $oPrice, $aDiscounts );
03255     }
03256 
03264     public function applyDiscountsForVariant( $oPrice )
03265     {
03266         // apply discounts
03267         if ( !$this->skipDiscounts() ) {
03268             $oDiscountList = oxDiscountList::getInstance();
03269             $oDiscountList->applyDiscounts( $oPrice, $oDiscountList->getArticleDiscounts( $this, $this->getArticleUser() ) );
03270         }
03271     }
03272 
03281     protected function _applyCurrency(oxPrice $oPrice, $oCur = null )
03282     {
03283         if ( !$oCur ) {
03284             $oCur = $this->getConfig()->getActShopCurrencyObject();
03285         }
03286 
03287         $oPrice->multiply($oCur->rate);
03288     }
03289 
03290 
03299     protected function _getAttribsString(&$sAttribs, &$iCnt)
03300     {
03301         // we do not use lists here as we dont need this overhead right now
03302         $oDB = oxDb::getDb(true);
03303         $sSelect =  'select oxattrid from oxobject2attribute where oxobject2attribute.oxobjectid="'.$this->getId().'" ';
03304         $sAttribs = '';
03305         $blSep = false;
03306         $rs = $oDB->execute( $sSelect);
03307         $iCnt = 0;
03308         if ($rs != false && $rs->recordCount() > 0) {
03309             while (!$rs->EOF) {
03310                 if ( $blSep) {
03311                     $sAttribs .= ' or ';
03312                 }
03313                 $sAttribs .= 't1.oxattrid = '.$oDB->quote($rs->fields['oxattrid']).' ';
03314                 $blSep = true;
03315                 $iCnt++;
03316                 $rs->moveNext();
03317             }
03318         }
03319     }
03320 
03329     protected function _getSimList($sAttribs, $iCnt)
03330     {
03331         $myConfig = $this->getConfig();
03332         $oDB      = oxDb::getDb(true);
03333 
03334         // #523A
03335         $iAttrPercent = $myConfig->getConfigParam( 'iAttributesPercent' )/100;
03336         // 70% same attributes
03337         if ( !$iAttrPercent || $iAttrPercent < 0 || $iAttrPercent > 1) {
03338             $iAttrPercent = 0.70;
03339         }
03340         // #1137V iAttributesPercent = 100 doesnt work
03341         $iHitMin = ceil( $iCnt * $iAttrPercent );
03342 
03343         // we do not use lists here as we dont need this overhead right now
03344         $aList= array();
03345         $sSelect =  "select oxobjectid, count(*) as cnt from oxobject2attribute as t1 where
03346                     ( $sAttribs )
03347                     and t1.oxobjectid != '".$this->oxarticles__oxid->value."'
03348                     group by t1.oxobjectid having count(*) >= $iHitMin ";
03349 
03350         $rs = $oDB->selectLimit( $sSelect, 20, 0);
03351         if ($rs != false && $rs->recordCount() > 0) {
03352             while (!$rs->EOF) {
03353                 $oTemp = new stdClass();    // #663
03354                 $oTemp->cnt = $rs->fields['cnt'];
03355                 $oTemp->id  = $rs->fields['oxobjectid'];
03356                 $aList[] = $oTemp;
03357                 $rs->moveNext();
03358             }
03359         }
03360         return $aList;
03361     }
03362 
03371     protected function _generateSimListSearchStr($sArticleTable, $aList)
03372     {
03373         $myConfig = $this->getConfig();
03374         $sFieldList = $this->getSelectFields();
03375         $sSearch = "select $sFieldList from $sArticleTable where ".$this->getSqlActiveSnippet()."  and $sArticleTable.oxissearch = 1 and $sArticleTable.oxid in ( ";
03376         $blSep = false;
03377         $iCnt = 0;
03378         $oDb = oxDb::getDb();
03379         foreach ( $aList as $oTemp) {
03380             if ( $blSep) {
03381                 $sSearch .= ',';
03382             }
03383             $sSearch .= $oDb->quote($oTemp->id);
03384             $blSep = true;
03385             if ( $iCnt >= $myConfig->getConfigParam( 'iNrofSimilarArticles' ) ) {
03386                 break;
03387             }
03388             $iCnt++;
03389         }
03390 
03391         //#1741T
03392         //$sSearch .= ") and $sArticleTable.oxparentid = '' ";
03393         $sSearch .= ') ';
03394 
03395         // #524A -- randomizing articles in attribute list
03396         $sSearch .= ' order by rand() ';
03397 
03398         return $sSearch;
03399     }
03400 
03409     protected function _generateSearchStr($sOXID, $blSearchPriceCat = false )
03410     {
03411         $sCatView = getViewName( 'oxcategories' );
03412         $sO2CView = getViewName( 'oxobject2category' );
03413 
03414         // we do not use lists here as we dont need this overhead right now
03415         if ( !$blSearchPriceCat ) {
03416             $sSelect  = "select {$sCatView}.* from {$sO2CView} as oxobject2category left join {$sCatView} on
03417                          {$sCatView}.oxid = oxobject2category.oxcatnid
03418                          where oxobject2category.oxobjectid=".oxDb::getDb()->quote($sOXID)." and {$sCatView}.oxid is not null ";
03419         } else {
03420             $sSelect  = "select {$sCatView}.* from {$sCatView} where
03421                          '{$this->oxarticles__oxprice->value}' >= {$sCatView}.oxpricefrom and
03422                          '{$this->oxarticles__oxprice->value}' <= {$sCatView}.oxpriceto ";
03423         }
03424         return $sSelect;
03425     }
03426 
03432     protected function _generateSearchStrForCustomerBought()
03433     {
03434         $sArtTable = $this->_getObjectViewName( 'oxarticles' );
03435         $sOrderArtTable = getViewName( 'oxorderarticles' );
03436 
03437         // fetching filter params
03438         $sIn = " '{$this->oxarticles__oxid->value}' ";
03439         if ( $this->oxarticles__oxparentid->value ) {
03440 
03441             // adding article parent
03442             $sIn .= ", '{$this->oxarticles__oxparentid->value}' ";
03443             $sParentIdForVariants = $this->oxarticles__oxparentid->value;
03444 
03445         } else {
03446             $sParentIdForVariants = $this->getId();
03447         }
03448 
03449         // adding variants
03450         $oDb = oxDb::getDb(true);
03451         $oRs = $oDb->execute( "select oxid from {$sArtTable} where oxparentid = ".$oDb->quote($sParentIdForVariants)." and oxid != ".$oDb->quote($this->oxarticles__oxid->value)." " );
03452         if ( $oRs != false && $oRs->recordCount() > 0) {
03453             while ( !$oRs->EOF ) {
03454                 $sIn .= ", ".$oDb->quote(current( $oRs->fields ))." ";
03455                 $oRs->moveNext();
03456             }
03457         }
03458 
03459         $iLimit = (int) $this->getConfig()->getConfigParam( 'iNrofCustomerWhoArticles' );
03460         $iLimit = $iLimit?( $iLimit * 10 ): 50;
03461 
03462         // building sql (optimized)
03463         $sQ = "select distinct {$sArtTable}.* from (
03464                    select d.oxorderid as suborderid from {$sOrderArtTable} as d use index ( oxartid ) where d.oxartid in ( {$sIn} ) limit {$iLimit}
03465                ) as suborder
03466                left join {$sOrderArtTable} force index ( oxorderid ) on suborder.suborderid = {$sOrderArtTable}.oxorderid
03467                left join {$sArtTable} on {$sArtTable}.oxid = {$sOrderArtTable}.oxartid
03468                where {$sArtTable}.oxid not in ( {$sIn} )
03469                and ( {$sArtTable}.oxissearch = 1 or {$sArtTable}.oxparentid <> '' ) and ".$this->getSqlActiveSnippet();
03470 
03471         /* non optimized, but could be used if index forcing is not supported
03472         // building sql
03473         $sQ = "select distinct {$sArtTable}.* from {$sOrderArtTable}, {$sArtTable} where {$sOrderArtTable}.oxorderid in (
03474                    select {$sOrderArtTable}.oxorderid from {$sOrderArtTable} where {$sOrderArtTable}.oxartid in ( {$sIn} )
03475                ) and {$sArtTable}.oxid = {$sOrderArtTable}.oxartid and {$sArtTable}.oxid not in ( {$sIn} )
03476                and ( {$sArtTable}.oxissearch = 1 or {$sArtTable}.oxparentid <> '' )
03477                and ".$this->getSqlActiveSnippet();
03478         */
03479 
03480         return $sQ;
03481     }
03482 
03492     protected function _generateSelectCatStr($sOXID, $sCatId, $dPriceFromTo = false)
03493     {
03494         $sCategoryView = getViewName('oxcategories');
03495         $sO2CView = getViewName('oxobject2category');
03496         $sLangPrefix = (($this->getLanguage())?'_'.$this->getLanguage():'');
03497 
03498         $oDb    = oxDb::getDb();
03499         $sOXID  = $oDb->quote($sOXID);
03500         $sCatId = $oDb->quote($sCatId);
03501 
03502         if (!$dPriceFromTo) {
03503             $sSelect  = "select oxobject2category.oxcatnid from $sO2CView as oxobject2category ";
03504             $sSelect .= "left join $sCategoryView as oxcategories on oxcategories.oxid = oxobject2category.oxcatnid ";
03505             $sSelect .= "where oxobject2category.oxcatnid=$sCatId and oxobject2category.oxobjectid=$sOXID ";
03506             $sSelect .= "and oxcategories.oxactive$sLangPrefix = 1 order by oxobject2category.oxtime ";
03507         } else {
03508             $dPriceFromTo = $oDb->quote($dPriceFromTo);
03509             $sSelect  = "select oxcategories.oxid from $sCategoryView as oxcategories where ";
03510             $sSelect .= "oxcategories.oxid=$sCatId and $dPriceFromTo >= oxcategories.oxpricefrom and ";
03511             $sSelect .= "$dPriceFromTo <= oxcategories.oxpriceto ";
03512         }
03513         return $sSelect;
03514     }
03515 
03521     protected function _getAmountPriceList()
03522     {
03523         if ($this->_oAmountPriceList) {
03524             return $this->_oAmountPriceList;
03525         }
03526 
03527         $myConfig = $this->getConfig();
03528 
03529         $sArtID  = $this->getId();
03530 
03531         // #1690C - Scale prices and variants
03532         if ( !$this->isAdmin() && $myConfig->getConfigParam( 'blVariantInheritAmountPrice' ) && $this->oxarticles__oxparentid->value ) {
03533             $sArtID = $this->oxarticles__oxparentid->value;
03534         }
03535 
03536         $sArtID = mysql_real_escape_string($sArtID);
03537 
03538         //collecting assigned to article amount-price list
03539         $oAmPriceList = oxNew( 'oxlist');
03540         $oAmPriceList->init('oxbase', 'oxprice2article');
03541 
03542         $sShopID = $myConfig->getShopID();
03543         if ( $myConfig->getConfigParam( 'blMallInterchangeArticles' ) ) {
03544             $sShopSelect = '1';
03545         } else {
03546             $sShopSelect = " oxshopid =  '$sShopID' ";
03547         }
03548 
03549         $oAmPriceList->selectString( "select * from oxprice2article where oxartid = '$sArtID' and $sShopSelect order by oxamount ");
03550 
03551         // prepare abs prices if currently having percentages
03552         $oBasePrice = $this->_getGroupPrice();
03553         foreach ($oAmPriceList as $oAmPrice) {
03554             if ($oAmPrice->oxprice2article__oxaddperc->value) {
03555                 $oAmPrice->oxprice2article__oxaddabs = new oxField(oxPrice::percent( $oBasePrice, 100 - $oAmPrice->oxprice2article__oxaddperc->value ), oxField::T_RAW);
03556             }
03557         }
03558 
03559         $this->_oAmountPriceList = $oAmPriceList;
03560         return $oAmPriceList;
03561     }
03562 
03570     protected function _isFieldEmpty($sFieldName)
03571     {
03572         $mValue = $this->$sFieldName->value;
03573 
03574         if (is_null($mValue)) {
03575             return true;
03576         }
03577 
03578         if ($mValue === '') {
03579             return true;
03580         }
03581 
03582         $aDoubleCopyFields = array('oxarticles__oxprice',
03583                                        'oxarticles__oxvat');
03584 
03585         if (!$mValue && in_array($sFieldName, $aDoubleCopyFields)) {
03586             return true;
03587         }
03588 
03589 
03590         if (!strcmp($mValue, '0000-00-00 00:00:00') || !strcmp($mValue, '0000-00-00')) {
03591             return true;
03592         }
03593 
03594         $sFieldName = strtolower($sFieldName);
03595 
03596         if ( $sFieldName == 'oxarticles__oxicon' && strpos($mValue, "nopic_ico.jpg") !== false ) {
03597             return true;
03598         }
03599 
03600         if ( strpos($mValue, "nopic.jpg") !== false && ($sFieldName == 'oxarticles__oxthumb' || substr($sFieldName, 0, 17) == 'oxarticles__oxpic' || substr($sFieldName, 0, 18) == 'oxarticles__oxzoom') ) {
03601             return true;
03602         }
03603 
03604         return false;
03605     }
03606 
03614     protected function _assignParentFieldValue($sFieldName)
03615     {
03616         if (!($oParentArticle = $this->getParentArticle())) {
03617             return;
03618         }
03619 
03620         $sCopyFieldName = $this->_getFieldLongName($sFieldName);
03621 
03622         // assigning only theese which parent article has
03623         if ( $oParentArticle->$sCopyFieldName != null ) {
03624 
03625             // only overwrite database values
03626             if ( substr( $sCopyFieldName, 0, 12) != 'oxarticles__') {
03627                 continue;
03628             }
03629 
03630             //do not copy certain fields
03631             if (in_array($sCopyFieldName, $this->_aNonCopyParentFields)) {
03632                 return;
03633             }
03634 
03635             //COPY THE VALUE
03636             // assigning images from parent only if variant has no master image (#1807)
03637             if ( stristr($sCopyFieldName, '_oxthumb') || stristr($sCopyFieldName, '_oxicon') ) {
03638                 if ( $this->_isFieldEmpty( $sCopyFieldName ) && !$this->_hasMasterImage( 1 ) ) {
03639                     $this->$sCopyFieldName = clone $oParentArticle->$sCopyFieldName;
03640                 }
03641             } elseif ( stristr($sCopyFieldName, '_oxzoom') ) {
03642                 // for zoom images checking master image with specified index
03643                 $iIndex = (int) str_ireplace( "oxzoom", "", $sFieldName );
03644                 if ( $this->_isFieldEmpty( $sCopyFieldName ) && !$this->_hasMasterImage( $iIndex ) ) {
03645                     $this->$sCopyFieldName = clone $oParentArticle->$sCopyFieldName;
03646                 }
03647             } elseif ($this->_isFieldEmpty($sCopyFieldName) || in_array( $sCopyFieldName, $this->_aCopyParentField ) ) {
03648                 $this->$sCopyFieldName = clone $oParentArticle->$sCopyFieldName;
03649             }
03650 
03651         }
03652     }
03653 
03659     public function getParentArticle()
03660     {
03661         if ( ( $sParentId = $this->oxarticles__oxparentid->value ) ) {
03662             if ( !isset( self::$_aLoadedParents[$sParentId] ) ) {
03663                 self::$_aLoadedParents[$sParentId] = oxNew( 'oxarticle' );
03664                 self::$_aLoadedParents[$sParentId]->_blSkipAbPrice  = true;
03665                 self::$_aLoadedParents[$sParentId]->_blLoadPrice    = false;
03666                 self::$_aLoadedParents[$sParentId]->_blLoadVariants = false;
03667                 self::$_aLoadedParents[$sParentId]->load( $sParentId );
03668             }
03669             return self::$_aLoadedParents[$sParentId];
03670         }
03671     }
03672 
03680     protected function _getParentAricle()
03681     {
03682         return $this->getParentArticle();
03683     }
03684 
03690     protected function _assignParentFieldValues()
03691     {   startProfile('articleAssignParentInternal');
03692         if ( $this->oxarticles__oxparentid->value) {
03693             // yes, we are in fact a variant
03694             if ( !$this->isAdmin() || ($this->_blLoadParentData && $this->isAdmin() ) ) {
03695                 foreach ($this->_aFieldNames as $sFieldName => $sVal) {
03696                     $this->_assignParentFieldValue($sFieldName);
03697                 }
03698 
03699                 //assing long description
03700                 $oParentArticle = $this->getParentArticle();
03701                 if ( !$this->oxarticles__oxlongdesc->value ) {
03702                     $this->oxarticles__oxlongdesc = $oParentArticle->oxarticles__oxlongdesc;
03703                 }
03704 
03705             }
03706         }
03707         stopProfile('articleAssignParentInternal');
03708 
03709     }
03710 
03716     protected function _assignNotBuyableParent()
03717     {
03718         if ( !$this->getConfig()->getConfigParam( 'blVariantParentBuyable' ) &&
03719              ($this->_blHasVariants || $this->oxarticles__oxvarstock->value || $this->oxarticles__oxvarcount->value )) {
03720             $this->_blNotBuyableParent = true;
03721 
03722         }
03723     }
03724 
03730     protected function _assignAllPictureValues()
03731     {
03732         $myConfig = $this->getConfig();
03733 
03734         $this->_assignPictureValues( "oxarticles__oxicon" );
03735         $this->_assignPictureValues( "oxarticles__oxthumb" );
03736 
03737         $iPicCount = $myConfig->getConfigParam( 'iPicCount' );
03738         for ( $i=1; $i<= $iPicCount; $i++ ) {
03739             $this->_assignPictureValues( "oxarticles__oxpic".$i );
03740         }
03741 
03742         if ( $iZoomPicCount = $myConfig->getConfigParam( 'iZoomPicCount' ) ) {
03743             for ( $i=1; $i<= $iZoomPicCount; $i++ ) {
03744                 $this->_assignPictureValues( "oxarticles__oxzoom".$i );
03745             }
03746         }
03747     }
03748 
03756     protected function _assignPictureValues( $sName='' )
03757     {
03758         if ( $this->isAdmin() || !$sName ) {
03759             return;
03760         }
03761 
03762         $sFieldName = substr_replace( $sName, "", 0, 12);
03763         $myConfig = $this->getConfig();
03764 
03765         // add directories
03766         if ( $sFieldName == 'oxicon' && isset($this->_aFieldNames["oxicon"]) ) {
03767             $this->oxarticles__oxicon = new oxField('icon/'.$this->_getIconName());
03768             return;
03769         }
03770 
03771         if ( $sFieldName == 'oxthumb' && isset($this->_aFieldNames["oxthumb"]) ) {
03772             $this->oxarticles__oxthumb = new oxField('0/'.$this->_getThumbnailName());
03773             return;
03774         }
03775 
03776         $iPicCount = $myConfig->getConfigParam( 'iPicCount' );
03777 
03778         if ( strpos($sFieldName, "oxpic") === 0 && isset($this->_aFieldNames[$sFieldName] ) ) {
03779 
03780             $iIndex = (int) str_ireplace( "oxpic", "", $sFieldName );
03781 
03782             if ( $iIndex > 0 && $iIndex < $iPicCount ) {
03783                 $this->$sName = new oxField( $iIndex . '/'.$this->_getPictureName($iIndex) );
03784                 $this->{$sName.'_ico'} = new oxField( $iIndex . '/'.$this->_getIconName($iIndex) );
03785                 return;
03786             }
03787         }
03788 
03789         if ( strpos($sFieldName, "oxzoom") === 0 ) {
03790 
03791             $iIndex = (int) str_ireplace( "oxzoom", "", $sFieldName );
03792 
03793             // if oxzoom or oxpic field with same index is setted, loading field values
03794             if ( isset($this->_aFieldNames[$sFieldName]) || isset($this->_aFieldNames["oxpic".$iIndex]) ) {
03795                 if ( $iIndex > 0 && $iIndex < $iPicCount ) {
03796                     $this->$sName = new oxField( 'z' . $iIndex.'/'.$this->_getZoomPictureName($iIndex) );
03797                     return;
03798                 }
03799             }
03800         }
03801     }
03802 
03808     protected function _assignStock()
03809     {
03810         $myConfig = $this->getConfig();
03811         // -----------------------------------
03812         // stock
03813         // -----------------------------------
03814 
03815         // #1125 A. must round (using floor()) value taken from database and cast to int
03816         if (!$myConfig->getConfigParam( 'blAllowUnevenAmounts' ) && !$this->isAdmin() ) {
03817             $this->oxarticles__oxstock = new oxField((int) floor($this->oxarticles__oxstock->value));
03818         }
03819         //GREEN light
03820         $this->_iStockStatus = 0;
03821 
03822         // if we have flag /*1 or*/ 4 - we show always green light
03823         if ( $myConfig->getConfigParam( 'blUseStock' ) && /*$this->oxarticles__oxstockflag->value != 1 && */ $this->oxarticles__oxstockflag->value != 4) {
03824             //ORANGE light
03825             $iStock = $this->oxarticles__oxstock->value;
03826 
03827             if ($this->_blNotBuyableParent) {
03828                 $iStock = $this->oxarticles__oxvarstock->value;
03829             }
03830 
03831 
03832             if ( $iStock <= $myConfig->getConfigParam( 'sStockWarningLimit' ) && $iStock > 0) {
03833                 $this->_iStockStatus = 1;
03834             }
03835 
03836             //RED light
03837             if ($iStock <= 0) {
03838                 $this->_iStockStatus = -1;
03839             }
03840         }
03841 
03842 
03843         // stock
03844         if ( $myConfig->getConfigParam( 'blUseStock' ) && ($this->oxarticles__oxstockflag->value == 3 || $this->oxarticles__oxstockflag->value == 2)) {
03845             $iOnStock = $this->oxarticles__oxstock->value;
03846             if ($this->getConfig()->getConfigParam( 'blPsBasketReservationEnabled' )) {
03847                 $iOnStock += $this->getSession()->getBasketReservations()->getReservedAmount($this->getId());
03848             }
03849             if ($iOnStock <= 0) {
03850                 $this->_blNotBuyable = true;
03851             }
03852         }
03853 
03854         //exceptional handling for variant parent stock:
03855         if ($this->_blNotBuyable && $this->oxarticles__oxvarstock->value ) {
03856             $this->_blNotBuyable = false;
03857             //but then at least setting notBuaybleParent to true
03858             $this->_blNotBuyableParent = true;
03859         }
03860 
03861         //special treatment for lists when blVariantParentBuyable config option is set to false
03862         //then we just hide "to basket" button.
03863         //if variants are not loaded in the list and this article has variants and parent is not buyable then this article is not buyable
03864         if ( !$myConfig->getConfigParam( 'blVariantParentBuyable' ) && !$myConfig->getConfigParam( 'blLoadVariants' ) && $this->oxarticles__oxvarstock->value) {
03865             $this->_blNotBuyable = true;
03866         }
03867 
03868         //setting to non buyable when variant list is empty (for example not loaded or inactive) and $this is non buyable parent
03869         if ($this->_blNotBuyableParent && count($this->getVariants()) == 0) {
03870             $this->_blNotBuyable = true;
03871         }
03872     }
03873 
03879     protected function _assignPrices()
03880     {
03881         $myConfig = $this->getConfig();
03882 
03883         // Performance
03884         if ( !$myConfig->getConfigParam( 'bl_perfLoadPrice' ) || !$this->_blLoadPrice ) {
03885             return;
03886         }
03887 
03888         // compute price
03889         $dPrice = $this->getPrice()->getBruttoPrice();
03890 
03891         $oCur = $myConfig->getActShopCurrencyObject();
03892         //price per unit handling
03893         if ((double) $this->oxarticles__oxunitquantity->value && $this->oxarticles__oxunitname->value) {
03894             $this->_fPricePerUnit = oxLang::getInstance()->formatCurrency($dPrice / (double) $this->oxarticles__oxunitquantity->value, $oCur);
03895         }
03896 
03897 
03898         //getting min and max prices of variants
03899         $this->_applyRangePrice();
03900     }
03901 
03907     protected function _assignPersistentParam()
03908     {
03909         // Persistent Parameter Handling
03910         $aPersParam     = oxSession::getVar( 'persparam');
03911         if ( isset( $aPersParam) && isset( $aPersParam[$this->getId()])) {
03912             $this->_aPersistParam = $aPersParam[$this->getId()];
03913         }
03914     }
03915 
03921     protected function _assignDynImageDir()
03922     {
03923         $myConfig = $this->getConfig();
03924 
03925         $sThisShop = $this->oxarticles__oxshopid->value;
03926 
03927         $this->_sDynImageDir   = $myConfig->getPictureUrl( null, false );
03928         $this->dabsimagedir    = $myConfig->getPictureDir( false ); //$sThisShop
03929         $this->nossl_dimagedir = $myConfig->getPictureUrl( null, false, false, null, $sThisShop ); //$sThisShop
03930         $this->ssl_dimagedir   = $myConfig->getPictureUrl( null, false, true, null, $sThisShop ); //$sThisShop
03931     }
03932 
03938     protected function _assignComparisonListFlag()
03939     {
03940         // #657 add a flag if article is on comparisonlist
03941 
03942         $aItems = oxConfig::getParameter('aFiltcompproducts');
03943         if ( isset( $aItems[$this->getId()])) {
03944             $this->_blIsOnComparisonList = true;
03945         }
03946     }
03947 
03953     protected function _assignAttributes()
03954     {
03955         //#1029T load attributes
03956         //#1078S removed check for module "Produktvergleich"
03957         if ( $this->getConfig()->getConfigParam( 'bl_perfLoadAttributes' ) ) {
03958             $this->getAttributes();
03959         }
03960     }
03961 
03962 
03970     protected function _insert()
03971     {
03972         // set oxinsert
03973         $iInsertTime = time();
03974         $now = date('Y-m-d H:i:s', $iInsertTime);
03975         $this->oxarticles__oxinsert    = new oxField( $now );
03976         $this->oxarticles__oxtimestamp = new oxField( $now );
03977         if ( !is_object($this->oxarticles__oxsubclass) || $this->oxarticles__oxsubclass->value == '') {
03978             $this->oxarticles__oxsubclass = new oxField('oxarticle');
03979         }
03980 
03981         return parent::_insert();
03982     }
03983 
03989     protected function _update()
03990     {
03991 
03992         $this->_skipSaveFields();
03993 
03994         $myConfig = $this->getConfig();
03995 
03996 
03997         return parent::_update();
03998     }
03999 
04007     protected function _deleteRecords($sOXID)
04008     {
04009         $oDB = oxDb::getDb();
04010 
04011         $sOXID = $oDB->quote($sOXID);
04012 
04013         //delete the record
04014         $sDelete = 'delete from '.$this->_sCoreTbl.' where oxid = '.$sOXID.' ';
04015         $oDB->execute( $sDelete);
04016 
04017         //remove other records
04018         $sDelete = 'delete from oxobject2article where oxarticlenid = '.$sOXID.' or oxobjectid = '.$sOXID.' ';
04019         $oDB->execute( $sDelete);
04020 
04021         $sDelete = 'delete from oxobject2attribute where oxobjectid = '.$sOXID.' ';
04022         $oDB->execute( $sDelete);
04023 
04024         $sDelete = 'delete from oxobject2category where oxobjectid = '.$sOXID.' ';
04025         $oDB->execute( $sDelete);
04026 
04027         $sDelete = 'delete from oxobject2selectlist where oxobjectid = '.$sOXID.' ';
04028         $oDB->execute( $sDelete);
04029 
04030         $sDelete = 'delete from oxprice2article where oxartid = '.$sOXID.' ';
04031         $oDB->execute( $sDelete);
04032 
04033         $sDelete = 'delete from oxreviews where oxtype="oxarticle" and oxobjectid = '.$sOXID.' ';
04034         $oDB->execute( $sDelete);
04035 
04036         $sDelete = 'delete from oxaccessoire2article where oxobjectid = '.$sOXID.' or oxarticlenid = '.$sOXID.' ';
04037         $oDB->execute( $sDelete);
04038 
04039         //#1508C - deleting oxobject2delivery entries added
04040         $sDelete = 'delete from oxobject2delivery where oxobjectid = '.$sOXID.' and oxtype=\'oxarticles\' ';
04041         $oDB->execute( $sDelete);
04042 
04043         $sDelete = 'delete from oxartextends where oxid = '.$sOXID.' ';
04044         $oDB->execute( $sDelete);
04045 
04046         $sDelete = 'delete from oxactions2article where oxartid = '.$sOXID.' ';
04047         $rs = $oDB->execute( $sDelete );
04048 
04049         $sDelete = 'delete from oxobject2list where oxobjectid = '.$sOXID.' ';
04050         $rs = $oDB->execute( $sDelete );
04051 
04052 
04053         return $rs;
04054     }
04055 
04063     protected function _deleteVariantRecords($sOXID)
04064     {
04065         $oDB = oxDb::getDb();
04066         //collect variants to remove recursively
04067         $sVariants = 'select oxid from '.$this->getViewName().' where oxparentid = '.$oDB->quote($sOXID);
04068         $rs = $oDB->execute( $sVariants);
04069         if ($rs != false && $rs->recordCount() > 0) {
04070             while (!$rs->EOF) {
04071                 $this->delete( $rs->fields[0]);
04072                 $rs->moveNext();
04073             }
04074         }
04075     }
04076 
04086     protected function _resetCacheAndArticleCount( $sOxid )
04087     {
04088         $this->_onChangeResetCounts( $sOxid, $this->oxarticles__oxvendorid->value, $this->oxarticles__oxmanufacturerid->value );
04089     }
04090 
04096     protected function _deletePics()
04097     {
04098         $myUtilsPic = oxUtilsPic::getInstance();
04099         $myConfig   = $this->getConfig();
04100         $oPictureHandler = oxPictureHandler::getInstance();
04101 
04102         //deleting custom main icon
04103         $oPictureHandler->deleteMainIcon( $this );
04104 
04105         //deleting custom thumbnail
04106         $oPictureHandler->deleteThumbnail( $this );
04107 
04108         $sAbsDynImageDir = $myConfig->getAbsDynImageDir();
04109 
04110         // deleting master image and all generated images
04111         $iPicCount = $myConfig->getConfigParam( 'iPicCount' );
04112         for ( $i = 1; $i <= $iPicCount; $i++ ) {
04113             $oPictureHandler->deleteArticleMasterPicture( $this, $i );
04114         }
04115     }
04116 
04126     protected function _onChangeResetCounts( $sOxid, $sVendorId = null, $sManufacturerId = null )
04127     {
04128 
04129         $myUtilsCount = oxUtilsCount::getInstance();
04130 
04131         if ( $sVendorId ) {
04132             $myUtilsCount->resetVendorArticleCount( $sVendorId );
04133         }
04134 
04135         if ( $sManufacturerId ) {
04136             $myUtilsCount->resetManufacturerArticleCount( $sManufacturerId );
04137         }
04138 
04139         //also reseting category counts
04140         $oDb = oxDb::getDb();
04141         $sQ = "select oxcatnid from oxobject2category where oxobjectid = ".$oDb->quote($sOxid);
04142         $oRs = $oDb->execute( $sQ );
04143         if ( $oRs !== false && $oRs->recordCount() > 0) {
04144             while ( !$oRs->EOF ) {
04145                 $myUtilsCount->resetCatArticleCount( $oRs->fields[0] );
04146                 $oRs->moveNext();
04147             }
04148         }
04149     }
04150 
04158     protected function _onChangeUpdateStock( $sParentID )
04159     {
04160         $oDb = oxDb::getDb();
04161         $sParentIdQuoted = $oDb->quote($sParentID);
04162         $sQ = 'select oxstock, oxvendorid, oxmanufacturerid from oxarticles where oxid = '.$sParentIdQuoted;
04163         $rs = $oDb->execute($sQ);
04164         $iOldStock = $rs->fields[0];
04165         $iVendorID = $rs->fields[1];
04166         $iManufacturerID = $rs->fields[2];
04167 
04168         $sQ = 'select sum(oxstock) from oxarticles where oxparentid = '.$sParentIdQuoted.' and '. $this->getSqlActiveSnippet( true ).' and oxstock > 0 ';
04169         $iStock = (float) $oDb->getOne( $sQ );
04170 
04171         $sQ = 'update oxarticles set oxvarstock = '.$iStock.' where oxid = '.$sParentIdQuoted;
04172         $oDb->execute( $sQ );
04173 
04174             //now lets update category counts
04175             //first detect stock status change for this article (to or from 0)
04176             if ( $iStock < 0 ) {
04177                 $iStock = 0;
04178             }
04179             if ( $iOldStock < 0 ) {
04180                 $iOldStock = 0;
04181             }
04182             if ( $this->oxarticles__oxstockflag->value == 2 && $iOldStock xor $iStock ) {
04183                 //means the stock status could be changed (oxstock turns from 0 to 1 or from 1 to 0)
04184                 // so far we leave it like this but later we could move all count resets to one or two functions
04185                 $this->_onChangeResetCounts( $sParentID, $iVendorID, $iManufacturerID );
04186             }
04187     }
04188 
04196     protected function _onChangeStockResetCount( $sOxid )
04197     {
04198         $myConfig = $this->getConfig();
04199 
04200         if ( $myConfig->getConfigParam( 'blUseStock' ) && $this->oxarticles__oxstockflag->value == 2 &&
04201            ( $this->oxarticles__oxstock->value + $this->oxarticles__oxvarstock->value ) <= 0 ) {
04202 
04203                $this->_onChangeResetCounts( $sOxid, $this->oxarticles__oxvendorid->value, $this->oxarticles__oxmanufacturerid->value );
04204         }
04205     }
04206 
04214     protected function _onChangeUpdateVarCount( $sParentID )
04215     {
04216         $oDb = oxDb::getDb();
04217         $sParentIdQuoted = $oDb->quote($sParentID);
04218         $sQ = 'select count(*) as varcount from oxarticles where oxparentid = '.$sParentIdQuoted;
04219         $iVarCount = (int) $oDb->getOne($sQ);
04220 
04221         $sQ = 'update oxarticles set oxvarcount = '.$iVarCount.' where oxid = '.$sParentIdQuoted;
04222         $oDb->execute($sQ);
04223     }
04224 
04232     protected function _onChangeUpdateMinVarPrice( $sParentID )
04233     {
04234         $oDb = oxDb::getDb();
04235         $sParentIdQuoted = $oDb->quote($sParentID);
04236         //#M0000883 (Sarunas)
04237         $sQ = 'select min(oxprice) as varminprice from oxarticles where '.$this->getSqlActiveSnippet(true).' and (oxparentid = '.$sParentIdQuoted;
04238         //#M0000886 (Sarunas)
04239         if ( $this->getConfig()->getConfigParam( 'blVariantParentBuyable' ) ) {
04240             $sQ .= ' or oxid = '.$sParentIdQuoted;
04241         } else {
04242             $sQ .= ' or (oxid = '.$sParentIdQuoted.' and oxvarcount=0)';
04243         }
04244         $sQ .= ')';
04245         $dVarMinPrice = $oDb->getOne($sQ);
04246         if ( $dVarMinPrice ) {
04247             $sQ = 'update oxarticles set oxvarminprice = '.$dVarMinPrice.' where oxid = '.$sParentIdQuoted;
04248             $oDb->execute($sQ);
04249         }
04250     }
04251 
04252 
04258     protected function _applyRangePrice()
04259     {
04260         //#buglist_413 if bl_perfLoadPriceForAddList variant price shouldn't be loaded too
04261         if ( !$this->getConfig()->getConfigParam( 'bl_perfLoadPrice' ) || !$this->_blLoadPrice ) {
04262             return;
04263         }
04264 
04265         $this->_blIsRangePrice = false;
04266 
04267         // if parent is buyable - do not apply range price calcculations
04268         if ($this->_blSkipAbPrice || !$this->_blNotBuyableParent) {
04269             return;
04270         }
04271 
04272         $aPrices = array();
04273 
04274         if (!$this->_blNotBuyableParent) {
04275             $aPrices[] = $this->getPrice()->getBruttoPrice();
04276         }
04277 
04278         $aVariants = $this->getVariants(false);
04279 
04280         if (count($aVariants)) {
04281             foreach ($aVariants as $sKey => $oVariant) {
04282                 $aPrices[] = $oVariant->getPrice()->getBruttoPrice();
04283             }
04284         }
04285 
04286         /*  $oAmPrices = $this->loadAmountPriceInfo();
04287         foreach ($oAmPrices as $oAmPrice) {
04288             $aPrices[] = $oAmPrice->oxprice2article__oxaddabs->value;
04289         }*/
04290 
04291         if (count($aPrices)) {
04292             $dMinPrice = $aPrices[0];
04293             $dMaxPrice = $aPrices[0];
04294             foreach ($aPrices as $dPrice) {
04295                 if ($dMinPrice > $dPrice) {
04296                     $dMinPrice = $dPrice;
04297                 }
04298 
04299                 if ($dMaxPrice < $dPrice) {
04300                     $dMaxPrice = $dPrice;
04301                 }
04302             }
04303         }
04304 
04305         if ($this->_blNotBuyableParent && isset($dMinPrice) && $dMinPrice == $dMaxPrice) {
04306             $this->getPrice()->setBruttoPriceMode();
04307             $this->getPrice()->setPrice($dMinPrice);
04308         }
04309 
04310         if (isset($dMinPrice) && $dMinPrice != $dMaxPrice) {
04311             $this->getPrice()->setBruttoPriceMode();
04312             $this->getPrice()->setPrice($dMinPrice);
04313             $this->_blIsRangePrice = true;
04314         }
04315 
04316         if ( $this->isParentNotBuyable() && !$this->getConfig()->getConfigParam( 'blLoadVariants' )) {
04317             $this->getPrice()->setBruttoPriceMode();
04318             $this->getPrice()->setPrice($this->oxarticles__oxvarminprice->value);
04319             $this->_blIsRangePrice = true;
04320             $this->_calculatePrice( $this->getPrice() );
04321         }
04322     }
04323 
04330     public function getProductId()
04331     {
04332         return $this->getId();
04333     }
04334 
04340     public function getProductParentId()
04341     {
04342         return $this->oxarticles__oxparentid->value;
04343     }
04344 
04350     public function isOrderArticle()
04351     {
04352         return false;
04353     }
04354 
04360     public function isVariant()
04361     {
04362         return (bool) ( isset( $this->oxarticles__oxparentid ) ? $this->oxarticles__oxparentid->value : false );
04363     }
04364 
04370     public function isMdVariant()
04371     {
04372         $oMdVariant = oxNew( "oxVariantHandler" );
04373 
04374         return $oMdVariant->isMdVariant($this);
04375     }
04376 
04384     public function getSqlForPriceCategories($sFields = '')
04385     {
04386         if (!$sFields) {
04387             $sFields = 'oxid';
04388         }
04389         $sSelectWhere = "select $sFields from ".$this->_getObjectViewName('oxcategories')." where";
04390         $sQuotedPrice = oxDb::getDb()->quote( $this->oxarticles__oxprice->value );
04391         return  "$sSelectWhere oxpricefrom != 0 and oxpriceto != 0 and oxpricefrom <= $sQuotedPrice and oxpriceto >= $sQuotedPrice"
04392                ." union $sSelectWhere oxpricefrom != 0 and oxpriceto = 0 and oxpricefrom <= $sQuotedPrice"
04393                ." union $sSelectWhere oxpricefrom = 0 and oxpriceto != 0 and oxpriceto >= $sQuotedPrice";
04394     }
04395 
04403     public function inPriceCategory( $sCatNid )
04404     {
04405         $oDb = oxDb::getDb();
04406 
04407         $sQuotedPrice = oxDb::getDb()->quote( $this->oxarticles__oxprice->value );
04408         $sQuotedCnid = oxDb::getDb()->quote( $sCatNid );
04409         return (bool) $oDb->getOne(
04410             "select 1 from ".$this->_getObjectViewName('oxcategories')." where oxid=$sQuotedCnid and"
04411            ."(   (oxpricefrom != 0 and oxpriceto != 0 and oxpricefrom <= $sQuotedPrice and oxpriceto >= $sQuotedPrice)"
04412            ." or (oxpricefrom != 0 and oxpriceto = 0 and oxpricefrom <= $sQuotedPrice)"
04413            ." or (oxpricefrom = 0 and oxpriceto != 0 and oxpriceto >= $sQuotedPrice)"
04414            .")"
04415         );
04416     }
04417 
04423     public function getMdVariants()
04424     {
04425         if ( $this->_oMdVariants ) {
04426             return $this->_oMdVariants;
04427         }
04428 
04429         $oParentArticle = $this->getParentArticle();
04430         if ( $oParentArticle ) {
04431             $oVariants = $oParentArticle->getVariants();
04432         } else {
04433             $oVariants = $this->getVariants();
04434         }
04435 
04436         $oVariantHandler = oxNew( "oxVariantHandler" );
04437         $this->_oMdVariants = $oVariantHandler->buildMdVariants( $oVariants, $this->getId() );
04438 
04439         return $this->_oMdVariants;
04440     }
04441 
04447     public function getMdSubvariants()
04448     {
04449         return $this->getMdVariants()->getMdSubvariants();
04450     }
04451 
04452 
04461     protected function _generateImages( $iIndex )
04462     {
04463         if ( isset($this->_aFieldNames["oxpic".$iIndex]) && !$this->_hasGeneratedImage( $iIndex ) ) {
04464             if ( !$this->_isFieldEmpty( "oxarticles__oxpic".$iIndex ) && $this->_hasMasterImage( $iIndex ) ) {
04465                 $oPictureHandler = oxPictureHandler::getInstance();
04466                 $oPictureHandler->generateArticlePictures( $this, $iIndex );
04467             }
04468         }
04469     }
04470 
04479     public function updateAmountOfGeneratedPictures( $iTotalGenerated )
04480     {
04481         $sQ = "UPDATE oxarticles SET oxpicsgenerated = '$iTotalGenerated' WHERE oxid = '".$this->getId()."'";
04482         oxDb::getDb()->execute($sQ);
04483 
04484         $this->oxarticles__oxpicsgenerated = new oxField( $iTotalGenerated );
04485     }
04486 
04495     protected function _hasGeneratedImage( $iIndex )
04496     {
04497         if ( $iIndex > (int) $this->oxarticles__oxpicsgenerated->value ) {
04498             return false;
04499         }
04500 
04501         return true;
04502     }
04503 
04511     protected function _hasMasterImage( $iIndex )
04512     {
04513         $sPicName = basename($this->{"oxarticles__oxpic" . $iIndex}->value);
04514 
04515         if ( $sPicName == "nopic.jpg" ) {
04516             return false;
04517         }
04518 
04519         $sMasterPic = $iIndex . "/" . $sPicName;
04520 
04521         if ( $this->getConfig()->getMasterPicturePath( $sMasterPic ) ) {
04522             return true;
04523         }
04524 
04525         return false;
04526     }
04527 
04536     public function getPictureFieldValue( $sFieldName, $iIndex = null )
04537     {
04538         if ( $sFieldName ) {
04539             $sFieldName = "oxarticles__" . $sFieldName . $iIndex;
04540             return $this->$sFieldName->value;
04541         }
04542     }
04543 }