Subversion Repositories SmartDukaan

Rev

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