Subversion Repositories SmartDukaan

Rev

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