Subversion Repositories SmartDukaan

Rev

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