From 2d47a6d71c0973598bf2d59c59bd4050e77f3861 Mon Sep 17 00:00:00 2001 From: Jean-Philippe Lang Date: Sat, 27 Jan 2007 15:01:19 +0000 Subject: [PATCH] mail notifications added when: * a document is added * a file is added to the project * an attachment is added to an issue or a document git-svn-id: http://redmine.rubyforge.org/svn/trunk@196 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- app/controllers/documents_controller.rb | 10 +++++-- app/controllers/issues_controller.rb | 11 ++++--- app/controllers/projects_controller.rb | 9 ++++-- app/models/mailer.rb | 30 +++++++++++++++++++ app/views/mailer/attachments_add_de.rhtml | 6 ++++ app/views/mailer/attachments_add_en.rhtml | 6 ++++ app/views/mailer/attachments_add_es.rhtml | 6 ++++ app/views/mailer/attachments_add_fr.rhtml | 6 ++++ app/views/mailer/document_add_de.rhtml | 4 +++ app/views/mailer/document_add_en.rhtml | 4 +++ app/views/mailer/document_add_es.rhtml | 4 +++ app/views/mailer/document_add_fr.rhtml | 4 +++ .../018_set_doc_and_files_notifications.rb | 15 ++++++++++ 13 files changed, 106 insertions(+), 9 deletions(-) create mode 100644 app/views/mailer/attachments_add_de.rhtml create mode 100644 app/views/mailer/attachments_add_en.rhtml create mode 100644 app/views/mailer/attachments_add_es.rhtml create mode 100644 app/views/mailer/attachments_add_fr.rhtml create mode 100644 app/views/mailer/document_add_de.rhtml create mode 100644 app/views/mailer/document_add_en.rhtml create mode 100644 app/views/mailer/document_add_es.rhtml create mode 100644 app/views/mailer/document_add_fr.rhtml create mode 100644 db/migrate/018_set_doc_and_files_notifications.rb diff --git a/app/controllers/documents_controller.rb b/app/controllers/documents_controller.rb index 5ff3583d9..fe0f2d348 100644 --- a/app/controllers/documents_controller.rb +++ b/app/controllers/documents_controller.rb @@ -1,5 +1,5 @@ # redMine - project management software -# Copyright (C) 2006 Jean-Philippe Lang +# Copyright (C) 2006-2007 Jean-Philippe Lang # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License @@ -46,9 +46,13 @@ class DocumentsController < ApplicationController def add_attachment # Save the attachments - params[:attachments].each { |a| - Attachment.create(:container => @document, :file => a, :author => logged_in_user) unless a.size == 0 + @attachments = [] + params[:attachments].each { |file| + next unless file.size > 0 + a = Attachment.create(:container => @document, :file => file, :author => logged_in_user) + @attachments << a unless a.new_record? } if params[:attachments] and params[:attachments].is_a? Array + Mailer.deliver_attachments_add(@attachments) if !@attachments.empty? and Permission.find_by_controller_and_action(params[:controller], params[:action]).mail_enabled? redirect_to :action => 'show', :id => @document end diff --git a/app/controllers/issues_controller.rb b/app/controllers/issues_controller.rb index d37333e37..6ac8bc79b 100644 --- a/app/controllers/issues_controller.rb +++ b/app/controllers/issues_controller.rb @@ -1,5 +1,5 @@ # redMine - project management software -# Copyright (C) 2006 Jean-Philippe Lang +# Copyright (C) 2006-2007 Jean-Philippe Lang # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License @@ -115,10 +115,13 @@ class IssuesController < ApplicationController def add_attachment # Save the attachments - params[:attachments].each { |a| - @attachment = @issue.attachments.build(:file => a, :author => self.logged_in_user) unless a.size == 0 - @attachment.save + @attachments = [] + params[:attachments].each { |file| + next unless file.size > 0 + a = Attachment.create(:container => @issue, :file => file, :author => logged_in_user) + @attachments << a unless a.new_record? } if params[:attachments] and params[:attachments].is_a? Array + Mailer.deliver_attachments_add(@attachments) if !@attachments.empty? and Permission.find_by_controller_and_action(params[:controller], params[:action]).mail_enabled? redirect_to :action => 'show', :id => @issue end diff --git a/app/controllers/projects_controller.rb b/app/controllers/projects_controller.rb index f536c39a2..e7db204ed 100644 --- a/app/controllers/projects_controller.rb +++ b/app/controllers/projects_controller.rb @@ -184,6 +184,7 @@ class ProjectsController < ApplicationController Attachment.create(:container => @document, :file => a, :author => logged_in_user) unless a.size == 0 } if params[:attachments] and params[:attachments].is_a? Array flash[:notice] = l(:notice_successful_create) + Mailer.deliver_document_add(@document) if Permission.find_by_controller_and_action(params[:controller], params[:action]).mail_enabled? redirect_to :action => 'list_documents', :id => @project end end @@ -385,9 +386,13 @@ class ProjectsController < ApplicationController if request.post? @version = @project.versions.find_by_id(params[:version_id]) # Save the attachments - params[:attachments].each { |a| - Attachment.create(:container => @version, :file => a, :author => logged_in_user) unless a.size == 0 + @attachments = [] + params[:attachments].each { |file| + next unless file.size > 0 + a = Attachment.create(:container => @version, :file => file, :author => logged_in_user) + @attachments << a unless a.new_record? } if params[:attachments] and params[:attachments].is_a? Array + Mailer.deliver_attachments_add(@attachments) if !@attachments.empty? and Permission.find_by_controller_and_action(params[:controller], params[:action]).mail_enabled? redirect_to :controller => 'projects', :action => 'list_files', :id => @project end @versions = @project.versions diff --git a/app/models/mailer.rb b/app/models/mailer.rb index ba93b5bc7..0da6f967f 100644 --- a/app/models/mailer.rb +++ b/app/models/mailer.rb @@ -36,6 +36,36 @@ class Mailer < ActionMailer::Base @body['journal']= journal end + def document_add(document) + @recipients = document.project.users.collect { |u| u.mail if u.mail_notification }.compact + @from = Setting.mail_from + @subject = "[#{document.project.name}] #{l(:label_document_new)}: #{document.title}" + @body['document'] = document + end + + def attachments_add(attachments) + container = attachments.first.container + url = "http://#{Setting.host_name}/" + added_to = "" + case container.class.to_s + when 'Version' + url << "projects/list_files/#{container.project_id}" + added_to = "#{l(:label_version)}: #{container.name}" + when 'Document' + url << "documents/show/#{container.id}" + added_to = "#{l(:label_document)}: #{container.title}" + when 'Issue' + url << "issues/show/#{container.id}" + added_to = "#{container.tracker.name} ##{container.id}: #{container.subject}" + end + @recipients = container.project.users.collect { |u| u.mail if u.mail_notification }.compact + @from = Setting.mail_from + @subject = "[#{container.project.name}] #{l(:label_attachment_new)}" + @body['attachments'] = attachments + @body['url'] = url + @body['added_to'] = added_to + end + def lost_password(token) @recipients = token.user.mail @from = Setting.mail_from diff --git a/app/views/mailer/attachments_add_de.rhtml b/app/views/mailer/attachments_add_de.rhtml new file mode 100644 index 000000000..f17af9d8e --- /dev/null +++ b/app/views/mailer/attachments_add_de.rhtml @@ -0,0 +1,6 @@ +<%= @added_to %> +<%= @attachments.size %> files(s) added. +<% @attachments.each do |attachment | %> +- <%= attachment.filename %><% end %> + +<%= @url %> \ No newline at end of file diff --git a/app/views/mailer/attachments_add_en.rhtml b/app/views/mailer/attachments_add_en.rhtml new file mode 100644 index 000000000..f17af9d8e --- /dev/null +++ b/app/views/mailer/attachments_add_en.rhtml @@ -0,0 +1,6 @@ +<%= @added_to %> +<%= @attachments.size %> files(s) added. +<% @attachments.each do |attachment | %> +- <%= attachment.filename %><% end %> + +<%= @url %> \ No newline at end of file diff --git a/app/views/mailer/attachments_add_es.rhtml b/app/views/mailer/attachments_add_es.rhtml new file mode 100644 index 000000000..f17af9d8e --- /dev/null +++ b/app/views/mailer/attachments_add_es.rhtml @@ -0,0 +1,6 @@ +<%= @added_to %> +<%= @attachments.size %> files(s) added. +<% @attachments.each do |attachment | %> +- <%= attachment.filename %><% end %> + +<%= @url %> \ No newline at end of file diff --git a/app/views/mailer/attachments_add_fr.rhtml b/app/views/mailer/attachments_add_fr.rhtml new file mode 100644 index 000000000..381b48961 --- /dev/null +++ b/app/views/mailer/attachments_add_fr.rhtml @@ -0,0 +1,6 @@ +<%= @added_to %> +<%= @attachments.size %> fichier(s) ajouté(s). +<% @attachments.each do |attachment | %> +- <%= attachment.filename %><% end %> + +<%= @url %> \ No newline at end of file diff --git a/app/views/mailer/document_add_de.rhtml b/app/views/mailer/document_add_de.rhtml new file mode 100644 index 000000000..a022829a7 --- /dev/null +++ b/app/views/mailer/document_add_de.rhtml @@ -0,0 +1,4 @@ +A document has been added to <%= @document.project.name %> (<%= @document.category.name %>): +<%= l(:field_title) %>: <%= @document.title %> + +http://<%= Setting.host_name %>/documents/show/<%= @document.id %> \ No newline at end of file diff --git a/app/views/mailer/document_add_en.rhtml b/app/views/mailer/document_add_en.rhtml new file mode 100644 index 000000000..a022829a7 --- /dev/null +++ b/app/views/mailer/document_add_en.rhtml @@ -0,0 +1,4 @@ +A document has been added to <%= @document.project.name %> (<%= @document.category.name %>): +<%= l(:field_title) %>: <%= @document.title %> + +http://<%= Setting.host_name %>/documents/show/<%= @document.id %> \ No newline at end of file diff --git a/app/views/mailer/document_add_es.rhtml b/app/views/mailer/document_add_es.rhtml new file mode 100644 index 000000000..a022829a7 --- /dev/null +++ b/app/views/mailer/document_add_es.rhtml @@ -0,0 +1,4 @@ +A document has been added to <%= @document.project.name %> (<%= @document.category.name %>): +<%= l(:field_title) %>: <%= @document.title %> + +http://<%= Setting.host_name %>/documents/show/<%= @document.id %> \ No newline at end of file diff --git a/app/views/mailer/document_add_fr.rhtml b/app/views/mailer/document_add_fr.rhtml new file mode 100644 index 000000000..c116c84cb --- /dev/null +++ b/app/views/mailer/document_add_fr.rhtml @@ -0,0 +1,4 @@ +Un document a été ajouté à <%= @document.project.name %> (<%= @document.category.name %>): +<%= l(:field_title) %>: <%= @document.title %> + +http://<%= Setting.host_name %>/documents/show/<%= @document.id %> \ No newline at end of file diff --git a/db/migrate/018_set_doc_and_files_notifications.rb b/db/migrate/018_set_doc_and_files_notifications.rb new file mode 100644 index 000000000..adbdd3b88 --- /dev/null +++ b/db/migrate/018_set_doc_and_files_notifications.rb @@ -0,0 +1,15 @@ +class SetDocAndFilesNotifications < ActiveRecord::Migration + def self.up + Permission.find_by_controller_and_action("projects", "add_file").update_attribute(:mail_option, true) + Permission.find_by_controller_and_action("projects", "add_document").update_attribute(:mail_option, true) + Permission.find_by_controller_and_action("documents", "add_attachment").update_attribute(:mail_option, true) + Permission.find_by_controller_and_action("issues", "add_attachment").update_attribute(:mail_option, true) + end + + def self.down + Permission.find_by_controller_and_action("projects", "add_file").update_attribute(:mail_option, false) + Permission.find_by_controller_and_action("projects", "add_document").update_attribute(:mail_option, false) + Permission.find_by_controller_and_action("documents", "add_attachment").update_attribute(:mail_option, false) + Permission.find_by_controller_and_action("issues", "add_attachment").update_attribute(:mail_option, false) + end +end