Subversion Repositories SmartDukaan

Rev

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