Subversion Repositories SmartDukaan

Rev

Rev 17810 | Rev 18004 | 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');
17810 manish.sha 129
		$error = $this->request->query('error');
13583 anikendra 130
		if(!isset($page)){
131
			$page = 1;
16549 anikendra 132
		}
133
 
13808 anikendra 134
		$sort = $this->request->query('sort');
135
		$direction = $this->request->query('direction');
15044 anikendra 136
		// $filter = $this->request->query('filter');
137
		// $brands = $this->request->query('brands');
16549 anikendra 138
		if($id != 2) {
139
			//Fetch deals
140
			$likedDeals = $disLikedDeals = array();
17174 naman 141
			$brandschosen = $_COOKIE['brandschosen'];
17683 naman 142
			$subcategorieschosen = $_COOKIE['subcategorieschosen'];			
17759 naman 143
 
144
			// echo "Cookie val", $_COOKIE['old_id'];
145
			// echo "id val", $id;
17683 naman 146
 
17759 naman 147
 
17183 anikendra 148
			if((isset($_COOKIE['old_id'])) && ($_COOKIE['old_id'] != $id)) {
17683 naman 149
 
150
 
17174 naman 151
				unset($brandschosen);
17683 naman 152
				// setcookie('brandschosen', 1, time()-1, '/');
153
				// unset($_COOKIE['brandschosen']);
154
    			setcookie('brandschosen', null, -1, '/');
155
				unset($subcategorieschosen);
156
				setcookie('subcategorieschosen', 1, time()-1, '/');
157
 
158
			}		
159
 
160
			if((isset($_COOKIE['brandschosen'])) && ($_COOKIE['old_id'] != $id)) {
161
 
162
				unset($brandschosen);
163
				// setcookie('brandschosen', 1, time()-10, '/');
164
				// unset($_COOKIE['brandschosen']);
165
    			setcookie('brandschosen', null, -1, '/');
166
				unset($subcategorieschosen);
167
				setcookie('subcategorieschosen', 1, time()-1, '/');
168
 
16549 anikendra 169
			}
17683 naman 170
 
171
			setcookie('old_id', $id, -1, '/');
172
 
173
 
174
				if(!empty($brandschosen) && !empty($subcategorieschosen)){
175
					$filter = 'brand|subcategory';
176
				}			
177
				else if(!empty($brandschosen) && empty($subcategorieschosen)){
178
					$filter = 'brand';
179
				}
180
				if(!empty($subcategorieschosen) && empty($brandschosen)){
181
					$filter = 'subcategory';
182
				}
183
 
184
				/*if(!empty($brandschosen)){
185
					$filter = 'brand';
186
				}*/
187
 
16549 anikendra 188
			$brands = str_replace(',', '^', $brandschosen);
17683 naman 189
			$subcategories = str_replace(',', '^', $subcategorieschosen);
190
			$url = $this->getDealsApiUrl($page,$this->Auth->User('id'),$id,$sort,$direction,$filter,$brands,$subcategories);
16549 anikendra 191
			// $url = $this->apihost.'deals/'.$this->Auth->User('id').'?categoryId='.$id;
192
			$response = $this->make_request($url,null);
193
			$deals = array();
17683 naman 194
 
195
			$response_count =1;
196
			if($response == '')
197
			{
198
				$response_count = 0;
199
			}
200
			// debug($response_count);
16549 anikendra 201
			if(!empty($response)){
202
				foreach ($response as $key => $value) {
203
					if(!empty($value)){
204
						$deals[] = $value;
205
					}
16098 anikendra 206
				}
207
			}
17683 naman 208
			#print_r($deals);
16549 anikendra 209
			$this->loadModel('Api');
210
			$myactions = $this->Api->getMyActions($this->Auth->User('id'));
211
			if(!empty($myactions)) {
212
				foreach ($myactions['actions'] as $key => $value) {
213
					if($value['UserAction']['action'] == 'like'){
214
						$likedDeals[$value['UserAction']['store_product_id']] = $value['UserAction']['id'];
215
					}else{
216
						$disLikedDeals[$value['UserAction']['store_product_id']] = $value['UserAction']['id'];
217
					}
13583 anikendra 218
				}
219
			}
16549 anikendra 220
			$this->loadModel('NotificationRule');
221
			$notification = $this->NotificationRule->getNotification($this->Auth->User('id'));	
222
		    $filterstr = '';
223
			if(isset($filter) && !empty($filter)){
17759 naman 224
 
17683 naman 225
			    $filterstr = '&filter='.$filter.'&brands='.$brands.'&subcategories='.$subcategories;
17695 naman 226
		  	} 
227
 
17759 naman 228
 
17695 naman 229
		  	$get_url = "'".$_SERVER['REQUEST_URI']."'";
230
		  	$urlArray = explode('=',$_SERVER['REQUEST_URI']);
231
		  	$last = $urlArray[sizeof($urlArray)-1];
232
 
17759 naman 233
		  	// <Code for url used after scrolling...>
17695 naman 234
		  	if(!isset($filter) && empty($filter)){
235
		  		if (strpos($get_url,'filter=brand&brands') !== false)
236
		  		{
237
		  			$filterstr= "&filter=brand&brands=".$last;
238
		  		}
239
		  		if (strpos($get_url,'filter=subcategory&subcategories') !== false)
240
		  		{
241
		  			$filterstr= "&filter=subcategory&subcategories=".$last;
242
		  		}
243
 
17759 naman 244
		  	}
245
 
246
 
247
			if(!empty($subcategorieschosen) && isset($subcategorieschosen) && empty($brandschosen) && !isset($brandschosen)){	
248
					$filterstr= "&filter=subcategory&subcategories=".$subcategorieschosen;				
249
			}
17810 manish.sha 250
 
251
			$errorstr = '';
252
 
253
			if(isset($error)){
254
				$errorstr = 'Oops!! Some Error Occured. <br> Please try after Some Time';
255
			}
17759 naman 256
 
17810 manish.sha 257
			$this->set(compact('response_count','deals','id','likedDeals','disLikedDeals','page','sort','direction','notification','filter','brands','filterstr','brandschosen','subcategories','subcategorieschosen','errorstr'));
16549 anikendra 258
		}else{
16583 anikendra 259
			//Check for apk support of sharing
260
			$sharable = 0;
261
			if(isset($_COOKIE['shareApps']) && !empty($_COOKIE['shareApps'])) {
262
				$sharable = 1;
17183 anikendra 263
			}			
16704 anikendra 264
			$url = $this->apihost."appOffers/1";
265
			$appOffers = $this->make_request($url,null);
266
			$this->set(compact('page','id','sharable','appOffers'));
16549 anikendra 267
			$this->render('viewapps');
13583 anikendra 268
		}
13532 anikendra 269
	}
270
 
16549 anikendra 271
	public function getapps($id=null){
272
		$this->layout = 'ajax';
273
		$page = $this->request->query('page');
274
		if(!isset($page)){
275
			$page = 1;
276
		}
277
		$this->loadModel('AppOffer');
278
		$this->AppOffer->recursive = -1;
279
		$options = array('conditions'=>array('offer_active'=>1,'show'=>1),'limit' => Configure::read('searchresultsperpage'),'page'=>$page);
280
		$this->Paginator->settings = $options;
281
		try{
282
			$appOffers = $this->Paginator->paginate('AppOffer');
283
		} catch (NotFoundException $e) {
284
	        //get current page
285
	        $page = $this->request->params['named']['page'];
286
	     	$appOffers = array();   
287
		}
288
		debug($appOffers);		
289
		if(!empty($appOffers)){
290
			$this->set(compact('page','id','appOffers'));
291
			$this->render('/Elements/appoffers');
292
		}else{
293
			$this->render('/Elements/nooffers');
294
		}		
295
	}
296
 
13579 anikendra 297
	public function getdeals($id = null) {
17695 naman 298
 
13808 anikendra 299
		$this->log('getdeal id '.$id,'api');
13583 anikendra 300
		$likedDeals = $disLikedDeals = array();
13579 anikendra 301
		$this->layout = 'ajax';
302
		$page = $this->request->query('page');
303
		if(!isset($page)){
304
			$page = 1;
305
		}
13808 anikendra 306
		$sort = $this->request->query('sort');
307
		$direction = $this->request->query('direction');
15044 anikendra 308
		// $filter = $this->request->query('filter');
309
		// $brands = $this->request->query('brands');
17759 naman 310
		$brandschosen = $_COOKIE['brandschosen'];	 
311
		$subcategorieschosen = $_COOKIE['subcategorieschosen']; 	
312
 
313
		// Replace with the below content
314
		// if(!empty($brandschosen)){
315
		// 	$filter = 'brand';
316
		// }
317
 
318
		//I have replaced the above content
319
		if(!empty($brandschosen) && !empty($subcategorieschosen)){
320
			$filter = 'brand|subcategory';
321
		}			
322
		else if(!empty($brandschosen) && empty($subcategorieschosen)){
15044 anikendra 323
			$filter = 'brand';
324
		}
17759 naman 325
		if(!empty($subcategorieschosen) && empty($brandschosen)){
326
			$filter = 'subcategory';
327
		}
328
		//Replaced end
329
 
15044 anikendra 330
		$brands = str_replace(',', '^', $brandschosen);
17759 naman 331
		$subcategories = str_replace(',', '^', $subcategorieschosen);
332
		$url = $this->getDealsApiUrl($page,$this->Auth->User('id'),$id,$sort,$direction,$filter,$brands,$subcategories);
16098 anikendra 333
		$response = $this->make_request($url,null);
334
		$deals = array();
335
		if(!empty($response)){
336
			foreach ($response as $key => $value) {
337
				if(!empty($value)){
338
					$deals[] = $value;
339
				}
340
			}
341
		}
13794 anikendra 342
		// if (!$this->Category->exists($id)) {
343
			// throw new NotFoundException(__('Invalid category'));
344
		// }
13579 anikendra 345
		$this->loadModel('Api');
13794 anikendra 346
		// $apideals = $this->Api->getDealsByCategory($this->Auth->User('id'),$id,$page);
347
		// $deals = $apideals['products'];
13583 anikendra 348
		$myactions = $this->Api->getMyActions($this->Auth->User('id'));
349
		if(!empty($myactions)) {
350
			foreach ($myactions['actions'] as $key => $value) {
351
				if($value['UserAction']['action'] == 'like'){
352
					$likedDeals[$value['UserAction']['store_product_id']] = $value['UserAction']['id'];
353
				}else{
354
					$disLikedDeals[$value['UserAction']['store_product_id']] = $value['UserAction']['id'];
355
				}
356
			}
357
		}
15026 anikendra 358
		$filterstr = '';
17695 naman 359
 
360
		$get_url = "'".$_SERVER['REQUEST_URI']."'";
361
		$urlArray = explode('=',$_SERVER['REQUEST_URI']);
362
		$last = $urlArray[sizeof($urlArray)-1];
363
 
364
		if(!isset($filter) && empty($filter)){
365
			if (strpos($get_url,'filter=brand&brands') !== false)
366
			{
367
				$filterstr= "&filter=brand&brands=".$last;
368
			}
369
			if (strpos($get_url,'filter=subcategory&subcategories') !== false)
370
			{
371
				$filterstr= "&filter=subcategory&subcategories=".$last;
372
			}
373
 
374
		} 	  	
375
 
15026 anikendra 376
		if(isset($filter) && !empty($filter)){
17759 naman 377
		    // $filterstr = '&filter='.$filter.'&brands='.$brands;
378
		    $filterstr = '&filter='.$filter.'&brands='.$brands.'&subcategories='.$subcategories;
15026 anikendra 379
	  	} 
17759 naman 380
 
381
	  	if(!empty($subcategorieschosen) && isset($subcategorieschosen)&& empty($brandschosen) && !isset($brandschosen)){
382
 
383
	  			$filterstr= "&filter=subcategory&subcategories=".$subcategorieschosen;
384
	  	}
385
 
386
 
15026 anikendra 387
		$this->set(compact('deals','id','page','likedDeals','disLikedDeals','sort','direction','brands','filter','filterstr'));
16098 anikendra 388
		if(!empty($deals) && !empty($deals[0])){
15026 anikendra 389
			$this->render('/Elements/deals');
390
		}else{
391
			$this->render('/Elements/nodeals');
392
		}
17695 naman 393
 
394
 
395
 
13579 anikendra 396
	}
13532 anikendra 397
/**
398
 * add method
399
 *
400
 * @return void
401
 */
402
	public function add() {
403
		if ($this->request->is('post')) {
404
			$this->Category->create();
405
			if ($this->Category->save($this->request->data)) {
406
				$this->Session->setFlash(__('The category has been saved.'));
407
				return $this->redirect(array('action' => 'index'));
408
			} else {
409
				$this->Session->setFlash(__('The category could not be saved. Please, try again.'));
410
			}
411
		}
412
	}
413
 
414
/**
415
 * edit method
416
 *
417
 * @throws NotFoundException
418
 * @param string $id
419
 * @return void
420
 */
421
	public function edit($id = null) {
422
		if (!$this->Category->exists($id)) {
423
			throw new NotFoundException(__('Invalid category'));
424
		}
425
		if ($this->request->is(array('post', 'put'))) {
426
			if ($this->Category->save($this->request->data)) {
427
				$this->Session->setFlash(__('The category has been saved.'));
428
				return $this->redirect(array('action' => 'index'));
429
			} else {
430
				$this->Session->setFlash(__('The category could not be saved. Please, try again.'));
431
			}
432
		} else {
433
			$options = array('conditions' => array('Category.' . $this->Category->primaryKey => $id));
434
			$this->request->data = $this->Category->find('first', $options);
435
		}
436
	}
437
 
438
/**
439
 * delete method
440
 *
441
 * @throws NotFoundException
442
 * @param string $id
443
 * @return void
444
 */
445
	public function delete($id = null) {
446
		$this->Category->id = $id;
447
		if (!$this->Category->exists()) {
448
			throw new NotFoundException(__('Invalid category'));
449
		}
450
		$this->request->onlyAllow('post', 'delete');
451
		if ($this->Category->delete()) {
452
			$this->Session->setFlash(__('The category has been deleted.'));
453
		} else {
454
			$this->Session->setFlash(__('The category could not be deleted. Please, try again.'));
455
		}
456
		return $this->redirect(array('action' => 'index'));
457
	}
458
 
459
/**
460
 * admin_index method
461
 *
462
 * @return void
463
 */
464
	public function admin_index() {
465
		$this->Category->recursive = 0;
466
		$this->set('categories', $this->Paginator->paginate());
467
	}
468
 
469
/**
470
 * admin_view method
471
 *
472
 * @throws NotFoundException
473
 * @param string $id
474
 * @return void
475
 */
476
	public function admin_view($id = null) {
477
		if (!$this->Category->exists($id)) {
478
			throw new NotFoundException(__('Invalid category'));
479
		}
480
		$options = array('conditions' => array('Category.' . $this->Category->primaryKey => $id));
481
		$this->set('category', $this->Category->find('first', $options));
482
	}
483
 
484
/**
485
 * admin_add method
486
 *
487
 * @return void
488
 */
489
	public function admin_add() {
490
		if ($this->request->is('post')) {
491
			// print_r($this->request->data);die;
492
			$this->Category->create();
493
			if ($this->Category->save($this->request->data)) {
494
				$this->Session->setFlash(__('The category has been saved.'));
495
				return $this->redirect(array('action' => 'index'));
496
			} else {
497
				$this->Session->setFlash(__('The category could not be saved. Please, try again.'));
498
			}
499
		}
500
		$this->set('parent_id',$this->Category->find('list'));
501
	}
502
 
503
/**
504
 * admin_edit method
505
 *
506
 * @throws NotFoundException
507
 * @param string $id
508
 * @return void
509
 */
510
	public function admin_edit($id = null) {
511
		if (!$this->Category->exists($id)) {
512
			throw new NotFoundException(__('Invalid category'));
513
		}
514
		if ($this->request->is(array('post', 'put'))) {
515
			if ($this->Category->save($this->request->data)) {
516
				$this->Session->setFlash(__('The category has been saved.'));
517
				return $this->redirect(array('action' => 'index'));
518
			} else {
519
				$this->Session->setFlash(__('The category could not be saved. Please, try again.'));
520
			}
521
		} else {
522
			$options = array('conditions' => array('Category.' . $this->Category->primaryKey => $id));
523
			$this->request->data = $this->Category->find('first', $options);
524
		}
525
	}
526
 
527
/**
528
 * admin_delete method
529
 *
530
 * @throws NotFoundException
531
 * @param string $id
532
 * @return void
533
 */
534
	public function admin_delete($id = null) {
535
		$this->Category->id = $id;
536
		if (!$this->Category->exists()) {
537
			throw new NotFoundException(__('Invalid category'));
538
		}
539
		$this->request->onlyAllow('post', 'delete');
540
		if ($this->Category->delete()) {
541
			$this->Session->setFlash(__('The category has been deleted.'));
542
		} else {
543
			$this->Session->setFlash(__('The category could not be deleted. Please, try again.'));
544
		}
545
		return $this->redirect(array('action' => 'index'));
546
	}}