Subversion Repositories SmartDukaan

Rev

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