Subversion Repositories SmartDukaan

Rev

Rev 15654 | Rev 17205 | 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();
15767 anikendra 20
		$this->Auth->allow('add','identifyUser');
13532 anikendra 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;
14768 anikendra 59
		$referrerRequired = true;
13532 anikendra 60
		if ($this->request->is('post')) {
13633 anikendra 61
			$this->log(print_r($this->request->data,1),'registration');
13532 anikendra 62
			$data = $this->request->data;
63
			$data['social_id'] = $this->request->data['id'];
64
			$data['access_token'] = $this->request->data['token'];
65
			unset($data['id']);
66
			unset($data['token']);
67
			unset($data['gender']);
68
			$this->response->type('json');
69
			$this->layout = 'ajax';
70
			$conditions = array('social_id'=>$this->request->data['id'],'type'=>$this->request->data['type']);
71
			$socialProfile = $this->SocialProfile->find('first',array('conditions'=>$conditions));
14783 anikendra 72
			//If Social profile doesn't exist
73
			if(empty($socialProfile)) {
13532 anikendra 74
				//Check if user with same email is registered and if so just add his profile
13659 anikendra 75
				if(!empty($this->request->data['email'])) {
76
					$conditions = array('email'=>$this->request->data['email']);
77
					$user = $this->SocialProfile->User->find('first',array('conditions'=>$conditions));
14783 anikendra 78
					/*if(!empty($this->request->data['referrer']) || !empty($this->request->data['utm_campaign'])){
14768 anikendra 79
						$referrerRequired = false;
14783 anikendra 80
					}*/
13659 anikendra 81
					if(!empty($user)) {
14068 anikendra 82
						//Existing user
83
						if(!empty($user['User']['mobile_number'])){
84
							$mobileRequired = false;
85
						}
14783 anikendra 86
						if(!empty($user['User']['activated']) && $user['User']['activated']==1) {
14768 anikendra 87
							$referrerRequired = false;
88
						}
14561 anikendra 89
						//Don't update referrer
90
						// if(strlen(trim($this->request->data['referrer']))>0) {
91
							// $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']);
92
						// } else{
14424 anikendra 93
							$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 94
						// }
13768 anikendra 95
						$this->SocialProfile->User->save($userData);
13659 anikendra 96
						$data['user_id'] = $user['User']['id'];	
14884 anikendra 97
					} else {						
13659 anikendra 98
						//Create a new user and then insert user_id in social_profiles table
13769 anikendra 99
						$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']);
14768 anikendra 100
						//Check for utm parameters
14702 anikendra 101
						if(!empty($this->request->data['utm_source'])){
102
							$userData['utm_source'] = $this->request->data['utm_source'];
103
						}
104
						if(!empty($this->request->data['utm_medium'])){
105
							$userData['utm_medium'] = $this->request->data['utm_medium'];
106
						}
107
						if(!empty($this->request->data['utm_term'])){
108
							$userData['utm_term'] = $this->request->data['utm_term'];
109
						}
110
						if(!empty($this->request->data['utm_content'])){
111
							$userData['utm_content'] = $this->request->data['utm_content'];
112
						}
113
						if(!empty($this->request->data['utm_campaign'])){
114
							$userData['utm_campaign'] = $this->request->data['utm_campaign'];
115
						}
14783 anikendra 116
						if(!empty($this->request->data['utm_campaign']) || !empty($this->request->data['referrer'])) {
15654 anikendra 117
							if(!empty($this->request->data['utm_campaign'])) {
118
								$referrer = $this->request->data['utm_campaign'];								
119
							}elseif (!empty($this->request->data['referrer'])) {
120
								$referrer = $this->request->data['referrer'];
121
							}
122
							$this->log(print_r($referrer,1),'activations');	
123
			        		$this->loadModel('ActivationCode');
124
			        		$exists = $this->ActivationCode->findByCode(strtoupper($referrer));
125
			        		$this->log(print_r($exists,1),'activations');
126
			        		if(empty($exists)){
127
			        			$referrerRequired = true;
128
			        		} else {
129
			        			$userData['activated'] = 1;							
130
			        			$referrerRequired = false;
131
			        		}														
14783 anikendra 132
						}
13659 anikendra 133
						if($this->SocialProfile->User->save($userData)) {
14424 anikendra 134
							$data['user_id'] = $this->SocialProfile->User->getLastInsertId();
14068 anikendra 135
						} else{
14768 anikendra 136
							$result = array('success' => false, 'message' => $this->SocialProfile->User->validationErrors,'mobileRequired'=>$mobileRequired,'referrerRequired'=>$referrerRequired);
13659 anikendra 137
							break;
138
						}
13532 anikendra 139
					}
13659 anikendra 140
					$this->SocialProfile->create();
141
					if ($this->SocialProfile->save($data)) {
14768 anikendra 142
						$result = array('success' => true, 'message' => 'Social Profile Created','id' => $data['user_id'],'mobileRequired'=>$mobileRequired,'referrerRequired'=>$referrerRequired);
15378 anikendra 143
						if(!$referrerRequired) {
144
							$this->markUserActivated($data['user_id']);
145
						}
13659 anikendra 146
					} else {
14783 anikendra 147
						$result = array('success' => false, 'message' => 'Social Profile Could Not Be Created','id' => -1,'mobileRequired'=>$mobileRequired,'referrerRequired'=>$referrerRequired);
13659 anikendra 148
					}
13532 anikendra 149
				} else {
14783 anikendra 150
					$result = array('success' => false, 'message' => "Email is missing",'mobileRequired'=>$mobileRequired,'referrerRequired'=>$referrerRequired);
13659 anikendra 151
					break;
152
				}				
13532 anikendra 153
			} else {
14783 anikendra 154
				//If Social Profile exists, do not update referrer
14768 anikendra 155
				$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']);
156
				//Check for utm parameters
157
				if(!empty($this->request->data['utm_source'])){
158
					$userData['utm_source'] = $this->request->data['utm_source'];
159
				}
160
				if(!empty($this->request->data['utm_medium'])){
161
					$userData['utm_medium'] = $this->request->data['utm_medium'];
162
				}
163
				if(!empty($this->request->data['utm_term'])){
164
					$userData['utm_term'] = $this->request->data['utm_term'];
165
				}
166
				if(!empty($this->request->data['utm_content'])){
167
					$userData['utm_content'] = $this->request->data['utm_content'];
168
				}
169
				if(!empty($this->request->data['utm_campaign'])){
170
					$userData['utm_campaign'] = $this->request->data['utm_campaign'];
171
				}				
13768 anikendra 172
				$this->SocialProfile->User->save($userData);
14890 anikendra 173
				//Update token ra
174
				$socialProfile['SocialProfile']['access_token'] = $data['access_token'];
175
				$this->SocialProfile->save($socialProfile);
14139 anikendra 176
				//Check for mobile number
177
				$mobilenumber = $this->SocialProfile->User->find('first',array('conditions'=>array('id'=>$socialProfile['SocialProfile']['user_id']),'recursive'=>-1));
178
				if(!empty($mobilenumber['User']['mobile_number'])){
179
					$mobileRequired = false;
180
				}
14783 anikendra 181
				//Check if activated feild is set or not.
182
				if(!empty($mobilenumber['User']['activated'])) {
14768 anikendra 183
					$referrerRequired = false;
184
				}
13683 anikendra 185
				$data['user_id'] = $socialProfile['SocialProfile']['user_id'];
14768 anikendra 186
				$result = array('success' => true, 'message' => 'Existing Social Profile','id' => $socialProfile['SocialProfile']['user_id'],'mobileRequired'=>$mobileRequired,'referrerRequired'=>$referrerRequired);
13591 anikendra 187
			}
13532 anikendra 188
		}
13673 anikendra 189
		$this->updateSaholicUser($data['user_id'],$this->request->data['email']);
13532 anikendra 190
		$this->set(array(
191
		    'result' => $result,
192
		    'callback' => $callback,
193
		    '_serialize' => array('result')
194
		));
14425 anikendra 195
		$this->log(print_r($result,1),'registration');
15767 anikendra 196
		//$this->identifyUser($data['user_id']);
13532 anikendra 197
		$this->render('/Elements/json');
198
	}
199
 
15606 anikendra 200
	private function identifyUser($id) {
201
		$options = array('conditions'=>array('id'=>$id),'recursive'=>-1);
202
		$user = $this->User->find('first',$options);
15767 anikendra 203
		$this->log("[Identify] ".print_r($user,1),'registration');
15606 anikendra 204
		$pmaurl = Configure::read('pmaurl');
205
		if(!empty($user)) {
206
			$data = array('id'=>$user['User']['id'],'email'=>$user['User']['email'],'mobilenumber'=>$user['User']['mobilenumber'],'name'=>$user['User']['first_name'],'referral_code'=>$user['User']['referrer']);
207
			$this->post_request($pmaurl.'/identify',$data);	
208
		}
209
	}
210
 
13673 anikendra 211
	private function updateSaholicUser($userId,$email=null) {
212
		if(!$email){
213
			//Handle it properly
214
			return;
215
		}
13683 anikendra 216
		$this->log('userId '.$userId,'registration');
217
		$this->log('email '.$email ,'registration');
13673 anikendra 218
		$this->loadModel('UserAccount');
219
		$options = array('conditions'=>array('user_id' => $userId,'account_type' => 'saholic'),'recursive'=>-1);
220
		$exists = $this->UserAccount->find('count',$options);
221
		if(!$exists){
14685 anikendra 222
			$url = $this->apihost."register?email=$email&from=profitmandi";
13673 anikendra 223
			$response = $this->make_request($url,null);
13683 anikendra 224
			$this->log('response '.print_r($response,1),'registration');
13673 anikendra 225
			if(!empty($response)){
14685 anikendra 226
				if($response['userId']<1)return;
13673 anikendra 227
				$data = array('account_type'=>'saholic','user_id'=>$userId,'account_key'=>$response['userId']);
228
				$this->UserAccount->create();
13683 anikendra 229
				$this->UserAccount->save($data);
14166 anikendra 230
				$data = array('account_type'=>'cartId','user_id'=>$userId,'account_key'=>$response['cartId']);
231
				$this->UserAccount->create();
232
				$this->UserAccount->save($data);
13673 anikendra 233
			}
234
		}
235
	}
13532 anikendra 236
/**
237
 * edit method
238
 *
239
 * @throws NotFoundException
240
 * @param string $id
241
 * @return void
242
 */
243
	public function 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
 * delete method
264
 *
265
 * @throws NotFoundException
266
 * @param string $id
267
 * @return void
268
 */
269
	public function delete($id = null) {
270
		throw new NotFoundException(__('Access Denied'));
271
		$this->SocialProfile->id = $id;
272
		if (!$this->SocialProfile->exists()) {
273
			throw new NotFoundException(__('Invalid social profile'));
274
		}
275
		$this->request->onlyAllow('post', 'delete');
276
		if ($this->SocialProfile->delete()) {
277
			$this->Session->setFlash(__('The social profile has been deleted.'));
278
		} else {
279
			$this->Session->setFlash(__('The social profile could not be deleted. Please, try again.'));
280
		}
281
		return $this->redirect(array('action' => 'index'));
282
	}
283
 
284
/**
285
 * admin_index method
286
 *
287
 * @return void
288
 */
289
	public function admin_index() {
290
		$this->SocialProfile->recursive = 0;
291
		$this->set('socialProfiles', $this->Paginator->paginate());
292
	}
293
 
294
/**
295
 * admin_view method
296
 *
297
 * @throws NotFoundException
298
 * @param string $id
299
 * @return void
300
 */
301
	public function admin_view($id = null) {
302
		if (!$this->SocialProfile->exists($id)) {
303
			throw new NotFoundException(__('Invalid social profile'));
304
		}
305
		$options = array('conditions' => array('SocialProfile.' . $this->SocialProfile->primaryKey => $id));
306
		$this->set('socialProfile', $this->SocialProfile->find('first', $options));
307
	}
308
 
309
/**
310
 * admin_add method
311
 *
312
 * @return void
313
 */
314
	public function admin_add() {
315
		if ($this->request->is('post')) {
316
			$this->SocialProfile->create();
317
			if ($this->SocialProfile->save($this->request->data)) {
318
				$this->Session->setFlash(__('The social profile has been saved.'));
319
				return $this->redirect(array('action' => 'index'));
320
			} else {
321
				$this->Session->setFlash(__('The social profile could not be saved. Please, try again.'));
322
			}
323
		}
324
		$users = $this->SocialProfile->User->find('list');
325
		$this->set(compact('users'));
326
	}
327
 
328
/**
329
 * admin_edit method
330
 *
331
 * @throws NotFoundException
332
 * @param string $id
333
 * @return void
334
 */
335
	public function admin_edit($id = null) {
336
		if (!$this->SocialProfile->exists($id)) {
337
			throw new NotFoundException(__('Invalid social profile'));
338
		}
339
		if ($this->request->is(array('post', 'put'))) {
340
			if ($this->SocialProfile->save($this->request->data)) {
341
				$this->Session->setFlash(__('The social profile has been saved.'));
342
				return $this->redirect(array('action' => 'index'));
343
			} else {
344
				$this->Session->setFlash(__('The social profile could not be saved. Please, try again.'));
345
			}
346
		} else {
347
			$options = array('conditions' => array('SocialProfile.' . $this->SocialProfile->primaryKey => $id));
348
			$this->request->data = $this->SocialProfile->find('first', $options);
349
		}
350
		$users = $this->SocialProfile->User->find('list');
351
		$this->set(compact('users'));
352
	}
353
 
354
/**
355
 * admin_delete method
356
 *
357
 * @throws NotFoundException
358
 * @param string $id
359
 * @return void
360
 */
361
	public function admin_delete($id = null) {
362
		$this->SocialProfile->id = $id;
363
		if (!$this->SocialProfile->exists()) {
364
			throw new NotFoundException(__('Invalid social profile'));
365
		}
366
		$this->request->onlyAllow('post', 'delete');
367
		if ($this->SocialProfile->delete()) {
368
			$this->Session->setFlash(__('The social profile has been deleted.'));
369
		} else {
370
			$this->Session->setFlash(__('The social profile could not be deleted. Please, try again.'));
371
		}
372
		return $this->redirect(array('action' => 'index'));
373
	}}