Subversion Repositories SmartDukaan

Rev

Rev 13685 | Rev 13750 | 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
 * 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');
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');
32
		$contain = array('StoreProduct.title','StoreProduct.thumbnail','StoreProduct.mrp','StoreProduct.available_price');
33
		$options = array('contain'=>$contain,'conditions'=>array('UserAction.user_id'=>$this->Auth->User('id'),'UserAction.action'=>'like'));
34
		$myfavorites = $this->UserAction->find('all',$options);
13674 anikendra 35
		$this->set(compact('myfavorites'));
13672 anikendra 36
	}
37
 
13562 anikendra 38
	public function update($user_id,$store_product_id,$action) {
39
		if(!empty($user_id) && !empty($store_product_id) && !empty($action)) {
40
			$data = array('user_id'=>$user_id,'store_product_id'=>$store_product_id);
41
			$this->UserAction->deleteAll(array($data),false);
42
			$this->response->type('json');
43
			$this->layout = 'ajax';
44
			$callback = $this->request->query('callback');
45
			$data['action'] = $action;
46
			$this->UserAction->create();
47
			if ($this->UserAction->save($data)) {
48
				$result = array('success'=>true,'message'=>(__('The user action has been saved.')));
49
			} else {
50
				$result = array('success'=>false,(__('The user action could not be saved. Please, try again.')));
51
			}
52
			$this->set(array(
53
			    'result' => $result,
54
			    'callback' => $callback,
55
			    '_serialize' => array('result')
56
			));
57
			$this->render('/Elements/jsonp');
58
		}		
59
	}
60
 
61
	public function rem($user_id,$store_product_id,$action) {
62
		if(!empty($user_id) && !empty($store_product_id) && !empty($action)) {
63
			$data = array('user_id'=>$user_id,'store_product_id'=>$store_product_id);
64
			$this->UserAction->deleteAll(array($data),false);
65
			$this->response->type('json');
66
			$this->layout = 'ajax';
67
			$callback = $this->request->query('callback');			
68
			$result = array('success'=>true,'message'=>(__('The user action has been deleted.')));
69
			$this->set(array(
70
			    'result' => $result,
71
			    'callback' => $callback,
72
			    '_serialize' => array('result')
73
			));
74
			$this->render('/Elements/jsonp');
75
		}		
76
	}
77
 
13731 anikendra 78
	public function deletefav($user_id,$id) {
79
		if(!empty($user_id) && !empty($id)) {
80
			$loggedInUserId = $this->Auth->User('id');
81
			if($user_id != $loggedInUserId){
82
				$result = array('success'=>false,'message'=>(__('Unauthorized access. Try again later')));
83
			} else {
84
				$sql = "DELETE FROM user_actions WHERE id = $id AND user_id = $user_id";
85
				$this->UserAction->query($sql);
86
				$result = array('success'=>true,'message'=>(__('The favourite product has been removed.')));				
87
			}
88
			$this->response->type('json');
89
			$this->layout = 'ajax';
90
			$callback = $this->request->query('callback');			
91
			$this->set(array(
92
			    'result' => $result,
93
			    'callback' => $callback,
94
			    '_serialize' => array('result')
95
			));
96
			$this->render('/Elements/jsonp');
97
		}		
98
	}
99
 
100
	public function deleteallfavs($user_id) {
101
		if(!empty($user_id)) {
102
			$loggedInUserId = $this->Auth->User('id');
103
			if($user_id != $loggedInUserId){
104
				$result = array('success'=>false,'message'=>(__('Unauthorized access. Try again later')));
105
			} else {
106
				$sql = "DELETE FROM user_actions WHERE user_id = $user_id";
107
				$this->UserAction->query($sql);
108
				$result = array('success'=>true,'message'=>(__('All favourite products have been removed.')));				
109
			}
110
			$this->response->type('json');
111
			$this->layout = 'ajax';
112
			$callback = $this->request->query('callback');			
113
			$this->set(array(
114
			    'result' => $result,
115
			    'callback' => $callback,
116
			    '_serialize' => array('result')
117
			));
118
			$this->render('/Elements/jsonp');
119
		}		
120
	}
121
 
13562 anikendra 122
	public function by($user_id=null){
123
		if(!empty($user_id)){
124
			$this->response->type('json');
125
			$this->layout = 'ajax';
126
			$callback = $this->request->query('callback');
127
			$this->UserAction->recursive = -1;
128
			$conditions = array('user_id' => $user_id);
13583 anikendra 129
			$result['actions'] = $this->UserAction->find('all',array('conditions'=>$conditions));
13562 anikendra 130
			$this->set(array(
131
			    'result' => $result,
132
			    'callback' => $callback,
133
			    '_serialize' => array('result')
134
			));
13583 anikendra 135
			$this->render('/Elements/json');
13562 anikendra 136
		}
137
	}
13532 anikendra 138
/**
139
 * index method
140
 *
141
 * @return void
142
 */
143
	public function index() {
144
		$this->UserAction->recursive = 0;
145
		$this->set('userActions', $this->Paginator->paginate());
146
	}
147
 
148
/**
149
 * view method
150
 *
151
 * @throws NotFoundException
152
 * @param string $id
153
 * @return void
154
 */
155
	public function view($id = null) {
156
		if (!$this->UserAction->exists($id)) {
157
			throw new NotFoundException(__('Invalid user action'));
158
		}
159
		$options = array('conditions' => array('UserAction.' . $this->UserAction->primaryKey => $id));
160
		$this->set('userAction', $this->UserAction->find('first', $options));
161
	}
162
 
163
/**
164
 * add method
165
 *
166
 * @return void
167
 */
168
	public function add() {
169
		if ($this->request->is('post')) {
170
			$this->UserAction->create();
171
			if ($this->UserAction->save($this->request->data)) {
172
				$this->Session->setFlash(__('The user action has been saved.'));
173
				return $this->redirect(array('action' => 'index'));
174
			} else {
175
				$this->Session->setFlash(__('The user action could not be saved. Please, try again.'));
176
			}
177
		}
13562 anikendra 178
		// $users = $this->UserAction->User->find('list');
179
		// $storeProducts = $this->UserAction->StoreProduct->find('list');
180
		// $this->set(compact('users', 'storeProducts'));
13532 anikendra 181
	}
182
 
183
/**
184
 * edit method
185
 *
186
 * @throws NotFoundException
187
 * @param string $id
188
 * @return void
189
 */
190
	public function edit($id = null) {
191
		if (!$this->UserAction->exists($id)) {
192
			throw new NotFoundException(__('Invalid user action'));
193
		}
194
		if ($this->request->is(array('post', 'put'))) {
195
			if ($this->UserAction->save($this->request->data)) {
196
				$this->Session->setFlash(__('The user action has been saved.'));
197
				return $this->redirect(array('action' => 'index'));
198
			} else {
199
				$this->Session->setFlash(__('The user action could not be saved. Please, try again.'));
200
			}
201
		} else {
202
			$options = array('conditions' => array('UserAction.' . $this->UserAction->primaryKey => $id));
203
			$this->request->data = $this->UserAction->find('first', $options);
204
		}
205
		$users = $this->UserAction->User->find('list');
206
		$storeProducts = $this->UserAction->StoreProduct->find('list');
207
		$this->set(compact('users', 'storeProducts'));
208
	}
209
 
210
/**
211
 * delete method
212
 *
213
 * @throws NotFoundException
214
 * @param string $id
215
 * @return void
216
 */
217
	public function delete($id = null) {
218
		$this->UserAction->id = $id;
219
		if (!$this->UserAction->exists()) {
220
			throw new NotFoundException(__('Invalid user action'));
221
		}
222
		$this->request->onlyAllow('post', 'delete');
223
		if ($this->UserAction->delete()) {
224
			$this->Session->setFlash(__('The user action has been deleted.'));
225
		} else {
226
			$this->Session->setFlash(__('The user action could not be deleted. Please, try again.'));
227
		}
228
		return $this->redirect(array('action' => 'index'));
229
	}
230
 
231
/**
232
 * admin_index method
233
 *
234
 * @return void
235
 */
236
	public function admin_index() {
237
		$this->UserAction->recursive = 0;
238
		$this->set('userActions', $this->Paginator->paginate());
239
	}
240
 
241
/**
242
 * admin_view method
243
 *
244
 * @throws NotFoundException
245
 * @param string $id
246
 * @return void
247
 */
248
	public function admin_view($id = null) {
249
		if (!$this->UserAction->exists($id)) {
250
			throw new NotFoundException(__('Invalid user action'));
251
		}
252
		$options = array('conditions' => array('UserAction.' . $this->UserAction->primaryKey => $id));
253
		$this->set('userAction', $this->UserAction->find('first', $options));
254
	}
255
 
256
/**
257
 * admin_add method
258
 *
259
 * @return void
260
 */
261
	public function admin_add() {
262
		if ($this->request->is('post')) {
263
			$this->UserAction->create();
264
			if ($this->UserAction->save($this->request->data)) {
265
				$this->Session->setFlash(__('The user action has been saved.'));
266
				return $this->redirect(array('action' => 'index'));
267
			} else {
268
				$this->Session->setFlash(__('The user action could not be saved. Please, try again.'));
269
			}
270
		}
271
		$users = $this->UserAction->User->find('list');
272
		$storeProducts = $this->UserAction->StoreProduct->find('list');
273
		$this->set(compact('users', 'storeProducts'));
274
	}
275
 
276
/**
277
 * admin_edit method
278
 *
279
 * @throws NotFoundException
280
 * @param string $id
281
 * @return void
282
 */
283
	public function admin_edit($id = null) {
284
		if (!$this->UserAction->exists($id)) {
285
			throw new NotFoundException(__('Invalid user action'));
286
		}
287
		if ($this->request->is(array('post', 'put'))) {
288
			if ($this->UserAction->save($this->request->data)) {
289
				$this->Session->setFlash(__('The user action has been saved.'));
290
				return $this->redirect(array('action' => 'index'));
291
			} else {
292
				$this->Session->setFlash(__('The user action could not be saved. Please, try again.'));
293
			}
294
		} else {
295
			$options = array('conditions' => array('UserAction.' . $this->UserAction->primaryKey => $id));
296
			$this->request->data = $this->UserAction->find('first', $options);
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_delete method
305
 *
306
 * @throws NotFoundException
307
 * @param string $id
308
 * @return void
309
 */
310
	public function admin_delete($id = null) {
311
		$this->UserAction->id = $id;
312
		if (!$this->UserAction->exists()) {
313
			throw new NotFoundException(__('Invalid user action'));
314
		}
315
		$this->request->onlyAllow('post', 'delete');
316
		if ($this->UserAction->delete()) {
317
			$this->Session->setFlash(__('The user action has been deleted.'));
318
		} else {
319
			$this->Session->setFlash(__('The user action could not be deleted. Please, try again.'));
320
		}
321
		return $this->redirect(array('action' => 'index'));
322
	}}