Subversion Repositories SmartDukaan

Rev

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