Subversion Repositories SmartDukaan

Rev

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