Subversion Repositories SmartDukaan

Rev

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