Subversion Repositories SmartDukaan

Rev

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