Subversion Repositories SmartDukaan

Rev

Rev 13532 | Rev 13633 | 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')) {
57
			error_log(print_r($this->request->data,1));
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
70
				$conditions = array('email'=>$this->request->data['email']);
71
				$user = $this->SocialProfile->User->find('first',array('conditions'=>$conditions));
72
				if(!empty($user)) {
73
					$data['user_id'] = $user['User']['id'];	
74
				}else{
75
					//Create a new user and then insert user_id in social_profiles table
13591 anikendra 76
					$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']);
13532 anikendra 77
					if($this->SocialProfile->User->save($userData)) {
78
						$data['user_id'] = $this->SocialProfile->User->getLastInsertId();
79
					}else{
80
						$result = array('success' => false, 'message' => $this->SocialProfile->User->validationErrors);
81
						break;
82
					}
83
				}
84
				$this->SocialProfile->create();
85
				if ($this->SocialProfile->save($data)) {
86
					$result = array('success' => true, 'message' => 'Social Profile Created','id' => $data['user_id']);
87
				} else {
88
					$result = array('success' => false, 'message' => 'Social Profile Could Not Be Created','id' => -1);
89
				}
90
			} else {
91
				$result = array('success' => true, 'message' => 'Existing Social Profile','id' => $socialProfile['SocialProfile']['user_id']);
13591 anikendra 92
			}
13532 anikendra 93
		}
94
		$this->set(array(
95
		    'result' => $result,
96
		    'callback' => $callback,
97
		    '_serialize' => array('result')
98
		));
99
		//$this->render('/Elements/jsonp');
100
		$this->render('/Elements/json');
101
	}
102
 
103
/**
104
 * edit method
105
 *
106
 * @throws NotFoundException
107
 * @param string $id
108
 * @return void
109
 */
110
	public function edit($id = null) {
111
		if (!$this->SocialProfile->exists($id)) {
112
			throw new NotFoundException(__('Invalid social profile'));
113
		}
114
		if ($this->request->is(array('post', 'put'))) {
115
			if ($this->SocialProfile->save($this->request->data)) {
116
				$this->Session->setFlash(__('The social profile has been saved.'));
117
				return $this->redirect(array('action' => 'index'));
118
			} else {
119
				$this->Session->setFlash(__('The social profile could not be saved. Please, try again.'));
120
			}
121
		} else {
122
			$options = array('conditions' => array('SocialProfile.' . $this->SocialProfile->primaryKey => $id));
123
			$this->request->data = $this->SocialProfile->find('first', $options);
124
		}
125
		$users = $this->SocialProfile->User->find('list');
126
		$this->set(compact('users'));
127
	}
128
 
129
/**
130
 * delete method
131
 *
132
 * @throws NotFoundException
133
 * @param string $id
134
 * @return void
135
 */
136
	public function delete($id = null) {
137
		throw new NotFoundException(__('Access Denied'));
138
		$this->SocialProfile->id = $id;
139
		if (!$this->SocialProfile->exists()) {
140
			throw new NotFoundException(__('Invalid social profile'));
141
		}
142
		$this->request->onlyAllow('post', 'delete');
143
		if ($this->SocialProfile->delete()) {
144
			$this->Session->setFlash(__('The social profile has been deleted.'));
145
		} else {
146
			$this->Session->setFlash(__('The social profile could not be deleted. Please, try again.'));
147
		}
148
		return $this->redirect(array('action' => 'index'));
149
	}
150
 
151
/**
152
 * admin_index method
153
 *
154
 * @return void
155
 */
156
	public function admin_index() {
157
		$this->SocialProfile->recursive = 0;
158
		$this->set('socialProfiles', $this->Paginator->paginate());
159
	}
160
 
161
/**
162
 * admin_view method
163
 *
164
 * @throws NotFoundException
165
 * @param string $id
166
 * @return void
167
 */
168
	public function admin_view($id = null) {
169
		if (!$this->SocialProfile->exists($id)) {
170
			throw new NotFoundException(__('Invalid social profile'));
171
		}
172
		$options = array('conditions' => array('SocialProfile.' . $this->SocialProfile->primaryKey => $id));
173
		$this->set('socialProfile', $this->SocialProfile->find('first', $options));
174
	}
175
 
176
/**
177
 * admin_add method
178
 *
179
 * @return void
180
 */
181
	public function admin_add() {
182
		if ($this->request->is('post')) {
183
			$this->SocialProfile->create();
184
			if ($this->SocialProfile->save($this->request->data)) {
185
				$this->Session->setFlash(__('The social profile has been saved.'));
186
				return $this->redirect(array('action' => 'index'));
187
			} else {
188
				$this->Session->setFlash(__('The social profile could not be saved. Please, try again.'));
189
			}
190
		}
191
		$users = $this->SocialProfile->User->find('list');
192
		$this->set(compact('users'));
193
	}
194
 
195
/**
196
 * admin_edit method
197
 *
198
 * @throws NotFoundException
199
 * @param string $id
200
 * @return void
201
 */
202
	public function admin_edit($id = null) {
203
		if (!$this->SocialProfile->exists($id)) {
204
			throw new NotFoundException(__('Invalid social profile'));
205
		}
206
		if ($this->request->is(array('post', 'put'))) {
207
			if ($this->SocialProfile->save($this->request->data)) {
208
				$this->Session->setFlash(__('The social profile has been saved.'));
209
				return $this->redirect(array('action' => 'index'));
210
			} else {
211
				$this->Session->setFlash(__('The social profile could not be saved. Please, try again.'));
212
			}
213
		} else {
214
			$options = array('conditions' => array('SocialProfile.' . $this->SocialProfile->primaryKey => $id));
215
			$this->request->data = $this->SocialProfile->find('first', $options);
216
		}
217
		$users = $this->SocialProfile->User->find('list');
218
		$this->set(compact('users'));
219
	}
220
 
221
/**
222
 * admin_delete method
223
 *
224
 * @throws NotFoundException
225
 * @param string $id
226
 * @return void
227
 */
228
	public function admin_delete($id = null) {
229
		$this->SocialProfile->id = $id;
230
		if (!$this->SocialProfile->exists()) {
231
			throw new NotFoundException(__('Invalid social profile'));
232
		}
233
		$this->request->onlyAllow('post', 'delete');
234
		if ($this->SocialProfile->delete()) {
235
			$this->Session->setFlash(__('The social profile has been deleted.'));
236
		} else {
237
			$this->Session->setFlash(__('The social profile could not be deleted. Please, try again.'));
238
		}
239
		return $this->redirect(array('action' => 'index'));
240
	}}