<?php
	require_once( $GLOBALS["site_root"] . "/includes/urlgen/MyUser.inc.php" );

	class UserModel {
		public function addUser( $p ) {
			// insert user
			$ins = array();
			if (isset( $p["reg_tstamp"] ) && $p["reg_tstamp"]!="0000-00-00 00:00:00") $ins["reg_tstamp"]=sprintf( "'%s'" , FNTH_db_addslashes( $p["reg_tstamp"] ) );
			if (isset( $p["user_name"] ) && !empty( $p["user_name"] )) $ins["user_name"]=sprintf( "'%s'" , FNTH_db_addslashes( $p["user_name"] ) );
			if (isset( $p["contact_phone"] ) && !empty( $p["contact_phone"] )) $ins["contact_phone"]=sprintf( "'%s'" , FNTH_db_addslashes( $p["contact_phone"] ) );
			if (isset( $p["is_acknowledged"] ))
				$ins["is_acknowledged"]=($p["is_acknowledged"]) ? "true" : "false";
			if (isset( $p["is_admin_acknowledged"] ))
				$ins["is_admin_acknowledged"]=($p["is_admin_acknowledged"]) ? "true" : "false";
			if (isset( $p["is_instant_sender"] ))
				$ins["is_instant_sender"]=($p["is_instant_sender"]) ? "true" : "false";
			if (isset( $p["is_registered"] ))
				$ins["is_registered"]=($p["is_registered"]) ? "true" : "false";
			if (isset( $p["npublic_comment"] )) $ins["npublic_comment"]=sprintf( "'%s'" , FNTH_db_addslashes( $p["npublic_comment"] ) );
			if (isset( $p["public_comment"] )) $ins["public_comment"]=sprintf( "'%s'" , FNTH_db_addslashes( $p["public_comment"] ) );
			if (isset( $p["url"] )) $ins["url"]=sprintf( "'%s'" , FNTH_db_addslashes( $p["url"] ) );
			$ins["registration_ip"]=sprintf( "'%s'" , $_SERVER["REMOTE_ADDR"] );
			$ins["email"]=sprintf( "'%s'" , FNTH_db_addslashes( $p["email"] ) );
			// $ins["username"]=sprintf( "'%s'" , FNTH_db_addslashes( $p["username"] ) );
			$ins["hash"] = sprintf( "'%s'", FNTH_db_addslashes(
				(isset( $p["hash"] ) && strlen( $p["hash"] ) == 32) ? $p["hash"] : md5( uniqid( "" ) ) ) );
			$ins["pwd"]=sprintf( "'%s'" , FNTH_db_addslashes( $p["pwd"] ) );
			FNTH_db_free_result( FNTH_db_query(  FNTH_db_generate_insert_query( "users" , $ins ) ) );

			$q = sprintf( "select currval('users_user_id_seq') as num" );
			$seq = FNTH_db_get_row( $q );
			$user_id = $seq["num"];
			return $user_id;
		}

		public function updateUser( $user_id , $p ) {
			$upd = array();
			if (isset( $p["reg_tstamp"] ) && $p["reg_tstamp"]!="0000-00-00 00:00:00") $upd["reg_tstamp"]=sprintf( "'%s'" , FNTH_db_addslashes( $p["reg_tstamp"] ) );
			if (isset( $p["user_name"] ) && !empty( $p["user_name"] )) $upd["user_name"]=sprintf( "'%s'" , FNTH_db_addslashes( $p["user_name"] ) );
			if (isset( $p["email"] ) && !empty( $p["email"] )) $upd["email"]=sprintf( "'%s'" , FNTH_db_addslashes( $p["email"] ) );
			if (isset( $p["contact_phone"] ) && !empty( $p["contact_phone"] )) $upd["contact_phone"]=sprintf( "'%s'" , FNTH_db_addslashes( $p["contact_phone"] ) );
			if (isset( $p["pwd"] ) && !empty( $p["pwd"] )) $upd["pwd"]=sprintf( "'%s'" , FNTH_db_addslashes( $p["pwd"] ) );
			if (isset( $p["hash"] ) && !empty( $p["hash"] )) $upd["hash"]=sprintf( "'%s'" , FNTH_db_addslashes( $p["hash"] ) );

			if (isset( $p["is_acknowledged"] )) $upd["is_acknowledged"]=($p["is_acknowledged"]) ? "true" : "false";
			if (isset( $p["is_admin_acknowledged"] )) $upd["is_admin_acknowledged"]=($p["is_admin_acknowledged"]) ? "true" : "false";
			if (isset( $p["is_instant_sender"] )) $upd["is_instant_sender"]=($p["is_instant_sender"]) ? "true" : "false";
			if (isset( $p["is_registered"] )) $upd["is_registered"]=($p["is_registered"]) ? "true" : "false";
			if (isset( $p["npublic_comment"] )) $upd["npublic_comment"]=sprintf( "'%s'" , FNTH_db_addslashes( $p["npublic_comment"] ) );
			if (isset( $p["public_comment"] )) $upd["public_comment"]=sprintf( "'%s'" , FNTH_db_addslashes( $p["public_comment"] ) );
			if (isset( $p["url"] )) $upd["url"]=sprintf( "'%s'" , FNTH_db_addslashes( $p["url"] ) );

			$q = FNTH_db_generate_update_query( "users" , $upd , sprintf( "user_id=%d" , $user_id ) );
			FNTH_db_free_result( FNTH_db_query( $q ) );
		}


		public function deleteUser( $user_id , $delete = true ) {
			$query = sprintf( "update users set is_deleted=%s where user_id=%d" , ($delete) ? "true" : "false" , $user_id );
			FNTH_db_free_result( FNTH_db_query( $query ) );
		}

		public function getUserHash( $user_id ) {
			$query = sprintf( "select hash from users where user_id=%d" , $user_id );
			$row = FNTH_db_get_row( $query );
			if (!$row) return false;
			return $row["hash"];
		}

		public function getUserIdFromOldId( $oldid ) {
			$query = sprintf( "select user_id from members where oldid=%d" , $oldid );
			$row = FNTH_db_get_row( $query );
			if (!$row) return false;
			return $row["user_id"];
		}
		public function getUserIdFromEmail( $email ) {
			$query = sprintf( "select user_id from users where email='%s' and not is_deleted",
				FNTH_db_addslashes( $email ) );
			$row = FNTH_db_get_row( $query );
			if (!$row) return false;
			return $row["user_id"];
		}
		
		public function getUserIdFromHash( $hash ) {
			$query = sprintf( "select user_id from users where hash='%s' and not is_deleted",
				FNTH_db_addslashes( $hash ) );
			$row = FNTH_db_get_row( $query );
			if (!$row) return false;
			return $row["user_id"];
		}

		protected function prepareUser( $user ) {
			$user["url_activate"] = MyUserURL::renderRegistrationActivationURL( array( "hash" => $user["hash"] ) );
			$user["url_registrate"] = MyUserURL::renderRegistrationURL();
			$user["url_feedback"] = MyUserURL::renderFeedbackURL( array( "user_id" => $user["user_id"] ) );
			return $user;
		}

		public function getUserFromUserId( $user_id ) {
			$query = sprintf( "select * from users where user_id=%d" , $user_id );
			$user = FNTH_db_get_row( $query );
			$user = $this->prepareUser( $user );	
			return $user;
		}

		public function acknowledgeUser( $user_id ) {
			$query = sprintf( "select count(*) as num from users where not is_acknowledged and user_id=%d" , $user_id );
			$kell_e = FNTH_db_get_field( $query , "num" );
			$query = sprintf( "update users set is_acknowledged=true where user_id=%d" , $user_id );
			FNTH_db_free_result( FNTH_db_query( $query ) );
			return ($kell_e>0);
		}

		public function isUserExistsOnEmail( $email ) {
			$query = sprintf( "select email from users where email='%s' and not is_deleted",
				FNTH_db_addslashes( $email ) );
			$row = FNTH_db_get_row( $query );
			return ($row) ? true : false;
		}

		public function getUsersFromUserIds( $user_ids = array()) {
			if (empty( $user_ids )) return array();
			$query = sprintf( "select * from users where user_id in (%s)" , 
				implode( "," , $user_ids ) );
			return FNTH_db_get_resultset( $query );	
		}

		public function resetPassword( $user_id ) {
			$chars="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
			$len = strlen( $chars );

			$genpwd = "";
			for ($i=0;$i<8;$i++) $genpwd.=substr( $chars , rand()%$len , 1 );

			$up = array();
			$up["pwd"]=$genpwd;
			$this->updateUser( $user_id , $up );

			$mail = new TemplateMailModel( "sys_forgotten_password" );
			$mail->setParam( "new_password" , $genpwd );

			$mail->sendMail();
			FNTH_redirect( MyUserUrl::renderForgottenPasswordSentURL() );

			return $genpwd;
		}

		public function adminAcknowledgeUser( $user_id , $value = true ) {
			$query = sprintf( "update users set is_admin_acknowledged=%s where user_id=%d",
				($value) ? "true" : "false" , $user_id );
			FNTH_db_free_result( FNTH_db_query( $query ) );
		}

		public function isUserAcknowledged( $user_id ) {
			$query = sprintf( "select is_acknowledged from users where user_id=%d" , $user_id );
			return (FNTH_db_get_field( $query , "is_acknowledged" )=="t");
		}

		public function getUserType( $user_id ) {
			$user = $this->getUserFromUserId( $user_id );
			if (!$user) return false;
			if ($user["is_registered"]=="f") return "unregistered_user";
			require_once( $GLOBALS["site_root"] . "/includes/models/Member.inc.php" );
			$mm = new MemberModel();
			$member = $mm->getMemberFromUserId( $user_id );
			if (!$member) return "user";
			return $member["member_type"];
		}

		public function isUserRegistered( $user_id , $user = false ) {
			if (!$user) {
				$user = $this->getUserFromUserId();
				if (!$user) return false;
			}
			return ($user["is_registered"]=="t");
		}

		public function isUserGetsFeedbackEmail( $user_id , $user = false ) {
			if (!$user) {
				$user = $this->getUserFromUserId();
				if (!$user) return false;
			}
			if (!$this->isUserRegistered( $user_id , $user )) return false;
			// torolt user sem kapja meg
			if ($user["is_deleted"]=="t") return false;
			return true;
		}

		public function touchFeedbackOrderingTstamp( $user_id ) {
			$query = sprintf( "update users set feedback_ordering_tstamp=now() where user_id=%d" , $user_id );
			FNTH_db_free_result( FNTH_db_query( $query ) );
		}

		protected function mergeUserTable( $tablename , $fieldname , $sid , $tid ) {
			$query = sprintf( "update %s set %s=%d where %s=%d",
				$tablename,
				$fieldname,
				$tid,
				$fieldname,
				$sid );
			FNTH_db_free_result( FNTH_db_query( $query ) );
		}

		public function mergeUserInto( $source_user_id , $target_user_id , $target_survives = true ) {
			FNTH_db_free_result( FNTH_db_query( "begin transaction" ) );
			$this->mergeUserTable( "cc_transactions" , "user_id" , $source_user_id , $target_user_id );
			$this->mergeUserTable( "chat_messages" , "poster_user_id" , $source_user_id , $target_user_id );
			$this->mergeUserTable( "chat_messages" , "target_user_id" , $source_user_id , $target_user_id );
			$this->mergeUserTable( "chatroom_users" , "user_id" , $source_user_id , $target_user_id );
			$this->mergeUserTable( "coupon_tickets" , "user_id" , $source_user_id , $target_user_id );
			$this->mergeUserTable( "forum_banned_users" , "user_id" , $source_user_id , $target_user_id );
			$this->mergeUserTable( "forum_banned_users" , "banned_user_id" , $source_user_id , $target_user_id );
			$this->mergeUserTable( "forum_posts" , "owner_user_id" , $source_user_id , $target_user_id );
			$this->mergeUserTable( "forum_topics" , "last_poster_user_id" , $source_user_id , $target_user_id );
			$this->mergeUserTable( "forum_topics" , "owner_user_id" , $source_user_id , $target_user_id );
			$this->mergeUserTable( "jobs" , "poster_user_id" , $source_user_id , $target_user_id );
			$this->mergeUserTable( "larticle_comments" , "user_id" , $source_user_id , $target_user_id );
			$this->mergeUserTable( "members" , "user_id" , $source_user_id , $target_user_id );
			$this->mergeUserTable( "pager_messages" , "poster_user_id" , $source_user_id , $target_user_id );
			$this->mergeUserTable( "pager_messages" , "target_user_id" , $source_user_id , $target_user_id );
			$this->mergeUserTable( "todo_listitems" , "user_id" , $source_user_id , $target_user_id );
			$this->mergeUserTable( "user_comment_details" , "poster_user_id" , $source_user_id , $target_user_id );
			$this->mergeUserTable( "user_comment_details" , "target_user_id" , $source_user_id , $target_user_id );
			$this->mergeUserTable( "user_comments" , "user_id" , $source_user_id , $target_user_id );
			$this->mergeUserTable( "user_log" , "user_id" , $source_user_id , $target_user_id );
			$this->mergeUserTable( "user_template_users" , "user_id" , $source_user_id , $target_user_id );
			$this->mergeUserTable( "user_templates" , "owner_user_id" , $source_user_id , $target_user_id );
			$this->mergeUserTable( "users_keywords" , "user_id" , $source_user_id , $target_user_id );
			FNTH_db_free_result( FNTH_db_query( sprintf( "delete from users where user_id=%d" , $source_user_id ) ) );
			FNTH_db_free_result( FNTH_db_query( "commit transaction" ) );
		}
	}

?>
