Subversion Repositories SmartDukaan

Rev

Rev 12345 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
12345 anikendra 1
<?php
2
App::uses('AppController', 'Controller');
3
/**
4
 * Users Controller
5
 *
6
 * @property User $User
7
 */
8
class UsersController extends AppController {
9
 
10
	public $components = array('SignMeUp.SignMeUp');
12354 anikendra 11
	public $uses = array('User','Api');
12345 anikendra 12
 
13
    public function beforeFilter() {
14
    	parent::beforeFilter();
15
    	$this->Auth->loginRedirect = array('controller' => 'users', 'action' => 'dashboard');
16
        $this->Auth->allow(array('login', 'forgotten_password', 'register', 'activate', 'checkemail','checkfbuser','registertwitteruser'));        
17
    }    
18
 
19
	function registertwitteruser(){
20
		$this->twitterObj->setToken($_GET['oauth_token']);
21
		$token = $this->twitterObj->getAccessToken();
22
		$this->twitterObj->setToken($token->oauth_token, $token->oauth_token_secret);
23
		$twitterInfo= $this->twitterObj->get_accountVerify_credentials();
24
		$data = array();
25
		$data['twitter_id'] = $twitterInfo->id;		
26
		$this->Session->write('twtusername',$twitterInfo->screen_name);
27
		$data['twitter_token'] = $token->oauth_token;
28
		$data['twitter_secret'] = $token->oauth_token_secret;
29
		$data['twitter_screen_name'] = $twitterInfo->screen_name;					
30
		$data['active'] = 1;
31
 
32
		$id = $this->Auth->user('id');
33
		if(!isset($id) || $id == ''){
34
		//	$count = $this->User->find('count',array('conditions' => array('twitter_id' => $data['twitter_id'])));
35
			$user = $this->User->find('first',array('conditions' => array('twitter_id' => $data['twitter_id'])));
36
			//if($count<1){
37
			if(empty($user)){
38
				$this->User->create();
39
				$this->User->save($data);
40
//				$dbuser = $this->User->read(null,$this->User->id);
41
				$this->Auth->login($this->User->data);
42
			}else{
43
		//		$data = $this->User->find('first',array('conditions' => array('twitter_id' => $data['twitter_id'])));
44
				$this->Auth->login($user['User']);
45
			}
46
		}else{
47
			//$data['id'] = $this->Auth->user('id');
48
			$user = $this->Auth->user;
49
//			$this->User->save($data);
50
//			$this->Auth->login($data);
51
		}                
52
		$user = $this->User->read(null, $this->Auth->User('id'));
53
		if ($user['User']['group_id'] == 1) {
54
			$this->redirect(array('controller' => 'users', 'action' => 'dashboard'));
55
			exit();
56
		}
57
		elseif ($user['User']['group_id'] == 2) {
58
			$this->redirect(array('controller' => 'administration', 'action' => 'dashboard'));
59
			exit();
60
		}
61
	}
62
 
63
    public function checkfbuser($accessToken=null){
64
	   	$sessionState = $this->Session->read('state');
65
	   	if($sessionState != $_GET['state']) {
66
			$this->redirect(array('controller'=>'pages','action'=>'display','home'));
67
		}
68
		else {
69
			if(isset($_GET['code'])){
70
			 	$code = $_GET['code'];
71
			 	$facebookConfig = Configure::read("Facebook");		
72
			 	$token_url = "https://graph.facebook.com/oauth/access_token?"
73
			       . "client_id=" . $facebookConfig['fbappid'] . "&redirect_uri=" . urlencode($facebookConfig['base_url'].'/users/checkfbuser/')
74
			       . "&client_secret=" . $facebookConfig['secret'] . "&code=" . $code;
75
 
76
			    $response = file_get_contents($token_url);
77
			    $params = null;
78
			    parse_str($response, $params);
79
			    $accessToken = $params['access_token'];
80
			}else{
81
				$this->redirect(array('controller'=>'pages','action'=>'display','home'));
82
			}
83
		}
84
		$this->User->recursive = -1;
85
        if (!$accessToken) {
86
            $this->redirect(array('controller'=>'pages','action'=>'display','home'));
87
        }        
88
        if (isset($accessToken) && $accessToken != 'undefined') {			
89
 
90
		    $graph_url = "https://graph.facebook.com/me?access_token=" . $accessToken;
91
 
92
		    $user = json_decode(file_get_contents($graph_url));
93
            if (!empty($user)) {
94
                if (!$this->isAuthorized()) {
95
                    $id = $this->User->checkFbUser($user,$accessToken);
96
			$dbuser = $this->User->findById($id);
97
                    $this->Session->write('facebook_id',$user->id);
98
			$this->Auth->login($dbuser['User']);
99
			if($this->Auth->login()){
100
				$this->log("logged = ".print_r($this->Auth->user('id'),1)."\n",'fb');
101
			}
102
			$this->redirect(array('controller'=>'users','action'=>'dashboard'));
103
                } else {                	
104
			$this->redirect(array('controller'=>'users','action'=>'dashboard'));
105
                }
106
            }
107
        }
108
	}
109
 
110
    function checkemail() {
111
        $this->User->recursive = -1;
112
        $this->layout = 'ajax';
113
        if (!empty($this->params['url']['data']['User']['email'])) {
114
            $username = $this->params['url']['data']['User']['email'];
115
        } elseif (!empty($this->params['url']['data']['Doctor']['email'])) {
116
            $username = $this->params['url']['data']['Doctor']['email'];
117
        } else {
118
            $result = false;
119
            $this->set('result', $result);
120
        }
121
        if ($this->Auth->user('id') != null) {
122
            $conditions = array('User.email' => $username, 'User.id !=' => $this->Auth->user('id'));
123
        } else {
124
            $conditions = array('User.email' => $username);
125
        }
126
        $count = $this->User->find('count', array('conditions' => $conditions));
127
        if ($count > 0) {
128
            $result = false;
129
        } else {
130
            $result = true;
131
        }
132
        $this->set('result', $result);
133
    }
134
 
135
    public function edit() {
136
        $this->User->recursive = -1;
137
        $id = $this->Auth->User('id');
138
        if ($this->request->is('post') || $this->request->is('put')) {
139
            if ($this->User->save($this->request->data)) {
140
                $this->Session->setFlash(__('Your profile has been saved', true),'success_message');
141
                $this->redirect(array('action' => 'dashboard'));
142
            } else {
143
                $this->Session->setFlash(__('The user could not be saved. <br>'.print_r($this->User->validationErrors).' <br>Please, try again.', true),'error_message');
144
            }
145
        } else {        
146
            $this->request->data = $this->User->read(null, $id);
147
        }
148
        $this->loadModel('Country');
149
        $this->Country->recursive = -1;
150
        $fields = array('country', 'FIPS104');
151
        $result = $this->Country->find('all', array('fields' => $fields));
152
        $countries = array();
153
        foreach ($result AS $country) {
154
            $countries[$country['Country']['FIPS104']] = $country['Country']['country'];
155
        }        
156
        Configure::load('constant');
157
		$this->set('timeZones' , Configure::read("timeZones"));
158
        $this->set(compact('countries','id'));
159
        $this->set('title_for_layout', "Update Profile");
160
    }
161
 
162
/**
163
 * admin_index method
164
 *
165
 * @return void
166
 */
167
	public function admin_index() {
168
		$this->User->recursive = 0;
169
		$this->set('users', $this->paginate());
170
	}
171
 
172
/**
173
 * admin_view method
174
 *
175
 * @throws NotFoundException
176
 * @param string $id
177
 * @return void
178
 */
179
	public function admin_view($id = null) {
180
		$this->User->id = $id;
181
		if (!$this->User->exists()) {
182
			throw new NotFoundException(__('Invalid user'));
183
		}
184
		$this->set('user', $this->User->read(null, $id));
185
	}
186
 
187
/**
188
 * admin_add method
189
 *
190
 * @return void
191
 */
192
	public function admin_add() {
193
		if ($this->request->is('post')) {
194
			$this->User->create();
195
			if ($this->User->save($this->request->data)) {
196
				$this->Session->setFlash(__('The user has been saved'));
197
				$this->redirect(array('action' => 'index'));
198
			} else {
199
				$this->Session->setFlash(__('The user could not be saved. Please, try again.'));
200
			}
201
		}
202
		$groups = $this->User->Group->find('list');
203
		$this->set(compact('groups'));
204
	}
205
 
206
/**
207
 * admin_edit method
208
 *
209
 * @throws NotFoundException
210
 * @param string $id
211
 * @return void
212
 */
213
	public function admin_edit($id = null) {
214
		$this->User->id = $id;
215
		if (!$this->User->exists()) {
216
			throw new NotFoundException(__('Invalid user'));
217
		}
218
		if ($this->request->is('post') || $this->request->is('put')) {
219
			if ($this->User->save($this->request->data)) {
220
				$this->Session->setFlash(__('The user has been saved'));
221
				$this->redirect(array('action' => 'index'));
222
			} else {
223
				$this->Session->setFlash(__('The user could not be saved. Please, try again.'));
224
			}
225
		} else {
226
			$this->request->data = $this->User->read(null, $id);
227
		}
228
		$groups = $this->User->Group->find('list');
229
		$this->set(compact('groups'));
230
	}
231
 
232
/**
233
 * admin_delete method
234
 *
235
 * @throws MethodNotAllowedException
236
 * @throws NotFoundException
237
 * @param string $id
238
 * @return void
239
 */
240
	public function admin_delete($id = null) {
241
		if (!$this->request->is('post')) {
242
			throw new MethodNotAllowedException();
243
		}
244
		$this->User->id = $id;
245
		if (!$this->User->exists()) {
246
			throw new NotFoundException(__('Invalid user'));
247
		}
248
		if ($this->User->delete()) {
249
			$this->Session->setFlash(__('User deleted'));
250
			$this->redirect(array('action' => 'index'));
251
		}
252
		$this->Session->setFlash(__('User was not deleted'));
253
		$this->redirect(array('action' => 'index'));
254
	}
255
 
256
	public function dashboard() {
257
		App::uses('CakeTime', 'Utility');
258
		$key = 'lists';
259
		$lists = Cache::read($key);
260
        if(!isset($lists) || empty($lists)){
261
			$lists = $this->Api->getLists();
262
			Cache::write($key,$lists);
263
		}
264
		if($lists['DATASET']['TYPE']=='success'){
265
			$this->set('lists',$lists);
266
		}		
267
		$this->set('title_for_layout', "Dashboard");
268
	}
269
 
270
/**
271
* user authentication funtions
272
*/
273
	public function register() {
274
		$this->layout = 'homepage';
275
		$this->set('title_for_layout','Register your free copublish.in account');
276
	    $this->SignMeUp->register();
277
	}
278
 
279
	public function activate() {
280
		$this->layout = 'homepage';
281
		$this->set('title_for_layout','Activate your copublish.in account');
282
	    $this->SignMeUp->activate();
283
	}
284
 
285
	public function forgotten_password() {
286
		$this->layout = 'homepage';
287
		$this->set('title_for_layout','Reset your copublish.in password');
288
	    $this->SignMeUp->forgottenPassword();
289
	}	
290
 
291
	public function login() {
292
		$this->layout = 'homepage';
293
		if (!empty($this->data['User']['next'])) {
294
            $next = $this->data['User']['next'];
295
            $this->Session->write('next', $next);
296
        } elseif (!empty($this->params['url']['next'])) {
297
            $next = $this->params['url']['next'];
298
            $this->Session->write('next', $next);
299
        }
300
        if ($this->Auth->login()) {
301
            $next = $this->Session->read('next');
302
            if (!empty($next)) {
303
                header('location:' . $next);
304
                exit();
305
            }
306
            $user = $this->User->read(null, $this->Auth->User('id'));
307
            if ($user['User']['active'] == 0) {
308
                $this->Session->setFlash(__('Your account has not been activated yet. Contact the administrator.', true),'error_message');
309
                $this->redirect($this->Auth->logout());
310
                exit();
311
            } elseif ($user['User']['group_id'] == 2) {
312
                $this->redirect(array('controller' => 'administration', 'action' => 'dashboard'));
313
                exit();
314
            }
315
            $this->redirect(array('controller' => 'users', 'action' => 'dashboard'));
316
        } else {
317
            if (!empty($this->data)) {                
318
                $this->Session->setFlash(__('Invalid username or password'),'error_message');
319
            }
320
        }
321
        $this->set('title_for_layout', "Login to copublish.in and manage your Pinterest accounts");
322
	}
323
 
324
	public function logout() {
325
		$this->Session->setFlash(__('You have been logged out now'),'success_message');
326
		$this->redirect($this->Auth->logout());
327
	}
328
 
329
	public function updatetimezone() {
330
		$this->layout = 'ajax';		
331
		$timezone = $_GET['tz'];
332
		Configure::load('constant');
333
		$timeZones = Configure::read("timeZones");
334
		$query = "UPDATE users SET user_timezone = $timezone WHERE id = ".$this->Auth->User('id');
335
		if($this->User->query($query)){
336
			$result = array('success' => true,'tz' => $timeZones[$timezone]);
337
		}else{
338
			$result = array('success' => false);
339
		}
340
		$this->set('result',$result);
341
	}
342
}