Subversion Repositories SmartDukaan

Rev

Details | 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
	//The Associations below have been created with all possible keys, those that are not needed can be removed
78
 
79
	public function checkFbUser($user,$access_token){
80
		$conditions = array('User.email' => $user->email);
81
		// $conditions = array('User.facebook_id' => $user->id);
82
		$nativeUser = $this->find('first',array('conditions' => $conditions,'recursive' => -1));
83
		if(!empty($nativeUser) && isset($nativeUser['User']['id'])){
84
			$nativeUser['User']['fb_access_token'] = $access_token;
85
			$nativeUser['User']['active'] = 1;
86
			$nativeUser['User']['username'] =  $user->email;
87
			$nativeUser['User']['facebook_id'] = $user->id;
88
		$this->log(print_r($nativeUser,1),'fb');
89
			$this->save($nativeUser);
90
		}else{
91
			$conditions = array('User.facebook_id' => $user->id);
92
			$nativeUser = $this->find('first',array('conditions' => $conditions,'recursive' => -1));
93
			if(empty($nativeUser) && !isset($nativeUser['User']['id'])){
94
				$data['fb_access_token'] = $access_token;
95
				$data['active'] = 1;
96
				$data['facebook_id'] = $user->id;
97
				$data['email'] = $user->email;
98
				$data['username'] =  $user->email;
99
				$data['first_name'] = $user->first_name;
100
				// $data['group_id'] = $groupId;
101
				if(!empty($user->middle_name)){
102
					$data['last_name'] = $user->middle_name.' '.$user->last_name;
103
				}else{
104
					$data['last_name'] = $user->last_name;
105
				}
106
				if(!empty($user->gender)){
107
					$data['gender'] = $user->gender;
108
				}
109
				if(!empty($user->location->name)){
110
					$parts = explode(',',$user->location->name);
111
					$data['city'] = $parts[0];				
112
				}
113
				if(!empty($user->birthday)){
114
					$parts = explode('/',$user->birthday);
115
					$data['dob'] = $parts[2].'-'.$parts[0].'-'.$parts[1];
116
				}
117
				// $data['active'] = 1;
118
				$this->log(print_r($data,1),'fb');
119
				$this->set($data);
120
				if($this->validates()){
121
					$this->save($data);
122
				}else{
123
					$this->log(print_r($this->validationErrors,1),'fb');
124
				}
125
			}
126
		}
127
		return $this->id;
128
	}
129
 
130
	function afterSave($created, $options = array()) {
131
		if($created){
132
		    $this->generateReferralUrl($this->id);
133
		}
134
	}
135
 
136
	public function generateReferralUrl($id) {
137
		$url = 'http://api.letushaggle.com/register/?referrer='.$id;
138
		$referralUrl = $this->make_bitly_url($url,'webappniche','R_bb459f7deeace4103f50c32a296e2b95');
139
		$sql = "UPDATE users SET referral_url = '$referralUrl' WHERE id = $id";
140
		$this->query($sql);
141
		//$data =array('User'=> array('id' => $id,'referral_url'=>$referralUrl));
142
		//$this->save($data);
143
	}	
144
}