Subversion Repositories SmartDukaan

Rev

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