Subversion Repositories SmartDukaan

Rev

Rev 14969 | Rev 15311 | 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) {
241
		$this->layout = "innerpages";
242
		$cachekey = 'storeproduct-'.$id;
14139 anikendra 243
		$product = Cache::read($cachekey,'fivemin');
13901 anikendra 244
		if(empty($product)) {
245
			$url = $this->apihost.'masterData/getSkuById/'.$id;
246
			$product = $this->make_request($url,null);
14139 anikendra 247
			Cache::write($cachekey,$product,'fivemin');			
13532 anikendra 248
		}
13901 anikendra 249
		$storeProduct = json_decode($product[0],1);
250
		$activestores = Configure::read('activestores');
251
		$this->set(compact('storeProduct','activestores'));
13532 anikendra 252
	}
253
 
13901 anikendra 254
	public function getliveprice($bundleId=null,$storeId=null) {
14386 anikendra 255
		// $cachekey = 'liveprice-'.$bundleId.'-'.$storeId;
256
		// $this->log($cachekey,'api');
14345 anikendra 257
		// $products = Cache::read($cachekey,'fivemin');
14386 anikendra 258
		// if(empty($products)) {
14387 anikendra 259
		session_write_close();
14386 anikendra 260
		$result = array();
261
		$this->response->type('json');
262
		$this->layout = 'ajax';
263
		$url = $this->livepriceurl."?skuBundleId=$bundleId&source_id=$storeId";
264
		$products = $this->make_request($url,null);
265
			// Cache::write($cachekey,$products,'fivemin');			
266
		// }
13901 anikendra 267
		if(!empty($products)){
268
			$result['products'] = array();
269
			foreach($products AS $product) {
14386 anikendra 270
				// debug($product);
271
				array_push($result['products'],$product);
13901 anikendra 272
			}
273
			$products = json_decode(stripslashes(json_encode($products)));
274
			$result['success'] = true;
275
			// $result['products'] = $products;
14386 anikendra 276
		} else{
13901 anikendra 277
			$result['success'] = false;
278
		}
279
		$this->response->type('json');
280
		$this->layout = 'ajax';
281
		$this->set(array(
282
		    'result' => $result,
283
		    '_serialize' => array('result')
284
		));
285
		$this->render('/Elements/json');
286
	}
287
 
13532 anikendra 288
/**
289
 * add method
290
 *
291
 * @return void
292
 */
293
	public function add() {
294
		if ($this->request->is('post')) {
295
			$this->StoreProduct->create();
296
			if ($this->StoreProduct->save($this->request->data)) {
297
				$this->Session->setFlash(__('The store product has been saved.'));
298
				return $this->redirect(array('action' => 'index'));
299
			} else {
300
				$this->Session->setFlash(__('The store product could not be saved. Please, try again.'));
301
			}
302
		}
303
		$stores = $this->StoreProduct->Store->find('list');
304
		$products = $this->StoreProduct->Product->find('list');
305
		$this->set(compact('stores', 'products'));
306
	}
307
 
308
/**
309
 * edit method
310
 *
311
 * @throws NotFoundException
312
 * @param string $id
313
 * @return void
314
 */
315
	public function edit($id = null) {
316
		if (!$this->StoreProduct->exists($id)) {
317
			throw new NotFoundException(__('Invalid store product'));
318
		}
319
		if ($this->request->is(array('post', 'put'))) {
320
			if ($this->StoreProduct->save($this->request->data)) {
321
				$this->Session->setFlash(__('The store product has been saved.'));
322
				return $this->redirect(array('action' => 'index'));
323
			} else {
324
				$this->Session->setFlash(__('The store product could not be saved. Please, try again.'));
325
			}
326
		} else {
327
			$options = array('conditions' => array('StoreProduct.' . $this->StoreProduct->primaryKey => $id));
328
			$this->request->data = $this->StoreProduct->find('first', $options);
329
		}
330
		$stores = $this->StoreProduct->Store->find('list');
331
		$products = $this->StoreProduct->Product->find('list');
332
		$this->set(compact('stores', 'products'));
333
	}
334
 
335
/**
336
 * delete method
337
 *
338
 * @throws NotFoundException
339
 * @param string $id
340
 * @return void
341
 */
342
	public function delete($id = null) {
343
		$this->StoreProduct->id = $id;
344
		if (!$this->StoreProduct->exists()) {
345
			throw new NotFoundException(__('Invalid store product'));
346
		}
347
		$this->request->onlyAllow('post', 'delete');
348
		if ($this->StoreProduct->delete()) {
349
			$this->Session->setFlash(__('The store product has been deleted.'));
350
		} else {
351
			$this->Session->setFlash(__('The store product could not be deleted. Please, try again.'));
352
		}
353
		return $this->redirect(array('action' => 'index'));
354
	}
355
 
356
/**
357
 * admin_index method
358
 *
359
 * @return void
360
 */
361
	public function admin_index() {
362
		$this->StoreProduct->recursive = 0;
363
		$this->set('storeProducts', $this->Paginator->paginate());
364
	}
365
 
366
/**
367
 * admin_view method
368
 *
369
 * @throws NotFoundException
370
 * @param string $id
371
 * @return void
372
 */
373
	public function admin_view($id = null) {
374
		if (!$this->StoreProduct->exists($id)) {
375
			throw new NotFoundException(__('Invalid store product'));
376
		}
377
		$options = array('conditions' => array('StoreProduct.' . $this->StoreProduct->primaryKey => $id));
378
		$this->set('storeProduct', $this->StoreProduct->find('first', $options));
379
	}
380
 
381
/**
382
 * admin_add method
383
 *
384
 * @return void
385
 */
386
	public function admin_add() {
387
		if ($this->request->is('post')) {
388
			$this->StoreProduct->create();
389
			if ($this->StoreProduct->save($this->request->data)) {
390
				$this->Session->setFlash(__('The store product has been saved.'));
391
				return $this->redirect(array('action' => 'index'));
392
			} else {
393
				$this->Session->setFlash(__('The store product could not be saved. Please, try again.'));
394
			}
395
		}
396
		$stores = $this->StoreProduct->Store->find('list');
397
		$products = $this->StoreProduct->Product->find('list');
398
		$this->set(compact('stores', 'products'));
399
	}
400
 
401
/**
402
 * admin_edit method
403
 *
404
 * @throws NotFoundException
405
 * @param string $id
406
 * @return void
407
 */
408
	public function admin_edit($id = null) {
409
		if (!$this->StoreProduct->exists($id)) {
410
			throw new NotFoundException(__('Invalid store product'));
411
		}
412
		if ($this->request->is(array('post', 'put'))) {
413
			if ($this->StoreProduct->save($this->request->data)) {
414
				$this->Session->setFlash(__('The store product has been saved.'));
415
				return $this->redirect(array('action' => 'index'));
416
			} else {
417
				$this->Session->setFlash(__('The store product could not be saved. Please, try again.'));
418
			}
419
		} else {
420
			$options = array('conditions' => array('StoreProduct.' . $this->StoreProduct->primaryKey => $id));
421
			$this->request->data = $this->StoreProduct->find('first', $options);
422
		}
423
		$stores = $this->StoreProduct->Store->find('list');
424
		$products = $this->StoreProduct->Product->find('list');
425
		$this->set(compact('stores', 'products'));
426
	}
427
 
428
/**
429
 * admin_delete method
430
 *
431
 * @throws NotFoundException
432
 * @param string $id
433
 * @return void
434
 */
435
	public function admin_delete($id = null) {
436
		$this->StoreProduct->id = $id;
437
		if (!$this->StoreProduct->exists()) {
438
			throw new NotFoundException(__('Invalid store product'));
439
		}
440
		$this->request->onlyAllow('post', 'delete');
441
		if ($this->StoreProduct->delete()) {
442
			$this->Session->setFlash(__('The store product has been deleted.'));
443
		} else {
444
			$this->Session->setFlash(__('The store product could not be deleted. Please, try again.'));
445
		}
446
		return $this->redirect(array('action' => 'index'));
447
	}}