mBrands = []; $this->mGoods = []; $this->mDirty = false; if(isset($_SESSION['favorite'])) { if(isset($_SESSION['favorite']['brands'])) { $this->mBrands = $_SESSION['favorite']['brands']; } if(isset($_SESSION['favorite']['goods'])) { $this->mGoods = $_SESSION['favorite']['goods']; } } else { $this->onLogin(); } } public function __destruct() { if($this->mDirty) { $_SESSION['favorite']['brands'] = $this->mBrands; $_SESSION['favorite']['goods'] = $this->mGoods; } } public function onLogin() { $this->mBrands = []; $this->mGoods = []; $this->mDirty = true; $mod_favorites = Model('favorites'); $favorites = $mod_favorites->getGoodsFavoritesList(array('member_id' => $_SESSION['member_id'])); foreach ($favorites as $item) { if($item['fav_type'] == 'goods') { $this->mGoods[] = intval($item['fav_id']); } elseif($item['fav_type'] == 'brand') { $this->mBrands[] = intval($item['fav_id']); } else { continue; } } sort($this->mBrands); sort($this->mGoods); } public function add($type,$id) { $id = intval($id); if($type == 'brand') { if(algorithm::binary_search($this->mBrands,$id) == false) { $pos = algorithm::lower_bonud($this->mBrands,$id); algorithm::array_insert($this->mBrands,$pos,$id); $this->mDirty = true; } } elseif($type == 'goods') { if(algorithm::binary_search($this->mGoods,$id) == false) { $pos = algorithm::lower_bonud($this->mGoods,$id); algorithm::array_insert($this->mGoods,$pos,$id); $this->mDirty = true; } } } public function del($type,$id) { $id = intval($id); if($type == 'brand') { if(algorithm::binary_search($this->mBrands,$id) == true) { $pos = algorithm::lower_bonud($this->mBrands,$id); algorithm::array_erase($this->mBrands,$pos); $this->mDirty = true; } } elseif($type == 'goods') { if(algorithm::binary_search($this->mGoods,$id) == true) { $pos = algorithm::lower_bonud($this->mGoods,$id); algorithm::array_erase($this->mGoods,$pos); $this->mDirty = true; } } } public function favored_goods($goods_id) { if(!session_helper::logined()) return false; $goods_id = intval($goods_id); return algorithm::binary_search($this->mGoods,$goods_id); } public function favored_brand($brand_id) { if(!session_helper::logined()) return false; $brand_id = intval($brand_id); return algorithm::binary_search($this->mBrands,$brand_id); } }