Subversion Repositories SmartDukaan

Rev

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