<?php
	fo3_model( "Cache" );

	class MembershipsModel {
		public static function getMembershipOptions( $choose = true , $short = true , $link = false ) {
			$mc = CacheModel::getCacheNameOnParams( "membership_options" ,
				array( "lang" => $GLOBALS["language"], "choose" => $choose , "short" => $short , "link" => $link ) );
			$retval = CacheModel::getCacheValueOnString( $mc );
			if (is_array( $retval )) return $retval;
			$retval = array();
			if ($choose) $retval[0]=__("Choose");
			$query = sprintf( "select * from memberships" );
			$data = FNTH_db_get_resultset( $query );
			foreach ($data as $row) {
				if ($link && isset( $row["membership_url"] ) && strlen( $row["membership_url"] ) ) {
					$retval[$row["membership_id"]]=
						($short) ? sprintf( "<a class=\"nrm\" href=\"%s\" target=\"_blank\">%s</a>" , $row["membership_url"] , $row["membership_name"]  ) : 
						sprintf( "<a class=\"nrm\" href=\"%s\" target=\"_blank\">%s</a>" , $row["membership_url"] , $row["membership_description"] );
				} else {
					$retval[$row["membership_id"]]=
						($short) ? $row["membership_name"] : $row["membership_description"];
				}

			}
			CacheModel::cacheValueOnString( $mc , $retval );
			return $retval;
		}

		public function getMembershipFromId( $membership_id ) {
			$query = sprintf( "select * from memberships where membership_id=%d" , $membership_id );
			return FNTH_db_get_row( $query );
		}

		public function updateMembership( $membership_id , $membership ) {
			$upd = array();
			if (isset( $membership["membership_name"] )) $upd["membership_name"]=sprintf( "'%s'" , FNTH_db_addslashes( $membership["membership_name"] ) );
			if (isset( $membership["membership_description"] )) $upd["membership_description"]=sprintf( "'%s'" , FNTH_db_addslashes( $membership["membership_description"] ) );
			if (isset( $membership["membership_url"] )) $upd["membership_url"]=sprintf( "'%s'" , FNTH_db_addslashes( $membership["membership_url"] ) );
			FNTH_db_free_result(
				FNTH_db_query(
					FNTH_db_generate_update_query( "memberships" , $upd , sprintf( "membership_id=%d" , $membership_id ) )
				)
			);
		}
		
		public function addMembership( $membership ) {
			$ins = array();
			if (isset( $membership["membership_name"] )) $ins["membership_name"]=sprintf( "'%s'" , FNTH_db_addslashes( $membership["membership_name"] ) );
			if (isset( $membership["membership_description"] )) $ins["membership_description"]=sprintf( "'%s'" , FNTH_db_addslashes( $membership["membership_description"] ) );
			if (isset( $membership["membership_url"] )) $ins["membership_url"]=sprintf( "'%s'" , FNTH_db_addslashes( $membership["membership_url"] ) );
			FNTH_db_free_result(
				FNTH_db_query(
					FNTH_db_generate_insert_query( "memberships" , $ins )
				)
			);
		}

		public function deleteMembership( $membership_id ) {
			FNTH_db_free_result( FNTH_db_query( sprintf( "delete from member_memberships where membership_id=%d" , $membership_id ) ) );
			FNTH_db_free_result( FNTH_db_query( sprintf( "delete from memberships where membership_id=%d" , $membership_id ) ) );
		}
	}

?>
