Subversion Repositories SmartDukaan

Rev

Rev 13633 | Rev 13673 | 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');
22
	}
23
/**
24
 * index method
25
 *
26
 * @return void
27
 */
28
	public function index() {
29
		throw new NotFoundException(__('Access Denied'));
30
		$this->SocialProfile->recursive = 0;
31
		$this->set('socialProfiles', $this->Paginator->paginate());
32
	}
33
 
34
/**
35
 * view method
36
 *
37
 * @throws NotFoundException
38
 * @param string $id
39
 * @return void
40
 */
41
	public function view($id = null) {
42
		throw new NotFoundException(__('Access Denied'));
43
		if (!$this->SocialProfile->exists($id)) {
44
			throw new NotFoundException(__('Invalid social profile'));
45
		}
46
		$options = array('conditions' => array('SocialProfile.' . $this->SocialProfile->primaryKey => $id));
47
		$this->set('socialProfile', $this->SocialProfile->find('first', $options));
48
	}
49
 
50
/**
51
 * add method
52
 *
53
 * @return void
54
 */
55
	public function add() {
56
		if ($this->request->is('post')) {
13633 anikendra 57
			$this->log(print_r($this->request->data,1),'registration');
13532 anikendra 58
			$data = $this->request->data;
59
			$data['social_id'] = $this->request->data['id'];
60
			$data['access_token'] = $this->request->data['token'];
61
			unset($data['id']);
62
			unset($data['token']);
63
			unset($data['gender']);
64
			$this->response->type('json');
65
			$this->layout = 'ajax';
66
			$conditions = array('social_id'=>$this->request->data['id'],'type'=>$this->request->data['type']);
67
			$socialProfile = $this->SocialProfile->find('first',array('conditions'=>$conditions));
68
			if(empty($socialProfile)){
69
				//Check if user with same email is registered and if so just add his profile
13659 anikendra 70
				if(!empty($this->request->data['email'])) {
71
					$conditions = array('email'=>$this->request->data['email']);
72
					$user = $this->SocialProfile->User->find('first',array('conditions'=>$conditions));
73
 
74
					if(!empty($user)) {
75
						$data['user_id'] = $user['User']['id'];	
13532 anikendra 76
					}else{
13659 anikendra 77
						//Create a new user and then insert user_id in social_profiles table
78
						$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']);
79
						if($this->SocialProfile->User->save($userData)) {
80
							$data['user_id'] = $this->SocialProfile->User->getLastInsertId();
81
						}else{
82
							$result = array('success' => false, 'message' => $this->SocialProfile->User->validationErrors);
83
							break;
84
						}
13532 anikendra 85
					}
13659 anikendra 86
					$this->SocialProfile->create();
87
					if ($this->SocialProfile->save($data)) {
88
						$result = array('success' => true, 'message' => 'Social Profile Created','id' => $data['user_id']);
89
					} else {
90
						$result = array('success' => false, 'message' => 'Social Profile Could Not Be Created','id' => -1);
91
					}
13532 anikendra 92
				} else {
13659 anikendra 93
					$result = array('success' => false, 'message' => "Email is missing");
94
					break;
95
				}				
13532 anikendra 96
			} else {
97
				$result = array('success' => true, 'message' => 'Existing Social Profile','id' => $socialProfile['SocialProfile']['user_id']);
13591 anikendra 98
			}
13532 anikendra 99
		}
100
		$this->set(array(
101
		    'result' => $result,
102
		    'callback' => $callback,
103
		    '_serialize' => array('result')
104
		));
105
		//$this->render('/Elements/jsonp');
106
		$this->render('/Elements/json');
107
	}
108
 
109
/**
110
 * edit method
111
 *
112
 * @throws NotFoundException
113
 * @param string $id
114
 * @return void
115
 */
116
	public function edit($id = null) {
117
		if (!$this->SocialProfile->exists($id)) {
118
			throw new NotFoundException(__('Invalid social profile'));
119
		}
120
		if ($this->request->is(array('post', 'put'))) {
121
			if ($this->SocialProfile->save($this->request->data)) {
122
				$this->Session->setFlash(__('The social profile has been saved.'));
123
				return $this->redirect(array('action' => 'index'));
124
			} else {
125
				$this->Session->setFlash(__('The social profile could not be saved. Please, try again.'));
126
			}
127
		} else {
128
			$options = array('conditions' => array('SocialProfile.' . $this->SocialProfile->primaryKey => $id));
129
			$this->request->data = $this->SocialProfile->find('first', $options);
130
		}
131
		$users = $this->SocialProfile->User->find('list');
132
		$this->set(compact('users'));
133
	}
134
 
135
/**
136
 * delete method
137
 *
138
 * @throws NotFoundException
139
 * @param string $id
140
 * @return void
141
 */
142
	public function delete($id = null) {
143
		throw new NotFoundException(__('Access Denied'));
144
		$this->SocialProfile->id = $id;
145
		if (!$this->SocialProfile->exists()) {
146
			throw new NotFoundException(__('Invalid social profile'));
147
		}
148
		$this->request->onlyAllow('post', 'delete');
149
		if ($this->SocialProfile->delete()) {
150
			$this->Session->setFlash(__('The social profile has been deleted.'));
151
		} else {
152
			$this->Session->setFlash(__('The social profile could not be deleted. Please, try again.'));
153
		}
154
		return $this->redirect(array('action' => 'index'));
155
	}
156
 
157
/**
158
 * admin_index method
159
 *
160
 * @return void
161
 */
162
	public function admin_index() {
163
		$this->SocialProfile->recursive = 0;
164
		$this->set('socialProfiles', $this->Paginator->paginate());
165
	}
166
 
167
/**
168
 * admin_view method
169
 *
170
 * @throws NotFoundException
171
 * @param string $id
172
 * @return void
173
 */
174
	public function admin_view($id = null) {
175
		if (!$this->SocialProfile->exists($id)) {
176
			throw new NotFoundException(__('Invalid social profile'));
177
		}
178
		$options = array('conditions' => array('SocialProfile.' . $this->SocialProfile->primaryKey => $id));
179
		$this->set('socialProfile', $this->SocialProfile->find('first', $options));
180
	}
181
 
182
/**
183
 * admin_add method
184
 *
185
 * @return void
186
 */
187
	public function admin_add() {
188
		if ($this->request->is('post')) {
189
			$this->SocialProfile->create();
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
		}
197
		$users = $this->SocialProfile->User->find('list');
198
		$this->set(compact('users'));
199
	}
200
 
201
/**
202
 * admin_edit method
203
 *
204
 * @throws NotFoundException
205
 * @param string $id
206
 * @return void
207
 */
208
	public function admin_edit($id = null) {
209
		if (!$this->SocialProfile->exists($id)) {
210
			throw new NotFoundException(__('Invalid social profile'));
211
		}
212
		if ($this->request->is(array('post', 'put'))) {
213
			if ($this->SocialProfile->save($this->request->data)) {
214
				$this->Session->setFlash(__('The social profile has been saved.'));
215
				return $this->redirect(array('action' => 'index'));
216
			} else {
217
				$this->Session->setFlash(__('The social profile could not be saved. Please, try again.'));
218
			}
219
		} else {
220
			$options = array('conditions' => array('SocialProfile.' . $this->SocialProfile->primaryKey => $id));
221
			$this->request->data = $this->SocialProfile->find('first', $options);
222
		}
223
		$users = $this->SocialProfile->User->find('list');
224
		$this->set(compact('users'));
225
	}
226
 
227
/**
228
 * admin_delete method
229
 *
230
 * @throws NotFoundException
231
 * @param string $id
232
 * @return void
233
 */
234
	public function admin_delete($id = null) {
235
		$this->SocialProfile->id = $id;
236
		if (!$this->SocialProfile->exists()) {
237
			throw new NotFoundException(__('Invalid social profile'));
238
		}
239
		$this->request->onlyAllow('post', 'delete');
240
		if ($this->SocialProfile->delete()) {
241
			$this->Session->setFlash(__('The social profile has been deleted.'));
242
		} else {
243
			$this->Session->setFlash(__('The social profile could not be deleted. Please, try again.'));
244
		}
245
		return $this->redirect(array('action' => 'index'));
246
	}}