Subversion Repositories SmartDukaan

Rev

Rev 14068 | Rev 14166 | 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
						}
13769 anikendra 82
						$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']);
13768 anikendra 83
						$this->SocialProfile->User->save($userData);
13659 anikendra 84
						$data['user_id'] = $user['User']['id'];	
13532 anikendra 85
					}else{
13659 anikendra 86
						//Create a new user and then insert user_id in social_profiles table
13769 anikendra 87
						$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 88
						if($this->SocialProfile->User->save($userData)) {
14139 anikendra 89
						$data['user_id'] = $this->SocialProfile->User->getLastInsertId();
14068 anikendra 90
						} else{
91
							$result = array('success' => false, 'message' => $this->SocialProfile->User->validationErrors,'mobileRequired'=>$mobileRequired);
13659 anikendra 92
							break;
93
						}
13532 anikendra 94
					}
13659 anikendra 95
					$this->SocialProfile->create();
96
					if ($this->SocialProfile->save($data)) {
14068 anikendra 97
						$result = array('success' => true, 'message' => 'Social Profile Created','id' => $data['user_id'],'mobileRequired'=>$mobileRequired);
13659 anikendra 98
					} else {
14068 anikendra 99
						$result = array('success' => false, 'message' => 'Social Profile Could Not Be Created','id' => -1,'mobileRequired'=>$mobileRequired);
13659 anikendra 100
					}
13532 anikendra 101
				} else {
14068 anikendra 102
					$result = array('success' => false, 'message' => "Email is missing",'mobileRequired'=>$mobileRequired);
13659 anikendra 103
					break;
104
				}				
13532 anikendra 105
			} else {
13769 anikendra 106
				$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']);
13768 anikendra 107
				$this->SocialProfile->User->save($userData);
14139 anikendra 108
				//Check for mobile number
109
				$mobilenumber = $this->SocialProfile->User->find('first',array('conditions'=>array('id'=>$socialProfile['SocialProfile']['user_id']),'recursive'=>-1));
110
				$this->log(print_r($mobilenumber->User,1),'registration');
111
				if(!empty($mobilenumber['User']['mobile_number'])){
112
					$mobileRequired = false;
113
				}
13683 anikendra 114
				$data['user_id'] = $socialProfile['SocialProfile']['user_id'];
14068 anikendra 115
				$result = array('success' => true, 'message' => 'Existing Social Profile','id' => $socialProfile['SocialProfile']['user_id'],'mobileRequired'=>$mobileRequired);
13591 anikendra 116
			}
13532 anikendra 117
		}
13673 anikendra 118
		$this->updateSaholicUser($data['user_id'],$this->request->data['email']);
13532 anikendra 119
		$this->set(array(
120
		    'result' => $result,
121
		    'callback' => $callback,
122
		    '_serialize' => array('result')
123
		));
124
		//$this->render('/Elements/jsonp');
125
		$this->render('/Elements/json');
126
	}
127
 
13673 anikendra 128
	private function updateSaholicUser($userId,$email=null) {
129
		if(!$email){
130
			//Handle it properly
131
			return;
132
		}
13683 anikendra 133
		$this->log('userId '.$userId,'registration');
134
		$this->log('email '.$email ,'registration');
13673 anikendra 135
		$this->loadModel('UserAccount');
136
		$options = array('conditions'=>array('user_id' => $userId,'account_type' => 'saholic'),'recursive'=>-1);
137
		$exists = $this->UserAccount->find('count',$options);
138
		if(!$exists){
13683 anikendra 139
			$url = $this->apihost."register?email=$email";
13673 anikendra 140
			$response = $this->make_request($url,null);
13683 anikendra 141
			$this->log('response '.print_r($response,1),'registration');
13673 anikendra 142
			if(!empty($response)){
143
				$data = array('account_type'=>'saholic','user_id'=>$userId,'account_key'=>$response['userId']);
144
				$this->UserAccount->create();
13683 anikendra 145
				$this->UserAccount->save($data);
13673 anikendra 146
			}
147
		}
148
	}
13532 anikendra 149
/**
150
 * edit method
151
 *
152
 * @throws NotFoundException
153
 * @param string $id
154
 * @return void
155
 */
156
	public function edit($id = null) {
157
		if (!$this->SocialProfile->exists($id)) {
158
			throw new NotFoundException(__('Invalid social profile'));
159
		}
160
		if ($this->request->is(array('post', 'put'))) {
161
			if ($this->SocialProfile->save($this->request->data)) {
162
				$this->Session->setFlash(__('The social profile has been saved.'));
163
				return $this->redirect(array('action' => 'index'));
164
			} else {
165
				$this->Session->setFlash(__('The social profile could not be saved. Please, try again.'));
166
			}
167
		} else {
168
			$options = array('conditions' => array('SocialProfile.' . $this->SocialProfile->primaryKey => $id));
169
			$this->request->data = $this->SocialProfile->find('first', $options);
170
		}
171
		$users = $this->SocialProfile->User->find('list');
172
		$this->set(compact('users'));
173
	}
174
 
175
/**
176
 * delete method
177
 *
178
 * @throws NotFoundException
179
 * @param string $id
180
 * @return void
181
 */
182
	public function delete($id = null) {
183
		throw new NotFoundException(__('Access Denied'));
184
		$this->SocialProfile->id = $id;
185
		if (!$this->SocialProfile->exists()) {
186
			throw new NotFoundException(__('Invalid social profile'));
187
		}
188
		$this->request->onlyAllow('post', 'delete');
189
		if ($this->SocialProfile->delete()) {
190
			$this->Session->setFlash(__('The social profile has been deleted.'));
191
		} else {
192
			$this->Session->setFlash(__('The social profile could not be deleted. Please, try again.'));
193
		}
194
		return $this->redirect(array('action' => 'index'));
195
	}
196
 
197
/**
198
 * admin_index method
199
 *
200
 * @return void
201
 */
202
	public function admin_index() {
203
		$this->SocialProfile->recursive = 0;
204
		$this->set('socialProfiles', $this->Paginator->paginate());
205
	}
206
 
207
/**
208
 * admin_view method
209
 *
210
 * @throws NotFoundException
211
 * @param string $id
212
 * @return void
213
 */
214
	public function admin_view($id = null) {
215
		if (!$this->SocialProfile->exists($id)) {
216
			throw new NotFoundException(__('Invalid social profile'));
217
		}
218
		$options = array('conditions' => array('SocialProfile.' . $this->SocialProfile->primaryKey => $id));
219
		$this->set('socialProfile', $this->SocialProfile->find('first', $options));
220
	}
221
 
222
/**
223
 * admin_add method
224
 *
225
 * @return void
226
 */
227
	public function admin_add() {
228
		if ($this->request->is('post')) {
229
			$this->SocialProfile->create();
230
			if ($this->SocialProfile->save($this->request->data)) {
231
				$this->Session->setFlash(__('The social profile has been saved.'));
232
				return $this->redirect(array('action' => 'index'));
233
			} else {
234
				$this->Session->setFlash(__('The social profile could not be saved. Please, try again.'));
235
			}
236
		}
237
		$users = $this->SocialProfile->User->find('list');
238
		$this->set(compact('users'));
239
	}
240
 
241
/**
242
 * admin_edit method
243
 *
244
 * @throws NotFoundException
245
 * @param string $id
246
 * @return void
247
 */
248
	public function admin_edit($id = null) {
249
		if (!$this->SocialProfile->exists($id)) {
250
			throw new NotFoundException(__('Invalid social profile'));
251
		}
252
		if ($this->request->is(array('post', 'put'))) {
253
			if ($this->SocialProfile->save($this->request->data)) {
254
				$this->Session->setFlash(__('The social profile has been saved.'));
255
				return $this->redirect(array('action' => 'index'));
256
			} else {
257
				$this->Session->setFlash(__('The social profile could not be saved. Please, try again.'));
258
			}
259
		} else {
260
			$options = array('conditions' => array('SocialProfile.' . $this->SocialProfile->primaryKey => $id));
261
			$this->request->data = $this->SocialProfile->find('first', $options);
262
		}
263
		$users = $this->SocialProfile->User->find('list');
264
		$this->set(compact('users'));
265
	}
266
 
267
/**
268
 * admin_delete method
269
 *
270
 * @throws NotFoundException
271
 * @param string $id
272
 * @return void
273
 */
274
	public function admin_delete($id = null) {
275
		$this->SocialProfile->id = $id;
276
		if (!$this->SocialProfile->exists()) {
277
			throw new NotFoundException(__('Invalid social profile'));
278
		}
279
		$this->request->onlyAllow('post', 'delete');
280
		if ($this->SocialProfile->delete()) {
281
			$this->Session->setFlash(__('The social profile has been deleted.'));
282
		} else {
283
			$this->Session->setFlash(__('The social profile could not be deleted. Please, try again.'));
284
		}
285
		return $this->redirect(array('action' => 'index'));
286
	}}