Subversion Repositories SmartDukaan

Rev

Rev 19941 | Rev 20010 | 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) {
19609 naman 124
		$arrSubCategory = Configure::read('arrSubCategory');
18393 amit.gupta 125
		$userId = $this->request->query('user_id');
19991 naman 126
		$dealpin = $this->getpin();
127
// 		$this->set(compact('dealpin'));
128
 
15378 anikendra 129
		if(isset($userId) && !empty($userId)) {
14930 anikendra 130
			$this->loadModel('User');
131
			$dbuser = $this->User->findById($userId);
132
			$this->Auth->login($dbuser['User']);
19829 naman 133
		}
19941 naman 134
		$version = $this->request->query('version');
135
		if(isset($version) && !isset($_COOKIE['version'])){
136
				setcookie('version', $version, time() + (86400 * 30), "/");
137
		}
138
 
139
 
19829 naman 140
		$color = array('0'=>'#458ccc','1'=>'#44cbbc','2'=>'#a77189', '3'=>'#f9b931' ,'4' =>'#44cbbc', '5' => '#f48f3f' , '6'=>'#a77189');
19345 naman 141
 
18390 naman 142
		//Sort - type, ordering
143
		$sort = $this->request->query('sort');
144
		$direction = $this->request->query('direction');
145
 
146
		//Filter - brand/subcategory		
147
		$brands = $this->request->query('brands');
148
		$subcategories = $this->request->query('subcategories');
149
 
19543 amit.gupta 150
		$searchableSubCategories = array('20'=>'Screen Guard', '27'=>'Back Cover', '29'=>'Batteries', '33'=>'Flip Cover', '28'=>'Tempered Glass');
18390 naman 151
 
19609 naman 152
		$subcatlist = Configure::read('arrSubCategory');
153
 
154
		$whatfirst = $this->request->query('whatfirst');
19640 naman 155
 
19622 naman 156
		if($id == 6)
157
		{
19644 naman 158
 
159
			if(isset($brands) && !empty($brands) && !isset($subcategories) && !isset($whatfirst)){
160
				$whatfirst = "brand";
161
			}
162
			else if(isset($subcategories) && !empty($subcategories) && !isset($brands) && !isset($whatfirst)){
163
				$whatfirst = "subCategory";
164
			}else if(isset($subcategories) && !empty($subcategories) && isset($brands) && !empty($brands) && !isset($whatfirst)){
165
				$whatfirst = "subCategory";
166
			}
167
 
19622 naman 168
			$cachekey = 'subcat-6';
169
			$getdet = Cache::read($cachekey,'subcatdetail');
170
			if(empty($getdet) || $getdet === false){
19657 naman 171
				$url = $this->apihost."getHeaderLinks/?category_id=6";
19622 naman 172
				$getdet = $this->make_request($url,null);
173
				Cache::write($cachekey , $getdet ,'subcatdetail');
174
			}
175
			$this->set(compact('getdet'));
176
		}
177
 
19609 naman 178
		$whatsecond = "";
179
		$whatsecondid = "";
180
		$whatfirstid = "";
181
		$secondloop = "";
182
		if(isset($whatfirst)){
183
			$brandprourl = "";
184
			if($whatfirst == "subCategory"){
185
				$secondloop = explode("^",$brands);
186
				$whatsecond = "brand";
187
				$whatsecondid = "brand_id";
188
				$whatfirstid = "subCategoryId";
189
				$brandprourl = $this->apihost."getBrandSubCategoryInfo/?category_id=6&type=subCategoryInfo&id_list=".$subcategories;
190
			}
191
			else if($whatfirst == "brand"){
192
 
193
				$secondloop = explode("^",$subcategories);
194
				$whatsecond = "subCategory";
195
				$whatsecondid = "subCategoryId";
196
				$whatfirstid = "brand_id";
197
				$brandprourl = $this->apihost."getBrandSubCategoryInfo/?category_id=6&type=brandInfo&id_list=".$brands;
198
			}
199
			$allbrandsubcat = $this->make_request($brandprourl, null);
200
			$this->set(compact('secondloop','whatfirstid','whatsecondid','subcatlist','whatfirst','whatsecond','allbrandsubcat'));
201
		}
202
 
203
 
13583 anikendra 204
		$page = $this->request->query('page');
18063 naman 205
		$searchfor = $this->request->query('searchFor');
206
		// echo "page=>",$page;
17810 manish.sha 207
		$error = $this->request->query('error');
13583 anikendra 208
		if(!isset($page)){
209
			$page = 1;
16549 anikendra 210
		}
211
 
18207 amit.gupta 212
		//$filter = $this->request->query('filter');
15044 anikendra 213
		// $brands = $this->request->query('brands');
16549 anikendra 214
		if($id != 2) {
19829 naman 215
			if($id != 6){
19834 naman 216
				$url = $this->apihost.'deals/brands/?category_id='.$id;
19829 naman 217
				$brandres = $this->make_request($url,null);
218
				$brandlist = array();
219
				foreach ($brandres as $bkey => $bvalue){
220
					$brandlist[$bvalue['brand_id']] = $bvalue['brand'];
221
				}
222
				$this->set(compact('brandlist'));
223
			}
16549 anikendra 224
			//Fetch deals
225
			$likedDeals = $disLikedDeals = array();
18390 naman 226
			if(!empty($brands) && !empty($subcategories)){
227
				$filter = 'brand|subcategory';
228
			}			
229
			else if(!empty($brands)){
230
				$filter = 'brand';
231
			}
232
			else if(!empty($subcategories)){
233
				$filter = 'subcategory';
234
			}
17759 naman 235
 
18390 naman 236
			$sortlabel = 'recommended';
237
			if	($sort=='bestSellerPoints'){
238
				$sortlabel  = 'bestseller';
18063 naman 239
			}
18390 naman 240
			else if ($sort=='available_price' && $direction==-1){
241
				$sortlabel  = 'pricehigh';
16549 anikendra 242
			}
18390 naman 243
			else if ($sort=='available_price' && $direction==1){
244
				$sortlabel  = 'pricelow';
245
			}
17683 naman 246
 
247
			$url = $this->getDealsApiUrl($page,$this->Auth->User('id'),$id,$sort,$direction,$filter,$brands,$subcategories);
19991 naman 248
			$url = $url."&pin=".$dealpin;
16549 anikendra 249
			$response = $this->make_request($url,null);
250
			$deals = array();
17683 naman 251
 
252
			$response_count =1;
253
			if($response == '')
254
			{
255
				$response_count = 0;
256
			}
257
			// debug($response_count);
16549 anikendra 258
			if(!empty($response)){
259
				foreach ($response as $key => $value) {
260
					if(!empty($value)){
261
						$deals[] = $value;
262
					}
16098 anikendra 263
				}
264
			}
17683 naman 265
			#print_r($deals);
16549 anikendra 266
			$this->loadModel('Api');
267
			$myactions = $this->Api->getMyActions($this->Auth->User('id'));
268
			if(!empty($myactions)) {
269
				foreach ($myactions['actions'] as $key => $value) {
270
					if($value['UserAction']['action'] == 'like'){
271
						$likedDeals[$value['UserAction']['store_product_id']] = $value['UserAction']['id'];
272
					}else{
273
						$disLikedDeals[$value['UserAction']['store_product_id']] = $value['UserAction']['id'];
274
					}
13583 anikendra 275
				}
276
			}
16549 anikendra 277
			$this->loadModel('NotificationRule');
278
			$notification = $this->NotificationRule->getNotification($this->Auth->User('id'));	
279
		    $filterstr = '';
280
			if(isset($filter) && !empty($filter)){
17759 naman 281
 
17683 naman 282
			    $filterstr = '&filter='.$filter.'&brands='.$brands.'&subcategories='.$subcategories;
17695 naman 283
		  	} 
284
		  	$get_url = "'".$_SERVER['REQUEST_URI']."'";
285
		  	$urlArray = explode('=',$_SERVER['REQUEST_URI']);
286
		  	$last = $urlArray[sizeof($urlArray)-1];
287
 
17810 manish.sha 288
			$errorstr = '';
289
 
290
			if(isset($error)){
291
				$errorstr = 'Oops!! Some Error Occured. <br> Please try after Some Time';
292
			}
17759 naman 293
 
18390 naman 294
			$filterstr = '&brands='.$brands.'&subcategories='.$subcategories;
295
			$nexturl = "/categories/getdeals/".$id."/?page=".($page+1)."&sort=".$sort."&direction=".$direction."".$filterstr;			
18063 naman 296
 
19609 naman 297
			$offerresponse = $this->getuseroffer();
19923 naman 298
			$clearancesale = Configure::read('clearancesale');
299
			$this->set(compact('clearancesale','color','arrSubCategory','offerresponse','response_count','deals','id','likedDeals','disLikedDeals','page','sort','direction','notification','filter','brands','filterstr','subcategories','errorstr','nexturl','searchfor', 'searchableSubCategories', 'sortlabel'));
16549 anikendra 300
		}else{
16583 anikendra 301
			//Check for apk support of sharing
302
			$sharable = 0;
303
			if(isset($_COOKIE['shareApps']) && !empty($_COOKIE['shareApps'])) {
304
				$sharable = 1;
17183 anikendra 305
			}			
16704 anikendra 306
			$url = $this->apihost."appOffers/1";
307
			$appOffers = $this->make_request($url,null);
308
			$this->set(compact('page','id','sharable','appOffers'));
16549 anikendra 309
			$this->render('viewapps');
13583 anikendra 310
		}
13532 anikendra 311
	}
312
 
16549 anikendra 313
	public function getapps($id=null){
314
		$this->layout = 'ajax';
315
		$page = $this->request->query('page');
316
		if(!isset($page)){
317
			$page = 1;
318
		}
319
		$this->loadModel('AppOffer');
320
		$this->AppOffer->recursive = -1;
321
		$options = array('conditions'=>array('offer_active'=>1,'show'=>1),'limit' => Configure::read('searchresultsperpage'),'page'=>$page);
322
		$this->Paginator->settings = $options;
323
		try{
324
			$appOffers = $this->Paginator->paginate('AppOffer');
325
		} catch (NotFoundException $e) {
326
	        //get current page
327
	        $page = $this->request->params['named']['page'];
328
	     	$appOffers = array();   
329
		}
330
		debug($appOffers);		
331
		if(!empty($appOffers)){
332
			$this->set(compact('page','id','appOffers'));
333
			$this->render('/Elements/appoffers');
334
		}else{
335
			$this->render('/Elements/nooffers');
336
		}		
337
	}
338
 
13579 anikendra 339
	public function getdeals($id = null) {
17695 naman 340
 
13808 anikendra 341
		$this->log('getdeal id '.$id,'api');
13583 anikendra 342
		$likedDeals = $disLikedDeals = array();
13579 anikendra 343
		$this->layout = 'ajax';
344
		$page = $this->request->query('page');
345
		if(!isset($page)){
346
			$page = 1;
347
		}
19991 naman 348
		$dealpin = $this->getpin();
13808 anikendra 349
		$sort = $this->request->query('sort');
350
		$direction = $this->request->query('direction');
15044 anikendra 351
		// $filter = $this->request->query('filter');
352
		// $brands = $this->request->query('brands');
18390 naman 353
		$brands = $this->request->query('brands');
354
		$subcategories = $this->request->query('subcategories'); 	
17759 naman 355
 
18390 naman 356
		$likedDeals = $disLikedDeals = array();
357
		if(!empty($brands) && !empty($subcategories)){
17759 naman 358
			$filter = 'brand|subcategory';
18390 naman 359
		}
360
		else if(!empty($brands)){
15044 anikendra 361
			$filter = 'brand';
362
		}
18390 naman 363
		else if(!empty($subcategories)){
17759 naman 364
			$filter = 'subcategory';
365
		}
18390 naman 366
 
17759 naman 367
		$url = $this->getDealsApiUrl($page,$this->Auth->User('id'),$id,$sort,$direction,$filter,$brands,$subcategories);
19991 naman 368
		$url = $url."&pin=".$dealpin;
16098 anikendra 369
		$response = $this->make_request($url,null);
370
		$deals = array();
371
		if(!empty($response)){
372
			foreach ($response as $key => $value) {
373
				if(!empty($value)){
374
					$deals[] = $value;
375
				}
376
			}
377
		}
13794 anikendra 378
		// if (!$this->Category->exists($id)) {
379
			// throw new NotFoundException(__('Invalid category'));
380
		// }
13579 anikendra 381
		$this->loadModel('Api');
13794 anikendra 382
		// $apideals = $this->Api->getDealsByCategory($this->Auth->User('id'),$id,$page);
383
		// $deals = $apideals['products'];
13583 anikendra 384
		$myactions = $this->Api->getMyActions($this->Auth->User('id'));
385
		if(!empty($myactions)) {
386
			foreach ($myactions['actions'] as $key => $value) {
387
				if($value['UserAction']['action'] == 'like'){
388
					$likedDeals[$value['UserAction']['store_product_id']] = $value['UserAction']['id'];
389
				}else{
390
					$disLikedDeals[$value['UserAction']['store_product_id']] = $value['UserAction']['id'];
391
				}
392
			}
393
		}
18390 naman 394
 
395
		$filterstr = '&brands='.$brands.'&subcategories='.$subcategories;
396
		$nexturl = "/categories/getdeals/".$id."/?page=".($page+1)."&sort=".$sort."&direction=".$direction."".$filterstr;
19306 naman 397
 
19609 naman 398
		$offerresponse = $this->getuseroffer();
19306 naman 399
 
19609 naman 400
		$this->set(compact('offerresponse','nexturl','deals','id','page','likedDeals','disLikedDeals','sort','direction','brands','filter','filterstr'));
16098 anikendra 401
		if(!empty($deals) && !empty($deals[0])){
15026 anikendra 402
			$this->render('/Elements/deals');
403
		}else{
404
			$this->render('/Elements/nodeals');
405
		}
17695 naman 406
 
407
 
408
 
13579 anikendra 409
	}
19904 naman 410
 
411
	public function linkdeal($id = null) {
412
		$filtertype = $this->request->query('filtertype');
413
		$page = $this->request->query('page');
414
		if(!isset($page)){
415
			$page = 1;
416
		}
417
		$url = $this->apihost."getDealsByType?categoryId=".$id."&offset=0&limit=20&type=".$filtertype;
418
		$response = $this->make_request($url,null);
419
		$deals = array();
420
 
421
		$response_count =1;
422
		if($response == '')
423
		{
424
			$response_count = 0;
425
		}
18063 naman 426
 
19904 naman 427
		if(!empty($response)){
428
			foreach ($response as $key => $value) {
429
				if(!empty($value)){
430
					$deals[] = $value;
431
				}
432
			}
433
		}
434
 
435
		$this->loadModel('Api');
436
		$myactions = $this->Api->getMyActions($this->Auth->User('id'));
437
		if(!empty($myactions)) {
438
			foreach ($myactions['actions'] as $key => $value) {
439
				if($value['UserAction']['action'] == 'like'){
440
					$likedDeals[$value['UserAction']['store_product_id']] = $value['UserAction']['id'];
441
				}else{
442
					$disLikedDeals[$value['UserAction']['store_product_id']] = $value['UserAction']['id'];
443
				}
444
			}
445
		}
446
 
447
 
448
		$nexturl = "/categories/getlinkdeals/".$id."?page=".($page+1)."&filtertype=".$filtertype;			
449
		$offerresponse = $this->getuseroffer();
450
 
451
		$this->set(compact('offerresponse','response_count','deals','id','likedDeals','disLikedDeals','page','nexturl'));
452
	}
453
 
454
 
455
	public function getlinkdeals($id = null) {
456
 
457
		$likedDeals = $disLikedDeals = array();
458
		$this->layout = 'ajax';
459
		$page = $this->request->query('page');
460
		$filtertype = $this->request->query('filtertype');
461
		if(!isset($page)){
462
			$page = 1;
463
		}
464
		$limit = 20;
465
		$offset = ($page - 1)*$limit;
466
 
467
		$likedDeals = $disLikedDeals = array();
468
		$url = $this->apihost."getDealsByType?categoryId=".$id."&type=".$filtertype."&limit=".$limit."&offset=".$offset;
469
		$response = $this->make_request($url,null);
470
		$deals = array();
471
		if(!empty($response)){
472
			foreach ($response as $key => $value) {
473
				if(!empty($value)){
474
					$deals[] = $value;
475
				}
476
			}
477
		}
478
 
479
		$this->loadModel('Api');
480
 
481
		$myactions = $this->Api->getMyActions($this->Auth->User('id'));
482
		if(!empty($myactions)) {
483
			foreach ($myactions['actions'] as $key => $value) {
484
				if($value['UserAction']['action'] == 'like'){
485
					$likedDeals[$value['UserAction']['store_product_id']] = $value['UserAction']['id'];
486
				}else{
487
					$disLikedDeals[$value['UserAction']['store_product_id']] = $value['UserAction']['id'];
488
				}
489
			}
490
		}
491
 
492
		$nexturl = "/categories/getlinkdeals/".$id."/?page=".($page+1)."&filtertype=".$filtertype;
493
 
494
		$offerresponse = $this->getuseroffer();
495
 
496
		$this->set(compact('offerresponse','nexturl','deals','id','page','likedDeals','disLikedDeals'));
497
		if(!empty($deals) && !empty($deals[0])){
498
			$this->render('/Elements/deals');
499
		}else{
500
			$this->render('/Elements/nodeals');
501
		}
502
	}
503
 
504
 
18212 naman 505
	public function dealdetail($item_id = null){
19244 amit.gupta 506
		setcookie("fresh", '0', null, '/');
18212 naman 507
		$this->layout = "innerpages";
18223 naman 508
		$dealinfo = $deal = array();
509
		$likedDeals = $disLikedDeals = array();
510
		$url = $this->mobileapi.'entity/'.$item_id;
511
		$dealinfo = $this->make_request($url,null);
512
		$this->loadModel('Api');
513
		$myactions = $this->Api->getMyActions($this->Auth->User('id'));
514
		if(!empty($myactions)) {
515
			foreach ($myactions['actions'] as $key => $value) {
516
				if($value['UserAction']['action'] == 'like'){
517
					$likedDeals[$value['UserAction']['store_product_id']] = $value['UserAction']['id'];
518
				}else{
519
					$disLikedDeals[$value['UserAction']['store_product_id']] = $value['UserAction']['id'];
520
				}
521
			}
522
		}
523
		$this->set(compact('deal','dealinfo','likedDeals','disLikedDeals'));
18212 naman 524
	}
525
 
18288 naman 526
	public function saholicdeal($proid = null){
527
		$likedDeals = $disLikedDeals = array();
528
		$this->layout = 'ajax';
529
		$deal = array();
530
		$url = $this->apihost."getDealById/".$proid;
531
		$deal = $this->make_request($url,null);
532
		$this->loadModel('Api');
533
 
534
		$myactions = $this->Api->getMyActions($this->Auth->User('id'));
535
		if(!empty($myactions)) {
536
			foreach ($myactions['actions'] as $key => $value) {
537
				if($value['UserAction']['action'] == 'like'){
538
					$likedDeals[$value['UserAction']['store_product_id']] = $value['UserAction']['id'];
539
				}else{
540
					$disLikedDeals[$value['UserAction']['store_product_id']] = $value['UserAction']['id'];
541
				}
542
			}
543
		}
544
 
19609 naman 545
		$offerresponse = $this->getuseroffer();
18288 naman 546
 
19609 naman 547
		$this->set(compact('offerresponse','deal','likedDeals','disLikedDeals'));
19327 naman 548
 
18288 naman 549
		$this->render('/Elements/saholicdeal');
550
 
551
 
552
	}
553
 
18063 naman 554
	public function getdealsforsearchterm($searchterm,$page = null){
555
		$likedDeals = $disLikedDeals = array();
556
		$this->layout = 'ajax';
557
		$page = $this->request->query('page');
18390 naman 558
		$subcategories = $this->request->query('subcategories'); 
18063 naman 559
		if(!isset($page)){
560
			$page = 1;
561
			$offset = 0;
562
			$limit=10;
563
		}
564
		else{
565
			$offset = ($page*20) - 30;
566
			$limit = 20;
567
		}
568
		$sort = $this->request->query('sort');
569
		$direction = $this->request->query('direction');
570
 
18390 naman 571
		$url = $this->apihost."searchSubCategory/?offset=".$offset."&limit=".$limit."&searchTerm=".urlencode($searchterm)."&subCategoryId=".$subcategories;
18063 naman 572
		$response = $this->make_request($url,null);
573
		$deals = array();
574
		if(!empty($response)){
575
			foreach ($response as $key => $value) {
576
				if(!empty($value)){
577
					$deals[] = $value;
578
				}
579
			}
580
		}
18115 amit.gupta 581
 
18063 naman 582
		$this->loadModel('Api');
18115 amit.gupta 583
 
18063 naman 584
		$myactions = $this->Api->getMyActions($this->Auth->User('id'));
585
		if(!empty($myactions)) {
586
			foreach ($myactions['actions'] as $key => $value) {
587
				if($value['UserAction']['action'] == 'like'){
588
					$likedDeals[$value['UserAction']['store_product_id']] = $value['UserAction']['id'];
589
				}else{
590
					$disLikedDeals[$value['UserAction']['store_product_id']] = $value['UserAction']['id'];
591
				}
592
			}
593
		}
594
 
18390 naman 595
		$nexturl = "/categories/getdealsforsearchterm/".urlencode($searchterm)."/?page=".($page+1).'&subcategories='.$subcategories;
19327 naman 596
 
19609 naman 597
		$offerresponse = $this->getuseroffer(); 
19327 naman 598
 
19609 naman 599
		$this->set(compact('offerresponse','deals','id','page','likedDeals','disLikedDeals','sort','direction','brands','filter','nexturl'));
18063 naman 600
		if(!empty($deals) && !empty($deals[0])){
601
			$this->render('/Elements/deals');
602
		}else{
603
			$this->render('/Elements/nodeals');
604
		}
605
 
606
 
607
	}
19306 naman 608
 
19345 naman 609
	public function target(){
19306 naman 610
		$this->layout = "innerpages";
19374 naman 611
		$user_id = $this->Auth->user('id');
19357 naman 612
 
19306 naman 613
		$cachekey = 'target-'.$user_id;
614
		$getoffer = Cache::read($cachekey,'target');
19333 naman 615
		$current_time = time();
19306 naman 616
		$response = "";
617
		if($getoffer === false){
19357 naman 618
			$offerurl = $this->apihost."getOfferForUser/?user_id=".$user_id;
19306 naman 619
			$response = $this->make_request($offerurl,null);
620
			Cache::write($cachekey , $response ,'target');
621
			if(!empty($response)){
19333 naman 622
 
19670 naman 623
// 								if($response['startDate']/1000 <= $current_time && $response['endDate']/1000 >= $current_time ){
19306 naman 624
 
19670 naman 625
// 								}
626
// 								else{
627
// 									$response = "";
628
// 									}
19306 naman 629
				}
630
				else{
19333 naman 631
					$response = "";
19306 naman 632
				}
633
		}else{
19333 naman 634
			if(!empty($getoffer)){
635
					$response = $getoffer;
19670 naman 636
// 					if($response['startDate']/1000 <= $current_time && $response['endDate']/1000 >= $current_time ){
19333 naman 637
 
19670 naman 638
// 					}
639
// 					else{
640
// 						$response = "";
641
// 					}
19333 naman 642
				}
19306 naman 643
		}
644
 
645
		$maxpercentage = 0;
19357 naman 646
 
19306 naman 647
		if(isset($response['target2_cash_back_percetage']) && !empty($response['target2_cash_back_percetage'])){
648
			$maxpercentage = $response['target2_cash_back_percetage'];
649
		}
650
		else{
651
			if(isset($response['target1_cash_back_percetage']) && !empty($response['target1_cash_back_percetage'])){
652
				$maxpercentage = $response['target1_cash_back_percetage'];
653
			}
654
		}
655
 
19357 naman 656
 
657
		$this->loadModel('User');
658
		$user_email = "";
19345 naman 659
 
19374 naman 660
		$user_email = $this->Auth->user('email');
19357 naman 661
		$this->set(compact('response','maxpercentage','user_email'));
19306 naman 662
	}
13532 anikendra 663
/**
664
 * add method
665
 *
666
 * @return void
667
 */
668
	public function add() {
669
		if ($this->request->is('post')) {
670
			$this->Category->create();
671
			if ($this->Category->save($this->request->data)) {
672
				$this->Session->setFlash(__('The category has been saved.'));
673
				return $this->redirect(array('action' => 'index'));
674
			} else {
675
				$this->Session->setFlash(__('The category could not be saved. Please, try again.'));
676
			}
677
		}
678
	}
679
 
680
/**
681
 * edit method
682
 *
683
 * @throws NotFoundException
684
 * @param string $id
685
 * @return void
686
 */
687
	public function edit($id = null) {
688
		if (!$this->Category->exists($id)) {
689
			throw new NotFoundException(__('Invalid category'));
690
		}
691
		if ($this->request->is(array('post', 'put'))) {
692
			if ($this->Category->save($this->request->data)) {
693
				$this->Session->setFlash(__('The category has been saved.'));
694
				return $this->redirect(array('action' => 'index'));
695
			} else {
696
				$this->Session->setFlash(__('The category could not be saved. Please, try again.'));
697
			}
698
		} else {
699
			$options = array('conditions' => array('Category.' . $this->Category->primaryKey => $id));
700
			$this->request->data = $this->Category->find('first', $options);
701
		}
702
	}
703
 
704
/**
705
 * delete method
706
 *
707
 * @throws NotFoundException
708
 * @param string $id
709
 * @return void
710
 */
711
	public function delete($id = null) {
712
		$this->Category->id = $id;
713
		if (!$this->Category->exists()) {
714
			throw new NotFoundException(__('Invalid category'));
715
		}
716
		$this->request->onlyAllow('post', 'delete');
717
		if ($this->Category->delete()) {
718
			$this->Session->setFlash(__('The category has been deleted.'));
719
		} else {
720
			$this->Session->setFlash(__('The category could not be deleted. Please, try again.'));
721
		}
722
		return $this->redirect(array('action' => 'index'));
723
	}
724
 
725
/**
726
 * admin_index method
727
 *
728
 * @return void
729
 */
730
	public function admin_index() {
731
		$this->Category->recursive = 0;
732
		$this->set('categories', $this->Paginator->paginate());
733
	}
734
 
735
/**
736
 * admin_view method
737
 *
738
 * @throws NotFoundException
739
 * @param string $id
740
 * @return void
741
 */
742
	public function admin_view($id = null) {
743
		if (!$this->Category->exists($id)) {
744
			throw new NotFoundException(__('Invalid category'));
745
		}
746
		$options = array('conditions' => array('Category.' . $this->Category->primaryKey => $id));
747
		$this->set('category', $this->Category->find('first', $options));
748
	}
749
 
750
/**
751
 * admin_add method
752
 *
753
 * @return void
754
 */
755
	public function admin_add() {
756
		if ($this->request->is('post')) {
757
			// print_r($this->request->data);die;
758
			$this->Category->create();
759
			if ($this->Category->save($this->request->data)) {
760
				$this->Session->setFlash(__('The category has been saved.'));
761
				return $this->redirect(array('action' => 'index'));
762
			} else {
763
				$this->Session->setFlash(__('The category could not be saved. Please, try again.'));
764
			}
765
		}
766
		$this->set('parent_id',$this->Category->find('list'));
767
	}
768
 
769
/**
770
 * admin_edit method
771
 *
772
 * @throws NotFoundException
773
 * @param string $id
774
 * @return void
775
 */
776
	public function admin_edit($id = null) {
777
		if (!$this->Category->exists($id)) {
778
			throw new NotFoundException(__('Invalid category'));
779
		}
780
		if ($this->request->is(array('post', 'put'))) {
781
			if ($this->Category->save($this->request->data)) {
782
				$this->Session->setFlash(__('The category has been saved.'));
783
				return $this->redirect(array('action' => 'index'));
784
			} else {
785
				$this->Session->setFlash(__('The category could not be saved. Please, try again.'));
786
			}
787
		} else {
788
			$options = array('conditions' => array('Category.' . $this->Category->primaryKey => $id));
789
			$this->request->data = $this->Category->find('first', $options);
790
		}
791
	}
792
 
793
/**
794
 * admin_delete method
795
 *
796
 * @throws NotFoundException
797
 * @param string $id
798
 * @return void
799
 */
800
	public function admin_delete($id = null) {
801
		$this->Category->id = $id;
802
		if (!$this->Category->exists()) {
803
			throw new NotFoundException(__('Invalid category'));
804
		}
805
		$this->request->onlyAllow('post', 'delete');
806
		if ($this->Category->delete()) {
807
			$this->Session->setFlash(__('The category has been deleted.'));
808
		} else {
809
			$this->Session->setFlash(__('The category could not be deleted. Please, try again.'));
810
		}
811
		return $this->redirect(array('action' => 'index'));
19613 naman 812
	}}