Subversion Repositories SmartDukaan

Rev

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