Subversion Repositories SmartDukaan

Rev

Rev 13674 | Rev 13685 | 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();
20
		$this->Auth->allow('update','by','rem');
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
 
78
	public function by($user_id=null){
79
		if(!empty($user_id)){
80
			$this->response->type('json');
81
			$this->layout = 'ajax';
82
			$callback = $this->request->query('callback');
83
			$this->UserAction->recursive = -1;
84
			$conditions = array('user_id' => $user_id);
13583 anikendra 85
			$result['actions'] = $this->UserAction->find('all',array('conditions'=>$conditions));
13562 anikendra 86
			$this->set(array(
87
			    'result' => $result,
88
			    'callback' => $callback,
89
			    '_serialize' => array('result')
90
			));
13583 anikendra 91
			$this->render('/Elements/json');
13562 anikendra 92
		}
93
	}
13532 anikendra 94
/**
95
 * index method
96
 *
97
 * @return void
98
 */
99
	public function index() {
100
		$this->UserAction->recursive = 0;
101
		$this->set('userActions', $this->Paginator->paginate());
102
	}
103
 
104
/**
105
 * view method
106
 *
107
 * @throws NotFoundException
108
 * @param string $id
109
 * @return void
110
 */
111
	public function view($id = null) {
112
		if (!$this->UserAction->exists($id)) {
113
			throw new NotFoundException(__('Invalid user action'));
114
		}
115
		$options = array('conditions' => array('UserAction.' . $this->UserAction->primaryKey => $id));
116
		$this->set('userAction', $this->UserAction->find('first', $options));
117
	}
118
 
119
/**
120
 * add method
121
 *
122
 * @return void
123
 */
124
	public function add() {
125
		if ($this->request->is('post')) {
126
			$this->UserAction->create();
127
			if ($this->UserAction->save($this->request->data)) {
128
				$this->Session->setFlash(__('The user action has been saved.'));
129
				return $this->redirect(array('action' => 'index'));
130
			} else {
131
				$this->Session->setFlash(__('The user action could not be saved. Please, try again.'));
132
			}
133
		}
13562 anikendra 134
		// $users = $this->UserAction->User->find('list');
135
		// $storeProducts = $this->UserAction->StoreProduct->find('list');
136
		// $this->set(compact('users', 'storeProducts'));
13532 anikendra 137
	}
138
 
139
/**
140
 * edit method
141
 *
142
 * @throws NotFoundException
143
 * @param string $id
144
 * @return void
145
 */
146
	public function edit($id = null) {
147
		if (!$this->UserAction->exists($id)) {
148
			throw new NotFoundException(__('Invalid user action'));
149
		}
150
		if ($this->request->is(array('post', 'put'))) {
151
			if ($this->UserAction->save($this->request->data)) {
152
				$this->Session->setFlash(__('The user action has been saved.'));
153
				return $this->redirect(array('action' => 'index'));
154
			} else {
155
				$this->Session->setFlash(__('The user action could not be saved. Please, try again.'));
156
			}
157
		} else {
158
			$options = array('conditions' => array('UserAction.' . $this->UserAction->primaryKey => $id));
159
			$this->request->data = $this->UserAction->find('first', $options);
160
		}
161
		$users = $this->UserAction->User->find('list');
162
		$storeProducts = $this->UserAction->StoreProduct->find('list');
163
		$this->set(compact('users', 'storeProducts'));
164
	}
165
 
166
/**
167
 * delete method
168
 *
169
 * @throws NotFoundException
170
 * @param string $id
171
 * @return void
172
 */
173
	public function delete($id = null) {
174
		$this->UserAction->id = $id;
175
		if (!$this->UserAction->exists()) {
176
			throw new NotFoundException(__('Invalid user action'));
177
		}
178
		$this->request->onlyAllow('post', 'delete');
179
		if ($this->UserAction->delete()) {
180
			$this->Session->setFlash(__('The user action has been deleted.'));
181
		} else {
182
			$this->Session->setFlash(__('The user action could not be deleted. Please, try again.'));
183
		}
184
		return $this->redirect(array('action' => 'index'));
185
	}
186
 
187
/**
188
 * admin_index method
189
 *
190
 * @return void
191
 */
192
	public function admin_index() {
193
		$this->UserAction->recursive = 0;
194
		$this->set('userActions', $this->Paginator->paginate());
195
	}
196
 
197
/**
198
 * admin_view method
199
 *
200
 * @throws NotFoundException
201
 * @param string $id
202
 * @return void
203
 */
204
	public function admin_view($id = null) {
205
		if (!$this->UserAction->exists($id)) {
206
			throw new NotFoundException(__('Invalid user action'));
207
		}
208
		$options = array('conditions' => array('UserAction.' . $this->UserAction->primaryKey => $id));
209
		$this->set('userAction', $this->UserAction->find('first', $options));
210
	}
211
 
212
/**
213
 * admin_add method
214
 *
215
 * @return void
216
 */
217
	public function admin_add() {
218
		if ($this->request->is('post')) {
219
			$this->UserAction->create();
220
			if ($this->UserAction->save($this->request->data)) {
221
				$this->Session->setFlash(__('The user action has been saved.'));
222
				return $this->redirect(array('action' => 'index'));
223
			} else {
224
				$this->Session->setFlash(__('The user action could not be saved. Please, try again.'));
225
			}
226
		}
227
		$users = $this->UserAction->User->find('list');
228
		$storeProducts = $this->UserAction->StoreProduct->find('list');
229
		$this->set(compact('users', 'storeProducts'));
230
	}
231
 
232
/**
233
 * admin_edit method
234
 *
235
 * @throws NotFoundException
236
 * @param string $id
237
 * @return void
238
 */
239
	public function admin_edit($id = null) {
240
		if (!$this->UserAction->exists($id)) {
241
			throw new NotFoundException(__('Invalid user action'));
242
		}
243
		if ($this->request->is(array('post', 'put'))) {
244
			if ($this->UserAction->save($this->request->data)) {
245
				$this->Session->setFlash(__('The user action has been saved.'));
246
				return $this->redirect(array('action' => 'index'));
247
			} else {
248
				$this->Session->setFlash(__('The user action could not be saved. Please, try again.'));
249
			}
250
		} else {
251
			$options = array('conditions' => array('UserAction.' . $this->UserAction->primaryKey => $id));
252
			$this->request->data = $this->UserAction->find('first', $options);
253
		}
254
		$users = $this->UserAction->User->find('list');
255
		$storeProducts = $this->UserAction->StoreProduct->find('list');
256
		$this->set(compact('users', 'storeProducts'));
257
	}
258
 
259
/**
260
 * admin_delete method
261
 *
262
 * @throws NotFoundException
263
 * @param string $id
264
 * @return void
265
 */
266
	public function admin_delete($id = null) {
267
		$this->UserAction->id = $id;
268
		if (!$this->UserAction->exists()) {
269
			throw new NotFoundException(__('Invalid user action'));
270
		}
271
		$this->request->onlyAllow('post', 'delete');
272
		if ($this->UserAction->delete()) {
273
			$this->Session->setFlash(__('The user action has been deleted.'));
274
		} else {
275
			$this->Session->setFlash(__('The user action could not be deleted. Please, try again.'));
276
		}
277
		return $this->redirect(array('action' => 'index'));
278
	}}