Subversion Repositories SmartDukaan

Rev

Rev 12351 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
12345 anikendra 1
<?php
2
App::uses('AppModel', 'Model');
3
/**
4
 * User Model
5
 *
6
 * @property Group $Group
7
 * @property Profile $Profile
8
 * @property Publisher $Publisher
9
 */
10
class User extends AppModel {
11
 
12
	public $actsAs = array('SignMeUp.SignMeUp');
13
 
14
/**
15
 * Display field
16
 *
17
 * @var string
18
 */
19
	public $displayField = 'username';
20
 
21
/**
22
 * Validation rules
23
 *
24
 * @var array
25
 */
26
	public $validate = array(
27
		/*'first_name' => array(
28
			'notempty' => array(
29
//				'rule' => array('notempty'),
30
				//'message' => 'Your custom message here',
31
				//'allowEmpty' => false,
32
				//'required' => false,
33
				//'last' => false, // Stop validation after this rule
34
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
35
			),
36
		),
37
		'last_name' => array(
38
			'notempty' => array(
39
//				'rule' => array('notempty'),
40
				//'message' => 'Your custom message here',
41
				//'allowEmpty' => false,
42
				//'required' => false,
43
				//'last' => false, // Stop validation after this rule
44
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
45
			),
46
		),
47
		'zipcode' => array(
48
			'notempty' => array(
49
//				'rule' => array('notempty'),
50
				//'message' => 'Your custom message here',
51
				//'allowEmpty' => false,
52
				//'required' => false,
53
				//'last' => false, // Stop validation after this rule
54
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
55
			),
56
		),*/
57
/*		'dob' => array(
58
			'date' => array(
59
//				'rule' => array('date'),
60
				//'message' => 'Your custom message here',
61
				'allowEmpty' => true,
62
				'required' => false,
63
				//'last' => false, // Stop validation after this rule
64
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
65
			),
66
		),*/
67
		'username' => array(
68
			'notempty' => array(
69
				'rule' => array('notempty'),
70
				//'message' => 'Your custom message here',
71
				//'allowEmpty' => false,
72
				//'required' => false,
73
				//'last' => false, // Stop validation after this rule
74
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
75
			),
76
		),
77
		'email' => array(
78
			'email' => array(
79
				'rule' => array('email'),
80
				//'message' => 'Your custom message here',
81
				//'allowEmpty' => false,
82
				//'required' => false,
83
				//'last' => false, // Stop validation after this rule
84
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
85
			),
86
			'emailExists' => array(
87
				'rule' => 'isUnique',
88
				'message' => 'Sorry, this email already exists'
89
			),
90
		),
91
		'password' => array(
92
			'notempty' => array(
93
				'rule' => array('notempty'),
94
				//'message' => 'Your custom message here',
95
				//'allowEmpty' => false,
96
				//'required' => false,
97
				//'last' => false, // Stop validation after this rule
98
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
99
			),
100
		),
101
/*
102
		'address_1' => array(
103
			'notempty' => array(
104
				//'rule' => array('notempty'),
105
				//'message' => 'Your custom message here',
106
				//'allowEmpty' => false,
107
				//'required' => false,
108
				//'last' => false, // Stop validation after this rule
109
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
110
			),
111
		),
112
		'address_2' => array(
113
			'notempty' => array(
114
				//'rule' => array('notempty'),
115
				//'message' => 'Your custom message here',
116
				//'allowEmpty' => false,
117
				//'required' => false,
118
				//'last' => false, // Stop validation after this rule
119
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
120
			),
121
		),
122
		'city' => array(
123
			'notempty' => array(
124
				//'rule' => array('notempty'),
125
				//'message' => 'Your custom message here',
126
				//'allowEmpty' => false,
127
				//'required' => false,
128
				//'last' => false, // Stop validation after this rule
129
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
130
			),
131
		),
132
		'state' => array(
133
			'notempty' => array(
134
				//'rule' => array('notempty'),
135
				//'message' => 'Your custom message here',
136
				//'allowEmpty' => false,
137
				//'required' => false,
138
				//'last' => false, // Stop validation after this rule
139
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
140
			),
141
		),*/
142
		'auth_token' => array(
143
			'notempty' => array(
144
				//'rule' => array('notempty'),
145
				//'message' => 'Your custom message here',
146
				//'allowEmpty' => false,
147
				//'required' => false,
148
				//'last' => false, // Stop validation after this rule
149
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
150
			),
151
		),
152
		'group_id' => array(
153
			'numeric' => array(
154
				'rule' => array('numeric'),
155
				//'message' => 'Your custom message here',
156
				//'allowEmpty' => false,
157
				//'required' => false,
158
				//'last' => false, // Stop validation after this rule
159
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
160
			),
161
		)
162
	);
163
 
164
	//The Associations below have been created with all possible keys, those that are not needed can be removed
165
 
166
/**
167
 * belongsTo associations
168
 *
169
 * @var array
170
 */
171
	public $belongsTo = array(
172
		'Group' => array(
173
			'className' => 'Group',
174
			'foreignKey' => 'group_id',
175
			'conditions' => '',
176
			'fields' => '',
177
			'order' => ''
178
		)
179
	);
180
 
181
	public function checkFbUser($user,$access_token){
182
		$conditions = array('User.email' => $user->email);
183
		// $conditions = array('User.facebook_id' => $user->id);
184
		$nativeUser = $this->find('first',array('conditions' => $conditions,'recursive' => -1));
185
		if(!empty($nativeUser) && isset($nativeUser['User']['id'])){
186
			$nativeUser['User']['fb_access_token'] = $access_token;
187
			$nativeUser['User']['active'] = 1;
188
			$nativeUser['User']['username'] =  $user->email;
189
			$nativeUser['User']['facebook_id'] = $user->id;
190
		$this->log(print_r($nativeUser,1),'fb');
191
			$this->save($nativeUser);
192
		}else{
193
			$conditions = array('User.facebook_id' => $user->id);
194
			$nativeUser = $this->find('first',array('conditions' => $conditions,'recursive' => -1));
195
			if(empty($nativeUser) && !isset($nativeUser['User']['id'])){
196
				$data['fb_access_token'] = $access_token;
197
				$data['active'] = 1;
198
				$data['facebook_id'] = $user->id;
199
				$data['email'] = $user->email;
200
				$data['username'] =  $user->email;
201
				$data['first_name'] = $user->first_name;
202
				// $data['group_id'] = $groupId;
203
				if(!empty($user->middle_name)){
204
					$data['last_name'] = $user->middle_name.' '.$user->last_name;
205
				}else{
206
					$data['last_name'] = $user->last_name;
207
				}
208
				if(!empty($user->gender)){
209
					$data['gender'] = $user->gender;
210
				}
211
				if(!empty($user->location->name)){
212
					$parts = explode(',',$user->location->name);
213
					$data['city'] = $parts[0];				
214
				}
215
				if(!empty($user->birthday)){
216
					$parts = explode('/',$user->birthday);
217
					$data['dob'] = $parts[2].'-'.$parts[0].'-'.$parts[1];
218
				}
219
				// $data['active'] = 1;
220
				$this->log(print_r($data,1),'fb');
221
				$this->set($data);
222
				if($this->validates()){
223
					$this->save($data);
224
				}else{
225
					$this->log(print_r($this->validationErrors,1),'fb');
226
				}
227
			}
228
		}
229
		return $this->id;
230
	}
231
}