00001 <?php
00002
00011 class oxUserBasket extends oxBase
00012 {
00013
00019 protected $_aSkipSaveFields = array('oxcreate');
00020
00026 protected $_sClassName = 'oxUserbasket';
00027
00033 protected $_aBasketItems = null;
00034
00040 protected $_blNewBasket = false;
00041
00047 public function __construct()
00048 {
00049 parent::__construct();
00050 $this->init( 'oxuserbaskets' );
00051 }
00052
00058 protected function _insert()
00059 {
00060
00061 $this->_blNewBasket = false;
00062
00063 if ( !isset( $this->oxuserbaskets__oxpublic->value ) ) {
00064 $this->oxuserbaskets__oxpublic = new oxField(1, oxField::T_RAW);
00065 }
00066
00067 return parent::_insert();
00068 }
00069
00076 public function setIsNewBasket()
00077 {
00078 $this->_blNewBasket = true;
00079 }
00080
00086 public function getArticles()
00087 {
00088 $aRes = array();
00089 $aItems = $this->getItems();
00090 if ( is_array( $aItems ) ) {
00091 foreach ( $aItems as $sId => $oItem ) {
00092 $oArticle = $oItem->getArticle( $sId );
00093 $aRes[$this->_getItemKey($oArticle->getId(), $oItem->getSelList())] = $oArticle;
00094 }
00095 }
00096 return $aRes;
00097 }
00098
00106 public function getItems( $blReload = false )
00107 {
00108
00109 if ( $this->_aBasketItems !== null && !$blReload ) {
00110 return $this->_aBasketItems;
00111 }
00112
00113
00114 $this->_aBasketItems = array();
00115
00116
00117 $oArticle = oxNew( 'oxarticle' );
00118 $sViewName = $oArticle->getViewName();
00119
00120 $sSelect = "select oxuserbasketitems.* from oxuserbasketitems left join $sViewName on oxuserbasketitems.oxartid = $sViewName.oxid ";
00121 $sSelect .= 'and '.$oArticle->getSqlActiveSnippet().' ';
00122 $sSelect .= "where oxuserbasketitems.oxbasketid = '".$this->getId()."' and $sViewName.oxid is not null ";
00123
00124 $oItems = oxNew( 'oxlist' );
00125 $oItems->init( 'oxuserbasketitem' );
00126 $oItems->selectstring( $sSelect );
00127
00128 foreach ( $oItems as $oItem ) {
00129 $sKey = $this->_getItemKey( $oItem->oxuserbasketitems__oxartid->value, $oItem->getSelList() );
00130 $this->_aBasketItems[$sKey] = $oItem;
00131 }
00132
00133 return $this->_aBasketItems;
00134 }
00135
00145 protected function _createItem( $sProductId, $aSelList = null )
00146 {
00147 $oNewItem = oxNew( 'oxuserbasketitem' );
00148 $oNewItem->oxuserbasketitems__oxartid = new oxField($sProductId, oxField::T_RAW);
00149 $oNewItem->oxuserbasketitems__oxbasketid = new oxField($this->getId(), oxField::T_RAW);
00150
00151 if ( !$aSelList ) {
00152 $oArticle = oxNew( 'oxArticle' );
00153 $oArticle->load( $sProductId );
00154 $aSelectLists = $oArticle->getSelectLists();
00155 if ( ( $iSelCnt = count( $aSelectLists ) ) ) {
00156 $aSelList = array_fill( 0, $iSelCnt, '0' );
00157 }
00158 }
00159
00160 $oNewItem->setSelList( $aSelList );
00161
00162 return $oNewItem;
00163 }
00164
00165
00175 public function getItem( $sProductId, $aSelList)
00176 {
00177
00178 $aItems = $this->getItems();
00179 $sItemKey = $this->_getItemKey( $sProductId, $aSelList );
00180
00181
00182 if ( isset( $aItems[$sProductId] )) {
00183 return $aItems[$sProductId];
00184 } elseif ( isset( $aItems[$sItemKey] ) ) {
00185 return $aItems[$sItemKey];
00186 } else {
00187 return $this->_createItem( $sProductId, $aSelList );
00188 }
00189 }
00190
00199 protected function _getItemKey( $sProductId, $aSel = null )
00200 {
00201 $aSel = ( $aSel != null) ? $aSel : array (0=>'0');
00202 return md5( $sProductId.'|'.serialize( $aSel ) );
00203 }
00204
00212 public function getItemCount( $blReload = false )
00213 {
00214 return count( $this->getItems( $blReload ) );
00215 }
00216
00228 public function addItemToBasket( $sProductId = null, $dAmount = null, $aSel = null, $blOverride = false )
00229 {
00230
00231 if ( $this->_blNewBasket ) {
00232 $this->save();
00233 }
00234
00235 if ( ( $oUserBasketItem = $this->getItem( $sProductId, $aSel ) ) ) {
00236
00237
00238 if ( !$dAmount ) {
00239
00240 $oUserBasketItem->delete();
00241 if ( isset($this->_aBasketItems[$this->_getItemKey($sProductId, $aSel)])) {
00242 unset( $this->_aBasketItems[$this->_getItemKey($sProductId, $aSel)] );
00243 }
00244
00245 } else {
00246 if ( !$blOverride && !empty($oUserBasketItem->oxuserbasketitems__oxamount->value) ) {
00247 $dAmount += $oUserBasketItem->oxuserbasketitems__oxamount->value;
00248 }
00249
00250 $oUserBasketItem->oxuserbasketitems__oxamount = new oxField($dAmount, oxField::T_RAW);
00251 $oUserBasketItem->save();
00252
00253 $this->_aBasketItems[$this->_getItemKey($sProductId, $aSel)] = $oUserBasketItem;
00254 }
00255
00256 return $dAmount;
00257 }
00258 }
00259
00267 public function delete( $sOXID = null )
00268 {
00269 if ( !$sOXID ) {
00270 $sOXID = $this->getId();
00271 }
00272 if ( !$sOXID ) {
00273 return false;
00274 }
00275
00276 if ( ( $blDelete = parent::delete( $sOXID ) ) ) {
00277
00278 $sQ = "delete from oxuserbasketitems where oxbasketid = '$sOXID' ";
00279 oxDb::getDb()->execute( $sQ );
00280 }
00281 return $blDelete;
00282 }
00283 }