<?php
	class MailtemplatesModel {
		public function getMailTemplateTypes( $choose = true ) {
			$retval = array();
			if ($choose)
				$retval[""]=__("Choose" );
			$query = sprintf( "select mail_template_type_id,mail_template_type_name from mail_template_types" );
			$data = FNTH_db_get_resultset( $query );
			foreach ($data as $row)
				$retval[$row["mail_template_type_id"]]=__($row["mail_template_type_name"]);
			return $retval;
		}

		public function getMailTemplateFromId( $id ) {
			$query = sprintf( "select * from mail_templates where mail_template_id = %d" , $id );
			return FNTH_db_get_row( $query );
		}

		protected function prepareMailTemplate( $datas ) {
			$retval = array();
			$retval["site_language_id"]=(isset( $datas["site_language_id"] )) ? sprintf( "%d" , $datas["site_language_id"] ) : 1;
			$retval["mail_template_type_id"]=(isset( $datas["mail_template_type_id"] )) ? sprintf( "%d" , $datas["mail_template_type_id"] ) : 1;
			$is_html = (isset( $datas["template_is_html"] )) ? $datas["template_is_html"] : true;
			$retval["template_is_html"]=($is_html) ? "true" : "false";
			$is_raw_html = (isset( $datas["template_is_raw_html"] )) ? $datas["template_is_raw_html"] : true;
			$retval["template_is_raw_html"]=($is_raw_html) ? "true" : "false";
			$retval["template_name"]=sprintf( "'%s'" , FNTH_db_addslashes( $datas["template_name"] ) );
			$retval["template_title"]=sprintf( "'%s'" , FNTH_db_addslashes( $datas["template_title"] ) );
			$retval["template_subject"]=sprintf( "'%s'" , FNTH_db_addslashes( $datas["template_subject"] ) );
			$retval["template_head"]=(isset( $datas["template_head"] ) && !empty( $datas["template_head"] )) ?
				sprintf( "'%s'" , FNTH_db_addslashes( $datas["template_head"] ) ) : "null";
			$retval["template_content"]=sprintf( "'%s'" , FNTH_db_addslashes( $datas["template_content"] ) );
			return $retval;
		}

		public function addMailTemplate( $datas ) {
			$ins = $this->prepareMailTemplate( $datas );
			FNTH_db_free_result( FNTH_db_query(
				FNTH_db_generate_insert_query( "mail_templates" , $ins  ) ) );
		}

		public function updateMailTemplate( $uid = 0 , $datas = array() ) {
			$upd = $this->prepareMailTemplate( $datas );
			FNTH_db_free_result( FNTH_db_query(
				FNTH_db_generate_update_query( "mail_templates" , $upd ,
					sprintf( "mail_template_id=%d" , $uid ) ) ) );
		}

		public function deleteMailTemplate( $mail_template_id ) {
			FNTH_db_free_result( FNTH_db_query(
				sprintf( "delete from mail_templates where mail_template_id=%d" , $mail_template_id ) ) );
		}

		public function getMailTemplateOptions( $choose = true , $title_also = true ) {
			$retval = array();
			$query = sprintf( "select mail_template_id,template_name,template_title from mail_templates order by template_name" );
			$ds = FNTH_db_get_resultset( $query );
			if ($choose) $retval[""]=__("Choose","attr");
			foreach ($ds as $row) {
				$item = ($title_also) ? sprintf( "%s (%s)",$row["template_name"],$row["template_title"] ) :
					$row["template_name"];
				$retval[$row["mail_template_id"]]=$item;
			}
			return $retval;
		}
	}
?>
