Subversion Repositories SmartDukaan

Rev

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