Subversion Repositories SmartDukaan

Rev

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