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         }
00514 
00515         //checking for zoom picture
00516         if ( strpos($sName, "oxarticles__oxzoom") === 0 ) {
00517             $this->_assignZoomPictureValues( $sName );
00518             return $this->$sName;
00519         }
00520 
00521         $this->$sName = parent::__get($sName);
00522         if ( $this->$sName ) {
00523             $this->_assignParentFieldValue($sName);
00524         }
00525 
00526         //checking for picture information
00527         if ( $sName == "oxarticles__oxthumb" || $sName == "oxarticles__oxicon" || (strpos($sName, "oxarticles__oxpic") === 0 && $sName != "oxarticles__oxpicsgenerated") ) {
00528             $this->_assignPictureValues( $sName );
00529             return $this->$sName;
00530         }
00531 
00532         return $this->$sName;
00533     }
00534 
00542     public function setId( $sId = null )
00543     {
00544         $sId = parent::setId( $sId );
00545 
00546         // TODO: in oxbase::setId make it to check if exists and update, not recreate, then delete this overload
00547         $this->oxarticles__oxnid = $this->oxarticles__oxid;
00548 
00549         return $sId;
00550     }
00551 
00559     public function getTableNameForActiveSnippet( $blForceCoreTable = false )
00560     {
00561             $sTable = $this->getCoreTableName();
00562 
00563         return $sTable;
00564     }
00565 
00575     public function getActiveCheckQuery( $blForceCoreTable = false )
00576     {
00577         $sTable = $this->getTableNameForActiveSnippet( $blForceCoreTable );
00578 
00579         // check if article is still active
00580         $sQ = " $sTable.oxactive = 1 ";
00581 
00582         // enabled time range check ?
00583         if ( $this->getConfig()->getConfigParam( 'blUseTimeCheck' ) ) {
00584             $sDate = date( 'Y-m-d H:i:s', oxUtilsDate::getInstance()->getTime() );
00585             $sQ = "( $sQ or ( $sTable.oxactivefrom < '$sDate' and $sTable.oxactiveto > '$sDate' ) ) ";
00586         }
00587 
00588         return $sQ;
00589     }
00590 
00604     public function getStockCheckQuery( $blForceCoreTable = false )
00605     {
00606         $myConfig = $this->getConfig();
00607         $sTable = $this->getTableNameForActiveSnippet( $blForceCoreTable );
00608 
00609         $sQ = "";
00610 
00611         //do not check for variants
00612         if ( $myConfig->getConfigParam( 'blUseStock' ) ) {
00613             $sQ = " and ( $sTable.oxstockflag != 2 or ( $sTable.oxstock + $sTable.oxvarstock ) > 0  ) ";
00614             //V #M513: When Parent article is not purchaseble, it's visibility should be displayed in shop only if any of Variants is available.
00615             if ( !$myConfig->getConfigParam( 'blVariantParentBuyable' ) ) {
00616                 $sTimeCheckQ = '';
00617                 if ( $myConfig->getConfigParam( 'blUseTimeCheck' ) ) {
00618                      $sDate = date( 'Y-m-d H:i:s', oxUtilsDate::getInstance()->getTime() );
00619                      $sTimeCheckQ = " or ( art.oxactivefrom < '$sDate' and art.oxactiveto > '$sDate' )";
00620                 }
00621                 $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 ) ) ";
00622             }
00623         }
00624 
00625         return $sQ;
00626     }
00627 
00639     public function getVariantsQuery( $blRemoveNotOrderables, $blForceCoreTable = false  )
00640     {
00641         $sTable = $this->getTableNameForActiveSnippet( $blForceCoreTable );
00642         $sQ = " and $sTable.oxparentid = '".$this->getId()."' ";
00643 
00644         //checking if variant is active and stock status
00645         if ( $this->getConfig()->getConfigParam( 'blUseStock' ) ) {
00646             $sQ .= " and ( $sTable.oxstock > 0 or ( $sTable.oxstock <= 0 and $sTable.oxstockflag != 2 ";
00647             if ( $blRemoveNotOrderables ) {
00648                 $sQ .= " and $sTable.oxstockflag != 3 ";
00649             }
00650             $sQ .= " ) ) ";
00651         }
00652 
00653         return $sQ;
00654     }
00655 
00663     public function getSqlActiveSnippet( $blForceCoreTable = false )
00664     {
00665         $myConfig = $this->getConfig();
00666 
00667         // check if article is still active
00668         $sQ = $this->getActiveCheckQuery( $blForceCoreTable );
00669 
00670         // stock and variants check
00671         $sQ .= $this->getStockCheckQuery( $blForceCoreTable );
00672 
00673 
00674         return "( $sQ ) ";
00675     }
00676 
00684     public function setSkipAssign($blSkipAssign)
00685     {
00686         $this->_blSkipAssign = $blSkipAssign;
00687     }
00688 
00696     public function disablePriceLoad( $oArticle )
00697     {
00698         $oArticle->_blLoadPrice = false;
00699     }
00700 
00706     public function getItemKey()
00707     {
00708         return $this->_sItemKey;
00709     }
00710 
00718     public function setItemKey($sItemKey)
00719     {
00720         $this->_sItemKey = $sItemKey;
00721     }
00722 
00730     public function setNoVariantLoading( $blLoadVariants )
00731     {
00732         $this->_blLoadVariants = !$blLoadVariants;
00733     }
00734 
00740     public function isBuyable()
00741     {
00742         if ($this->_blNotBuyableParent) {
00743             return false;
00744         }
00745 
00746         return !$this->_blNotBuyable;
00747     }
00748 
00754     public function getPersParams()
00755     {
00756         return $this->_aPersistParam;
00757     }
00758 
00764     public function isOnComparisonList()
00765     {
00766         return $this->_blIsOnComparisonList;
00767     }
00768 
00776     public function setOnComparisonList( $blOnList )
00777     {
00778         $this->_blIsOnComparisonList = $blOnList;
00779     }
00780 
00788     public function setLoadParentData($blLoadParentData)
00789     {
00790         $this->_blLoadParentData = $blLoadParentData;
00791     }
00792 
00800     public function setSkipAbPrice( $blSkipAbPrice = null )
00801     {
00802         $this->_blSkipAbPrice = $blSkipAbPrice;
00803     }
00804 
00810     public function getSearchableFields()
00811     {
00812         $aSkipFields = array("oxblfixedprice", "oxicon", "oxvarselect", "oxamitemid", "oxamtaskid", "oxpixiexport", "oxpixiexported") ;
00813         $aFields = array_diff( array_keys($this->_aFieldNames), $aSkipFields );
00814 
00815         return $aFields;
00816     }
00817 
00818 
00826     public function isMultilingualField($sFieldName)
00827     {
00828         switch ($sFieldName) {
00829             case "oxlongdesc":
00830             case "oxtags":
00831                 return true;
00832         }
00833 
00834         return parent::isMultilingualField($sFieldName);
00835     }
00836 
00842     public function isVisible()
00843     {
00844 
00845         // admin preview mode
00846         $myConfig  = $this->getConfig();
00847         if ( ( $sPrevId = oxConfig::getParameter( 'preview' ) ) &&
00848              ( $sAdminSid = oxUtilsServer::getInstance()->getOxCookie( 'admin_sid' ) ) ) {
00849 
00850             $oDb = oxDb::getDb();
00851             $sPrevId   = $oDb->quote( $sPrevId );
00852             $sAdminSid = $oDb->quote( $sAdminSid );
00853             $sTable    = getViewName( 'oxuser' );
00854 
00855             return (bool) $oDb->getOne( "select 1 from $sTable where MD5( CONCAT( {$sAdminSid}, {$sTable}.oxid, {$sTable}.oxpassword, {$sTable}.oxrights ) ) = $sPrevId" );
00856         }
00857 
00858         // active ?
00859         $sNow = date('Y-m-d H:i:s');
00860         if ( !$this->oxarticles__oxactive->value &&
00861              (  $this->oxarticles__oxactivefrom->value > $sNow ||
00862                 $this->oxarticles__oxactiveto->value < $sNow
00863              )) {
00864             return false;
00865         }
00866 
00867         // stock flags
00868         if ( $myConfig->getConfigParam( 'blUseStock' ) && $this->oxarticles__oxstockflag->value == 2) {
00869             $iOnStock = $this->oxarticles__oxstock->value + $this->oxarticles__oxvarstock->value;
00870             if ($this->getConfig()->getConfigParam( 'blPsBasketReservationEnabled' )) {
00871                 $iOnStock += $this->getSession()->getBasketReservations()->getReservedAmount($this->getId());
00872             }
00873             if ( $iOnStock <= 0 ) {
00874                 return false;
00875             }
00876         }
00877 
00878         return true;
00879     }
00880 
00889     public function assign( $aRecord)
00890     {
00891         startProfile('articleAssign');
00892 
00893 
00894         // load object from database
00895         parent::assign( $aRecord);
00896 
00897         $this->oxarticles__oxnid = $this->oxarticles__oxid;
00898 
00899         // check for simple article.
00900         if ($this->_blSkipAssign) {
00901             return;
00902         }
00903 
00904         $this->_assignParentFieldValues();
00905         $this->_assignNotBuyableParent();
00906 
00907         $this->_assignAllPictureValues();
00908 
00909         $this->_assignStock();
00910         startProfile('articleAssignPrices');
00911         $this->_assignPrices();
00912         stopProfile('articleAssignPrices');
00913         $this->_assignPersistentParam();
00914         $this->_assignDynImageDir();
00915         $this->_assignComparisonListFlag();
00916         $this->_assignAttributes();
00917 
00918 
00919         //$this->_seoAssign();
00920 
00921         stopProfile('articleAssign');
00922     }
00923 
00934     public function load( $oxID)
00935     {
00936         // A. #1325 resetting to avoid problems when reloading (details etc)
00937         $this->_blNotBuyableParent = false;
00938 
00939         $blRet = parent::load( $oxID);
00940 
00941         // convert date's to international format
00942         $this->oxarticles__oxinsert    = new oxField(oxUtilsDate::getInstance()->formatDBDate( $this->oxarticles__oxinsert->value));
00943         $this->oxarticles__oxtimestamp = new oxField(oxUtilsDate::getInstance()->formatDBDate( $this->oxarticles__oxtimestamp->value));
00944 
00945         return $blRet;
00946     }
00947 
00955     public function addToRatingAverage( $iRating)
00956     {
00957         $dOldRating = $this->oxarticles__oxrating->value;
00958         $dOldCnt    = $this->oxarticles__oxratingcnt->value;
00959         $this->oxarticles__oxrating->setValue(( $dOldRating * $dOldCnt + $iRating ) / ($dOldCnt + 1));
00960         $this->oxarticles__oxratingcnt->setValue($dOldCnt + 1);
00961         $dRating = ( $dOldRating * $dOldCnt + $iRating ) / ($dOldCnt + 1);
00962         $dRatingCnt = (int) ($dOldCnt + 1);
00963         // oxarticles.oxtimestamp = oxarticles.oxtimestamp to keep old timestamp value
00964         oxDb::getDb()->execute( 'update oxarticles set oxarticles.oxrating = '.$dRating.',oxarticles.oxratingcnt = '.$dRatingCnt.', oxarticles.oxtimestamp = oxarticles.oxtimestamp where oxarticles.oxid = "'.$this->getId().'" ' );
00965     }
00966 
00972     public function getArticleRatingAverage()
00973     {
00974         return round( $this->oxarticles__oxrating->value, 1);
00975     }
00976 
00982     public function getReviews()
00983     {
00984         $myConfig  = $this->getConfig();
00985 
00986         $aIds = array($this->getId());
00987 
00988         if ( $this->oxarticles__oxparentid->value ) {
00989                 $aIds[] = $this->oxarticles__oxparentid->value;
00990         }
00991 
00992         // showing variant reviews ..
00993         if ( $myConfig->getConfigParam( 'blShowVariantReviews' ) ) {
00994             $aAdd = $this->_getVariantsIds();
00995             if (is_array($aAdd)) {
00996                 $aIds = array_merge($aIds, $aAdd);
00997             }
00998         }
00999 
01000         $oReview = oxNew('oxreview');
01001         $oRevs = $oReview->loadList('oxarticle', $aIds);
01002 
01003         //if no review found, return null
01004         if ( $oRevs->count() < 1 ) {
01005             return null;
01006         }
01007 
01008         return $oRevs;
01009     }
01010 
01016     public function getCrossSelling()
01017     {
01018         $oCrosslist = oxNew( "oxarticlelist");
01019         $oCrosslist->loadArticleCrossSell($this->oxarticles__oxid->value);
01020         if ( $oCrosslist->count() ) {
01021             return $oCrosslist;
01022         }
01023     }
01024 
01030     public function getAccessoires()
01031     {
01032         $myConfig = $this->getConfig();
01033 
01034         // Performance
01035         if ( !$myConfig->getConfigParam( 'bl_perfLoadAccessoires' ) ) {
01036             return;
01037         }
01038 
01039         $oAcclist = oxNew( "oxarticlelist");
01040         $oAcclist->setSqlLimit( 0, $myConfig->getConfigParam( 'iNrofCrossellArticles' ));
01041         $oAcclist->loadArticleAccessoires($this->oxarticles__oxid->value);
01042 
01043         if ( $oAcclist->count()) {
01044             return $oAcclist;
01045         }
01046     }
01047 
01053     public function getSimilarProducts()
01054     {
01055         // Performance
01056         $myConfig = $this->getConfig();
01057         if ( !$myConfig->getConfigParam( 'bl_perfLoadSimilar' ) ) {
01058             return;
01059         }
01060 
01061         $sArticleTable = $this->_getObjectViewName('oxarticles');
01062 
01063         $sAttribs = '';
01064         $iCnt = 0;
01065         $this->_getAttribsString($sAttribs, $iCnt);
01066 
01067         if ( !$sAttribs) {
01068             return null;
01069         }
01070 
01071         $aList = $this->_getSimList($sAttribs, $iCnt);
01072 
01073         if ( count( $aList ) ) {
01074             uasort( $aList, 'cmpart');
01075 
01076             $sSearch = $this->_generateSimListSearchStr($sArticleTable, $aList);
01077 
01078             $oSimilarlist = oxNew( 'oxarticlelist' );
01079             $oSimilarlist->setSqlLimit( 0, $myConfig->getConfigParam( 'iNrofSimilarArticles' ));
01080             $oSimilarlist->selectString( $sSearch);
01081 
01082             return $oSimilarlist;
01083         }
01084     }
01085 
01091     public function getCustomerAlsoBoughtThisProducts()
01092     {
01093         // Performance
01094         $myConfig = $this->getConfig();
01095         if ( !$myConfig->getConfigParam( 'bl_perfLoadCustomerWhoBoughtThis' ) ) {
01096             return;
01097         }
01098 
01099         // selecting products that fits
01100         $sQ = $this->_generateSearchStrForCustomerBought();
01101 
01102         $oArticles = oxNew( 'oxarticlelist' );
01103         $oArticles->setSqlLimit( 0, $myConfig->getConfigParam( 'iNrofCustomerWhoArticles' ));
01104         $oArticles->selectString( $sQ );
01105         if ( $oArticles->count() ) {
01106             return $oArticles;
01107         }
01108     }
01109 
01116     public function loadAmountPriceInfo()
01117     {
01118         $myConfig = $this->getConfig();
01119         if ( !$myConfig->getConfigParam( 'bl_perfLoadPrice' ) || !$this->_blLoadPrice || !$this->_blCalcPrice) {
01120             return array();
01121         }
01122 
01123         if ( $this->_oAmountPriceInfo === null ) {
01124             $this->_oAmountPriceInfo = array();
01125             if ( count( ( $oAmPriceList = $this->_getAmountPriceList() ) ) ) {
01126                 $this->_oAmountPriceInfo = $this->_fillAmountPriceList( $oAmPriceList );
01127 
01128             }
01129         }
01130         return $this->_oAmountPriceInfo;
01131     }
01132 
01140     public function getSelectLists($sKeyPrefix = null)
01141     {
01142         //#1468C - more then one article in basket with different selectlist...
01143         //optionall function parameter $sKeyPrefix added, used only in basket.php
01144         $sKey = $this->getId();
01145         if ( isset( $sKeyPrefix ) ) {
01146             $sKey = $sKeyPrefix.'__'.$this->getId();
01147         }
01148 
01149         if ( !isset( self::$_aSelList[$sKey] ) ) {
01150             // all selectlists this article has
01151             $oLists = oxNew( 'oxlist' );
01152             $oLists->init('oxselectlist');
01153             $sSLViewName = getViewName('oxselectlist');
01154             $sSelect  = "select $sSLViewName.* from oxobject2selectlist left join $sSLViewName on $sSLViewName.oxid=oxobject2selectlist.oxselnid ";
01155             $sSelect .= 'where oxobject2selectlist.oxobjectid=\''.$this->getId().'\' ';
01156             //sorting
01157             $sSelect .= ' order by oxobject2selectlist.oxsort';
01158 
01159             $oLists->selectString( $sSelect );
01160 
01161             //#1104S if this is variant ant it has no selectlists, trying with parent
01162             if ( $this->oxarticles__oxparentid->value && $oLists->count() == 0 ) {
01163                 $sParentQuoted = oxDb::getDb()->quote($this->oxarticles__oxparentid->value);
01164                 //#1496C - select fixed ( * => $sSLViewName.*)
01165                 $sSelect  = "select $sSLViewName.* from oxobject2selectlist left join $sSLViewName on $sSLViewName.oxid=oxobject2selectlist.oxselnid ";
01166                 $sSelect .= "where oxobject2selectlist.oxobjectid=$sParentQuoted ";
01167                 //sorting
01168                 $sSelect .= ' order by oxobject2selectlist.oxsort';
01169                 $oLists->selectString( $sSelect);
01170             }
01171 
01172             $dVat = 0;
01173             if ( $this->getPrice() != null ) {
01174                 $dVat = $this->getPrice()->getVat();
01175             }
01176 
01177             $iCnt = 0;
01178             self::$_aSelList[$sKey] = array();
01179             foreach ( $oLists as $oSelectlist ) {
01180                 self::$_aSelList[$sKey][$iCnt] = $oSelectlist->getFieldList( $dVat );
01181                 self::$_aSelList[$sKey][$iCnt]['name'] = $oSelectlist->oxselectlist__oxtitle->value;
01182                 $iCnt++;
01183             }
01184         }
01185         return self::$_aSelList[$sKey];
01186     }
01187 
01195     protected function _hasAnyVariant( $blForceCoreTable = false )
01196     {
01197         $sArticleTable = $this->getTableNameForActiveSnippet( $blForceCoreTable );
01198         return (bool) oxDb::getDb()->getOne( "select 1 from $sArticleTable where oxparentid='".$this->getId()."'" );
01199     }
01200 
01206     public function hasMdVariants()
01207     {
01208         return $this->_blHasMdVariants;
01209     }
01210 
01219     public function getVariants( $blRemoveNotOrderables = true, $blForceCoreTable = false  )
01220     {
01221         if ( $blRemoveNotOrderables && $this->_aVariants ) {
01222             return $this->_aVariants;
01223         } elseif ( !$blRemoveNotOrderables && $this->_aVariantsWithNotOrderables ) {
01224             return $this->_aVariantsWithNotOrderables;
01225         }
01226 
01227         $myConfig = $this->getConfig();
01228         if ( !$this->_blLoadVariants ||
01229             ( !$this->isAdmin() && !$myConfig->getConfigParam( 'blLoadVariants') ) ||
01230             ( !$this->isAdmin() && !$this->oxarticles__oxvarcount->value ) ) {
01231             return array();
01232         }
01233 
01234         //do not load me as a parent later
01235         self::$_aLoadedParents[$this->getId()] = $this;
01236 
01237         //load simple variants for lists
01238         if ( $this->_isInList() ) {
01239             $oVariants = oxNew( 'oxsimplevariantlist' );
01240             $oVariants->setParent( $this );
01241         } else {
01242             //loading variants
01243             $oVariants = oxNew( 'oxarticlelist' );
01244             $oVariants->getBaseObject()->modifyCacheKey( '_variants' );
01245         }
01246 
01247         if ( $this->_blHasVariants = $this->_hasAnyVariant( $blForceCoreTable ) ) {
01248 
01249             startProfile("selectVariants");
01250             $blUseCoreTable = $blForceCoreTable;
01251             $oBaseObject = $oVariants->getBaseObject();
01252             $sArticleTable = $this->getTableNameForActiveSnippet( $blUseCoreTable );
01253 
01254             $sSelect = "select ".$oBaseObject->getSelectFields()." from $sArticleTable where " .
01255                        $this->getActiveCheckQuery( $blUseCoreTable ) .
01256                        $this->getVariantsQuery( $blRemoveNotOrderables, $blUseCoreTable ) .
01257                        " order by $sArticleTable.oxsort";
01258 
01259             $oVariants->selectString( $sSelect );
01260 
01261             //if this is multidimensional variants, make additional processing
01262             if ( $myConfig->getConfigParam( 'blUseMultidimensionVariants' ) ) {
01263                 $oMdVariants = oxNew( "oxVariantHandler" );
01264                 $this->_blHasMdVariants = $oMdVariants->isMdVariant( $oVariants->current() );
01265             }
01266             stopProfile("selectVariants");
01267         }
01268 
01269         //if we have variants then depending on config option the parent may be non buyable
01270         if ( !$myConfig->getConfigParam( 'blVariantParentBuyable' ) && $this->_blHasVariants ) {
01271             $this->_blNotBuyableParent = true;
01272         }
01273 
01274         //if we have variants, but all variants are incative means article may be non buyable (depends on config option)
01275         if ( !$myConfig->getConfigParam( 'blVariantParentBuyable' ) && $oVariants->count() == 0 && $this->_blHasVariants ) {
01276             $this->_blNotBuyable = true;
01277         }
01278 
01279         // cache
01280         if ( $blRemoveNotOrderables ) {
01281             $this->_aVariants = $oVariants;
01282         } else {
01283             $this->_aVariantsWithNotOrderables = $oVariants;
01284         }
01285 
01286         return $oVariants;
01287     }
01288 
01294     public function getSimpleVariants()
01295     {
01296         if ( $this->oxarticles__oxvarcount->value) {
01297             return $this->getVariants();
01298         }
01299     }
01300 
01309     public function getAdminVariants( $sLanguage = null )
01310     {
01311         $myConfig = $this->getConfig();
01312 
01313         $oVariants = oxNew( 'oxarticlelist');
01314 
01315         if ( is_null($sLanguage) ) {
01316             $oVariants->getBaseObject()->setLanguage(oxLang::getInstance()->getBaseLanguage());
01317         } else {
01318             $oVariants->getBaseObject()->setLanguage($sLanguage);
01319         }
01320 
01321         $sSql = 'select * from oxarticles where oxparentid = "'.$this->getId().'" order by oxsort ';
01322 
01323         $oVariants->selectString( $sSql);
01324 
01325         //if we have variants then depending on config option the parent may be non buyable
01326         if (!$myConfig->getConfigParam( 'blVariantParentBuyable' ) && ($oVariants->count() > 0)) {
01327             //$this->blNotBuyable = true;
01328             $this->_blNotBuyableParent = true;
01329         }
01330 
01331         return $oVariants;
01332     }
01333 
01341     public function getCategory()
01342     {
01343         startPRofile( 'getCategory' );
01344 
01345         $oCategory = oxNew( 'oxcategory' );
01346         $oCategory->setLanguage( $this->getLanguage() );
01347 
01348         // variant handling
01349         $sOXID = $this->getId();
01350         if ( isset( $this->oxarticles__oxparentid->value ) && $this->oxarticles__oxparentid->value ) {
01351             $sOXID = $this->oxarticles__oxparentid->value;
01352         }
01353 
01354         $oStr = getStr();
01355         $sWhere   = $oCategory->getSqlActiveSnippet();
01356         $sSelect  = $this->_generateSearchStr( $sOXID );
01357         $sSelect .= ( $oStr->strstr( $sSelect, 'where' )?' and ':' where ') . $sWhere . " order by oxobject2category.oxtime limit 1";
01358 
01359         // category not found ?
01360         if ( !$oCategory->assignRecord( $sSelect ) ) {
01361 
01362             $sSelect  = $this->_generateSearchStr( $sOXID, true );
01363             $sSelect .= ( $oStr->strstr( $sSelect, 'where' )?' and ':' where ') . $sWhere . " limit 1";
01364 
01365             // looking for price category
01366             if ( !$oCategory->assignRecord( $sSelect ) ) {
01367                 $oCategory = null;
01368             }
01369         }
01370 
01371         stopPRofile( 'getCategory' );
01372         return $oCategory;
01373     }
01374 
01383     public function getCategoryIds( $blActCats = false, $blSkipCache = false )
01384     {
01385         $myConfig = $this->getConfig();
01386         if ( isset( self::$_aArticleCats[$this->getId()] ) && !$blSkipCache ) {
01387             return self::$_aArticleCats[$this->getId()];
01388         }
01389 
01390         // variant handling
01391         $sOXID = $this->getId();
01392         if (isset( $this->oxarticles__oxparentid->value) && $this->oxarticles__oxparentid->value) {
01393             $sOXID = $this->oxarticles__oxparentid->value;
01394         }
01395 
01396         // we do not use lists here as we dont need this overhead right now
01397         $sSql = $this->_getSelectCatIds( $sOXID, $blActCats );
01398         $oDB = oxDb::getDb(true);
01399         $rs = $oDB->execute( $sSql );
01400 
01401 
01402         $aRet = array();
01403 
01404         if ($rs != false && $rs->recordCount() > 0) {
01405             while (!$rs->EOF) {
01406                 $aRet[] = $rs->fields['oxcatnid'];
01407                 $rs->moveNext();
01408             }
01409         }
01410 
01411         // adding price categories if such exists
01412         $sSql = $this->getSqlForPriceCategories();
01413 
01414         $oDB = oxDb::getDb( true );
01415         $rs = $oDB->execute( $sSql );
01416 
01417         if ($rs != false && $rs->recordCount() > 0) {
01418             while (!$rs->EOF) {
01419 
01420                 if ( is_array( $rs->fields ) ) {
01421                    $rs->fields = array_change_key_case( $rs->fields, CASE_LOWER );
01422                 }
01423 
01424 
01425                 if ( !$aRet[$rs->fields['oxid']] ) {
01426                     $aRet[] = $rs->fields['oxid'];
01427                 }
01428                 $rs->moveNext();
01429             }
01430         }
01431 
01432         return self::$_aArticleCats[$this->getId()] = $aRet;
01433     }
01434 
01443     protected function _getSelectCatIds( $sOXID, $blActCats = false )
01444     {
01445         $sO2CView = $this->_getObjectViewName('oxobject2category');
01446         $sCatView = $this->_getObjectViewName('oxcategories');
01447         $sSelect =  "select oxobject2category.oxcatnid as oxcatnid from $sO2CView as oxobject2category left join $sCatView as oxcategories on oxcategories.oxid = oxobject2category.oxcatnid ";
01448         $sSelect .= 'where oxobject2category.oxobjectid='.oxDb::getDb()->quote($sOXID).' and oxcategories.oxid is not null and oxcategories.oxactive'.(($this->getLanguage())?'_'.$this->getLanguage():'').' = 1 ';
01449         if ( $blActCats ) {
01450             $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 ";
01451         }
01452         $sSelect .= 'order by oxobject2category.oxtime ';
01453         return $sSelect;
01454     }
01455 
01465     public function getVendor( $blShopCheck = true )
01466     {
01467         if ( ( $sVendorId = $this->getVendorId() ) ) {
01468             $oVendor = oxNew( 'oxvendor' );
01469         } elseif ( !$blShopCheck && $this->oxarticles__oxvendorid->value ) {
01470             $oVendor = oxNew( 'oxi18n' );
01471             $oVendor->init('oxvendor');
01472             $oVendor->setReadOnly( true );
01473             $sVendorId = $this->oxarticles__oxvendorid->value;
01474         }
01475         if ( $sVendorId && $oVendor->load( $sVendorId ) && $oVendor->oxvendor__oxactive->value ) {
01476             if ( !$this->getConfig()->getConfigParam( 'bl_perfLoadVendorTree' ) ) {
01477                 $oVendor->setReadOnly( true );
01478             }
01479             return $oVendor;
01480         }
01481         return null;
01482     }
01483 
01491     public function getVendorId( $blForceReload = false )
01492     {
01493         $sVendorId = false;
01494         if ( $this->oxarticles__oxvendorid->value ) {
01495             if ( !$blForceReload && isset( self::$_aArticleVendors[$this->getId()] ) ) {
01496                 return self::$_aArticleVendors[$this->getId()];
01497             }
01498             $oDb = oxDb::getDb();
01499             $sVendorIdQuoted = $oDb->quote($this->oxarticles__oxvendorid->value);
01500             $sQ = "select oxid from ".getViewName('oxvendor')." where oxid=$sVendorIdQuoted";
01501             self::$_aArticleVendors[$this->getId()] = $sVendorId = $oDb->getOne( $sQ );
01502         }
01503         return $sVendorId;
01504     }
01505 
01513     public function getManufacturerId( $blForceReload = false )
01514     {
01515         $sManufacturerId = false;
01516         if ( $this->oxarticles__oxmanufacturerid->value ) {
01517             if ( !$blForceReload && isset( self::$_aArticleManufacturers[$this->getId()])) {
01518                 return self::$_aArticleManufacturers[$this->getId()];
01519             }
01520             $oDb = oxDb::getDb();
01521             $sQ = "select oxid from ".getViewName('oxmanufacturers')." where oxid=".$oDb->quote($this->oxarticles__oxmanufacturerid->value);
01522             self::$_aArticleManufacturers[$this->getId()] = $sManufacturerId = $oDb->getOne( $sQ );
01523         }
01524         return $sManufacturerId;
01525     }
01526 
01536     public function getManufacturer( $blShopCheck = true )
01537     {
01538         $oManufacturer = oxNew( 'oxmanufacturer' );;
01539         if ( !( $sManufacturerId = $this->getManufacturerId() ) &&
01540              !$blShopCheck && $this->oxarticles__oxmanufacturerid->value ) {
01541             $oManufacturer->setReadOnly( true );
01542             $sManufacturerId = $this->oxarticles__oxmanufacturerid->value;
01543         }
01544 
01545         if ( $sManufacturerId && $oManufacturer->load( $sManufacturerId ) ) {
01546             if ( !$this->getConfig()->getConfigParam( 'bl_perfLoadManufacturerTree' ) ) {
01547                 $oManufacturer->setReadOnly( true );
01548             }
01549             $oManufacturer = $oManufacturer->oxmanufacturers__oxactive->value ? $oManufacturer : null;
01550         } else {
01551             $oManufacturer = null;
01552         }
01553 
01554         return $oManufacturer;
01555     }
01556 
01564     public function inCategory( $sCatNid)
01565     {
01566         return in_array( $sCatNid, $this->getCategoryIds());
01567     }
01568 
01577     public function isAssignedToCategory( $sCatId )
01578     {
01579         // variant handling
01580         $sOXID = $this->getId();
01581         if ( isset( $this->oxarticles__oxparentid->value) && $this->oxarticles__oxparentid->value) {
01582             $sOXID = $this->oxarticles__oxparentid->value;
01583         }
01584 
01585         $oDB = oxDb::getDb();
01586         $sSelect = $this->_generateSelectCatStr( $sOXID, $sCatId);
01587         $sOXID = $oDB->getOne( $sSelect);
01588         // article is assigned to passed category!
01589         if ( isset( $sOXID) && $sOXID) {
01590             return true;
01591         }
01592 
01593         // maybe this category is price category ?
01594         if ( $this->getConfig()->getConfigParam( 'bl_perfLoadPrice' ) && $this->_blLoadPrice ) {
01595             $dPriceFromTo = $this->getPrice()->getBruttoPrice();
01596             if ( $dPriceFromTo > 0) {
01597                 $sSelect = $this->_generateSelectCatStr( $sOXID, $sCatId, $dPriceFromTo);
01598                 $sOXID = $oDB->getOne( $sSelect);
01599                 // article is assigned to passed category!
01600                 if ( isset( $sOXID) && $sOXID) {
01601                     return true;
01602                 }
01603             }
01604         }
01605         return false;
01606     }
01607 
01613     public function getTPrice()
01614     {
01615         if ( !$this->getConfig()->getConfigParam( 'bl_perfLoadPrice' ) || !$this->_blLoadPrice ) {
01616             return;
01617         }
01618         // return cached result, since oPrice is created ONLY in this function [or function of EQUAL level]
01619         if ( $this->_oTPrice !== null ) {
01620             return $this->_oTPrice;
01621         }
01622 
01623         $this->_oTPrice = oxNew( 'oxPrice' );
01624         $this->_oTPrice->setPrice( $this->oxarticles__oxtprice->value );
01625 
01626         $this->_applyVat( $this->_oTPrice, $this->getArticleVat() );
01627         $this->_applyCurrency( $this->_oTPrice );
01628 
01629         return $this->_oTPrice;
01630     }
01631 
01637     public function skipDiscounts()
01638     {
01639         // allready loaded skip discounts config
01640         if ( $this->_blSkipDiscounts !== null )
01641             return $this->_blSkipDiscounts;
01642 
01643         if ( $this->oxarticles__oxskipdiscounts->value )
01644             return true;
01645 
01646         $sO2CView = getViewName('oxobject2category');
01647         $sSelect =  "select 1 from $sO2CView as oxobject2category left join oxcategories on oxcategories.oxid = oxobject2category.oxcatnid ";
01648         $sSelect .= 'where oxobject2category.oxobjectid="'.$this->getId().'" and oxcategories.oxactive'.(($this->getLanguage())?'_'.$this->getLanguage():'').' = 1 and oxcategories.oxskipdiscounts = "1" ';
01649 
01650         return $this->_blSkipDiscounts = ( oxDb::getDb()->getOne($sSelect) == 1 );
01651     }
01652 
01660     public function setPrice(oxPrice $oPrice)
01661     {
01662         $this->_oPrice = $oPrice;
01663     }
01664 
01673     public function getBasePrice( $dAmount = 1 )
01674     {
01675         // override this function if you want e.g. different prices
01676         // for diff. usergroups.
01677 
01678         // Performance
01679         $myConfig = $this->getConfig();
01680         if( !$myConfig->getConfigParam( 'bl_perfLoadPrice' ) || !$this->_blLoadPrice )
01681             return;
01682 
01683         // GroupPrice or DB price ajusted by AmountPrice
01684         $dPrice = $this->_getAmountPrice( $dAmount );
01685 
01686 
01687         return $dPrice;
01688     }
01689 
01697     public function getPrice( $dAmount = 1 )
01698     {
01699         $myConfig = $this->getConfig();
01700         // Performance
01701         if ( !$myConfig->getConfigParam( 'bl_perfLoadPrice' ) || !$this->_blLoadPrice ) {
01702             return;
01703         }
01704 
01705         // return cached result, since oPrice is created ONLY in this function [or function of EQUAL level]
01706         if ( $dAmount != 1 || $this->_oPrice === null ) {
01707             $oPrice = oxNew( 'oxPrice' );
01708 
01709             // get base
01710             $oPrice->setPrice( $this->getBasePrice( $dAmount ) );
01711 
01712             // price handling
01713             if ( !$this->_blCalcPrice && $dAmount == 1 ) {
01714                 return $this->_oPrice = $oPrice;
01715             }
01716 
01717             $this->_calculatePrice( $oPrice );
01718             if ( $dAmount != 1 ) {
01719                 return $oPrice;
01720             }
01721 
01722             $this->_oPrice = $oPrice;
01723         }
01724         return $this->_oPrice;
01725     }
01726 
01734     protected function _calculatePrice( $oPrice )
01735     {
01736         // apply VAT only if configuration requires it
01737         if ( !$this->getConfig()->getConfigParam( 'bl_perfCalcVatOnlyForBasketOrder' ) ) {
01738             $this->_applyVAT( $oPrice, $this->getArticleVat() );
01739         }
01740 
01741         // apply currency
01742         $this->_applyCurrency( $oPrice );
01743         // apply discounts
01744         if ( !$this->skipDiscounts() ) {
01745             $oDiscountList = oxDiscountList::getInstance();
01746             $oDiscountList->applyDiscounts( $oPrice, $oDiscountList->getArticleDiscounts($this, $this->getArticleUser() ) );
01747         }
01748 
01749         return $oPrice;
01750     }
01751 
01759     public function setArticleUser($oUser)
01760     {
01761         $this->_oUser = $oUser;
01762     }
01763 
01769     public function getArticleUser()
01770     {
01771         if ($this->_oUser) {
01772             return $this->_oUser;
01773         }
01774         return $this->getUser();
01775     }
01776 
01786     public function getBasketPrice( $dAmount, $aSelList, $oBasket )
01787     {
01788         $oUser = $oBasket->getBasketUser();
01789         $this->setArticleUser($oUser);
01790 
01791         $oBasketPrice = oxNew( 'oxPrice' );
01792 
01793         // get base price
01794         $dBasePrice = $this->getBasePrice( $dAmount );
01795 
01796         // applying select list price
01797         $dBasePrice = $this->_modifySelectListPrice( $dBasePrice, $aSelList );
01798 
01799         // setting price
01800         $oBasketPrice->setPrice( $dBasePrice );
01801 
01802         // apply VAT
01803         $this->_applyVat( $oBasketPrice, oxVatSelector::getInstance()->getBasketItemVat( $this, $oBasket ) );
01804 
01805         // apply currency
01806         $this->_applyCurrency( $oBasketPrice );
01807 
01808         // apply discounts
01809         if ( !$this->skipDiscounts() ) {
01810             // apply general discounts
01811             $oDiscountList = oxDiscountList::getInstance();
01812             $oDiscountList->applyDiscounts( $oBasketPrice, $oDiscountList->getArticleDiscounts( $this, $oUser ) );
01813         }
01814 
01815         // returning final price object
01816         return $oBasketPrice;
01817     }
01818 
01831     public function applyBasketDiscounts(oxPrice $oPrice, $aDiscounts, $dAmount = 1)
01832     {
01833         $oDiscountList = oxDiscountList::getInstance();
01834         return $oDiscountList->applyBasketDiscounts( $oPrice, $aDiscounts, $dAmount );
01835     }
01836 
01845     public function delete( $sOXID = null )
01846     {
01847         if ( !$sOXID ) {
01848             $sOXID = $this->getId();
01849         }
01850         if ( !$sOXID ) {
01851             return false;
01852         }
01853 
01854 
01855         $this->load( $sOXID );
01856         $this->_deletePics();
01857         $this->_onChangeResetCounts( $sOXID, $this->oxarticles__oxvendorid->value, $this->oxarticles__oxmanufacturerid->value );
01858         $this->_deleteVariantRecords( $sOXID );
01859         $rs = $this->_deleteRecords( $sOXID );
01860 
01861         oxSeoEncoderArticle::getInstance()->onDeleteArticle($this);
01862 
01863         $this->onChange( ACTION_DELETE, $sOXID, $this->oxarticles__oxparentid->value );
01864 
01865         return $rs->EOF;
01866     }
01867 
01876     public function reduceStock($dAmount, $blAllowNegativeStock = false)
01877     {
01878         $this->beforeUpdate();
01879 
01880         $iStockCount = $this->oxarticles__oxstock->value - $dAmount;
01881         if (!$blAllowNegativeStock && ($iStockCount < 0)) {
01882             $dAmount += $iStockCount;
01883             $iStockCount = 0;
01884         }
01885         $this->oxarticles__oxstock = new oxField($iStockCount);
01886 
01887         $oDb = oxDb::getDb();
01888         $oDb->execute( 'update oxarticles set oxarticles.oxstock = '.$oDb->quote( $iStockCount ).' where oxarticles.oxid = '.$oDb->quote( $this->getId() ) );
01889         $this->onChange( ACTION_UPDATE_STOCK );
01890         return $dAmount;
01891     }
01892 
01901     public function updateSoldAmount( $dAmount = 0 )
01902     {
01903         if ( !$dAmount ) {
01904             return;
01905         }
01906 
01907         $this->beforeUpdate();
01908 
01909         // article is not variant - should be updated current amount
01910         if ( !$this->oxarticles__oxparentid->value ) {
01911             //updating by SQL query, due to wrong behaviour if saving article using not admin mode
01912             $dAmount = (double) $dAmount;
01913             $oDb = oxDb::getDb();
01914             $rs = $oDb->execute( "update oxarticles set oxarticles.oxsoldamount = oxarticles.oxsoldamount + $dAmount where oxarticles.oxid = ".$oDb->quote($this->oxarticles__oxid->value));
01915         } elseif ( $this->oxarticles__oxparentid->value) {
01916             // article is variant - should be updated this article parent amount
01917             $oUpdateArticle = oxNewArticle( $this->oxarticles__oxparentid->value );
01918             $oUpdateArticle->updateSoldAmount( $dAmount );
01919         }
01920 
01921         $this->onChange( ACTION_UPDATE );
01922 
01923         return $rs;
01924     }
01925 
01931     public function disableReminder()
01932     {
01933         $oDB = oxDb::getDb(true);
01934         return $oDB->execute( "update oxarticles set oxarticles.oxremindactive = 2 where oxarticles.oxid = ".$oDB->quote($this->oxarticles__oxid->value));
01935     }
01936 
01943     public function save()
01944     {
01945         $myConfig = $this->getConfig();
01946 
01947         $this->oxarticles__oxthumb = new oxField(basename($this->oxarticles__oxthumb->value), oxField::T_RAW);
01948         $this->oxarticles__oxicon  = new oxField(basename($this->oxarticles__oxicon->value), oxField::T_RAW);
01949         $iPicCount = $myConfig->getConfigParam( 'iPicCount');
01950         for ($i=1; $i <= $iPicCount; $i++) {
01951             if ( isset($this->{'oxarticles__oxpic'.$i}) ) {
01952                 $this->{'oxarticles__oxpic'.$i}->setValue(basename($this->{'oxarticles__oxpic'.$i}->value));
01953             }
01954         }
01955 
01956         $blRet = parent::save();
01957 
01958         // save article long description
01959         $this->setArticleLongDesc();
01960 
01961         // load article images after save
01962         $this->_assignAllPictureValues();
01963 
01964         return $blRet;
01965     }
01966 
01967 
01974     public function getPictureGallery()
01975     {
01976         $myConfig = $this->getConfig();
01977 
01978         //initialize
01979         $blMorePic = false;
01980         $aArtPics  = array();
01981         $aArtIcons = array();
01982         $iActPicId = 1;
01983         $sActPic = $this->getPictureUrl( $iActPicId );
01984 
01985         if ( oxConfig::getParameter( 'actpicid' ) ) {
01986             $iActPicId = oxConfig::getParameter('actpicid');
01987         }
01988 
01989         $oStr = getStr();
01990         $iCntr = 0;
01991         $iPicCount = $myConfig->getConfigParam( 'iPicCount' );
01992         $blCheckActivePicId = true;
01993 
01994         for ( $i = 1; $i <= $iPicCount; $i++) {
01995             $sPicVal = $this->getPictureUrl( $i );
01996             $sIcoVal = $this->getIconUrl( $i );
01997             if ( !$oStr->strstr($sIcoVal, 'nopic_ico.jpg') && !$oStr->strstr($sIcoVal, 'nopic.jpg') ) {
01998                 if ($iCntr) {
01999                     $blMorePic = true;
02000                 }
02001                 $aArtIcons[$i]= $sIcoVal;
02002                 $aArtPics[$i]= $sPicVal;
02003                 $iCntr++;
02004 
02005                 if ($iActPicId == $i) {
02006                     $sActPic = $sPicVal;
02007                     $blCheckActivePicId = false;
02008                 }
02009 
02010             } else if ( $blCheckActivePicId && $iActPicId <= $i) {
02011                 // if picture is empty, setting active pic id to next
02012                 // picture
02013                 $iActPicId++;
02014             }
02015         }
02016 
02017         $blZoomPic  = false;
02018         $aZoomPics = array();
02019         $iZoomPicCount = $myConfig->getConfigParam( 'iPicCount' );
02020 
02021         for ( $j = 1,$c = 1; $j <= $iZoomPicCount; $j++) {
02022             $sVal = $this->getZoomPictureUrl($j);
02023 
02024             if ( !$oStr->strstr($sVal, 'nopic.jpg')) {
02025                 if ($this->getConfig()->getConfigParam('blFormerTplSupport')) {
02026                     $sVal = $this->_sDynImageDir."/".$sVal;
02027                 }
02028                 $blZoomPic = true;
02029                 $aZoomPics[$c]['id'] = $c;
02030                 $aZoomPics[$c]['file'] = $sVal;
02031                 //anything is better than empty name, because <img src=""> calls shop once more = x2 SLOW.
02032                 if (!$sVal) {
02033                     $aZoomPics[$c]['file'] = "nopic.jpg";
02034                 }
02035                 $c++;
02036             }
02037         }
02038 
02039         $aPicGallery = array('ActPicID' => $iActPicId,
02040                              'ActPic' => $sActPic,
02041                              'MorePics' => $blMorePic,
02042                              'Pics' => $aArtPics,
02043                              'Icons' => $aArtIcons,
02044                              'ZoomPic' => $blZoomPic,
02045                              'ZoomPics' => $aZoomPics);
02046 
02047         return $aPicGallery;
02048     }
02049 
02063     public function onChange($sAction = null, $sOXID = null, $sParentID = null)
02064     {
02065         $myConfig = $this->getConfig();
02066 
02067         if (!isset($sOXID)) {
02068             if ( $this->getId()) {
02069                 $sOXID = $this->getId();
02070             }
02071             if (!isset ($sOXID)) {
02072                 $sOXID = $this->oxarticles__oxid->value;
02073             }
02074             if ($this->oxarticles__oxparentid->value) {
02075                 $sParentID = $this->oxarticles__oxparentid->value;
02076             }
02077         }
02078         if (!isset($sOXID)) {
02079             return;
02080         }
02081 
02082         //if (isset($sOXID) && !$myConfig->blVariantParentBuyable && $myConfig->blUseStock)
02083         if ( $myConfig->getConfigParam( 'blUseStock' ) ) {
02084             //if article has variants then updating oxvarstock field
02085             //getting parent id
02086             if (!isset($sParentID)) {
02087                 $oDb = oxDb::getDb();
02088                 $sQ = 'select oxparentid from oxarticles where oxid = '.$oDb->quote($sOXID);
02089                 $sParentID = $oDb->getOne($sQ);
02090             }
02091             //if we have parent id then update stock
02092             if ($sParentID) {
02093                 $this->_onChangeUpdateStock($sParentID);
02094             }
02095         }
02096         //if we have parent id then update count
02097         //update count even if blUseStock is not active
02098         if ($sParentID) {
02099             $this->_onChangeUpdateVarCount($sParentID);
02100         }
02101 
02102         $sId = ( $sParentID ) ? $sParentID : $sOXID;
02103         $this->_onChangeUpdateMinVarPrice( $sId );
02104 
02105             // reseting articles count cache if stock has changed and some
02106             // articles goes offline (M:1448)
02107             if ( $sAction === ACTION_UPDATE_STOCK ) {
02108                 $this->_onChangeStockResetCount( $sOXID );
02109             }
02110 
02111     }
02112 
02119     public function getCustomVAT()
02120     {
02121         if ( isset($this->oxarticles__oxvat->value) ) {
02122             return $this->oxarticles__oxvat->value;
02123         }
02124     }
02125 
02134     public function checkForStock( $dAmount, $dArtStockAmount = 0 )
02135     {
02136         $myConfig = $this->getConfig();
02137         if ( !$myConfig->getConfigParam( 'blUseStock' ) ) {
02138             return true;
02139         }
02140 
02141         // fetching DB info as its up-to-date
02142         $sQ = 'select oxstock, oxstockflag from oxarticles where oxid = "'.$this->getId().'" ';
02143         $rs = oxDb::getDb(true)->Execute( $sQ );
02144 
02145         $iOnStock   = 0;
02146         $iStockFlag = 0;
02147         if ( $rs !== false && $rs->recordCount() > 0 ) {
02148             $iOnStock   = $rs->fields['oxstock'] - $dArtStockAmount;
02149             $iStockFlag = $rs->fields['oxstockflag'];
02150 
02151             // dodger : fremdlager is also always considered as on stock
02152             if ( $iStockFlag == 1 || $iStockFlag == 4) {
02153                 return true;
02154             }
02155             if ( !$myConfig->getConfigParam( 'blAllowUnevenAmounts' ) ) {
02156                 $iOnStock = floor( $iOnStock );
02157             }
02158         }
02159         if ($this->getConfig()->getConfigParam( 'blPsBasketReservationEnabled' )) {
02160             $iOnStock += $this->getSession()->getBasketReservations()->getReservedAmount($this->getId());
02161         }
02162         if ( $iOnStock >= $dAmount ) {
02163             return true;
02164         } else {
02165             if ( $iOnStock > 0 ) {
02166                 return $iOnStock;
02167             } else {
02168                 $oEx = oxNew( 'oxArticleInputException' );
02169                 $oEx->setMessage( 'EXCEPTION_ARTICLE_ARTICELNOTBUYABLE' );
02170                 oxUtilsView::getInstance()->addErrorToDisplay( $oEx );
02171                 return false;
02172             }
02173         }
02174     }
02175 
02176 
02184     public function getArticleLongDesc($sOXID = null)
02185     {
02186 
02187         if ( !$sOXID ) {
02188             $sOXID = $this->oxarticles__oxid->value;
02189         }
02190 
02191         if ($sOXID == $this->oxarticles__oxid->value) {
02192             if (isset($this->oxarticles__oxlongdesc) && ($this->oxarticles__oxlongdesc instanceof oxField) && $this->oxarticles__oxlongdesc->value) {
02193                 return $this->oxarticles__oxlongdesc;
02194             }
02195         }
02196 
02197         $myConfig = $this->getConfig();
02198 
02199         if ( $sOXID ) {
02200             $sLangField = oxLang::getInstance()->getLanguageTag($this->getLanguage());
02201             $oDb = oxDb::getDb();
02202             $this->_setLongDesc($oDb->getOne( "select oxlongdesc{$sLangField} from oxartextends where oxid = ".$oDb->quote($sOXID) ));
02203         } else {
02204             // TODO: check if keeping fldname is needed in non-admin mode
02205             $this->oxarticles__oxlongdesc = new oxField();
02206             $this->oxarticles__oxlongdesc->fldname = 'oxlongdesc';
02207             $this->oxarticles__oxlongdesc->table   = 'oxarticles';
02208             $this->oxarticles__oxlongdesc->fldtype = 'text';
02209         }
02210 
02211         return $this->oxarticles__oxlongdesc;
02212     }
02213 
02221     protected function _setLongDesc($sDbValue)
02222     {
02223         // TODO: the code below is redundant, optimize it, assignments should go smooth without conversions
02224         // hack, if editor screws up text, htmledit tends to do so
02225         $sDbValue = str_replace( '&amp;nbsp;', '&nbsp;', $sDbValue );
02226         $sDbValue = str_replace( '&amp;', '&', $sDbValue );
02227         $sDbValue = str_replace( '&quot;', '"', $sDbValue );
02228         $sDbValue = str_replace( '&lang=', '&amp;lang=', $sDbValue);
02229         //
02230 
02231         $oStr = getStr();
02232         $blHasSmarty = $oStr->strstr( $sDbValue, '[{' );
02233         $blHasPhp = $oStr->strstr( $sDbValue, '<?' );
02234         $myConfig = oxConfig::getInstance();
02235         if ( ( $blHasSmarty || $blHasPhp ) && ($myConfig->getConfigParam( 'blExport' ) || !$this->isAdmin() ) && $myConfig->getConfigParam( 'bl_perfParseLongDescinSmarty' ) ) {
02236             $sDbValue = oxUtilsView::getInstance()->parseThroughSmarty( $sDbValue, $this->getId() );
02237         }
02238         $this->oxarticles__oxlongdesc = new oxField($sDbValue, oxField::T_RAW);
02239         // TODO: check if keeping fldname is needed in non-admin mode
02240         $this->oxarticles__oxlongdesc->fldname = 'oxlongdesc';
02241         $this->oxarticles__oxlongdesc->table   = 'oxarticles';
02242         $this->oxarticles__oxlongdesc->fldtype = 'text';
02243     }
02244 
02250     public function setArticleLongDesc()
02251     {
02252 
02253         if ( $this->_blEmployMultilanguage ) {
02254             // update or insert article long description
02255             if ($this->oxarticles__oxlongdesc instanceof oxField) {
02256                 $sLongDesc = $this->oxarticles__oxlongdesc->getRawValue();
02257             } else {
02258                 $sLongDesc = $this->oxarticles__oxlongdesc->value;
02259             }
02260             $this->_saveArtLongDesc($this->getLanguage(), $sLongDesc);
02261         } else {
02262             $oArtExt = oxNew('oxi18n');
02263             $oArtExt->init('oxartextends');
02264             $aObjFields = $oArtExt->_getAllFields(true);
02265             foreach ($aObjFields as $sKey => $sValue ) {
02266                 if ( preg_match('/^oxlongdesc(_(\d{1,2}))?$/', $sKey) ) {
02267                     $sField = $this->_getFieldLongName($sKey);
02268                     if (isset($this->$sField)) {
02269                         $iLang = $oArtExt->_getFieldLang($sKey);
02270                         $sLongDesc = null;
02271                         if ($this->$sField instanceof oxField) {
02272                             $sLongDesc = $this->$sField->getRawValue();
02273                         } elseif (is_object($this->$sField)) {
02274                             $sLongDesc = $this->$sField->value;
02275                         }
02276                         if (isset($sLongDesc)) {
02277                             $this->_saveArtLongDesc($iLang, $sLongDesc);
02278                         }
02279                     }
02280                 }
02281             }
02282         }
02283     }
02284 
02290     public function getAttributes()
02291     {
02292         if ( $this->_oAttributeList === null ) {
02293             $this->_oAttributeList = oxNew( 'oxattributelist' );
02294             $this->_oAttributeList->loadAttributes( $this->getId() );
02295         }
02296 
02297         return $this->_oAttributeList;
02298     }
02299 
02308     public function appendLink( $sAddParams, $iLang = null )
02309     {
02310         if ( $sAddParams ) {
02311             if ( $iLang === null ) {
02312                 $iLang = $this->getLanguage();
02313             }
02314 
02315             $this->_aSeoAddParams[$iLang]  = isset( $this->_aSeoAddParams[$iLang] ) ? $this->_aSeoAddParams[$iLang] . "&amp;" : "";
02316             $this->_aSeoAddParams[$iLang] .= $sAddParams;
02317         }
02318     }
02319 
02328     public function getBaseSeoLink( $iLang, $blMain = false )
02329     {
02330         $oEncoder = oxSeoEncoderArticle::getInstance();
02331         if ( !$blMain ) {
02332             return $oEncoder->getArticleUrl( $this, $iLang, $this->getLinkType() );
02333         }
02334         return $oEncoder->getArticleMainUrl( $this, $iLang );
02335     }
02336 
02345     public function getLink( $iLang = null, $blMain = false  )
02346     {
02347         if ( !oxUtils::getInstance()->seoIsActive() ) {
02348             return $this->getStdLink( $iLang );
02349         }
02350 
02351         if ( $iLang === null ) {
02352             $iLang = $this->getLanguage();
02353         }
02354 
02355         $iLinkType = $this->getLinkType();
02356         if ( !isset( $this->_aSeoUrls[$iLang][$iLinkType] ) ) {
02357             $this->_aSeoUrls[$iLang][$iLinkType] = $this->getBaseSeoLink( $iLang, $blMain );
02358         }
02359 
02360         $sUrl = $this->_aSeoUrls[$iLang][$iLinkType];
02361         if ( isset($this->_aSeoAddParams[$iLang])) {
02362             $sUrl .= ( ( strpos( $sUrl.$this->_aSeoAddParams[$iLang], '?' ) === false ) ? '?' : '&amp;' ).$this->_aSeoAddParams[$iLang];
02363         }
02364 
02365         return $sUrl;
02366     }
02367 
02376     public function getMainLink( $iLang = null )
02377     {
02378         return $this->getLink( $iLang, true );
02379     }
02380 
02388     public function setLinkType( $iType )
02389     {
02390         // resetting detaisl link, to force new
02391         $this->_sDetailLink = null;
02392 
02393         // setting link type
02394         $this->_iLinkType = (int) $iType;
02395     }
02396 
02402     public function getLinkType()
02403     {
02404         return $this->_iLinkType;
02405     }
02406 
02415     public function appendStdLink( $sAddParams, $iLang = null )
02416     {
02417         if ( $sAddParams ) {
02418             if ( $iLang === null ) {
02419                 $iLang = $this->getLanguage();
02420             }
02421 
02422             $this->_aStdAddParams[$iLang]  = isset( $this->_aStdAddParams[$iLang] ) ? $this->_aStdAddParams[$iLang] . "&amp;" : "";
02423             $this->_aStdAddParams[$iLang] .= $sAddParams;
02424         }
02425     }
02426 
02436     public function getBaseStdLink( $iLang, $blAddId = true, $blFull = true )
02437     {
02438         $sUrl = '';
02439         if ( $blFull ) {
02440             //always returns shop url, not admin
02441             $sUrl = $this->getConfig()->getShopUrl( $iLang, false );
02442         }
02443 
02444         $sUrl .= "index.php?cl=details" . ( $blAddId ? "&amp;anid=".$this->getId() : "" );
02445         return $sUrl . ( isset( $this->_aStdAddParams[$iLang] ) ? "&amp;". $this->_aStdAddParams[$iLang] : "" );
02446     }
02447 
02456     public function getStdLink( $iLang = null, $aParams = array() )
02457     {
02458         if ( $iLang === null ) {
02459             $iLang = $this->getLanguage();
02460         }
02461 
02462         if ( !isset( $this->_aStdUrls[$iLang] ) ) {
02463             $this->_aStdUrls[$iLang] = $this->getBaseStdLink( $iLang );
02464         }
02465 
02466         return oxUtilsUrl::getInstance()->processStdUrl( $this->_aStdUrls[$iLang], $aParams, $iLang, $iLang != $this->getLanguage() );
02467     }
02468 
02476     public function getStdTagLink( $sTag )
02477     {
02478         $sStdTagLink = $this->getConfig()->getShopHomeURL( $this->getLanguage(), false );
02479         return $sStdTagLink . "cl=details&amp;anid=".$this->getId()."&amp;listtype=tag&amp;searchtag=".rawurlencode( $sTag );
02480     }
02481 
02487     public function getTags()
02488     {
02489         $sTagField = "oxtags".oxLang::getInstance()->getLanguageTag($this->getLanguage());
02490         $sQ = "select $sTagField from oxartextends where oxid = '".$this->getId()."'";
02491         $sTags = oxDb::getDb(true)->getOne($sQ);
02492         $oTagCloud = oxNew('oxtagcloud');
02493         $sTags = $oTagCloud->trimTags($sTags);
02494         return $sTags;
02495     }
02496 
02504     public function saveTags($sTags)
02505     {
02506         $sTags = mysql_real_escape_string($sTags);
02507         $oTagCloud = oxNew('oxtagcloud');
02508         $oTagCloud->resetTagCache();
02509         $sTags = $oTagCloud->prepareTags($sTags);
02510         $sTagField = "oxtags".oxLang::getInstance()->getLanguageTag($this->getLanguage());
02511         $sQ = "update oxartextends set $sTagField = '$sTags'  where oxid = '".$this->getId()."'";
02512         return oxDb::getDb()->execute($sQ);
02513 
02514     }
02515 
02523     public function addTag($sTag)
02524     {
02525         $oDb = oxDb::getDb();
02526         $sTag = mysql_real_escape_string($sTag);
02527 
02528         $oTagCloud = oxNew('oxtagcloud');
02529         $oTagCloud->resetTagCache();
02530         $sTag = $oTagCloud->prepareTags($sTag);
02531         $sTagSeparator = $this->getConfig()->getConfigParam('sTagSeparator');
02532 
02533         $sField = "oxartextends.OXTAGS" . oxLang::getInstance()->getLanguageTag();
02534 
02535         if ( $oDb->getOne( "select $sField from oxartextends where oxartextends.OXID = '{$this->getId()}'" ) ) {
02536             $sTailTag = $sTagSeparator . $sTag;
02537         } else {
02538             $sTailTag = $sTag;
02539         }
02540 
02541         $sQ = "insert into oxartextends (oxartextends.OXID, $sField) values ('".$this->getId()."', '{$sTag}')
02542                        ON DUPLICATE KEY update $sField = CONCAT(TRIM($sField), '$sTailTag') ";
02543 
02544         return $oDb->Execute($sQ);
02545     }
02546 
02552     public function getMediaUrls()
02553     {
02554         if ( $this->_aMediaUrls === null ) {
02555             $this->_aMediaUrls = oxNew("oxlist");
02556             $this->_aMediaUrls->init("oxmediaurl");
02557             $this->_aMediaUrls->getBaseObject()->setLanguage( $this->getLanguage() );
02558 
02559             $sQ = "select * from oxmediaurls where oxobjectid = '".$this->getId()."'";
02560             $this->_aMediaUrls->selectString($sQ);
02561         }
02562         return $this->_aMediaUrls;
02563     }
02564 
02570     public function getDynImageDir()
02571     {
02572         return $this->_sDynImageDir;
02573     }
02574 
02580     public function getDispSelList()
02581     {
02582         if ($this->_aDispSelList === null) {
02583             if ( $this->getConfig()->getConfigParam( 'bl_perfLoadSelectLists' ) && $this->getConfig()->getConfigParam( 'bl_perfLoadSelectListsInAList' ) ) {
02584                 $this->_aDispSelList = $this->getSelectLists();
02585             }
02586         }
02587         return $this->_aDispSelList;
02588     }
02589 
02595     public function getMoreDetailLink()
02596     {
02597         if ( $this->_sMoreDetailLink == null ) {
02598 
02599             // and assign special article values
02600             $this->_sMoreDetailLink = $this->getConfig()->getShopHomeURL() . 'cl=moredetails';
02601 
02602             // not always it is okey, as not all the time active category is the same as primary article cat.
02603             if ( $sActCat = oxConfig::getParameter( 'cnid' ) ) {
02604                 $this->_sMoreDetailLink .= '&amp;cnid='.$sActCat;
02605             }
02606             $this->_sMoreDetailLink .= '&amp;anid='.$this->getId();
02607             $this->_sMoreDetailLink = $this->_sMoreDetailLink;
02608         }
02609 
02610         return $this->_sMoreDetailLink;
02611     }
02612 
02618     public function getToBasketLink()
02619     {
02620         if ( $this->_sToBasketLink == null ) {
02621             $myConfig = $this->getConfig();
02622 
02623             if ( oxUtils::getInstance()->isSearchEngine() ) {
02624                 $this->_sToBasketLink = $this->getLink();
02625             } else {
02626                 // and assign special article values
02627                 $this->_sToBasketLink = $myConfig->getShopHomeURL();
02628 
02629                 // override some classes as these should never showup
02630                 $sActClass = oxConfig::getParameter( 'cl' );
02631                 if ( $sActClass == 'thankyou') {
02632                     $sActClass = 'basket';
02633                 }
02634                 $this->_sToBasketLink .= 'cl='.$sActClass;
02635 
02636                 // this is not very correct
02637                 if ( $sActCat = oxConfig::getParameter( 'cnid' ) ) {
02638                     $this->_sToBasketLink .= '&amp;cnid='.$sActCat;
02639                 }
02640 
02641                 $this->_sToBasketLink .= '&amp;fnc=tobasket&amp;aid='.$this->getId().'&amp;anid='.$this->getId();
02642 
02643                 if ( $sTpl = basename( oxConfig::getParameter( 'tpl' ) ) ) {
02644                     $this->_sToBasketLink .= '&amp;tpl='.$sTpl;
02645                 }
02646             }
02647         }
02648 
02649         return $this->_sToBasketLink;
02650     }
02651 
02657     public function getStockStatus()
02658     {
02659         return $this->_iStockStatus;
02660     }
02661 
02667     public function getDeliveryDate()
02668     {
02669         if ( $this->oxarticles__oxdelivery->value != '0000-00-00') {
02670             return oxUtilsDate::getInstance()->formatDBDate( $this->oxarticles__oxdelivery->value);
02671         }
02672         return false;
02673     }
02674 
02680     public function getFTPrice()
02681     {
02682         if ( $oPrice = $this->getTPrice() ) {
02683             if ( $oPrice->getBruttoPrice() ) {
02684                 return oxLang::getInstance()->formatCurrency( oxUtils::getInstance()->fRound($oPrice->getBruttoPrice()));
02685             }
02686         }
02687     }
02688 
02694     public function getFPrice()
02695     {
02696         if ( $oPrice = $this->getPrice() ) {
02697             return $this->getPriceFromPrefix().oxLang::getInstance()->formatCurrency( $oPrice->getBruttoPrice() );
02698         }
02699     }
02700 
02706     public function getPricePerUnit()
02707     {
02708         return $this->_fPricePerUnit;
02709     }
02710 
02716     public function isParentNotBuyable()
02717     {
02718         return $this->_blNotBuyableParent;
02719     }
02720 
02726     public function isNotBuyable()
02727     {
02728         return $this->_blNotBuyable;
02729     }
02730 
02738     public function setBuyableState( $blBuyable = false )
02739     {
02740         $this->_blNotBuyable = !$blBuyable;
02741     }
02742 
02751     public function getVariantList()
02752     {
02753         return $this->getVariants();
02754     }
02755 
02763     public function setSelectlist( $aSelList )
02764     {
02765         $this->_aDispSelList = $aSelList;
02766     }
02767 
02775     public function getPictureUrl( $iIndex = '' )
02776     {
02777         if ( $iIndex ) {
02778 
02779             if ( !$this->_hasGeneratedImage( $iIndex ) ) {
02780                 $this->_generateImages( $iIndex );
02781             }
02782 
02783             $sPic = $iIndex . "/" . $this->_getPictureName( $iIndex );
02784 
02785             return $this->getConfig()->getPictureUrl( $sPic, $this->isAdmin() );
02786         }
02787     }
02788 
02796     protected function _getPictureName( $iIndex = '' )
02797     {
02798         if ( !$this->_isFieldEmpty( "oxarticles__oxpic".$iIndex ) ) {
02799             $sPicName = basename($this->{"oxarticles__oxpic".$iIndex}->value);
02800         } else {
02801             $sPicName = "nopic.jpg";
02802         }
02803 
02804         return $sPicName;
02805     }
02806 
02815     public function getIconUrl( $iIndex = '')
02816     {
02817         if ( $this->_isFieldEmpty( "oxarticles__oxicon" ) ) {
02818 
02819             $iIconIndex = ( $iIndex ) ? $iIndex : 1;
02820             //generating new images if needed
02821             if ( !$this->_hasGeneratedImage( $iIconIndex ) ) {
02822                 $this->_generateImages( $iIconIndex );
02823             }
02824         }
02825 
02826         $sIconName = $this->_getIconName( $iIndex );
02827 
02828         if ( !$iIndex ) {
02829             $sPic = "icon/" . basename( $sIconName );
02830         } else {
02831             $sPic = $iIndex . "/" . basename( $sIconName );
02832         }
02833 
02834         return $this->getConfig()->getIconUrl( $sPic, $this->isAdmin() );
02835     }
02836 
02845     protected function _getIconName( $iIndex = '' )
02846     {
02847         $oPictureHandler = oxPictureHandler::getInstance();
02848         $sIconName = "nopic_ico.jpg";
02849 
02850         if ( !$iIndex ) {
02851             if ( !$this->_isFieldEmpty( "oxarticles__oxicon" ) ) {
02852                 $sIconName = basename( $this->oxarticles__oxicon->value );
02853             } elseif ( $this->_hasGeneratedImage( 1 ) && $this->_hasMasterImage( 1 )  && $this->oxarticles__oxpic1->value ) {
02854                 $sIconName = $oPictureHandler->getMainIconName( $this->oxarticles__oxpic1->value );
02855             }
02856         } else {
02857             if ( $this->_hasGeneratedImage( $iIndex ) && $this->{"oxarticles__oxpic".$iIndex}->value ) {
02858                 $sIconName = $oPictureHandler->getIconName( $this->{"oxarticles__oxpic".$iIndex}->value );
02859             }
02860         }
02861 
02862         return $sIconName;
02863     }
02864 
02870     public function getThumbnailUrl()
02871     {
02872         if ( $this->_isFieldEmpty( "oxarticles__oxthumb" ) ) {
02873             //generating new images if needed
02874             if ( !$this->_hasGeneratedImage( 1 ) ) {
02875                 $this->_generateImages( 1 );
02876             }
02877         }
02878 
02879         $sPic = "0/" . $this->_getThumbnailName();
02880 
02881         return $this->getConfig()->getPictureUrl( $sPic, $this->isAdmin() );
02882     }
02883 
02889     protected function _getThumbnailName()
02890     {
02891         if ( !$this->_isFieldEmpty( "oxarticles__oxthumb" ) ) {
02892             $sThumbName = basename($this->oxarticles__oxthumb->value);
02893         } else {
02894             if ( $this->_hasGeneratedImage( 1 ) && $this->_hasMasterImage( 1 ) && $this->oxarticles__oxpic1->value ) {
02895                 $oPictureHandler = oxPictureHandler::getInstance();
02896                 $sThumbName = $oPictureHandler->getThumbName( $this->oxarticles__oxpic1->value );
02897             } else {
02898                 $sThumbName = "nopic.jpg";
02899             }
02900         }
02901 
02902         return $sThumbName;
02903     }
02904 
02912     public function getZoomPictureUrl( $iIndex = '' )
02913     {
02914         $iIndex = (int) $iIndex;
02915         if ( $iIndex > 0) {
02916             //generating new images if needed
02917             if ( $this->_isFieldEmpty( "oxarticles__oxzoom" . $iIndex ) ) {
02918                 //generating new images if needed
02919                 if ( !$this->_hasGeneratedImage( $iIndex ) ) {
02920                     $this->_generateImages( $iIndex );
02921                 }
02922             }
02923 
02924             $sPic = "z{$iIndex}/" . $this->_getZoomPictureName( $iIndex );
02925 
02926             return $this->getConfig()->getPictureUrl( $sPic, $this->isAdmin() );
02927         }
02928     }
02929 
02937     protected function _getZoomPictureName( $iIndex = '' )
02938     {
02939         $sZoomField = "oxarticles__oxzoom" . $iIndex;
02940 
02941         if ( !$this->_isFieldEmpty( $sZoomField ) ) {
02942             $sZoomName = basename( $this->$sZoomField->value );
02943         } else {
02944             if ( $this->_hasGeneratedImage( $iIndex ) && $this->_hasMasterImage( $iIndex ) && $this->{"oxarticles__oxpic".$iIndex}->value ) {
02945                 $oPictureHandler = oxPictureHandler::getInstance();
02946                 $sZoomName = $oPictureHandler->getZoomName( $this->{"oxarticles__oxpic".$iIndex}->value, $iIndex );
02947             } else {
02948                 $sZoomName = "nopic.jpg";
02949             }
02950         }
02951 
02952         return $sZoomName;
02953     }
02954 
02960     public function getFileUrl()
02961     {
02962         return $this->getConfig()->getPictureUrl( '0/' );
02963     }
02964 
02970     public function getPriceFromPrefix()
02971     {
02972         $sPricePrefics = '';
02973         if ( $this->_blIsRangePrice) {
02974             $sPricePrefics = oxLang::getInstance()->translateString('priceFrom').' ';
02975         }
02976 
02977         return $sPricePrefics;
02978     }
02979 
02988     protected function _saveArtLongDesc($iLang, $sValue)
02989     {
02990         $oDB = oxDb::getDb();
02991         $iLang = (int) $iLang;
02992         $sLangField = ($iLang > '0') ? '_'.$iLang : '';
02993         $sLongDesc = $oDB->quote($sValue);
02994         $sLongDescSQL = "insert into oxartextends (oxartextends.OXID, oxartextends.OXLONGDESC{$sLangField})
02995                        VALUES ('".$this->getId()."', {$sLongDesc})
02996                        ON DUPLICATE KEY update oxartextends.OXLONGDESC{$sLangField} = {$sLongDesc} ";
02997 
02998         $oDB->execute($sLongDescSQL);
02999     }
03000 
03006     protected function _skipSaveFields()
03007     {
03008         $myConfig = $this->getConfig();
03009 
03010         $this->_aSkipSaveFields = array();
03011 
03012         $this->_aSkipSaveFields[] = 'oxtimestamp';
03013         $this->_aSkipSaveFields[] = 'oxlongdesc';
03014         $this->_aSkipSaveFields[] = 'oxinsert';
03015 
03016         if ( !isset( $this->oxarticles__oxparentid->value) || $this->oxarticles__oxparentid->value == '') {
03017             $this->_aSkipSaveFields[] = 'oxparentid';
03018         }
03019 
03020     }
03021 
03031     protected function _mergeDiscounts( $aDiscounts, $aItemDiscounts)
03032     {
03033         foreach ( $aItemDiscounts as $sKey => $oDiscount ) {
03034             // add prices of the same discounts
03035             if ( array_key_exists ($sKey, $aDiscounts) ) {
03036                 $aDiscounts[$sKey]->dDiscount += $oDiscount->dDiscount;
03037             } else {
03038                 $aDiscounts[$sKey] = $oDiscount;
03039             }
03040         }
03041         return $aDiscounts;
03042     }
03043 
03049     protected function _getGroupPrice()
03050     {
03051         $dPrice = $this->oxarticles__oxprice->value;
03052 
03053         $oUser = $this->getArticleUser();
03054         if ( $oUser ) {
03055             if ( $oUser->inGroup( 'oxidpricea' ) ) {
03056                 $dPrice = $this->oxarticles__oxpricea->value;
03057             } elseif ( $oUser->inGroup( 'oxidpriceb' ) ) {
03058                 $dPrice = $this->oxarticles__oxpriceb->value;
03059             } elseif ( $oUser->inGroup( 'oxidpricec' ) ) {
03060                 $dPrice = $this->oxarticles__oxpricec->value;
03061             }
03062         }
03063 
03064         // #1437/1436C - added config option, and check for zero A,B,C price values
03065         if ( $this->getConfig()->getConfigParam( 'blOverrideZeroABCPrices' ) && (double) $dPrice == 0 ) {
03066             $dPrice = $this->oxarticles__oxprice->value;
03067         }
03068 
03069         return $dPrice;
03070     }
03071 
03080     protected function _getAmountPrice($dAmount = 1)
03081     {
03082         $myConfig = $this->getConfig();
03083 
03084         startProfile( "_getAmountPrice" );
03085 
03086         $dPrice = $this->_getGroupPrice();
03087         $oAmtPrices = $this->_getAmountPriceList();
03088         foreach ($oAmtPrices as $oAmPrice) {
03089             if ($oAmPrice->oxprice2article__oxamount->value <= $dAmount
03090                     && $dAmount <= $oAmPrice->oxprice2article__oxamountto->value
03091                     && $dPrice > $oAmPrice->oxprice2article__oxaddabs->value ) {
03092                 $dPrice = $oAmPrice->oxprice2article__oxaddabs->value;
03093             }
03094         }
03095 
03096         stopProfile( "_getAmountPrice" );
03097         return $dPrice;
03098     }
03099 
03108     protected function _modifySelectListPrice( $dPrice, $aChosenList = null )
03109     {
03110         $myConfig = $this->getConfig();
03111         // #690
03112         if ( $myConfig->getConfigParam( 'bl_perfLoadSelectLists' ) && $myConfig->getConfigParam( 'bl_perfUseSelectlistPrice' ) ) {
03113 
03114             $aSelLists = $this->getSelectLists();
03115             foreach ( $aSelLists as $key => $aSel) {
03116                 if ( isset( $aChosenList[$key]) && isset($aSel[$aChosenList[$key]] ) ) {
03117                     $oSel = $aSel[$aChosenList[$key]];
03118                     if ( $oSel->priceUnit =='abs' ) {
03119                         $dPrice += $oSel->price;
03120                     } elseif ( $oSel->priceUnit =='%' ) {
03121                         $dPrice += oxPrice::percent( $dPrice, $oSel->price );
03122                     }
03123                 }
03124             }
03125         }
03126         return $dPrice;
03127     }
03128 
03129 
03137     protected function _fillAmountPriceList($oAmPriceList)
03138     {
03139         $myConfig = $this->getConfig();
03140         $myUtils  = oxUtils::getInstance();
03141 
03142         //modifying price
03143         $oCur = $myConfig->getActShopCurrencyObject();
03144 
03145         $oUser = $this->getArticleUser();
03146 
03147         $oDiscountList = oxDiscountList::getInstance();
03148         $aDiscountList = $oDiscountList->getArticleDiscounts( $this, $oUser );
03149 
03150         $oLowestPrice = null;
03151 
03152         $dBasePrice = $this->_getGroupPrice();
03153         $oLang = oxLang::getInstance();
03154 
03155         $dArticleVat = null;
03156         if ( !$myConfig->getConfigParam( 'bl_perfCalcVatOnlyForBasketOrder' ) ) {
03157             $dArticleVat = $this->getArticleVat();
03158         }
03159 
03160         // trying to find lowest price value
03161         foreach ($oAmPriceList as $sId => $oItem) {
03162             $oItemPrice = oxNew( 'oxprice' );
03163             if ( $oItem->oxprice2article__oxaddabs->value) {
03164                 $oItemPrice->setPrice( $oItem->oxprice2article__oxaddabs->value );
03165                 $oDiscountList->applyDiscounts( $oItemPrice, $aDiscountList );
03166                 $this->_applyCurrency( $oItemPrice, $oCur );
03167             } else {
03168                 $oItemPrice->setPrice( $dBasePrice );
03169                 $oItemPrice->subtractPercent( $oItem->oxprice2article__oxaddperc->value );
03170             }
03171 
03172             if (isset($dArticleVat)) {
03173                 $this->_applyVAT($oItemPrice, $dArticleVat);
03174             }
03175 
03176             if (!$oLowestPrice) {
03177                 $oLowestPrice = $oItemPrice;
03178             } elseif ($oLowestPrice->getBruttoPrice() > $oItemPrice->getBruttoPrice()) {
03179                 $oLowestPrice = $oItemPrice;
03180             }
03181 
03182             $oAmPriceList[$sId]->oxprice2article__oxaddabs = new oxField( $oLang->formatCurrency( $myUtils->fRound( $oItemPrice->getBruttoPrice(), $oCur ) ) );
03183             $oAmPriceList[$sId]->fnetprice  = $oLang->formatCurrency( $myUtils->fRound($oItemPrice->getNettoPrice(), $oCur ) );
03184             $oAmPriceList[$sId]->fbrutprice = $oLang->formatCurrency( $myUtils->fRound($oItemPrice->getBruttoPrice(), $oCur ) );
03185         }
03186 
03187         $this->_dAmountPrice = $myUtils->fRound( $oLowestPrice->getBruttoPrice() );
03188         return $oAmPriceList;
03189     }
03190 
03196     protected function _getVariantsIds()
03197     {
03198         $aSelect = array();
03199         $oRs = oxDb::getDb(true)->execute( "select oxid from oxarticles where oxparentid = '".$this->getId()."' " );
03200         if ( $oRs != false && $oRs->recordCount() > 0 ) {
03201             while (!$oRs->EOF) {
03202                 $aSelect[] = $oRs->fields['oxid'];
03203                 $oRs->moveNext();
03204             }
03205         }
03206         return $aSelect;
03207     }
03208 
03214     public function getArticleVat()
03215     {
03216         if (!isset($this->_dArticleVat)) {
03217             $this->_dArticleVat = oxVatSelector::getInstance()->getArticleVat( $this );
03218         }
03219         return $this->_dArticleVat;
03220     }
03221 
03230     protected function _applyVAT( oxPrice $oPrice, $dVat )
03231     {
03232         startProfile(__FUNCTION__);
03233         $oPrice->setVAT( $dVat );
03234         if ( ($dVat = oxVatSelector::getInstance()->getArticleUserVat($this)) !== false ) {
03235             $oPrice->setUserVat( $dVat );
03236         }
03237         stopProfile(__FUNCTION__);
03238     }
03239 
03247     public function applyVats( oxPrice $oPrice )
03248     {
03249         $this->_applyVAT($oPrice, $this->getArticleVat() );
03250     }
03251 
03262     protected function _applyDiscounts( $oPrice, $aDiscounts )
03263     {
03264         $oDiscountList = oxDiscountList::getInstance();
03265         $oDiscountList->applyDiscounts( $oPrice, $aDiscounts );
03266     }
03267 
03275     public function applyDiscountsForVariant( $oPrice )
03276     {
03277         // apply discounts
03278         if ( !$this->skipDiscounts() ) {
03279             $oDiscountList = oxDiscountList::getInstance();
03280             $oDiscountList->applyDiscounts( $oPrice, $oDiscountList->getArticleDiscounts( $this, $this->getArticleUser() ) );
03281         }
03282     }
03283 
03292     protected function _applyCurrency(oxPrice $oPrice, $oCur = null )
03293     {
03294         if ( !$oCur ) {
03295             $oCur = $this->getConfig()->getActShopCurrencyObject();
03296         }
03297 
03298         $oPrice->multiply($oCur->rate);
03299     }
03300 
03301 
03310     protected function _getAttribsString(&$sAttribs, &$iCnt)
03311     {
03312         // we do not use lists here as we dont need this overhead right now
03313         $oDB = oxDb::getDb(true);
03314         $sSelect =  'select oxattrid from oxobject2attribute where oxobject2attribute.oxobjectid="'.$this->getId().'" ';
03315         $sAttribs = '';
03316         $blSep = false;
03317         $rs = $oDB->execute( $sSelect);
03318         $iCnt = 0;
03319         if ($rs != false && $rs->recordCount() > 0) {
03320             while (!$rs->EOF) {
03321                 if ( $blSep) {
03322                     $sAttribs .= ' or ';
03323                 }
03324                 $sAttribs .= 't1.oxattrid = '.$oDB->quote($rs->fields['oxattrid']).' ';
03325                 $blSep = true;
03326                 $iCnt++;
03327                 $rs->moveNext();
03328             }
03329         }
03330     }
03331 
03340     protected function _getSimList($sAttribs, $iCnt)
03341     {
03342         $myConfig = $this->getConfig();
03343         $oDB      = oxDb::getDb(true);
03344 
03345         // #523A
03346         $iAttrPercent = $myConfig->getConfigParam( 'iAttributesPercent' )/100;
03347         // 70% same attributes
03348         if ( !$iAttrPercent || $iAttrPercent < 0 || $iAttrPercent > 1) {
03349             $iAttrPercent = 0.70;
03350         }
03351         // #1137V iAttributesPercent = 100 doesnt work
03352         $iHitMin = ceil( $iCnt * $iAttrPercent );
03353 
03354         // we do not use lists here as we dont need this overhead right now
03355         $aList= array();
03356         $sSelect =  "select oxobjectid, count(*) as cnt from oxobject2attribute as t1 where
03357                     ( $sAttribs )
03358                     and t1.oxobjectid != '".$this->oxarticles__oxid->value."'
03359                     group by t1.oxobjectid having count(*) >= $iHitMin ";
03360 
03361         $rs = $oDB->selectLimit( $sSelect, 20, 0);
03362         if ($rs != false && $rs->recordCount() > 0) {
03363             while (!$rs->EOF) {
03364                 $oTemp = new stdClass();    // #663
03365                 $oTemp->cnt = $rs->fields['cnt'];
03366                 $oTemp->id  = $rs->fields['oxobjectid'];
03367                 $aList[] = $oTemp;
03368                 $rs->moveNext();
03369             }
03370         }
03371         return $aList;
03372     }
03373 
03382     protected function _generateSimListSearchStr($sArticleTable, $aList)
03383     {
03384         $myConfig = $this->getConfig();
03385         $sFieldList = $this->getSelectFields();
03386         $sSearch = "select $sFieldList from $sArticleTable where ".$this->getSqlActiveSnippet()."  and $sArticleTable.oxissearch = 1 and $sArticleTable.oxid in ( ";
03387         $blSep = false;
03388         $iCnt = 0;
03389         $oDb = oxDb::getDb();
03390         foreach ( $aList as $oTemp) {
03391             if ( $blSep) {
03392                 $sSearch .= ',';
03393             }
03394             $sSearch .= $oDb->quote($oTemp->id);
03395             $blSep = true;
03396             if ( $iCnt >= $myConfig->getConfigParam( 'iNrofSimilarArticles' ) ) {
03397                 break;
03398             }
03399             $iCnt++;
03400         }
03401 
03402         //#1741T
03403         //$sSearch .= ") and $sArticleTable.oxparentid = '' ";
03404         $sSearch .= ') ';
03405 
03406         // #524A -- randomizing articles in attribute list
03407         $sSearch .= ' order by rand() ';
03408 
03409         return $sSearch;
03410     }
03411 
03420     protected function _generateSearchStr($sOXID, $blSearchPriceCat = false )
03421     {
03422         $sCatView = getViewName( 'oxcategories' );
03423         $sO2CView = getViewName( 'oxobject2category' );
03424 
03425         // we do not use lists here as we dont need this overhead right now
03426         if ( !$blSearchPriceCat ) {
03427             $sSelect  = "select {$sCatView}.* from {$sO2CView} as oxobject2category left join {$sCatView} on
03428                          {$sCatView}.oxid = oxobject2category.oxcatnid
03429                          where oxobject2category.oxobjectid=".oxDb::getDb()->quote($sOXID)." and {$sCatView}.oxid is not null ";
03430         } else {
03431             $sSelect  = "select {$sCatView}.* from {$sCatView} where
03432                          '{$this->oxarticles__oxprice->value}' >= {$sCatView}.oxpricefrom and
03433                          '{$this->oxarticles__oxprice->value}' <= {$sCatView}.oxpriceto ";
03434         }
03435         return $sSelect;
03436     }
03437 
03443     protected function _generateSearchStrForCustomerBought()
03444     {
03445         $sArtTable = $this->_getObjectViewName( 'oxarticles' );
03446         $sOrderArtTable = getViewName( 'oxorderarticles' );
03447 
03448         // fetching filter params
03449         $sIn = " '{$this->oxarticles__oxid->value}' ";
03450         if ( $this->oxarticles__oxparentid->value ) {
03451 
03452             // adding article parent
03453             $sIn .= ", '{$this->oxarticles__oxparentid->value}' ";
03454             $sParentIdForVariants = $this->oxarticles__oxparentid->value;
03455 
03456         } else {
03457             $sParentIdForVariants = $this->getId();
03458         }
03459 
03460         // adding variants
03461         $oDb = oxDb::getDb(true);
03462         $oRs = $oDb->execute( "select oxid from {$sArtTable} where oxparentid = ".$oDb->quote($sParentIdForVariants)." and oxid != ".$oDb->quote($this->oxarticles__oxid->value)." " );
03463         if ( $oRs != false && $oRs->recordCount() > 0) {
03464             while ( !$oRs->EOF ) {
03465                 $sIn .= ", ".$oDb->quote(current( $oRs->fields ))." ";
03466                 $oRs->moveNext();
03467             }
03468         }
03469 
03470         $iLimit = (int) $this->getConfig()->getConfigParam( 'iNrofCustomerWhoArticles' );
03471         $iLimit = $iLimit?( $iLimit * 10 ): 50;
03472 
03473         // building sql (optimized)
03474         $sQ = "select distinct {$sArtTable}.* from (
03475                    select d.oxorderid as suborderid from {$sOrderArtTable} as d use index ( oxartid ) where d.oxartid in ( {$sIn} ) limit {$iLimit}
03476                ) as suborder
03477                left join {$sOrderArtTable} force index ( oxorderid ) on suborder.suborderid = {$sOrderArtTable}.oxorderid
03478                left join {$sArtTable} on {$sArtTable}.oxid = {$sOrderArtTable}.oxartid
03479                where {$sArtTable}.oxid not in ( {$sIn} )
03480                and ( {$sArtTable}.oxissearch = 1 or {$sArtTable}.oxparentid <> '' ) and ".$this->getSqlActiveSnippet();
03481 
03482         /* non optimized, but could be used if index forcing is not supported
03483         // building sql
03484         $sQ = "select distinct {$sArtTable}.* from {$sOrderArtTable}, {$sArtTable} where {$sOrderArtTable}.oxorderid in (
03485                    select {$sOrderArtTable}.oxorderid from {$sOrderArtTable} where {$sOrderArtTable}.oxartid in ( {$sIn} )
03486                ) and {$sArtTable}.oxid = {$sOrderArtTable}.oxartid and {$sArtTable}.oxid not in ( {$sIn} )
03487                and ( {$sArtTable}.oxissearch = 1 or {$sArtTable}.oxparentid <> '' )
03488                and ".$this->getSqlActiveSnippet();
03489         */
03490 
03491         return $sQ;
03492     }
03493 
03503     protected function _generateSelectCatStr($sOXID, $sCatId, $dPriceFromTo = false)
03504     {
03505         $sCategoryView = getViewName('oxcategories');
03506         $sO2CView = getViewName('oxobject2category');
03507         $sLangPrefix = (($this->getLanguage())?'_'.$this->getLanguage():'');
03508 
03509         $oDb    = oxDb::getDb();
03510         $sOXID  = $oDb->quote($sOXID);
03511         $sCatId = $oDb->quote($sCatId);
03512 
03513         if (!$dPriceFromTo) {
03514             $sSelect  = "select oxobject2category.oxcatnid from $sO2CView as oxobject2category ";
03515             $sSelect .= "left join $sCategoryView as oxcategories on oxcategories.oxid = oxobject2category.oxcatnid ";
03516             $sSelect .= "where oxobject2category.oxcatnid=$sCatId and oxobject2category.oxobjectid=$sOXID ";
03517             $sSelect .= "and oxcategories.oxactive$sLangPrefix = 1 order by oxobject2category.oxtime ";
03518         } else {
03519             $dPriceFromTo = $oDb->quote($dPriceFromTo);
03520             $sSelect  = "select oxcategories.oxid from $sCategoryView as oxcategories where ";
03521             $sSelect .= "oxcategories.oxid=$sCatId and $dPriceFromTo >= oxcategories.oxpricefrom and ";
03522             $sSelect .= "$dPriceFromTo <= oxcategories.oxpriceto ";
03523         }
03524         return $sSelect;
03525     }
03526 
03532     protected function _getAmountPriceList()
03533     {
03534         if ($this->_oAmountPriceList) {
03535             return $this->_oAmountPriceList;
03536         }
03537 
03538         $myConfig = $this->getConfig();
03539 
03540         $sArtID  = $this->getId();
03541 
03542         // #1690C - Scale prices and variants
03543         if ( !$this->isAdmin() && $myConfig->getConfigParam( 'blVariantInheritAmountPrice' ) && $this->oxarticles__oxparentid->value ) {
03544             $sArtID = $this->oxarticles__oxparentid->value;
03545         }
03546 
03547         $sArtID = mysql_real_escape_string($sArtID);
03548 
03549         //collecting assigned to article amount-price list
03550         $oAmPriceList = oxNew( 'oxlist');
03551         $oAmPriceList->init('oxbase', 'oxprice2article');
03552 
03553         $sShopID = $myConfig->getShopID();
03554         if ( $myConfig->getConfigParam( 'blMallInterchangeArticles' ) ) {
03555             $sShopSelect = '1';
03556         } else {
03557             $sShopSelect = " oxshopid =  '$sShopID' ";
03558         }
03559 
03560         $oAmPriceList->selectString( "select * from oxprice2article where oxartid = '$sArtID' and $sShopSelect order by oxamount ");
03561 
03562         // prepare abs prices if currently having percentages
03563         $oBasePrice = $this->_getGroupPrice();
03564         foreach ($oAmPriceList as $oAmPrice) {
03565             if ($oAmPrice->oxprice2article__oxaddperc->value) {
03566                 $oAmPrice->oxprice2article__oxaddabs = new oxField(oxPrice::percent( $oBasePrice, 100 - $oAmPrice->oxprice2article__oxaddperc->value ), oxField::T_RAW);
03567             }
03568         }
03569 
03570         $this->_oAmountPriceList = $oAmPriceList;
03571         return $oAmPriceList;
03572     }
03573 
03581     protected function _isFieldEmpty($sFieldName)
03582     {
03583         $mValue = $this->$sFieldName->value;
03584 
03585         if (is_null($mValue)) {
03586             return true;
03587         }
03588 
03589         if ($mValue === '') {
03590             return true;
03591         }
03592 
03593         $aDoubleCopyFields = array('oxarticles__oxprice',
03594                                        'oxarticles__oxvat');
03595 
03596         if (!$mValue && in_array($sFieldName, $aDoubleCopyFields)) {
03597             return true;
03598         }
03599 
03600 
03601         if (!strcmp($mValue, '0000-00-00 00:00:00') || !strcmp($mValue, '0000-00-00')) {
03602             return true;
03603         }
03604 
03605         $sFieldName = strtolower($sFieldName);
03606 
03607         if ( $sFieldName == 'oxarticles__oxicon' && strpos($mValue, "nopic_ico.jpg") !== false ) {
03608             return true;
03609         }
03610 
03611         if ( strpos($mValue, "nopic.jpg") !== false && ($sFieldName == 'oxarticles__oxthumb' || substr($sFieldName, 0, 17) == 'oxarticles__oxpic' || substr($sFieldName, 0, 18) == 'oxarticles__oxzoom') ) {
03612             return true;
03613         }
03614 
03615         return false;
03616     }
03617 
03625     protected function _assignParentFieldValue($sFieldName)
03626     {
03627         if (!($oParentArticle = $this->getParentArticle())) {
03628             return;
03629         }
03630 
03631         $sCopyFieldName = $this->_getFieldLongName($sFieldName);
03632 
03633         // assigning only theese which parent article has
03634         if ( $oParentArticle->$sCopyFieldName != null ) {
03635 
03636             // only overwrite database values
03637             if ( substr( $sCopyFieldName, 0, 12) != 'oxarticles__') {
03638                 continue;
03639             }
03640 
03641             //do not copy certain fields
03642             if (in_array($sCopyFieldName, $this->_aNonCopyParentFields)) {
03643                 return;
03644             }
03645 
03646             //COPY THE VALUE
03647             // assigning images from parent only if variant has no master image (#1807)
03648             if ( stristr($sCopyFieldName, '_oxthumb') || stristr($sCopyFieldName, '_oxicon') ) {
03649                 if ( $this->_isFieldEmpty( $sCopyFieldName ) && !$this->_hasMasterImage( 1 ) ) {
03650                     $this->$sCopyFieldName = clone $oParentArticle->$sCopyFieldName;
03651                 }
03652             } elseif ( stristr($sCopyFieldName, '_oxzoom') ) {
03653                 // for zoom images checking master image with specified index
03654                 // assign from parent only if no pictures to variant are added
03655                 $iIndex = (int) str_ireplace( "oxarticles__oxzoom", "", $sFieldName );
03656                 if ( $this->_isFieldEmpty( $sCopyFieldName ) && !$this->_hasMasterImage( $iIndex ) && !$this->_hasMasterImage( 1 ) ) {
03657                     $this->$sCopyFieldName = clone $oParentArticle->$sCopyFieldName;
03658                 }
03659             } elseif ( stristr($sCopyFieldName, '_oxpicsgenerated') && $this->{$sCopyFieldName}->value == 0 ) {
03660                 // if no pics generated for variants, load all from
03661                 $this->$sCopyFieldName = clone $oParentArticle->$sCopyFieldName;
03662             } elseif ($this->_isFieldEmpty($sCopyFieldName) || in_array( $sCopyFieldName, $this->_aCopyParentField ) ) {
03663                 $this->$sCopyFieldName = clone $oParentArticle->$sCopyFieldName;
03664             }
03665         }
03666     }
03667 
03673     public function getParentArticle()
03674     {
03675         if ( ( $sParentId = $this->oxarticles__oxparentid->value ) ) {
03676             if ( !isset( self::$_aLoadedParents[$sParentId] ) ) {
03677                 self::$_aLoadedParents[$sParentId] = oxNew( 'oxarticle' );
03678                 self::$_aLoadedParents[$sParentId]->_blSkipAbPrice  = true;
03679                 self::$_aLoadedParents[$sParentId]->_blLoadPrice    = false;
03680                 self::$_aLoadedParents[$sParentId]->_blLoadVariants = false;
03681                 self::$_aLoadedParents[$sParentId]->load( $sParentId );
03682             }
03683             return self::$_aLoadedParents[$sParentId];
03684         }
03685     }
03686 
03694     protected function _getParentAricle()
03695     {
03696         return $this->getParentArticle();
03697     }
03698 
03704     protected function _assignParentFieldValues()
03705     {   startProfile('articleAssignParentInternal');
03706         if ( $this->oxarticles__oxparentid->value) {
03707             // yes, we are in fact a variant
03708             if ( !$this->isAdmin() || ($this->_blLoadParentData && $this->isAdmin() ) ) {
03709                 foreach ($this->_aFieldNames as $sFieldName => $sVal) {
03710                     $this->_assignParentFieldValue($sFieldName);
03711                 }
03712 
03713                 //assing long description
03714                 $oParentArticle = $this->getParentArticle();
03715                 if ( !$this->oxarticles__oxlongdesc->value ) {
03716                     $this->oxarticles__oxlongdesc = $oParentArticle->oxarticles__oxlongdesc;
03717                 }
03718 
03719             }
03720         }
03721         stopProfile('articleAssignParentInternal');
03722 
03723     }
03724 
03730     protected function _assignNotBuyableParent()
03731     {
03732         if ( !$this->getConfig()->getConfigParam( 'blVariantParentBuyable' ) &&
03733              ($this->_blHasVariants || $this->oxarticles__oxvarstock->value || $this->oxarticles__oxvarcount->value )) {
03734             $this->_blNotBuyableParent = true;
03735 
03736         }
03737     }
03738 
03744     protected function _assignAllPictureValues()
03745     {
03746         $myConfig = $this->getConfig();
03747 
03748         $this->_assignPictureValues( "oxarticles__oxicon" );
03749         $this->_assignPictureValues( "oxarticles__oxthumb" );
03750 
03751         $iPicCount = $myConfig->getConfigParam( 'iPicCount' );
03752 
03753         for ( $i=1; $i<= $iPicCount; $i++ ) {
03754             $this->_assignPictureValues( "oxarticles__oxpic".$i );
03755         }
03756 
03757         if ( $iZoomPicCount = $myConfig->getConfigParam( 'iZoomPicCount' ) ) {
03758             for ( $i=1; $i<= $iZoomPicCount; $i++ ) {
03759                 $this->_assignZoomPictureValues( "oxarticles__oxzoom".$i );
03760             }
03761         }
03762     }
03763 
03771     protected function _assignZoomPictureValues( $sName='' )
03772     {
03773         if ( $this->isAdmin() ) {
03774             return;
03775         }
03776 
03777         $sFieldName = substr_replace( $sName, "", 0, 12);
03778 
03779         $aAllFields = $this->_getAllFields( true );
03780 
03781         if ( isset( $aAllFields[$sFieldName] ) ) {
03782             $this->$sName = parent::__get( $sName );
03783             $this->$sName->value;
03784         }
03785 
03786         $this->_assignParentFieldValue( $sName );
03787 
03788         $iIndex = (int) str_ireplace( "oxzoom", "", $sFieldName );
03789 
03790         if ( isset($this->_aFieldNames[$sFieldName]) || isset($this->_aFieldNames["oxpic".$iIndex]) ) {
03791             if ( $iIndex > 0 ) {
03792                 $this->$sName = new oxField( 'z' . $iIndex.'/'.$this->_getZoomPictureName($iIndex) );
03793             }
03794         }
03795     }
03796 
03804     protected function _assignPictureValues( $sName='' )
03805     {
03806         if ( $this->isAdmin() || !$sName ) {
03807             return;
03808         }
03809 
03810         $sFieldName = substr_replace( $sName, "", 0, 12);
03811         $myConfig = $this->getConfig();
03812 
03813         // add directories
03814         if ( $sFieldName == 'oxicon' && isset($this->_aFieldNames["oxicon"]) ) {
03815             $this->oxarticles__oxicon = new oxField('icon/'.$this->_getIconName());
03816             return;
03817         }
03818 
03819         if ( $sFieldName == 'oxthumb' && isset($this->_aFieldNames["oxthumb"]) ) {
03820             $this->oxarticles__oxthumb = new oxField('0/'.$this->_getThumbnailName());
03821             return;
03822         }
03823 
03824         $iPicCount = $myConfig->getConfigParam( 'iPicCount' );
03825         if ( strpos($sFieldName, "oxpic") === 0 && isset($this->_aFieldNames[$sFieldName] ) ) {
03826 
03827             $iIndex = (int) str_ireplace( "oxpic", "", $sFieldName );
03828 
03829             if ( $iIndex > 0 && $iIndex < $iPicCount ) {
03830                 $this->$sName = new oxField( $iIndex . '/'.$this->_getPictureName($iIndex) );
03831                 $this->{$sName.'_ico'} = new oxField( $iIndex . '/'.$this->_getIconName($iIndex) );
03832                 return;
03833             }
03834         }
03835     }
03836 
03842     protected function _assignStock()
03843     {
03844         $myConfig = $this->getConfig();
03845         // -----------------------------------
03846         // stock
03847         // -----------------------------------
03848 
03849         // #1125 A. must round (using floor()) value taken from database and cast to int
03850         if (!$myConfig->getConfigParam( 'blAllowUnevenAmounts' ) && !$this->isAdmin() ) {
03851             $this->oxarticles__oxstock = new oxField((int) floor($this->oxarticles__oxstock->value));
03852         }
03853         //GREEN light
03854         $this->_iStockStatus = 0;
03855 
03856         // if we have flag /*1 or*/ 4 - we show always green light
03857         if ( $myConfig->getConfigParam( 'blUseStock' ) && /*$this->oxarticles__oxstockflag->value != 1 && */ $this->oxarticles__oxstockflag->value != 4) {
03858             //ORANGE light
03859             $iStock = $this->oxarticles__oxstock->value;
03860 
03861             if ($this->_blNotBuyableParent) {
03862                 $iStock = $this->oxarticles__oxvarstock->value;
03863             }
03864 
03865 
03866             if ( $iStock <= $myConfig->getConfigParam( 'sStockWarningLimit' ) && $iStock > 0) {
03867                 $this->_iStockStatus = 1;
03868             }
03869 
03870             //RED light
03871             if ($iStock <= 0) {
03872                 $this->_iStockStatus = -1;
03873             }
03874         }
03875 
03876 
03877         // stock
03878         if ( $myConfig->getConfigParam( 'blUseStock' ) && ($this->oxarticles__oxstockflag->value == 3 || $this->oxarticles__oxstockflag->value == 2)) {
03879             $iOnStock = $this->oxarticles__oxstock->value;
03880             if ($this->getConfig()->getConfigParam( 'blPsBasketReservationEnabled' )) {
03881                 $iOnStock += $this->getSession()->getBasketReservations()->getReservedAmount($this->getId());
03882             }
03883             if ($iOnStock <= 0) {
03884                 $this->_blNotBuyable = true;
03885             }
03886         }
03887 
03888         //exceptional handling for variant parent stock:
03889         if ($this->_blNotBuyable && $this->oxarticles__oxvarstock->value ) {
03890             $this->_blNotBuyable = false;
03891             //but then at least setting notBuaybleParent to true
03892             $this->_blNotBuyableParent = true;
03893         }
03894 
03895         //special treatment for lists when blVariantParentBuyable config option is set to false
03896         //then we just hide "to basket" button.
03897         //if variants are not loaded in the list and this article has variants and parent is not buyable then this article is not buyable
03898         if ( !$myConfig->getConfigParam( 'blVariantParentBuyable' ) && !$myConfig->getConfigParam( 'blLoadVariants' ) && $this->oxarticles__oxvarstock->value) {
03899             $this->_blNotBuyable = true;
03900         }
03901 
03902         //setting to non buyable when variant list is empty (for example not loaded or inactive) and $this is non buyable parent
03903         if ($this->_blNotBuyableParent && count($this->getVariants()) == 0) {
03904             $this->_blNotBuyable = true;
03905         }
03906     }
03907 
03913     protected function _assignPrices()
03914     {
03915         $myConfig = $this->getConfig();
03916 
03917         // Performance
03918         if ( !$myConfig->getConfigParam( 'bl_perfLoadPrice' ) || !$this->_blLoadPrice ) {
03919             return;
03920         }
03921 
03922         // compute price
03923         $dPrice = $this->getPrice()->getBruttoPrice();
03924 
03925         $oCur = $myConfig->getActShopCurrencyObject();
03926         //price per unit handling
03927         if ((double) $this->oxarticles__oxunitquantity->value && $this->oxarticles__oxunitname->value) {
03928             $this->_fPricePerUnit = oxLang::getInstance()->formatCurrency($dPrice / (double) $this->oxarticles__oxunitquantity->value, $oCur);
03929         }
03930 
03931 
03932         //getting min and max prices of variants
03933         $this->_applyRangePrice();
03934     }
03935 
03941     protected function _assignPersistentParam()
03942     {
03943         // Persistent Parameter Handling
03944         $aPersParam     = oxSession::getVar( 'persparam');
03945         if ( isset( $aPersParam) && isset( $aPersParam[$this->getId()])) {
03946             $this->_aPersistParam = $aPersParam[$this->getId()];
03947         }
03948     }
03949 
03955     protected function _assignDynImageDir()
03956     {
03957         $myConfig = $this->getConfig();
03958 
03959         $sThisShop = $this->oxarticles__oxshopid->value;
03960 
03961         $this->_sDynImageDir   = $myConfig->getPictureUrl( null, false );
03962         $this->dabsimagedir    = $myConfig->getPictureDir( false ); //$sThisShop
03963         $this->nossl_dimagedir = $myConfig->getPictureUrl( null, false, false, null, $sThisShop ); //$sThisShop
03964         $this->ssl_dimagedir   = $myConfig->getPictureUrl( null, false, true, null, $sThisShop ); //$sThisShop
03965     }
03966 
03972     protected function _assignComparisonListFlag()
03973     {
03974         // #657 add a flag if article is on comparisonlist
03975 
03976         $aItems = oxConfig::getParameter('aFiltcompproducts');
03977         if ( isset( $aItems[$this->getId()])) {
03978             $this->_blIsOnComparisonList = true;
03979         }
03980     }
03981 
03987     protected function _assignAttributes()
03988     {
03989         //#1029T load attributes
03990         //#1078S removed check for module "Produktvergleich"
03991         if ( $this->getConfig()->getConfigParam( 'bl_perfLoadAttributes' ) ) {
03992             $this->getAttributes();
03993         }
03994     }
03995 
03996 
04004     protected function _insert()
04005     {
04006         // set oxinsert
04007         $iInsertTime = time();
04008         $now = date('Y-m-d H:i:s', $iInsertTime);
04009         $this->oxarticles__oxinsert    = new oxField( $now );
04010         $this->oxarticles__oxtimestamp = new oxField( $now );
04011         if ( !is_object($this->oxarticles__oxsubclass) || $this->oxarticles__oxsubclass->value == '') {
04012             $this->oxarticles__oxsubclass = new oxField('oxarticle');
04013         }
04014 
04015         return parent::_insert();
04016     }
04017 
04023     protected function _update()
04024     {
04025 
04026         $this->_skipSaveFields();
04027 
04028         $myConfig = $this->getConfig();
04029 
04030 
04031         return parent::_update();
04032     }
04033 
04041     protected function _deleteRecords($sOXID)
04042     {
04043         $oDB = oxDb::getDb();
04044 
04045         $sOXID = $oDB->quote($sOXID);
04046 
04047         //delete the record
04048         $sDelete = 'delete from '.$this->_sCoreTbl.' where oxid = '.$sOXID.' ';
04049         $oDB->execute( $sDelete);
04050 
04051         //remove other records
04052         $sDelete = 'delete from oxobject2article where oxarticlenid = '.$sOXID.' or oxobjectid = '.$sOXID.' ';
04053         $oDB->execute( $sDelete);
04054 
04055         $sDelete = 'delete from oxobject2attribute where oxobjectid = '.$sOXID.' ';
04056         $oDB->execute( $sDelete);
04057 
04058         $sDelete = 'delete from oxobject2category where oxobjectid = '.$sOXID.' ';
04059         $oDB->execute( $sDelete);
04060 
04061         $sDelete = 'delete from oxobject2selectlist where oxobjectid = '.$sOXID.' ';
04062         $oDB->execute( $sDelete);
04063 
04064         $sDelete = 'delete from oxprice2article where oxartid = '.$sOXID.' ';
04065         $oDB->execute( $sDelete);
04066 
04067         $sDelete = 'delete from oxreviews where oxtype="oxarticle" and oxobjectid = '.$sOXID.' ';
04068         $oDB->execute( $sDelete);
04069 
04070         $sDelete = 'delete from oxaccessoire2article where oxobjectid = '.$sOXID.' or oxarticlenid = '.$sOXID.' ';
04071         $oDB->execute( $sDelete);
04072 
04073         //#1508C - deleting oxobject2delivery entries added
04074         $sDelete = 'delete from oxobject2delivery where oxobjectid = '.$sOXID.' and oxtype=\'oxarticles\' ';
04075         $oDB->execute( $sDelete);
04076 
04077         $sDelete = 'delete from oxartextends where oxid = '.$sOXID.' ';
04078         $oDB->execute( $sDelete);
04079 
04080         $sDelete = 'delete from oxactions2article where oxartid = '.$sOXID.' ';
04081         $rs = $oDB->execute( $sDelete );
04082 
04083         $sDelete = 'delete from oxobject2list where oxobjectid = '.$sOXID.' ';
04084         $rs = $oDB->execute( $sDelete );
04085 
04086 
04087         return $rs;
04088     }
04089 
04097     protected function _deleteVariantRecords($sOXID)
04098     {
04099         $oDB = oxDb::getDb();
04100         //collect variants to remove recursively
04101         $sVariants = 'select oxid from '.$this->getViewName().' where oxparentid = '.$oDB->quote($sOXID);
04102         $rs = $oDB->execute( $sVariants);
04103         if ($rs != false && $rs->recordCount() > 0) {
04104             while (!$rs->EOF) {
04105                 $this->delete( $rs->fields[0]);
04106                 $rs->moveNext();
04107             }
04108         }
04109     }
04110 
04120     protected function _resetCacheAndArticleCount( $sOxid )
04121     {
04122         $this->_onChangeResetCounts( $sOxid, $this->oxarticles__oxvendorid->value, $this->oxarticles__oxmanufacturerid->value );
04123     }
04124 
04130     protected function _deletePics()
04131     {
04132         $myUtilsPic = oxUtilsPic::getInstance();
04133         $myConfig   = $this->getConfig();
04134         $oPictureHandler = oxPictureHandler::getInstance();
04135 
04136         //deleting custom main icon
04137         $oPictureHandler->deleteMainIcon( $this );
04138 
04139         //deleting custom thumbnail
04140         $oPictureHandler->deleteThumbnail( $this );
04141 
04142         $sAbsDynImageDir = $myConfig->getAbsDynImageDir();
04143 
04144         // deleting master image and all generated images
04145         $iPicCount = $myConfig->getConfigParam( 'iPicCount' );
04146         for ( $i = 1; $i <= $iPicCount; $i++ ) {
04147             $oPictureHandler->deleteArticleMasterPicture( $this, $i );
04148         }
04149     }
04150 
04160     protected function _onChangeResetCounts( $sOxid, $sVendorId = null, $sManufacturerId = null )
04161     {
04162 
04163         $myUtilsCount = oxUtilsCount::getInstance();
04164 
04165         if ( $sVendorId ) {
04166             $myUtilsCount->resetVendorArticleCount( $sVendorId );
04167         }
04168 
04169         if ( $sManufacturerId ) {
04170             $myUtilsCount->resetManufacturerArticleCount( $sManufacturerId );
04171         }
04172 
04173         //also reseting category counts
04174         $oDb = oxDb::getDb();
04175         $sQ = "select oxcatnid from oxobject2category where oxobjectid = ".$oDb->quote($sOxid);
04176         $oRs = $oDb->execute( $sQ );
04177         if ( $oRs !== false && $oRs->recordCount() > 0) {
04178             while ( !$oRs->EOF ) {
04179                 $myUtilsCount->resetCatArticleCount( $oRs->fields[0] );
04180                 $oRs->moveNext();
04181             }
04182         }
04183     }
04184 
04192     protected function _onChangeUpdateStock( $sParentID )
04193     {
04194         $oDb = oxDb::getDb();
04195         $sParentIdQuoted = $oDb->quote($sParentID);
04196         $sQ = 'select oxstock, oxvendorid, oxmanufacturerid from oxarticles where oxid = '.$sParentIdQuoted;
04197         $rs = $oDb->execute($sQ);
04198         $iOldStock = $rs->fields[0];
04199         $iVendorID = $rs->fields[1];
04200         $iManufacturerID = $rs->fields[2];
04201 
04202         $sQ = 'select sum(oxstock) from oxarticles where oxparentid = '.$sParentIdQuoted.' and '. $this->getSqlActiveSnippet( true ).' and oxstock > 0 ';
04203         $iStock = (float) $oDb->getOne( $sQ );
04204 
04205         $sQ = 'update oxarticles set oxvarstock = '.$iStock.' where oxid = '.$sParentIdQuoted;
04206         $oDb->execute( $sQ );
04207 
04208             //now lets update category counts
04209             //first detect stock status change for this article (to or from 0)
04210             if ( $iStock < 0 ) {
04211                 $iStock = 0;
04212             }
04213             if ( $iOldStock < 0 ) {
04214                 $iOldStock = 0;
04215             }
04216             if ( $this->oxarticles__oxstockflag->value == 2 && $iOldStock xor $iStock ) {
04217                 //means the stock status could be changed (oxstock turns from 0 to 1 or from 1 to 0)
04218                 // so far we leave it like this but later we could move all count resets to one or two functions
04219                 $this->_onChangeResetCounts( $sParentID, $iVendorID, $iManufacturerID );
04220             }
04221     }
04222 
04230     protected function _onChangeStockResetCount( $sOxid )
04231     {
04232         $myConfig = $this->getConfig();
04233 
04234         if ( $myConfig->getConfigParam( 'blUseStock' ) && $this->oxarticles__oxstockflag->value == 2 &&
04235            ( $this->oxarticles__oxstock->value + $this->oxarticles__oxvarstock->value ) <= 0 ) {
04236 
04237                $this->_onChangeResetCounts( $sOxid, $this->oxarticles__oxvendorid->value, $this->oxarticles__oxmanufacturerid->value );
04238         }
04239     }
04240 
04248     protected function _onChangeUpdateVarCount( $sParentID )
04249     {
04250         $oDb = oxDb::getDb();
04251         $sParentIdQuoted = $oDb->quote($sParentID);
04252         $sQ = 'select count(*) as varcount from oxarticles where oxparentid = '.$sParentIdQuoted;
04253         $iVarCount = (int) $oDb->getOne($sQ);
04254 
04255         $sQ = 'update oxarticles set oxvarcount = '.$iVarCount.' where oxid = '.$sParentIdQuoted;
04256         $oDb->execute($sQ);
04257     }
04258 
04266     protected function _onChangeUpdateMinVarPrice( $sParentID )
04267     {
04268         $oDb = oxDb::getDb();
04269         $sParentIdQuoted = $oDb->quote($sParentID);
04270         //#M0000883 (Sarunas)
04271         $sQ = 'select min(oxprice) as varminprice from oxarticles where '.$this->getSqlActiveSnippet(true).' and (oxparentid = '.$sParentIdQuoted.')';
04272         $dVarMinPrice = $oDb->getOne($sQ);
04273 
04274         $dParentPrice = $oDb->getOne("select oxprice from oxarticles where oxid = $sParentIdQuoted ");
04275 
04276         $blParentBuyable =  $this->getConfig()->getConfigParam( 'blVariantParentBuyable' );
04277 
04278         if ($dVarMinPrice) {
04279             if ($blParentBuyable) {
04280                 $dVarMinPrice = min($dVarMinPrice, $dParentPrice);
04281             }
04282 
04283         } else {
04284             $dVarMinPrice = $dParentPrice;
04285         }
04286 
04287         if ( $dVarMinPrice ) {
04288             $sQ = 'update oxarticles set oxvarminprice = '.$dVarMinPrice.' where oxid = '.$sParentIdQuoted;
04289             $oDb->execute($sQ);
04290         }
04291     }
04292 
04293 
04299     protected function _applyRangePrice()
04300     {
04301         //#buglist_413 if bl_perfLoadPriceForAddList variant price shouldn't be loaded too
04302         if ( !$this->getConfig()->getConfigParam( 'bl_perfLoadPrice' ) || !$this->_blLoadPrice ) {
04303             return;
04304         }
04305 
04306         $this->_blIsRangePrice = false;
04307 
04308         // if parent is buyable - do not apply range price calcculations
04309         if ($this->_blSkipAbPrice || !$this->_blNotBuyableParent) {
04310             return;
04311         }
04312 
04313         $aPrices = array();
04314 
04315         if (!$this->_blNotBuyableParent) {
04316             $aPrices[] = $this->getPrice()->getBruttoPrice();
04317         }
04318 
04319         $aVariants = $this->getVariants(false);
04320 
04321         if (count($aVariants)) {
04322             foreach ($aVariants as $sKey => $oVariant) {
04323                 $aPrices[] = $oVariant->getPrice()->getBruttoPrice();
04324             }
04325         }
04326 
04327         /*  $oAmPrices = $this->loadAmountPriceInfo();
04328         foreach ($oAmPrices as $oAmPrice) {
04329             $aPrices[] = $oAmPrice->oxprice2article__oxaddabs->value;
04330         }*/
04331 
04332         if (count($aPrices)) {
04333             $dMinPrice = $aPrices[0];
04334             $dMaxPrice = $aPrices[0];
04335             foreach ($aPrices as $dPrice) {
04336                 if ($dMinPrice > $dPrice) {
04337                     $dMinPrice = $dPrice;
04338                 }
04339 
04340                 if ($dMaxPrice < $dPrice) {
04341                     $dMaxPrice = $dPrice;
04342                 }
04343             }
04344         }
04345 
04346         if ($this->_blNotBuyableParent && isset($dMinPrice) && $dMinPrice == $dMaxPrice) {
04347             $this->getPrice()->setBruttoPriceMode();
04348             $this->getPrice()->setPrice($dMinPrice);
04349         }
04350 
04351         if (isset($dMinPrice) && $dMinPrice != $dMaxPrice) {
04352             $this->getPrice()->setBruttoPriceMode();
04353             $this->getPrice()->setPrice($dMinPrice);
04354             $this->_blIsRangePrice = true;
04355         }
04356 
04357         if ( $this->isParentNotBuyable() && !$this->getConfig()->getConfigParam( 'blLoadVariants' )) {
04358             $this->getPrice()->setBruttoPriceMode();
04359             $this->getPrice()->setPrice($this->oxarticles__oxvarminprice->value);
04360             $this->_blIsRangePrice = true;
04361             $this->_calculatePrice( $this->getPrice() );
04362         }
04363     }
04364 
04371     public function getProductId()
04372     {
04373         return $this->getId();
04374     }
04375 
04381     public function getProductParentId()
04382     {
04383         return $this->oxarticles__oxparentid->value;
04384     }
04385 
04391     public function isOrderArticle()
04392     {
04393         return false;
04394     }
04395 
04401     public function isVariant()
04402     {
04403         return (bool) ( isset( $this->oxarticles__oxparentid ) ? $this->oxarticles__oxparentid->value : false );
04404     }
04405 
04411     public function isMdVariant()
04412     {
04413         $oMdVariant = oxNew( "oxVariantHandler" );
04414 
04415         return $oMdVariant->isMdVariant($this);
04416     }
04417 
04425     public function getSqlForPriceCategories($sFields = '')
04426     {
04427         if (!$sFields) {
04428             $sFields = 'oxid';
04429         }
04430         $sSelectWhere = "select $sFields from ".$this->_getObjectViewName('oxcategories')." where";
04431         $sQuotedPrice = oxDb::getDb()->quote( $this->oxarticles__oxprice->value );
04432         return  "$sSelectWhere oxpricefrom != 0 and oxpriceto != 0 and oxpricefrom <= $sQuotedPrice and oxpriceto >= $sQuotedPrice"
04433                ." union $sSelectWhere oxpricefrom != 0 and oxpriceto = 0 and oxpricefrom <= $sQuotedPrice"
04434                ." union $sSelectWhere oxpricefrom = 0 and oxpriceto != 0 and oxpriceto >= $sQuotedPrice";
04435     }
04436 
04444     public function inPriceCategory( $sCatNid )
04445     {
04446         $oDb = oxDb::getDb();
04447 
04448         $sQuotedPrice = oxDb::getDb()->quote( $this->oxarticles__oxprice->value );
04449         $sQuotedCnid = oxDb::getDb()->quote( $sCatNid );
04450         return (bool) $oDb->getOne(
04451             "select 1 from ".$this->_getObjectViewName('oxcategories')." where oxid=$sQuotedCnid and"
04452            ."(   (oxpricefrom != 0 and oxpriceto != 0 and oxpricefrom <= $sQuotedPrice and oxpriceto >= $sQuotedPrice)"
04453            ." or (oxpricefrom != 0 and oxpriceto = 0 and oxpricefrom <= $sQuotedPrice)"
04454            ." or (oxpricefrom = 0 and oxpriceto != 0 and oxpriceto >= $sQuotedPrice)"
04455            .")"
04456         );
04457     }
04458 
04464     public function getMdVariants()
04465     {
04466         if ( $this->_oMdVariants ) {
04467             return $this->_oMdVariants;
04468         }
04469 
04470         $oParentArticle = $this->getParentArticle();
04471         if ( $oParentArticle ) {
04472             $oVariants = $oParentArticle->getVariants();
04473         } else {
04474             $oVariants = $this->getVariants();
04475         }
04476 
04477         $oVariantHandler = oxNew( "oxVariantHandler" );
04478         $this->_oMdVariants = $oVariantHandler->buildMdVariants( $oVariants, $this->getId() );
04479 
04480         return $this->_oMdVariants;
04481     }
04482 
04488     public function getMdSubvariants()
04489     {
04490         return $this->getMdVariants()->getMdSubvariants();
04491     }
04492 
04493 
04502     protected function _generateImages( $iIndex )
04503     {
04504         if ( isset($this->_aFieldNames["oxpic".$iIndex]) && !$this->_hasGeneratedImage( $iIndex ) ) {
04505             if ( !$this->_isFieldEmpty( "oxarticles__oxpic".$iIndex ) && $this->_hasMasterImage( $iIndex ) ) {
04506                 $oPictureHandler = oxPictureHandler::getInstance();
04507                 $oPictureHandler->generateArticlePictures( $this, $iIndex );
04508             }
04509         }
04510     }
04511 
04520     public function updateAmountOfGeneratedPictures( $iTotalGenerated )
04521     {
04522         $this->oxarticles__oxpicsgenerated = new oxField( $iTotalGenerated );
04523         $oDb = oxDb::getDb();
04524         $sIdQuoted = $oDb->quote($this->getId());
04525 
04526         $sQ = 'update oxarticles set oxpicsgenerated = '.$iTotalGenerated.' where oxid = '.$sIdQuoted;
04527         $oDb->execute( $sQ );
04528     }
04529 
04538     protected function _hasGeneratedImage( $iIndex )
04539     {
04540         if ( $iIndex > (int) $this->oxarticles__oxpicsgenerated->value ) {
04541             return false;
04542         }
04543 
04544         return true;
04545     }
04546 
04554     protected function _hasMasterImage( $iIndex )
04555     {
04556         $sPicName = basename($this->{"oxarticles__oxpic" . $iIndex}->value);
04557 
04558         if ( $sPicName == "nopic.jpg" ) {
04559             return false;
04560         }
04561         if ( $this->isVariant() && $this->getParentArticle()->{"oxarticles__oxpic".$iIndex}->value == $this->{"oxarticles__oxpic".$iIndex}->value ) {
04562             return false;
04563         }
04564 
04565         $sMasterPic = $iIndex . "/" . $sPicName;
04566 
04567         if ( $this->getConfig()->getMasterPicturePath( $sMasterPic ) ) {
04568             return true;
04569         }
04570 
04571         return false;
04572     }
04573 
04582     public function getPictureFieldValue( $sFieldName, $iIndex = null )
04583     {
04584         if ( $sFieldName ) {
04585             $sFieldName = "oxarticles__" . $sFieldName . $iIndex;
04586             return $this->$sFieldName->value;
04587         }
04588     }
04589 }