Subversion Repositories SmartDukaan

Rev

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