class MailTemplatesController < ApplicationController before_action :require_current_project! before_action :set_template, only: %i[edit update destroy] def index @page_title = "Template email" @mail_templates = MailTemplate.for_project(current_project) end def new @page_title = "Nuovo template" @mail_template = MailTemplate.new(project: current_project, body_html: default_html, subject: "MatchLiveTV per {{societa}}") end def create @mail_template = MailTemplate.new(template_params) @mail_template.project ||= current_project if @mail_template.save redirect_to mail_templates_path, notice: "Template salvato." else @page_title = "Nuovo template" render :new, status: :unprocessable_entity end end def edit @page_title = "Modifica template" end def update if @mail_template.update(template_params) redirect_to mail_templates_path, notice: "Template aggiornato." else @page_title = "Modifica template" render :edit, status: :unprocessable_entity end end def destroy if @mail_template.mailings.exists? || @mail_template.mailings_as_b.exists? redirect_to mail_templates_path, alert: "Template giĆ usato in un invio: non si elimina, puoi solo modificarlo." return end @mail_template.destroy! redirect_to mail_templates_path, notice: "Template eliminato." end private def set_template @mail_template = MailTemplate.for_project(current_project).find(params[:id]) end def template_params params.require(:mail_template).permit(:name, :subject, :body_html) end def default_html <<~HTML
Ciao {{contatto_nome}},
scriviamo a {{societa}} ({{regione}}) per presentarti MatchLiveTV.
Possiamo aiutarvi a trasmettere le giovanili in modo semplice.
A presto,
Il team MatchLiveTV