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

	define( "USER_LOG_GENERAL" , 0x0100 );
	define( "USER_REG_SIMPLE_USER_REGISTERED"	, 0x0001 );
	define( "USER_REG_SIMPLE_USER_ACTIVATED"	, 0x0002 );
	define( "USER_REG_PARTTIME_USER_REGISTERED"	, 0x0003 );
	define( "USER_REG_PARTTIME_USER_ACTIVATED"	, 0x0004 );
	define( "USER_REG_FREELANCER_USER_REGISTERED"	, 0x0005 );
	define( "USER_REG_FREELANCER_USER_ACTIVATED"	, 0x0006 );
	define( "USER_REG_AGENCY_USER_REGISTERED"	, 0x0007 );
	define( "USER_REG_AGENCY_USER_ACTIVATED"	, 0x0008 );

	define( "USER_GENERAL_MODIFIED_PROFILE" , 0x0009 );

	define( "USER_LOG_MONETARY"			,0x0200 );
	define( "USER_MONETARY_COUPON"		,0x0001 );
	define( "USER_MONETARY_CREDITCARD"	,0x0002 );

	define( "USER_LOG_LOGIN"	,0x0300 );
	define( "USER_LOGIN_ENTER"	,0x0001 );
	define( "USER_LOGIN_EXIT"	,0x0002 );


	define( "USER_LOG_BROWSE"	,0x0400 );
	define( "USER_LOG_JOB_OFFER" , 0x0500 );
	define( "USER_JOB_OFFER_ACTIVATED"			, 0x0001 );

	define( "USER_LOG_JOB_QUOTE" , 0x0600 );
	define( "USER_JOB_QUOTE_SENT", 0x0001 );

	class UserLogModel {
		protected $user_log_type_id;
		protected $user_id;
		protected $member_id;
		protected $is_system;
		protected $is_admin;
		protected $is_user;
		protected $user_log_ipaddress;
		protected $user_log_foreign_id;
		protected $user_log_note;

		protected $mmm;

		public function initLogMessage() {
			$this->user_log_type_id = USER_LOG_GENERAL;
			$this->user_id = 0;
			$this->member_id = 0;
			$this->is_system = false;
			$this->is_admin = false;
			$this->is_user = false;
			$this->user_log_ipaddress = "";
			$this->user_log_foreign_id = "";
			$this->user_log_note = "";
		}


		public function __construct() {
			$this->initLogMessage();
			$this->mmm = new MyMemberModel();
		}

		protected function logQueueMessage() {
			$ins = array();
			$ins["user_log_type_id"]=sprintf( "%d" , ($this->user_log_type_id>>8) );
			$ins["user_log_subtype_id"]=sprintf( "%d" , $this->user_log_type_id&0xFF );
			if ($this->user_id) $ins["user_id"]=sprintf( "%d" , $this->user_id );
			if ($this->member_id) $ins["member_id"]=sprintf( "%d" , $this->member_id );
			$ins["is_system"]=($this->is_system) ? "true" : "false";
			$ins["is_admin"]=($this->is_admin) ? "true" : "false";
			$ins["is_user"]=($this->is_user) ? "true" : "false";
			$ins["user_log_ipaddress"]=sprintf( "'%s'" , FNTH_db_addslashes( $this->user_log_ipaddress ) );
			$ins["user_log_foreign_id"]=sprintf( "'%s'" , FNTH_db_addslashes( $this->user_log_foreign_id ) );
			$ins["user_log_note"]=sprintf( "'%s'" , FNTH_db_addslashes( $this->user_log_note ) );
			$q = FNTH_db_generate_insert_query( "user_log" , $ins );
			FNTH_db_free_result( FNTH_db_query( $q ) );
		}

		public function initUserMessage( $user_id = 0) {
			$this->is_user = true;
			if ($user_id) {
				$this->user_id = $user_id;
			} else {
				$this->user_id = $this->mmm->getLoggedinUserId();
				$this->member_id = $this->mmm->getLoggedinMemberId();
			}
		}

		public function logSimpleUserMessage( $message = "" , $type = USER_LOG_GENERAL , $user_id = 0 , $member_id = 0 ) {
			$this->initLogMessage();
			$this->initUserMessage( $user_id );
			if ($member_id) $this->member_id = $member_id;
			$this->user_log_type_id = $type;
			$this->user_log_ipaddress = fo3_get_remote_ipaddress();
			$this->user_log_note = $message;
			$this->logQueueMessage();
		}

		public function logLogin() {
			require_once( $GLOBALS["site_root"] . "/includes/models/User.inc.php" );
			$um = new UserModel();
			$user = $um->getUserFromUserId( $this->mmm->getLoggedinUserId() );
			$member_id = ($this->mmm->isMemberLoggedIn()) ? $this->mmm->getLoggedinMemberId() : 0;
			$name = sprintf( "%s (#%d)" , $user["user_name"] , $user["user_id"] );
			$this->logSimpleUserMessage( sprintf( "%s bejelentkezett" , $name ) , USER_LOG_LOGIN | USER_LOGIN_ENTER , $user["user_id"] , $member_id );
		}
		
		public function logLogout() {
			require_once( $GLOBALS["site_root"] . "/includes/models/User.inc.php" );
			$um = new UserModel();
			$user = $um->getUserFromUserId( $this->mmm->getLoggedinUserId() );
			$name = sprintf( "%s (#%d)" , $user["user_name"] , $user["user_id"] );
			$this->logSimpleUserMessage( sprintf( "%s kijelentkezett" , $name ) , USER_LOG_LOGIN | USER_LOGIN_EXIT );
		}

		public function logFailedLogin( $user_id ) {
			$this->initLogMessage();
			$this->is_user = true;
			require_once( $GLOBALS["site_root"] . "/includes/models/User.inc.php" );
			$user = $um->getUserFromUserId( $user_id );
			$this->user_id = $user_id;
			$name = sprintf( "%s (#%d)" , $user["user_name"] , $user["user_id"] );
			$this->user_log_note = sprintf( "%s hibás bejelentkezés" , $name );
			$this->user_log_type_id = USER_LOG_LOGIN;
			$this->logQueueMessage();
		}

		public function logRegistration( $user_id , $name ) {
			$this->initLogMessage();
			$this->initUserMessage( $user_id );
			$this->logSimpleUserMessage(
				sprintf( "%s (#%d) regisztrált" , $name , $user_id ),
				USER_LOG_GENERAL | USER_REG_SIMPLE_USER_REGISTERED , 
				$user_id );
		}
		
		public function logMemberRegistration( $user_id , $member_id , $type , $name ) {
			$this->initLogMessage();
			$this->initUserMessage( $user_id );
			$flags = USER_LOG_GENERAL;
			switch ($type) {
			case "parttime": 	
				$flags|= USER_REG_PARTTIME_USER_REGISTERED;
				break;
			case "freelancer":
				$flags!= USER_REG_FREELANCER_USER_REGISTERED;
				break;
			case "agency":
				$flags|= USER_REG_AGENCY_USER_REGISTERED;
				break;
			}	
			$this->logSimpleUserMessage(
				sprintf( "%s (#%d) regisztrált" , $name , $user_id , $member_id ),
				USER_LOG_GENERAL | USER_REG_SIMPLE_USER_REGISTERED , 
				$user_id , 
				$member_id );
		}

		public function logMemberActivity( $flags , $msg , $member_id = 0 ) {
			require_once( $GLOBALS["site_root"] . "/includes/models/User.inc.php" );

			$this->initLogMessage();
			$mmm = new MyMemberModel();
			$mm = new MemberModel();
			$um = new UserModel();

			if (!$member_id) $member_id = $mmm->getLoggedinMemberId();
			$member = $mm->getMemberFromMemberId( $member_id );
			if ($member) {
				$user_id = $member["user_id"];
			} else {
				$user_id = $mmm->getLoggedinUserId();
			}

			$this->initUserMessage( $user_id );

			$user = $um->getUserFromUserId( $user_id );
			$this->logSimpleUserMessage(
				sprintf( "%s (#%d) %s", $user["user_name"],	$user_id, $msg ),
				$flags,
				$user_id,
				$member_id );
		}

		public function logActivation( $user_id ) {
			$this->initLogMessage();
			$this->initUserMessage( $user_id );
			$um = new UserModel();
			$user = $um->getUserFromUserId( $user_id );
			if (!$user) return;

			$flags = USER_LOG_GENERAL;
			$mm = new MemberModel();
			$member = $mm->getMemberFromUserId( $user_id );
			$member_id = 0;
			if ($member) {
				$member_id = ($member) ? $member["member_id"] : 0;
				switch ($member["member_type"]) {
				case "parttime": 	
					$flags|= USER_REG_PARTTIME_USER_ACTIVATED;
					break;
				case "freelancer":
					$flags!= USER_REG_FREELANCER_USER_ACTIVATED;
					break;
				case "agency":
					$flags|= USER_REG_AGENCY_USER_ACTIVATED;
					break;
				}	
			} else {
				$flags|= USER_REG_SIMPLE_USER_ACTIVATED;
			}
			$this->logSimpleUserMessage(
				sprintf( "%s (#%d) aktivált" , $user["user_name"] , $user_id ),
				$flags , 
				$user_id , $member_id );
		}

	}

?>
