Subversion Repositories SmartDukaan

Rev

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