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             $oDb = oxDb::getDb();
01151             $sSLViewName = getViewName( 'oxselectlist' );
01152 
01153             $sQ = "select {$sSLViewName}.* from oxobject2selectlist join {$sSLViewName} on $sSLViewName.oxid=oxobject2selectlist.oxselnid
01154                    where oxobject2selectlist.oxobjectid=%s order by oxobject2selectlist.oxsort";
01155 
01156             // all selectlists this article has
01157             $oLists = oxNew( 'oxlist' );
01158             $oLists->init( 'oxselectlist' );
01159             $oLists->selectString( sprintf( $sQ, $oDb->quote( $this->getId() ) ) );
01160 
01161             //#1104S if this is variant ant it has no selectlists, trying with parent
01162             if ( $oLists->count() == 0 && $this->oxarticles__oxparentid->value ) {
01163                 $oLists->selectString( sprintf( $sQ, $oDb->quote( $this->oxarticles__oxparentid->value ) ) );
01164             }
01165 
01166             $dVat = 0;
01167             if ( $this->getPrice() != null ) {
01168                 $dVat = $this->getPrice()->getVat();
01169             }
01170 
01171             $iCnt = 0;
01172             self::$_aSelList[$sKey] = array();
01173             foreach ( $oLists as $oSelectlist ) {
01174                 self::$_aSelList[$sKey][$iCnt] = $oSelectlist->getFieldList( $dVat );
01175                 self::$_aSelList[$sKey][$iCnt]['name'] = $oSelectlist->oxselectlist__oxtitle->value;
01176                 $iCnt++;
01177             }
01178         }
01179         return self::$_aSelList[$sKey];
01180     }
01181 
01189     protected function _hasAnyVariant( $blForceCoreTable = false )
01190     {
01191         $sArticleTable = $this->getTableNameForActiveSnippet( $blForceCoreTable );
01192         return (bool) oxDb::getDb()->getOne( "select 1 from $sArticleTable where oxparentid='".$this->getId()."'" );
01193     }
01194 
01200     public function hasMdVariants()
01201     {
01202         return $this->_blHasMdVariants;
01203     }
01204 
01213     public function getVariants( $blRemoveNotOrderables = true, $blForceCoreTable = false  )
01214     {
01215         if ( $blRemoveNotOrderables && $this->_aVariants ) {
01216             return $this->_aVariants;
01217         } elseif ( !$blRemoveNotOrderables && $this->_aVariantsWithNotOrderables ) {
01218             return $this->_aVariantsWithNotOrderables;
01219         }
01220 
01221         $myConfig = $this->getConfig();
01222         if ( !$this->_blLoadVariants ||
01223             ( !$this->isAdmin() && !$myConfig->getConfigParam( 'blLoadVariants') ) ||
01224             ( !$this->isAdmin() && !$this->oxarticles__oxvarcount->value ) ) {
01225             return array();
01226         }
01227 
01228         //do not load me as a parent later
01229         self::$_aLoadedParents[$this->getId()] = $this;
01230 
01231         //load simple variants for lists
01232         if ( $this->_isInList() ) {
01233             $oVariants = oxNew( 'oxsimplevariantlist' );
01234             $oVariants->setParent( $this );
01235         } else {
01236             //loading variants
01237             $oVariants = oxNew( 'oxarticlelist' );
01238             $oVariants->getBaseObject()->modifyCacheKey( '_variants' );
01239         }
01240 
01241         if ( $this->_blHasVariants = $this->_hasAnyVariant( $blForceCoreTable ) ) {
01242 
01243             startProfile("selectVariants");
01244             $blUseCoreTable = $blForceCoreTable;
01245             $oBaseObject = $oVariants->getBaseObject();
01246             $sArticleTable = $this->getTableNameForActiveSnippet( $blUseCoreTable );
01247 
01248             $sSelect = "select ".$oBaseObject->getSelectFields()." from $sArticleTable where " .
01249                        $this->getActiveCheckQuery( $blUseCoreTable ) .
01250                        $this->getVariantsQuery( $blRemoveNotOrderables, $blUseCoreTable ) .
01251                        " order by $sArticleTable.oxsort";
01252 
01253             $oVariants->selectString( $sSelect );
01254 
01255             //if this is multidimensional variants, make additional processing
01256             if ( $myConfig->getConfigParam( 'blUseMultidimensionVariants' ) ) {
01257                 $oMdVariants = oxNew( "oxVariantHandler" );
01258                 $this->_blHasMdVariants = $oMdVariants->isMdVariant( $oVariants->current() );
01259             }
01260             stopProfile("selectVariants");
01261         }
01262 
01263         //if we have variants then depending on config option the parent may be non buyable
01264         if ( !$myConfig->getConfigParam( 'blVariantParentBuyable' ) && $this->_blHasVariants ) {
01265             $this->_blNotBuyableParent = true;
01266         }
01267 
01268         //if we have variants, but all variants are incative means article may be non buyable (depends on config option)
01269         if ( !$myConfig->getConfigParam( 'blVariantParentBuyable' ) && $oVariants->count() == 0 && $this->_blHasVariants ) {
01270             $this->_blNotBuyable = true;
01271         }
01272 
01273         // cache
01274         if ( $blRemoveNotOrderables ) {
01275             $this->_aVariants = $oVariants;
01276         } else {
01277             $this->_aVariantsWithNotOrderables = $oVariants;
01278         }
01279 
01280         return $oVariants;
01281     }
01282 
01288     public function getSimpleVariants()
01289     {
01290         if ( $this->oxarticles__oxvarcount->value) {
01291             return $this->getVariants();
01292         }
01293     }
01294 
01303     public function getAdminVariants( $sLanguage = null )
01304     {
01305         $myConfig = $this->getConfig();
01306 
01307         $oVariants = oxNew( 'oxarticlelist');
01308 
01309         if ( is_null($sLanguage) ) {
01310             $oVariants->getBaseObject()->setLanguage(oxLang::getInstance()->getBaseLanguage());
01311         } else {
01312             $oVariants->getBaseObject()->setLanguage($sLanguage);
01313         }
01314 
01315         $sSql = 'select * from oxarticles where oxparentid = "'.$this->getId().'" order by oxsort ';
01316 
01317         $oVariants->selectString( $sSql);
01318 
01319         //if we have variants then depending on config option the parent may be non buyable
01320         if (!$myConfig->getConfigParam( 'blVariantParentBuyable' ) && ($oVariants->count() > 0)) {
01321             //$this->blNotBuyable = true;
01322             $this->_blNotBuyableParent = true;
01323         }
01324 
01325         return $oVariants;
01326     }
01327 
01335     public function getCategory()
01336     {
01337         startPRofile( 'getCategory' );
01338 
01339         $oCategory = oxNew( 'oxcategory' );
01340         $oCategory->setLanguage( $this->getLanguage() );
01341 
01342         // variant handling
01343         $sOXID = $this->getId();
01344         if ( isset( $this->oxarticles__oxparentid->value ) && $this->oxarticles__oxparentid->value ) {
01345             $sOXID = $this->oxarticles__oxparentid->value;
01346         }
01347 
01348         $oStr = getStr();
01349         $sWhere   = $oCategory->getSqlActiveSnippet();
01350         $sSelect  = $this->_generateSearchStr( $sOXID );
01351         $sSelect .= ( $oStr->strstr( $sSelect, 'where' )?' and ':' where ') . $sWhere . " order by oxobject2category.oxtime limit 1";
01352 
01353         // category not found ?
01354         if ( !$oCategory->assignRecord( $sSelect ) ) {
01355 
01356             $sSelect  = $this->_generateSearchStr( $sOXID, true );
01357             $sSelect .= ( $oStr->strstr( $sSelect, 'where' )?' and ':' where ') . $sWhere . " limit 1";
01358 
01359             // looking for price category
01360             if ( !$oCategory->assignRecord( $sSelect ) ) {
01361                 $oCategory = null;
01362             }
01363         }
01364 
01365         stopPRofile( 'getCategory' );
01366         return $oCategory;
01367     }
01368 
01377     public function getCategoryIds( $blActCats = false, $blSkipCache = false )
01378     {
01379         $myConfig = $this->getConfig();
01380         if ( isset( self::$_aArticleCats[$this->getId()] ) && !$blSkipCache ) {
01381             return self::$_aArticleCats[$this->getId()];
01382         }
01383 
01384         // variant handling
01385         $sOXID = $this->getId();
01386         if (isset( $this->oxarticles__oxparentid->value) && $this->oxarticles__oxparentid->value) {
01387             $sOXID = $this->oxarticles__oxparentid->value;
01388         }
01389 
01390         // we do not use lists here as we dont need this overhead right now
01391         $sSql = $this->_getSelectCatIds( $sOXID, $blActCats );
01392         $oDB = oxDb::getDb(true);
01393         $rs = $oDB->execute( $sSql );
01394 
01395 
01396         $aRet = array();
01397 
01398         if ($rs != false && $rs->recordCount() > 0) {
01399             while (!$rs->EOF) {
01400                 $aRet[] = $rs->fields['oxcatnid'];
01401                 $rs->moveNext();
01402             }
01403         }
01404 
01405         // adding price categories if such exists
01406         $sSql = $this->getSqlForPriceCategories();
01407 
01408         $oDB = oxDb::getDb( true );
01409         $rs = $oDB->execute( $sSql );
01410 
01411         if ($rs != false && $rs->recordCount() > 0) {
01412             while (!$rs->EOF) {
01413 
01414                 if ( is_array( $rs->fields ) ) {
01415                    $rs->fields = array_change_key_case( $rs->fields, CASE_LOWER );
01416                 }
01417 
01418 
01419                 if ( !$aRet[$rs->fields['oxid']] ) {
01420                     $aRet[] = $rs->fields['oxid'];
01421                 }
01422                 $rs->moveNext();
01423             }
01424         }
01425 
01426         return self::$_aArticleCats[$this->getId()] = $aRet;
01427     }
01428 
01437     protected function _getSelectCatIds( $sOXID, $blActCats = false )
01438     {
01439         $sO2CView = $this->_getObjectViewName('oxobject2category');
01440         $sCatView = $this->_getObjectViewName('oxcategories');
01441         $sSelect =  "select oxobject2category.oxcatnid as oxcatnid from $sO2CView as oxobject2category left join $sCatView as oxcategories on oxcategories.oxid = oxobject2category.oxcatnid ";
01442         $sSelect .= 'where oxobject2category.oxobjectid='.oxDb::getDb()->quote($sOXID).' and oxcategories.oxid is not null and oxcategories.oxactive'.(($this->getLanguage())?'_'.$this->getLanguage():'').' = 1 ';
01443         if ( $blActCats ) {
01444             $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 ";
01445         }
01446         $sSelect .= 'order by oxobject2category.oxtime ';
01447         return $sSelect;
01448     }
01449 
01459     public function getVendor( $blShopCheck = true )
01460     {
01461         if ( ( $sVendorId = $this->getVendorId() ) ) {
01462             $oVendor = oxNew( 'oxvendor' );
01463         } elseif ( !$blShopCheck && $this->oxarticles__oxvendorid->value ) {
01464             $oVendor = oxNew( 'oxi18n' );
01465             $oVendor->init('oxvendor');
01466             $oVendor->setReadOnly( true );
01467             $sVendorId = $this->oxarticles__oxvendorid->value;
01468         }
01469         if ( $sVendorId && $oVendor->load( $sVendorId ) && $oVendor->oxvendor__oxactive->value ) {
01470             if ( !$this->getConfig()->getConfigParam( 'bl_perfLoadVendorTree' ) ) {
01471                 $oVendor->setReadOnly( true );
01472             }
01473             return $oVendor;
01474         }
01475         return null;
01476     }
01477 
01485     public function getVendorId( $blForceReload = false )
01486     {
01487         $sVendorId = false;
01488         if ( $this->oxarticles__oxvendorid->value ) {
01489             if ( !$blForceReload && isset( self::$_aArticleVendors[$this->getId()] ) ) {
01490                 return self::$_aArticleVendors[$this->getId()];
01491             }
01492             $oDb = oxDb::getDb();
01493             $sVendorIdQuoted = $oDb->quote($this->oxarticles__oxvendorid->value);
01494             $sQ = "select oxid from ".getViewName('oxvendor')." where oxid=$sVendorIdQuoted";
01495             self::$_aArticleVendors[$this->getId()] = $sVendorId = $oDb->getOne( $sQ );
01496         }
01497         return $sVendorId;
01498     }
01499 
01507     public function getManufacturerId( $blForceReload = false )
01508     {
01509         $sManufacturerId = false;
01510         if ( $this->oxarticles__oxmanufacturerid->value ) {
01511             if ( !$blForceReload && isset( self::$_aArticleManufacturers[$this->getId()])) {
01512                 return self::$_aArticleManufacturers[$this->getId()];
01513             }
01514             $oDb = oxDb::getDb();
01515             $sQ = "select oxid from ".getViewName('oxmanufacturers')." where oxid=".$oDb->quote($this->oxarticles__oxmanufacturerid->value);
01516             self::$_aArticleManufacturers[$this->getId()] = $sManufacturerId = $oDb->getOne( $sQ );
01517         }
01518         return $sManufacturerId;
01519     }
01520 
01530     public function getManufacturer( $blShopCheck = true )
01531     {
01532         $oManufacturer = oxNew( 'oxmanufacturer' );;
01533         if ( !( $sManufacturerId = $this->getManufacturerId() ) &&
01534              !$blShopCheck && $this->oxarticles__oxmanufacturerid->value ) {
01535             $oManufacturer->setReadOnly( true );
01536             $sManufacturerId = $this->oxarticles__oxmanufacturerid->value;
01537         }
01538 
01539         if ( $sManufacturerId && $oManufacturer->load( $sManufacturerId ) ) {
01540             if ( !$this->getConfig()->getConfigParam( 'bl_perfLoadManufacturerTree' ) ) {
01541                 $oManufacturer->setReadOnly( true );
01542             }
01543             $oManufacturer = $oManufacturer->oxmanufacturers__oxactive->value ? $oManufacturer : null;
01544         } else {
01545             $oManufacturer = null;
01546         }
01547 
01548         return $oManufacturer;
01549     }
01550 
01558     public function inCategory( $sCatNid)
01559     {
01560         return in_array( $sCatNid, $this->getCategoryIds());
01561     }
01562 
01571     public function isAssignedToCategory( $sCatId )
01572     {
01573         // variant handling
01574         $sOXID = $this->getId();
01575         if ( isset( $this->oxarticles__oxparentid->value) && $this->oxarticles__oxparentid->value) {
01576             $sOXID = $this->oxarticles__oxparentid->value;
01577         }
01578 
01579         $oDB = oxDb::getDb();
01580         $sSelect = $this->_generateSelectCatStr( $sOXID, $sCatId);
01581         $sOXID = $oDB->getOne( $sSelect);
01582         // article is assigned to passed category!
01583         if ( isset( $sOXID) && $sOXID) {
01584             return true;
01585         }
01586 
01587         // maybe this category is price category ?
01588         if ( $this->getConfig()->getConfigParam( 'bl_perfLoadPrice' ) && $this->_blLoadPrice ) {
01589             $dPriceFromTo = $this->getPrice()->getBruttoPrice();
01590             if ( $dPriceFromTo > 0) {
01591                 $sSelect = $this->_generateSelectCatStr( $sOXID, $sCatId, $dPriceFromTo);
01592                 $sOXID = $oDB->getOne( $sSelect);
01593                 // article is assigned to passed category!
01594                 if ( isset( $sOXID) && $sOXID) {
01595                     return true;
01596                 }
01597             }
01598         }
01599         return false;
01600     }
01601 
01607     public function getTPrice()
01608     {
01609         if ( !$this->getConfig()->getConfigParam( 'bl_perfLoadPrice' ) || !$this->_blLoadPrice ) {
01610             return;
01611         }
01612         // return cached result, since oPrice is created ONLY in this function [or function of EQUAL level]
01613         if ( $this->_oTPrice !== null ) {
01614             return $this->_oTPrice;
01615         }
01616 
01617         $this->_oTPrice = oxNew( 'oxPrice' );
01618         $this->_oTPrice->setPrice( $this->oxarticles__oxtprice->value );
01619 
01620         $this->_applyVat( $this->_oTPrice, $this->getArticleVat() );
01621         $this->_applyCurrency( $this->_oTPrice );
01622 
01623         return $this->_oTPrice;
01624     }
01625 
01631     public function skipDiscounts()
01632     {
01633         // allready loaded skip discounts config
01634         if ( $this->_blSkipDiscounts !== null )
01635             return $this->_blSkipDiscounts;
01636 
01637         if ( $this->oxarticles__oxskipdiscounts->value )
01638             return true;
01639 
01640         $sO2CView = getViewName('oxobject2category');
01641         $sSelect =  "select 1 from $sO2CView as oxobject2category left join oxcategories on oxcategories.oxid = oxobject2category.oxcatnid ";
01642         $sSelect .= 'where oxobject2category.oxobjectid="'.$this->getId().'" and oxcategories.oxactive'.(($this->getLanguage())?'_'.$this->getLanguage():'').' = 1 and oxcategories.oxskipdiscounts = "1" ';
01643 
01644         return $this->_blSkipDiscounts = ( oxDb::getDb()->getOne($sSelect) == 1 );
01645     }
01646 
01654     public function setPrice(oxPrice $oPrice)
01655     {
01656         $this->_oPrice = $oPrice;
01657     }
01658 
01667     public function getBasePrice( $dAmount = 1 )
01668     {
01669         // override this function if you want e.g. different prices
01670         // for diff. usergroups.
01671 
01672         // Performance
01673         $myConfig = $this->getConfig();
01674         if( !$myConfig->getConfigParam( 'bl_perfLoadPrice' ) || !$this->_blLoadPrice )
01675             return;
01676 
01677         // GroupPrice or DB price ajusted by AmountPrice
01678         $dPrice = $this->_getAmountPrice( $dAmount );
01679 
01680 
01681         return $dPrice;
01682     }
01683 
01691     public function getPrice( $dAmount = 1 )
01692     {
01693         $myConfig = $this->getConfig();
01694         // Performance
01695         if ( !$myConfig->getConfigParam( 'bl_perfLoadPrice' ) || !$this->_blLoadPrice ) {
01696             return;
01697         }
01698 
01699         // return cached result, since oPrice is created ONLY in this function [or function of EQUAL level]
01700         if ( $dAmount != 1 || $this->_oPrice === null ) {
01701             $oPrice = oxNew( 'oxPrice' );
01702 
01703             // get base
01704             $oPrice->setPrice( $this->getBasePrice( $dAmount ) );
01705 
01706             // price handling
01707             if ( !$this->_blCalcPrice && $dAmount == 1 ) {
01708                 return $this->_oPrice = $oPrice;
01709             }
01710 
01711             $this->_calculatePrice( $oPrice );
01712             if ( $dAmount != 1 ) {
01713                 return $oPrice;
01714             }
01715 
01716             $this->_oPrice = $oPrice;
01717         }
01718         return $this->_oPrice;
01719     }
01720 
01728     protected function _calculatePrice( $oPrice )
01729     {
01730         // apply VAT only if configuration requires it
01731         if ( !$this->getConfig()->getConfigParam( 'bl_perfCalcVatOnlyForBasketOrder' ) ) {
01732             $this->_applyVAT( $oPrice, $this->getArticleVat() );
01733         }
01734 
01735         // apply currency
01736         $this->_applyCurrency( $oPrice );
01737         // apply discounts
01738         if ( !$this->skipDiscounts() ) {
01739             $oDiscountList = oxDiscountList::getInstance();
01740             $oDiscountList->applyDiscounts( $oPrice, $oDiscountList->getArticleDiscounts($this, $this->getArticleUser() ) );
01741         }
01742 
01743         return $oPrice;
01744     }
01745 
01753     public function setArticleUser($oUser)
01754     {
01755         $this->_oUser = $oUser;
01756     }
01757 
01763     public function getArticleUser()
01764     {
01765         if ($this->_oUser) {
01766             return $this->_oUser;
01767         }
01768         return $this->getUser();
01769     }
01770 
01780     public function getBasketPrice( $dAmount, $aSelList, $oBasket )
01781     {
01782         $oUser = $oBasket->getBasketUser();
01783         $this->setArticleUser($oUser);
01784 
01785         $oBasketPrice = oxNew( 'oxPrice' );
01786 
01787         // get base price
01788         $dBasePrice = $this->getBasePrice( $dAmount );
01789 
01790         // applying select list price
01791         $dBasePrice = $this->_modifySelectListPrice( $dBasePrice, $aSelList );
01792 
01793         // setting price
01794         $oBasketPrice->setPrice( $dBasePrice );
01795 
01796         // apply VAT
01797         $this->_applyVat( $oBasketPrice, oxVatSelector::getInstance()->getBasketItemVat( $this, $oBasket ) );
01798 
01799         // apply currency
01800         $this->_applyCurrency( $oBasketPrice );
01801 
01802         // apply discounts
01803         if ( !$this->skipDiscounts() ) {
01804             // apply general discounts
01805             $oDiscountList = oxDiscountList::getInstance();
01806             $oDiscountList->applyDiscounts( $oBasketPrice, $oDiscountList->getArticleDiscounts( $this, $oUser ) );
01807         }
01808 
01809         // returning final price object
01810         return $oBasketPrice;
01811     }
01812 
01825     public function applyBasketDiscounts(oxPrice $oPrice, $aDiscounts, $dAmount = 1)
01826     {
01827         $oDiscountList = oxDiscountList::getInstance();
01828         return $oDiscountList->applyBasketDiscounts( $oPrice, $aDiscounts, $dAmount );
01829     }
01830 
01839     public function delete( $sOXID = null )
01840     {
01841         if ( !$sOXID ) {
01842             $sOXID = $this->getId();
01843         }
01844         if ( !$sOXID ) {
01845             return false;
01846         }
01847 
01848 
01849         $this->load( $sOXID );
01850         $this->_deletePics();
01851         $this->_onChangeResetCounts( $sOXID, $this->oxarticles__oxvendorid->value, $this->oxarticles__oxmanufacturerid->value );
01852         $this->_deleteVariantRecords( $sOXID );
01853         $rs = $this->_deleteRecords( $sOXID );
01854 
01855         oxSeoEncoderArticle::getInstance()->onDeleteArticle($this);
01856 
01857         $this->onChange( ACTION_DELETE, $sOXID, $this->oxarticles__oxparentid->value );
01858 
01859         return $rs->EOF;
01860     }
01861 
01870     public function reduceStock($dAmount, $blAllowNegativeStock = false)
01871     {
01872         $this->beforeUpdate();
01873 
01874         $iStockCount = $this->oxarticles__oxstock->value - $dAmount;
01875         if (!$blAllowNegativeStock && ($iStockCount < 0)) {
01876             $dAmount += $iStockCount;
01877             $iStockCount = 0;
01878         }
01879         $this->oxarticles__oxstock = new oxField($iStockCount);
01880 
01881         $oDb = oxDb::getDb();
01882         $oDb->execute( 'update oxarticles set oxarticles.oxstock = '.$oDb->quote( $iStockCount ).' where oxarticles.oxid = '.$oDb->quote( $this->getId() ) );
01883         $this->onChange( ACTION_UPDATE_STOCK );
01884         return $dAmount;
01885     }
01886 
01895     public function updateSoldAmount( $dAmount = 0 )
01896     {
01897         if ( !$dAmount ) {
01898             return;
01899         }
01900 
01901         $this->beforeUpdate();
01902 
01903         // article is not variant - should be updated current amount
01904         if ( !$this->oxarticles__oxparentid->value ) {
01905             //updating by SQL query, due to wrong behaviour if saving article using not admin mode
01906             $dAmount = (double) $dAmount;
01907             $oDb = oxDb::getDb();
01908             $rs = $oDb->execute( "update oxarticles set oxarticles.oxsoldamount = oxarticles.oxsoldamount + $dAmount where oxarticles.oxid = ".$oDb->quote($this->oxarticles__oxid->value));
01909         } elseif ( $this->oxarticles__oxparentid->value) {
01910             // article is variant - should be updated this article parent amount
01911             $oUpdateArticle = oxNewArticle( $this->oxarticles__oxparentid->value );
01912             $oUpdateArticle->updateSoldAmount( $dAmount );
01913         }
01914 
01915         $this->onChange( ACTION_UPDATE );
01916 
01917         return $rs;
01918     }
01919 
01925     public function disableReminder()
01926     {
01927         $oDB = oxDb::getDb(true);
01928         return $oDB->execute( "update oxarticles set oxarticles.oxremindactive = 2 where oxarticles.oxid = ".$oDB->quote($this->oxarticles__oxid->value));
01929     }
01930 
01937     public function save()
01938     {
01939         $myConfig = $this->getConfig();
01940 
01941         $this->oxarticles__oxthumb = new oxField(basename($this->oxarticles__oxthumb->value), oxField::T_RAW);
01942         $this->oxarticles__oxicon  = new oxField(basename($this->oxarticles__oxicon->value), oxField::T_RAW);
01943         $iPicCount = $myConfig->getConfigParam( 'iPicCount');
01944         for ($i=1; $i <= $iPicCount; $i++) {
01945             if ( isset($this->{'oxarticles__oxpic'.$i}) ) {
01946                 $this->{'oxarticles__oxpic'.$i}->setValue(basename($this->{'oxarticles__oxpic'.$i}->value));
01947             }
01948         }
01949 
01950         $blRet = parent::save();
01951 
01952         // save article long description
01953         $this->setArticleLongDesc();
01954 
01955         // load article images after save
01956         $this->_assignAllPictureValues();
01957 
01958         return $blRet;
01959     }
01960 
01961 
01968     public function getPictureGallery()
01969     {
01970         $myConfig = $this->getConfig();
01971 
01972         //initialize
01973         $blMorePic = false;
01974         $aArtPics  = array();
01975         $aArtIcons = array();
01976         $iActPicId = 1;
01977         $sActPic = $this->getPictureUrl( $iActPicId );
01978 
01979         if ( oxConfig::getParameter( 'actpicid' ) ) {
01980             $iActPicId = oxConfig::getParameter('actpicid');
01981         }
01982 
01983         $oStr = getStr();
01984         $iCntr = 0;
01985         $iPicCount = $myConfig->getConfigParam( 'iPicCount' );
01986         $blCheckActivePicId = true;
01987 
01988         for ( $i = 1; $i <= $iPicCount; $i++) {
01989             $sPicVal = $this->getPictureUrl( $i );
01990             $sIcoVal = $this->getIconUrl( $i );
01991             if ( !$oStr->strstr($sIcoVal, 'nopic_ico.jpg') && !$oStr->strstr($sIcoVal, 'nopic.jpg') ) {
01992                 if ($iCntr) {
01993                     $blMorePic = true;
01994                 }
01995                 $aArtIcons[$i]= $sIcoVal;
01996                 $aArtPics[$i]= $sPicVal;
01997                 $iCntr++;
01998 
01999                 if ($iActPicId == $i) {
02000                     $sActPic = $sPicVal;
02001                     $blCheckActivePicId = false;
02002                 }
02003 
02004             } else if ( $blCheckActivePicId && $iActPicId <= $i) {
02005                 // if picture is empty, setting active pic id to next
02006                 // picture
02007                 $iActPicId++;
02008             }
02009         }
02010 
02011         $blZoomPic  = false;
02012         $aZoomPics = array();
02013         $iZoomPicCount = $myConfig->getConfigParam( 'iPicCount' );
02014 
02015         for ( $j = 1,$c = 1; $j <= $iZoomPicCount; $j++) {
02016             $sVal = $this->getZoomPictureUrl($j);
02017 
02018             if ( !$oStr->strstr($sVal, 'nopic.jpg')) {
02019                 if ($this->getConfig()->getConfigParam('blFormerTplSupport')) {
02020                     $sVal = $this->_sDynImageDir."/".$sVal;
02021                 }
02022                 $blZoomPic = true;
02023                 $aZoomPics[$c]['id'] = $c;
02024                 $aZoomPics[$c]['file'] = $sVal;
02025                 //anything is better than empty name, because <img src=""> calls shop once more = x2 SLOW.
02026                 if (!$sVal) {
02027                     $aZoomPics[$c]['file'] = "nopic.jpg";
02028                 }
02029                 $c++;
02030             }
02031         }
02032 
02033         $aPicGallery = array('ActPicID' => $iActPicId,
02034                              'ActPic' => $sActPic,
02035                              'MorePics' => $blMorePic,
02036                              'Pics' => $aArtPics,
02037                              'Icons' => $aArtIcons,
02038                              'ZoomPic' => $blZoomPic,
02039                              'ZoomPics' => $aZoomPics);
02040 
02041         return $aPicGallery;
02042     }
02043 
02057     public function onChange($sAction = null, $sOXID = null, $sParentID = null)
02058     {
02059         $myConfig = $this->getConfig();
02060 
02061         if (!isset($sOXID)) {
02062             if ( $this->getId()) {
02063                 $sOXID = $this->getId();
02064             }
02065             if (!isset ($sOXID)) {
02066                 $sOXID = $this->oxarticles__oxid->value;
02067             }
02068             if ($this->oxarticles__oxparentid->value) {
02069                 $sParentID = $this->oxarticles__oxparentid->value;
02070             }
02071         }
02072         if (!isset($sOXID)) {
02073             return;
02074         }
02075 
02076         //if (isset($sOXID) && !$myConfig->blVariantParentBuyable && $myConfig->blUseStock)
02077         if ( $myConfig->getConfigParam( 'blUseStock' ) ) {
02078             //if article has variants then updating oxvarstock field
02079             //getting parent id
02080             if (!isset($sParentID)) {
02081                 $oDb = oxDb::getDb();
02082                 $sQ = 'select oxparentid from oxarticles where oxid = '.$oDb->quote($sOXID);
02083                 $sParentID = $oDb->getOne($sQ);
02084             }
02085             //if we have parent id then update stock
02086             if ($sParentID) {
02087                 $this->_onChangeUpdateStock($sParentID);
02088             }
02089         }
02090         //if we have parent id then update count
02091         //update count even if blUseStock is not active
02092         if ($sParentID) {
02093             $this->_onChangeUpdateVarCount($sParentID);
02094         }
02095 
02096         $sId = ( $sParentID ) ? $sParentID : $sOXID;
02097         $this->_onChangeUpdateMinVarPrice( $sId );
02098 
02099             // reseting articles count cache if stock has changed and some
02100             // articles goes offline (M:1448)
02101             if ( $sAction === ACTION_UPDATE_STOCK ) {
02102                 $this->_onChangeStockResetCount( $sOXID );
02103             }
02104 
02105     }
02106 
02113     public function getCustomVAT()
02114     {
02115         if ( isset($this->oxarticles__oxvat->value) ) {
02116             return $this->oxarticles__oxvat->value;
02117         }
02118     }
02119 
02128     public function checkForStock( $dAmount, $dArtStockAmount = 0 )
02129     {
02130         $myConfig = $this->getConfig();
02131         if ( !$myConfig->getConfigParam( 'blUseStock' ) ) {
02132             return true;
02133         }
02134 
02135         // fetching DB info as its up-to-date
02136         $sQ = 'select oxstock, oxstockflag from oxarticles where oxid = "'.$this->getId().'" ';
02137         $rs = oxDb::getDb(true)->Execute( $sQ );
02138 
02139         $iOnStock   = 0;
02140         $iStockFlag = 0;
02141         if ( $rs !== false && $rs->recordCount() > 0 ) {
02142             $iOnStock   = $rs->fields['oxstock'] - $dArtStockAmount;
02143             $iStockFlag = $rs->fields['oxstockflag'];
02144 
02145             // dodger : fremdlager is also always considered as on stock
02146             if ( $iStockFlag == 1 || $iStockFlag == 4) {
02147                 return true;
02148             }
02149             if ( !$myConfig->getConfigParam( 'blAllowUnevenAmounts' ) ) {
02150                 $iOnStock = floor( $iOnStock );
02151             }
02152         }
02153         if ($this->getConfig()->getConfigParam( 'blPsBasketReservationEnabled' )) {
02154             $iOnStock += $this->getSession()->getBasketReservations()->getReservedAmount($this->getId());
02155         }
02156         if ( $iOnStock >= $dAmount ) {
02157             return true;
02158         } else {
02159             if ( $iOnStock > 0 ) {
02160                 return $iOnStock;
02161             } else {
02162                 $oEx = oxNew( 'oxArticleInputException' );
02163                 $oEx->setMessage( 'EXCEPTION_ARTICLE_ARTICELNOTBUYABLE' );
02164                 oxUtilsView::getInstance()->addErrorToDisplay( $oEx );
02165                 return false;
02166             }
02167         }
02168     }
02169 
02170 
02178     public function getArticleLongDesc($sOXID = null)
02179     {
02180 
02181         if ( !$sOXID ) {
02182             $sOXID = $this->oxarticles__oxid->value;
02183         }
02184 
02185         if ($sOXID == $this->oxarticles__oxid->value) {
02186             if (isset($this->oxarticles__oxlongdesc) && ($this->oxarticles__oxlongdesc instanceof oxField) && $this->oxarticles__oxlongdesc->value) {
02187                 return $this->oxarticles__oxlongdesc;
02188             }
02189         }
02190 
02191         $myConfig = $this->getConfig();
02192 
02193         if ( $sOXID ) {
02194             $sLangField = oxLang::getInstance()->getLanguageTag($this->getLanguage());
02195             $oDb = oxDb::getDb();
02196             $this->_setLongDesc($oDb->getOne( "select oxlongdesc{$sLangField} from oxartextends where oxid = ".$oDb->quote($sOXID) ));
02197         } else {
02198             // TODO: check if keeping fldname is needed in non-admin mode
02199             $this->oxarticles__oxlongdesc = new oxField();
02200             $this->oxarticles__oxlongdesc->fldname = 'oxlongdesc';
02201             $this->oxarticles__oxlongdesc->table   = 'oxarticles';
02202             $this->oxarticles__oxlongdesc->fldtype = 'text';
02203         }
02204 
02205         return $this->oxarticles__oxlongdesc;
02206     }
02207 
02215     protected function _setLongDesc($sDbValue)
02216     {
02217         // TODO: the code below is redundant, optimize it, assignments should go smooth without conversions
02218         // hack, if editor screws up text, htmledit tends to do so
02219         $sDbValue = str_replace( '&amp;nbsp;', '&nbsp;', $sDbValue );
02220         $sDbValue = str_replace( '&amp;', '&', $sDbValue );
02221         $sDbValue = str_replace( '&quot;', '"', $sDbValue );
02222         $sDbValue = str_replace( '&lang=', '&amp;lang=', $sDbValue);
02223         //
02224 
02225         $oStr = getStr();
02226         $blHasSmarty = $oStr->strstr( $sDbValue, '[{' );
02227         $blHasPhp = $oStr->strstr( $sDbValue, '<?' );
02228         $myConfig = oxConfig::getInstance();
02229         if ( ( $blHasSmarty || $blHasPhp ) && ($myConfig->getConfigParam( 'blExport' ) || !$this->isAdmin() ) && $myConfig->getConfigParam( 'bl_perfParseLongDescinSmarty' ) ) {
02230             $sDbValue = oxUtilsView::getInstance()->parseThroughSmarty( $sDbValue, $this->getId() );
02231         }
02232         $this->oxarticles__oxlongdesc = new oxField($sDbValue, oxField::T_RAW);
02233         // TODO: check if keeping fldname is needed in non-admin mode
02234         $this->oxarticles__oxlongdesc->fldname = 'oxlongdesc';
02235         $this->oxarticles__oxlongdesc->table   = 'oxarticles';
02236         $this->oxarticles__oxlongdesc->fldtype = 'text';
02237     }
02238 
02244     public function setArticleLongDesc()
02245     {
02246 
02247         if ( $this->_blEmployMultilanguage ) {
02248             // update or insert article long description
02249             if ($this->oxarticles__oxlongdesc instanceof oxField) {
02250                 $sLongDesc = $this->oxarticles__oxlongdesc->getRawValue();
02251             } else {
02252                 $sLongDesc = $this->oxarticles__oxlongdesc->value;
02253             }
02254             $this->_saveArtLongDesc($this->getLanguage(), $sLongDesc);
02255         } else {
02256             $oArtExt = oxNew('oxi18n');
02257             $oArtExt->init('oxartextends');
02258             $aObjFields = $oArtExt->_getAllFields(true);
02259             foreach ($aObjFields as $sKey => $sValue ) {
02260                 if ( preg_match('/^oxlongdesc(_(\d{1,2}))?$/', $sKey) ) {
02261                     $sField = $this->_getFieldLongName($sKey);
02262                     if (isset($this->$sField)) {
02263                         $iLang = $oArtExt->_getFieldLang($sKey);
02264                         $sLongDesc = null;
02265                         if ($this->$sField instanceof oxField) {
02266                             $sLongDesc = $this->$sField->getRawValue();
02267                         } elseif (is_object($this->$sField)) {
02268                             $sLongDesc = $this->$sField->value;
02269                         }
02270                         if (isset($sLongDesc)) {
02271                             $this->_saveArtLongDesc($iLang, $sLongDesc);
02272                         }
02273                     }
02274                 }
02275             }
02276         }
02277     }
02278 
02284     public function getAttributes()
02285     {
02286         if ( $this->_oAttributeList === null ) {
02287             $this->_oAttributeList = oxNew( 'oxattributelist' );
02288             $this->_oAttributeList->loadAttributes( $this->getId() );
02289         }
02290 
02291         return $this->_oAttributeList;
02292     }
02293 
02302     public function appendLink( $sAddParams, $iLang = null )
02303     {
02304         if ( $sAddParams ) {
02305             if ( $iLang === null ) {
02306                 $iLang = $this->getLanguage();
02307             }
02308 
02309             $this->_aSeoAddParams[$iLang]  = isset( $this->_aSeoAddParams[$iLang] ) ? $this->_aSeoAddParams[$iLang] . "&amp;" : "";
02310             $this->_aSeoAddParams[$iLang] .= $sAddParams;
02311         }
02312     }
02313 
02322     public function getBaseSeoLink( $iLang, $blMain = false )
02323     {
02324         $oEncoder = oxSeoEncoderArticle::getInstance();
02325         if ( !$blMain ) {
02326             return $oEncoder->getArticleUrl( $this, $iLang, $this->getLinkType() );
02327         }
02328         return $oEncoder->getArticleMainUrl( $this, $iLang );
02329     }
02330 
02339     public function getLink( $iLang = null, $blMain = false  )
02340     {
02341         if ( !oxUtils::getInstance()->seoIsActive() ) {
02342             return $this->getStdLink( $iLang );
02343         }
02344 
02345         if ( $iLang === null ) {
02346             $iLang = $this->getLanguage();
02347         }
02348 
02349         $iLinkType = $this->getLinkType();
02350         if ( !isset( $this->_aSeoUrls[$iLang][$iLinkType] ) ) {
02351             $this->_aSeoUrls[$iLang][$iLinkType] = $this->getBaseSeoLink( $iLang, $blMain );
02352         }
02353 
02354         $sUrl = $this->_aSeoUrls[$iLang][$iLinkType];
02355         if ( isset($this->_aSeoAddParams[$iLang])) {
02356             $sUrl .= ( ( strpos( $sUrl.$this->_aSeoAddParams[$iLang], '?' ) === false ) ? '?' : '&amp;' ).$this->_aSeoAddParams[$iLang];
02357         }
02358 
02359         return $sUrl;
02360     }
02361 
02370     public function getMainLink( $iLang = null )
02371     {
02372         return $this->getLink( $iLang, true );
02373     }
02374 
02382     public function setLinkType( $iType )
02383     {
02384         // resetting detaisl link, to force new
02385         $this->_sDetailLink = null;
02386 
02387         // setting link type
02388         $this->_iLinkType = (int) $iType;
02389     }
02390 
02396     public function getLinkType()
02397     {
02398         return $this->_iLinkType;
02399     }
02400 
02409     public function appendStdLink( $sAddParams, $iLang = null )
02410     {
02411         if ( $sAddParams ) {
02412             if ( $iLang === null ) {
02413                 $iLang = $this->getLanguage();
02414             }
02415 
02416             $this->_aStdAddParams[$iLang]  = isset( $this->_aStdAddParams[$iLang] ) ? $this->_aStdAddParams[$iLang] . "&amp;" : "";
02417             $this->_aStdAddParams[$iLang] .= $sAddParams;
02418         }
02419     }
02420 
02430     public function getBaseStdLink( $iLang, $blAddId = true, $blFull = true )
02431     {
02432         $sUrl = '';
02433         if ( $blFull ) {
02434             //always returns shop url, not admin
02435             $sUrl = $this->getConfig()->getShopUrl( $iLang, false );
02436         }
02437 
02438         $sUrl .= "index.php?cl=details" . ( $blAddId ? "&amp;anid=".$this->getId() : "" );
02439         return $sUrl . ( isset( $this->_aStdAddParams[$iLang] ) ? "&amp;". $this->_aStdAddParams[$iLang] : "" );
02440     }
02441 
02450     public function getStdLink( $iLang = null, $aParams = array() )
02451     {
02452         if ( $iLang === null ) {
02453             $iLang = $this->getLanguage();
02454         }
02455 
02456         if ( !isset( $this->_aStdUrls[$iLang] ) ) {
02457             $this->_aStdUrls[$iLang] = $this->getBaseStdLink( $iLang );
02458         }
02459 
02460         return oxUtilsUrl::getInstance()->processStdUrl( $this->_aStdUrls[$iLang], $aParams, $iLang, $iLang != $this->getLanguage() );
02461     }
02462 
02470     public function getStdTagLink( $sTag )
02471     {
02472         $sStdTagLink = $this->getConfig()->getShopHomeURL( $this->getLanguage(), false );
02473         return $sStdTagLink . "cl=details&amp;anid=".$this->getId()."&amp;listtype=tag&amp;searchtag=".rawurlencode( $sTag );
02474     }
02475 
02481     public function getTags()
02482     {
02483         $sTagField = "oxtags".oxLang::getInstance()->getLanguageTag($this->getLanguage());
02484         $sQ = "select $sTagField from oxartextends where oxid = '".$this->getId()."'";
02485         $sTags = oxDb::getDb(true)->getOne($sQ);
02486         $oTagCloud = oxNew('oxtagcloud');
02487         $sTags = $oTagCloud->trimTags($sTags);
02488         return $sTags;
02489     }
02490 
02498     public function saveTags($sTags)
02499     {
02500         //do not allow derived update
02501         if ( !$this->allowDerivedUpdate() ) {
02502             return false;
02503         }
02504 
02505 
02506         $oTagCloud = oxNew('oxtagcloud');
02507         $oTagCloud->resetTagCache();
02508         $sTags = $oTagCloud->prepareTags($sTags);
02509 
02510         $sTags = mysql_real_escape_string($sTags);
02511 
02512         $sTagField = "oxtags".oxLang::getInstance()->getLanguageTag($this->getLanguage());
02513         $sQ = "update oxartextends set $sTagField = '$sTags'  where oxid = '".$this->getId()."'";
02514         return oxDb::getDb()->execute($sQ);
02515     }
02516 
02524     public function addTag($sTag)
02525     {
02526         $oDb = oxDb::getDb();
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         $sTag = mysql_real_escape_string($sTag);
02542         $sTailTag = mysql_real_escape_string($sTailTag);
02543 
02544         $sQ = "insert into oxartextends (oxartextends.OXID, $sField) values ('".$this->getId()."', '{$sTag}')
02545                        ON DUPLICATE KEY update $sField = CONCAT(TRIM($sField), '$sTailTag') ";
02546 
02547         return $oDb->Execute($sQ);
02548     }
02549 
02555     public function getMediaUrls()
02556     {
02557         if ( $this->_aMediaUrls === null ) {
02558             $this->_aMediaUrls = oxNew("oxlist");
02559             $this->_aMediaUrls->init("oxmediaurl");
02560             $this->_aMediaUrls->getBaseObject()->setLanguage( $this->getLanguage() );
02561 
02562             $sQ = "select * from oxmediaurls where oxobjectid = '".$this->getId()."'";
02563             $this->_aMediaUrls->selectString($sQ);
02564         }
02565         return $this->_aMediaUrls;
02566     }
02567 
02573     public function getDynImageDir()
02574     {
02575         return $this->_sDynImageDir;
02576     }
02577 
02583     public function getDispSelList()
02584     {
02585         if ($this->_aDispSelList === null) {
02586             if ( $this->getConfig()->getConfigParam( 'bl_perfLoadSelectLists' ) && $this->getConfig()->getConfigParam( 'bl_perfLoadSelectListsInAList' ) ) {
02587                 $this->_aDispSelList = $this->getSelectLists();
02588             }
02589         }
02590         return $this->_aDispSelList;
02591     }
02592 
02598     public function getMoreDetailLink()
02599     {
02600         if ( $this->_sMoreDetailLink == null ) {
02601 
02602             // and assign special article values
02603             $this->_sMoreDetailLink = $this->getConfig()->getShopHomeURL() . 'cl=moredetails';
02604 
02605             // not always it is okey, as not all the time active category is the same as primary article cat.
02606             if ( $sActCat = oxConfig::getParameter( 'cnid' ) ) {
02607                 $this->_sMoreDetailLink .= '&amp;cnid='.$sActCat;
02608             }
02609             $this->_sMoreDetailLink .= '&amp;anid='.$this->getId();
02610             $this->_sMoreDetailLink = $this->_sMoreDetailLink;
02611         }
02612 
02613         return $this->_sMoreDetailLink;
02614     }
02615 
02621     public function getToBasketLink()
02622     {
02623         if ( $this->_sToBasketLink == null ) {
02624             $myConfig = $this->getConfig();
02625 
02626             if ( oxUtils::getInstance()->isSearchEngine() ) {
02627                 $this->_sToBasketLink = $this->getLink();
02628             } else {
02629                 // and assign special article values
02630                 $this->_sToBasketLink = $myConfig->getShopHomeURL();
02631 
02632                 // override some classes as these should never showup
02633                 $sActClass = oxConfig::getParameter( 'cl' );
02634                 if ( $sActClass == 'thankyou') {
02635                     $sActClass = 'basket';
02636                 }
02637                 $this->_sToBasketLink .= 'cl='.$sActClass;
02638 
02639                 // this is not very correct
02640                 if ( $sActCat = oxConfig::getParameter( 'cnid' ) ) {
02641                     $this->_sToBasketLink .= '&amp;cnid='.$sActCat;
02642                 }
02643 
02644                 $this->_sToBasketLink .= '&amp;fnc=tobasket&amp;aid='.$this->getId().'&amp;anid='.$this->getId();
02645 
02646                 if ( $sTpl = basename( oxConfig::getParameter( 'tpl' ) ) ) {
02647                     $this->_sToBasketLink .= '&amp;tpl='.$sTpl;
02648                 }
02649             }
02650         }
02651 
02652         return $this->_sToBasketLink;
02653     }
02654 
02660     public function getStockStatus()
02661     {
02662         return $this->_iStockStatus;
02663     }
02664 
02670     public function getDeliveryDate()
02671     {
02672         if ( $this->oxarticles__oxdelivery->value != '0000-00-00') {
02673             return oxUtilsDate::getInstance()->formatDBDate( $this->oxarticles__oxdelivery->value);
02674         }
02675         return false;
02676     }
02677 
02683     public function getFTPrice()
02684     {
02685         if ( $oPrice = $this->getTPrice() ) {
02686             if ( $oPrice->getBruttoPrice() ) {
02687                 return oxLang::getInstance()->formatCurrency( oxUtils::getInstance()->fRound($oPrice->getBruttoPrice()));
02688             }
02689         }
02690     }
02691 
02697     public function getFPrice()
02698     {
02699         if ( $oPrice = $this->getPrice() ) {
02700             return $this->getPriceFromPrefix().oxLang::getInstance()->formatCurrency( $oPrice->getBruttoPrice() );
02701         }
02702     }
02703 
02709     public function getPricePerUnit()
02710     {
02711         return $this->_fPricePerUnit;
02712     }
02713 
02719     public function isParentNotBuyable()
02720     {
02721         return $this->_blNotBuyableParent;
02722     }
02723 
02729     public function isNotBuyable()
02730     {
02731         return $this->_blNotBuyable;
02732     }
02733 
02741     public function setBuyableState( $blBuyable = false )
02742     {
02743         $this->_blNotBuyable = !$blBuyable;
02744     }
02745 
02754     public function getVariantList()
02755     {
02756         return $this->getVariants();
02757     }
02758 
02766     public function setSelectlist( $aSelList )
02767     {
02768         $this->_aDispSelList = $aSelList;
02769     }
02770 
02778     public function getPictureUrl( $iIndex = '' )
02779     {
02780         if ( $iIndex ) {
02781 
02782             if ( !$this->_hasGeneratedImage( $iIndex ) ) {
02783                 $this->_generateImages( $iIndex );
02784             }
02785 
02786             $sPic = $iIndex . "/" . $this->_getPictureName( $iIndex );
02787 
02788             return $this->getConfig()->getPictureUrl( $sPic, $this->isAdmin() );
02789         }
02790     }
02791 
02799     protected function _getPictureName( $iIndex = '' )
02800     {
02801         if ( !$this->_isFieldEmpty( "oxarticles__oxpic".$iIndex ) ) {
02802             $sPicName = basename($this->{"oxarticles__oxpic".$iIndex}->value);
02803         } else {
02804             $sPicName = "nopic.jpg";
02805         }
02806 
02807         return $sPicName;
02808     }
02809 
02818     public function getIconUrl( $iIndex = '')
02819     {
02820         if ( $this->_isFieldEmpty( "oxarticles__oxicon" ) ) {
02821 
02822             $iIconIndex = ( $iIndex ) ? $iIndex : 1;
02823             //generating new images if needed
02824             if ( !$this->_hasGeneratedImage( $iIconIndex ) ) {
02825                 $this->_generateImages( $iIconIndex );
02826             }
02827         }
02828 
02829         $sIconName = $this->_getIconName( $iIndex );
02830 
02831         if ( !$iIndex ) {
02832             $sPic = "icon/" . basename( $sIconName );
02833         } else {
02834             $sPic = $iIndex . "/" . basename( $sIconName );
02835         }
02836 
02837         return $this->getConfig()->getIconUrl( $sPic, $this->isAdmin() );
02838     }
02839 
02848     protected function _getIconName( $iIndex = '' )
02849     {
02850         $oPictureHandler = oxPictureHandler::getInstance();
02851         $sIconName = "nopic_ico.jpg";
02852 
02853         if ( !$iIndex ) {
02854             if ( !$this->_isFieldEmpty( "oxarticles__oxicon" ) ) {
02855                 $sIconName = basename( $this->oxarticles__oxicon->value );
02856             } elseif ( $this->_hasGeneratedImage( 1 ) && $this->_hasMasterImage( 1 )  && $this->oxarticles__oxpic1->value ) {
02857                 $sIconName = $oPictureHandler->getMainIconName( $this->oxarticles__oxpic1->value );
02858             }
02859         } else {
02860             if ( $this->_hasGeneratedImage( $iIndex ) && $this->{"oxarticles__oxpic".$iIndex}->value ) {
02861                 $sIconName = $oPictureHandler->getIconName( $this->{"oxarticles__oxpic".$iIndex}->value );
02862             }
02863         }
02864 
02865         return $sIconName;
02866     }
02867 
02873     public function getThumbnailUrl()
02874     {
02875         if ( $this->_isFieldEmpty( "oxarticles__oxthumb" ) ) {
02876             //generating new images if needed
02877             if ( !$this->_hasGeneratedImage( 1 ) ) {
02878                 $this->_generateImages( 1 );
02879             }
02880         }
02881 
02882         $sPic = "0/" . $this->_getThumbnailName();
02883 
02884         return $this->getConfig()->getPictureUrl( $sPic, $this->isAdmin() );
02885     }
02886 
02892     protected function _getThumbnailName()
02893     {
02894         if ( !$this->_isFieldEmpty( "oxarticles__oxthumb" ) ) {
02895             $sThumbName = basename($this->oxarticles__oxthumb->value);
02896         } else {
02897             if ( $this->_hasGeneratedImage( 1 ) && $this->_hasMasterImage( 1 ) && $this->oxarticles__oxpic1->value ) {
02898                 $oPictureHandler = oxPictureHandler::getInstance();
02899                 $sThumbName = $oPictureHandler->getThumbName( $this->oxarticles__oxpic1->value );
02900             } else {
02901                 $sThumbName = "nopic.jpg";
02902             }
02903         }
02904 
02905         return $sThumbName;
02906     }
02907 
02915     public function getZoomPictureUrl( $iIndex = '' )
02916     {
02917         $iIndex = (int) $iIndex;
02918         if ( $iIndex > 0) {
02919             //generating new images if needed
02920             if ( $this->_isFieldEmpty( "oxarticles__oxzoom" . $iIndex ) ) {
02921                 //generating new images if needed
02922                 if ( !$this->_hasGeneratedImage( $iIndex ) ) {
02923                     $this->_generateImages( $iIndex );
02924                 }
02925             }
02926 
02927             $sPic = "z{$iIndex}/" . $this->_getZoomPictureName( $iIndex );
02928 
02929             return $this->getConfig()->getPictureUrl( $sPic, $this->isAdmin() );
02930         }
02931     }
02932 
02940     protected function _getZoomPictureName( $iIndex = '' )
02941     {
02942         $sZoomField = "oxarticles__oxzoom" . $iIndex;
02943 
02944         if ( !$this->_isFieldEmpty( $sZoomField ) ) {
02945             $sZoomName = basename( $this->$sZoomField->value );
02946         } else {
02947             if ( $this->_hasGeneratedImage( $iIndex ) && $this->_hasMasterImage( $iIndex ) && $this->{"oxarticles__oxpic".$iIndex}->value ) {
02948                 $oPictureHandler = oxPictureHandler::getInstance();
02949                 $sZoomName = $oPictureHandler->getZoomName( $this->{"oxarticles__oxpic".$iIndex}->value, $iIndex );
02950             } else {
02951                 $sZoomName = "nopic.jpg";
02952             }
02953         }
02954 
02955         return $sZoomName;
02956     }
02957 
02963     public function getFileUrl()
02964     {
02965         return $this->getConfig()->getPictureUrl( '0/' );
02966     }
02967 
02973     public function getPriceFromPrefix()
02974     {
02975         $sPricePrefics = '';
02976         if ( $this->_blIsRangePrice) {
02977             $sPricePrefics = oxLang::getInstance()->translateString('priceFrom').' ';
02978         }
02979 
02980         return $sPricePrefics;
02981     }
02982 
02991     protected function _saveArtLongDesc($iLang, $sValue)
02992     {
02993         $oDB = oxDb::getDb();
02994         $iLang = (int) $iLang;
02995         $sLangField = ($iLang > '0') ? '_'.$iLang : '';
02996         $sLongDesc = $oDB->quote($sValue);
02997         $sLongDescSQL = "insert into oxartextends (oxartextends.OXID, oxartextends.OXLONGDESC{$sLangField})
02998                        VALUES ('".$this->getId()."', {$sLongDesc})
02999                        ON DUPLICATE KEY update oxartextends.OXLONGDESC{$sLangField} = {$sLongDesc} ";
03000 
03001         $oDB->execute($sLongDescSQL);
03002     }
03003 
03009     protected function _skipSaveFields()
03010     {
03011         $myConfig = $this->getConfig();
03012 
03013         $this->_aSkipSaveFields = array();
03014 
03015         $this->_aSkipSaveFields[] = 'oxtimestamp';
03016         $this->_aSkipSaveFields[] = 'oxlongdesc';
03017         $this->_aSkipSaveFields[] = 'oxinsert';
03018 
03019         if ( !isset( $this->oxarticles__oxparentid->value) || $this->oxarticles__oxparentid->value == '') {
03020             $this->_aSkipSaveFields[] = 'oxparentid';
03021         }
03022 
03023     }
03024 
03034     protected function _mergeDiscounts( $aDiscounts, $aItemDiscounts)
03035     {
03036         foreach ( $aItemDiscounts as $sKey => $oDiscount ) {
03037             // add prices of the same discounts
03038             if ( array_key_exists ($sKey, $aDiscounts) ) {
03039                 $aDiscounts[$sKey]->dDiscount += $oDiscount->dDiscount;
03040             } else {
03041                 $aDiscounts[$sKey] = $oDiscount;
03042             }
03043         }
03044         return $aDiscounts;
03045     }
03046 
03052     protected function _getGroupPrice()
03053     {
03054         $dPrice = $this->oxarticles__oxprice->value;
03055 
03056         $oUser = $this->getArticleUser();
03057         if ( $oUser ) {
03058             if ( $oUser->inGroup( 'oxidpricea' ) ) {
03059                 $dPrice = $this->oxarticles__oxpricea->value;
03060             } elseif ( $oUser->inGroup( 'oxidpriceb' ) ) {
03061                 $dPrice = $this->oxarticles__oxpriceb->value;
03062             } elseif ( $oUser->inGroup( 'oxidpricec' ) ) {
03063                 $dPrice = $this->oxarticles__oxpricec->value;
03064             }
03065         }
03066 
03067         // #1437/1436C - added config option, and check for zero A,B,C price values
03068         if ( $this->getConfig()->getConfigParam( 'blOverrideZeroABCPrices' ) && (double) $dPrice == 0 ) {
03069             $dPrice = $this->oxarticles__oxprice->value;
03070         }
03071 
03072         return $dPrice;
03073     }
03074 
03083     protected function _getAmountPrice($dAmount = 1)
03084     {
03085         $myConfig = $this->getConfig();
03086 
03087         startProfile( "_getAmountPrice" );
03088 
03089         $dPrice = $this->_getGroupPrice();
03090         $oAmtPrices = $this->_getAmountPriceList();
03091         foreach ($oAmtPrices as $oAmPrice) {
03092             if ($oAmPrice->oxprice2article__oxamount->value <= $dAmount
03093                     && $dAmount <= $oAmPrice->oxprice2article__oxamountto->value
03094                     && $dPrice > $oAmPrice->oxprice2article__oxaddabs->value ) {
03095                 $dPrice = $oAmPrice->oxprice2article__oxaddabs->value;
03096             }
03097         }
03098 
03099         stopProfile( "_getAmountPrice" );
03100         return $dPrice;
03101     }
03102 
03111     protected function _modifySelectListPrice( $dPrice, $aChosenList = null )
03112     {
03113         $myConfig = $this->getConfig();
03114         // #690
03115         if ( $myConfig->getConfigParam( 'bl_perfLoadSelectLists' ) && $myConfig->getConfigParam( 'bl_perfUseSelectlistPrice' ) ) {
03116 
03117             $aSelLists = $this->getSelectLists();
03118             foreach ( $aSelLists as $key => $aSel) {
03119                 if ( isset( $aChosenList[$key]) && isset($aSel[$aChosenList[$key]] ) ) {
03120                     $oSel = $aSel[$aChosenList[$key]];
03121                     if ( $oSel->priceUnit =='abs' ) {
03122                         $dPrice += $oSel->price;
03123                     } elseif ( $oSel->priceUnit =='%' ) {
03124                         $dPrice += oxPrice::percent( $dPrice, $oSel->price );
03125                     }
03126                 }
03127             }
03128         }
03129         return $dPrice;
03130     }
03131 
03132 
03140     protected function _fillAmountPriceList($oAmPriceList)
03141     {
03142         $myConfig = $this->getConfig();
03143         $myUtils  = oxUtils::getInstance();
03144 
03145         //modifying price
03146         $oCur = $myConfig->getActShopCurrencyObject();
03147 
03148         $oUser = $this->getArticleUser();
03149 
03150         $oDiscountList = oxDiscountList::getInstance();
03151         $aDiscountList = $oDiscountList->getArticleDiscounts( $this, $oUser );
03152 
03153         $oLowestPrice = null;
03154 
03155         $dBasePrice = $this->_getGroupPrice();
03156         $oLang = oxLang::getInstance();
03157 
03158         $dArticleVat = null;
03159         if ( !$myConfig->getConfigParam( 'bl_perfCalcVatOnlyForBasketOrder' ) ) {
03160             $dArticleVat = $this->getArticleVat();
03161         }
03162 
03163         // trying to find lowest price value
03164         foreach ($oAmPriceList as $sId => $oItem) {
03165             $oItemPrice = oxNew( 'oxprice' );
03166             if ( $oItem->oxprice2article__oxaddabs->value) {
03167                 $oItemPrice->setPrice( $oItem->oxprice2article__oxaddabs->value );
03168                 $oDiscountList->applyDiscounts( $oItemPrice, $aDiscountList );
03169                 $this->_applyCurrency( $oItemPrice, $oCur );
03170             } else {
03171                 $oItemPrice->setPrice( $dBasePrice );
03172                 $oItemPrice->subtractPercent( $oItem->oxprice2article__oxaddperc->value );
03173             }
03174 
03175             if (isset($dArticleVat)) {
03176                 $this->_applyVAT($oItemPrice, $dArticleVat);
03177             }
03178 
03179             if (!$oLowestPrice) {
03180                 $oLowestPrice = $oItemPrice;
03181             } elseif ($oLowestPrice->getBruttoPrice() > $oItemPrice->getBruttoPrice()) {
03182                 $oLowestPrice = $oItemPrice;
03183             }
03184 
03185             $oAmPriceList[$sId]->oxprice2article__oxaddabs = new oxField( $oLang->formatCurrency( $myUtils->fRound( $oItemPrice->getBruttoPrice(), $oCur ) ) );
03186             $oAmPriceList[$sId]->fnetprice  = $oLang->formatCurrency( $myUtils->fRound($oItemPrice->getNettoPrice(), $oCur ) );
03187             $oAmPriceList[$sId]->fbrutprice = $oLang->formatCurrency( $myUtils->fRound($oItemPrice->getBruttoPrice(), $oCur ) );
03188         }
03189 
03190         $this->_dAmountPrice = $myUtils->fRound( $oLowestPrice->getBruttoPrice() );
03191         return $oAmPriceList;
03192     }
03193 
03199     protected function _getVariantsIds()
03200     {
03201         $aSelect = array();
03202         $oRs = oxDb::getDb(true)->execute( "select oxid from oxarticles where oxparentid = '".$this->getId()."' " );
03203         if ( $oRs != false && $oRs->recordCount() > 0 ) {
03204             while (!$oRs->EOF) {
03205                 $aSelect[] = $oRs->fields['oxid'];
03206                 $oRs->moveNext();
03207             }
03208         }
03209         return $aSelect;
03210     }
03211 
03217     public function getArticleVat()
03218     {
03219         if (!isset($this->_dArticleVat)) {
03220             $this->_dArticleVat = oxVatSelector::getInstance()->getArticleVat( $this );
03221         }
03222         return $this->_dArticleVat;
03223     }
03224 
03233     protected function _applyVAT( oxPrice $oPrice, $dVat )
03234     {
03235         startProfile(__FUNCTION__);
03236         $oPrice->setVAT( $dVat );
03237         if ( ($dVat = oxVatSelector::getInstance()->getArticleUserVat($this)) !== false ) {
03238             $oPrice->setUserVat( $dVat );
03239         }
03240         stopProfile(__FUNCTION__);
03241     }
03242 
03250     public function applyVats( oxPrice $oPrice )
03251     {
03252         $this->_applyVAT($oPrice, $this->getArticleVat() );
03253     }
03254 
03265     protected function _applyDiscounts( $oPrice, $aDiscounts )
03266     {
03267         $oDiscountList = oxDiscountList::getInstance();
03268         $oDiscountList->applyDiscounts( $oPrice, $aDiscounts );
03269     }
03270 
03278     public function applyDiscountsForVariant( $oPrice )
03279     {
03280         // apply discounts
03281         if ( !$this->skipDiscounts() ) {
03282             $oDiscountList = oxDiscountList::getInstance();
03283             $oDiscountList->applyDiscounts( $oPrice, $oDiscountList->getArticleDiscounts( $this, $this->getArticleUser() ) );
03284         }
03285     }
03286 
03295     protected function _applyCurrency(oxPrice $oPrice, $oCur = null )
03296     {
03297         if ( !$oCur ) {
03298             $oCur = $this->getConfig()->getActShopCurrencyObject();
03299         }
03300 
03301         $oPrice->multiply($oCur->rate);
03302     }
03303 
03304 
03313     protected function _getAttribsString(&$sAttribs, &$iCnt)
03314     {
03315         // we do not use lists here as we dont need this overhead right now
03316         $oDB = oxDb::getDb(true);
03317         $sSelect =  'select oxattrid from oxobject2attribute where oxobject2attribute.oxobjectid="'.$this->getId().'" ';
03318         $sAttribs = '';
03319         $blSep = false;
03320         $rs = $oDB->execute( $sSelect);
03321         $iCnt = 0;
03322         if ($rs != false && $rs->recordCount() > 0) {
03323             while (!$rs->EOF) {
03324                 if ( $blSep) {
03325                     $sAttribs .= ' or ';
03326                 }
03327                 $sAttribs .= 't1.oxattrid = '.$oDB->quote($rs->fields['oxattrid']).' ';
03328                 $blSep = true;
03329                 $iCnt++;
03330                 $rs->moveNext();
03331             }
03332         }
03333     }
03334 
03343     protected function _getSimList($sAttribs, $iCnt)
03344     {
03345         $myConfig = $this->getConfig();
03346         $oDB      = oxDb::getDb(true);
03347 
03348         // #523A
03349         $iAttrPercent = $myConfig->getConfigParam( 'iAttributesPercent' )/100;
03350         // 70% same attributes
03351         if ( !$iAttrPercent || $iAttrPercent < 0 || $iAttrPercent > 1) {
03352             $iAttrPercent = 0.70;
03353         }
03354         // #1137V iAttributesPercent = 100 doesnt work
03355         $iHitMin = ceil( $iCnt * $iAttrPercent );
03356 
03357         // we do not use lists here as we dont need this overhead right now
03358         $aList= array();
03359         $sSelect =  "select oxobjectid, count(*) as cnt from oxobject2attribute as t1 where
03360                     ( $sAttribs )
03361                     and t1.oxobjectid != '".$this->oxarticles__oxid->value."'
03362                     group by t1.oxobjectid having count(*) >= $iHitMin ";
03363 
03364         $rs = $oDB->selectLimit( $sSelect, 20, 0);
03365         if ($rs != false && $rs->recordCount() > 0) {
03366             while (!$rs->EOF) {
03367                 $oTemp = new stdClass();    // #663
03368                 $oTemp->cnt = $rs->fields['cnt'];
03369                 $oTemp->id  = $rs->fields['oxobjectid'];
03370                 $aList[] = $oTemp;
03371                 $rs->moveNext();
03372             }
03373         }
03374         return $aList;
03375     }
03376 
03385     protected function _generateSimListSearchStr($sArticleTable, $aList)
03386     {
03387         $myConfig = $this->getConfig();
03388         $sFieldList = $this->getSelectFields();
03389         $sSearch = "select $sFieldList from $sArticleTable where ".$this->getSqlActiveSnippet()."  and $sArticleTable.oxissearch = 1 and $sArticleTable.oxid in ( ";
03390         $blSep = false;
03391         $iCnt = 0;
03392         $oDb = oxDb::getDb();
03393         foreach ( $aList as $oTemp) {
03394             if ( $blSep) {
03395                 $sSearch .= ',';
03396             }
03397             $sSearch .= $oDb->quote($oTemp->id);
03398             $blSep = true;
03399             if ( $iCnt >= $myConfig->getConfigParam( 'iNrofSimilarArticles' ) ) {
03400                 break;
03401             }
03402             $iCnt++;
03403         }
03404 
03405         //#1741T
03406         //$sSearch .= ") and $sArticleTable.oxparentid = '' ";
03407         $sSearch .= ') ';
03408 
03409         // #524A -- randomizing articles in attribute list
03410         $sSearch .= ' order by rand() ';
03411 
03412         return $sSearch;
03413     }
03414 
03423     protected function _generateSearchStr($sOXID, $blSearchPriceCat = false )
03424     {
03425         $sCatView = getViewName( 'oxcategories' );
03426         $sO2CView = getViewName( 'oxobject2category' );
03427 
03428         // we do not use lists here as we dont need this overhead right now
03429         if ( !$blSearchPriceCat ) {
03430             $sSelect  = "select {$sCatView}.* from {$sO2CView} as oxobject2category left join {$sCatView} on
03431                          {$sCatView}.oxid = oxobject2category.oxcatnid
03432                          where oxobject2category.oxobjectid=".oxDb::getDb()->quote($sOXID)." and {$sCatView}.oxid is not null ";
03433         } else {
03434             $sSelect  = "select {$sCatView}.* from {$sCatView} where
03435                          '{$this->oxarticles__oxprice->value}' >= {$sCatView}.oxpricefrom and
03436                          '{$this->oxarticles__oxprice->value}' <= {$sCatView}.oxpriceto ";
03437         }
03438         return $sSelect;
03439     }
03440 
03446     protected function _generateSearchStrForCustomerBought()
03447     {
03448         $sArtTable = $this->_getObjectViewName( 'oxarticles' );
03449         $sOrderArtTable = getViewName( 'oxorderarticles' );
03450 
03451         // fetching filter params
03452         $sIn = " '{$this->oxarticles__oxid->value}' ";
03453         if ( $this->oxarticles__oxparentid->value ) {
03454 
03455             // adding article parent
03456             $sIn .= ", '{$this->oxarticles__oxparentid->value}' ";
03457             $sParentIdForVariants = $this->oxarticles__oxparentid->value;
03458 
03459         } else {
03460             $sParentIdForVariants = $this->getId();
03461         }
03462 
03463         // adding variants
03464         $oDb = oxDb::getDb(true);
03465         $oRs = $oDb->execute( "select oxid from {$sArtTable} where oxparentid = ".$oDb->quote($sParentIdForVariants)." and oxid != ".$oDb->quote($this->oxarticles__oxid->value)." " );
03466         if ( $oRs != false && $oRs->recordCount() > 0) {
03467             while ( !$oRs->EOF ) {
03468                 $sIn .= ", ".$oDb->quote(current( $oRs->fields ))." ";
03469                 $oRs->moveNext();
03470             }
03471         }
03472 
03473         $iLimit = (int) $this->getConfig()->getConfigParam( 'iNrofCustomerWhoArticles' );
03474         $iLimit = $iLimit?( $iLimit * 10 ): 50;
03475 
03476         // building sql (optimized)
03477         $sQ = "select distinct {$sArtTable}.* from (
03478                    select d.oxorderid as suborderid from {$sOrderArtTable} as d use index ( oxartid ) where d.oxartid in ( {$sIn} ) limit {$iLimit}
03479                ) as suborder
03480                left join {$sOrderArtTable} force index ( oxorderid ) on suborder.suborderid = {$sOrderArtTable}.oxorderid
03481                left join {$sArtTable} on {$sArtTable}.oxid = {$sOrderArtTable}.oxartid
03482                where {$sArtTable}.oxid not in ( {$sIn} )
03483                and ( {$sArtTable}.oxissearch = 1 or {$sArtTable}.oxparentid <> '' ) and ".$this->getSqlActiveSnippet();
03484 
03485         /* non optimized, but could be used if index forcing is not supported
03486         // building sql
03487         $sQ = "select distinct {$sArtTable}.* from {$sOrderArtTable}, {$sArtTable} where {$sOrderArtTable}.oxorderid in (
03488                    select {$sOrderArtTable}.oxorderid from {$sOrderArtTable} where {$sOrderArtTable}.oxartid in ( {$sIn} )
03489                ) and {$sArtTable}.oxid = {$sOrderArtTable}.oxartid and {$sArtTable}.oxid not in ( {$sIn} )
03490                and ( {$sArtTable}.oxissearch = 1 or {$sArtTable}.oxparentid <> '' )
03491                and ".$this->getSqlActiveSnippet();
03492         */
03493 
03494         return $sQ;
03495     }
03496 
03506     protected function _generateSelectCatStr($sOXID, $sCatId, $dPriceFromTo = false)
03507     {
03508         $sCategoryView = getViewName('oxcategories');
03509         $sO2CView = getViewName('oxobject2category');
03510         $sLangPrefix = (($this->getLanguage())?'_'.$this->getLanguage():'');
03511 
03512         $oDb    = oxDb::getDb();
03513         $sOXID  = $oDb->quote($sOXID);
03514         $sCatId = $oDb->quote($sCatId);
03515 
03516         if (!$dPriceFromTo) {
03517             $sSelect  = "select oxobject2category.oxcatnid from $sO2CView as oxobject2category ";
03518             $sSelect .= "left join $sCategoryView as oxcategories on oxcategories.oxid = oxobject2category.oxcatnid ";
03519             $sSelect .= "where oxobject2category.oxcatnid=$sCatId and oxobject2category.oxobjectid=$sOXID ";
03520             $sSelect .= "and oxcategories.oxactive$sLangPrefix = 1 order by oxobject2category.oxtime ";
03521         } else {
03522             $dPriceFromTo = $oDb->quote($dPriceFromTo);
03523             $sSelect  = "select oxcategories.oxid from $sCategoryView as oxcategories where ";
03524             $sSelect .= "oxcategories.oxid=$sCatId and $dPriceFromTo >= oxcategories.oxpricefrom and ";
03525             $sSelect .= "$dPriceFromTo <= oxcategories.oxpriceto ";
03526         }
03527         return $sSelect;
03528     }
03529 
03535     protected function _getAmountPriceList()
03536     {
03537         if ($this->_oAmountPriceList) {
03538             return $this->_oAmountPriceList;
03539         }
03540 
03541         $myConfig = $this->getConfig();
03542 
03543         $sArtID  = $this->getId();
03544 
03545         // #1690C - Scale prices and variants
03546         if ( !$this->isAdmin() && $myConfig->getConfigParam( 'blVariantInheritAmountPrice' ) && $this->oxarticles__oxparentid->value ) {
03547             $sArtID = $this->oxarticles__oxparentid->value;
03548         }
03549 
03550         $sArtID = mysql_real_escape_string($sArtID);
03551 
03552         //collecting assigned to article amount-price list
03553         $oAmPriceList = oxNew( 'oxlist');
03554         $oAmPriceList->init('oxbase', 'oxprice2article');
03555 
03556         $sShopID = $myConfig->getShopID();
03557         if ( $myConfig->getConfigParam( 'blMallInterchangeArticles' ) ) {
03558             $sShopSelect = '1';
03559         } else {
03560             $sShopSelect = " oxshopid =  '$sShopID' ";
03561         }
03562 
03563         $oAmPriceList->selectString( "select * from oxprice2article where oxartid = '$sArtID' and $sShopSelect order by oxamount ");
03564 
03565         // prepare abs prices if currently having percentages
03566         $oBasePrice = $this->_getGroupPrice();
03567         foreach ($oAmPriceList as $oAmPrice) {
03568             if ($oAmPrice->oxprice2article__oxaddperc->value) {
03569                 $oAmPrice->oxprice2article__oxaddabs = new oxField(oxPrice::percent( $oBasePrice, 100 - $oAmPrice->oxprice2article__oxaddperc->value ), oxField::T_RAW);
03570             }
03571         }
03572 
03573         $this->_oAmountPriceList = $oAmPriceList;
03574         return $oAmPriceList;
03575     }
03576 
03584     protected function _isFieldEmpty($sFieldName)
03585     {
03586         $mValue = $this->$sFieldName->value;
03587 
03588         if (is_null($mValue)) {
03589             return true;
03590         }
03591 
03592         if ($mValue === '') {
03593             return true;
03594         }
03595 
03596         $aDoubleCopyFields = array('oxarticles__oxprice',
03597                                        'oxarticles__oxvat');
03598 
03599         if (!$mValue && in_array($sFieldName, $aDoubleCopyFields)) {
03600             return true;
03601         }
03602 
03603 
03604         if (!strcmp($mValue, '0000-00-00 00:00:00') || !strcmp($mValue, '0000-00-00')) {
03605             return true;
03606         }
03607 
03608         $sFieldName = strtolower($sFieldName);
03609 
03610         if ( $sFieldName == 'oxarticles__oxicon' && strpos($mValue, "nopic_ico.jpg") !== false ) {
03611             return true;
03612         }
03613 
03614         if ( strpos($mValue, "nopic.jpg") !== false && ($sFieldName == 'oxarticles__oxthumb' || substr($sFieldName, 0, 17) == 'oxarticles__oxpic' || substr($sFieldName, 0, 18) == 'oxarticles__oxzoom') ) {
03615             return true;
03616         }
03617 
03618         return false;
03619     }
03620 
03628     protected function _assignParentFieldValue($sFieldName)
03629     {
03630         if (!($oParentArticle = $this->getParentArticle())) {
03631             return;
03632         }
03633 
03634         $sCopyFieldName = $this->_getFieldLongName($sFieldName);
03635 
03636         // assigning only theese which parent article has
03637         if ( $oParentArticle->$sCopyFieldName != null ) {
03638 
03639             // only overwrite database values
03640             if ( substr( $sCopyFieldName, 0, 12) != 'oxarticles__') {
03641                 continue;
03642             }
03643 
03644             //do not copy certain fields
03645             if (in_array($sCopyFieldName, $this->_aNonCopyParentFields)) {
03646                 return;
03647             }
03648 
03649             //COPY THE VALUE
03650             // assigning images from parent only if variant has no master image (#1807)
03651             if ( stristr($sCopyFieldName, '_oxthumb') || stristr($sCopyFieldName, '_oxicon') ) {
03652                 if ( $this->_isFieldEmpty( $sCopyFieldName ) && !$this->_hasMasterImage( 1 ) ) {
03653                     $this->$sCopyFieldName = clone $oParentArticle->$sCopyFieldName;
03654                 }
03655             } elseif ( stristr($sCopyFieldName, '_oxzoom') ) {
03656                 // for zoom images checking master image with specified index
03657                 // assign from parent only if no pictures to variant are added
03658                 $iIndex = (int) str_ireplace( "oxarticles__oxzoom", "", $sFieldName );
03659                 if ( $this->_isFieldEmpty( $sCopyFieldName ) && !$this->_hasMasterImage( $iIndex ) && !$this->_hasMasterImage( 1 ) ) {
03660                     $this->$sCopyFieldName = clone $oParentArticle->$sCopyFieldName;
03661                 }
03662             } elseif ( stristr($sCopyFieldName, '_oxpicsgenerated') && $this->{$sCopyFieldName}->value == 0 ) {
03663                 // if no pics generated for variants, load all from
03664                 $this->$sCopyFieldName = clone $oParentArticle->$sCopyFieldName;
03665             } elseif ($this->_isFieldEmpty($sCopyFieldName) || in_array( $sCopyFieldName, $this->_aCopyParentField ) ) {
03666                 $this->$sCopyFieldName = clone $oParentArticle->$sCopyFieldName;
03667             }
03668         }
03669     }
03670 
03676     public function getParentArticle()
03677     {
03678         if ( ( $sParentId = $this->oxarticles__oxparentid->value ) ) {
03679             if ( !isset( self::$_aLoadedParents[$sParentId] ) ) {
03680                 self::$_aLoadedParents[$sParentId] = oxNew( 'oxarticle' );
03681                 self::$_aLoadedParents[$sParentId]->_blSkipAbPrice  = true;
03682                 self::$_aLoadedParents[$sParentId]->_blLoadPrice    = false;
03683                 self::$_aLoadedParents[$sParentId]->_blLoadVariants = false;
03684                 self::$_aLoadedParents[$sParentId]->load( $sParentId );
03685             }
03686             return self::$_aLoadedParents[$sParentId];
03687         }
03688     }
03689 
03697     protected function _getParentAricle()
03698     {
03699         return $this->getParentArticle();
03700     }
03701 
03707     protected function _assignParentFieldValues()
03708     {   startProfile('articleAssignParentInternal');
03709         if ( $this->oxarticles__oxparentid->value) {
03710             // yes, we are in fact a variant
03711             if ( !$this->isAdmin() || ($this->_blLoadParentData && $this->isAdmin() ) ) {
03712                 foreach ($this->_aFieldNames as $sFieldName => $sVal) {
03713                     $this->_assignParentFieldValue($sFieldName);
03714                 }
03715 
03716                 //assing long description
03717                 $oParentArticle = $this->getParentArticle();
03718                 if ( !$this->oxarticles__oxlongdesc->value ) {
03719                     $this->oxarticles__oxlongdesc = $oParentArticle->oxarticles__oxlongdesc;
03720                 }
03721 
03722             }
03723         }
03724         stopProfile('articleAssignParentInternal');
03725 
03726     }
03727 
03733     protected function _assignNotBuyableParent()
03734     {
03735         if ( !$this->getConfig()->getConfigParam( 'blVariantParentBuyable' ) &&
03736              ($this->_blHasVariants || $this->oxarticles__oxvarstock->value || $this->oxarticles__oxvarcount->value )) {
03737             $this->_blNotBuyableParent = true;
03738 
03739         }
03740     }
03741 
03747     protected function _assignAllPictureValues()
03748     {
03749         $myConfig = $this->getConfig();
03750 
03751         $this->_assignPictureValues( "oxarticles__oxicon" );
03752         $this->_assignPictureValues( "oxarticles__oxthumb" );
03753 
03754         $iPicCount = $myConfig->getConfigParam( 'iPicCount' );
03755 
03756         for ( $i=1; $i<= $iPicCount; $i++ ) {
03757             $this->_assignPictureValues( "oxarticles__oxpic".$i );
03758         }
03759 
03760         if ( $iZoomPicCount = $myConfig->getConfigParam( 'iZoomPicCount' ) ) {
03761             for ( $i=1; $i<= $iZoomPicCount; $i++ ) {
03762                 $this->_assignZoomPictureValues( "oxarticles__oxzoom".$i );
03763             }
03764         }
03765     }
03766 
03774     protected function _assignZoomPictureValues( $sName='' )
03775     {
03776         if ( $this->isAdmin() ) {
03777             return;
03778         }
03779 
03780         $sFieldName = substr_replace( $sName, "", 0, 12);
03781 
03782         $aAllFields = $this->_getAllFields( true );
03783 
03784         if ( isset( $aAllFields[$sFieldName] ) ) {
03785             $this->$sName = parent::__get( $sName );
03786             $this->$sName->value;
03787         }
03788 
03789         $this->_assignParentFieldValue( $sName );
03790 
03791         $iIndex = (int) str_ireplace( "oxzoom", "", $sFieldName );
03792 
03793         if ( isset($this->_aFieldNames[$sFieldName]) || isset($this->_aFieldNames["oxpic".$iIndex]) ) {
03794             if ( $iIndex > 0 ) {
03795                 $this->$sName = new oxField( 'z' . $iIndex.'/'.$this->_getZoomPictureName($iIndex) );
03796             }
03797         }
03798     }
03799 
03807     protected function _assignPictureValues( $sName='' )
03808     {
03809         if ( $this->isAdmin() || !$sName ) {
03810             return;
03811         }
03812 
03813         $sFieldName = substr_replace( $sName, "", 0, 12);
03814         $myConfig = $this->getConfig();
03815 
03816         // add directories
03817         if ( $sFieldName == 'oxicon' && isset($this->_aFieldNames["oxicon"]) ) {
03818             $this->oxarticles__oxicon = new oxField('icon/'.$this->_getIconName());
03819             return;
03820         }
03821 
03822         if ( $sFieldName == 'oxthumb' && isset($this->_aFieldNames["oxthumb"]) ) {
03823             $this->oxarticles__oxthumb = new oxField('0/'.$this->_getThumbnailName());
03824             return;
03825         }
03826 
03827         $iPicCount = $myConfig->getConfigParam( 'iPicCount' );
03828         if ( strpos($sFieldName, "oxpic") === 0 && isset($this->_aFieldNames[$sFieldName] ) ) {
03829 
03830             $iIndex = (int) str_ireplace( "oxpic", "", $sFieldName );
03831 
03832             if ( $iIndex > 0 && $iIndex < $iPicCount ) {
03833                 $this->$sName = new oxField( $iIndex . '/'.$this->_getPictureName($iIndex) );
03834                 $this->{$sName.'_ico'} = new oxField( $iIndex . '/'.$this->_getIconName($iIndex) );
03835                 return;
03836             }
03837         }
03838     }
03839 
03845     protected function _assignStock()
03846     {
03847         $myConfig = $this->getConfig();
03848         // -----------------------------------
03849         // stock
03850         // -----------------------------------
03851 
03852         // #1125 A. must round (using floor()) value taken from database and cast to int
03853         if (!$myConfig->getConfigParam( 'blAllowUnevenAmounts' ) && !$this->isAdmin() ) {
03854             $this->oxarticles__oxstock = new oxField((int) floor($this->oxarticles__oxstock->value));
03855         }
03856         //GREEN light
03857         $this->_iStockStatus = 0;
03858 
03859         // if we have flag /*1 or*/ 4 - we show always green light
03860         if ( $myConfig->getConfigParam( 'blUseStock' ) && /*$this->oxarticles__oxstockflag->value != 1 && */ $this->oxarticles__oxstockflag->value != 4) {
03861             //ORANGE light
03862             $iStock = $this->oxarticles__oxstock->value;
03863 
03864             if ($this->_blNotBuyableParent) {
03865                 $iStock = $this->oxarticles__oxvarstock->value;
03866             }
03867 
03868 
03869             if ( $iStock <= $myConfig->getConfigParam( 'sStockWarningLimit' ) && $iStock > 0) {
03870                 $this->_iStockStatus = 1;
03871             }
03872 
03873             //RED light
03874             if ($iStock <= 0) {
03875                 $this->_iStockStatus = -1;
03876             }
03877         }
03878 
03879 
03880         // stock
03881         if ( $myConfig->getConfigParam( 'blUseStock' ) && ($this->oxarticles__oxstockflag->value == 3 || $this->oxarticles__oxstockflag->value == 2)) {
03882             $iOnStock = $this->oxarticles__oxstock->value;
03883             if ($this->getConfig()->getConfigParam( 'blPsBasketReservationEnabled' )) {
03884                 $iOnStock += $this->getSession()->getBasketReservations()->getReservedAmount($this->getId());
03885             }
03886             if ($iOnStock <= 0) {
03887                 $this->_blNotBuyable = true;
03888             }
03889         }
03890 
03891         //exceptional handling for variant parent stock:
03892         if ($this->_blNotBuyable && $this->oxarticles__oxvarstock->value ) {
03893             $this->_blNotBuyable = false;
03894             //but then at least setting notBuaybleParent to true
03895             $this->_blNotBuyableParent = true;
03896         }
03897 
03898         //special treatment for lists when blVariantParentBuyable config option is set to false
03899         //then we just hide "to basket" button.
03900         //if variants are not loaded in the list and this article has variants and parent is not buyable then this article is not buyable
03901         if ( !$myConfig->getConfigParam( 'blVariantParentBuyable' ) && !$myConfig->getConfigParam( 'blLoadVariants' ) && $this->oxarticles__oxvarstock->value) {
03902             $this->_blNotBuyable = true;
03903         }
03904 
03905         //setting to non buyable when variant list is empty (for example not loaded or inactive) and $this is non buyable parent
03906         if ($this->_blNotBuyableParent && count($this->getVariants()) == 0) {
03907             $this->_blNotBuyable = true;
03908         }
03909     }
03910 
03916     protected function _assignPrices()
03917     {
03918         $myConfig = $this->getConfig();
03919 
03920         // Performance
03921         if ( !$myConfig->getConfigParam( 'bl_perfLoadPrice' ) || !$this->_blLoadPrice ) {
03922             return;
03923         }
03924 
03925         // compute price
03926         $dPrice = $this->getPrice()->getBruttoPrice();
03927 
03928         $oCur = $myConfig->getActShopCurrencyObject();
03929         //price per unit handling
03930         if ((double) $this->oxarticles__oxunitquantity->value && $this->oxarticles__oxunitname->value) {
03931             $this->_fPricePerUnit = oxLang::getInstance()->formatCurrency($dPrice / (double) $this->oxarticles__oxunitquantity->value, $oCur);
03932         }
03933 
03934 
03935         //getting min and max prices of variants
03936         $this->_applyRangePrice();
03937     }
03938 
03944     protected function _assignPersistentParam()
03945     {
03946         // Persistent Parameter Handling
03947         $aPersParam     = oxSession::getVar( 'persparam');
03948         if ( isset( $aPersParam) && isset( $aPersParam[$this->getId()])) {
03949             $this->_aPersistParam = $aPersParam[$this->getId()];
03950         }
03951     }
03952 
03958     protected function _assignDynImageDir()
03959     {
03960         $myConfig = $this->getConfig();
03961 
03962         $sThisShop = $this->oxarticles__oxshopid->value;
03963 
03964         $this->_sDynImageDir   = $myConfig->getPictureUrl( null, false );
03965         $this->dabsimagedir    = $myConfig->getPictureDir( false ); //$sThisShop
03966         $this->nossl_dimagedir = $myConfig->getPictureUrl( null, false, false, null, $sThisShop ); //$sThisShop
03967         $this->ssl_dimagedir   = $myConfig->getPictureUrl( null, false, true, null, $sThisShop ); //$sThisShop
03968     }
03969 
03975     protected function _assignComparisonListFlag()
03976     {
03977         // #657 add a flag if article is on comparisonlist
03978 
03979         $aItems = oxConfig::getParameter('aFiltcompproducts');
03980         if ( isset( $aItems[$this->getId()])) {
03981             $this->_blIsOnComparisonList = true;
03982         }
03983     }
03984 
03990     protected function _assignAttributes()
03991     {
03992         //#1029T load attributes
03993         //#1078S removed check for module "Produktvergleich"
03994         if ( $this->getConfig()->getConfigParam( 'bl_perfLoadAttributes' ) ) {
03995             $this->getAttributes();
03996         }
03997     }
03998 
03999 
04007     protected function _insert()
04008     {
04009         // set oxinsert
04010         $iInsertTime = time();
04011         $now = date('Y-m-d H:i:s', $iInsertTime);
04012         $this->oxarticles__oxinsert    = new oxField( $now );
04013         $this->oxarticles__oxtimestamp = new oxField( $now );
04014         if ( !is_object($this->oxarticles__oxsubclass) || $this->oxarticles__oxsubclass->value == '') {
04015             $this->oxarticles__oxsubclass = new oxField('oxarticle');
04016         }
04017 
04018         return parent::_insert();
04019     }
04020 
04026     protected function _update()
04027     {
04028 
04029         $this->_skipSaveFields();
04030 
04031         $myConfig = $this->getConfig();
04032 
04033 
04034         return parent::_update();
04035     }
04036 
04044     protected function _deleteRecords($sOXID)
04045     {
04046         $oDB = oxDb::getDb();
04047 
04048         $sOXID = $oDB->quote($sOXID);
04049 
04050         //delete the record
04051         $sDelete = 'delete from '.$this->_sCoreTbl.' where oxid = '.$sOXID.' ';
04052         $oDB->execute( $sDelete);
04053 
04054         //remove other records
04055         $sDelete = 'delete from oxobject2article where oxarticlenid = '.$sOXID.' or oxobjectid = '.$sOXID.' ';
04056         $oDB->execute( $sDelete);
04057 
04058         $sDelete = 'delete from oxobject2attribute where oxobjectid = '.$sOXID.' ';
04059         $oDB->execute( $sDelete);
04060 
04061         $sDelete = 'delete from oxobject2category where oxobjectid = '.$sOXID.' ';
04062         $oDB->execute( $sDelete);
04063 
04064         $sDelete = 'delete from oxobject2selectlist where oxobjectid = '.$sOXID.' ';
04065         $oDB->execute( $sDelete);
04066 
04067         $sDelete = 'delete from oxprice2article where oxartid = '.$sOXID.' ';
04068         $oDB->execute( $sDelete);
04069 
04070         $sDelete = 'delete from oxreviews where oxtype="oxarticle" and oxobjectid = '.$sOXID.' ';
04071         $oDB->execute( $sDelete);
04072 
04073         $sDelete = 'delete from oxaccessoire2article where oxobjectid = '.$sOXID.' or oxarticlenid = '.$sOXID.' ';
04074         $oDB->execute( $sDelete);
04075 
04076         //#1508C - deleting oxobject2delivery entries added
04077         $sDelete = 'delete from oxobject2delivery where oxobjectid = '.$sOXID.' and oxtype=\'oxarticles\' ';
04078         $oDB->execute( $sDelete);
04079 
04080         $sDelete = 'delete from oxartextends where oxid = '.$sOXID.' ';
04081         $oDB->execute( $sDelete);
04082 
04083         $sDelete = 'delete from oxactions2article where oxartid = '.$sOXID.' ';
04084         $rs = $oDB->execute( $sDelete );
04085 
04086         $sDelete = 'delete from oxobject2list where oxobjectid = '.$sOXID.' ';
04087         $rs = $oDB->execute( $sDelete );
04088 
04089 
04090         return $rs;
04091     }
04092 
04100     protected function _deleteVariantRecords($sOXID)
04101     {
04102         $oDB = oxDb::getDb();
04103         //collect variants to remove recursively
04104         $sVariants = 'select oxid from '.$this->getViewName().' where oxparentid = '.$oDB->quote($sOXID);
04105         $rs = $oDB->execute( $sVariants);
04106         if ($rs != false && $rs->recordCount() > 0) {
04107             while (!$rs->EOF) {
04108                 $this->delete( $rs->fields[0]);
04109                 $rs->moveNext();
04110             }
04111         }
04112     }
04113 
04123     protected function _resetCacheAndArticleCount( $sOxid )
04124     {
04125         $this->_onChangeResetCounts( $sOxid, $this->oxarticles__oxvendorid->value, $this->oxarticles__oxmanufacturerid->value );
04126     }
04127 
04133     protected function _deletePics()
04134     {
04135         $myUtilsPic = oxUtilsPic::getInstance();
04136         $myConfig   = $this->getConfig();
04137         $oPictureHandler = oxPictureHandler::getInstance();
04138 
04139         //deleting custom main icon
04140         $oPictureHandler->deleteMainIcon( $this );
04141 
04142         //deleting custom thumbnail
04143         $oPictureHandler->deleteThumbnail( $this );
04144 
04145         $sAbsDynImageDir = $myConfig->getAbsDynImageDir();
04146 
04147         // deleting master image and all generated images
04148         $iPicCount = $myConfig->getConfigParam( 'iPicCount' );
04149         for ( $i = 1; $i <= $iPicCount; $i++ ) {
04150             $oPictureHandler->deleteArticleMasterPicture( $this, $i );
04151         }
04152     }
04153 
04163     protected function _onChangeResetCounts( $sOxid, $sVendorId = null, $sManufacturerId = null )
04164     {
04165 
04166         $myUtilsCount = oxUtilsCount::getInstance();
04167 
04168         if ( $sVendorId ) {
04169             $myUtilsCount->resetVendorArticleCount( $sVendorId );
04170         }
04171 
04172         if ( $sManufacturerId ) {
04173             $myUtilsCount->resetManufacturerArticleCount( $sManufacturerId );
04174         }
04175 
04176         //also reseting category counts
04177         $oDb = oxDb::getDb();
04178         $sQ = "select oxcatnid from oxobject2category where oxobjectid = ".$oDb->quote($sOxid);
04179         $oRs = $oDb->execute( $sQ );
04180         if ( $oRs !== false && $oRs->recordCount() > 0) {
04181             while ( !$oRs->EOF ) {
04182                 $myUtilsCount->resetCatArticleCount( $oRs->fields[0] );
04183                 $oRs->moveNext();
04184             }
04185         }
04186     }
04187 
04195     protected function _onChangeUpdateStock( $sParentID )
04196     {
04197         $oDb = oxDb::getDb();
04198         $sParentIdQuoted = $oDb->quote($sParentID);
04199         $sQ = 'select oxstock, oxvendorid, oxmanufacturerid from oxarticles where oxid = '.$sParentIdQuoted;
04200         $rs = $oDb->execute($sQ);
04201         $iOldStock = $rs->fields[0];
04202         $iVendorID = $rs->fields[1];
04203         $iManufacturerID = $rs->fields[2];
04204 
04205         $sQ = 'select sum(oxstock) from oxarticles where oxparentid = '.$sParentIdQuoted.' and '. $this->getSqlActiveSnippet( true ).' and oxstock > 0 ';
04206         $iStock = (float) $oDb->getOne( $sQ );
04207 
04208         $sQ = 'update oxarticles set oxvarstock = '.$iStock.' where oxid = '.$sParentIdQuoted;
04209         $oDb->execute( $sQ );
04210 
04211             //now lets update category counts
04212             //first detect stock status change for this article (to or from 0)
04213             if ( $iStock < 0 ) {
04214                 $iStock = 0;
04215             }
04216             if ( $iOldStock < 0 ) {
04217                 $iOldStock = 0;
04218             }
04219             if ( $this->oxarticles__oxstockflag->value == 2 && $iOldStock xor $iStock ) {
04220                 //means the stock status could be changed (oxstock turns from 0 to 1 or from 1 to 0)
04221                 // so far we leave it like this but later we could move all count resets to one or two functions
04222                 $this->_onChangeResetCounts( $sParentID, $iVendorID, $iManufacturerID );
04223             }
04224     }
04225 
04233     protected function _onChangeStockResetCount( $sOxid )
04234     {
04235         $myConfig = $this->getConfig();
04236 
04237         if ( $myConfig->getConfigParam( 'blUseStock' ) && $this->oxarticles__oxstockflag->value == 2 &&
04238            ( $this->oxarticles__oxstock->value + $this->oxarticles__oxvarstock->value ) <= 0 ) {
04239 
04240                $this->_onChangeResetCounts( $sOxid, $this->oxarticles__oxvendorid->value, $this->oxarticles__oxmanufacturerid->value );
04241         }
04242     }
04243 
04251     protected function _onChangeUpdateVarCount( $sParentID )
04252     {
04253         $oDb = oxDb::getDb();
04254         $sParentIdQuoted = $oDb->quote($sParentID);
04255         $sQ = 'select count(*) as varcount from oxarticles where oxparentid = '.$sParentIdQuoted;
04256         $iVarCount = (int) $oDb->getOne($sQ);
04257 
04258         $sQ = 'update oxarticles set oxvarcount = '.$iVarCount.' where oxid = '.$sParentIdQuoted;
04259         $oDb->execute($sQ);
04260     }
04261 
04269     protected function _onChangeUpdateMinVarPrice( $sParentID )
04270     {
04271         $oDb = oxDb::getDb();
04272         $sParentIdQuoted = $oDb->quote($sParentID);
04273         //#M0000883 (Sarunas)
04274         $sQ = 'select min(oxprice) as varminprice from oxarticles where '.$this->getSqlActiveSnippet(true).' and (oxparentid = '.$sParentIdQuoted.')';
04275         $dVarMinPrice = $oDb->getOne($sQ);
04276 
04277         $dParentPrice = $oDb->getOne("select oxprice from oxarticles where oxid = $sParentIdQuoted ");
04278 
04279         $blParentBuyable =  $this->getConfig()->getConfigParam( 'blVariantParentBuyable' );
04280 
04281         if ($dVarMinPrice) {
04282             if ($blParentBuyable) {
04283                 $dVarMinPrice = min($dVarMinPrice, $dParentPrice);
04284             }
04285 
04286         } else {
04287             $dVarMinPrice = $dParentPrice;
04288         }
04289 
04290         if ( $dVarMinPrice ) {
04291             $sQ = 'update oxarticles set oxvarminprice = '.$dVarMinPrice.' where oxid = '.$sParentIdQuoted;
04292             $oDb->execute($sQ);
04293         }
04294     }
04295 
04296 
04302     protected function _applyRangePrice()
04303     {
04304         //#buglist_413 if bl_perfLoadPriceForAddList variant price shouldn't be loaded too
04305         if ( !$this->getConfig()->getConfigParam( 'bl_perfLoadPrice' ) || !$this->_blLoadPrice ) {
04306             return;
04307         }
04308 
04309         $this->_blIsRangePrice = false;
04310 
04311         // if parent is buyable - do not apply range price calcculations
04312         if ($this->_blSkipAbPrice || !$this->_blNotBuyableParent) {
04313             return;
04314         }
04315 
04316         $aPrices = array();
04317 
04318         if (!$this->_blNotBuyableParent) {
04319             $aPrices[] = $this->getPrice()->getBruttoPrice();
04320         }
04321 
04322         $aVariants = $this->getVariants(false);
04323 
04324         if (count($aVariants)) {
04325             foreach ($aVariants as $sKey => $oVariant) {
04326                 $aPrices[] = $oVariant->getPrice()->getBruttoPrice();
04327             }
04328         }
04329 
04330         /*  $oAmPrices = $this->loadAmountPriceInfo();
04331         foreach ($oAmPrices as $oAmPrice) {
04332             $aPrices[] = $oAmPrice->oxprice2article__oxaddabs->value;
04333         }*/
04334 
04335         if (count($aPrices)) {
04336             $dMinPrice = $aPrices[0];
04337             $dMaxPrice = $aPrices[0];
04338             foreach ($aPrices as $dPrice) {
04339                 if ($dMinPrice > $dPrice) {
04340                     $dMinPrice = $dPrice;
04341                 }
04342 
04343                 if ($dMaxPrice < $dPrice) {
04344                     $dMaxPrice = $dPrice;
04345                 }
04346             }
04347         }
04348 
04349         if ($this->_blNotBuyableParent && isset($dMinPrice) && $dMinPrice == $dMaxPrice) {
04350             $this->getPrice()->setBruttoPriceMode();
04351             $this->getPrice()->setPrice($dMinPrice);
04352         }
04353 
04354         if (isset($dMinPrice) && $dMinPrice != $dMaxPrice) {
04355             $this->getPrice()->setBruttoPriceMode();
04356             $this->getPrice()->setPrice($dMinPrice);
04357             $this->_blIsRangePrice = true;
04358         }
04359 
04360         if ( $this->isParentNotBuyable() && !$this->getConfig()->getConfigParam( 'blLoadVariants' )) {
04361             $this->getPrice()->setBruttoPriceMode();
04362             $this->getPrice()->setPrice($this->oxarticles__oxvarminprice->value);
04363             $this->_blIsRangePrice = true;
04364             $this->_calculatePrice( $this->getPrice() );
04365         }
04366     }
04367 
04374     public function getProductId()
04375     {
04376         return $this->getId();
04377     }
04378 
04384     public function getProductParentId()
04385     {
04386         return $this->oxarticles__oxparentid->value;
04387     }
04388 
04394     public function isOrderArticle()
04395     {
04396         return false;
04397     }
04398 
04404     public function isVariant()
04405     {
04406         return (bool) ( isset( $this->oxarticles__oxparentid ) ? $this->oxarticles__oxparentid->value : false );
04407     }
04408 
04414     public function isMdVariant()
04415     {
04416         $oMdVariant = oxNew( "oxVariantHandler" );
04417 
04418         return $oMdVariant->isMdVariant($this);
04419     }
04420 
04428     public function getSqlForPriceCategories($sFields = '')
04429     {
04430         if (!$sFields) {
04431             $sFields = 'oxid';
04432         }
04433         $sSelectWhere = "select $sFields from ".$this->_getObjectViewName('oxcategories')." where";
04434         $sQuotedPrice = oxDb::getDb()->quote( $this->oxarticles__oxprice->value );
04435         return  "$sSelectWhere oxpricefrom != 0 and oxpriceto != 0 and oxpricefrom <= $sQuotedPrice and oxpriceto >= $sQuotedPrice"
04436                ." union $sSelectWhere oxpricefrom != 0 and oxpriceto = 0 and oxpricefrom <= $sQuotedPrice"
04437                ." union $sSelectWhere oxpricefrom = 0 and oxpriceto != 0 and oxpriceto >= $sQuotedPrice";
04438     }
04439 
04447     public function inPriceCategory( $sCatNid )
04448     {
04449         $oDb = oxDb::getDb();
04450 
04451         $sQuotedPrice = oxDb::getDb()->quote( $this->oxarticles__oxprice->value );
04452         $sQuotedCnid = oxDb::getDb()->quote( $sCatNid );
04453         return (bool) $oDb->getOne(
04454             "select 1 from ".$this->_getObjectViewName('oxcategories')." where oxid=$sQuotedCnid and"
04455            ."(   (oxpricefrom != 0 and oxpriceto != 0 and oxpricefrom <= $sQuotedPrice and oxpriceto >= $sQuotedPrice)"
04456            ." or (oxpricefrom != 0 and oxpriceto = 0 and oxpricefrom <= $sQuotedPrice)"
04457            ." or (oxpricefrom = 0 and oxpriceto != 0 and oxpriceto >= $sQuotedPrice)"
04458            .")"
04459         );
04460     }
04461 
04467     public function getMdVariants()
04468     {
04469         if ( $this->_oMdVariants ) {
04470             return $this->_oMdVariants;
04471         }
04472 
04473         $oParentArticle = $this->getParentArticle();
04474         if ( $oParentArticle ) {
04475             $oVariants = $oParentArticle->getVariants();
04476         } else {
04477             $oVariants = $this->getVariants();
04478         }
04479 
04480         $oVariantHandler = oxNew( "oxVariantHandler" );
04481         $this->_oMdVariants = $oVariantHandler->buildMdVariants( $oVariants, $this->getId() );
04482 
04483         return $this->_oMdVariants;
04484     }
04485 
04491     public function getMdSubvariants()
04492     {
04493         return $this->getMdVariants()->getMdSubvariants();
04494     }
04495 
04496 
04505     protected function _generateImages( $iIndex )
04506     {
04507         if ( isset($this->_aFieldNames["oxpic".$iIndex]) && !$this->_hasGeneratedImage( $iIndex ) ) {
04508             if ( !$this->_isFieldEmpty( "oxarticles__oxpic".$iIndex ) && $this->_hasMasterImage( $iIndex ) ) {
04509                 $oPictureHandler = oxPictureHandler::getInstance();
04510                 $oPictureHandler->generateArticlePictures( $this, $iIndex );
04511             }
04512         }
04513     }
04514 
04523     public function updateAmountOfGeneratedPictures( $iTotalGenerated )
04524     {
04525         $this->oxarticles__oxpicsgenerated = new oxField( $iTotalGenerated );
04526         $oDb = oxDb::getDb();
04527         $sIdQuoted = $oDb->quote($this->getId());
04528 
04529         $sQ = 'update oxarticles set oxpicsgenerated = '.$iTotalGenerated.' where oxid = '.$sIdQuoted;
04530         $oDb->execute( $sQ );
04531     }
04532 
04541     protected function _hasGeneratedImage( $iIndex )
04542     {
04543         if ( $iIndex > (int) $this->oxarticles__oxpicsgenerated->value ) {
04544             return false;
04545         }
04546 
04547         return true;
04548     }
04549 
04557     protected function _hasMasterImage( $iIndex )
04558     {
04559         $sPicName = basename($this->{"oxarticles__oxpic" . $iIndex}->value);
04560 
04561         if ( $sPicName == "nopic.jpg" || $sPicName == "" ) {
04562             return false;
04563         }
04564         if ( $this->isVariant() && $this->getParentArticle()->{"oxarticles__oxpic".$iIndex}->value == $this->{"oxarticles__oxpic".$iIndex}->value ) {
04565             return false;
04566         }
04567 
04568         $sMasterPic = $iIndex . "/" . $sPicName;
04569 
04570         if ( $this->getConfig()->getMasterPicturePath( $sMasterPic ) ) {
04571             return true;
04572         }
04573 
04574         return false;
04575     }
04576 
04585     public function getPictureFieldValue( $sFieldName, $iIndex = null )
04586     {
04587         if ( $sFieldName ) {
04588             $sFieldName = "oxarticles__" . $sFieldName . $iIndex;
04589             return $this->$sFieldName->value;
04590         }
04591     }
04592 }