Subversion Repositories SmartDukaan

Rev

Rev 13695 | Rev 14019 | Go to most recent revision | 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
		),
64
		'password' => array(
65
			'notEmpty' => array(
66
				'rule' => array('notEmpty'),
67
				//'message' => 'Your custom message here',
68
				//'allowEmpty' => false,
69
				'required' => false,
70
				//'last' => false, // Stop validation after this rule
71
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
72
			),
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' => ''
113
		)
114
	);
13532 anikendra 115
	//The Associations below have been created with all possible keys, those that are not needed can be removed
116
 
117
	public function checkFbUser($user,$access_token){
118
		if(!empty($user->email)) {
119
			$conditions = array('User.email' => $user->email);
120
			// $conditions = array('User.facebook_id' => $user->id);
121
			$nativeUser = $this->find('first',array('conditions' => $conditions,'recursive' => -1));
122
			if(!empty($nativeUser) && isset($nativeUser['User']['id'])){
123
				$nativeUser['User']['fb_access_token'] = $access_token;
124
				$nativeUser['User']['active'] = 1;
125
				$nativeUser['User']['username'] =  $user->email;
126
				$nativeUser['User']['facebook_id'] = $user->id;
127
				$this->log(print_r($nativeUser,1),'fb');
128
				$this->save($nativeUser);
129
				return $nativeUser['User']['id'];
130
			}
131
		}
132
		$conditions = array('User.facebook_id' => $user->id);
133
		$nativeUser = $this->find('first',array('conditions' => $conditions,'recursive' => -1));
134
		if(empty($nativeUser) && !isset($nativeUser['User']['id'])){
135
			$data['fb_access_token'] = $access_token;
136
			$data['active'] = 1;
137
			$data['facebook_id'] = $user->id;
138
			$data['email'] = $user->email;
139
			$data['username'] =  $user->email;
140
			$data['first_name'] = $user->first_name;
141
			// $data['group_id'] = $groupId;
142
			if(!empty($user->middle_name)){
143
				$data['last_name'] = $user->middle_name.' '.$user->last_name;
144
			}else{
145
				$data['last_name'] = $user->last_name;
146
			}
147
			if(!empty($user->gender)){
148
				$data['gender'] = $user->gender;
149
			}
150
			if(!empty($user->location->name)){
151
				$parts = explode(',',$user->location->name);
152
				$data['city'] = $parts[0];				
153
			}
154
			if(!empty($user->birthday)){
155
				$parts = explode('/',$user->birthday);
156
				$data['dob'] = $parts[2].'-'.$parts[0].'-'.$parts[1];
157
			}
158
			// $data['active'] = 1;
159
			$this->log(print_r($data,1),'fb');
160
			$this->set($data);
161
			if($this->validates()){
162
				$this->save($data);
163
			}else{
164
				$this->log(print_r($this->validationErrors,1),'fb');
165
			}
166
		}
167
		return $this->id;
168
	}
169
 
170
	function afterSave($created, $options = array()) {
171
		if($created){
172
		    $this->generateReferralUrl($this->id);
173
		}
174
	}
175
 
176
	public function generateReferralUrl($id) {
13714 anikendra 177
		$url = 'http://api.profittill.com/register/?referrer='.$id;
13532 anikendra 178
		$referralUrl = $this->make_bitly_url($url,'webappniche','R_bb459f7deeace4103f50c32a296e2b95');
179
		$sql = "UPDATE users SET referral_url = '$referralUrl' WHERE id = $id";
180
		$this->query($sql);
181
		//$data =array('User'=> array('id' => $id,'referral_url'=>$referralUrl));
182
		//$this->save($data);
183
	}	
184
}