Subversion Repositories SmartDukaan

Rev

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

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