Subversion Repositories SmartDukaan

Rev

Rev 16704 | Rev 17183 | 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;
77
		$deals = $this->make_request($url,null);		
13583 anikendra 78
		$myactions = $this->Api->getMyActions($this->Auth->User('id'));		
79
		if(!empty($myactions)) {
80
			foreach ($myactions['actions'] as $key => $value) {
81
				if($value['UserAction']['action'] == 'like'){
82
					$likedDeals[$value['UserAction']['store_product_id']] = $value['UserAction']['id'];
83
				}else{
84
					$disLikedDeals[$value['UserAction']['store_product_id']] = $value['UserAction']['id'];
85
				}
86
			}
87
		}
13567 anikendra 88
		$rows = sizeof($categorydeals);
89
		if($rows>=1){
90
			$this->set('deals',$categorydeals);
13583 anikendra 91
			$this->set('likedDeals',$likedDeals);
92
			$this->set('disLikedDeals',$disLikedDeals);
13567 anikendra 93
			$this->render('categorydeals');
94
		}else{
95
			foreach ($categorydeals as $key => $dealarr) {
96
				foreach ($dealarr as $key => $deal) {
97
					$deals[] = $deal[0];
98
				}				
99
			}
13583 anikendra 100
			$this->set(compact('deals','likedDeals','disLikedDeals'));
13567 anikendra 101
		}		
102
	}
103
/*
104
	*
13532 anikendra 105
 * view method
106
 *
107
 * @throws NotFoundException
108
 * @param string $id
109
 * @return void
110
 */
111
	public function view($id = null) {
14930 anikendra 112
		$userId = $this->request->query('user_id');
15378 anikendra 113
		if(isset($userId) && !empty($userId)) {
14930 anikendra 114
			$this->loadModel('User');
115
			$dbuser = $this->User->findById($userId);
116
			$this->Auth->login($dbuser['User']);
16549 anikendra 117
		}				
13583 anikendra 118
		$page = $this->request->query('page');
119
		if(!isset($page)){
120
			$page = 1;
16549 anikendra 121
		}
122
 
13808 anikendra 123
		$sort = $this->request->query('sort');
124
		$direction = $this->request->query('direction');
15044 anikendra 125
		// $filter = $this->request->query('filter');
126
		// $brands = $this->request->query('brands');
16549 anikendra 127
		if($id != 2) {
128
			//Fetch deals
129
			$likedDeals = $disLikedDeals = array();
17174 naman 130
			$brandschosen = $_COOKIE['brandschosen'];
131
			if((isset($_COOKIE['old_id'])) && ($_COOKIE['old_id'] != $id))
132
			{
133
				unset($brandschosen);
134
				setcookie('brandschosen', 1, time()-1, '/');
135
			}
136
			else
137
			{
138
				$brandschosen = $_COOKIE['brandschosen'];	  		
139
			}
140
			// $this->check_id($id);
141
			if(!isset($_COOKIE['old_id']))
142
			{
143
				setcookie('old_id',$id);	
144
			}
145
			else
146
			{
147
				// unset($_COOKIE['brandschosen']);
148
				unset($_COOKIE['old_id']);
149
				setcookie('old_id',$id);
150
			}
151
 
16549 anikendra 152
			if(!empty($brandschosen)){
153
				$filter = 'brand';
154
			}
155
			$brands = str_replace(',', '^', $brandschosen);
156
			$url = $this->getDealsApiUrl($page,$this->Auth->User('id'),$id,$sort,$direction,$filter,$brands);
157
			// $url = $this->apihost.'deals/'.$this->Auth->User('id').'?categoryId='.$id;
158
			$response = $this->make_request($url,null);
159
			$deals = array();
160
			if(!empty($response)){
161
				foreach ($response as $key => $value) {
162
					if(!empty($value)){
163
						$deals[] = $value;
164
					}
16098 anikendra 165
				}
166
			}
16549 anikendra 167
			$this->loadModel('Api');
168
			// $apideals = $this->Api->getDealsByCategory($this->Auth->User('id'),$id,$page);
169
			// $deals = $apideals['products'];
170
			$myactions = $this->Api->getMyActions($this->Auth->User('id'));
171
			if(!empty($myactions)) {
172
				foreach ($myactions['actions'] as $key => $value) {
173
					if($value['UserAction']['action'] == 'like'){
174
						$likedDeals[$value['UserAction']['store_product_id']] = $value['UserAction']['id'];
175
					}else{
176
						$disLikedDeals[$value['UserAction']['store_product_id']] = $value['UserAction']['id'];
177
					}
13583 anikendra 178
				}
179
			}
16549 anikendra 180
			$this->loadModel('NotificationRule');
181
			$notification = $this->NotificationRule->getNotification($this->Auth->User('id'));	
182
		    $filterstr = '';
183
			if(isset($filter) && !empty($filter)){
184
			    $filterstr = '&filter='.$filter.'&brands='.$brands;
185
		  	} 	  	
186
			$this->set(compact('deals','id','likedDeals','disLikedDeals','page','sort','direction','notification','filter','brands','filterstr','brandschosen'));
187
		}else{
16583 anikendra 188
			//Check for apk support of sharing
189
			$sharable = 0;
190
			if(isset($_COOKIE['shareApps']) && !empty($_COOKIE['shareApps'])) {
191
				$sharable = 1;
192
			}
16549 anikendra 193
			//Fetch app offers
16704 anikendra 194
			// $this->loadModel('AppOffer');
195
			// $this->AppOffer->recursive = -1;
196
			// $options = array('conditions'=>array('offer_active'=>1,'show'=>1),'limit' => Configure::read('searchresultsperpage'),'page'=>$page);
197
			// $this->Paginator->settings = $options;
198
			// $this->set('appOffers', $this->Paginator->paginate('AppOffer'));
199
			$url = $this->apihost."appOffers/1";
200
			$appOffers = $this->make_request($url,null);
201
			$this->set(compact('page','id','sharable','appOffers'));
16549 anikendra 202
			$this->render('viewapps');
13583 anikendra 203
		}
13532 anikendra 204
	}
205
 
16549 anikendra 206
	public function getapps($id=null){
207
		$this->layout = 'ajax';
208
		$page = $this->request->query('page');
209
		if(!isset($page)){
210
			$page = 1;
211
		}
212
		$this->loadModel('AppOffer');
213
		$this->AppOffer->recursive = -1;
214
		$options = array('conditions'=>array('offer_active'=>1,'show'=>1),'limit' => Configure::read('searchresultsperpage'),'page'=>$page);
215
		$this->Paginator->settings = $options;
216
		try{
217
			$appOffers = $this->Paginator->paginate('AppOffer');
218
		} catch (NotFoundException $e) {
219
	        //get current page
220
	        $page = $this->request->params['named']['page'];
221
	     	$appOffers = array();   
222
		}
223
		debug($appOffers);		
224
		if(!empty($appOffers)){
225
			$this->set(compact('page','id','appOffers'));
226
			$this->render('/Elements/appoffers');
227
		}else{
228
			$this->render('/Elements/nooffers');
229
		}		
230
	}
231
 
13579 anikendra 232
	public function getdeals($id = null) {
13808 anikendra 233
		$this->log('getdeal id '.$id,'api');
13583 anikendra 234
		$likedDeals = $disLikedDeals = array();
13579 anikendra 235
		$this->layout = 'ajax';
236
		$page = $this->request->query('page');
237
		if(!isset($page)){
238
			$page = 1;
239
		}
13808 anikendra 240
		$sort = $this->request->query('sort');
241
		$direction = $this->request->query('direction');
15044 anikendra 242
		// $filter = $this->request->query('filter');
243
		// $brands = $this->request->query('brands');
244
		$brandschosen = $_COOKIE['brandschosen'];	  	
245
		if(!empty($brandschosen)){
246
			$filter = 'brand';
247
		}
248
		$brands = str_replace(',', '^', $brandschosen);
15026 anikendra 249
		$url = $this->getDealsApiUrl($page,$this->Auth->User('id'),$id,$sort,$direction,$filter,$brands);
16098 anikendra 250
		$response = $this->make_request($url,null);
251
		$deals = array();
252
		if(!empty($response)){
253
			foreach ($response as $key => $value) {
254
				if(!empty($value)){
255
					$deals[] = $value;
256
				}
257
			}
258
		}
13794 anikendra 259
		// if (!$this->Category->exists($id)) {
260
			// throw new NotFoundException(__('Invalid category'));
261
		// }
13579 anikendra 262
		$this->loadModel('Api');
13794 anikendra 263
		// $apideals = $this->Api->getDealsByCategory($this->Auth->User('id'),$id,$page);
264
		// $deals = $apideals['products'];
13583 anikendra 265
		$myactions = $this->Api->getMyActions($this->Auth->User('id'));
266
		if(!empty($myactions)) {
267
			foreach ($myactions['actions'] as $key => $value) {
268
				if($value['UserAction']['action'] == 'like'){
269
					$likedDeals[$value['UserAction']['store_product_id']] = $value['UserAction']['id'];
270
				}else{
271
					$disLikedDeals[$value['UserAction']['store_product_id']] = $value['UserAction']['id'];
272
				}
273
			}
274
		}
15026 anikendra 275
		$filterstr = '';
276
		if(isset($filter) && !empty($filter)){
277
		    $filterstr = '&filter='.$filter.'&brands='.$brands;
278
	  	} 
279
		$this->set(compact('deals','id','page','likedDeals','disLikedDeals','sort','direction','brands','filter','filterstr'));
16098 anikendra 280
		if(!empty($deals) && !empty($deals[0])){
15026 anikendra 281
			$this->render('/Elements/deals');
282
		}else{
283
			$this->render('/Elements/nodeals');
284
		}
13579 anikendra 285
	}
13532 anikendra 286
/**
287
 * add method
288
 *
289
 * @return void
290
 */
291
	public function add() {
292
		if ($this->request->is('post')) {
293
			$this->Category->create();
294
			if ($this->Category->save($this->request->data)) {
295
				$this->Session->setFlash(__('The category has been saved.'));
296
				return $this->redirect(array('action' => 'index'));
297
			} else {
298
				$this->Session->setFlash(__('The category could not be saved. Please, try again.'));
299
			}
300
		}
301
	}
302
 
303
/**
304
 * edit method
305
 *
306
 * @throws NotFoundException
307
 * @param string $id
308
 * @return void
309
 */
310
	public function edit($id = null) {
311
		if (!$this->Category->exists($id)) {
312
			throw new NotFoundException(__('Invalid category'));
313
		}
314
		if ($this->request->is(array('post', 'put'))) {
315
			if ($this->Category->save($this->request->data)) {
316
				$this->Session->setFlash(__('The category has been saved.'));
317
				return $this->redirect(array('action' => 'index'));
318
			} else {
319
				$this->Session->setFlash(__('The category could not be saved. Please, try again.'));
320
			}
321
		} else {
322
			$options = array('conditions' => array('Category.' . $this->Category->primaryKey => $id));
323
			$this->request->data = $this->Category->find('first', $options);
324
		}
325
	}
326
 
327
/**
328
 * delete method
329
 *
330
 * @throws NotFoundException
331
 * @param string $id
332
 * @return void
333
 */
334
	public function delete($id = null) {
335
		$this->Category->id = $id;
336
		if (!$this->Category->exists()) {
337
			throw new NotFoundException(__('Invalid category'));
338
		}
339
		$this->request->onlyAllow('post', 'delete');
340
		if ($this->Category->delete()) {
341
			$this->Session->setFlash(__('The category has been deleted.'));
342
		} else {
343
			$this->Session->setFlash(__('The category could not be deleted. Please, try again.'));
344
		}
345
		return $this->redirect(array('action' => 'index'));
346
	}
347
 
348
/**
349
 * admin_index method
350
 *
351
 * @return void
352
 */
353
	public function admin_index() {
354
		$this->Category->recursive = 0;
355
		$this->set('categories', $this->Paginator->paginate());
356
	}
357
 
358
/**
359
 * admin_view method
360
 *
361
 * @throws NotFoundException
362
 * @param string $id
363
 * @return void
364
 */
365
	public function admin_view($id = null) {
366
		if (!$this->Category->exists($id)) {
367
			throw new NotFoundException(__('Invalid category'));
368
		}
369
		$options = array('conditions' => array('Category.' . $this->Category->primaryKey => $id));
370
		$this->set('category', $this->Category->find('first', $options));
371
	}
372
 
373
/**
374
 * admin_add method
375
 *
376
 * @return void
377
 */
378
	public function admin_add() {
379
		if ($this->request->is('post')) {
380
			// print_r($this->request->data);die;
381
			$this->Category->create();
382
			if ($this->Category->save($this->request->data)) {
383
				$this->Session->setFlash(__('The category has been saved.'));
384
				return $this->redirect(array('action' => 'index'));
385
			} else {
386
				$this->Session->setFlash(__('The category could not be saved. Please, try again.'));
387
			}
388
		}
389
		$this->set('parent_id',$this->Category->find('list'));
390
	}
391
 
392
/**
393
 * admin_edit method
394
 *
395
 * @throws NotFoundException
396
 * @param string $id
397
 * @return void
398
 */
399
	public function admin_edit($id = null) {
400
		if (!$this->Category->exists($id)) {
401
			throw new NotFoundException(__('Invalid category'));
402
		}
403
		if ($this->request->is(array('post', 'put'))) {
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
		} else {
411
			$options = array('conditions' => array('Category.' . $this->Category->primaryKey => $id));
412
			$this->request->data = $this->Category->find('first', $options);
413
		}
414
	}
415
 
416
/**
417
 * admin_delete method
418
 *
419
 * @throws NotFoundException
420
 * @param string $id
421
 * @return void
422
 */
423
	public function admin_delete($id = null) {
424
		$this->Category->id = $id;
425
		if (!$this->Category->exists()) {
426
			throw new NotFoundException(__('Invalid category'));
427
		}
428
		$this->request->onlyAllow('post', 'delete');
429
		if ($this->Category->delete()) {
430
			$this->Session->setFlash(__('The category has been deleted.'));
431
		} else {
432
			$this->Session->setFlash(__('The category could not be deleted. Please, try again.'));
433
		}
434
		return $this->redirect(array('action' => 'index'));
435
	}}