Subversion Repositories SmartDukaan

Rev

Rev 17979 | Rev 19992 | 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();
16423 anikendra 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');
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');
94
		if(isset($userId) && !empty($userId)){
13597 anikendra 95
			$this->loadModel('User');
96
			$dbuser = $this->User->findById($userId);
97
			$this->Auth->login($dbuser['User']);
13591 anikendra 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');
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';
279
		$url = $this->livepriceurl."?skuBundleId=$bundleId&source_id=$storeId";
280
		$products = $this->make_request($url,null);
281
			// Cache::write($cachekey,$products,'fivemin');			
282
		// }
13901 anikendra 283
		if(!empty($products)){
284
			$result['products'] = array();
285
			foreach($products AS $product) {
14386 anikendra 286
				// debug($product);
287
				array_push($result['products'],$product);
13901 anikendra 288
			}
289
			$products = json_decode(stripslashes(json_encode($products)));
290
			$result['success'] = true;
19325 naman 291
			$subcat_id = $result['products'][0]['subCategoryId'];
292
			$cat_id = $result['products'][0]['category_id'];
293
 
294
			//check for offer start
295
			$user_id = $this->Auth->user('id');
296
			$cachekey = 'target-'.$user_id;
297
			$getoffer = Cache::read($cachekey,'target');
298
 
299
			$offertext = "";
300
			$offerresponse = "";
301
			if($result['products'][0]['source_id'] == 4){
302
				if($getoffer === false){
303
					$offerurl = $this->apihost."getOfferForUser/?user_id=".$user_id;
304
					$offerresponse = $this->make_request($offerurl,null);
305
					Cache::write($cachekey , $offerresponse ,'target');
306
					if(!empty($offerresponse)){
307
						$current_time = time();
308
						if($offerresponse['startDate']/1000 <= $current_time && $offerresponse['endDate']/1000 >= $current_time ){
309
 
310
							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'])){
311
								$offertext = $offerresponse['offer_description'];
312
							}
313
						}
314
 
315
					}
316
				}else{
317
					if(!empty($getoffer)){
318
							$offerresponse = $getoffer;
319
							$current_time = time();
320
							if($offerresponse['startDate']/1000 <= $current_time && $offerresponse['endDate']/1000 >= $current_time ){
321
 
322
								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'])){
323
									$offertext = $offerresponse['offer_description'];
324
								}
325
							}
326
 
327
						}
328
				}
329
			}	
330
 
331
			$result['offertext'] = $offertext;
332
			//check for offer end
333
 
13901 anikendra 334
			// $result['products'] = $products;
14386 anikendra 335
		} else{
13901 anikendra 336
			$result['success'] = false;
337
		}
338
		$this->response->type('json');
339
		$this->layout = 'ajax';
340
		$this->set(array(
341
		    'result' => $result,
342
		    '_serialize' => array('result')
343
		));
344
		$this->render('/Elements/json');
345
	}
346
 
13532 anikendra 347
/**
348
 * add method
349
 *
350
 * @return void
351
 */
352
	public function add() {
353
		if ($this->request->is('post')) {
354
			$this->StoreProduct->create();
355
			if ($this->StoreProduct->save($this->request->data)) {
356
				$this->Session->setFlash(__('The store product has been saved.'));
357
				return $this->redirect(array('action' => 'index'));
358
			} else {
359
				$this->Session->setFlash(__('The store product could not be saved. Please, try again.'));
360
			}
361
		}
362
		$stores = $this->StoreProduct->Store->find('list');
363
		$products = $this->StoreProduct->Product->find('list');
364
		$this->set(compact('stores', 'products'));
365
	}
366
 
367
/**
368
 * edit method
369
 *
370
 * @throws NotFoundException
371
 * @param string $id
372
 * @return void
373
 */
374
	public function edit($id = null) {
375
		if (!$this->StoreProduct->exists($id)) {
376
			throw new NotFoundException(__('Invalid store product'));
377
		}
378
		if ($this->request->is(array('post', 'put'))) {
379
			if ($this->StoreProduct->save($this->request->data)) {
380
				$this->Session->setFlash(__('The store product has been saved.'));
381
				return $this->redirect(array('action' => 'index'));
382
			} else {
383
				$this->Session->setFlash(__('The store product could not be saved. Please, try again.'));
384
			}
385
		} else {
386
			$options = array('conditions' => array('StoreProduct.' . $this->StoreProduct->primaryKey => $id));
387
			$this->request->data = $this->StoreProduct->find('first', $options);
388
		}
389
		$stores = $this->StoreProduct->Store->find('list');
390
		$products = $this->StoreProduct->Product->find('list');
391
		$this->set(compact('stores', 'products'));
392
	}
393
 
394
/**
395
 * delete method
396
 *
397
 * @throws NotFoundException
398
 * @param string $id
399
 * @return void
400
 */
401
	public function delete($id = null) {
402
		$this->StoreProduct->id = $id;
403
		if (!$this->StoreProduct->exists()) {
404
			throw new NotFoundException(__('Invalid store product'));
405
		}
406
		$this->request->onlyAllow('post', 'delete');
407
		if ($this->StoreProduct->delete()) {
408
			$this->Session->setFlash(__('The store product has been deleted.'));
409
		} else {
410
			$this->Session->setFlash(__('The store product could not be deleted. Please, try again.'));
411
		}
412
		return $this->redirect(array('action' => 'index'));
413
	}
414
 
415
/**
416
 * admin_index method
417
 *
418
 * @return void
419
 */
420
	public function admin_index() {
421
		$this->StoreProduct->recursive = 0;
422
		$this->set('storeProducts', $this->Paginator->paginate());
423
	}
424
 
425
/**
426
 * admin_view method
427
 *
428
 * @throws NotFoundException
429
 * @param string $id
430
 * @return void
431
 */
432
	public function admin_view($id = null) {
433
		if (!$this->StoreProduct->exists($id)) {
434
			throw new NotFoundException(__('Invalid store product'));
435
		}
436
		$options = array('conditions' => array('StoreProduct.' . $this->StoreProduct->primaryKey => $id));
437
		$this->set('storeProduct', $this->StoreProduct->find('first', $options));
438
	}
439
 
440
/**
441
 * admin_add method
442
 *
443
 * @return void
444
 */
445
	public function admin_add() {
446
		if ($this->request->is('post')) {
447
			$this->StoreProduct->create();
448
			if ($this->StoreProduct->save($this->request->data)) {
449
				$this->Session->setFlash(__('The store product has been saved.'));
450
				return $this->redirect(array('action' => 'index'));
451
			} else {
452
				$this->Session->setFlash(__('The store product could not be saved. Please, try again.'));
453
			}
454
		}
455
		$stores = $this->StoreProduct->Store->find('list');
456
		$products = $this->StoreProduct->Product->find('list');
457
		$this->set(compact('stores', 'products'));
458
	}
459
 
460
/**
461
 * admin_edit method
462
 *
463
 * @throws NotFoundException
464
 * @param string $id
465
 * @return void
466
 */
467
	public function admin_edit($id = null) {
468
		if (!$this->StoreProduct->exists($id)) {
469
			throw new NotFoundException(__('Invalid store product'));
470
		}
471
		if ($this->request->is(array('post', 'put'))) {
472
			if ($this->StoreProduct->save($this->request->data)) {
473
				$this->Session->setFlash(__('The store product has been saved.'));
474
				return $this->redirect(array('action' => 'index'));
475
			} else {
476
				$this->Session->setFlash(__('The store product could not be saved. Please, try again.'));
477
			}
478
		} else {
479
			$options = array('conditions' => array('StoreProduct.' . $this->StoreProduct->primaryKey => $id));
480
			$this->request->data = $this->StoreProduct->find('first', $options);
481
		}
482
		$stores = $this->StoreProduct->Store->find('list');
483
		$products = $this->StoreProduct->Product->find('list');
484
		$this->set(compact('stores', 'products'));
485
	}
486
 
487
/**
488
 * admin_delete method
489
 *
490
 * @throws NotFoundException
491
 * @param string $id
492
 * @return void
493
 */
494
	public function admin_delete($id = null) {
495
		$this->StoreProduct->id = $id;
496
		if (!$this->StoreProduct->exists()) {
497
			throw new NotFoundException(__('Invalid store product'));
498
		}
499
		$this->request->onlyAllow('post', 'delete');
500
		if ($this->StoreProduct->delete()) {
501
			$this->Session->setFlash(__('The store product has been deleted.'));
502
		} else {
503
			$this->Session->setFlash(__('The store product could not be deleted. Please, try again.'));
504
		}
505
		return $this->redirect(array('action' => 'index'));
16363 anikendra 506
	}
507
 
508
	public function admin_genurl() {
16368 anikendra 509
 
16363 anikendra 510
	}
16368 anikendra 511
 
512
	public function skus($bundleIds) {
513
		$userId = $this->request->query('user_id');
514
		if(isset($userId) && !empty($userId)) {
515
			$this->loadModel('User');
17979 amit.gupta 516
			$this->User->recursive = -1;
16368 anikendra 517
			$dbuser = $this->User->findById($userId);
518
			$this->Auth->login($dbuser['User']);
519
		}
520
		//Fetch list of bundleIds from api
521
		$url = $this->apihost.'getDealsForNotification/'.$bundleIds;
522
		$response = $this->make_request($url,null);
523
		$deals = array();
524
		if(!empty($response)){
525
			foreach ($response as $key => $value) {
526
				if(!empty($value)){
527
					$deals[] = $value;
528
				}
529
			}
530
		}
531
		$noscrolling = true;
532
		$this->set(compact('deals','id','noscrolling'));
533
	}
16363 anikendra 534
}