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

	class MyMemberModel extends MyUserModel {

		public function getMenu( $pass = false) {
			$retval = array();
			if ($pass || $this->isMemberLoggedin()) {
				$mmu = new MyMemberURL();
				$retval[]=array(
					"name" => "registrational_datas",
					"url" => "/adataim/",
					"title" => __("menu_My_member_Registrational_datas") );
				$retval[]=array(
					"name" => "profile_datas",
					"url" => "/adataim/parameterek/",
					"title" => __("menu_My_member_Profile_datas" ) );
				$retval[]=array(
					"name" => "billing_shipping",
					"url" => "/adataim/szamlazas-postazas/",
					"title" => __("menu_My_member_Billing_shipping_datas" ) );
				$retval[]=array(
					"name" => "specialties",
					"url" => "/adataim/szakteruletek/",
					"title" => __("menu_My_member_Specialties" ) );
				$retval[]=array(
					"name" => "languages",
					"url" => $mmu->renderMyLanguagesURL(),
					"title" => __("menu_My_member_Languages") );
				$retval[]=array(
					"name" => "language_pairs",
					"url" => $mmu->renderMyLanguagePairsURL(),
					"title" => __("menu_My_member_Language_pairs") );
				$retval[]=array(
					"name" => "memberships",
					"url" => $mmu->renderMyMembershipsURL(),
					"title" => __("menu_My_member_Memberships") );
				$retval[]=array(
					"name" => "cat",
					"url" => $mmu->renderMyCATURL(),
					"title" => __("menu_My_member_CAT") );
				$retval[]=array(
					"name" => "capacity",
					"url" => $mmu->renderMyCapacityURL(),
					"title" => __("menu_My_member_Capacity") );
				$retval[]=array(
					"name" => "user_interface",
					"url" => $mmu->renderMyUserInterfaceURL(),
					"title" => __("menu_My_member_User_interface") );
			}
			return $retval;
		}


		public function loginUser( $user_id ) {
			parent::loginUser( $user_id );	
			$query = sprintf( "select * from members where user_id=%d and not is_deleted",$user_id );
			$member = FNTH_db_get_row( $query );
			if ($member) {
				$_SESSION["member_id"]=$member["member_id"];
				$this->refreshLoggedInMember();
				$_SESSION["old_member"]=(isset( $member["oldid"] ));
			} else {
				unset( $_SESSION["member_id"] );
				unset( $_SESSION["member_expires"] );
				unset( $_SESSION["member_type"] );
				unset( $_SESSION["old_member"] );
			}
		}

		public function refreshLoggedInMember() {
			$mm = new MemberModel();
			$member = $mm->getMemberFromMemberId( $this->getLoggedinMemberId() );
			$_SESSION["member_expires"]=fo3_convert_YmdHis_to_unix( $member["expires_tstamp"] );
			$_SESSION["member_type"]=$member["member_type"];
		}

		public function loginMember( $member_id ) {
			$mm = new MemberModel();
			$member = $mm->getMemberFromMemberId( $member_id );
			if (!$member) return;
			parent::loginUser( $member["user_id"] );
			$_SESSION["member_id"]=$member["member_id"];
			$_SESSION["old_member"]=(isset( $member["oldid"] ));
			$this->refreshLoggedInMember();
		}

		public function isOldMemberLoggedin() {
			if (!isset( $_SESSION["old_member"] )) return false;
			return $_SESSION["old_member"];
		}
		
		public function isMemberLoggedin() {
			return isset($_SESSION["member_id"]);
		}

		public function isLoggedinMemberActiveSubscriber() {
			if (!isset( $_SESSION["member_id"] )) return false;
			if (!isset( $_SESSION["member_expires"] )) return false;
			if ($_SESSION["member_expires"]>fo3_time()) return true;
			return false;
		}

		public function getLoggedinMemberId() {
			return (isset($_SESSION["member_id"])) ? $_SESSION["member_id"] : false;
		}

		public function getLoggedinMemberType() {
			return (isset($_SESSION["member_type"])) ? $_SESSION["member_type"] : false;
		}

		public function logoutUser( ) {
			parent::logoutUser();
			unset( $_SESSION["member_id"] );
			unset( $_SESSION["member_expires"] );
			unset( $_SESSION["member_type"] );
			unset( $_SESSION["old_member"] );
		}
		
		public function getUserIdFromUsername( $username ) {
			switch ($GLOBALS["model_version"]) {
			case 3.0:
			default:
				$query = sprintf( "select
					u.user_id as id from users as u,members as m
				where
					u.user_id=m.user_id and
					m.member_username='%s' and
					not u.is_deleted and 
					not m.is_deleted",
					FNTH_db_addslashes( $username ) );
				break;
			}
			$row = FNTH_db_get_row( $query );
			if (!$row) return false;
			return $row["id"];
		}

		public function getUserIdFromUsernameAndPassword( $username , $password ) {
			switch ($GLOBALS["model_version"]) {
			case 3.0:
			default:
				$query = sprintf( "select
					u.user_id as id from users as u,members as m
				where
					u.user_id=m.user_id and
					m.member_username='%s' and
					u.pwd='%s' and
					not u.is_deleted and 
					not m.is_deleted",
					FNTH_db_addslashes( $username ),
					FNTH_db_addslashes( $password ) );
				break;
			}
			$row = FNTH_db_get_row( $query );
			if (!$row) return false;
			return $row["id"];
		}

		public function getLoggedInUserIdentificator() {
			if (!isset( $_SESSION["user_id"] )) return false;
			if (isset( $_SESSION["member_id"] )) {
				$query = sprintf( "select user_name from users where user_id=%d" , $_SESSION["user_id"] );
				return FNTH_db_get_field( $query , "user_name" );
			} else {
				$query = sprintf( "select email from users where user_id=%d" , $_SESSION["user_id"] );
				return FNTH_db_get_field( $query , "email" );
			}
		}
		
		public function getLoggedInUserType( $translated = false ) {
			if (!isset( $_SESSION["user_id"] )) return false;
			if (isset( $_SESSION["member_id"] )) {
				$query = sprintf( "select member_type from members where member_id=%d" , $_SESSION["member_id"] );
				$val =	FNTH_db_get_field( $query , "member_type" );
				return ($translated) ? __( "users_" . $val ) : $val;
			}
			return ($translated) ? __( "job_offerer" ) : "job_offerer";
		}
	}

?>
