Subversion Repositories SmartDukaan

Rev

Rev 14139 | Rev 14424 | 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);
14166 anikendra 146
				$data = array('account_type'=>'cartId','user_id'=>$userId,'account_key'=>$response['cartId']);
147
				$this->UserAccount->create();
148
				$this->UserAccount->save($data);
13673 anikendra 149
			}
150
		}
151
	}
13532 anikendra 152
/**
153
 * edit method
154
 *
155
 * @throws NotFoundException
156
 * @param string $id
157
 * @return void
158
 */
159
	public function edit($id = null) {
160
		if (!$this->SocialProfile->exists($id)) {
161
			throw new NotFoundException(__('Invalid social profile'));
162
		}
163
		if ($this->request->is(array('post', 'put'))) {
164
			if ($this->SocialProfile->save($this->request->data)) {
165
				$this->Session->setFlash(__('The social profile has been saved.'));
166
				return $this->redirect(array('action' => 'index'));
167
			} else {
168
				$this->Session->setFlash(__('The social profile could not be saved. Please, try again.'));
169
			}
170
		} else {
171
			$options = array('conditions' => array('SocialProfile.' . $this->SocialProfile->primaryKey => $id));
172
			$this->request->data = $this->SocialProfile->find('first', $options);
173
		}
174
		$users = $this->SocialProfile->User->find('list');
175
		$this->set(compact('users'));
176
	}
177
 
178
/**
179
 * delete method
180
 *
181
 * @throws NotFoundException
182
 * @param string $id
183
 * @return void
184
 */
185
	public function delete($id = null) {
186
		throw new NotFoundException(__('Access Denied'));
187
		$this->SocialProfile->id = $id;
188
		if (!$this->SocialProfile->exists()) {
189
			throw new NotFoundException(__('Invalid social profile'));
190
		}
191
		$this->request->onlyAllow('post', 'delete');
192
		if ($this->SocialProfile->delete()) {
193
			$this->Session->setFlash(__('The social profile has been deleted.'));
194
		} else {
195
			$this->Session->setFlash(__('The social profile could not be deleted. Please, try again.'));
196
		}
197
		return $this->redirect(array('action' => 'index'));
198
	}
199
 
200
/**
201
 * admin_index method
202
 *
203
 * @return void
204
 */
205
	public function admin_index() {
206
		$this->SocialProfile->recursive = 0;
207
		$this->set('socialProfiles', $this->Paginator->paginate());
208
	}
209
 
210
/**
211
 * admin_view method
212
 *
213
 * @throws NotFoundException
214
 * @param string $id
215
 * @return void
216
 */
217
	public function admin_view($id = null) {
218
		if (!$this->SocialProfile->exists($id)) {
219
			throw new NotFoundException(__('Invalid social profile'));
220
		}
221
		$options = array('conditions' => array('SocialProfile.' . $this->SocialProfile->primaryKey => $id));
222
		$this->set('socialProfile', $this->SocialProfile->find('first', $options));
223
	}
224
 
225
/**
226
 * admin_add method
227
 *
228
 * @return void
229
 */
230
	public function admin_add() {
231
		if ($this->request->is('post')) {
232
			$this->SocialProfile->create();
233
			if ($this->SocialProfile->save($this->request->data)) {
234
				$this->Session->setFlash(__('The social profile has been saved.'));
235
				return $this->redirect(array('action' => 'index'));
236
			} else {
237
				$this->Session->setFlash(__('The social profile could not be saved. Please, try again.'));
238
			}
239
		}
240
		$users = $this->SocialProfile->User->find('list');
241
		$this->set(compact('users'));
242
	}
243
 
244
/**
245
 * admin_edit method
246
 *
247
 * @throws NotFoundException
248
 * @param string $id
249
 * @return void
250
 */
251
	public function admin_edit($id = null) {
252
		if (!$this->SocialProfile->exists($id)) {
253
			throw new NotFoundException(__('Invalid social profile'));
254
		}
255
		if ($this->request->is(array('post', 'put'))) {
256
			if ($this->SocialProfile->save($this->request->data)) {
257
				$this->Session->setFlash(__('The social profile has been saved.'));
258
				return $this->redirect(array('action' => 'index'));
259
			} else {
260
				$this->Session->setFlash(__('The social profile could not be saved. Please, try again.'));
261
			}
262
		} else {
263
			$options = array('conditions' => array('SocialProfile.' . $this->SocialProfile->primaryKey => $id));
264
			$this->request->data = $this->SocialProfile->find('first', $options);
265
		}
266
		$users = $this->SocialProfile->User->find('list');
267
		$this->set(compact('users'));
268
	}
269
 
270
/**
271
 * admin_delete method
272
 *
273
 * @throws NotFoundException
274
 * @param string $id
275
 * @return void
276
 */
277
	public function admin_delete($id = null) {
278
		$this->SocialProfile->id = $id;
279
		if (!$this->SocialProfile->exists()) {
280
			throw new NotFoundException(__('Invalid social profile'));
281
		}
282
		$this->request->onlyAllow('post', 'delete');
283
		if ($this->SocialProfile->delete()) {
284
			$this->Session->setFlash(__('The social profile has been deleted.'));
285
		} else {
286
			$this->Session->setFlash(__('The social profile could not be deleted. Please, try again.'));
287
		}
288
		return $this->redirect(array('action' => 'index'));
289
	}}