Subversion Repositories SmartDukaan

Rev

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