<?php
saxue_includedb();
class saxuememberusers extends saxueobjectdata {
		function saxuememberusers() {
				$this -> initvar( "uid", SAXUE_TYPE_INT, 0, "会员ID", false, 11 );
				$this -> initvar( "uname", SAXUE_TYPE_TXTBOX, "", "会员名", false, 30 );
				$this -> initvar( "password", SAXUE_TYPE_TXTBOX, "", "密码", false, 32 );
				$this -> initvar( "email", SAXUE_TYPE_TXTBOX, "", "邮箱", false, 30 );
				$this -> initvar( "status", SAXUE_TYPE_INT, 1, "状态", false, 1 );
				$this -> initvar( "avatar", SAXUE_TYPE_INT, 0, "有无头像", false, 1 );
				$this -> initvar( "score", SAXUE_TYPE_INT, 0, "积分", false, 11 );
				$this -> initvar( "money", SAXUE_TYPE_OTHER, "0.00", "金钱", false, null );
				$this -> initvar( "views", SAXUE_TYPE_INT, 0, "访问量", false, 11 );
				$this -> initvar( "follows", SAXUE_TYPE_INT, 0, "关注数", false, 5 );
				$this -> initvar( "fans", SAXUE_TYPE_INT, 0, "粉丝数", false, 5 );
				$this -> initvar( "isrobot", SAXUE_TYPE_INT, 0, "是否采集", false, 1 );
		} 
} 

class saxuememberusershandler extends saxueobjecthandler {
		function saxuememberusershandler( $_db = "" ) {
				$this -> saxueobjecthandler( $_db );
				$this -> basename = "memberusers";
				$this -> autoid = "uid";
				$this -> dbname = "member_users";
		} 

		function encryptpass( $_pass ) {
				return md5( 'U%^&*()_' . $_pass . '&^O&^&^&^KJ' );
		}

		function getbyname( $name ) {
				if ( !empty( $name ) ) {
						$name = saxue_dbslashes( $name );
						$sql = "SELECT * FROM " . saxue_dbprefix( $this -> dbname ) . " WHERE uname='" . $name . "'";
						if ( !( $result = $this -> db -> query( $sql ) ) ) {
								return false;
						} 
						$numrows = $this -> db -> getrowsnum( $result );
						if ( 1 <= $numrows ) {
								$tmpvar = "Saxue" . ucfirst( $this -> basename );
								${$this->basename} = new $tmpvar();
								${$this->basename} -> setvars( $this -> db -> fetcharray( $result ) );
								return ${$this->basename};
						} 
				} 
				return false;
		} 

		function changescore( $_uid, $_score, $_type = 0, $_aid = 0 ) {
				if ( empty( $_score ) || !is_numeric( $_score ) || empty( $_uid ) || !is_numeric( $_uid ) ) {
						return false;
				} 
				$_userinfo = $this -> get( $_uid );
				if ( !is_object( $_userinfo ) ) {
						return false;
				} 
				$_balance = $_userinfo -> getvar( "score", "n" );
				$_balance += $_score;
				if ( $_balance < 0 ) {
						return false;
				} 
				$_userinfo -> setvar( 'score', $_balance );
				if ( !$this -> insert( $_userinfo ) ) {
						return false;
				}
				include_once SAXUE_ROOT_PATH . "/model/member_score.php";
				$score_handler = &saxuememberscorehandler :: getinstance( "saxuememberscorehandler" );
				$_record = array( 'uid' => $_uid, 'amount' => $_score, 'balance' => $_balance, 'type' => $_type, 'aid' => $_aid );
				$score_handler -> addlog( $_record );
				if ( $_SESSION['saxueUserId'] == $_uid ) {
						$GLOBALS['_SESSION']['saxueUserScore'] = $_balance;
				} 
				return true;
		}

		function changemoney( $_uid, $_money, $_type = 0, $_aid = 0 ) {
				if ( empty( $_money ) || !is_numeric( $_money ) || $_money == 0 || empty( $_uid ) || !is_numeric( $_uid ) ) {
						return false;
				} 
				$_money = saxue_dround( $_money );
				$_userinfo = $this -> get( $_uid );
				if ( !is_object( $_userinfo ) ) {
						return false;
				} 
				$_balance = $_userinfo -> getvar( "money", "n" );
				$_balance = saxue_dround( $_balance + $_money );
				if ( $_balance < 0 ) {
						return false;
				} 
				$_userinfo -> setvar( 'money', $_balance );
				if ( !$this -> insert( $_userinfo ) ) {
						return false;
				}
				include_once SAXUE_ROOT_PATH . "/model/member_money.php";
				$money_handler = &saxuemembermoneyhandler :: getinstance( "saxuemembermoneyhandler" );
				$_record = array( 'uid' => $_uid, 'amount' => $_money, 'balance' => $_balance, 'type' => $_type, 'aid' => $_aid );
				$money_handler -> addlog( $_record );
				if ( $_SESSION['saxueUserId'] == $_uid ) {
						$GLOBALS['_SESSION']['saxueUserMoney'] = $_balance;
				} 
				return true;
		}
}