Subversion Repositories SmartDukaan

Rev

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