Subversion Repositories SmartDukaan

Rev

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