Subversion Repositories SmartDukaan

Rev

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