Subversion Repositories SmartDukaan

Rev

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