Subversion Repositories SmartDukaan

Rev

Rev 13659 | Rev 13683 | 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 {
99
				$result = array('success' => true, 'message' => 'Existing Social Profile','id' => $socialProfile['SocialProfile']['user_id']);
13591 anikendra 100
			}
13532 anikendra 101
		}
13673 anikendra 102
		$this->updateSaholicUser($data['user_id'],$this->request->data['email']);
13532 anikendra 103
		$this->set(array(
104
		    'result' => $result,
105
		    'callback' => $callback,
106
		    '_serialize' => array('result')
107
		));
108
		//$this->render('/Elements/jsonp');
109
		$this->render('/Elements/json');
110
	}
111
 
13673 anikendra 112
	private function updateSaholicUser($userId,$email=null) {
113
		if(!$email){
114
			//Handle it properly
115
			return;
116
		}
117
		$this->loadModel('UserAccount');
118
		$options = array('conditions'=>array('user_id' => $userId,'account_type' => 'saholic'),'recursive'=>-1);
119
		$exists = $this->UserAccount->find('count',$options);
120
		if(!$exists){
121
			$url = $this->saholicapihost."/register/?email=$email";
122
			$response = $this->make_request($url,null);
123
			if(!empty($response)){
124
				$data = array('account_type'=>'saholic','user_id'=>$userId,'account_key'=>$response['userId']);
125
				$this->UserAccount->create();
126
				$this->UserAccount-save($data);
127
			}
128
		}
129
	}
13532 anikendra 130
/**
131
 * edit method
132
 *
133
 * @throws NotFoundException
134
 * @param string $id
135
 * @return void
136
 */
137
	public function edit($id = null) {
138
		if (!$this->SocialProfile->exists($id)) {
139
			throw new NotFoundException(__('Invalid social profile'));
140
		}
141
		if ($this->request->is(array('post', 'put'))) {
142
			if ($this->SocialProfile->save($this->request->data)) {
143
				$this->Session->setFlash(__('The social profile has been saved.'));
144
				return $this->redirect(array('action' => 'index'));
145
			} else {
146
				$this->Session->setFlash(__('The social profile could not be saved. Please, try again.'));
147
			}
148
		} else {
149
			$options = array('conditions' => array('SocialProfile.' . $this->SocialProfile->primaryKey => $id));
150
			$this->request->data = $this->SocialProfile->find('first', $options);
151
		}
152
		$users = $this->SocialProfile->User->find('list');
153
		$this->set(compact('users'));
154
	}
155
 
156
/**
157
 * delete method
158
 *
159
 * @throws NotFoundException
160
 * @param string $id
161
 * @return void
162
 */
163
	public function delete($id = null) {
164
		throw new NotFoundException(__('Access Denied'));
165
		$this->SocialProfile->id = $id;
166
		if (!$this->SocialProfile->exists()) {
167
			throw new NotFoundException(__('Invalid social profile'));
168
		}
169
		$this->request->onlyAllow('post', 'delete');
170
		if ($this->SocialProfile->delete()) {
171
			$this->Session->setFlash(__('The social profile has been deleted.'));
172
		} else {
173
			$this->Session->setFlash(__('The social profile could not be deleted. Please, try again.'));
174
		}
175
		return $this->redirect(array('action' => 'index'));
176
	}
177
 
178
/**
179
 * admin_index method
180
 *
181
 * @return void
182
 */
183
	public function admin_index() {
184
		$this->SocialProfile->recursive = 0;
185
		$this->set('socialProfiles', $this->Paginator->paginate());
186
	}
187
 
188
/**
189
 * admin_view method
190
 *
191
 * @throws NotFoundException
192
 * @param string $id
193
 * @return void
194
 */
195
	public function admin_view($id = null) {
196
		if (!$this->SocialProfile->exists($id)) {
197
			throw new NotFoundException(__('Invalid social profile'));
198
		}
199
		$options = array('conditions' => array('SocialProfile.' . $this->SocialProfile->primaryKey => $id));
200
		$this->set('socialProfile', $this->SocialProfile->find('first', $options));
201
	}
202
 
203
/**
204
 * admin_add method
205
 *
206
 * @return void
207
 */
208
	public function admin_add() {
209
		if ($this->request->is('post')) {
210
			$this->SocialProfile->create();
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
		}
218
		$users = $this->SocialProfile->User->find('list');
219
		$this->set(compact('users'));
220
	}
221
 
222
/**
223
 * admin_edit method
224
 *
225
 * @throws NotFoundException
226
 * @param string $id
227
 * @return void
228
 */
229
	public function admin_edit($id = null) {
230
		if (!$this->SocialProfile->exists($id)) {
231
			throw new NotFoundException(__('Invalid social profile'));
232
		}
233
		if ($this->request->is(array('post', 'put'))) {
234
			if ($this->SocialProfile->save($this->request->data)) {
235
				$this->Session->setFlash(__('The social profile has been saved.'));
236
				return $this->redirect(array('action' => 'index'));
237
			} else {
238
				$this->Session->setFlash(__('The social profile could not be saved. Please, try again.'));
239
			}
240
		} else {
241
			$options = array('conditions' => array('SocialProfile.' . $this->SocialProfile->primaryKey => $id));
242
			$this->request->data = $this->SocialProfile->find('first', $options);
243
		}
244
		$users = $this->SocialProfile->User->find('list');
245
		$this->set(compact('users'));
246
	}
247
 
248
/**
249
 * admin_delete method
250
 *
251
 * @throws NotFoundException
252
 * @param string $id
253
 * @return void
254
 */
255
	public function admin_delete($id = null) {
256
		$this->SocialProfile->id = $id;
257
		if (!$this->SocialProfile->exists()) {
258
			throw new NotFoundException(__('Invalid social profile'));
259
		}
260
		$this->request->onlyAllow('post', 'delete');
261
		if ($this->SocialProfile->delete()) {
262
			$this->Session->setFlash(__('The social profile has been deleted.'));
263
		} else {
264
			$this->Session->setFlash(__('The social profile could not be deleted. Please, try again.'));
265
		}
266
		return $this->redirect(array('action' => 'index'));
267
	}}