Subversion Repositories SmartDukaan

Rev

Blame | Last modification | View Log | RSS feed

<?php App::uses('AppModel', 'Model');
/**
 * User Model
 *
 * @property Content $Content
 */
class User extends AppModel {

        public $actsAs = array('SignMeUp.SignMeUp');
        
/**
 * Display field
 *
 * @var string
 */
        public $displayField = 'username';

/**
 * Validation rules
 *
 * @var array
 */
        public $validate = array(
                'first_name' => array(
                        'notEmpty' => array(
                                'rule' => array('notEmpty'),
                                //'message' => 'Your custom message here',
                                //'allowEmpty' => false,
                                'required' => false,
                                //'last' => false, // Stop validation after this rule
                                //'on' => 'create', // Limit validation to 'create' or 'update' operations
                        ),
                ),
                'dob' => array(
                        'date' => array(
                                'rule' => array('date'),
                                //'message' => 'Your custom message here',
                                //'allowEmpty' => false,
                                'required' => false,
                                //'last' => false, // Stop validation after this rule
                                //'on' => 'create', // Limit validation to 'create' or 'update' operations
                        ),
                ),
/*              'username' => array(
                        'notEmpty' => array(
                                'rule' => array('notEmpty'),
                                //'message' => 'Your custom message here',
                                //'allowEmpty' => false,
                                'required' => false,
                                //'last' => false, // Stop validation after this rule
                                //'on' => 'create', // Limit validation to 'create' or 'update' operations
                        ),
                ),
                'email' => array(
                        'email' => array(
                                'rule' => array('email'),
                                //'message' => 'Your custom message here',
                                //'allowEmpty' => false,
                                'required' => false,
                                //'last' => false, // Stop validation after this rule
                                //'on' => 'create', // Limit validation to 'create' or 'update' operations
                        ),
                ),
                'password' => array(
                        'notEmpty' => array(
                                'rule' => array('notEmpty'),
                                //'message' => 'Your custom message here',
                                //'allowEmpty' => false,
                                'required' => false,
                                //'last' => false, // Stop validation after this rule
                                //'on' => 'create', // Limit validation to 'create' or 'update' operations
                        ),
                ),
*/
        );

        //The Associations below have been created with all possible keys, those that are not needed can be removed

        public function checkFbUser($user,$access_token){
                $conditions = array('User.email' => $user->email);
                // $conditions = array('User.facebook_id' => $user->id);
                $nativeUser = $this->find('first',array('conditions' => $conditions,'recursive' => -1));
                if(!empty($nativeUser) && isset($nativeUser['User']['id'])){
                        $nativeUser['User']['fb_access_token'] = $access_token;
                        $nativeUser['User']['active'] = 1;
                        $nativeUser['User']['username'] =  $user->email;
                        $nativeUser['User']['facebook_id'] = $user->id;
                $this->log(print_r($nativeUser,1),'fb');
                        $this->save($nativeUser);
                }else{
                        $conditions = array('User.facebook_id' => $user->id);
                        $nativeUser = $this->find('first',array('conditions' => $conditions,'recursive' => -1));
                        if(empty($nativeUser) && !isset($nativeUser['User']['id'])){
                                $data['fb_access_token'] = $access_token;
                                $data['active'] = 1;
                                $data['facebook_id'] = $user->id;
                                $data['email'] = $user->email;
                                $data['username'] =  $user->email;
                                $data['first_name'] = $user->first_name;
                                // $data['group_id'] = $groupId;
                                if(!empty($user->middle_name)){
                                        $data['last_name'] = $user->middle_name.' '.$user->last_name;
                                }else{
                                        $data['last_name'] = $user->last_name;
                                }
                                if(!empty($user->gender)){
                                        $data['gender'] = $user->gender;
                                }
                                if(!empty($user->location->name)){
                                        $parts = explode(',',$user->location->name);
                                        $data['city'] = $parts[0];                              
                                }
                                if(!empty($user->birthday)){
                                        $parts = explode('/',$user->birthday);
                                        $data['dob'] = $parts[2].'-'.$parts[0].'-'.$parts[1];
                                }
                                // $data['active'] = 1;
                                $this->log(print_r($data,1),'fb');
                                $this->set($data);
                                if($this->validates()){
                                        $this->save($data);
                                }else{
                                        $this->log(print_r($this->validationErrors,1),'fb');
                                }
                        }
                }
                return $this->id;
        }

        function afterSave($created, $options = array()) {
                if($created){
                    $this->generateReferralUrl($this->id);
                }
        }

        public function generateReferralUrl($id) {
                $url = 'http://api.letushaggle.com/register/?referrer='.$id;
                $referralUrl = $this->make_bitly_url($url,'webappniche','R_bb459f7deeace4103f50c32a296e2b95');
                $sql = "UPDATE users SET referral_url = '$referralUrl' WHERE id = $id";
                $this->query($sql);
                //$data =array('User'=> array('id' => $id,'referral_url'=>$referralUrl));
                //$this->save($data);
        }       
}