Subversion Repositories SmartDukaan

Rev

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