Subversion Repositories SmartDukaan

Rev

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