Subversion Repositories SmartDukaan

Rev

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