Subversion Repositories SmartDukaan

Rev

Rev 14139 | 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');
13695 anikendra 71
		if(isset($userId) && !empty($userId)){			
13688 anikendra 72
			$dbuser = $this->User->findById($userId);
73
			$this->Auth->login($dbuser['User']);
74
		}
13695 anikendra 75
		//Get all categories
76
		$this->Category->Behaviors->attach('Containable');
13759 anikendra 77
		$options = array('conditions'=>array('parent_id !='=>0),'contain'=>(array('Brand.name','Brand.displayed_in_preference_page')));
13695 anikendra 78
		$categories = $this->Category->find('all',$options);
79
		$this->User->Behaviors->attach('Containable');
80
		$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'))));
81
		$user = $this->User->find('first',$options);
82
		$preferredBrands = $preferredPrices = array();
83
		if(!empty($user['BrandPreference'])){
84
			foreach ($user['BrandPreference'] as $key => $value) {
85
				$preferredBrands[$value['category_id']][] = $value['brand'];
86
			}			
87
		}
88
		if(!empty($user['PricePreference'])){
89
			foreach ($user['PricePreference'] as $key => $value) {
90
				$preferredPrices[$value['category_id']] = $value;
91
			}
92
		}
14049 anikendra 93
		$priceranges = Configure::read('priceranges');
94
		$this->set(compact('categories','user','preferredPrices','preferredBrands','priceranges'));
13688 anikendra 95
	}
96
 
13532 anikendra 97
/**
98
 * index method
99
 *
100
 * @return void
101
 */
102
	public function index() {
103
		$this->Preference->recursive = 0;
104
		$this->set('preferences', $this->Paginator->paginate());
105
	}
106
 
107
/**
108
 * view method
109
 *
110
 * @throws NotFoundException
111
 * @param string $id
112
 * @return void
113
 */
114
	public function view($id = null) {
115
		if (!$this->Preference->exists($id)) {
116
			throw new NotFoundException(__('Invalid preference'));
117
		}
118
		$options = array('conditions' => array('Preference.' . $this->Preference->primaryKey => $id));
119
		$this->set('preference', $this->Preference->find('first', $options));
120
	}
121
 
122
/**
123
 * add method
124
 *
125
 * @return void
126
 */
13695 anikendra 127
	public function add() {		
128
		$priceSaved = $brandSaved = false;
129
		$errors = array();
14148 anikendra 130
		$this->log('request '.print_r($this->request->data,1),'preferences');
13532 anikendra 131
		if ($this->request->is('post')) {
13695 anikendra 132
			// print_r($this->request->data);die;
133
			if(!empty($this->request->data['pricerange'])){
134
				//First delete exisitng price preferences
135
				$this->loadModel('PricePreference');
136
				$this->PricePreference->recursive = -1;
137
				// $conditions = array('user_id'=>$this->request->data['user_id'],'category_id'=>$this->request->data['category_id']);
138
				// $this->PricePreference->deleteAll($conditions,false);
139
				$sql = "DELETE FROM price_preferences WHERE user_id = ".$this->request->data['user_id']." AND category_id = ".$this->request->data['category_id'];
140
				$this->PricePreference->query($sql);
141
				$pricerange = explode(',', $this->request->data['pricerange']);				
142
				$data = array('category_id'=>$this->request->data['category_id'],'user_id'=>$this->request->data['user_id'],'min_price'=>$pricerange[0],'max_price'=>$pricerange[1]);
143
				$this->PricePreference->create();
144
				if($this->PricePreference->save($data)){
145
					$priceSaved = true;
146
				}else{
147
					$errors[] = $this->PricePreference->validationErrors;
148
				}
13532 anikendra 149
			}
14051 anikendra 150
			//if(!empty($this->request->data['brand'])){
13695 anikendra 151
				//First delete all shown brands of this category
152
				$this->loadModel('BrandPreference');
153
				$this->BrandPreference->recursive = -1;
154
				// $conditions = array('user_id'=>$this->request->data['user_id'],'category_id'=>$this->request->data['category_id'],'Brand.status'=>'show');
155
				// $this->BrandPreference->deleteAll($conditions,false);
156
				$sql = "DELETE FROM brand_preferences WHERE user_id = ".$this->request->data['user_id']." AND category_id = ".$this->request->data['category_id']." AND status = 'show'";
157
				$this->BrandPreference->query($sql);
158
				$data = array();
14148 anikendra 159
				if(!empty($this->request->data['brand'])){
160
					foreach ($this->request->data['brand'] as $key => $brand) {
161
						$temp = array('user_id' => $this->request->data['user_id'], 'category_id' => $this->request->data['category_id'], 'brand' => $brand, 'status' => 'show');
162
						$data[] = $temp;
163
					}				
164
					$this->BrandPreference->create();
165
					if($this->BrandPreference->saveAll($data)){
166
						$brandSaved = true;
167
					}else{
168
						$errors[] = $this->BrandPreference->validationErrors;
169
					}
13695 anikendra 170
				}				
14051 anikendra 171
			// }
13695 anikendra 172
			$this->response->type('json');
173
			$this->layout = 'ajax';		
174
			if($brandSaved || $priceSaved) {
175
				$result = array('success'=>true,'message'=>'Preferences Saved');
176
			}else{
177
				$result = array('success'=>false,'message'=>$errors);
178
			}
14051 anikendra 179
			$url = $this->apihost.'resetCache/'.$this->request->data['user_id'];
14148 anikendra 180
			$response = $this->make_request($url,null);
181
			$this->log('response '.print_r($response,1),'preferences');
13695 anikendra 182
			$this->set(array(
183
			    'result' => $result,
184
			    'callback' => $callback,
185
			    '_serialize' => array('result')
186
			));
187
			$this->render('/Elements/json');
13532 anikendra 188
		}
189
	}
190
 
191
/**
192
 * edit method
193
 *
194
 * @throws NotFoundException
195
 * @param string $id
196
 * @return void
197
 */
198
	public function edit($id = null) {
199
		if (!$this->Preference->exists($id)) {
200
			throw new NotFoundException(__('Invalid preference'));
201
		}
202
		if ($this->request->is(array('post', 'put'))) {
203
			if ($this->Preference->save($this->request->data)) {
204
				$this->Session->setFlash(__('The preference has been saved.'));
205
				return $this->redirect(array('action' => 'index'));
206
			} else {
207
				$this->Session->setFlash(__('The preference could not be saved. Please, try again.'));
208
			}
209
		} else {
210
			$options = array('conditions' => array('Preference.' . $this->Preference->primaryKey => $id));
211
			$this->request->data = $this->Preference->find('first', $options);
212
		}
213
		$users = $this->Preference->User->find('list');
214
		$this->set(compact('users'));
215
	}
216
 
217
/**
218
 * delete method
219
 *
220
 * @throws NotFoundException
221
 * @param string $id
222
 * @return void
223
 */
224
	public function delete($id = null) {
225
		$this->Preference->id = $id;
226
		if (!$this->Preference->exists()) {
227
			throw new NotFoundException(__('Invalid preference'));
228
		}
229
		$this->request->onlyAllow('post', 'delete');
230
		if ($this->Preference->delete()) {
231
			$this->Session->setFlash(__('The preference has been deleted.'));
232
		} else {
233
			$this->Session->setFlash(__('The preference could not be deleted. Please, try again.'));
234
		}
235
		return $this->redirect(array('action' => 'index'));
236
	}
237
 
238
/**
239
 * admin_index method
240
 *
241
 * @return void
242
 */
243
	public function admin_index() {
244
		$this->Preference->recursive = 0;
245
		$this->set('preferences', $this->Paginator->paginate());
246
	}
247
 
248
/**
249
 * admin_view method
250
 *
251
 * @throws NotFoundException
252
 * @param string $id
253
 * @return void
254
 */
255
	public function admin_view($id = null) {
256
		if (!$this->Preference->exists($id)) {
257
			throw new NotFoundException(__('Invalid preference'));
258
		}
259
		$options = array('conditions' => array('Preference.' . $this->Preference->primaryKey => $id));
260
		$this->set('preference', $this->Preference->find('first', $options));
261
	}
262
 
263
/**
264
 * admin_add method
265
 *
266
 * @return void
267
 */
268
	public function admin_add() {
269
		if ($this->request->is('post')) {
270
			$this->Preference->create();
271
			if ($this->Preference->save($this->request->data)) {
272
				$this->Session->setFlash(__('The preference has been saved.'));
273
				return $this->redirect(array('action' => 'index'));
274
			} else {
275
				$this->Session->setFlash(__('The preference could not be saved. Please, try again.'));
276
			}
277
		}
278
		$users = $this->Preference->User->find('list');
279
		$this->set(compact('users'));
280
	}
281
 
282
/**
283
 * admin_edit method
284
 *
285
 * @throws NotFoundException
286
 * @param string $id
287
 * @return void
288
 */
289
	public function admin_edit($id = null) {
290
		if (!$this->Preference->exists($id)) {
291
			throw new NotFoundException(__('Invalid preference'));
292
		}
293
		if ($this->request->is(array('post', 'put'))) {
294
			if ($this->Preference->save($this->request->data)) {
295
				$this->Session->setFlash(__('The preference has been saved.'));
296
				return $this->redirect(array('action' => 'index'));
297
			} else {
298
				$this->Session->setFlash(__('The preference could not be saved. Please, try again.'));
299
			}
300
		} else {
301
			$options = array('conditions' => array('Preference.' . $this->Preference->primaryKey => $id));
302
			$this->request->data = $this->Preference->find('first', $options);
303
		}
304
		$users = $this->Preference->User->find('list');
305
		$this->set(compact('users'));
306
	}
307
 
308
/**
309
 * admin_delete method
310
 *
311
 * @throws NotFoundException
312
 * @param string $id
313
 * @return void
314
 */
315
	public function admin_delete($id = null) {
316
		$this->Preference->id = $id;
317
		if (!$this->Preference->exists()) {
318
			throw new NotFoundException(__('Invalid preference'));
319
		}
320
		$this->request->onlyAllow('post', 'delete');
321
		if ($this->Preference->delete()) {
322
			$this->Session->setFlash(__('The preference has been deleted.'));
323
		} else {
324
			$this->Session->setFlash(__('The preference could not be deleted. Please, try again.'));
325
		}
326
		return $this->redirect(array('action' => 'index'));
327
	}}