Subversion Repositories SmartDukaan

Rev

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