Subversion Repositories SmartDukaan

Rev

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