Subversion Repositories SmartDukaan

Rev

Rev 14424 | Rev 14561 | 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
 * SocialProfiles Controller
5
 *
6
 * @property SocialProfile $SocialProfile
7
 * @property PaginatorComponent $Paginator
8
 */
9
class SocialProfilesController extends AppController {
10
 
11
/**
12
 * Components
13
 *
14
 * @var array
15
 */
16
	public $components = array('Paginator');
17
 
18
	public function beforeFilter() {
19
		parent::beforeFilter();
20
		$this->Auth->allow('add');
21
		$callback = $this->request->query('callback');
13946 anikendra 22
		//Configure::load('dev');
13673 anikendra 23
		$this->apihost = Configure::read('saholicapihost');
13532 anikendra 24
	}
25
/**
26
 * index method
27
 *
28
 * @return void
29
 */
30
	public function index() {
31
		throw new NotFoundException(__('Access Denied'));
32
		$this->SocialProfile->recursive = 0;
33
		$this->set('socialProfiles', $this->Paginator->paginate());
34
	}
35
 
36
/**
37
 * view method
38
 *
39
 * @throws NotFoundException
40
 * @param string $id
41
 * @return void
42
 */
43
	public function view($id = null) {
44
		throw new NotFoundException(__('Access Denied'));
45
		if (!$this->SocialProfile->exists($id)) {
46
			throw new NotFoundException(__('Invalid social profile'));
47
		}
48
		$options = array('conditions' => array('SocialProfile.' . $this->SocialProfile->primaryKey => $id));
49
		$this->set('socialProfile', $this->SocialProfile->find('first', $options));
50
	}
51
 
52
/**
53
 * add method
54
 *
55
 * @return void
56
 */
57
	public function add() {
14068 anikendra 58
		$mobileRequired = true;
13532 anikendra 59
		if ($this->request->is('post')) {
13633 anikendra 60
			$this->log(print_r($this->request->data,1),'registration');
13532 anikendra 61
			$data = $this->request->data;
62
			$data['social_id'] = $this->request->data['id'];
63
			$data['access_token'] = $this->request->data['token'];
64
			unset($data['id']);
65
			unset($data['token']);
66
			unset($data['gender']);
67
			$this->response->type('json');
68
			$this->layout = 'ajax';
69
			$conditions = array('social_id'=>$this->request->data['id'],'type'=>$this->request->data['type']);
70
			$socialProfile = $this->SocialProfile->find('first',array('conditions'=>$conditions));
71
			if(empty($socialProfile)){
72
				//Check if user with same email is registered and if so just add his profile
13659 anikendra 73
				if(!empty($this->request->data['email'])) {
74
					$conditions = array('email'=>$this->request->data['email']);
75
					$user = $this->SocialProfile->User->find('first',array('conditions'=>$conditions));
76
 
77
					if(!empty($user)) {
14068 anikendra 78
						//Existing user
79
						if(!empty($user['User']['mobile_number'])){
80
							$mobileRequired = false;
81
						}
14425 anikendra 82
						if(strlen(trim($this->request->data['referrer']))>0) {
14424 anikendra 83
							$userData = array('id'=>$user['User']['id'],'email'=>$this->request->data['email'],'username'=>$this->request->data['email'],'first_name'=>$this->request->data['name'],'gender'=>$this->request->data['gender'],'referrer'=>$this->request->data['referrer'],'profile_pic'=> $this->request->data['profile_pic']);
84
						} else{
85
							$userData = array('id'=>$user['User']['id'],'email'=>$this->request->data['email'],'username'=>$this->request->data['email'],'first_name'=>$this->request->data['name'],'gender'=>$this->request->data['gender'],'profile_pic'=> $this->request->data['profile_pic']);
86
						}
13768 anikendra 87
						$this->SocialProfile->User->save($userData);
13659 anikendra 88
						$data['user_id'] = $user['User']['id'];	
13532 anikendra 89
					}else{
13659 anikendra 90
						//Create a new user and then insert user_id in social_profiles table
13769 anikendra 91
						$userData = array('profile_pic'=> $this->request->data['profile_pic'], 'email'=>$this->request->data['email'],'username'=>$this->request->data['email'],'first_name'=>$this->request->data['name'],'gender'=>$this->request->data['gender'],'referrer'=>$this->request->data['referrer']);
13659 anikendra 92
						if($this->SocialProfile->User->save($userData)) {
14424 anikendra 93
							$data['user_id'] = $this->SocialProfile->User->getLastInsertId();
14068 anikendra 94
						} else{
95
							$result = array('success' => false, 'message' => $this->SocialProfile->User->validationErrors,'mobileRequired'=>$mobileRequired);
13659 anikendra 96
							break;
97
						}
13532 anikendra 98
					}
13659 anikendra 99
					$this->SocialProfile->create();
100
					if ($this->SocialProfile->save($data)) {
14068 anikendra 101
						$result = array('success' => true, 'message' => 'Social Profile Created','id' => $data['user_id'],'mobileRequired'=>$mobileRequired);
13659 anikendra 102
					} else {
14068 anikendra 103
						$result = array('success' => false, 'message' => 'Social Profile Could Not Be Created','id' => -1,'mobileRequired'=>$mobileRequired);
13659 anikendra 104
					}
13532 anikendra 105
				} else {
14068 anikendra 106
					$result = array('success' => false, 'message' => "Email is missing",'mobileRequired'=>$mobileRequired);
13659 anikendra 107
					break;
108
				}				
13532 anikendra 109
			} else {
14425 anikendra 110
				if(strlen(trim($this->request->data['referrer']))>0) {
111
					$userData = array('id'=>$socialProfile['SocialProfile']['user_id'],'email'=>$this->request->data['email'],'username'=>$this->request->data['email'],'first_name'=>$this->request->data['name'],'gender'=>$this->request->data['gender'],'referrer'=>$this->request->data['referrer'],'profile_pic'=> $this->request->data['profile_pic']);
112
				} else{
113
					$userData = array('id'=>$socialProfile['SocialProfile']['user_id'],'email'=>$this->request->data['email'],'username'=>$this->request->data['email'],'first_name'=>$this->request->data['name'],'gender'=>$this->request->data['gender'],'profile_pic'=> $this->request->data['profile_pic']);
114
				}
13768 anikendra 115
				$this->SocialProfile->User->save($userData);
14139 anikendra 116
				//Check for mobile number
117
				$mobilenumber = $this->SocialProfile->User->find('first',array('conditions'=>array('id'=>$socialProfile['SocialProfile']['user_id']),'recursive'=>-1));
118
				if(!empty($mobilenumber['User']['mobile_number'])){
119
					$mobileRequired = false;
120
				}
13683 anikendra 121
				$data['user_id'] = $socialProfile['SocialProfile']['user_id'];
14068 anikendra 122
				$result = array('success' => true, 'message' => 'Existing Social Profile','id' => $socialProfile['SocialProfile']['user_id'],'mobileRequired'=>$mobileRequired);
13591 anikendra 123
			}
13532 anikendra 124
		}
13673 anikendra 125
		$this->updateSaholicUser($data['user_id'],$this->request->data['email']);
13532 anikendra 126
		$this->set(array(
127
		    'result' => $result,
128
		    'callback' => $callback,
129
		    '_serialize' => array('result')
130
		));
14425 anikendra 131
		$this->log(print_r($result,1),'registration');
13532 anikendra 132
		$this->render('/Elements/json');
133
	}
134
 
13673 anikendra 135
	private function updateSaholicUser($userId,$email=null) {
136
		if(!$email){
137
			//Handle it properly
138
			return;
139
		}
13683 anikendra 140
		$this->log('userId '.$userId,'registration');
141
		$this->log('email '.$email ,'registration');
13673 anikendra 142
		$this->loadModel('UserAccount');
143
		$options = array('conditions'=>array('user_id' => $userId,'account_type' => 'saholic'),'recursive'=>-1);
144
		$exists = $this->UserAccount->find('count',$options);
145
		if(!$exists){
13683 anikendra 146
			$url = $this->apihost."register?email=$email";
13673 anikendra 147
			$response = $this->make_request($url,null);
13683 anikendra 148
			$this->log('response '.print_r($response,1),'registration');
13673 anikendra 149
			if(!empty($response)){
150
				$data = array('account_type'=>'saholic','user_id'=>$userId,'account_key'=>$response['userId']);
151
				$this->UserAccount->create();
13683 anikendra 152
				$this->UserAccount->save($data);
14166 anikendra 153
				$data = array('account_type'=>'cartId','user_id'=>$userId,'account_key'=>$response['cartId']);
154
				$this->UserAccount->create();
155
				$this->UserAccount->save($data);
13673 anikendra 156
			}
157
		}
158
	}
13532 anikendra 159
/**
160
 * edit method
161
 *
162
 * @throws NotFoundException
163
 * @param string $id
164
 * @return void
165
 */
166
	public function edit($id = null) {
167
		if (!$this->SocialProfile->exists($id)) {
168
			throw new NotFoundException(__('Invalid social profile'));
169
		}
170
		if ($this->request->is(array('post', 'put'))) {
171
			if ($this->SocialProfile->save($this->request->data)) {
172
				$this->Session->setFlash(__('The social profile has been saved.'));
173
				return $this->redirect(array('action' => 'index'));
174
			} else {
175
				$this->Session->setFlash(__('The social profile could not be saved. Please, try again.'));
176
			}
177
		} else {
178
			$options = array('conditions' => array('SocialProfile.' . $this->SocialProfile->primaryKey => $id));
179
			$this->request->data = $this->SocialProfile->find('first', $options);
180
		}
181
		$users = $this->SocialProfile->User->find('list');
182
		$this->set(compact('users'));
183
	}
184
 
185
/**
186
 * delete method
187
 *
188
 * @throws NotFoundException
189
 * @param string $id
190
 * @return void
191
 */
192
	public function delete($id = null) {
193
		throw new NotFoundException(__('Access Denied'));
194
		$this->SocialProfile->id = $id;
195
		if (!$this->SocialProfile->exists()) {
196
			throw new NotFoundException(__('Invalid social profile'));
197
		}
198
		$this->request->onlyAllow('post', 'delete');
199
		if ($this->SocialProfile->delete()) {
200
			$this->Session->setFlash(__('The social profile has been deleted.'));
201
		} else {
202
			$this->Session->setFlash(__('The social profile could not be deleted. Please, try again.'));
203
		}
204
		return $this->redirect(array('action' => 'index'));
205
	}
206
 
207
/**
208
 * admin_index method
209
 *
210
 * @return void
211
 */
212
	public function admin_index() {
213
		$this->SocialProfile->recursive = 0;
214
		$this->set('socialProfiles', $this->Paginator->paginate());
215
	}
216
 
217
/**
218
 * admin_view method
219
 *
220
 * @throws NotFoundException
221
 * @param string $id
222
 * @return void
223
 */
224
	public function admin_view($id = null) {
225
		if (!$this->SocialProfile->exists($id)) {
226
			throw new NotFoundException(__('Invalid social profile'));
227
		}
228
		$options = array('conditions' => array('SocialProfile.' . $this->SocialProfile->primaryKey => $id));
229
		$this->set('socialProfile', $this->SocialProfile->find('first', $options));
230
	}
231
 
232
/**
233
 * admin_add method
234
 *
235
 * @return void
236
 */
237
	public function admin_add() {
238
		if ($this->request->is('post')) {
239
			$this->SocialProfile->create();
240
			if ($this->SocialProfile->save($this->request->data)) {
241
				$this->Session->setFlash(__('The social profile has been saved.'));
242
				return $this->redirect(array('action' => 'index'));
243
			} else {
244
				$this->Session->setFlash(__('The social profile could not be saved. Please, try again.'));
245
			}
246
		}
247
		$users = $this->SocialProfile->User->find('list');
248
		$this->set(compact('users'));
249
	}
250
 
251
/**
252
 * admin_edit method
253
 *
254
 * @throws NotFoundException
255
 * @param string $id
256
 * @return void
257
 */
258
	public function admin_edit($id = null) {
259
		if (!$this->SocialProfile->exists($id)) {
260
			throw new NotFoundException(__('Invalid social profile'));
261
		}
262
		if ($this->request->is(array('post', 'put'))) {
263
			if ($this->SocialProfile->save($this->request->data)) {
264
				$this->Session->setFlash(__('The social profile has been saved.'));
265
				return $this->redirect(array('action' => 'index'));
266
			} else {
267
				$this->Session->setFlash(__('The social profile could not be saved. Please, try again.'));
268
			}
269
		} else {
270
			$options = array('conditions' => array('SocialProfile.' . $this->SocialProfile->primaryKey => $id));
271
			$this->request->data = $this->SocialProfile->find('first', $options);
272
		}
273
		$users = $this->SocialProfile->User->find('list');
274
		$this->set(compact('users'));
275
	}
276
 
277
/**
278
 * admin_delete method
279
 *
280
 * @throws NotFoundException
281
 * @param string $id
282
 * @return void
283
 */
284
	public function admin_delete($id = null) {
285
		$this->SocialProfile->id = $id;
286
		if (!$this->SocialProfile->exists()) {
287
			throw new NotFoundException(__('Invalid social profile'));
288
		}
289
		$this->request->onlyAllow('post', 'delete');
290
		if ($this->SocialProfile->delete()) {
291
			$this->Session->setFlash(__('The social profile has been deleted.'));
292
		} else {
293
			$this->Session->setFlash(__('The social profile could not be deleted. Please, try again.'));
294
		}
295
		return $this->redirect(array('action' => 'index'));
296
	}}