Subversion Repositories SmartDukaan

Rev

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