Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
15403 manish.sha 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');
22
		//Configure::load('dev');
23
		$this->apihost = Configure::read('saholicapihost');
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() {
58
		$mobileRequired = true;
59
		$referrerRequired = true;
60
		if ($this->request->is('post')) {
61
			$this->log(print_r($this->request->data,1),'registration');
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));
72
			//If Social profile doesn't exist
73
			if(empty($socialProfile)) {
74
				//Check if user with same email is registered and if so just add his profile
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));
78
					/*if(!empty($this->request->data['referrer']) || !empty($this->request->data['utm_campaign'])){
79
						$referrerRequired = false;
80
					}*/
81
					if(!empty($user)) {
82
						//Existing user
83
						if(!empty($user['User']['mobile_number'])){
84
							$mobileRequired = false;
85
						}
86
						if(!empty($user['User']['activated']) && $user['User']['activated']==1) {
87
							$referrerRequired = false;
88
						}
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{
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']);
94
						// }
95
						$this->SocialProfile->User->save($userData);
96
						$data['user_id'] = $user['User']['id'];	
97
					} else {						
98
						//Create a new user and then insert user_id in social_profiles table
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']);
100
						//Check for utm parameters
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
						}
116
						if(!empty($this->request->data['utm_campaign']) || !empty($this->request->data['referrer'])) {
117
							$userData['activated'] = 1;
118
							$referrerRequired = false;
119
						}
120
						if($this->SocialProfile->User->save($userData)) {
121
							$data['user_id'] = $this->SocialProfile->User->getLastInsertId();
122
						} else{
123
							$result = array('success' => false, 'message' => $this->SocialProfile->User->validationErrors,'mobileRequired'=>$mobileRequired,'referrerRequired'=>$referrerRequired);
124
							break;
125
						}
126
					}
127
					$this->SocialProfile->create();
128
					if ($this->SocialProfile->save($data)) {
129
						$result = array('success' => true, 'message' => 'Social Profile Created','id' => $data['user_id'],'mobileRequired'=>$mobileRequired,'referrerRequired'=>$referrerRequired);
130
					} else {
131
						$result = array('success' => false, 'message' => 'Social Profile Could Not Be Created','id' => -1,'mobileRequired'=>$mobileRequired,'referrerRequired'=>$referrerRequired);
132
					}
133
				} else {
134
					$result = array('success' => false, 'message' => "Email is missing",'mobileRequired'=>$mobileRequired,'referrerRequired'=>$referrerRequired);
135
					break;
136
				}				
137
			} else {
138
				//If Social Profile exists, do not update referrer
139
				$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']);
140
				//Check for utm parameters
141
				if(!empty($this->request->data['utm_source'])){
142
					$userData['utm_source'] = $this->request->data['utm_source'];
143
				}
144
				if(!empty($this->request->data['utm_medium'])){
145
					$userData['utm_medium'] = $this->request->data['utm_medium'];
146
				}
147
				if(!empty($this->request->data['utm_term'])){
148
					$userData['utm_term'] = $this->request->data['utm_term'];
149
				}
150
				if(!empty($this->request->data['utm_content'])){
151
					$userData['utm_content'] = $this->request->data['utm_content'];
152
				}
153
				if(!empty($this->request->data['utm_campaign'])){
154
					$userData['utm_campaign'] = $this->request->data['utm_campaign'];
155
				}				
156
				$this->SocialProfile->User->save($userData);
157
				//Update token ra
158
				$socialProfile['SocialProfile']['access_token'] = $data['access_token'];
159
				$this->SocialProfile->save($socialProfile);
160
				//Check for mobile number
161
				$mobilenumber = $this->SocialProfile->User->find('first',array('conditions'=>array('id'=>$socialProfile['SocialProfile']['user_id']),'recursive'=>-1));
162
				if(!empty($mobilenumber['User']['mobile_number'])){
163
					$mobileRequired = false;
164
				}
165
				//Check if activated feild is set or not.
166
				if(!empty($mobilenumber['User']['activated'])) {
167
					$referrerRequired = false;
168
				}
169
				$data['user_id'] = $socialProfile['SocialProfile']['user_id'];
170
				$result = array('success' => true, 'message' => 'Existing Social Profile','id' => $socialProfile['SocialProfile']['user_id'],'mobileRequired'=>$mobileRequired,'referrerRequired'=>$referrerRequired);
171
			}
172
		}
173
		$this->updateSaholicUser($data['user_id'],$this->request->data['email']);
174
		$this->set(array(
175
		    'result' => $result,
176
		    'callback' => $callback,
177
		    '_serialize' => array('result')
178
		));
179
		$this->log(print_r($result,1),'registration');
180
		$this->render('/Elements/json');
181
	}
182
 
183
	private function updateSaholicUser($userId,$email=null) {
184
		if(!$email){
185
			//Handle it properly
186
			return;
187
		}
188
		$this->log('userId '.$userId,'registration');
189
		$this->log('email '.$email ,'registration');
190
		$this->loadModel('UserAccount');
191
		$options = array('conditions'=>array('user_id' => $userId,'account_type' => 'saholic'),'recursive'=>-1);
192
		$exists = $this->UserAccount->find('count',$options);
193
		if(!$exists){
194
			$url = $this->apihost."register?email=$email&from=profitmandi";
195
			$response = $this->make_request($url,null);
196
			$this->log('response '.print_r($response,1),'registration');
197
			if(!empty($response)){
198
				if($response['userId']<1)return;
199
				$data = array('account_type'=>'saholic','user_id'=>$userId,'account_key'=>$response['userId']);
200
				$this->UserAccount->create();
201
				$this->UserAccount->save($data);
202
				$data = array('account_type'=>'cartId','user_id'=>$userId,'account_key'=>$response['cartId']);
203
				$this->UserAccount->create();
204
				$this->UserAccount->save($data);
205
			}
206
		}
207
	}
208
/**
209
 * edit method
210
 *
211
 * @throws NotFoundException
212
 * @param string $id
213
 * @return void
214
 */
215
	public function edit($id = null) {
216
		if (!$this->SocialProfile->exists($id)) {
217
			throw new NotFoundException(__('Invalid social profile'));
218
		}
219
		if ($this->request->is(array('post', 'put'))) {
220
			if ($this->SocialProfile->save($this->request->data)) {
221
				$this->Session->setFlash(__('The social profile has been saved.'));
222
				return $this->redirect(array('action' => 'index'));
223
			} else {
224
				$this->Session->setFlash(__('The social profile could not be saved. Please, try again.'));
225
			}
226
		} else {
227
			$options = array('conditions' => array('SocialProfile.' . $this->SocialProfile->primaryKey => $id));
228
			$this->request->data = $this->SocialProfile->find('first', $options);
229
		}
230
		$users = $this->SocialProfile->User->find('list');
231
		$this->set(compact('users'));
232
	}
233
 
234
/**
235
 * delete method
236
 *
237
 * @throws NotFoundException
238
 * @param string $id
239
 * @return void
240
 */
241
	public function delete($id = null) {
242
		throw new NotFoundException(__('Access Denied'));
243
		$this->SocialProfile->id = $id;
244
		if (!$this->SocialProfile->exists()) {
245
			throw new NotFoundException(__('Invalid social profile'));
246
		}
247
		$this->request->onlyAllow('post', 'delete');
248
		if ($this->SocialProfile->delete()) {
249
			$this->Session->setFlash(__('The social profile has been deleted.'));
250
		} else {
251
			$this->Session->setFlash(__('The social profile could not be deleted. Please, try again.'));
252
		}
253
		return $this->redirect(array('action' => 'index'));
254
	}
255
 
256
/**
257
 * admin_index method
258
 *
259
 * @return void
260
 */
261
	public function admin_index() {
262
		$this->SocialProfile->recursive = 0;
263
		$this->set('socialProfiles', $this->Paginator->paginate());
264
	}
265
 
266
/**
267
 * admin_view method
268
 *
269
 * @throws NotFoundException
270
 * @param string $id
271
 * @return void
272
 */
273
	public function admin_view($id = null) {
274
		if (!$this->SocialProfile->exists($id)) {
275
			throw new NotFoundException(__('Invalid social profile'));
276
		}
277
		$options = array('conditions' => array('SocialProfile.' . $this->SocialProfile->primaryKey => $id));
278
		$this->set('socialProfile', $this->SocialProfile->find('first', $options));
279
	}
280
 
281
/**
282
 * admin_add method
283
 *
284
 * @return void
285
 */
286
	public function admin_add() {
287
		if ($this->request->is('post')) {
288
			$this->SocialProfile->create();
289
			if ($this->SocialProfile->save($this->request->data)) {
290
				$this->Session->setFlash(__('The social profile has been saved.'));
291
				return $this->redirect(array('action' => 'index'));
292
			} else {
293
				$this->Session->setFlash(__('The social profile could not be saved. Please, try again.'));
294
			}
295
		}
296
		$users = $this->SocialProfile->User->find('list');
297
		$this->set(compact('users'));
298
	}
299
 
300
/**
301
 * admin_edit method
302
 *
303
 * @throws NotFoundException
304
 * @param string $id
305
 * @return void
306
 */
307
	public function admin_edit($id = null) {
308
		if (!$this->SocialProfile->exists($id)) {
309
			throw new NotFoundException(__('Invalid social profile'));
310
		}
311
		if ($this->request->is(array('post', 'put'))) {
312
			if ($this->SocialProfile->save($this->request->data)) {
313
				$this->Session->setFlash(__('The social profile has been saved.'));
314
				return $this->redirect(array('action' => 'index'));
315
			} else {
316
				$this->Session->setFlash(__('The social profile could not be saved. Please, try again.'));
317
			}
318
		} else {
319
			$options = array('conditions' => array('SocialProfile.' . $this->SocialProfile->primaryKey => $id));
320
			$this->request->data = $this->SocialProfile->find('first', $options);
321
		}
322
		$users = $this->SocialProfile->User->find('list');
323
		$this->set(compact('users'));
324
	}
325
 
326
/**
327
 * admin_delete method
328
 *
329
 * @throws NotFoundException
330
 * @param string $id
331
 * @return void
332
 */
333
	public function admin_delete($id = null) {
334
		$this->SocialProfile->id = $id;
335
		if (!$this->SocialProfile->exists()) {
336
			throw new NotFoundException(__('Invalid social profile'));
337
		}
338
		$this->request->onlyAllow('post', 'delete');
339
		if ($this->SocialProfile->delete()) {
340
			$this->Session->setFlash(__('The social profile has been deleted.'));
341
		} else {
342
			$this->Session->setFlash(__('The social profile could not be deleted. Please, try again.'));
343
		}
344
		return $this->redirect(array('action' => 'index'));
345
	}}