Subversion Repositories SmartDukaan

Rev

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