Subversion Repositories SmartDukaan

Rev

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