Subversion Repositories SmartDukaan

Rev

Rev 18655 | Rev 19306 | 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
 * Categories Controller
5
 *
6
 * @property Category $Category
7
 * @property PaginatorComponent $Paginator
8
 */
9
class CategoriesController extends AppController {
10
 
11
/**
12
 * Components
13
 *
14
 * @var array
15
 */
16
	public $components = array('Paginator');
13794 anikendra 17
	public $apihost;
18
	public $limit;
13532 anikendra 19
 
13596 anikendra 20
	public function beforeFilter() {
21
		parent::beforeFilter();
22
		$this->Auth->allow('deals');
13794 anikendra 23
		$this->apihost = Configure::read('pythonapihost');
24
		$this->limit = Configure::read('dealsperpage');
18223 naman 25
		$this->mobileapi = Configure::read('saholicapihost');
19244 amit.gupta 26
		setcookie("fresh", '1', null, '/');
18223 naman 27
 
13596 anikendra 28
	}
29
 
13532 anikendra 30
/**
31
 * index method
32
 *
33
 * @return void
34
 */
35
	public function index() {
36
		// $this->Category->recursive = 0;
37
		// $this->set('categories', $this->Paginator->paginate());
13541 anikendra 38
		$userId = $this->request->query('user_id');
39
		$this->loadModel('UserCategory');
13689 anikendra 40
		$options = array('conditions' => array('user_id'=>$userId),'order'=>array('rank','asc'),'recursive'=>-1);
13541 anikendra 41
		$userCategories = $this->UserCategory->find('all',$options);		
13532 anikendra 42
		$this->response->type('json');
43
		$this->layout = 'ajax';
13541 anikendra 44
		$conditions = array(array('Category.parent_id !='=>0));
45
		if(!empty($userCategories)){
46
			foreach ($userCategories as $key => $value) {
47
				$categoryIds[] = $value['UserCategory']['category_id'];
48
			}
49
			array_push($conditions,array('Category.id'=>$categoryIds));
50
		}
51
		$this->Category->recursive = -1;		
13532 anikendra 52
		$categories = $this->Paginator->paginate(null,$conditions);
53
		$callback = $this->request->query('callback');
54
		$result = array('categories' => $categories);
55
		$this->set(array(
56
		    'result' => $result,
57
		    'callback' => $callback,
58
		    '_serialize' => array('result')
59
		));
60
		$this->render('/Elements/jsonp');
61
	}
62
 
13567 anikendra 63
	public function deals(){
13591 anikendra 64
		$userId = $this->request->query('user_id');
17962 manish.sha 65
		$error = $this->request->query('error');
13591 anikendra 66
		if(isset($userId) && !empty($userId)){
13597 anikendra 67
			$this->loadModel('User');
68
			$dbuser = $this->User->findById($userId);
69
			$this->Auth->login($dbuser['User']);	
13591 anikendra 70
		}
13583 anikendra 71
		$likedDeals = $disLikedDeals = array();
13794 anikendra 72
		// $this->loadModel('Api');
73
		// $apideals = $this->Api->getCategoryDeals($this->Auth->User('id'),1);
74
		// $categorydeals = $apideals['products'];
75
		$page = $this->request->query('page');
76
		if(!isset($page)){
77
			$page = 1;
78
		}
79
		$offset = ($page - 1) * $this->limit;
80
		$url = $this->apihost.'deals/'.$this->Auth->User('id').'?categoryId=0&limit='.$this->limit.'&offset='.$offset;
17683 naman 81
		//debug($url);
13794 anikendra 82
		$deals = $this->make_request($url,null);		
13583 anikendra 83
		$myactions = $this->Api->getMyActions($this->Auth->User('id'));		
84
		if(!empty($myactions)) {
85
			foreach ($myactions['actions'] as $key => $value) {
86
				if($value['UserAction']['action'] == 'like'){
87
					$likedDeals[$value['UserAction']['store_product_id']] = $value['UserAction']['id'];
88
				}else{
89
					$disLikedDeals[$value['UserAction']['store_product_id']] = $value['UserAction']['id'];
90
				}
91
			}
92
		}
17962 manish.sha 93
 
94
		$errorstr = '';
95
 
96
		if(isset($error)){
97
			$errorstr = 'Oops!! Some Error Occured. <br> Please try after Some Time';
98
		}
13567 anikendra 99
		$rows = sizeof($categorydeals);
100
		if($rows>=1){
101
			$this->set('deals',$categorydeals);
13583 anikendra 102
			$this->set('likedDeals',$likedDeals);
103
			$this->set('disLikedDeals',$disLikedDeals);
17962 manish.sha 104
			$this->set('errorstr',$errorstr);
13567 anikendra 105
			$this->render('categorydeals');
106
		}else{
107
			foreach ($categorydeals as $key => $dealarr) {
108
				foreach ($dealarr as $key => $deal) {
109
					$deals[] = $deal[0];
110
				}				
111
			}
17962 manish.sha 112
			$this->set(compact('deals','likedDeals','disLikedDeals','errorstr'));
13567 anikendra 113
		}		
114
	}
115
/*
116
	*
13532 anikendra 117
 * view method
118
 *
119
 * @throws NotFoundException
120
 * @param string $id
121
 * @return void
122
 */
123
	public function view($id = null) {
18393 amit.gupta 124
		$userId = $this->request->query('user_id');
15378 anikendra 125
		if(isset($userId) && !empty($userId)) {
14930 anikendra 126
			$this->loadModel('User');
127
			$dbuser = $this->User->findById($userId);
128
			$this->Auth->login($dbuser['User']);
16549 anikendra 129
		}				
18390 naman 130
		//Sort - type, ordering
131
		$sort = $this->request->query('sort');
132
		$direction = $this->request->query('direction');
133
 
134
		//Filter - brand/subcategory		
135
		$brands = $this->request->query('brands');
136
		$subcategories = $this->request->query('subcategories');
137
 
18655 naman 138
		$searchableSubCategories = array('20'=>'Tempered Glasses', '27'=>'Back Cover', '29'=>'Batteries');
18390 naman 139
 
13583 anikendra 140
		$page = $this->request->query('page');
18063 naman 141
		$searchfor = $this->request->query('searchFor');
142
		// echo "page=>",$page;
17810 manish.sha 143
		$error = $this->request->query('error');
13583 anikendra 144
		if(!isset($page)){
145
			$page = 1;
16549 anikendra 146
		}
147
 
18207 amit.gupta 148
		//$filter = $this->request->query('filter');
15044 anikendra 149
		// $brands = $this->request->query('brands');
16549 anikendra 150
		if($id != 2) {
151
			//Fetch deals
152
			$likedDeals = $disLikedDeals = array();
18390 naman 153
			if(!empty($brands) && !empty($subcategories)){
154
				$filter = 'brand|subcategory';
155
			}			
156
			else if(!empty($brands)){
157
				$filter = 'brand';
158
			}
159
			else if(!empty($subcategories)){
160
				$filter = 'subcategory';
161
			}
17759 naman 162
 
18390 naman 163
			$sortlabel = 'recommended';
164
			if	($sort=='bestSellerPoints'){
165
				$sortlabel  = 'bestseller';
18063 naman 166
			}
18390 naman 167
			else if ($sort=='available_price' && $direction==-1){
168
				$sortlabel  = 'pricehigh';
16549 anikendra 169
			}
18390 naman 170
			else if ($sort=='available_price' && $direction==1){
171
				$sortlabel  = 'pricelow';
172
			}
17683 naman 173
 
174
			$url = $this->getDealsApiUrl($page,$this->Auth->User('id'),$id,$sort,$direction,$filter,$brands,$subcategories);
16549 anikendra 175
			$response = $this->make_request($url,null);
176
			$deals = array();
17683 naman 177
 
178
			$response_count =1;
179
			if($response == '')
180
			{
181
				$response_count = 0;
182
			}
183
			// debug($response_count);
16549 anikendra 184
			if(!empty($response)){
185
				foreach ($response as $key => $value) {
186
					if(!empty($value)){
187
						$deals[] = $value;
188
					}
16098 anikendra 189
				}
190
			}
17683 naman 191
			#print_r($deals);
16549 anikendra 192
			$this->loadModel('Api');
193
			$myactions = $this->Api->getMyActions($this->Auth->User('id'));
194
			if(!empty($myactions)) {
195
				foreach ($myactions['actions'] as $key => $value) {
196
					if($value['UserAction']['action'] == 'like'){
197
						$likedDeals[$value['UserAction']['store_product_id']] = $value['UserAction']['id'];
198
					}else{
199
						$disLikedDeals[$value['UserAction']['store_product_id']] = $value['UserAction']['id'];
200
					}
13583 anikendra 201
				}
202
			}
16549 anikendra 203
			$this->loadModel('NotificationRule');
204
			$notification = $this->NotificationRule->getNotification($this->Auth->User('id'));	
205
		    $filterstr = '';
206
			if(isset($filter) && !empty($filter)){
17759 naman 207
 
17683 naman 208
			    $filterstr = '&filter='.$filter.'&brands='.$brands.'&subcategories='.$subcategories;
17695 naman 209
		  	} 
210
		  	$get_url = "'".$_SERVER['REQUEST_URI']."'";
211
		  	$urlArray = explode('=',$_SERVER['REQUEST_URI']);
212
		  	$last = $urlArray[sizeof($urlArray)-1];
213
 
17810 manish.sha 214
			$errorstr = '';
215
 
216
			if(isset($error)){
217
				$errorstr = 'Oops!! Some Error Occured. <br> Please try after Some Time';
218
			}
17759 naman 219
 
18390 naman 220
			$filterstr = '&brands='.$brands.'&subcategories='.$subcategories;
221
			$nexturl = "/categories/getdeals/".$id."/?page=".($page+1)."&sort=".$sort."&direction=".$direction."".$filterstr;			
18063 naman 222
 
18390 naman 223
			$this->set(compact('response_count','deals','id','likedDeals','disLikedDeals','page','sort','direction','notification','filter','brands','filterstr','subcategories','errorstr','nexturl','searchfor', 'searchableSubCategories', 'sortlabel'));
16549 anikendra 224
		}else{
16583 anikendra 225
			//Check for apk support of sharing
226
			$sharable = 0;
227
			if(isset($_COOKIE['shareApps']) && !empty($_COOKIE['shareApps'])) {
228
				$sharable = 1;
17183 anikendra 229
			}			
16704 anikendra 230
			$url = $this->apihost."appOffers/1";
231
			$appOffers = $this->make_request($url,null);
232
			$this->set(compact('page','id','sharable','appOffers'));
16549 anikendra 233
			$this->render('viewapps');
13583 anikendra 234
		}
13532 anikendra 235
	}
236
 
16549 anikendra 237
	public function getapps($id=null){
238
		$this->layout = 'ajax';
239
		$page = $this->request->query('page');
240
		if(!isset($page)){
241
			$page = 1;
242
		}
243
		$this->loadModel('AppOffer');
244
		$this->AppOffer->recursive = -1;
245
		$options = array('conditions'=>array('offer_active'=>1,'show'=>1),'limit' => Configure::read('searchresultsperpage'),'page'=>$page);
246
		$this->Paginator->settings = $options;
247
		try{
248
			$appOffers = $this->Paginator->paginate('AppOffer');
249
		} catch (NotFoundException $e) {
250
	        //get current page
251
	        $page = $this->request->params['named']['page'];
252
	     	$appOffers = array();   
253
		}
254
		debug($appOffers);		
255
		if(!empty($appOffers)){
256
			$this->set(compact('page','id','appOffers'));
257
			$this->render('/Elements/appoffers');
258
		}else{
259
			$this->render('/Elements/nooffers');
260
		}		
261
	}
262
 
13579 anikendra 263
	public function getdeals($id = null) {
17695 naman 264
 
13808 anikendra 265
		$this->log('getdeal id '.$id,'api');
13583 anikendra 266
		$likedDeals = $disLikedDeals = array();
13579 anikendra 267
		$this->layout = 'ajax';
268
		$page = $this->request->query('page');
269
		if(!isset($page)){
270
			$page = 1;
271
		}
13808 anikendra 272
		$sort = $this->request->query('sort');
273
		$direction = $this->request->query('direction');
15044 anikendra 274
		// $filter = $this->request->query('filter');
275
		// $brands = $this->request->query('brands');
18390 naman 276
		$brands = $this->request->query('brands');
277
		$subcategories = $this->request->query('subcategories'); 	
17759 naman 278
 
18390 naman 279
		$likedDeals = $disLikedDeals = array();
280
		if(!empty($brands) && !empty($subcategories)){
17759 naman 281
			$filter = 'brand|subcategory';
18390 naman 282
		}
283
		else if(!empty($brands)){
15044 anikendra 284
			$filter = 'brand';
285
		}
18390 naman 286
		else if(!empty($subcategories)){
17759 naman 287
			$filter = 'subcategory';
288
		}
18390 naman 289
 
17759 naman 290
		$url = $this->getDealsApiUrl($page,$this->Auth->User('id'),$id,$sort,$direction,$filter,$brands,$subcategories);
16098 anikendra 291
		$response = $this->make_request($url,null);
292
		$deals = array();
293
		if(!empty($response)){
294
			foreach ($response as $key => $value) {
295
				if(!empty($value)){
296
					$deals[] = $value;
297
				}
298
			}
299
		}
13794 anikendra 300
		// if (!$this->Category->exists($id)) {
301
			// throw new NotFoundException(__('Invalid category'));
302
		// }
13579 anikendra 303
		$this->loadModel('Api');
13794 anikendra 304
		// $apideals = $this->Api->getDealsByCategory($this->Auth->User('id'),$id,$page);
305
		// $deals = $apideals['products'];
13583 anikendra 306
		$myactions = $this->Api->getMyActions($this->Auth->User('id'));
307
		if(!empty($myactions)) {
308
			foreach ($myactions['actions'] as $key => $value) {
309
				if($value['UserAction']['action'] == 'like'){
310
					$likedDeals[$value['UserAction']['store_product_id']] = $value['UserAction']['id'];
311
				}else{
312
					$disLikedDeals[$value['UserAction']['store_product_id']] = $value['UserAction']['id'];
313
				}
314
			}
315
		}
18390 naman 316
 
317
		$filterstr = '&brands='.$brands.'&subcategories='.$subcategories;
318
		$nexturl = "/categories/getdeals/".$id."/?page=".($page+1)."&sort=".$sort."&direction=".$direction."".$filterstr;
17695 naman 319
 
18063 naman 320
		$this->set(compact('nexturl','deals','id','page','likedDeals','disLikedDeals','sort','direction','brands','filter','filterstr'));
16098 anikendra 321
		if(!empty($deals) && !empty($deals[0])){
15026 anikendra 322
			$this->render('/Elements/deals');
323
		}else{
324
			$this->render('/Elements/nodeals');
325
		}
17695 naman 326
 
327
 
328
 
13579 anikendra 329
	}
18063 naman 330
 
18212 naman 331
	public function dealdetail($item_id = null){
19244 amit.gupta 332
		setcookie("fresh", '0', null, '/');
18212 naman 333
		$this->layout = "innerpages";
18223 naman 334
		$dealinfo = $deal = array();
335
		$likedDeals = $disLikedDeals = array();
336
		$url = $this->mobileapi.'entity/'.$item_id;
337
		$dealinfo = $this->make_request($url,null);
338
		$this->loadModel('Api');
339
		$myactions = $this->Api->getMyActions($this->Auth->User('id'));
340
		if(!empty($myactions)) {
341
			foreach ($myactions['actions'] as $key => $value) {
342
				if($value['UserAction']['action'] == 'like'){
343
					$likedDeals[$value['UserAction']['store_product_id']] = $value['UserAction']['id'];
344
				}else{
345
					$disLikedDeals[$value['UserAction']['store_product_id']] = $value['UserAction']['id'];
346
				}
347
			}
348
		}
349
		$this->set(compact('deal','dealinfo','likedDeals','disLikedDeals'));
18212 naman 350
	}
351
 
18288 naman 352
	public function saholicdeal($proid = null){
353
		$likedDeals = $disLikedDeals = array();
354
		$this->layout = 'ajax';
355
		$deal = array();
356
		$url = $this->apihost."getDealById/".$proid;
357
		$deal = $this->make_request($url,null);
358
		$this->loadModel('Api');
359
 
360
		$myactions = $this->Api->getMyActions($this->Auth->User('id'));
361
		if(!empty($myactions)) {
362
			foreach ($myactions['actions'] as $key => $value) {
363
				if($value['UserAction']['action'] == 'like'){
364
					$likedDeals[$value['UserAction']['store_product_id']] = $value['UserAction']['id'];
365
				}else{
366
					$disLikedDeals[$value['UserAction']['store_product_id']] = $value['UserAction']['id'];
367
				}
368
			}
369
		}
370
 
371
		$this->set(compact('deal','likedDeals','disLikedDeals'));
372
 
373
		$this->render('/Elements/saholicdeal');
374
 
375
 
376
	}
377
 
18063 naman 378
	public function getdealsforsearchterm($searchterm,$page = null){
379
		$likedDeals = $disLikedDeals = array();
380
		$this->layout = 'ajax';
381
		$page = $this->request->query('page');
18390 naman 382
		$subcategories = $this->request->query('subcategories'); 
18063 naman 383
		if(!isset($page)){
384
			$page = 1;
385
			$offset = 0;
386
			$limit=10;
387
		}
388
		else{
389
			$offset = ($page*20) - 30;
390
			$limit = 20;
391
		}
392
		$sort = $this->request->query('sort');
393
		$direction = $this->request->query('direction');
394
 
18390 naman 395
		$url = $this->apihost."searchSubCategory/?offset=".$offset."&limit=".$limit."&searchTerm=".urlencode($searchterm)."&subCategoryId=".$subcategories;
18063 naman 396
		$response = $this->make_request($url,null);
397
		$deals = array();
398
		if(!empty($response)){
399
			foreach ($response as $key => $value) {
400
				if(!empty($value)){
401
					$deals[] = $value;
402
				}
403
			}
404
		}
18115 amit.gupta 405
 
18063 naman 406
		$this->loadModel('Api');
18115 amit.gupta 407
 
18063 naman 408
		$myactions = $this->Api->getMyActions($this->Auth->User('id'));
409
		if(!empty($myactions)) {
410
			foreach ($myactions['actions'] as $key => $value) {
411
				if($value['UserAction']['action'] == 'like'){
412
					$likedDeals[$value['UserAction']['store_product_id']] = $value['UserAction']['id'];
413
				}else{
414
					$disLikedDeals[$value['UserAction']['store_product_id']] = $value['UserAction']['id'];
415
				}
416
			}
417
		}
418
 
18390 naman 419
		$nexturl = "/categories/getdealsforsearchterm/".urlencode($searchterm)."/?page=".($page+1).'&subcategories='.$subcategories;
18063 naman 420
 
421
		$this->set(compact('deals','id','page','likedDeals','disLikedDeals','sort','direction','brands','filter','nexturl'));
422
		if(!empty($deals) && !empty($deals[0])){
423
			$this->render('/Elements/deals');
424
		}else{
425
			$this->render('/Elements/nodeals');
426
		}
427
 
428
 
429
	}
13532 anikendra 430
/**
431
 * add method
432
 *
433
 * @return void
434
 */
435
	public function add() {
436
		if ($this->request->is('post')) {
437
			$this->Category->create();
438
			if ($this->Category->save($this->request->data)) {
439
				$this->Session->setFlash(__('The category has been saved.'));
440
				return $this->redirect(array('action' => 'index'));
441
			} else {
442
				$this->Session->setFlash(__('The category could not be saved. Please, try again.'));
443
			}
444
		}
445
	}
446
 
447
/**
448
 * edit method
449
 *
450
 * @throws NotFoundException
451
 * @param string $id
452
 * @return void
453
 */
454
	public function edit($id = null) {
455
		if (!$this->Category->exists($id)) {
456
			throw new NotFoundException(__('Invalid category'));
457
		}
458
		if ($this->request->is(array('post', 'put'))) {
459
			if ($this->Category->save($this->request->data)) {
460
				$this->Session->setFlash(__('The category has been saved.'));
461
				return $this->redirect(array('action' => 'index'));
462
			} else {
463
				$this->Session->setFlash(__('The category could not be saved. Please, try again.'));
464
			}
465
		} else {
466
			$options = array('conditions' => array('Category.' . $this->Category->primaryKey => $id));
467
			$this->request->data = $this->Category->find('first', $options);
468
		}
469
	}
470
 
471
/**
472
 * delete method
473
 *
474
 * @throws NotFoundException
475
 * @param string $id
476
 * @return void
477
 */
478
	public function delete($id = null) {
479
		$this->Category->id = $id;
480
		if (!$this->Category->exists()) {
481
			throw new NotFoundException(__('Invalid category'));
482
		}
483
		$this->request->onlyAllow('post', 'delete');
484
		if ($this->Category->delete()) {
485
			$this->Session->setFlash(__('The category has been deleted.'));
486
		} else {
487
			$this->Session->setFlash(__('The category could not be deleted. Please, try again.'));
488
		}
489
		return $this->redirect(array('action' => 'index'));
490
	}
491
 
492
/**
493
 * admin_index method
494
 *
495
 * @return void
496
 */
497
	public function admin_index() {
498
		$this->Category->recursive = 0;
499
		$this->set('categories', $this->Paginator->paginate());
500
	}
501
 
502
/**
503
 * admin_view method
504
 *
505
 * @throws NotFoundException
506
 * @param string $id
507
 * @return void
508
 */
509
	public function admin_view($id = null) {
510
		if (!$this->Category->exists($id)) {
511
			throw new NotFoundException(__('Invalid category'));
512
		}
513
		$options = array('conditions' => array('Category.' . $this->Category->primaryKey => $id));
514
		$this->set('category', $this->Category->find('first', $options));
515
	}
516
 
517
/**
518
 * admin_add method
519
 *
520
 * @return void
521
 */
522
	public function admin_add() {
523
		if ($this->request->is('post')) {
524
			// print_r($this->request->data);die;
525
			$this->Category->create();
526
			if ($this->Category->save($this->request->data)) {
527
				$this->Session->setFlash(__('The category has been saved.'));
528
				return $this->redirect(array('action' => 'index'));
529
			} else {
530
				$this->Session->setFlash(__('The category could not be saved. Please, try again.'));
531
			}
532
		}
533
		$this->set('parent_id',$this->Category->find('list'));
534
	}
535
 
536
/**
537
 * admin_edit method
538
 *
539
 * @throws NotFoundException
540
 * @param string $id
541
 * @return void
542
 */
543
	public function admin_edit($id = null) {
544
		if (!$this->Category->exists($id)) {
545
			throw new NotFoundException(__('Invalid category'));
546
		}
547
		if ($this->request->is(array('post', 'put'))) {
548
			if ($this->Category->save($this->request->data)) {
549
				$this->Session->setFlash(__('The category has been saved.'));
550
				return $this->redirect(array('action' => 'index'));
551
			} else {
552
				$this->Session->setFlash(__('The category could not be saved. Please, try again.'));
553
			}
554
		} else {
555
			$options = array('conditions' => array('Category.' . $this->Category->primaryKey => $id));
556
			$this->request->data = $this->Category->find('first', $options);
557
		}
558
	}
559
 
560
/**
561
 * admin_delete method
562
 *
563
 * @throws NotFoundException
564
 * @param string $id
565
 * @return void
566
 */
567
	public function admin_delete($id = null) {
568
		$this->Category->id = $id;
569
		if (!$this->Category->exists()) {
570
			throw new NotFoundException(__('Invalid category'));
571
		}
572
		$this->request->onlyAllow('post', 'delete');
573
		if ($this->Category->delete()) {
574
			$this->Session->setFlash(__('The category has been deleted.'));
575
		} else {
576
			$this->Session->setFlash(__('The category could not be deleted. Please, try again.'));
577
		}
578
		return $this->redirect(array('action' => 'index'));
579
	}}