Subversion Repositories SmartDukaan

Rev

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