Subversion Repositories SmartDukaan

Rev

Rev 17183 | Rev 17695 | 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;
16549 anikendra 213
		  	} 	  	
17683 naman 214
			$this->set(compact('response_count','deals','id','likedDeals','disLikedDeals','page','sort','direction','notification','filter','brands','filterstr','brandschosen','subcategories','subcategorieschosen'));
16549 anikendra 215
		}else{
16583 anikendra 216
			//Check for apk support of sharing
217
			$sharable = 0;
218
			if(isset($_COOKIE['shareApps']) && !empty($_COOKIE['shareApps'])) {
219
				$sharable = 1;
17183 anikendra 220
			}			
16704 anikendra 221
			$url = $this->apihost."appOffers/1";
222
			$appOffers = $this->make_request($url,null);
223
			$this->set(compact('page','id','sharable','appOffers'));
16549 anikendra 224
			$this->render('viewapps');
13583 anikendra 225
		}
13532 anikendra 226
	}
227
 
16549 anikendra 228
	public function getapps($id=null){
229
		$this->layout = 'ajax';
230
		$page = $this->request->query('page');
231
		if(!isset($page)){
232
			$page = 1;
233
		}
234
		$this->loadModel('AppOffer');
235
		$this->AppOffer->recursive = -1;
236
		$options = array('conditions'=>array('offer_active'=>1,'show'=>1),'limit' => Configure::read('searchresultsperpage'),'page'=>$page);
237
		$this->Paginator->settings = $options;
238
		try{
239
			$appOffers = $this->Paginator->paginate('AppOffer');
240
		} catch (NotFoundException $e) {
241
	        //get current page
242
	        $page = $this->request->params['named']['page'];
243
	     	$appOffers = array();   
244
		}
245
		debug($appOffers);		
246
		if(!empty($appOffers)){
247
			$this->set(compact('page','id','appOffers'));
248
			$this->render('/Elements/appoffers');
249
		}else{
250
			$this->render('/Elements/nooffers');
251
		}		
252
	}
253
 
13579 anikendra 254
	public function getdeals($id = null) {
13808 anikendra 255
		$this->log('getdeal id '.$id,'api');
13583 anikendra 256
		$likedDeals = $disLikedDeals = array();
13579 anikendra 257
		$this->layout = 'ajax';
258
		$page = $this->request->query('page');
259
		if(!isset($page)){
260
			$page = 1;
261
		}
13808 anikendra 262
		$sort = $this->request->query('sort');
263
		$direction = $this->request->query('direction');
15044 anikendra 264
		// $filter = $this->request->query('filter');
265
		// $brands = $this->request->query('brands');
266
		$brandschosen = $_COOKIE['brandschosen'];	  	
267
		if(!empty($brandschosen)){
268
			$filter = 'brand';
269
		}
270
		$brands = str_replace(',', '^', $brandschosen);
15026 anikendra 271
		$url = $this->getDealsApiUrl($page,$this->Auth->User('id'),$id,$sort,$direction,$filter,$brands);
16098 anikendra 272
		$response = $this->make_request($url,null);
273
		$deals = array();
274
		if(!empty($response)){
275
			foreach ($response as $key => $value) {
276
				if(!empty($value)){
277
					$deals[] = $value;
278
				}
279
			}
280
		}
13794 anikendra 281
		// if (!$this->Category->exists($id)) {
282
			// throw new NotFoundException(__('Invalid category'));
283
		// }
13579 anikendra 284
		$this->loadModel('Api');
13794 anikendra 285
		// $apideals = $this->Api->getDealsByCategory($this->Auth->User('id'),$id,$page);
286
		// $deals = $apideals['products'];
13583 anikendra 287
		$myactions = $this->Api->getMyActions($this->Auth->User('id'));
288
		if(!empty($myactions)) {
289
			foreach ($myactions['actions'] as $key => $value) {
290
				if($value['UserAction']['action'] == 'like'){
291
					$likedDeals[$value['UserAction']['store_product_id']] = $value['UserAction']['id'];
292
				}else{
293
					$disLikedDeals[$value['UserAction']['store_product_id']] = $value['UserAction']['id'];
294
				}
295
			}
296
		}
15026 anikendra 297
		$filterstr = '';
298
		if(isset($filter) && !empty($filter)){
299
		    $filterstr = '&filter='.$filter.'&brands='.$brands;
300
	  	} 
301
		$this->set(compact('deals','id','page','likedDeals','disLikedDeals','sort','direction','brands','filter','filterstr'));
16098 anikendra 302
		if(!empty($deals) && !empty($deals[0])){
15026 anikendra 303
			$this->render('/Elements/deals');
304
		}else{
305
			$this->render('/Elements/nodeals');
306
		}
13579 anikendra 307
	}
13532 anikendra 308
/**
309
 * add method
310
 *
311
 * @return void
312
 */
313
	public function add() {
314
		if ($this->request->is('post')) {
315
			$this->Category->create();
316
			if ($this->Category->save($this->request->data)) {
317
				$this->Session->setFlash(__('The category has been saved.'));
318
				return $this->redirect(array('action' => 'index'));
319
			} else {
320
				$this->Session->setFlash(__('The category could not be saved. Please, try again.'));
321
			}
322
		}
323
	}
324
 
325
/**
326
 * edit method
327
 *
328
 * @throws NotFoundException
329
 * @param string $id
330
 * @return void
331
 */
332
	public function edit($id = null) {
333
		if (!$this->Category->exists($id)) {
334
			throw new NotFoundException(__('Invalid category'));
335
		}
336
		if ($this->request->is(array('post', 'put'))) {
337
			if ($this->Category->save($this->request->data)) {
338
				$this->Session->setFlash(__('The category has been saved.'));
339
				return $this->redirect(array('action' => 'index'));
340
			} else {
341
				$this->Session->setFlash(__('The category could not be saved. Please, try again.'));
342
			}
343
		} else {
344
			$options = array('conditions' => array('Category.' . $this->Category->primaryKey => $id));
345
			$this->request->data = $this->Category->find('first', $options);
346
		}
347
	}
348
 
349
/**
350
 * delete method
351
 *
352
 * @throws NotFoundException
353
 * @param string $id
354
 * @return void
355
 */
356
	public function delete($id = null) {
357
		$this->Category->id = $id;
358
		if (!$this->Category->exists()) {
359
			throw new NotFoundException(__('Invalid category'));
360
		}
361
		$this->request->onlyAllow('post', 'delete');
362
		if ($this->Category->delete()) {
363
			$this->Session->setFlash(__('The category has been deleted.'));
364
		} else {
365
			$this->Session->setFlash(__('The category could not be deleted. Please, try again.'));
366
		}
367
		return $this->redirect(array('action' => 'index'));
368
	}
369
 
370
/**
371
 * admin_index method
372
 *
373
 * @return void
374
 */
375
	public function admin_index() {
376
		$this->Category->recursive = 0;
377
		$this->set('categories', $this->Paginator->paginate());
378
	}
379
 
380
/**
381
 * admin_view method
382
 *
383
 * @throws NotFoundException
384
 * @param string $id
385
 * @return void
386
 */
387
	public function admin_view($id = null) {
388
		if (!$this->Category->exists($id)) {
389
			throw new NotFoundException(__('Invalid category'));
390
		}
391
		$options = array('conditions' => array('Category.' . $this->Category->primaryKey => $id));
392
		$this->set('category', $this->Category->find('first', $options));
393
	}
394
 
395
/**
396
 * admin_add method
397
 *
398
 * @return void
399
 */
400
	public function admin_add() {
401
		if ($this->request->is('post')) {
402
			// print_r($this->request->data);die;
403
			$this->Category->create();
404
			if ($this->Category->save($this->request->data)) {
405
				$this->Session->setFlash(__('The category has been saved.'));
406
				return $this->redirect(array('action' => 'index'));
407
			} else {
408
				$this->Session->setFlash(__('The category could not be saved. Please, try again.'));
409
			}
410
		}
411
		$this->set('parent_id',$this->Category->find('list'));
412
	}
413
 
414
/**
415
 * admin_edit method
416
 *
417
 * @throws NotFoundException
418
 * @param string $id
419
 * @return void
420
 */
421
	public function admin_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
 * admin_delete method
440
 *
441
 * @throws NotFoundException
442
 * @param string $id
443
 * @return void
444
 */
445
	public function admin_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
	}}