Subversion Repositories SmartDukaan

Rev

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