Subversion Repositories SmartDukaan

Rev

Rev 14300 | 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
 * UserActions Controller
5
 *
6
 * @property UserAction $UserAction
7
 * @property PaginatorComponent $Paginator
8
 */
9
class UserActionsController extends AppController {
10
 
11
/**
12
 * Components
13
 *
14
 * @var array
15
 */
16
	public $components = array('Paginator');
17
 
13562 anikendra 18
	public function beforeFilter() {
19
		parent::beforeFilter();
13685 anikendra 20
		$this->Auth->allow('update','by','rem','mine');
13562 anikendra 21
	}
22
 
13672 anikendra 23
	public function mine() {
13682 anikendra 24
		$userId = $this->request->query('user_id');
20106 naman 25
// 		if(isset($userId) && !empty($userId)){
26
// 			$this->loadModel('User');
27
// 			$dbuser = $this->User->findById($userId);
28
// 			$this->Auth->login($dbuser['User']);	
29
// 		}
13672 anikendra 30
		$this->layout = "innerpages";
31
		$this->UserAction->Behaviors->attach('Containable');
13962 anikendra 32
		$options = array('recursive'=>-1,'conditions'=>array('UserAction.user_id'=>$this->Auth->User('id'),'UserAction.action'=>'like'));
14215 anikendra 33
		// $options = array('recursive'=>-1,'conditions'=>array('UserAction.user_id'=>$this->Auth->User('id')));
13672 anikendra 34
		$myfavorites = $this->UserAction->find('all',$options);
13962 anikendra 35
		if(!empty($myfavorites)){
36
			foreach ($myfavorites as $key => $value) {				
37
				$cachekey = 'storeproduct-'.$value['UserAction']['store_product_id'];
38
				$product = Cache::read($cachekey,'fivemin');
39
				if(empty($product)) {
40
					$url = $this->apihost.'masterData/getSkuById/'.$value['UserAction']['store_product_id'];
41
					$product = $this->make_request($url,null);
42
					Cache::write($cachekey,$product,'fivemin');			
43
				}
44
				$myfavorites[$key]['StoreProduct'] = json_decode($product[0],1);
45
			}
46
		}
14300 anikendra 47
		$options = array('recursive'=>-1,'conditions'=>array('UserAction.user_id'=>$this->Auth->User('id'),'UserAction.action'=>'dislike'));
48
		// $options = array('recursive'=>-1,'conditions'=>array('UserAction.user_id'=>$this->Auth->User('id')));
49
		$mydislikes = $this->UserAction->find('all',$options);
50
		if(!empty($mydislikes)){
51
			foreach ($mydislikes as $key => $value) {				
52
				$cachekey = 'storeproduct-'.$value['UserAction']['store_product_id'];
53
				$product = Cache::read($cachekey,'fivemin');
54
				if(empty($product)) {
55
					$url = $this->apihost.'masterData/getSkuById/'.$value['UserAction']['store_product_id'];
56
					$product = $this->make_request($url,null);
57
					Cache::write($cachekey,$product,'fivemin');			
58
				}
59
				$mydislikes[$key]['StoreProduct'] = json_decode($product[0],1);
60
			}
61
		}
62
		$this->set(compact('myfavorites','mydislikes'));
13672 anikendra 63
	}
64
 
13562 anikendra 65
	public function update($user_id,$store_product_id,$action) {
66
		if(!empty($user_id) && !empty($store_product_id) && !empty($action)) {
67
			$data = array('user_id'=>$user_id,'store_product_id'=>$store_product_id);
68
			$this->UserAction->deleteAll(array($data),false);
69
			$this->response->type('json');
70
			$this->layout = 'ajax';
71
			$callback = $this->request->query('callback');
72
			$data['action'] = $action;
73
			$this->UserAction->create();
74
			if ($this->UserAction->save($data)) {
75
				$result = array('success'=>true,'message'=>(__('The user action has been saved.')));
76
			} else {
77
				$result = array('success'=>false,(__('The user action could not be saved. Please, try again.')));
78
			}
79
			$this->set(array(
80
			    'result' => $result,
81
			    'callback' => $callback,
82
			    '_serialize' => array('result')
83
			));
84
			$this->render('/Elements/jsonp');
85
		}		
86
	}
87
 
88
	public function rem($user_id,$store_product_id,$action) {
89
		if(!empty($user_id) && !empty($store_product_id) && !empty($action)) {
90
			$data = array('user_id'=>$user_id,'store_product_id'=>$store_product_id);
91
			$this->UserAction->deleteAll(array($data),false);
92
			$this->response->type('json');
93
			$this->layout = 'ajax';
94
			$callback = $this->request->query('callback');			
95
			$result = array('success'=>true,'message'=>(__('The user action has been deleted.')));
96
			$this->set(array(
97
			    'result' => $result,
98
			    'callback' => $callback,
99
			    '_serialize' => array('result')
100
			));
101
			$this->render('/Elements/jsonp');
102
		}		
103
	}
104
 
13731 anikendra 105
	public function deletefav($user_id,$id) {
106
		if(!empty($user_id) && !empty($id)) {
107
			$loggedInUserId = $this->Auth->User('id');
108
			if($user_id != $loggedInUserId){
109
				$result = array('success'=>false,'message'=>(__('Unauthorized access. Try again later')));
110
			} else {
111
				$sql = "DELETE FROM user_actions WHERE id = $id AND user_id = $user_id";
112
				$this->UserAction->query($sql);
113
				$result = array('success'=>true,'message'=>(__('The favourite product has been removed.')));				
114
			}
115
			$this->response->type('json');
116
			$this->layout = 'ajax';
117
			$callback = $this->request->query('callback');			
118
			$this->set(array(
119
			    'result' => $result,
120
			    'callback' => $callback,
121
			    '_serialize' => array('result')
122
			));
123
			$this->render('/Elements/jsonp');
124
		}		
125
	}
126
 
14300 anikendra 127
	public function deleteallfavs($user_id,$type='like') {
13731 anikendra 128
		if(!empty($user_id)) {
129
			$loggedInUserId = $this->Auth->User('id');
130
			if($user_id != $loggedInUserId){
131
				$result = array('success'=>false,'message'=>(__('Unauthorized access. Try again later')));
132
			} else {
14300 anikendra 133
				$sql = "DELETE FROM user_actions WHERE user_id = $user_id AND action = '$type'";
13731 anikendra 134
				$this->UserAction->query($sql);
135
				$result = array('success'=>true,'message'=>(__('All favourite products have been removed.')));				
136
			}
137
			$this->response->type('json');
138
			$this->layout = 'ajax';
139
			$callback = $this->request->query('callback');			
140
			$this->set(array(
141
			    'result' => $result,
142
			    'callback' => $callback,
143
			    '_serialize' => array('result')
144
			));
145
			$this->render('/Elements/jsonp');
146
		}		
147
	}
148
 
13562 anikendra 149
	public function by($user_id=null){
150
		if(!empty($user_id)){
151
			$this->response->type('json');
152
			$this->layout = 'ajax';
153
			$callback = $this->request->query('callback');
154
			$this->UserAction->recursive = -1;
155
			$conditions = array('user_id' => $user_id);
13583 anikendra 156
			$result['actions'] = $this->UserAction->find('all',array('conditions'=>$conditions));
13562 anikendra 157
			$this->set(array(
158
			    'result' => $result,
159
			    'callback' => $callback,
160
			    '_serialize' => array('result')
161
			));
13583 anikendra 162
			$this->render('/Elements/json');
13562 anikendra 163
		}
164
	}
13532 anikendra 165
/**
166
 * index method
167
 *
168
 * @return void
169
 */
170
	public function index() {
171
		$this->UserAction->recursive = 0;
172
		$this->set('userActions', $this->Paginator->paginate());
173
	}
174
 
175
/**
176
 * view method
177
 *
178
 * @throws NotFoundException
179
 * @param string $id
180
 * @return void
181
 */
182
	public function view($id = null) {
183
		if (!$this->UserAction->exists($id)) {
184
			throw new NotFoundException(__('Invalid user action'));
185
		}
186
		$options = array('conditions' => array('UserAction.' . $this->UserAction->primaryKey => $id));
187
		$this->set('userAction', $this->UserAction->find('first', $options));
188
	}
189
 
190
/**
191
 * add method
192
 *
193
 * @return void
194
 */
195
	public function add() {
196
		if ($this->request->is('post')) {
197
			$this->UserAction->create();
198
			if ($this->UserAction->save($this->request->data)) {
199
				$this->Session->setFlash(__('The user action has been saved.'));
200
				return $this->redirect(array('action' => 'index'));
201
			} else {
202
				$this->Session->setFlash(__('The user action could not be saved. Please, try again.'));
203
			}
204
		}
13562 anikendra 205
		// $users = $this->UserAction->User->find('list');
206
		// $storeProducts = $this->UserAction->StoreProduct->find('list');
207
		// $this->set(compact('users', 'storeProducts'));
13532 anikendra 208
	}
209
 
210
/**
211
 * edit method
212
 *
213
 * @throws NotFoundException
214
 * @param string $id
215
 * @return void
216
 */
217
	public function edit($id = null) {
218
		if (!$this->UserAction->exists($id)) {
219
			throw new NotFoundException(__('Invalid user action'));
220
		}
221
		if ($this->request->is(array('post', 'put'))) {
222
			if ($this->UserAction->save($this->request->data)) {
223
				$this->Session->setFlash(__('The user action has been saved.'));
224
				return $this->redirect(array('action' => 'index'));
225
			} else {
226
				$this->Session->setFlash(__('The user action could not be saved. Please, try again.'));
227
			}
228
		} else {
229
			$options = array('conditions' => array('UserAction.' . $this->UserAction->primaryKey => $id));
230
			$this->request->data = $this->UserAction->find('first', $options);
231
		}
232
		$users = $this->UserAction->User->find('list');
233
		$storeProducts = $this->UserAction->StoreProduct->find('list');
234
		$this->set(compact('users', 'storeProducts'));
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->UserAction->id = $id;
246
		if (!$this->UserAction->exists()) {
247
			throw new NotFoundException(__('Invalid user action'));
248
		}
249
		$this->request->onlyAllow('post', 'delete');
250
		if ($this->UserAction->delete()) {
251
			$this->Session->setFlash(__('The user action has been deleted.'));
252
		} else {
253
			$this->Session->setFlash(__('The user action 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->UserAction->recursive = 0;
265
		$this->set('userActions', $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->UserAction->exists($id)) {
277
			throw new NotFoundException(__('Invalid user action'));
278
		}
279
		$options = array('conditions' => array('UserAction.' . $this->UserAction->primaryKey => $id));
280
		$this->set('userAction', $this->UserAction->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
			$this->UserAction->create();
291
			if ($this->UserAction->save($this->request->data)) {
292
				$this->Session->setFlash(__('The user action has been saved.'));
293
				return $this->redirect(array('action' => 'index'));
294
			} else {
295
				$this->Session->setFlash(__('The user action could not be saved. Please, try again.'));
296
			}
297
		}
298
		$users = $this->UserAction->User->find('list');
299
		$storeProducts = $this->UserAction->StoreProduct->find('list');
300
		$this->set(compact('users', 'storeProducts'));
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->UserAction->exists($id)) {
312
			throw new NotFoundException(__('Invalid user action'));
313
		}
314
		if ($this->request->is(array('post', 'put'))) {
315
			if ($this->UserAction->save($this->request->data)) {
316
				$this->Session->setFlash(__('The user action has been saved.'));
317
				return $this->redirect(array('action' => 'index'));
318
			} else {
319
				$this->Session->setFlash(__('The user action could not be saved. Please, try again.'));
320
			}
321
		} else {
322
			$options = array('conditions' => array('UserAction.' . $this->UserAction->primaryKey => $id));
323
			$this->request->data = $this->UserAction->find('first', $options);
324
		}
325
		$users = $this->UserAction->User->find('list');
326
		$storeProducts = $this->UserAction->StoreProduct->find('list');
327
		$this->set(compact('users', 'storeProducts'));
328
	}
329
 
330
/**
331
 * admin_delete method
332
 *
333
 * @throws NotFoundException
334
 * @param string $id
335
 * @return void
336
 */
337
	public function admin_delete($id = null) {
338
		$this->UserAction->id = $id;
339
		if (!$this->UserAction->exists()) {
340
			throw new NotFoundException(__('Invalid user action'));
341
		}
342
		$this->request->onlyAllow('post', 'delete');
343
		if ($this->UserAction->delete()) {
344
			$this->Session->setFlash(__('The user action has been deleted.'));
345
		} else {
346
			$this->Session->setFlash(__('The user action could not be deleted. Please, try again.'));
347
		}
348
		return $this->redirect(array('action' => 'index'));
349
	}}