Subversion Repositories SmartDukaan

Rev

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