Subversion Repositories SmartDukaan

Rev

Rev 16013 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
13532 anikendra 1
<?php
2
App::uses('AppController', 'Controller');
3
/**
4
 * Preferences Controller
5
 *
6
 * @property Preference $Preference
7
 * @property PaginatorComponent $Paginator
8
 */
9
class PreferencesController extends AppController {
10
 
11
/**
12
 * Components
13
 *
14
 * @var array
15
 */
16
	public $components = array('Paginator');
17
 
13688 anikendra 18
	public function beforeFilter() {
19
		parent::beforeFilter();
20
		$this->Auth->allow('mine');
13695 anikendra 21
		$callback = $this->request->query('callback');		
13688 anikendra 22
	}
23
 
13682 anikendra 24
	public function brand($action='hide') {
25
		$this->response->type('json');
26
		$this->layout = 'ajax';
27
		$userId = $this->request->query('user_id');
28
		$productId = $this->request->query('product_id');
29
		$hide = $this->request->query('hide');
30
		if(isset($hide) && !empty($hide) && $hide == 1){
13926 anikendra 31
			// $this->loadModel('StoreProduct');
32
			// $options = array('conditions'=>array('id'=>$productId),'fields'=>array('category_id','brand'),'recursive'=>-1);
33
			// $product = $this->StoreProduct->find('first',$options);
34
			$cachekey = 'storeproduct-'.$productId;
14139 anikendra 35
			$product = Cache::read($cachekey,'fivemin');
13926 anikendra 36
			if(empty($product)) {
37
				$url = $this->apihost.'masterData/getSkuById/'.$productId;
38
				$productarr = $this->make_request($url,null);
39
				$product = json_decode($productarr[0],1);
14139 anikendra 40
				Cache::write($cachekey,$product,'fivemin');			
13926 anikendra 41
			}	
13695 anikendra 42
			$this->loadModel('BrandPreference');
13926 anikendra 43
			$data = array('user_id' => $userId, 'category_id' => $product['category_id'],'brand'=>$product['brand'],'status'=>'hide');
44
			try{
45
				$this->BrandPreference->create();
46
				if($this->BrandPreference->save($data)){
47
					$result = array('success'=>true,'message'=>'brand hidden');
48
				}else{
49
					$result = array('success'=>false,'message'=>'database issue');
50
				}
51
			}catch(exception $ex){
52
				$result = array('success'=>true,'message'=>$ex->getMessage());
13682 anikendra 53
			}
54
		}else{
55
			$result = array('success'=>false,'message'=>'brand not hidden');
56
		}
57
		// $callback = $this->request->query('callback');
58
		$this->set(array(
59
		    'result' => $result,
60
		    // 'callback' => $callback,
61
		    '_serialize' => array('result')
62
		));
63
		$this->render('/Elements/json');
64
	}
65
 
13688 anikendra 66
	public function mine(){
13695 anikendra 67
		$this->loadModel('User');
68
		$this->loadModel('Category');
69
		$this->layout = 'innerpages';
13688 anikendra 70
		$userId = $this->request->query('user_id');
16013 anikendra 71
		$firsttime = $this->request->query('firsttime');
72
		if(isset($firsttime) && !empty($firsttime)){			
73
			$this->set('firsttime',1);
74
		}
20101 naman 75
// 		if(isset($userId) && !empty($userId)){			
76
// 			$dbuser = $this->User->findById($userId);
77
// 			$this->Auth->login($dbuser['User']);
78
// 		}
13695 anikendra 79
		//Get all categories
80
		$this->Category->Behaviors->attach('Containable');
13759 anikendra 81
		$options = array('conditions'=>array('parent_id !='=>0),'contain'=>(array('Brand.name','Brand.displayed_in_preference_page')));
13695 anikendra 82
		$categories = $this->Category->find('all',$options);
83
		$this->User->Behaviors->attach('Containable');
84
		$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'))));
85
		$user = $this->User->find('first',$options);
86
		$preferredBrands = $preferredPrices = array();
87
		if(!empty($user['BrandPreference'])){
88
			foreach ($user['BrandPreference'] as $key => $value) {
89
				$preferredBrands[$value['category_id']][] = $value['brand'];
90
			}			
91
		}
92
		if(!empty($user['PricePreference'])){
93
			foreach ($user['PricePreference'] as $key => $value) {
94
				$preferredPrices[$value['category_id']] = $value;
95
			}
96
		}
14049 anikendra 97
		$priceranges = Configure::read('priceranges');
16013 anikendra 98
		$this->set(compact('categories','user','preferredPrices','preferredBrands','priceranges','userId'));
13688 anikendra 99
	}
100
 
13532 anikendra 101
/**
102
 * index method
103
 *
104
 * @return void
105
 */
106
	public function index() {
107
		$this->Preference->recursive = 0;
108
		$this->set('preferences', $this->Paginator->paginate());
109
	}
110
 
111
/**
112
 * view method
113
 *
114
 * @throws NotFoundException
115
 * @param string $id
116
 * @return void
117
 */
118
	public function view($id = null) {
119
		if (!$this->Preference->exists($id)) {
120
			throw new NotFoundException(__('Invalid preference'));
121
		}
122
		$options = array('conditions' => array('Preference.' . $this->Preference->primaryKey => $id));
123
		$this->set('preference', $this->Preference->find('first', $options));
124
	}
125
 
126
/**
127
 * add method
128
 *
129
 * @return void
130
 */
13695 anikendra 131
	public function add() {		
132
		$priceSaved = $brandSaved = false;
133
		$errors = array();
14148 anikendra 134
		$this->log('request '.print_r($this->request->data,1),'preferences');
13532 anikendra 135
		if ($this->request->is('post')) {
13695 anikendra 136
			// print_r($this->request->data);die;
137
			if(!empty($this->request->data['pricerange'])){
138
				//First delete exisitng price preferences
139
				$this->loadModel('PricePreference');
140
				$this->PricePreference->recursive = -1;
141
				// $conditions = array('user_id'=>$this->request->data['user_id'],'category_id'=>$this->request->data['category_id']);
142
				// $this->PricePreference->deleteAll($conditions,false);
143
				$sql = "DELETE FROM price_preferences WHERE user_id = ".$this->request->data['user_id']." AND category_id = ".$this->request->data['category_id'];
144
				$this->PricePreference->query($sql);
145
				$pricerange = explode(',', $this->request->data['pricerange']);				
146
				$data = array('category_id'=>$this->request->data['category_id'],'user_id'=>$this->request->data['user_id'],'min_price'=>$pricerange[0],'max_price'=>$pricerange[1]);
147
				$this->PricePreference->create();
148
				if($this->PricePreference->save($data)){
149
					$priceSaved = true;
150
				}else{
151
					$errors[] = $this->PricePreference->validationErrors;
152
				}
13532 anikendra 153
			}
14051 anikendra 154
			//if(!empty($this->request->data['brand'])){
13695 anikendra 155
				//First delete all shown brands of this category
156
				$this->loadModel('BrandPreference');
157
				$this->BrandPreference->recursive = -1;
158
				// $conditions = array('user_id'=>$this->request->data['user_id'],'category_id'=>$this->request->data['category_id'],'Brand.status'=>'show');
159
				// $this->BrandPreference->deleteAll($conditions,false);
160
				$sql = "DELETE FROM brand_preferences WHERE user_id = ".$this->request->data['user_id']." AND category_id = ".$this->request->data['category_id']." AND status = 'show'";
161
				$this->BrandPreference->query($sql);
162
				$data = array();
14148 anikendra 163
				if(!empty($this->request->data['brand'])){
164
					foreach ($this->request->data['brand'] as $key => $brand) {
165
						$temp = array('user_id' => $this->request->data['user_id'], 'category_id' => $this->request->data['category_id'], 'brand' => $brand, 'status' => 'show');
166
						$data[] = $temp;
167
					}				
168
					$this->BrandPreference->create();
169
					if($this->BrandPreference->saveAll($data)){
170
						$brandSaved = true;
171
					}else{
172
						$errors[] = $this->BrandPreference->validationErrors;
173
					}
13695 anikendra 174
				}				
14051 anikendra 175
			// }
13695 anikendra 176
			$this->response->type('json');
177
			$this->layout = 'ajax';		
178
			if($brandSaved || $priceSaved) {
179
				$result = array('success'=>true,'message'=>'Preferences Saved');
180
			}else{
181
				$result = array('success'=>false,'message'=>$errors);
182
			}
14051 anikendra 183
			$url = $this->apihost.'resetCache/'.$this->request->data['user_id'];
14148 anikendra 184
			$response = $this->make_request($url,null);
185
			$this->log('response '.print_r($response,1),'preferences');
13695 anikendra 186
			$this->set(array(
187
			    'result' => $result,
188
			    'callback' => $callback,
189
			    '_serialize' => array('result')
190
			));
191
			$this->render('/Elements/json');
13532 anikendra 192
		}
193
	}
194
 
195
/**
196
 * edit method
197
 *
198
 * @throws NotFoundException
199
 * @param string $id
200
 * @return void
201
 */
202
	public function edit($id = null) {
203
		if (!$this->Preference->exists($id)) {
204
			throw new NotFoundException(__('Invalid preference'));
205
		}
206
		if ($this->request->is(array('post', 'put'))) {
207
			if ($this->Preference->save($this->request->data)) {
208
				$this->Session->setFlash(__('The preference has been saved.'));
209
				return $this->redirect(array('action' => 'index'));
210
			} else {
211
				$this->Session->setFlash(__('The preference could not be saved. Please, try again.'));
212
			}
213
		} else {
214
			$options = array('conditions' => array('Preference.' . $this->Preference->primaryKey => $id));
215
			$this->request->data = $this->Preference->find('first', $options);
216
		}
217
		$users = $this->Preference->User->find('list');
218
		$this->set(compact('users'));
219
	}
220
 
221
/**
222
 * delete method
223
 *
224
 * @throws NotFoundException
225
 * @param string $id
226
 * @return void
227
 */
228
	public function delete($id = null) {
229
		$this->Preference->id = $id;
230
		if (!$this->Preference->exists()) {
231
			throw new NotFoundException(__('Invalid preference'));
232
		}
233
		$this->request->onlyAllow('post', 'delete');
234
		if ($this->Preference->delete()) {
235
			$this->Session->setFlash(__('The preference has been deleted.'));
236
		} else {
237
			$this->Session->setFlash(__('The preference could not be deleted. Please, try again.'));
238
		}
239
		return $this->redirect(array('action' => 'index'));
240
	}
241
 
242
/**
243
 * admin_index method
244
 *
245
 * @return void
246
 */
247
	public function admin_index() {
248
		$this->Preference->recursive = 0;
249
		$this->set('preferences', $this->Paginator->paginate());
250
	}
251
 
252
/**
253
 * admin_view method
254
 *
255
 * @throws NotFoundException
256
 * @param string $id
257
 * @return void
258
 */
259
	public function admin_view($id = null) {
260
		if (!$this->Preference->exists($id)) {
261
			throw new NotFoundException(__('Invalid preference'));
262
		}
263
		$options = array('conditions' => array('Preference.' . $this->Preference->primaryKey => $id));
264
		$this->set('preference', $this->Preference->find('first', $options));
265
	}
266
 
267
/**
268
 * admin_add method
269
 *
270
 * @return void
271
 */
272
	public function admin_add() {
273
		if ($this->request->is('post')) {
274
			$this->Preference->create();
275
			if ($this->Preference->save($this->request->data)) {
276
				$this->Session->setFlash(__('The preference has been saved.'));
277
				return $this->redirect(array('action' => 'index'));
278
			} else {
279
				$this->Session->setFlash(__('The preference could not be saved. Please, try again.'));
280
			}
281
		}
282
		$users = $this->Preference->User->find('list');
283
		$this->set(compact('users'));
284
	}
285
 
286
/**
287
 * admin_edit method
288
 *
289
 * @throws NotFoundException
290
 * @param string $id
291
 * @return void
292
 */
293
	public function admin_edit($id = null) {
294
		if (!$this->Preference->exists($id)) {
295
			throw new NotFoundException(__('Invalid preference'));
296
		}
297
		if ($this->request->is(array('post', 'put'))) {
298
			if ($this->Preference->save($this->request->data)) {
299
				$this->Session->setFlash(__('The preference has been saved.'));
300
				return $this->redirect(array('action' => 'index'));
301
			} else {
302
				$this->Session->setFlash(__('The preference could not be saved. Please, try again.'));
303
			}
304
		} else {
305
			$options = array('conditions' => array('Preference.' . $this->Preference->primaryKey => $id));
306
			$this->request->data = $this->Preference->find('first', $options);
307
		}
308
		$users = $this->Preference->User->find('list');
309
		$this->set(compact('users'));
310
	}
311
 
312
/**
313
 * admin_delete method
314
 *
315
 * @throws NotFoundException
316
 * @param string $id
317
 * @return void
318
 */
319
	public function admin_delete($id = null) {
320
		$this->Preference->id = $id;
321
		if (!$this->Preference->exists()) {
322
			throw new NotFoundException(__('Invalid preference'));
323
		}
324
		$this->request->onlyAllow('post', 'delete');
325
		if ($this->Preference->delete()) {
326
			$this->Session->setFlash(__('The preference has been deleted.'));
327
		} else {
328
			$this->Session->setFlash(__('The preference could not be deleted. Please, try again.'));
329
		}
330
		return $this->redirect(array('action' => 'index'));
331
	}}