Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

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