123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- <?php
- /**
- * Created by PhpStorm.
- * User: stanley-king
- * Date: 2017/4/5
- * Time: 下午10:46
- */
- namespace user_session;
- require_once (BASE_ROOT_PATH . '/helper/algorithm.php');
- require_once (BASE_ROOT_PATH . '/helper/session_helper.php');
- use algorithm;
- use session_helper;
- class favorite
- {
- static $stTypes = array('goods','brand');
- private $mBrands;
- private $mGoods;
- private $mDirty;
- public function __construct()
- {
- $this->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);
- }
- }
|