Subversion Repositories SmartDukaan

Rev

Rev 20127 | Rev 20237 | 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
 */
14344 anikendra 9
 set_time_limit(180);
10
 
13532 anikendra 11
class StoreProductsController extends AppController {
12
 
13
/**
14
 * Components
15
 *
16
 * @var array
17
 */
18
	public $components = array('Paginator');
14969 anikendra 19
	public $helpers = array('Time');
13808 anikendra 20
	public $apihost;	
14386 anikendra 21
	public $livepriceurl;
13532 anikendra 22
 
23
	public function beforeFilter() {
24
		parent::beforeFilter();
20139 naman 25
// 		$this->Auth->allow('bycategory','category','mine','getdeals','getliveprice','search','filter','skus');
26
		$this->Auth->allow('bycategory','category','getdeals','getliveprice');
14386 anikendra 27
		$callback = $this->request->query('callback');	
28
		$this->livepriceurl = Configure::read('livepriceurl');	
13532 anikendra 29
	}
13570 anikendra 30
 
15015 anikendra 31
	public function filter($type='brand',$categoryId=3){
32
		$url = $this->apihost.'deals/brands/?category_id='.$categoryId;
33
		$brands = $this->make_request($url,null);
34
		$this->layout = 'innerpages';
35
		$this->set(compact('brands','categoryId'));
36
	}
37
 
17684 naman 38
	public function subcategoryfilter($type='subcategory',$categoryId=6){
39
		$url = $this->apihost.'deals/subCategory/?category_id='.$categoryId;
19325 naman 40
        //debug($url);
17684 naman 41
		$brands = $this->make_request($url,null);
19325 naman 42
		// debug($brands);
17684 naman 43
		$this->layout = 'innerpages';
44
		$this->set(compact('brands','categoryId'));
45
	}	
46
 
13808 anikendra 47
	public function search() {
14215 anikendra 48
		$userId = $this->request->query('user_id');
20103 naman 49
// 		if(isset($userId) && !empty($userId)){
50
// 			$this->loadModel('User');
51
// 			$dbuser = $this->User->findById($userId);
52
// 			$this->Auth->login($dbuser['User']);
53
// 		}
13815 anikendra 54
		$this->layout = 'innerpages';
13808 anikendra 55
		$q = $this->request->query('q');
13901 anikendra 56
		$page = $this->request->query('page');
57
		if(!isset($page)){
58
			$page = 1;
59
		}
60
		$dealsperpage = Configure::read('dealsperpage');
61
		$offset = ($page - 1)*$dealsperpage;
14509 anikendra 62
		$this->loadModel('Campaign');
63
		$campaigns = $this->Campaign->getActiveCampaigns();
13808 anikendra 64
		if(isset($q) && !empty($q)){
13815 anikendra 65
			if (Configure::read('log_solr_queries') === true) {
66
				$this->loadModel('SearchTerm');
67
				$data = array('user_id' => $this->Auth->User('id'),'search_term' => $q);
68
				$this->SearchTerm->create();
69
				$this->SearchTerm->save($data);					
70
			}
13901 anikendra 71
			$result = $this->get_solr_result($q,$page);
19325 naman 72
			//debug($result);
13901 anikendra 73
			$this->set(compact('result','q','page'));
14509 anikendra 74
		}else{
75
			$this->set(compact('campaigns'));
13901 anikendra 76
		}
77
	}
13815 anikendra 78
 
13901 anikendra 79
	public function getsearchresults() {
80
		$this->layout = 'ajax';
81
		$q = $this->request->query('q');
82
		$page = $this->request->query('page');
83
		if(!isset($page)){
84
			$page = 1;
85
		}		
86
		if(isset($q) && !empty($q)){			
87
			$result = $this->get_solr_result($q,$page);
88
			$this->set(compact('result','q','page'));
89
			$this->render('/Elements/searchresult');
13808 anikendra 90
		}
91
	}
92
 
13736 anikendra 93
	public function mine() {		
13591 anikendra 94
		$userId = $this->request->query('user_id');
20103 naman 95
// 		if(isset($userId) && !empty($userId)){
96
// 			$this->loadModel('User');
97
// 			$dbuser = $this->User->findById($userId);
98
// 			$this->Auth->login($dbuser['User']);
99
// 		}
13808 anikendra 100
		// $this->checkMobileNumber();
13583 anikendra 101
		$likedDeals = $disLikedDeals = array();
13570 anikendra 102
		$this->loadModel('Api');
103
		$page = $this->request->query('page');
104
		if(!isset($page)){
105
			$page = 1;
13808 anikendra 106
		}				
107
		//Sort and Direction
108
		$sort = $this->request->query('sort');
109
		$direction = $this->request->query('direction');
110
		$url = $this->getDealsApiUrl($page,$this->Auth->User('id'),0,$sort,$direction);		
13794 anikendra 111
		$deals = $this->make_request($url,null);
13583 anikendra 112
		$myactions = $this->Api->getMyActions($this->Auth->User('id'));
14928 anikendra 113
		$liveScore = $this->Api->getLiveScore();		
13583 anikendra 114
		if(!empty($myactions)) {
115
			foreach ($myactions['actions'] as $key => $value) {
116
				if($value['UserAction']['action'] == 'like'){
117
					$likedDeals[$value['UserAction']['store_product_id']] = $value['UserAction']['id'];
118
				}else{
119
					$disLikedDeals[$value['UserAction']['store_product_id']] = $value['UserAction']['id'];
120
				}
121
			}
122
		}
13794 anikendra 123
		$title_for_layout = "Get Cashback on favourite";
124
		$description = "Get cashback on your favourite products. Mouth watering deals";
14928 anikendra 125
		$this->set(compact('page', 'title_for_layout', 'description', 'deals', 'likedDeals','disLikedDeals','sort','direction','liveScore'));
13570 anikendra 126
		$this->render('/Pages/home');
127
	}
128
 
129
	public function getdeals() {
13583 anikendra 130
		$likedDeals = $disLikedDeals = array();
13570 anikendra 131
		$this->layout = 'ajax';
132
		$this->loadModel('Api');
133
		$page = $this->request->query('page');
134
		if(!isset($page)){
135
			$page = 1;
13794 anikendra 136
		}	
13815 anikendra 137
		//Sort and Direction
138
		$sort = $this->request->query('sort');
139
		$direction = $this->request->query('direction');
13808 anikendra 140
		$url = $this->getDealsApiUrl($page,$this->Auth->User('id'),0,$sort,$direction);
13794 anikendra 141
		$deals = $this->make_request($url,null);
13583 anikendra 142
		$myactions = $this->Api->getMyActions($this->Auth->User('id'));
143
		if(!empty($myactions)) {
144
			foreach ($myactions['actions'] as $key => $value) {
145
				if($value['UserAction']['action'] == 'like'){
146
					$likedDeals[$value['UserAction']['store_product_id']] = $value['UserAction']['id'];
147
				}else{
148
					$disLikedDeals[$value['UserAction']['store_product_id']] = $value['UserAction']['id'];
149
				}
150
			}
151
		}
13794 anikendra 152
		$title_for_layout = "Get Cashback on your favourite products";
153
		$description = "Get cashback on your favourite products. Mouth watering deals";
13808 anikendra 154
		$this->set(compact('page', 'title_for_layout', 'description', 'deals', 'likedDeals','disLikedDeals','sort','direction'));
13570 anikendra 155
		$this->render('/Elements/deals');
156
	}	
13532 anikendra 157
/**
158
 * index method
159
 *
160
 * @return void
161
 */
13550 anikendra 162
	public function index($user_id=null) {
13532 anikendra 163
		$this->response->type('json');
164
		$this->layout = 'ajax';
13550 anikendra 165
		$limit = 20;
166
		$this->StoreProduct->recursive = -1;
167
		$count = $this->StoreProduct->find('count');
168
		$this->StoreProduct->Behaviors->attach('Containable');
13689 anikendra 169
		$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 170
		$result = array('products' => $this->Paginator->paginate(),'maxresults'=>ceil($count/$limit));
13532 anikendra 171
		$callback = $this->request->query('callback');
172
		$this->set(array(
173
		    'result' => $result,
174
		    'callback' => $callback,
175
		    '_serialize' => array('result')
176
		));
13567 anikendra 177
		$this->render('/Elements/json');
13532 anikendra 178
	}
179
 
13579 anikendra 180
	public function category($user_id=null,$categoryId=null) {
181
		$this->response->type('json');
182
		$this->layout = 'ajax';
183
		$limit = 20;
184
		$this->StoreProduct->recursive = -1;
185
		// $count = $this->StoreProduct->find('count');
186
		$this->StoreProduct->Behaviors->attach('Containable');
13723 anikendra 187
		$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 188
		$result = array('products' => $this->Paginator->paginate());
13579 anikendra 189
		$callback = $this->request->query('callback');
190
		$this->set(array(
191
		    'result' => $result,
192
		    'callback' => $callback,
193
		    '_serialize' => array('result')
194
		));
195
		$this->render('/Elements/json');
196
	}
197
 
13550 anikendra 198
	public function bycategory($userId=null) {
13541 anikendra 199
		$this->loadModel('UserCategory');
200
		$options = array('conditions' => array('user_id'=>$userId),'recursive'=>-1);
201
		$userCategories = $this->UserCategory->find('all',$options);		
202
		$this->response->type('json');
203
		$this->layout = 'ajax';
13550 anikendra 204
		$limit = 20;
13541 anikendra 205
		$conditions = null;
13550 anikendra 206
		$this->StoreProduct->recursive = -1;
207
		$this->StoreProduct->Behaviors->attach('Containable');
208
		$products = array();
13700 anikendra 209
		// if(!empty($userCategories)){
210
		if(1==2){
13541 anikendra 211
			foreach ($userCategories as $key => $value) {
212
				$categoryIds[] = $value['UserCategory']['category_id'];
13700 anikendra 213
				$conditions = array('StoreProduct.category_id'=>$value['UserCategory']['category_id']);
13689 anikendra 214
				$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 215
				$rows = $this->Paginator->paginate();				
216
				foreach($rows AS $key => $product){
217
					$products[$value['UserCategory']['category_id']][$key][] = $product;
218
				}
13541 anikendra 219
			}
13567 anikendra 220
		}else{
221
			//Fetch all categories
222
			$categories = $this->StoreProduct->Product->Category->find('all');
223
			foreach ($categories as $key => $value) {
224
				$categoryIds[] = $value['Category']['id'];
13700 anikendra 225
				$conditions = array('StoreProduct.category_id'=>$value['Category']['id']);
13689 anikendra 226
				$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 227
				$rows = $this->Paginator->paginate();				
228
				foreach($rows AS $key => $product){
229
					$products[$value['Category']['id']][$key][] = $product;
230
				}
231
			}
13700 anikendra 232
		}		
233
		$this->log(print_r($products,1),'bycategory')	;		
13550 anikendra 234
		$result = array('products' => $products);//,'categories'=>$categoryIds);
13541 anikendra 235
		$callback = $this->request->query('callback');
236
		$this->set(array(
237
		    'result' => $result,
238
		    'callback' => $callback,
239
		    '_serialize' => array('result')
240
		));
13567 anikendra 241
		$this->render('/Elements/json');
13541 anikendra 242
	}
243
 
13532 anikendra 244
/**
245
 * view method
246
 *
247
 * @throws NotFoundException
248
 * @param string $id
249
 * @return void
250
 */
13901 anikendra 251
	public function view($id = null,$bundleId=null) {
15311 anikendra 252
		$userId = $this->request->query('user_id');
20103 naman 253
// 		if(isset($userId) && !empty($userId)){
254
// 			$this->loadModel('User');
255
// 			$dbuser = $this->User->findById($userId);
256
// 			$this->Auth->login($dbuser['User']);
257
// 		}
13901 anikendra 258
		$this->layout = "innerpages";
259
		$cachekey = 'storeproduct-'.$id;
16234 anikendra 260
		// $product = Cache::read($cachekey,'fivemin');
13901 anikendra 261
		if(empty($product)) {
16234 anikendra 262
			$url = $this->apihost.'masterData/getSkuById/'.$id.'?showDp=1';
13901 anikendra 263
			$product = $this->make_request($url,null);
14139 anikendra 264
			Cache::write($cachekey,$product,'fivemin');			
13532 anikendra 265
		}
13901 anikendra 266
		$storeProduct = json_decode($product[0],1);
267
		$activestores = Configure::read('activestores');
268
		$this->set(compact('storeProduct','activestores'));
13532 anikendra 269
	}
270
 
13901 anikendra 271
	public function getliveprice($bundleId=null,$storeId=null) {
14386 anikendra 272
		// $cachekey = 'liveprice-'.$bundleId.'-'.$storeId;
273
		// $this->log($cachekey,'api');
14345 anikendra 274
		// $products = Cache::read($cachekey,'fivemin');
14386 anikendra 275
		// if(empty($products)) {
14387 anikendra 276
		session_write_close();
14386 anikendra 277
		$result = array();
278
		$this->response->type('json');
279
		$this->layout = 'ajax';
20103 naman 280
// 		$dealpin = $this->getpin();
14386 anikendra 281
		$url = $this->livepriceurl."?skuBundleId=$bundleId&source_id=$storeId";
20103 naman 282
// 		$url = $url."&pin=".$dealpin;
14386 anikendra 283
		$products = $this->make_request($url,null);
284
			// Cache::write($cachekey,$products,'fivemin');			
285
		// }
13901 anikendra 286
		if(!empty($products)){
287
			$result['products'] = array();
288
			foreach($products AS $product) {
14386 anikendra 289
				// debug($product);
290
				array_push($result['products'],$product);
13901 anikendra 291
			}
292
			$products = json_decode(stripslashes(json_encode($products)));
293
			$result['success'] = true;
19325 naman 294
			$subcat_id = $result['products'][0]['subCategoryId'];
295
			$cat_id = $result['products'][0]['category_id'];
296
 
297
			//check for offer start
298
			$user_id = $this->Auth->user('id');
299
			$cachekey = 'target-'.$user_id;
300
			$getoffer = Cache::read($cachekey,'target');
301
 
302
			$offertext = "";
303
			$offerresponse = "";
304
			if($result['products'][0]['source_id'] == 4){
305
				if($getoffer === false){
306
					$offerurl = $this->apihost."getOfferForUser/?user_id=".$user_id;
307
					$offerresponse = $this->make_request($offerurl,null);
308
					Cache::write($cachekey , $offerresponse ,'target');
309
					if(!empty($offerresponse)){
310
						$current_time = time();
311
						if($offerresponse['startDate']/1000 <= $current_time && $offerresponse['endDate']/1000 >= $current_time ){
312
 
313
							if( isset($offerresponse['categories_applicable']) && in_array( $cat_id ,$offerresponse['categories_applicable']) && isset($offerresponse['sub_categories_not_applicable']) && !in_array($subcat_id ,$offerresponse['sub_categories_not_applicable'])){
314
								$offertext = $offerresponse['offer_description'];
315
							}
316
						}
317
 
318
					}
319
				}else{
320
					if(!empty($getoffer)){
321
							$offerresponse = $getoffer;
322
							$current_time = time();
323
							if($offerresponse['startDate']/1000 <= $current_time && $offerresponse['endDate']/1000 >= $current_time ){
324
 
325
								if( isset($offerresponse['categories_applicable']) && in_array( $cat_id ,$offerresponse['categories_applicable']) && isset($offerresponse['sub_categories_not_applicable']) && !in_array($subcat_id ,$offerresponse['sub_categories_not_applicable'])){
326
									$offertext = $offerresponse['offer_description'];
327
								}
328
							}
329
 
330
						}
331
				}
332
			}	
333
 
334
			$result['offertext'] = $offertext;
335
			//check for offer end
336
 
13901 anikendra 337
			// $result['products'] = $products;
14386 anikendra 338
		} else{
13901 anikendra 339
			$result['success'] = false;
340
		}
341
		$this->response->type('json');
342
		$this->layout = 'ajax';
343
		$this->set(array(
344
		    'result' => $result,
345
		    '_serialize' => array('result')
346
		));
347
		$this->render('/Elements/json');
348
	}
349
 
13532 anikendra 350
/**
351
 * add method
352
 *
353
 * @return void
354
 */
355
	public function add() {
356
		if ($this->request->is('post')) {
357
			$this->StoreProduct->create();
358
			if ($this->StoreProduct->save($this->request->data)) {
359
				$this->Session->setFlash(__('The store product has been saved.'));
360
				return $this->redirect(array('action' => 'index'));
361
			} else {
362
				$this->Session->setFlash(__('The store product could not be saved. Please, try again.'));
363
			}
364
		}
365
		$stores = $this->StoreProduct->Store->find('list');
366
		$products = $this->StoreProduct->Product->find('list');
367
		$this->set(compact('stores', 'products'));
368
	}
369
 
370
/**
371
 * edit method
372
 *
373
 * @throws NotFoundException
374
 * @param string $id
375
 * @return void
376
 */
377
	public function edit($id = null) {
378
		if (!$this->StoreProduct->exists($id)) {
379
			throw new NotFoundException(__('Invalid store product'));
380
		}
381
		if ($this->request->is(array('post', 'put'))) {
382
			if ($this->StoreProduct->save($this->request->data)) {
383
				$this->Session->setFlash(__('The store product has been saved.'));
384
				return $this->redirect(array('action' => 'index'));
385
			} else {
386
				$this->Session->setFlash(__('The store product could not be saved. Please, try again.'));
387
			}
388
		} else {
389
			$options = array('conditions' => array('StoreProduct.' . $this->StoreProduct->primaryKey => $id));
390
			$this->request->data = $this->StoreProduct->find('first', $options);
391
		}
392
		$stores = $this->StoreProduct->Store->find('list');
393
		$products = $this->StoreProduct->Product->find('list');
394
		$this->set(compact('stores', 'products'));
395
	}
396
 
397
/**
398
 * delete method
399
 *
400
 * @throws NotFoundException
401
 * @param string $id
402
 * @return void
403
 */
404
	public function delete($id = null) {
405
		$this->StoreProduct->id = $id;
406
		if (!$this->StoreProduct->exists()) {
407
			throw new NotFoundException(__('Invalid store product'));
408
		}
409
		$this->request->onlyAllow('post', 'delete');
410
		if ($this->StoreProduct->delete()) {
411
			$this->Session->setFlash(__('The store product has been deleted.'));
412
		} else {
413
			$this->Session->setFlash(__('The store product could not be deleted. Please, try again.'));
414
		}
415
		return $this->redirect(array('action' => 'index'));
416
	}
417
 
418
/**
419
 * admin_index method
420
 *
421
 * @return void
422
 */
423
	public function admin_index() {
424
		$this->StoreProduct->recursive = 0;
425
		$this->set('storeProducts', $this->Paginator->paginate());
426
	}
427
 
428
/**
429
 * admin_view method
430
 *
431
 * @throws NotFoundException
432
 * @param string $id
433
 * @return void
434
 */
435
	public function admin_view($id = null) {
436
		if (!$this->StoreProduct->exists($id)) {
437
			throw new NotFoundException(__('Invalid store product'));
438
		}
439
		$options = array('conditions' => array('StoreProduct.' . $this->StoreProduct->primaryKey => $id));
440
		$this->set('storeProduct', $this->StoreProduct->find('first', $options));
441
	}
442
 
443
/**
444
 * admin_add method
445
 *
446
 * @return void
447
 */
448
	public function admin_add() {
449
		if ($this->request->is('post')) {
450
			$this->StoreProduct->create();
451
			if ($this->StoreProduct->save($this->request->data)) {
452
				$this->Session->setFlash(__('The store product has been saved.'));
453
				return $this->redirect(array('action' => 'index'));
454
			} else {
455
				$this->Session->setFlash(__('The store product could not be saved. Please, try again.'));
456
			}
457
		}
458
		$stores = $this->StoreProduct->Store->find('list');
459
		$products = $this->StoreProduct->Product->find('list');
460
		$this->set(compact('stores', 'products'));
461
	}
462
 
463
/**
464
 * admin_edit method
465
 *
466
 * @throws NotFoundException
467
 * @param string $id
468
 * @return void
469
 */
470
	public function admin_edit($id = null) {
471
		if (!$this->StoreProduct->exists($id)) {
472
			throw new NotFoundException(__('Invalid store product'));
473
		}
474
		if ($this->request->is(array('post', 'put'))) {
475
			if ($this->StoreProduct->save($this->request->data)) {
476
				$this->Session->setFlash(__('The store product has been saved.'));
477
				return $this->redirect(array('action' => 'index'));
478
			} else {
479
				$this->Session->setFlash(__('The store product could not be saved. Please, try again.'));
480
			}
481
		} else {
482
			$options = array('conditions' => array('StoreProduct.' . $this->StoreProduct->primaryKey => $id));
483
			$this->request->data = $this->StoreProduct->find('first', $options);
484
		}
485
		$stores = $this->StoreProduct->Store->find('list');
486
		$products = $this->StoreProduct->Product->find('list');
487
		$this->set(compact('stores', 'products'));
488
	}
489
 
490
/**
491
 * admin_delete method
492
 *
493
 * @throws NotFoundException
494
 * @param string $id
495
 * @return void
496
 */
497
	public function admin_delete($id = null) {
498
		$this->StoreProduct->id = $id;
499
		if (!$this->StoreProduct->exists()) {
500
			throw new NotFoundException(__('Invalid store product'));
501
		}
502
		$this->request->onlyAllow('post', 'delete');
503
		if ($this->StoreProduct->delete()) {
504
			$this->Session->setFlash(__('The store product has been deleted.'));
505
		} else {
506
			$this->Session->setFlash(__('The store product could not be deleted. Please, try again.'));
507
		}
508
		return $this->redirect(array('action' => 'index'));
16363 anikendra 509
	}
510
 
511
	public function admin_genurl() {
16368 anikendra 512
 
16363 anikendra 513
	}
16368 anikendra 514
 
515
	public function skus($bundleIds) {
20139 naman 516
// 		$userId = $this->request->query('user_id');
517
// 		if(isset($userId) && !empty($userId)) {
518
// 			$this->loadModel('User');
519
// 			$this->User->recursive = -1;
520
// 			$dbuser = $this->User->findById($userId);
521
// 			$this->Auth->login($dbuser['User']);
522
// 		}
16368 anikendra 523
		//Fetch list of bundleIds from api
524
		$url = $this->apihost.'getDealsForNotification/'.$bundleIds;
525
		$response = $this->make_request($url,null);
526
		$deals = array();
527
		if(!empty($response)){
528
			foreach ($response as $key => $value) {
529
				if(!empty($value)){
530
					$deals[] = $value;
531
				}
532
			}
533
		}
534
		$noscrolling = true;
535
		$this->set(compact('deals','id','noscrolling'));
536
	}
16363 anikendra 537
}