Subversion Repositories SmartDukaan

Rev

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