Subversion Repositories SmartDukaan

Rev

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