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

	class MemberModel {

		public function addMember( $user_id , $p ) {
			$ins = array();
			$ins["user_id"]=sprintf( "%d" , $user_id );
			$ins["member_username"]=sprintf( "'%s'" , FNTH_db_addslashes( $p["member_username"] ) );
			$ins["member_aliasname"]=sprintf( "'%s'" , FNTH_db_addslashes( $p["member_aliasname"] ) );
			$ins["member_type"]=sprintf( "'%s'" , FNTH_db_addslashes( $p["member_type"] ) );
			$ins["current_status"]=sprintf( "'%s'" , FNTH_db_addslashes( $p["current_status"] ) );
			$ins["use_email"]=sprintf( "'%s'" , FNTH_db_addslashes( $p["use_email"] ) );
			$ins["zip"]=sprintf( "'%s'" , FNTH_db_addslashes( $p["zip"] ) );
			$ins["country_id"]=sprintf( "'%s'" , FNTH_db_addslashes( $p["country_id"] ) );
			if (isset( $p["county_id"] ) && $p["county_id"] ) $ins["county_id"]=sprintf( "%d" , $p["county_id"] );
			$ins["city"]=sprintf( "'%s'" , FNTH_db_addslashes( $p["city"] ) );
			$ins["address"]=sprintf( "'%s'" , FNTH_db_addslashes( $p["address"] ) );
			if (isset( $p["phone"] )) $ins["phone"]=sprintf( "'%s'" , FNTH_db_addslashes( $p["phone"] ) );
			if (isset( $p["fax"] )) $ins["fax"]=sprintf( "'%s'" , FNTH_db_addslashes( $p["fax"] ) );
			if (isset( $p["url"] )) $ins["url"]=sprintf( "'%s'" , FNTH_db_addslashes( $p["url"] ) );
			if (isset( $p["msn"] )) $ins["msn"]=sprintf( "'%s'" , FNTH_db_addslashes( $p["msn"] ) );
			if (isset( $p["skype"] )) $ins["skype"]=sprintf( "'%s'" , FNTH_db_addslashes( $p["skype"] ) );
			if (isset( $p["facebook"] )) $ins["facebook"]=sprintf( "'%s'" , FNTH_db_addslashes( $p["facebook"] ) );
			if (isset( $p["youtube"] )) $ins["youtube"]=sprintf( "'%s'" , FNTH_db_addslashes( $p["youtube"] ) );
			$ins["preferred_paymode"]=sprintf( "'%s'" , FNTH_db_addslashes( $p["preferred_paymode"] ) );
			$ins["billing_name"]=sprintf( "'%s'" , FNTH_db_addslashes( $p["billing_name"] ) );
			$ins["billing_zip"]=sprintf( "'%s'" , FNTH_db_addslashes( $p["billing_zip"] ) );
			$ins["billing_country_id"]=sprintf( "'%s'" , FNTH_db_addslashes( $p["billing_country_id"] ) );
			$ins["billing_city"]=sprintf( "'%s'" , FNTH_db_addslashes( $p["billing_city"] ) );
			$ins["billing_address"]=sprintf( "'%s'" , FNTH_db_addslashes( $p["billing_address"] ) );
			$ins["shipping_name"]=sprintf( "'%s'" , FNTH_db_addslashes( $p["shipping_name"] ) );
			$ins["shipping_zip"]=sprintf( "'%s'" , FNTH_db_addslashes( $p["shipping_zip"] ) );
			$ins["shipping_country_id"]=sprintf( "'%s'" , FNTH_db_addslashes( $p["shipping_country_id"] ) );
			$ins["shipping_city"]=sprintf( "'%s'" , FNTH_db_addslashes( $p["shipping_city"] ) );
			$ins["shipping_address"]=sprintf( "'%s'" , FNTH_db_addslashes( $p["shipping_address"] ) );
			if (isset( $p["contact_name"] )) $ins["contact_name"]=sprintf( "'%s'" , FNTH_db_addslashes( $p["contact_name"] ) );
			if (isset( $p["contact_email"] )) $ins["contact_email"]=sprintf( "'%s'" , FNTH_db_addslashes( $p["contact_email"] ) );
			$ins["haspicture"]=(isset( $p["haspicture"] ) && $p["haspicture"]) ? "true" : "false";
			$ins["notifications"]=sprintf( "'%s'" , FNTH_db_addslashes( $p["notifications"] ) );
			$ins["status_senttstamp"]=(isset( $p["status_senttstamp"] ) && !empty( $p["status_senttstamp"] )) ? sprintf( "'%s'" , FNTH_db_addslashes( $p["status_senttstamp"] ) ) : "null";
			$ins["paid_tstamp"]=(isset( $p["paid_tstamp"] ) && !empty( $p["paid_tstamp"] )) ? sprintf( "'%s'" , FNTH_db_addslashes( $p["paid_tstamp"] ) ) : "null";
			$ins["expires_tstamp"]=(isset( $p["expires_tstamp"] ) && !empty( $p["expires_tstamp"] )) ? sprintf( "'%s'" , FNTH_db_addslashes( $p["expires_tstamp"] ) ) : "null";
			$ins["custom_language"]=sprintf( "'%s'" , FNTH_db_addslashes( $p["custom_language"] ) );
			if (isset( $p["npublic_comment"] )) $ins["npublic_comment"]=sprintf( "'%s'" , FNTH_db_addslashes( $p["npublic_comment"] ) );
			$ins["is_deleted"]=(isset( $p["is_deleted"] ) && $p["is_deleted"]) ? "true" : "false";
			$ins["is_dirty"]=(isset( $p["is_dirty"] ) && $p["is_dirty"]) ? "true" : "false";
			$ins["umod_tstamp"]=(isset( $p["umod_tstamp"] ) && !empty( $p["umod_tstamp"] )) ? sprintf( "'%s'" , FNTH_db_addslashes( $p["umod_tstamp"] ) ) : "null";
			$ins["amod_tstamp"]=(isset( $p["amod_tstamp"] ) && !empty( $p["amod_tstamp"] )) ? sprintf( "'%s'" , FNTH_db_addslashes( $p["amod_tstamp"] ) ) : "null";
			$ins["ipaddress"]=sprintf( "'%s'" , FNTH_db_addslashes( 
				(isset( $p["ipaddress"] )) ? $p["ipaddress"] : fo3_get_remote_ipaddress() ) );
			$ins["useragent"]=sprintf( "'%s'" , FNTH_db_addslashes(
				(isset( $p["useragent"] )) ? $p["useragent"] : $_SERVER["HTTP_USER_AGENT"] ) );
			if (isset( $p["capacity"] )) $ins["capacity"]=sprintf( "%d" , $p["capacity"] );

			FNTH_db_free_result( FNTH_db_query(  FNTH_db_generate_insert_query( "members" , $ins ) ) );

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

		public function setMemberCustomSpecialty( $member_id , $lang_id , $value ) {
			$prop_id = $this->getExtendedPropertyIdFromName( "custom_specialty" );
			if (!$prop_id) return;
			$this->setMemberExtendedPropertyFromId( $member_id , $prop_id , $value , $lang_id );
		}

		public function getMemberCustomSpecialty( $member_id , $lang_id ) {
			$prop_id = $this->getExtendedPropertyIdFromName( "custom_specialty" );
			if (!$prop_id) return;
			return $this->getMemberExtendedPropertyFromId( $member_id , $prop_id , $lang_id );
		}

		public function setMemberCustomSpecialties( $member_id , $arr ) {
			foreach ($arr as $lang_id => $val) $this->setMemberCustomSpecialty( $member_id , $lang_id , $val );
		}

		public function getMemberCustomSpecialties( $member_id ) {
			$retval = array();
			foreach ($GLOBALS["languages"] as $langiso => $langcode ) {
				$retval[$langcode]=$this->getMemberCustomSpecialty( $member_id , $langcode );
			}
			return $retval;
		}
		
		public function setMemberNativeLanguage( $member_id , $lang_id , $value ) {
			$prop_id = $this->getExtendedPropertyIdFromName( "native_language_text" );
			if (!$prop_id) return;
			$this->setMemberExtendedPropertyFromId( $member_id , $prop_id , $value , $lang_id );
		}

		public function getMemberNativeLanguage( $member_id , $lang_id ) {
			$prop_id = $this->getExtendedPropertyIdFromName( "native_language_text" );
			if (!$prop_id) return;
			return $this->getMemberExtendedPropertyFromId( $member_id , $prop_id , $lang_id );
		}

		public function setMemberNativeLanguages( $member_id , $arr ) {
			foreach ($arr as $lang_id => $val) $this->setMemberNativeLanguage( $member_id , $lang_id , $val );
		}

		public function getMemberNativeLanguages( $member_id ) {
			$retval = array();
			foreach ($GLOBALS["languages"] as $langiso => $langcode ) {
				$retval[$langcode]=$this->getMemberNativeLanguage( $member_id , $langcode );
			}
			return $retval;
		}

		public function updateMember( $member_id , $p ) {
			$upd = array();

			// flags - boolean
			$flags = array( "is_address_visible" , "is_phone_visible" , "is_fax_visible" , "haspicture" );
			foreach ($flags as $flag) {
				if (!isset( $p[$flag] )) continue;
				$upd[$flag]=($p[$flag]) ? "true" : "false";
			}
		
			// notifications	
			if (isset( $p["notifications"] )) $upd["notifications"]=sprintf( "'%s'" , FNTH_db_addslashes( $p["notifications"] ));
			// preferred paymode
			if (isset( $p["preferred_paymode"] )) $upd["preferred_paymode"]=sprintf( "'%s'" , FNTH_db_addslashes( $p["preferred_paymode"] ));
		
			// member_type	
			if (isset( $p["member_type"] )) $upd["member_type"]=sprintf( "'%s'" , FNTH_db_addslashes( $p["member_type"] ) );
			// use_email	
			if (isset( $p["use_email"] )) $upd["use_email"]=sprintf( "'%s'" , FNTH_db_addslashes( $p["use_email"] ) );

			// contact info
			if (isset( $p["zip"] )) $upd["zip"]=sprintf( "'%s'" , FNTH_db_addslashes( $p["zip"] ));
			if (isset( $p["city"] )) $upd["city"]=sprintf( "'%s'" , FNTH_db_addslashes( $p["city"] ));
			if (isset( $p["address"] )) $upd["address"]=sprintf( "'%s'" , FNTH_db_addslashes( $p["address"] ));
			if (isset( $p["county_id"] )) $upd["county_id"]=sprintf( "%d" , $p["county_id"] );
			if (isset( $p["country_id"] )) $upd["country_id"]=sprintf( "'%s'" , FNTH_db_addslashes( $p["country_id"] ) );


			// billing info
			if (isset( $p["billing_name"] )) $upd["billing_name"]=sprintf( "'%s'" , FNTH_db_addslashes( $p["billing_name"] ));
			if (isset( $p["billing_zip"] )) $upd["billing_zip"]=sprintf( "'%s'" , FNTH_db_addslashes( $p["billing_zip"] ));
			if (isset( $p["billing_city"] )) $upd["billing_city"]=sprintf( "'%s'" , FNTH_db_addslashes( $p["billing_city"] ));
			if (isset( $p["billing_address"] )) $upd["billing_address"]=sprintf( "'%s'" , FNTH_db_addslashes( $p["billing_address"] ));
			if (isset( $p["billing_country_id"] )) $upd["billing_country_id"]=sprintf( "'%s'" , FNTH_db_addslashes( $p["billing_country_id"] ) );
		
			// shipping info	
			if (isset( $p["shipping_name"] )) $upd["shipping_name"]=sprintf( "'%s'" , FNTH_db_addslashes( $p["shipping_name"] ));
			if (isset( $p["shipping_zip"] )) $upd["shipping_zip"]=sprintf( "'%s'" , FNTH_db_addslashes( $p["shipping_zip"] ));
			if (isset( $p["shipping_city"] )) $upd["shipping_city"]=sprintf( "'%s'" , FNTH_db_addslashes( $p["shipping_city"] ));
			if (isset( $p["shipping_address"] )) $upd["shipping_address"]=sprintf( "'%s'" , FNTH_db_addslashes( $p["shipping_address"] ));
			if (isset( $p["shipping_country_id"] )) $upd["shipping_country_id"]=sprintf( "'%s'" , FNTH_db_addslashes( $p["shipping_country_id"] ) );
			
			if (isset( $p["contact_name"] )) $upd["contact_name"]=sprintf( "'%s'" , FNTH_db_addslashes( $p["contact_name"] ) );

			if (isset( $p["npublic_comment"] )) $upd["npublic_comment"]=sprintf( "'%s'" , FNTH_db_addslashes( $p["npublic_comment"] ) );
			if (isset( $p["paid_method"] )) $upd["paid_method"]=sprintf( "'%s'" , FNTH_db_addslashes( $p["paid_method"] ) );

			if (isset( $p["phone"] )) $upd["phone"]=sprintf( "'%s'" , FNTH_db_addslashes( $p["phone"] ) );

			if (isset( $p["fax"] )) $upd["fax"]=sprintf( "'%s'" , FNTH_db_addslashes( $p["fax"] ) );
			if (isset( $p["is_fax_visible"] )) $upd["is_fax_visible"]=($p["is_fax_visible"]) ? "true" : "false";
			if (isset( $p["is_phone_visible"] )) $upd["is_phone_visible"]=($p["is_phone_visible"]) ? "true" : "false";
			
			if (isset( $p["url"] )) $upd["url"]=sprintf( "'%s'" , FNTH_db_addslashes( $p["url"] ) );
			if (isset( $p["msn"] )) $upd["msn"]=sprintf( "'%s'" , FNTH_db_addslashes( $p["msn"] ) );
			if (isset( $p["skype"] )) $upd["skype"]=sprintf( "'%s'" , FNTH_db_addslashes( $p["skype"] ) );
			if (isset( $p["facebook"] )) $upd["facebook"]=sprintf( "'%s'" , FNTH_db_addslashes( $p["facebook"] ) );
			if (isset( $p["youtube"] )) $upd["youtube"]=sprintf( "'%s'" , FNTH_db_addslashes( $p["youtube"] ) );
			
			if (isset( $p["is_forditmail"] )) $upd["is_forditmail"]=($p["is_forditmail"]) ? "true" : "false";
			
			if (isset( $p["member_username"] )) $upd["member_username"]=sprintf( "'%s'" , FNTH_db_addslashes( $p["member_username"] ) );
			if (isset( $p["member_aliasname"] )) $upd["member_aliasname"]=sprintf( "'%s'" , FNTH_db_addslashes( $p["member_aliasname"] ) );
			if (isset( $p["is_forum_poster"] )) $upd["is_forum_poster"]=($p["is_forum_poster"]) ? "true" : "false";
			if (isset( $p["is_feedback_poster"] )) $upd["is_feedback_poster"]=($p["is_feedback_poster"]) ? "true" : "false";

			// datestrings
			foreach (array( "paid_tstamp" , "status_senttstamp" , "expires_tstamp" ) as $d) {
				if (!isset( $p[$d] )) continue;
				$upd[$d]=(strlen( $p[$d] )) ? sprintf( "'%s'" , FNTH_db_addslashes( $p[$d] ) ) : "null";
			}
		
			$q = FNTH_db_generate_update_query( "members",$upd,sprintf( "member_id=%d" , $member_id ) );
			FNTH_db_free_result( FNTH_db_query( $q ) );
		}

		public function setMemberOldId( $member_id , $oldid ) {
			$upd = array( "oldid" => sprintf( "%d" , $oldid ) );
			FNTH_db_free_result( FNTH_db_query( FNTH_db_generate_update_query( "members" , $upd ,
				sprintf( "member_id=%d" , $member_id ) ) ) );
		}

		public function getMemberIdFromOldid( $oldid ) {
			$query = sprintf( "select member_id from members where oldid=%d" , $oldid );
			$row = FNTH_db_get_row( $query );
			if (!$row) return false;
			return $row["member_id"];
		}

		public function prepareMember( $member , $p = array() ) {
			if (!is_array( $member )) return $member;
			if (isset( $member["haspicture"] ) && $member["haspicture"]=="t") {
				$member["picurlwww"]=$this->getAttachmentPath( $member["member_id"] ) . "/" . $member["member_aliasname"] . ".jpg";
				$member["thumburlwww"]=$this->getAttachmentPath( $member["member_id"] ) . "/" . $member["member_aliasname"] . "-thumbnail.jpg";
				$member["picurl"]=$GLOBALS["site_root"] . $this->getAttachmentPath( $member["member_id"] ) . "/" . $member["member_aliasname"] . ".jpg";
				$member["thumburl"]=$GLOBALS["site_root"] . $this->getAttachmentPath( $member["member_id"] ) . "/" . $member["member_aliasname"] . "-thumbnail.jpg";
			}
			$member["member_attachments"]=(isset( $p["attachments"] )) ? $p["attachments"] :
				$this->getMemberAttachments( $member["member_id"] );
			$member["member_url"]=MemberURL::renderURLFromMember( $member , $p  );
		
			switch ($GLOBALS["language"]) {
			case "hu":
				if (isset( $member["user_name"] )) {
					$member["localized_name"] = $member["user_name"];
				} else {
					$member["localized_name"] = FNTH_db_get_field(
						sprintf( "select user_name from users where user_id=%d" , $member["user_id"] ) , "user_name" );
				}
				break;
			default:
				if (isset( $p["langsite_name"] )) {
					$member["localized_name"]=$p["langsite_name"];
				} else {
					$prop_id = $this->getExtendedPropertyIdFromName( "langsite_name" );
					$member["localized_name"] = $this->getMemberExtendedPropertyFromId( $member["member_id"] , $prop_id , $GLOBALS["langcode"] );
					if (!strlen( $member["localized_name"] ))
						$member["localized_name"] = FNTH_db_get_field(
							sprintf( "select user_name from users where user_id=%d" , $member["user_id"] ) , "user_name" );
				}
			}	


			$member["is_address_visible"]=($member["is_address_visible"]=="t");
			$member["is_phone_visible"]=($member["is_phone_visible"]=="t");
			$member["is_fax_visible"]=($member["is_fax_visible"]=="t");
			return $member;
		}

		public function getMemberFromUserId( $user_id ) {
			$query = sprintf( "select * from members where user_id=%d" , $user_id );
			return $this->prepareMember( FNTH_db_get_row( $query ) );
		}
		
		public function getMemberFromMemberId( $member_id ) {
			$query = sprintf( "select * from members where member_id=%d" , $member_id );
			return $this->prepareMember( FNTH_db_get_row( $query ) );
		}

		public function getMemberIdFromUsername( $username,  $nodeleted = true ) {
			$query = sprintf( "select m.member_id from members as m,users as u where m.user_id=u.user_id and m.member_username='%s'" , FNTH_db_addslashes( $username ) );
			if ($nodeleted) $query.=" and not u.is_deleted and not m.is_deleted";
			$row = FNTH_db_get_row( $query );
			if (!$row) return false;
			return $row["member_id"];
		}
		
		public function getMemberIdFromAliasname( $aliasname , $nodeleted = true ) {
			switch ($GLOBALS["language"]) {
			case "hu":
				$query = sprintf( "select m.member_id from members as m,users as u where m.user_id=u.user_id and m.member_aliasname='%s'" , FNTH_db_addslashes( $aliasname ) );
				if ($nodeleted) $query.=" and not u.is_deleted and not m.is_deleted";
				$row = FNTH_db_get_row( $query );
				if (!$row) return false;
				return $row["member_id"];
			default:
				require_once( $GLOBALS["site_root"]."/includes/models/ExtendedProperties.inc.php" );
				$epm = new ExtendedPropertiesModel();
				$query = sprintf( "select member_id from member_extended_properties where language_id=%d and extended_property_id=%d and value='%s'",
					$GLOBALS["langcode"],
					$epm->getEPropIdFromName( "aliasname" ),
					FNTH_db_addslashes( $aliasname ) );
				$num = FNTH_db_get_field( $query , "member_id" );
				return $num;
			}
		}
		
		public function isUsedMemberAlias( $alias ) {
			$query = sprintf( "select count(*) as num from member_extended_properties where language_id=%d and extended_property_id=%d and value='%s'",
				$this->gen_member_alias_language_id,
				$this->gen_member_alias_property_id,
				FNTH_db_addslashes( $alias ) );
			return (FNTH_db_get_field( $query , "num")>0);
		}

		protected function generateMemberAlias( $member_id , $language_id ) {
			require_once( $GLOBALS["site_root"] . "/includes/models/ExtendedProperties.inc.php" );
			$epm = new ExtendedPropertiesModel();


			$langsite_name_id = $epm->getEPropIdFromName( "langsite_name" );
			$alias_id = $epm->getEPropIdFromName( "aliasname" );
			
			$langsite_name = $this->getMemberExtendedPropertyFromId( $member_id , $langsite_name_id , $language_id );

			$this->gen_member_alias_language_id = $language_id;
			$this->gen_member_alias_property_id = $alias_id;

			$alias = fo3_generate_unique_alias( $langsite_name , array( $this , "isUsedMemberAlias" ) , array( "lowercase" => false ) );
			return $alias;
		}

		public function updateMemberAliasname( $member_id ) {
			require_once( $GLOBALS["site_root"] . "/includes/models/ExtendedProperties.inc.php" );
			$epm = new ExtendedPropertiesModel();

			$use_langsite_id = $epm->getEPropIdFromName( "use_langsite" );
			$alias_id = $epm->getEPropIdFromName( "aliasname" );

			foreach ($GLOBALS["languages"] as $langcode => $langid ) {
				$use_langsite = $this->getMemberExtendedPropertyFromId( $member_id , $use_langsite_id , $langid );
				if ($langcode=="hu") continue;
				$this->unsetMemberExtendedPropertyFromId( $member_id , $alias_id , $langid );	// mindenkepp unseteljuk
				if ($use_langsite) {
					$aliasname = $this->generateMemberAlias( $member_id , $langid );
					$this->setMemberExtendedPropertyFromId( $member_id , $alias_id , $aliasname , $langid );
				}
			}
		}

		public function setMemberLanguages( $member_id , $language_ids = array() ) {
			// delete unused languagepairs (not in language_ids)
			if (!empty( $language_ids )) {
				$langs = implode( "," , $language_ids );
				$query = sprintf( "delete from member_languagepairs where member_id=%d and
					source_language_id not in (%s) and target_language_id not in (%s)",
					$member_id,
					$langs,$langs );
			} else {
				$query = sprintf( "delete from member_languagepairs where member_id=%d",$member_id );
			}
			FNTH_db_free_result( FNTH_db_query( $query ));
			// languages
			$query = sprintf( "delete from member_languages where member_id=%d" , $member_id );
			FNTH_db_free_result( FNTH_db_query( $query ) );

			// rebuild - FIXME, only nonexistings
			$ordering = 1;
			foreach ($language_ids as $language_id) {
				$ins=array(
					"member_id"=>sprintf( "%d" , $member_id ),
					"language_id"=>sprintf( "%d" , $language_id ),
					"ordering"=>$ordering);
				FNTH_db_free_result(
					FNTH_db_query(FNTH_db_generate_insert_query("member_languages",$ins)) );
				$ordering++;
			}
		}

		public function setMemberSpecialties( $member_id , $specialty_ids = array() ) {
			// specialties
			if (!empty( $specialty_ids )) {
				$specs = implode( "," , $specialty_ids );
				$query = sprintf( "delete from member_specialties where member_id=%d and
					specialty_id not in (%s)" , $member_id , $specs );
			} else {
				$query = sprintf( "delete from member_specialties where member_id=%d",$member_id );
			}
			FNTH_db_free_result( FNTH_db_query( $query ));
			// specialties
			$query = sprintf( "delete from member_specialties where member_id=%d" , $member_id );
			FNTH_db_free_result( FNTH_db_query( $query ) );

			// rebuild FIXME only nonexistings
			foreach ($specialty_ids as $specialty_id) {
				$ins= array( "member_id"=>sprintf( "%d" , $member_id ),"specialty_id"=>sprintf( "%d" , $specialty_id ));
				FNTH_db_free_result(
					FNTH_db_query(FNTH_db_generate_insert_query("member_specialties",$ins)) );
			}
		}

		public function getMemberSpecialties( $member_id ) {
			$query = sprintf( "select specialty_id from member_specialties where member_id=%d",
				$member_id );
			return FNTH_db_get_column( $query , "specialty_id" );
		}

		public function getMemberMemberships( $member_id ) {
			$retval = array();
			$retval["memberships"]=FNTH_db_get_column( 
				sprintf( "select membership_id from member_memberships where member_id=%d" , $member_id ),
				"membership_id" );
			$member = $this->getMemberFromMemberId( $member_id );
			$retval["custom_membership"]=$member["custom_membership"];
			return $retval;
		}
		
		public function setMemberMemberships( $member_id , $datas ) {
			FNTH_db_free_result( FNTH_db_query(
				sprintf( "delete from member_memberships where member_id=%d" , $member_id ) ) );

			if (isset( $datas["memberships"] ) && is_array( $datas["memberships"] )) {
				$ins = array();
				$ins["member_id"]=sprintf( "%d" , $member_id );
				foreach ($datas["memberships"] as $membership_id) {
					$ins["membership_id"]=sprintf( "%d" , $membership_id );
					FNTH_db_free_result( FNTH_db_query(
						FNTH_db_generate_insert_query( "member_memberships" , $ins ) ) );
				}
			}

			if (isset( $datas["custom_membership"] )) {
				$query = sprintf( "update members set custom_membership='%s' where member_id=%d",
					FNTH_db_addslashes( $datas["custom_membership"] ),
					$member_id );
				FNTH_db_free_result( FNTH_db_query( $query ) );
			}
		}
		
		public function getMemberCats( $member_id ) {
			$retval = array();
			$retval["cats"]=FNTH_db_get_associative( 
				sprintf( "select cat_id,level from member_cats where member_id=%d" , $member_id ),
				"cat_id","level" );
			$member = $this->getMemberFromMemberId( $member_id );
			$retval["custom_cat"]=$member["custom_cat"];
			return $retval;
		}

		public function setMemberCats( $member_id , $datas ) {
			FNTH_db_free_result( FNTH_db_query(
				sprintf( "delete from member_cats where member_id=%d" , $member_id ) ) );

			if (isset( $datas["cats"] ) && is_array( $datas["cats"] )) {
				$ins = array();
				$ins["member_id"]=sprintf( "%d" , $member_id );
				foreach ($datas["cats"] as $cat_id => $level ) {
					$ins["cat_id"]=sprintf( "%d" , $cat_id );
					$ins["level"]=sprintf( "%d" , $level );
					FNTH_db_free_result( FNTH_db_query(
						FNTH_db_generate_insert_query( "member_cats" , $ins ) ) );
				}
			}

			if (isset( $datas["custom_cat"] )) {
				$query = sprintf( "update members set custom_cat='%s' where member_id=%d",
					FNTH_db_addslashes( $datas["custom_cat"] ),
					$member_id );
				FNTH_db_free_result( FNTH_db_query( $query ) );
			}
		}

		public function getMemberLanguages( $member_id ) {
			return FNTH_db_get_column(
				sprintf( "select language_id from member_languages where member_id=%d order by ordering" , $member_id ),
				"language_id" );
		}

		public function getMemberLanguagePairs3D( $member_id ) {
			$query = sprintf( "select * from member_languagepairs where member_id=%d" , $member_id );
			$ds = FNTH_db_get_resultset( $query );
			$retval = array();
			foreach ($ds as $row) {
				$st_id = $row["servicetype_id"];
				$slid = $row["source_language_id"];
				$tlid = $row["target_language_id"];
				if (!isset( $retval[$st_id] )) $retval[$st_id]=array();
				if (!isset( $retval[$st_id][$slid] )) $retval[$st_id][$slid]=array();
				if (!isset( $retval[$st_id][$slid][$tlid] )) $retval[$st_id][$slid][$tlid]=true;
			}
			return $retval;
		}

		public function setMemberLanguagePairs3D( $member_id , $datas ) {
			FNTH_db_free_result( FNTH_db_query(
				sprintf( "delete from member_languagepairs where member_id=%d" , $member_id ) ) );
			$ins = array();
			$ins["member_id"]=$member_id;
			foreach ($datas as $servicetype_id => $sl ) {
				$ins["servicetype_id"]=sprintf( "%d" , $servicetype_id );
				foreach ($sl as $source_language_id => $tl) {
					$ins["source_language_id"]=sprintf( "%d" , $source_language_id );
					foreach ($tl as $target_language_id => $val) {
						$ins["target_language_id"]=sprintf( "%d" , $target_language_id );
						FNTH_db_free_result(
							FNTH_db_query( FNTH_db_generate_insert_query( "member_languagepairs" , $ins ) ) );
					}
				}
			}
		}

		public function getMembersFromMemberIds( $member_ids = array() ) {
			$epm = new ExtendedPropertiesModel();
			if (empty( $member_ids )) return array();
			$query = sprintf( "select m.*,u.* from members as m, users as u where m.user_id=u.user_id and m.member_id in (%s)" , 
				implode( "," , $member_ids ) );
			if ($GLOBALS["language"]!="hu") {
				$use_langsite_id = $epm->getEPropIdFromName( "use_langsite" );
				$query.=sprintf( " and member_id in (select member_id from member_extended_properties where extended_property_id=%d and language_id=%d and value='on')",
					$use_langsite_id,$GLOBALS["langcode"] );
				$aliasname_id = $epm->getEPropIdFromName( "aliasname" );
				$aliases = FNTH_db_get_associative( sprintf( "select member_id,value from member_extended_properties where extended_property_id=%d and language_id=%d" , $aliasname_id,$GLOBALS["langcode"] ) , "member_id","value" );
			}
			$members = FNTH_db_get_resultset( $query );

			$ds = FNTH_db_get_resultset( sprintf( "select * from member_attachments where member_id in (%s)",
				implode( "," , $member_ids ) ) );
			
			$attachments = array();
			foreach ($ds as $att) {
				if (!isset( $attachments[$att["member_id"]] )) $attachments[$att["member_id"]]=array();
				$attachments[$att["member_id"]][]=$att;
			}

			$aliasname_id = $epm->getEPropIdFromName( "aliasname" );
			$langsite_name = $epm->getEPropIdFromName( "langsite_name" );

			foreach ($members as $member) {
				$p = array();
				$p["aliasname"] = ($GLOBALS["language"]=="hu") ? $member["member_aliasname"] : $aliases[$member["member_id"]];
				$p["attachments"]=(isset( $attachments[$member["member_id"]] )) ? $attachments[$member["member_id"]] : array();
				$member = $this->prepareMember( $member , $p );
				$retval[]=$member;
			}
			return $retval;
		}

		public function getMemberTemplates( $member_id ) {
			$query = sprintf( "select * from member_templates where member_id=%d order by template_title",$member_id );
			return FNTH_db_get_resultset( $query );
		}
		
		public function getMemberTemplate( $member_id , $member_template_id  ) {
			$query = sprintf( "select * from member_templates where member_id=%d and member_template_id=%d
				order by template_title",
				$member_id , $member_template_id );
			return FNTH_db_get_row( $query );
		}

		public function addMemberTemplate( $member_id , $d = array() ) {
			$ins = array();
			$ins["member_id"]=sprintf( "%d" , $member_id );
			$ins["template_title"] = sprintf( "'%s'" , FNTH_db_addslashes( $d["template_title"] ));
			$ins["template_price"] = sprintf( "'%s'" , FNTH_db_addslashes( $d["template_price"] ));
			$ins["template_text"] = sprintf( "'%s'" , FNTH_db_addslashes( $d["template_text"] ));
			FNTH_db_free_result( FNTH_db_query( FNTH_db_generate_insert_query( "member_templates" , $ins ) ) );	
			return FNTH_db_get_field( "select currval('member_templates_member_template_id_seq') as num" , "num" );
		}

		public function updateMemberTemplate( $member_id , $member_template_id , $d = array() ) {
			$upd = array();
			$upd["template_title"] = sprintf( "'%s'" , FNTH_db_addslashes( $d["template_title"] ));
			$upd["template_price"] = sprintf( "'%s'" , FNTH_db_addslashes( $d["template_price"] ));
			$upd["template_text"] = sprintf( "'%s'" , FNTH_db_addslashes( $d["template_text"] ));
			FNTH_db_free_result( FNTH_db_query( FNTH_db_generate_update_query(
				"member_templates" , $upd , sprintf( "member_id=%d and member_template_id=%d",
				$member_id , $member_template_id ) ) ) );	
		}

		public function deleteMemberTemplate( $member_id , $member_template_id ) {
			FNTH_db_free_result( FNTH_db_query(
				sprintf( "delete from member_templates where member_id=%d and member_template_id=%d" , $member_id , $member_template_id ) ) );
		}

		public function getActiveMemberUsers( $p = array() ) {
			$where_clause = "u.user_id=m.user_id and
				u.is_acknowledged and
				not u.is_deleted and
				not m.is_deleted and
				m.expires_tstamp>now()";
			if (isset( $p["filter"] ) && strlen( $p["filter"] ))
				$where_clause.=sprintf( " and u.user_name like '%s%%'",$p["filter"] );
			$limit = (isset( $p["limit"] )) ? $p["limit"] : 10;
			$query = sprintf( "select u.user_id,u.user_name from users as u,members as m where %s
				order by u.user_name limit %d" , $where_clause , $limit );
			return FNTH_db_get_resultset( $query );
		}

		public function getExtendedPropertyIdFromName( $prop_name ) {
			$query = sprintf( "select extended_property_id from extended_properties where ep_name='%s'" , FNTH_db_addslashes( $prop_name ) );
			return FNTH_db_get_field( $query , "extended_property_id" );
		}
		
		public function getExtendedPropertyNameFromId( $extended_property_id ) {
			$query = sprintf( "select ep_name from extended_properties where extended_property_id=%d" , $extended_property_id );
			return FNTH_db_get_field( $query , "ep_name" );
		}

		public function setMemberExtendedPropertyFromId( $member_id , $prop_id , $value = "" , $language_id = 0 ) {
			$where_clause = sprintf( "member_id=%d and extended_property_id=%d" , $member_id , $prop_id );
			if ($language_id) $where_clause.=sprintf( " and language_id=%d" , $language_id );
			$query = "select count(*) as num from member_extended_properties where " . $where_clause;
			$num = FNTH_db_get_field( $query , "num" );

			$d = array();
			$d["member_id"]=$member_id;
			$d["extended_property_id"]=$prop_id;
			$d["value"]=sprintf( "'%s'" , FNTH_db_addslashes( $value ) );
			$d["language_id"]=($language_id) ? $language_id : "null";
			if ($num) {
				// update
				$query = FNTH_db_generate_update_query( "member_extended_properties" , $d , $where_clause );
			} else {
				$query = FNTH_db_generate_insert_query( "member_extended_properties" , $d );
			}
			FNTH_db_free_result( FNTH_db_query( $query ) );
		}

		public function unsetMemberExtendedPropertyFromId( $member_id , $prop_id , $language_id = 0 ) { 
			$where_clause = sprintf( "member_id=%d and extended_property_id=%d" , $member_id , $prop_id );
			if ($language_id) $where_clause.=sprintf( " and language_id=%d" , $language_id );
			FNTH_db_free_result( FNTH_db_query(
				sprintf( "delete from member_extended_properties where %s" , $where_clause ) ) );
		}

		public function getMemberExtendedPropertyFromId( $member_id , $prop_id , $language_id = 0 ) { 
			$where_clause = sprintf( "member_id=%d and extended_property_id=%d" , $member_id , $prop_id );
			if ($language_id) $where_clause.=sprintf( " and language_id=%d" , $language_id );
			$query = sprintf( "select value from member_extended_properties where %s" , $where_clause );
			return FNTH_db_get_field( $query , "value" );
		}

		public function setMemberDirty( $member_id , $dirty = true ) {
			$query = sprintf( "update members set is_dirty=%s where member_id=%d",
				($dirty) ? "true" : "false" , $member_id );
			FNTH_db_free_result( FNTH_db_query( $query ) );
		}

		public function refreshMember( $member_id ) {
			// TODO regen keywords
			$member = $this->getMemberFromMemberId( $member_id );
			$user_id = $member["user_id"];
			$um = new UserModel();
			$user = $um->getUserFromUserId( $user_id );
			
			// begin transaction
			FNTH_db_free_result( FNTH_db_query( "begin transaction" ) );

			// collect keywords
			$keywords = array();
			$longstring = "";
			// SL110604 - mindenkinek minden kulcsszo legeneralva
			// if ($member["expires_tstamp"]>fo3_date("Y-m-d H:i:s")) {
				$longstring.=" " . $user["user_name"];
				$longstring.=" " . $member["member_username"];
				$longstring.=" " . $member["member_aliasname"];
				$longstring.=" " . $member["city"];
				$longstring.=" " . $member["address"];
				// languages
				$query = sprintf( "select langdef_name from language_langdefs where language_id in (select language_id from member_languages where member_id=%d)" , $member_id );
				$languages = FNTH_db_get_column( $query , "langdef_name" );
				if (!empty( $languages )) foreach ($languages as $language) $longstring.=" " . $language;
				// services & references
				$query = sprintf( "select value from member_extended_properties where member_id=%d and extended_property_id in (select extended_property_id from extended_properties where ep_name in ('services','references'))",$member_id);
				$values = FNTH_db_get_column( $query , "value" );
				if (!empty( $values )) foreach ($values as $value) $longstring.=" " . $value;

				// memberships
				$query = sprintf( "select membership_name,membership_description from memberships where membership_id in (select membership_id from member_memberships where member_id=%d)" , $member_id );
				$ds = FNTH_db_get_resultset( $query );
				if (!empty( $ds )) foreach ($ds as $row) $longstring.=" ".$row["membership_name"] . " " . $row["membership_description"];

				// specialties
				$query = sprintf( "select langdef_name from specialty_langdefs where specialty_id in (select specialty_id from member_specialties where member_id=%d)" , $member_id );
				$specialties = FNTH_db_get_column( $query , "langdef_name" );
				if (!empty( $specialties )) foreach ($specialties as $specialty) $longstring.=" " . $specialty;

				$keywords = explode(" ",fo3_equ_delimiters(trim($longstring)));
			// }
			$keywords = array_unique( $keywords );

			// remove all former keywords
			$query = sprintf( "delete from users_keywords where user_id=%d" , $user_id );
			FNTH_db_free_result( FNTH_db_query( $query ) );
			// insert new ones
			$km = new KeywordsModel();
			foreach ($keywords as $keyword) {
				$keyword_id = $km->addGetKeyword( $keyword );
				$query = sprintf( "insert into users_keywords( user_id , keyword_id ) values(%d,%d)",
					$user_id , $keyword_id );
				FNTH_db_free_result( FNTH_db_query( $query ) );
			}

			// release is_dirty			
			$query = sprintf( "update members set is_dirty=false where member_id=%d" , $member_id );
			FNTH_db_free_result( FNTH_db_query( $query ) );
			// commit transaction
			FNTH_db_free_result( FNTH_db_query( "commit transaction" ) );
		}
		
		public function isMemberActiveSubscriber( $member_id = 0 , $member = false ) {
			if (!$member_id) return false;
			if (!$member) $member = $this->getMemberFromMemberId( $member_id );
			if (!isset( $member["expires_tstamp"] )) return false;
			if ($member["expires_tstamp"]<fo3_date( "Y-m-d H:i:s" )) return false;
			return true;
		}
		
		public function getAttachmentPath( $member_id ) {
			return sprintf( "/media/member-attachments/%d/%03d/%03d",
				(int)($member_id/1000000),
				((int)($member_id/1000))%1000,
				$member_id%1000 );
		}
		public function createAttachmentPath( $member_id ) {
			$fullpath = $GLOBALS["site_root"] . $this->getAttachmentPath( $member_id );
			if (!is_dir( $fullpath )) mkdir( $fullpath , 0777 , true );
			return $fullpath;
		}

		public function addMemberAttachment( $member_id  , $attachment = array() ) {
			if (!isset( $attachment["real_path"] )) return false;
			if (!file_exists( $attachment["real_path"] )) return false;
			$fsize = filesize( $attachment["real_path"] );
			if (!$fsize) return false;

			$ins = array();
			$ins["member_id"]=$member_id;
			$ins["attachment_title"] = sprintf( "'%s'" , FNTH_db_addslashes( $attachment["comment"] ) );
			$ins["attachment_type"]="'general'";
			$ins["attachment_mimetype"]=sprintf( "'%s'" , FNTH_db_addslashes( $attachment["mime_type"] ) );
			$ins["attachment_original_filename"]=sprintf( "'%s'", FNTH_db_addslashes( $attachment["filename"] ) );
			$ins["attachment_path"]=sprintf( "'%s'" , FNTH_db_addslashes( $this->getAttachmentPath( $member_id ) ) );
			$filename = uniqid("");
			$ins["attachment_filename"]=sprintf( "'%s'" , FNTH_db_addslashes( $filename ) );
			$ins["attachment_filesize"]=sprintf( "%d" , $fsize );
			$q = FNTH_db_generate_insert_query( "member_attachments" , $ins );
			FNTH_db_free_result( FNTH_db_query( $q ) );
			$fullpath = $this->createAttachmentPath( $member_id );
			copy( $attachment["real_path"] , $fullpath . "/" . $filename );
			return true;
		}
		
		public function prepareAttachment( $ma ) {
			$ma["icon"]=fo3_get_mimetype_icon_path( $ma["attachment_mimetype"] );
			$ma["attachment_url"]=MemberURL::renderMemberAttachmentURL( $ma["member_attachment_id"] );
			return $ma;
		}


		public function deleteMember( $member_id , $delete = true ) {
			$member = $this->getMemberFromMemberId( $member_id );
			if (!$member) return;

			$user_id = $member["user_id"];
			$query = sprintf( "update members set is_deleted=%s where member_id=%d" , ($delete) ? "true" : "false" , $member_id );
			FNTH_db_free_result( FNTH_db_query( $query ) );

			$um = new UserModel();
			$um->deleteUser( $user_id , $delete );

			return;

			// physical delete	
			// job custom recipients
			FNTH_db_free_result( FNTH_db_query( sprintf( "delete from job_custom_recipients where member_id=%d" , $member_id ) ) );
			// job notifications
			FNTH_db_free_result( FNTH_db_query( sprintf( "delete from job_notifications where member_id=%d" , $member_id ) ) );
			// job_quotes	
			FNTH_db_free_result( FNTH_db_query( sprintf( "delete from job_quotes where member_id=%d" , $member_id ) ) );
			// member attachments
			FNTH_db_free_result( FNTH_db_query( sprintf( "delete from member_attachments where member_id=%d" , $member_id ) ) );
			// member extended properties
			FNTH_db_free_result( FNTH_db_query( sprintf( "delete from member_extended_properties where member_id=%d" , $member_id ) ) );
			
			// languagepairs and languages
			FNTH_db_free_result( FNTH_db_query( sprintf( "delete from member_languagepairs where member_id=%d" , $member_id ) ) );
			FNTH_db_free_result( FNTH_db_query( sprintf( "delete from member_languages where member_id=%d" , $member_id ) ) );

			// memberships / cats / specialties / templates
			FNTH_db_free_result( FNTH_db_query( sprintf( "delete from member_cats where member_id=%d" , $member_id ) ) );
			FNTH_db_free_result( FNTH_db_query( sprintf( "delete from member_memberships where member_id=%d" , $member_id ) ) );
			FNTH_db_free_result( FNTH_db_query( sprintf( "delete from member_specialties where member_id=%d" , $member_id ) ) );
			FNTH_db_free_result( FNTH_db_query( sprintf( "delete from member_temmplates where member_id=%d" , $member_id ) ) );
			// user log
			FNTH_db_free_result( FNTH_db_query( sprintf( "delete from user_log where member_id=%d" , $member_id ) ) );

			// main record
			FNTH_db_free_result( FNTH_db_query( sprintf( "delete from members where member_id=%d" , $member_id ) ) );

	
		}

		public function deleteMemberAttachment( $member_attachment_id , $member_id = 0 ) {
			$query = sprintf( "select * from member_attachments where member_attachment_id=%d" , $member_attachment_id );
			if ($member_id) $query.=sprintf( " and member_id=%d" , $member_id );
			$row = FNTH_db_get_row( $query );
			if (!$row) return;
			$fname = $GLOBALS["site_root"] . $row["attachment_path"]. $row["attachment_filename"];
			if (file_exists( $fname )) unlink( $fname );
			$query = sprintf( "delete from member_attachments where member_attachment_id=%d" , $member_attachment_id );
			FNTH_db_free_result( FNTH_db_query( $query ) );
		}

		public function getMemberAttachments( $member_id ) {
			$query = sprintf( "select * from member_attachments where member_id=%d" , $member_id );
			$ds = FNTH_db_get_resultset( $query );
			if (empty( $ds )) return $ds;
			$retval = array();
			foreach ($ds as $row) $retval[]=$this->prepareAttachment( $row );
			return $retval;
		}

		public function getMemberAttachment( $member_attachment_id ) {
			$query = sprintf( "select * from member_attachments where member_attachment_id=%d" , $member_attachment_id );
			$retval = FNTH_db_get_row( $query );
			return $this->prepareAttachment( $retval );
		}


		public function updateMemberStatus( $member_id , $new_status , $activate_send = true ) {
			$upd = array();
			$upd["current_status"]=sprintf( "'%s'" , FNTH_db_addslashes( $new_status ) );
			$upd["status_senttstamp"]=($activate_send) ? "null" : "now()";
			$q = FNTH_db_generate_update_query( "members" , $upd , sprintf( "member_id=%d" , $member_id ) );
			FNTH_db_free_result( FNTH_db_query( $q ) );
		}
		
		public function activateMemberStatusEmail( $member_id ) {
			$upd = array();
			$upd["status_senttstamp"]="null";
			$q = FNTH_db_generate_update_query( "members" , $upd , sprintf( "member_id=%d" , $member_id ) );
			FNTH_db_free_result( FNTH_db_query( $q ) );
		}

		public function deactivateMemberStatusEmail( $member_id ) {
			$upd = array();
			$upd["status_senttstamp"]="now()";
			$q = FNTH_db_generate_update_query( "members" , $upd , sprintf( "member_id=%d" , $member_id ) );
			FNTH_db_free_result( FNTH_db_query( $q ) );
		}

		public function prolongMember( $member_id , $prolong_days , $prolong_type , $extra_params = array() ) {
			$member = $this->getMemberFromMemberId( $member_id );

			if (!isset( $member["expires_tstamp"] ))
				$member["expires_tstamp"]=fo3_date( "Y-m-d H:i:s" , fo3_time()-3600 );
			// calculate expire
			$expires_tstamp = substr( $member["expires_tstamp"] , 0 ,19 );
			$next_day = fo3_time();	//  + 86399;
			$next_day = fo3_date( "Y-m-d 23:59:59" , $next_day );
			if ($expires_tstamp<$next_day) $expires_tstamp=$next_day;
			$et = FNTH_convert_YmdHis_to_unix( $expires_tstamp );
			$et+=$prolong_days*86400;
			$expires_tstamp = fo3_date( "Y-m-d H:i:s" , $et );
			// calculate paid_tstamp
			$paid_tstamp=fo3_date( "Y-m-d H:i:s" );
				
			// update member
			$upd = array();
			$upd["expires_tstamp"]=sprintf( "'%s'" , FNTH_db_addslashes( $expires_tstamp ) );
			$upd["paid_tstamp"]=sprintf( "'%s'", FNTH_db_addslashes( $paid_tstamp ) );
			$upd["paid_method"]=sprintf( "'%s'" , FNTH_db_addslashes( $prolong_type ) );

			$comment = trim( $member["npublic_comment"] );
			switch ($prolong_type) {
			case PM_COUPON:
				$comment.=sprintf( "\n%s kupont váltott be (%d nap)",
					str_replace( "-" , "." , fo3_date( "Y-m-d. H:i:s" , fo3_time() ) ),
					$prolong_days );
				break;
			case PM_CREDIT_CARD:
				$comment.=sprintf( "\n%s bankkártyával fizetett (#%d - %d %s)",
					str_replace( "-" , "." , fo3_date( "Y-m-d. H:i:s" , fo3_time() ) ),
					$extra_params["transaction_id"],
					$extra_params["amount"],
					$extra_params["currency"] );
				break;
			}
			$upd["npublic_comment"]=sprintf( "'%s'" , FNTH_db_addslashes( $comment ) );
			$query = FNTH_db_generate_update_query( "members" , $upd , sprintf( "member_id=%d" , $member["member_id"] ) );
			FNTH_db_free_result( FNTH_db_query( $query ) );
			$this->updateMemberStatus( $member["member_id"] , STATUS_ACTIVE );
			$this->setMemberDirty( $member_id , true );

			$mmm = new MyMemberModel();
			if (!$mmm->isMemberLoggedin()) return;
			$mmm->refreshLoggedInMember();
		}

		public function getMemberProlongPrice( $member_id , $subscription_length_in_months = 6 , $currency = "huf" ) {
			require_once( $GLOBALS["site_root"]."/includes/models/SystemParameters.inc.php" );
			$member = $this->getMemberFromMemberId( $member_id );
			$active_subscriber = $this->isMemberActiveSubscriber( $member_id , $member );
			$prolong_prefix = ($active_subscriber) ? "prolong_" : "";
			$utype = $member["member_type"];

			switch ($currency) {
			default:
				$currency = "huf";
				break;
			case "huf":
			case "eur":
				break;
			}

			switch ($subscription_length_in_months) {
			default:
				$subscription_length_in_months = 6;
				break;
			case 6:
			case 12:
				break;
			}
			$sname = sprintf( "%s%s_%dm_%s_price",
				$prolong_prefix,
				$utype,
				$subscription_length_in_months,
				$currency );
			$retval = SystemParametersModel::getVar( $sname );
			return $retval;
		}

		public function isMemberExistsOnEmail( $email ) {
			$query = sprintf( "select count(*) as num from users where email='%s' and not is_deleted and user_id in (select user_id from members where not is_deleted)" , FNTH_db_addslashes( $email ) );
			return (FNTH_db_get_field( $query , "num" )>0);
		}

		public function deleteForditMail( $member_id ) {
			$member = $this->getMemberFromMemberId( $member_id );
			require_once( $GLOBALS["site_root"]."/includes/models/user_management.v2.inc.php" );
			$username = $member["member_username"];
			fo_um_del_user_unix( $username );
			fo_um_del_user_mailbox( $username );
			fo_um_del_user_mailrecord( $username );

			$upd = array();
			$upd["use_email"]="'own'";
			$upd["is_forditmail"]="false";
			$query = FNTH_db_generate_update_query( "members" , $upd , sprintf( "member_id=%d" , $member_id ) );
			FNTH_db_free_result( FNTH_db_query( $query ) );
		}
	}
?>
