Subversion Repositories SmartDukaan

Rev

Rev 19244 | Rev 19327 | 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
 
19306 naman 223
			//check for offer start
224
			$user_id = $this->Auth->user('id');
225
			$cachekey = 'target-'.$user_id;
226
			$getoffer = Cache::read($cachekey,'target');
227
 
228
			$offerresponse = "";
229
			if($getoffer === false){
230
				$offerurl = $this->apihost."getOfferForUser/?user_id=47";
231
				$offerresponse = $this->make_request($offerurl,null);
232
				Cache::write($cachekey , $offerresponse ,'target');
233
				if(!empty($offerresponse)){					
234
					$current_time = time();
235
								if($offerresponse['startDate']/1000 <= $current_time && $offerresponse['endDate']/1000 >= $current_time ){
236
 
237
								}
238
								else{
239
									$offerresponse = "";
240
								}
241
				}
242
				else{
243
					$offerresponse = "";
244
				}
245
			}else{
246
				$offerresponse = $getoffer;
247
			}
248
 
249
			//check for offer end 
250
 
251
 
252
			$this->set(compact('offerresponse','response_count','deals','id','likedDeals','disLikedDeals','page','sort','direction','notification','filter','brands','filterstr','subcategories','errorstr','nexturl','searchfor', 'searchableSubCategories', 'sortlabel'));
16549 anikendra 253
		}else{
16583 anikendra 254
			//Check for apk support of sharing
255
			$sharable = 0;
256
			if(isset($_COOKIE['shareApps']) && !empty($_COOKIE['shareApps'])) {
257
				$sharable = 1;
17183 anikendra 258
			}			
16704 anikendra 259
			$url = $this->apihost."appOffers/1";
260
			$appOffers = $this->make_request($url,null);
261
			$this->set(compact('page','id','sharable','appOffers'));
16549 anikendra 262
			$this->render('viewapps');
13583 anikendra 263
		}
13532 anikendra 264
	}
265
 
16549 anikendra 266
	public function getapps($id=null){
267
		$this->layout = 'ajax';
268
		$page = $this->request->query('page');
269
		if(!isset($page)){
270
			$page = 1;
271
		}
272
		$this->loadModel('AppOffer');
273
		$this->AppOffer->recursive = -1;
274
		$options = array('conditions'=>array('offer_active'=>1,'show'=>1),'limit' => Configure::read('searchresultsperpage'),'page'=>$page);
275
		$this->Paginator->settings = $options;
276
		try{
277
			$appOffers = $this->Paginator->paginate('AppOffer');
278
		} catch (NotFoundException $e) {
279
	        //get current page
280
	        $page = $this->request->params['named']['page'];
281
	     	$appOffers = array();   
282
		}
283
		debug($appOffers);		
284
		if(!empty($appOffers)){
285
			$this->set(compact('page','id','appOffers'));
286
			$this->render('/Elements/appoffers');
287
		}else{
288
			$this->render('/Elements/nooffers');
289
		}		
290
	}
291
 
13579 anikendra 292
	public function getdeals($id = null) {
17695 naman 293
 
13808 anikendra 294
		$this->log('getdeal id '.$id,'api');
13583 anikendra 295
		$likedDeals = $disLikedDeals = array();
13579 anikendra 296
		$this->layout = 'ajax';
297
		$page = $this->request->query('page');
298
		if(!isset($page)){
299
			$page = 1;
300
		}
13808 anikendra 301
		$sort = $this->request->query('sort');
302
		$direction = $this->request->query('direction');
15044 anikendra 303
		// $filter = $this->request->query('filter');
304
		// $brands = $this->request->query('brands');
18390 naman 305
		$brands = $this->request->query('brands');
306
		$subcategories = $this->request->query('subcategories'); 	
17759 naman 307
 
18390 naman 308
		$likedDeals = $disLikedDeals = array();
309
		if(!empty($brands) && !empty($subcategories)){
17759 naman 310
			$filter = 'brand|subcategory';
18390 naman 311
		}
312
		else if(!empty($brands)){
15044 anikendra 313
			$filter = 'brand';
314
		}
18390 naman 315
		else if(!empty($subcategories)){
17759 naman 316
			$filter = 'subcategory';
317
		}
18390 naman 318
 
17759 naman 319
		$url = $this->getDealsApiUrl($page,$this->Auth->User('id'),$id,$sort,$direction,$filter,$brands,$subcategories);
16098 anikendra 320
		$response = $this->make_request($url,null);
321
		$deals = array();
322
		if(!empty($response)){
323
			foreach ($response as $key => $value) {
324
				if(!empty($value)){
325
					$deals[] = $value;
326
				}
327
			}
328
		}
13794 anikendra 329
		// if (!$this->Category->exists($id)) {
330
			// throw new NotFoundException(__('Invalid category'));
331
		// }
13579 anikendra 332
		$this->loadModel('Api');
13794 anikendra 333
		// $apideals = $this->Api->getDealsByCategory($this->Auth->User('id'),$id,$page);
334
		// $deals = $apideals['products'];
13583 anikendra 335
		$myactions = $this->Api->getMyActions($this->Auth->User('id'));
336
		if(!empty($myactions)) {
337
			foreach ($myactions['actions'] as $key => $value) {
338
				if($value['UserAction']['action'] == 'like'){
339
					$likedDeals[$value['UserAction']['store_product_id']] = $value['UserAction']['id'];
340
				}else{
341
					$disLikedDeals[$value['UserAction']['store_product_id']] = $value['UserAction']['id'];
342
				}
343
			}
344
		}
18390 naman 345
 
346
		$filterstr = '&brands='.$brands.'&subcategories='.$subcategories;
347
		$nexturl = "/categories/getdeals/".$id."/?page=".($page+1)."&sort=".$sort."&direction=".$direction."".$filterstr;
19306 naman 348
 
349
		//check for offer start
350
		$offerurl = $this->apihost."getOfferForUser/?user_id=47";
351
		$offerresponse = $this->make_request($offerurl,null);
352
		Cache::write('target', $offerresponse);
353
		$current_time = time();
354
		if(!empty($offerresponse)){
355
			if($offerresponse['startDate']/1000 <= $current_time && $offerresponse['endDate']/1000 >= $current_time ){
356
				Cache::write('target', $offerresponse);
357
			}
358
			else{
359
				$offerresponse = "";
360
			}
361
		}
362
		else{
363
			$offerresponse = "";
364
		}
365
		//check for offer end
366
 
367
		$this->set(compact('offerresponse','nexturl','deals','id','page','likedDeals','disLikedDeals','sort','direction','brands','filter','filterstr'));
16098 anikendra 368
		if(!empty($deals) && !empty($deals[0])){
15026 anikendra 369
			$this->render('/Elements/deals');
370
		}else{
371
			$this->render('/Elements/nodeals');
372
		}
17695 naman 373
 
374
 
375
 
13579 anikendra 376
	}
18063 naman 377
 
18212 naman 378
	public function dealdetail($item_id = null){
19244 amit.gupta 379
		setcookie("fresh", '0', null, '/');
18212 naman 380
		$this->layout = "innerpages";
18223 naman 381
		$dealinfo = $deal = array();
382
		$likedDeals = $disLikedDeals = array();
383
		$url = $this->mobileapi.'entity/'.$item_id;
384
		$dealinfo = $this->make_request($url,null);
385
		$this->loadModel('Api');
386
		$myactions = $this->Api->getMyActions($this->Auth->User('id'));
387
		if(!empty($myactions)) {
388
			foreach ($myactions['actions'] as $key => $value) {
389
				if($value['UserAction']['action'] == 'like'){
390
					$likedDeals[$value['UserAction']['store_product_id']] = $value['UserAction']['id'];
391
				}else{
392
					$disLikedDeals[$value['UserAction']['store_product_id']] = $value['UserAction']['id'];
393
				}
394
			}
395
		}
396
		$this->set(compact('deal','dealinfo','likedDeals','disLikedDeals'));
18212 naman 397
	}
398
 
18288 naman 399
	public function saholicdeal($proid = null){
400
		$likedDeals = $disLikedDeals = array();
401
		$this->layout = 'ajax';
402
		$deal = array();
403
		$url = $this->apihost."getDealById/".$proid;
404
		$deal = $this->make_request($url,null);
405
		$this->loadModel('Api');
406
 
407
		$myactions = $this->Api->getMyActions($this->Auth->User('id'));
408
		if(!empty($myactions)) {
409
			foreach ($myactions['actions'] as $key => $value) {
410
				if($value['UserAction']['action'] == 'like'){
411
					$likedDeals[$value['UserAction']['store_product_id']] = $value['UserAction']['id'];
412
				}else{
413
					$disLikedDeals[$value['UserAction']['store_product_id']] = $value['UserAction']['id'];
414
				}
415
			}
416
		}
417
 
418
		$this->set(compact('deal','likedDeals','disLikedDeals'));
419
 
420
		$this->render('/Elements/saholicdeal');
421
 
422
 
423
	}
424
 
18063 naman 425
	public function getdealsforsearchterm($searchterm,$page = null){
426
		$likedDeals = $disLikedDeals = array();
427
		$this->layout = 'ajax';
428
		$page = $this->request->query('page');
18390 naman 429
		$subcategories = $this->request->query('subcategories'); 
18063 naman 430
		if(!isset($page)){
431
			$page = 1;
432
			$offset = 0;
433
			$limit=10;
434
		}
435
		else{
436
			$offset = ($page*20) - 30;
437
			$limit = 20;
438
		}
439
		$sort = $this->request->query('sort');
440
		$direction = $this->request->query('direction');
441
 
18390 naman 442
		$url = $this->apihost."searchSubCategory/?offset=".$offset."&limit=".$limit."&searchTerm=".urlencode($searchterm)."&subCategoryId=".$subcategories;
18063 naman 443
		$response = $this->make_request($url,null);
444
		$deals = array();
445
		if(!empty($response)){
446
			foreach ($response as $key => $value) {
447
				if(!empty($value)){
448
					$deals[] = $value;
449
				}
450
			}
451
		}
18115 amit.gupta 452
 
18063 naman 453
		$this->loadModel('Api');
18115 amit.gupta 454
 
18063 naman 455
		$myactions = $this->Api->getMyActions($this->Auth->User('id'));
456
		if(!empty($myactions)) {
457
			foreach ($myactions['actions'] as $key => $value) {
458
				if($value['UserAction']['action'] == 'like'){
459
					$likedDeals[$value['UserAction']['store_product_id']] = $value['UserAction']['id'];
460
				}else{
461
					$disLikedDeals[$value['UserAction']['store_product_id']] = $value['UserAction']['id'];
462
				}
463
			}
464
		}
465
 
18390 naman 466
		$nexturl = "/categories/getdealsforsearchterm/".urlencode($searchterm)."/?page=".($page+1).'&subcategories='.$subcategories;
18063 naman 467
 
468
		$this->set(compact('deals','id','page','likedDeals','disLikedDeals','sort','direction','brands','filter','nexturl'));
469
		if(!empty($deals) && !empty($deals[0])){
470
			$this->render('/Elements/deals');
471
		}else{
472
			$this->render('/Elements/nodeals');
473
		}
474
 
475
 
476
	}
19306 naman 477
 
478
	public function target($offer_id = null){
479
		$this->layout = "innerpages";
480
// 		$user_id =  $this->Auth->user('id');
481
// 		$url = $this->apihost."getOfferForUser/?user_id=47";
482
// 		$response = $this->make_request($url,null);
483
 
484
		$user_id = $this->Auth->user('id');
485
		$cachekey = 'target-'.$user_id;
486
		$getoffer = Cache::read($cachekey,'target');
487
 
488
		$response = "";
489
		if($getoffer === false){
490
			$offerurl = $this->apihost."getOfferForUser/?user_id=47";
491
			$response = $this->make_request($offerurl,null);
492
			Cache::write($cachekey , $response ,'target');
493
			if(!empty($response)){
494
				$current_time = time();
495
								if($offerresponse['startDate']/1000 <= $current_time && $offerresponse['endDate']/1000 >= $current_time ){
496
 
497
								}
498
								else{
499
									$offerresponse = "";
500
									}
501
				}
502
				else{
503
					$offerresponse = "";
504
				}
505
		}else{
506
			$response = $getoffer;
507
		}
508
 
509
		$maxpercentage = 0;
510
// 		debug($response);
511
 
512
		if(isset($response['target2_cash_back_percetage']) && !empty($response['target2_cash_back_percetage'])){
513
			$maxpercentage = $response['target2_cash_back_percetage'];
514
		}
515
		else{
516
			if(isset($response['target1_cash_back_percetage']) && !empty($response['target1_cash_back_percetage'])){
517
				$maxpercentage = $response['target1_cash_back_percetage'];
518
			}
519
		}
520
 
521
		$user_email = $this->Auth->user('email');
522
		$subcat = Configure::read('arrSubCategory');
523
		$this->set(compact('response','maxpercentage','user_email','subcat'));
524
	}
13532 anikendra 525
/**
526
 * add method
527
 *
528
 * @return void
529
 */
530
	public function add() {
531
		if ($this->request->is('post')) {
532
			$this->Category->create();
533
			if ($this->Category->save($this->request->data)) {
534
				$this->Session->setFlash(__('The category has been saved.'));
535
				return $this->redirect(array('action' => 'index'));
536
			} else {
537
				$this->Session->setFlash(__('The category could not be saved. Please, try again.'));
538
			}
539
		}
540
	}
541
 
542
/**
543
 * edit method
544
 *
545
 * @throws NotFoundException
546
 * @param string $id
547
 * @return void
548
 */
549
	public function edit($id = null) {
550
		if (!$this->Category->exists($id)) {
551
			throw new NotFoundException(__('Invalid category'));
552
		}
553
		if ($this->request->is(array('post', 'put'))) {
554
			if ($this->Category->save($this->request->data)) {
555
				$this->Session->setFlash(__('The category has been saved.'));
556
				return $this->redirect(array('action' => 'index'));
557
			} else {
558
				$this->Session->setFlash(__('The category could not be saved. Please, try again.'));
559
			}
560
		} else {
561
			$options = array('conditions' => array('Category.' . $this->Category->primaryKey => $id));
562
			$this->request->data = $this->Category->find('first', $options);
563
		}
564
	}
565
 
566
/**
567
 * delete method
568
 *
569
 * @throws NotFoundException
570
 * @param string $id
571
 * @return void
572
 */
573
	public function delete($id = null) {
574
		$this->Category->id = $id;
575
		if (!$this->Category->exists()) {
576
			throw new NotFoundException(__('Invalid category'));
577
		}
578
		$this->request->onlyAllow('post', 'delete');
579
		if ($this->Category->delete()) {
580
			$this->Session->setFlash(__('The category has been deleted.'));
581
		} else {
582
			$this->Session->setFlash(__('The category could not be deleted. Please, try again.'));
583
		}
584
		return $this->redirect(array('action' => 'index'));
585
	}
586
 
587
/**
588
 * admin_index method
589
 *
590
 * @return void
591
 */
592
	public function admin_index() {
593
		$this->Category->recursive = 0;
594
		$this->set('categories', $this->Paginator->paginate());
595
	}
596
 
597
/**
598
 * admin_view method
599
 *
600
 * @throws NotFoundException
601
 * @param string $id
602
 * @return void
603
 */
604
	public function admin_view($id = null) {
605
		if (!$this->Category->exists($id)) {
606
			throw new NotFoundException(__('Invalid category'));
607
		}
608
		$options = array('conditions' => array('Category.' . $this->Category->primaryKey => $id));
609
		$this->set('category', $this->Category->find('first', $options));
610
	}
611
 
612
/**
613
 * admin_add method
614
 *
615
 * @return void
616
 */
617
	public function admin_add() {
618
		if ($this->request->is('post')) {
619
			// print_r($this->request->data);die;
620
			$this->Category->create();
621
			if ($this->Category->save($this->request->data)) {
622
				$this->Session->setFlash(__('The category has been saved.'));
623
				return $this->redirect(array('action' => 'index'));
624
			} else {
625
				$this->Session->setFlash(__('The category could not be saved. Please, try again.'));
626
			}
627
		}
628
		$this->set('parent_id',$this->Category->find('list'));
629
	}
630
 
631
/**
632
 * admin_edit method
633
 *
634
 * @throws NotFoundException
635
 * @param string $id
636
 * @return void
637
 */
638
	public function admin_edit($id = null) {
639
		if (!$this->Category->exists($id)) {
640
			throw new NotFoundException(__('Invalid category'));
641
		}
642
		if ($this->request->is(array('post', 'put'))) {
643
			if ($this->Category->save($this->request->data)) {
644
				$this->Session->setFlash(__('The category has been saved.'));
645
				return $this->redirect(array('action' => 'index'));
646
			} else {
647
				$this->Session->setFlash(__('The category could not be saved. Please, try again.'));
648
			}
649
		} else {
650
			$options = array('conditions' => array('Category.' . $this->Category->primaryKey => $id));
651
			$this->request->data = $this->Category->find('first', $options);
652
		}
653
	}
654
 
655
/**
656
 * admin_delete method
657
 *
658
 * @throws NotFoundException
659
 * @param string $id
660
 * @return void
661
 */
662
	public function admin_delete($id = null) {
663
		$this->Category->id = $id;
664
		if (!$this->Category->exists()) {
665
			throw new NotFoundException(__('Invalid category'));
666
		}
667
		$this->request->onlyAllow('post', 'delete');
668
		if ($this->Category->delete()) {
669
			$this->Session->setFlash(__('The category has been deleted.'));
670
		} else {
671
			$this->Session->setFlash(__('The category could not be deleted. Please, try again.'));
672
		}
673
		return $this->redirect(array('action' => 'index'));
674
	}}