Subversion Repositories SmartDukaan

Rev

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