Subversion Repositories SmartDukaan

Rev

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