Subversion Repositories SmartDukaan

Rev

Rev 13688 | Rev 13759 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 13688 Rev 13695
Line 16... Line 16...
16
	public $components = array('Paginator');
16
	public $components = array('Paginator');
17
 
17
 
18
	public function beforeFilter() {
18
	public function beforeFilter() {
19
		parent::beforeFilter();
19
		parent::beforeFilter();
20
		$this->Auth->allow('mine');
20
		$this->Auth->allow('mine');
21
		$callback = $this->request->query('callback');
21
		$callback = $this->request->query('callback');		
22
	}
22
	}
23
 
23
 
24
	public function brand($action='hide') {
24
	public function brand($action='hide') {
25
		$this->response->type('json');
25
		$this->response->type('json');
26
		$this->layout = 'ajax';
26
		$this->layout = 'ajax';
Line 29... Line 29...
29
		$hide = $this->request->query('hide');
29
		$hide = $this->request->query('hide');
30
		if(isset($hide) && !empty($hide) && $hide == 1){
30
		if(isset($hide) && !empty($hide) && $hide == 1){
31
			$this->loadModel('StoreProduct');
31
			$this->loadModel('StoreProduct');
32
			$options = array('conditions'=>array('id'=>$productId),'fields'=>array('category_id','brand'),'recursive'=>-1);
32
			$options = array('conditions'=>array('id'=>$productId),'fields'=>array('category_id','brand'),'recursive'=>-1);
33
			$product = $this->StoreProduct->find('first',$options);
33
			$product = $this->StoreProduct->find('first',$options);
34
			$this->loadModel('HiddenBrand');
34
			$this->loadModel('BrandPreference');
35
			$data = array('user_id' => $userId, 'category_id' => $product['StoreProduct']['category_id'],'brand'=>$product['StoreProduct']['brand']);
35
			$data = array('user_id' => $userId, 'category_id' => $product['StoreProduct']['category_id'],'brand'=>$product['StoreProduct']['brand'],'status'=>'hide');
36
			$this->HiddenBrand->create();
36
			$this->BrandPreference->create();
37
			if($this->HiddenBrand->save($data)){
37
			if($this->BrandPreference->save($data)){
38
				$result = array('success'=>true,'message'=>'brand hidden');
38
				$result = array('success'=>true,'message'=>'brand hidden');
39
			}else{
39
			}else{
40
				$result = array('success'=>false,'message'=>'database issue');
40
				$result = array('success'=>false,'message'=>'database issue');
41
			}
41
			}
42
		}else{
42
		}else{
Line 50... Line 50...
50
		));
50
		));
51
		$this->render('/Elements/json');
51
		$this->render('/Elements/json');
52
	}
52
	}
53
 
53
 
54
	public function mine(){
54
	public function mine(){
-
 
55
		$this->loadModel('User');
-
 
56
		$this->loadModel('Category');
-
 
57
		$this->layout = 'innerpages';
55
		$userId = $this->request->query('user_id');
58
		$userId = $this->request->query('user_id');
56
		if(isset($userId) && !empty($userId)){
59
		if(isset($userId) && !empty($userId)){			
57
			$this->loadModel('User');
-
 
58
			$dbuser = $this->User->findById($userId);
60
			$dbuser = $this->User->findById($userId);
59
			$this->Auth->login($dbuser['User']);
61
			$this->Auth->login($dbuser['User']);
60
		}
62
		}
-
 
63
		//Get all categories
-
 
64
		$this->Category->Behaviors->attach('Containable');
-
 
65
		$options = array('conditions'=>array('parent_id !='=>0),'contain'=>('Brand.name'));
-
 
66
		$categories = $this->Category->find('all',$options);
-
 
67
		$this->User->Behaviors->attach('Containable');
-
 
68
		$options = array('conditions'=>array('User.id'=>$this->request->query('user_id')),'fields'=>array('User.id'),'contain'=>array('PricePreference','BrandPreference'=>array('conditions'=>array('BrandPreference.status'=>'show'))));
-
 
69
		$user = $this->User->find('first',$options);
-
 
70
		$preferredBrands = $preferredPrices = array();
-
 
71
		if(!empty($user['BrandPreference'])){
-
 
72
			foreach ($user['BrandPreference'] as $key => $value) {
-
 
73
				$preferredBrands[$value['category_id']][] = $value['brand'];
-
 
74
			}			
-
 
75
		}
-
 
76
		if(!empty($user['PricePreference'])){
-
 
77
			foreach ($user['PricePreference'] as $key => $value) {
-
 
78
				$preferredPrices[$value['category_id']] = $value;
-
 
79
			}
-
 
80
		}
-
 
81
		$this->set(compact('categories','user','preferredPrices','preferredBrands'));
61
	}
82
	}
62
 
83
 
63
/**
84
/**
64
 * index method
85
 * index method
65
 *
86
 *
Line 88... Line 109...
88
/**
109
/**
89
 * add method
110
 * add method
90
 *
111
 *
91
 * @return void
112
 * @return void
92
 */
113
 */
93
	public function add() {
114
	public function add() {		
-
 
115
		$priceSaved = $brandSaved = false;
-
 
116
		$errors = array();
94
		if ($this->request->is('post')) {
117
		if ($this->request->is('post')) {
-
 
118
			// print_r($this->request->data);die;
-
 
119
			if(!empty($this->request->data['pricerange'])){
-
 
120
				//First delete exisitng price preferences
-
 
121
				$this->loadModel('PricePreference');
-
 
122
				$this->PricePreference->recursive = -1;
-
 
123
				// $conditions = array('user_id'=>$this->request->data['user_id'],'category_id'=>$this->request->data['category_id']);
-
 
124
				// $this->PricePreference->deleteAll($conditions,false);
-
 
125
				$sql = "DELETE FROM price_preferences WHERE user_id = ".$this->request->data['user_id']." AND category_id = ".$this->request->data['category_id'];
-
 
126
				$this->PricePreference->query($sql);
-
 
127
				$pricerange = explode(',', $this->request->data['pricerange']);				
-
 
128
				$data = array('category_id'=>$this->request->data['category_id'],'user_id'=>$this->request->data['user_id'],'min_price'=>$pricerange[0],'max_price'=>$pricerange[1]);
95
			$this->Preference->create();
129
				$this->PricePreference->create();
96
			if ($this->Preference->save($this->request->data)) {
130
				if($this->PricePreference->save($data)){
-
 
131
					$priceSaved = true;
-
 
132
				}else{
-
 
133
					$errors[] = $this->PricePreference->validationErrors;
-
 
134
				}
-
 
135
			}
-
 
136
			if(!empty($this->request->data['brand'])){
-
 
137
				//First delete all shown brands of this category
-
 
138
				$this->loadModel('BrandPreference');
97
				$this->Session->setFlash(__('The preference has been saved.'));
139
				$this->BrandPreference->recursive = -1;
-
 
140
				// $conditions = array('user_id'=>$this->request->data['user_id'],'category_id'=>$this->request->data['category_id'],'Brand.status'=>'show');
-
 
141
				// $this->BrandPreference->deleteAll($conditions,false);
-
 
142
				$sql = "DELETE FROM brand_preferences WHERE user_id = ".$this->request->data['user_id']." AND category_id = ".$this->request->data['category_id']." AND status = 'show'";
-
 
143
				$this->BrandPreference->query($sql);
-
 
144
				$data = array();
98
				return $this->redirect(array('action' => 'index'));
145
				foreach ($this->request->data['brand'] as $key => $brand) {
-
 
146
					$temp = array('user_id' => $this->request->data['user_id'], 'category_id' => $this->request->data['category_id'], 'brand' => $brand, 'status' => 'show');
-
 
147
					$data[] = $temp;
-
 
148
				}				
-
 
149
				$this->BrandPreference->create();
-
 
150
				if($this->BrandPreference->saveAll($data)){
-
 
151
					$brandSaved = true;
99
			} else {
152
				}else{
-
 
153
					$errors[] = $this->BrandPreference->validationErrors;
-
 
154
				}
-
 
155
			}
-
 
156
			$this->response->type('json');
-
 
157
			$this->layout = 'ajax';		
-
 
158
			if($brandSaved || $priceSaved) {
100
				$this->Session->setFlash(__('The preference could not be saved. Please, try again.'));
159
				$result = array('success'=>true,'message'=>'Preferences Saved');
-
 
160
			}else{
-
 
161
				$result = array('success'=>false,'message'=>$errors);
101
			}
162
			}
-
 
163
			$this->set(array(
-
 
164
			    'result' => $result,
-
 
165
			    'callback' => $callback,
-
 
166
			    '_serialize' => array('result')
-
 
167
			));
-
 
168
			$this->render('/Elements/json');
102
		}
169
		}
103
		$users = $this->Preference->User->find('list');
-
 
104
		$this->set(compact('users'));
-
 
105
	}
170
	}
106
 
171
 
107
/**
172
/**
108
 * edit method
173
 * edit method
109
 *
174
 *