Subversion Repositories SmartDukaan

Rev

Rev 14139 | Rev 14344 | 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
 * StoreProducts Controller
5
 *
6
 * @property StoreProduct $StoreProduct
7
 * @property PaginatorComponent $Paginator
8
 */
9
class StoreProductsController extends AppController {
10
 
11
/**
12
 * Components
13
 *
14
 * @var array
15
 */
16
	public $components = array('Paginator');
13808 anikendra 17
	public $apihost;	
13532 anikendra 18
 
19
	public function beforeFilter() {
20
		parent::beforeFilter();
13901 anikendra 21
		$this->Auth->allow('bycategory','category','mine','getdeals','getliveprice','search');
13808 anikendra 22
		$callback = $this->request->query('callback');		
13532 anikendra 23
	}
13570 anikendra 24
 
13808 anikendra 25
	public function search() {
14215 anikendra 26
		$userId = $this->request->query('user_id');
27
		if(isset($userId) && !empty($userId)){
28
			$this->loadModel('User');
29
			$dbuser = $this->User->findById($userId);
30
			$this->Auth->login($dbuser['User']);
31
		}
13815 anikendra 32
		$this->layout = 'innerpages';
13808 anikendra 33
		$q = $this->request->query('q');
13901 anikendra 34
		$page = $this->request->query('page');
35
		if(!isset($page)){
36
			$page = 1;
37
		}
38
		$dealsperpage = Configure::read('dealsperpage');
39
		$offset = ($page - 1)*$dealsperpage;
13808 anikendra 40
		if(isset($q) && !empty($q)){
13815 anikendra 41
			if (Configure::read('log_solr_queries') === true) {
42
				$this->loadModel('SearchTerm');
43
				$data = array('user_id' => $this->Auth->User('id'),'search_term' => $q);
44
				$this->SearchTerm->create();
45
				$this->SearchTerm->save($data);					
46
			}
13901 anikendra 47
			$result = $this->get_solr_result($q,$page);
48
			$this->set(compact('result','q','page'));
49
		}
50
	}
13815 anikendra 51
 
13901 anikendra 52
	public function getsearchresults() {
53
		$this->layout = 'ajax';
54
		$q = $this->request->query('q');
55
		$page = $this->request->query('page');
56
		if(!isset($page)){
57
			$page = 1;
58
		}		
59
		if(isset($q) && !empty($q)){			
60
			$result = $this->get_solr_result($q,$page);
61
			$this->set(compact('result','q','page'));
62
			$this->render('/Elements/searchresult');
13808 anikendra 63
		}
64
	}
65
 
13736 anikendra 66
	public function mine() {		
13591 anikendra 67
		$userId = $this->request->query('user_id');
68
		if(isset($userId) && !empty($userId)){
13597 anikendra 69
			$this->loadModel('User');
70
			$dbuser = $this->User->findById($userId);
71
			$this->Auth->login($dbuser['User']);
13591 anikendra 72
		}
13808 anikendra 73
		// $this->checkMobileNumber();
13583 anikendra 74
		$likedDeals = $disLikedDeals = array();
13570 anikendra 75
		$this->loadModel('Api');
76
		$page = $this->request->query('page');
77
		if(!isset($page)){
78
			$page = 1;
13808 anikendra 79
		}				
80
		//Sort and Direction
81
		$sort = $this->request->query('sort');
82
		$direction = $this->request->query('direction');
83
		$url = $this->getDealsApiUrl($page,$this->Auth->User('id'),0,$sort,$direction);		
13794 anikendra 84
		$deals = $this->make_request($url,null);
13583 anikendra 85
		$myactions = $this->Api->getMyActions($this->Auth->User('id'));
86
		if(!empty($myactions)) {
87
			foreach ($myactions['actions'] as $key => $value) {
88
				if($value['UserAction']['action'] == 'like'){
89
					$likedDeals[$value['UserAction']['store_product_id']] = $value['UserAction']['id'];
90
				}else{
91
					$disLikedDeals[$value['UserAction']['store_product_id']] = $value['UserAction']['id'];
92
				}
93
			}
94
		}
13794 anikendra 95
		$title_for_layout = "Get Cashback on favourite";
96
		$description = "Get cashback on your favourite products. Mouth watering deals";
13808 anikendra 97
		$this->set(compact('page', 'title_for_layout', 'description', 'deals', 'likedDeals','disLikedDeals','sort','direction'));
13570 anikendra 98
		$this->render('/Pages/home');
99
	}
100
 
101
	public function getdeals() {
13583 anikendra 102
		$likedDeals = $disLikedDeals = array();
13570 anikendra 103
		$this->layout = 'ajax';
104
		$this->loadModel('Api');
105
		$page = $this->request->query('page');
106
		if(!isset($page)){
107
			$page = 1;
13794 anikendra 108
		}	
13815 anikendra 109
		//Sort and Direction
110
		$sort = $this->request->query('sort');
111
		$direction = $this->request->query('direction');
13808 anikendra 112
		$url = $this->getDealsApiUrl($page,$this->Auth->User('id'),0,$sort,$direction);
13794 anikendra 113
		$deals = $this->make_request($url,null);
13583 anikendra 114
		$myactions = $this->Api->getMyActions($this->Auth->User('id'));
115
		if(!empty($myactions)) {
116
			foreach ($myactions['actions'] as $key => $value) {
117
				if($value['UserAction']['action'] == 'like'){
118
					$likedDeals[$value['UserAction']['store_product_id']] = $value['UserAction']['id'];
119
				}else{
120
					$disLikedDeals[$value['UserAction']['store_product_id']] = $value['UserAction']['id'];
121
				}
122
			}
123
		}
13794 anikendra 124
		$title_for_layout = "Get Cashback on your favourite products";
125
		$description = "Get cashback on your favourite products. Mouth watering deals";
13808 anikendra 126
		$this->set(compact('page', 'title_for_layout', 'description', 'deals', 'likedDeals','disLikedDeals','sort','direction'));
13570 anikendra 127
		$this->render('/Elements/deals');
128
	}	
13532 anikendra 129
/**
130
 * index method
131
 *
132
 * @return void
133
 */
13550 anikendra 134
	public function index($user_id=null) {
13532 anikendra 135
		$this->response->type('json');
136
		$this->layout = 'ajax';
13550 anikendra 137
		$limit = 20;
138
		$this->StoreProduct->recursive = -1;
139
		$count = $this->StoreProduct->find('count');
140
		$this->StoreProduct->Behaviors->attach('Containable');
13689 anikendra 141
		$this->Paginator->settings = array('order'=>array('rank'=>'asc'),'contain'=>array('Product'),'limit'=>$limit,'fields'=>array('Product.category_id','StoreProduct.id','StoreProduct.title','StoreProduct.thumbnail','StoreProduct.available_price','StoreProduct.cashback','StoreProduct.tagline','StoreProduct.offer','StoreProduct.cashback','StoreProduct.available_price','Product.name'));
13550 anikendra 142
		$result = array('products' => $this->Paginator->paginate(),'maxresults'=>ceil($count/$limit));
13532 anikendra 143
		$callback = $this->request->query('callback');
144
		$this->set(array(
145
		    'result' => $result,
146
		    'callback' => $callback,
147
		    '_serialize' => array('result')
148
		));
13567 anikendra 149
		$this->render('/Elements/json');
13532 anikendra 150
	}
151
 
13579 anikendra 152
	public function category($user_id=null,$categoryId=null) {
153
		$this->response->type('json');
154
		$this->layout = 'ajax';
155
		$limit = 20;
156
		$this->StoreProduct->recursive = -1;
157
		// $count = $this->StoreProduct->find('count');
158
		$this->StoreProduct->Behaviors->attach('Containable');
13723 anikendra 159
		$this->Paginator->settings = array('order'=>array('rank'=>'asc'),'conditions'=>array('StoreProduct.category_id'=>$categoryId),'contain'=>array('Product'),'limit'=>$limit,'fields'=>array('Product.category_id','StoreProduct.id','StoreProduct.title','StoreProduct.thumbnail','StoreProduct.available_price','StoreProduct.cashback','StoreProduct.tagline','StoreProduct.offer','StoreProduct.available_price'));
13583 anikendra 160
		$result = array('products' => $this->Paginator->paginate());
13579 anikendra 161
		$callback = $this->request->query('callback');
162
		$this->set(array(
163
		    'result' => $result,
164
		    'callback' => $callback,
165
		    '_serialize' => array('result')
166
		));
167
		$this->render('/Elements/json');
168
	}
169
 
13550 anikendra 170
	public function bycategory($userId=null) {
13541 anikendra 171
		$this->loadModel('UserCategory');
172
		$options = array('conditions' => array('user_id'=>$userId),'recursive'=>-1);
173
		$userCategories = $this->UserCategory->find('all',$options);		
174
		$this->response->type('json');
175
		$this->layout = 'ajax';
13550 anikendra 176
		$limit = 20;
13541 anikendra 177
		$conditions = null;
13550 anikendra 178
		$this->StoreProduct->recursive = -1;
179
		$this->StoreProduct->Behaviors->attach('Containable');
180
		$products = array();
13700 anikendra 181
		// if(!empty($userCategories)){
182
		if(1==2){
13541 anikendra 183
			foreach ($userCategories as $key => $value) {
184
				$categoryIds[] = $value['UserCategory']['category_id'];
13700 anikendra 185
				$conditions = array('StoreProduct.category_id'=>$value['UserCategory']['category_id']);
13689 anikendra 186
				$this->Paginator->settings = array('contain'=>array('Product'),'limit'=>$limit,'fields'=>array('Product.category_id','StoreProduct.id','StoreProduct.title','StoreProduct.thumbnail','StoreProduct.available_price','StoreProduct.cashback','StoreProduct.available_price','StoreProduct.tagline','StoreProduct.offer'),'conditions'=>$conditions);
13550 anikendra 187
				$rows = $this->Paginator->paginate();				
188
				foreach($rows AS $key => $product){
189
					$products[$value['UserCategory']['category_id']][$key][] = $product;
190
				}
13541 anikendra 191
			}
13567 anikendra 192
		}else{
193
			//Fetch all categories
194
			$categories = $this->StoreProduct->Product->Category->find('all');
195
			foreach ($categories as $key => $value) {
196
				$categoryIds[] = $value['Category']['id'];
13700 anikendra 197
				$conditions = array('StoreProduct.category_id'=>$value['Category']['id']);
13689 anikendra 198
				$this->Paginator->settings = array('contain'=>array('Product'),'limit'=>$limit,'fields'=>array('Product.category_id','StoreProduct.id','StoreProduct.title','StoreProduct.thumbnail','StoreProduct.available_price','StoreProduct.cashback','StoreProduct.available_price','StoreProduct.tagline','StoreProduct.offer'),'conditions'=>$conditions);
13567 anikendra 199
				$rows = $this->Paginator->paginate();				
200
				foreach($rows AS $key => $product){
201
					$products[$value['Category']['id']][$key][] = $product;
202
				}
203
			}
13700 anikendra 204
		}		
205
		$this->log(print_r($products,1),'bycategory')	;		
13550 anikendra 206
		$result = array('products' => $products);//,'categories'=>$categoryIds);
13541 anikendra 207
		$callback = $this->request->query('callback');
208
		$this->set(array(
209
		    'result' => $result,
210
		    'callback' => $callback,
211
		    '_serialize' => array('result')
212
		));
13567 anikendra 213
		$this->render('/Elements/json');
13541 anikendra 214
	}
215
 
13532 anikendra 216
/**
217
 * view method
218
 *
219
 * @throws NotFoundException
220
 * @param string $id
221
 * @return void
222
 */
13901 anikendra 223
	public function view($id = null,$bundleId=null) {
224
		$this->layout = "innerpages";
225
		$cachekey = 'storeproduct-'.$id;
14139 anikendra 226
		$product = Cache::read($cachekey,'fivemin');
13901 anikendra 227
		if(empty($product)) {
228
			$url = $this->apihost.'masterData/getSkuById/'.$id;
229
			$product = $this->make_request($url,null);
14139 anikendra 230
			Cache::write($cachekey,$product,'fivemin');			
13532 anikendra 231
		}
13901 anikendra 232
		$storeProduct = json_decode($product[0],1);
233
		$activestores = Configure::read('activestores');
234
		$this->set(compact('storeProduct','activestores'));
13532 anikendra 235
	}
236
 
13901 anikendra 237
	public function getliveprice($bundleId=null,$storeId=null) {
238
		$cachekey = 'liveprice-'.$bundleId.'-'.$storeId;
239
		$products = Cache::read($cachekey,'fivemin');
240
		if(empty($products)) {
241
			$result = array();
242
			$this->response->type('json');
243
			$this->layout = 'ajax';
244
			$url = $this->apihost."Catalog/fetchLivePrices/?skuBundleId=$bundleId&source_id=$storeId";
245
			$products = $this->make_request($url,null);
246
			Cache::write($cachekey,$products,'fivemin');			
247
		}
248
		if(!empty($products)){
249
			$result['products'] = array();
250
			foreach($products AS $product) {
251
				array_push($result['products'],(json_decode($product)));
252
			}
253
			$products = json_decode(stripslashes(json_encode($products)));
254
			$result['success'] = true;
255
			// $result['products'] = $products;
256
		}else{
257
			$result['success'] = false;
258
		}
259
		$this->response->type('json');
260
		$this->layout = 'ajax';
261
		$this->set(array(
262
		    'result' => $result,
263
		    '_serialize' => array('result')
264
		));
265
		$this->render('/Elements/json');
266
	}
267
 
13532 anikendra 268
/**
269
 * add method
270
 *
271
 * @return void
272
 */
273
	public function add() {
274
		if ($this->request->is('post')) {
275
			$this->StoreProduct->create();
276
			if ($this->StoreProduct->save($this->request->data)) {
277
				$this->Session->setFlash(__('The store product has been saved.'));
278
				return $this->redirect(array('action' => 'index'));
279
			} else {
280
				$this->Session->setFlash(__('The store product could not be saved. Please, try again.'));
281
			}
282
		}
283
		$stores = $this->StoreProduct->Store->find('list');
284
		$products = $this->StoreProduct->Product->find('list');
285
		$this->set(compact('stores', 'products'));
286
	}
287
 
288
/**
289
 * edit method
290
 *
291
 * @throws NotFoundException
292
 * @param string $id
293
 * @return void
294
 */
295
	public function edit($id = null) {
296
		if (!$this->StoreProduct->exists($id)) {
297
			throw new NotFoundException(__('Invalid store product'));
298
		}
299
		if ($this->request->is(array('post', 'put'))) {
300
			if ($this->StoreProduct->save($this->request->data)) {
301
				$this->Session->setFlash(__('The store product has been saved.'));
302
				return $this->redirect(array('action' => 'index'));
303
			} else {
304
				$this->Session->setFlash(__('The store product could not be saved. Please, try again.'));
305
			}
306
		} else {
307
			$options = array('conditions' => array('StoreProduct.' . $this->StoreProduct->primaryKey => $id));
308
			$this->request->data = $this->StoreProduct->find('first', $options);
309
		}
310
		$stores = $this->StoreProduct->Store->find('list');
311
		$products = $this->StoreProduct->Product->find('list');
312
		$this->set(compact('stores', 'products'));
313
	}
314
 
315
/**
316
 * delete method
317
 *
318
 * @throws NotFoundException
319
 * @param string $id
320
 * @return void
321
 */
322
	public function delete($id = null) {
323
		$this->StoreProduct->id = $id;
324
		if (!$this->StoreProduct->exists()) {
325
			throw new NotFoundException(__('Invalid store product'));
326
		}
327
		$this->request->onlyAllow('post', 'delete');
328
		if ($this->StoreProduct->delete()) {
329
			$this->Session->setFlash(__('The store product has been deleted.'));
330
		} else {
331
			$this->Session->setFlash(__('The store product could not be deleted. Please, try again.'));
332
		}
333
		return $this->redirect(array('action' => 'index'));
334
	}
335
 
336
/**
337
 * admin_index method
338
 *
339
 * @return void
340
 */
341
	public function admin_index() {
342
		$this->StoreProduct->recursive = 0;
343
		$this->set('storeProducts', $this->Paginator->paginate());
344
	}
345
 
346
/**
347
 * admin_view method
348
 *
349
 * @throws NotFoundException
350
 * @param string $id
351
 * @return void
352
 */
353
	public function admin_view($id = null) {
354
		if (!$this->StoreProduct->exists($id)) {
355
			throw new NotFoundException(__('Invalid store product'));
356
		}
357
		$options = array('conditions' => array('StoreProduct.' . $this->StoreProduct->primaryKey => $id));
358
		$this->set('storeProduct', $this->StoreProduct->find('first', $options));
359
	}
360
 
361
/**
362
 * admin_add method
363
 *
364
 * @return void
365
 */
366
	public function admin_add() {
367
		if ($this->request->is('post')) {
368
			$this->StoreProduct->create();
369
			if ($this->StoreProduct->save($this->request->data)) {
370
				$this->Session->setFlash(__('The store product has been saved.'));
371
				return $this->redirect(array('action' => 'index'));
372
			} else {
373
				$this->Session->setFlash(__('The store product could not be saved. Please, try again.'));
374
			}
375
		}
376
		$stores = $this->StoreProduct->Store->find('list');
377
		$products = $this->StoreProduct->Product->find('list');
378
		$this->set(compact('stores', 'products'));
379
	}
380
 
381
/**
382
 * admin_edit method
383
 *
384
 * @throws NotFoundException
385
 * @param string $id
386
 * @return void
387
 */
388
	public function admin_edit($id = null) {
389
		if (!$this->StoreProduct->exists($id)) {
390
			throw new NotFoundException(__('Invalid store product'));
391
		}
392
		if ($this->request->is(array('post', 'put'))) {
393
			if ($this->StoreProduct->save($this->request->data)) {
394
				$this->Session->setFlash(__('The store product has been saved.'));
395
				return $this->redirect(array('action' => 'index'));
396
			} else {
397
				$this->Session->setFlash(__('The store product could not be saved. Please, try again.'));
398
			}
399
		} else {
400
			$options = array('conditions' => array('StoreProduct.' . $this->StoreProduct->primaryKey => $id));
401
			$this->request->data = $this->StoreProduct->find('first', $options);
402
		}
403
		$stores = $this->StoreProduct->Store->find('list');
404
		$products = $this->StoreProduct->Product->find('list');
405
		$this->set(compact('stores', 'products'));
406
	}
407
 
408
/**
409
 * admin_delete method
410
 *
411
 * @throws NotFoundException
412
 * @param string $id
413
 * @return void
414
 */
415
	public function admin_delete($id = null) {
416
		$this->StoreProduct->id = $id;
417
		if (!$this->StoreProduct->exists()) {
418
			throw new NotFoundException(__('Invalid store product'));
419
		}
420
		$this->request->onlyAllow('post', 'delete');
421
		if ($this->StoreProduct->delete()) {
422
			$this->Session->setFlash(__('The store product has been deleted.'));
423
		} else {
424
			$this->Session->setFlash(__('The store product could not be deleted. Please, try again.'));
425
		}
426
		return $this->redirect(array('action' => 'index'));
427
	}}