Subversion Repositories SmartDukaan

Rev

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