Subversion Repositories SmartDukaan

Rev

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