Subversion Repositories SmartDukaan

Rev

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