Subversion Repositories SmartDukaan

Rev

Rev 14768 | Rev 14884 | 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;
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'];	
14768 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'])) {
117
							$this->request->data['activated'] = 1;
118
							$referrerRequired = false;
119
						}
13659 anikendra 120
						if($this->SocialProfile->User->save($userData)) {
14424 anikendra 121
							$data['user_id'] = $this->SocialProfile->User->getLastInsertId();
14068 anikendra 122
						} else{
14768 anikendra 123
							$result = array('success' => false, 'message' => $this->SocialProfile->User->validationErrors,'mobileRequired'=>$mobileRequired,'referrerRequired'=>$referrerRequired);
13659 anikendra 124
							break;
125
						}
13532 anikendra 126
					}
13659 anikendra 127
					$this->SocialProfile->create();
128
					if ($this->SocialProfile->save($data)) {
14768 anikendra 129
						$result = array('success' => true, 'message' => 'Social Profile Created','id' => $data['user_id'],'mobileRequired'=>$mobileRequired,'referrerRequired'=>$referrerRequired);
13659 anikendra 130
					} else {
14783 anikendra 131
						$result = array('success' => false, 'message' => 'Social Profile Could Not Be Created','id' => -1,'mobileRequired'=>$mobileRequired,'referrerRequired'=>$referrerRequired);
13659 anikendra 132
					}
13532 anikendra 133
				} else {
14783 anikendra 134
					$result = array('success' => false, 'message' => "Email is missing",'mobileRequired'=>$mobileRequired,'referrerRequired'=>$referrerRequired);
13659 anikendra 135
					break;
136
				}				
13532 anikendra 137
			} else {
14783 anikendra 138
				//If Social Profile exists, do not update referrer
14768 anikendra 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
				}				
13768 anikendra 156
				$this->SocialProfile->User->save($userData);
14139 anikendra 157
				//Check for mobile number
158
				$mobilenumber = $this->SocialProfile->User->find('first',array('conditions'=>array('id'=>$socialProfile['SocialProfile']['user_id']),'recursive'=>-1));
159
				if(!empty($mobilenumber['User']['mobile_number'])){
160
					$mobileRequired = false;
161
				}
14783 anikendra 162
				//Check if activated feild is set or not.
163
				if(!empty($mobilenumber['User']['activated'])) {
14768 anikendra 164
					$referrerRequired = false;
165
				}
13683 anikendra 166
				$data['user_id'] = $socialProfile['SocialProfile']['user_id'];
14768 anikendra 167
				$result = array('success' => true, 'message' => 'Existing Social Profile','id' => $socialProfile['SocialProfile']['user_id'],'mobileRequired'=>$mobileRequired,'referrerRequired'=>$referrerRequired);
13591 anikendra 168
			}
13532 anikendra 169
		}
13673 anikendra 170
		$this->updateSaholicUser($data['user_id'],$this->request->data['email']);
13532 anikendra 171
		$this->set(array(
172
		    'result' => $result,
173
		    'callback' => $callback,
174
		    '_serialize' => array('result')
175
		));
14425 anikendra 176
		$this->log(print_r($result,1),'registration');
13532 anikendra 177
		$this->render('/Elements/json');
178
	}
179
 
13673 anikendra 180
	private function updateSaholicUser($userId,$email=null) {
181
		if(!$email){
182
			//Handle it properly
183
			return;
184
		}
13683 anikendra 185
		$this->log('userId '.$userId,'registration');
186
		$this->log('email '.$email ,'registration');
13673 anikendra 187
		$this->loadModel('UserAccount');
188
		$options = array('conditions'=>array('user_id' => $userId,'account_type' => 'saholic'),'recursive'=>-1);
189
		$exists = $this->UserAccount->find('count',$options);
190
		if(!$exists){
14685 anikendra 191
			$url = $this->apihost."register?email=$email&from=profitmandi";
13673 anikendra 192
			$response = $this->make_request($url,null);
13683 anikendra 193
			$this->log('response '.print_r($response,1),'registration');
13673 anikendra 194
			if(!empty($response)){
14685 anikendra 195
				if($response['userId']<1)return;
13673 anikendra 196
				$data = array('account_type'=>'saholic','user_id'=>$userId,'account_key'=>$response['userId']);
197
				$this->UserAccount->create();
13683 anikendra 198
				$this->UserAccount->save($data);
14166 anikendra 199
				$data = array('account_type'=>'cartId','user_id'=>$userId,'account_key'=>$response['cartId']);
200
				$this->UserAccount->create();
201
				$this->UserAccount->save($data);
13673 anikendra 202
			}
203
		}
204
	}
13532 anikendra 205
/**
206
 * edit method
207
 *
208
 * @throws NotFoundException
209
 * @param string $id
210
 * @return void
211
 */
212
	public function edit($id = null) {
213
		if (!$this->SocialProfile->exists($id)) {
214
			throw new NotFoundException(__('Invalid social profile'));
215
		}
216
		if ($this->request->is(array('post', 'put'))) {
217
			if ($this->SocialProfile->save($this->request->data)) {
218
				$this->Session->setFlash(__('The social profile has been saved.'));
219
				return $this->redirect(array('action' => 'index'));
220
			} else {
221
				$this->Session->setFlash(__('The social profile could not be saved. Please, try again.'));
222
			}
223
		} else {
224
			$options = array('conditions' => array('SocialProfile.' . $this->SocialProfile->primaryKey => $id));
225
			$this->request->data = $this->SocialProfile->find('first', $options);
226
		}
227
		$users = $this->SocialProfile->User->find('list');
228
		$this->set(compact('users'));
229
	}
230
 
231
/**
232
 * delete method
233
 *
234
 * @throws NotFoundException
235
 * @param string $id
236
 * @return void
237
 */
238
	public function delete($id = null) {
239
		throw new NotFoundException(__('Access Denied'));
240
		$this->SocialProfile->id = $id;
241
		if (!$this->SocialProfile->exists()) {
242
			throw new NotFoundException(__('Invalid social profile'));
243
		}
244
		$this->request->onlyAllow('post', 'delete');
245
		if ($this->SocialProfile->delete()) {
246
			$this->Session->setFlash(__('The social profile has been deleted.'));
247
		} else {
248
			$this->Session->setFlash(__('The social profile could not be deleted. Please, try again.'));
249
		}
250
		return $this->redirect(array('action' => 'index'));
251
	}
252
 
253
/**
254
 * admin_index method
255
 *
256
 * @return void
257
 */
258
	public function admin_index() {
259
		$this->SocialProfile->recursive = 0;
260
		$this->set('socialProfiles', $this->Paginator->paginate());
261
	}
262
 
263
/**
264
 * admin_view method
265
 *
266
 * @throws NotFoundException
267
 * @param string $id
268
 * @return void
269
 */
270
	public function admin_view($id = null) {
271
		if (!$this->SocialProfile->exists($id)) {
272
			throw new NotFoundException(__('Invalid social profile'));
273
		}
274
		$options = array('conditions' => array('SocialProfile.' . $this->SocialProfile->primaryKey => $id));
275
		$this->set('socialProfile', $this->SocialProfile->find('first', $options));
276
	}
277
 
278
/**
279
 * admin_add method
280
 *
281
 * @return void
282
 */
283
	public function admin_add() {
284
		if ($this->request->is('post')) {
285
			$this->SocialProfile->create();
286
			if ($this->SocialProfile->save($this->request->data)) {
287
				$this->Session->setFlash(__('The social profile has been saved.'));
288
				return $this->redirect(array('action' => 'index'));
289
			} else {
290
				$this->Session->setFlash(__('The social profile could not be saved. Please, try again.'));
291
			}
292
		}
293
		$users = $this->SocialProfile->User->find('list');
294
		$this->set(compact('users'));
295
	}
296
 
297
/**
298
 * admin_edit method
299
 *
300
 * @throws NotFoundException
301
 * @param string $id
302
 * @return void
303
 */
304
	public function admin_edit($id = null) {
305
		if (!$this->SocialProfile->exists($id)) {
306
			throw new NotFoundException(__('Invalid social profile'));
307
		}
308
		if ($this->request->is(array('post', 'put'))) {
309
			if ($this->SocialProfile->save($this->request->data)) {
310
				$this->Session->setFlash(__('The social profile has been saved.'));
311
				return $this->redirect(array('action' => 'index'));
312
			} else {
313
				$this->Session->setFlash(__('The social profile could not be saved. Please, try again.'));
314
			}
315
		} else {
316
			$options = array('conditions' => array('SocialProfile.' . $this->SocialProfile->primaryKey => $id));
317
			$this->request->data = $this->SocialProfile->find('first', $options);
318
		}
319
		$users = $this->SocialProfile->User->find('list');
320
		$this->set(compact('users'));
321
	}
322
 
323
/**
324
 * admin_delete method
325
 *
326
 * @throws NotFoundException
327
 * @param string $id
328
 * @return void
329
 */
330
	public function admin_delete($id = null) {
331
		$this->SocialProfile->id = $id;
332
		if (!$this->SocialProfile->exists()) {
333
			throw new NotFoundException(__('Invalid social profile'));
334
		}
335
		$this->request->onlyAllow('post', 'delete');
336
		if ($this->SocialProfile->delete()) {
337
			$this->Session->setFlash(__('The social profile has been deleted.'));
338
		} else {
339
			$this->Session->setFlash(__('The social profile could not be deleted. Please, try again.'));
340
		}
341
		return $this->redirect(array('action' => 'index'));
342
	}}