Subversion Repositories SmartDukaan

Rev

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