From 79a3216d402cee483e56ca2283cac1b504f99514 Mon Sep 17 00:00:00 2001 From: Karel Picman Date: Wed, 26 Feb 2014 10:17:56 +0100 Subject: [PATCH] #201 Download link by email --- app/controllers/dmsf_controller.rb | 32 +++++++++++---- app/models/dmsf_folder.rb | 6 +++ app/models/dmsf_mailer.rb | 18 +++++--- app/views/dmsf/email_entries.html.erb | 12 ++++-- app/views/dmsf_mailer/send_documents.html.erb | 41 +++++++++++++++++-- app/views/dmsf_mailer/send_documents.text.erb | 32 ++++++++++++++- config/locales/cs.yml | 1 + config/locales/de.yml | 1 + config/locales/en.yml | 1 + config/locales/es.yml | 1 + config/locales/fr.yml | 1 + config/locales/ja.yml | 1 + config/locales/ru.yml | 1 + config/locales/sl.yml | 1 + config/locales/zh.yml | 1 + config/routes.rb | 1 + init.rb | 2 +- 17 files changed, 129 insertions(+), 24 deletions(-) diff --git a/app/controllers/dmsf_controller.rb b/app/controllers/dmsf_controller.rb index db00e878..6c147003 100644 --- a/app/controllers/dmsf_controller.rb +++ b/app/controllers/dmsf_controller.rb @@ -63,6 +63,16 @@ class DmsfController < ApplicationController @ajax_upload_size = Setting.plugin_redmine_dmsf['dmsf_max_ajax_upload_filesize'].present? ? Setting.plugin_redmine_dmsf['dmsf_max_ajax_upload_filesize'] : 100 end + + def download_email_entries + send_file( + params[:path], + :filename => 'Documents.zip', + :type => 'application/zip', + :disposition => 'attachment') + rescue Exception => e + flash[:error] = e.message + end def entries_operation selected_folders = params[:subfolders].present? ? params[:subfolders] : [] @@ -110,16 +120,16 @@ class DmsfController < ApplicationController def entries_email @email_params = params[:email] - if @email_params['to'].strip.blank? + if @email_params[:to].strip.blank? flash.now[:error] = l(:error_email_to_must_be_entered) render :action => 'email_entries' return - end - DmsfMailer.send_documents(@project, User.current, @email_params['to'], @email_params['cc'], - @email_params['subject'], @email_params['zipped_content'], @email_params['body']).deliver + end + DmsfMailer.send_documents(@project, User.current, @email_params).deliver File.delete(@email_params['zipped_content']) flash[:notice] = l(:notice_email_sent, @email_params['to']) - redirect_to({:controller => 'dmsf', :action => 'show', :id => @project, :folder_id => @folder}) + + redirect_to dmsf_folder_path(:id => @project, :folder_id => @folder) end def delete_entries @@ -342,9 +352,9 @@ class DmsfController < ApplicationController zip = DmsfZip.new zip_entries(zip, selected_folders, selected_files) - ziped_content = "#{DmsfHelper.temp_dir}/#{DmsfHelper.temp_filename('dmsf_email_sent_documents.zip')}"; + zipped_content = "#{DmsfHelper.temp_dir}/#{DmsfHelper.temp_filename('dmsf_email_sent_documents.zip')}"; - File.open(ziped_content, 'wb') do |f| + File.open(zipped_content, 'wb') do |f| zip_file = File.open(zip.finish, 'rb') while (buffer = zip_file.read(8192)) f.write(buffer) @@ -352,7 +362,7 @@ class DmsfController < ApplicationController end max_filesize = Setting.plugin_redmine_dmsf['dmsf_max_email_filesize'].to_f - if max_filesize > 0 && File.size(ziped_content) > max_filesize * 1048576 + if max_filesize > 0 && File.size(zipped_content) > max_filesize * 1048576 raise EmailMaxFileSize end @@ -363,7 +373,11 @@ class DmsfController < ApplicationController audit.save! end - @email_params = {'zipped_content' => ziped_content} + @email_params = { + :zipped_content => zipped_content, + :folders => selected_folders, + :files => selected_files + } render :action => 'email_entries' rescue Exception => e flash[:error] = e.message diff --git a/app/models/dmsf_folder.rb b/app/models/dmsf_folder.rb index df63f559..57ec57ab 100644 --- a/app/models/dmsf_folder.rb +++ b/app/models/dmsf_folder.rb @@ -140,6 +140,12 @@ class DmsfFolder < ActiveRecord::Base return tree end + def folder_tree + tree = [[self.title, self.id]] + DmsfFolder.directory_subtree(tree, self, 2, nil) + return tree + end + def self.file_list(files) options = Array.new options.push ['', nil] diff --git a/app/models/dmsf_mailer.rb b/app/models/dmsf_mailer.rb index 825b91d2..92c54af3 100644 --- a/app/models/dmsf_mailer.rb +++ b/app/models/dmsf_mailer.rb @@ -55,16 +55,22 @@ class DmsfMailer < Mailer :subject => l(:text_email_doc_deleted_subject, :project => project.name) end end - - def send_documents(project, user, email_to, email_cc, email_subject, zipped_content, email_plain_body) - zipped_content_data = open(zipped_content, 'rb') {|io| io.read } + + def send_documents(project, user, email_params) + zipped_content_data = open(email_params[:zipped_content], 'rb') { |io| io.read } redmine_headers 'Project' => project.identifier if project - @body = email_plain_body + @body = email_params[:body] + @links_only = email_params[:links_only] + @folders = email_params[:folders] + @files = email_params[:files] - attachments['Documents.zip'] = {:content_type => 'application/zip', :content => zipped_content_data} - mail :to => email_to, :cc => email_cc, :subject => email_subject, :from => user.mail + unless @links_only == '1' + attachments['Documents.zip'] = { :content_type => 'application/zip', :content => zipped_content_data } + end + + mail :to => email_params[:to], :cc => email_params[:cc], :subject => email_params[:subject], :from => user.mail end def workflow_notification(user, workflow, revision, subject, text1, text2) diff --git a/app/views/dmsf/email_entries.html.erb b/app/views/dmsf/email_entries.html.erb index 522f2c13..f17464a1 100644 --- a/app/views/dmsf/email_entries.html.erb +++ b/app/views/dmsf/email_entries.html.erb @@ -28,10 +28,13 @@ <%= form_tag({:action => 'entries_email', :id => @project, :folder_id => @folder}, { :method=>:post, :class => 'tabular'}) do %> + <%= hidden_field_tag('email[zipped_content]', @email_params[:zipped_content]) %> + <%= hidden_field_tag('email[folders]', @email_params[:folders].to_json) %> + <%= hidden_field_tag('email[files]', @email_params[:files].to_json) %>

<%= label_tag('', "#{l(:label_email_from)}:") %> - <%= h(User.current.mail) %> + <%= h(User.current.mail) %>

<%= label_tag('email[to]', "#{l(:label_email_to)}:") %> @@ -47,8 +50,9 @@

<%= label_tag('', "#{l(:label_email_documents)}:") %> - Documents.zip - <%= hidden_field_tag('email[zipped_content]', @email_params['zipped_content']) %> + <%= link_to 'Documents.zip', + download_email_entries_path(:id => @project, :folder_id => @folder, :path => @email_params['zipped_content']) %> + <%= l(:label_or) %> <%= check_box_tag('email[links_only]') %> <%= l(:label_links_only) %>

<%= label_tag('email[body]', "#{l(:label_email_body)}:") %> @@ -57,3 +61,5 @@

<%= submit_tag(l(:label_email_send)) %>

<% end %> + +<%= wikitoolbar_for 'email_body' %> diff --git a/app/views/dmsf_mailer/send_documents.html.erb b/app/views/dmsf_mailer/send_documents.html.erb index a9e13b42..6e0248d2 100644 --- a/app/views/dmsf_mailer/send_documents.html.erb +++ b/app/views/dmsf_mailer/send_documents.html.erb @@ -1,7 +1,8 @@ <%# Redmine plugin for Document Management System "Features" # -# Copyright (C) 2011 Vít Jonáš -# Copyright (C) 2012 Daniel Munn +# Copyright (C) 2011 Vít Jonáš +# Copyright (C) 2012 Daniel Munn +# Copyright (C) 2011-14 Karel Pičman # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License @@ -17,4 +18,38 @@ # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.%> -<%= simple_format(@body) %> +<%= textilizable(@body) %> + +<% if @links_only == '1' %> + <% if @folders.present? %> + <% JSON.parse(@folders).each do |id| %> + <% folder = DmsfFolder.find_by_id id %> + <% if folder %> + <% folder.folder_tree.each do |name, i| %> + <% dir = DmsfFolder.find_by_id i %> + <% if dir %> +
+ <%= link_to(h(dir.dmsf_path_str), dmsf_folder_path(:id => dir.project_id, :folder_id => dir.id, :only_path => false)) %> +

+ <% dir.files.each do |file| %> + <%= link_to(h(file.title), dmsf_file_path(file, :only_path => false)) %> +  (<%= link_to(h(file.name), dmsf_file_path(file, :download => '', :only_path => false)) %>) +
+ <% end %> + <% end %> + <% end %> + <% end %> + <% end %> + <% end %> + <% if @files.present? %> +
+ <% JSON.parse(@files).each do |id| %> + <% file = DmsfFile.find_by_id id %> + <% if file %> + <%= link_to(h(file.title), dmsf_file_path(file, :only_path => false)) %> +  (<%= link_to(h(file.name), dmsf_file_path(file, :download => '', :only_path => false)) %>) +
+ <% end %> + <% end %> + <% end %> +<% end %> \ No newline at end of file diff --git a/app/views/dmsf_mailer/send_documents.text.erb b/app/views/dmsf_mailer/send_documents.text.erb index 91754792..10abf892 100644 --- a/app/views/dmsf_mailer/send_documents.text.erb +++ b/app/views/dmsf_mailer/send_documents.text.erb @@ -1,7 +1,8 @@ <%# Redmine plugin for Document Management System "Features" # -# Copyright (C) 2011 Vít Jonáš -# Copyright (C) 2012 Daniel Munn +# Copyright (C) 2011 Vít Jonáš +# Copyright (C) 2012 Daniel Munn +# Copyright (C) 2011-14 Karel Pičman # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License @@ -18,3 +19,30 @@ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.%> <%= @body %> + +<% if @links_only == '1' %> + <% if @folders.present? %> + <% JSON.parse(@folders).each do |id| %> + <% folder = DmsfFolder.find_by_id id %> + <% if folder %> + <% folder.folder_tree.each do |name, i| %> + <% dir = DmsfFolder.find_by_id i %> + <% if dir %> + <%= dir.dmsf_path_str %> + <% dir.files.each do |file| %> + <%= dmsf_file_path(file, :download => '', :only_path => false) %> + <% end %> + <% end %> + <% end %> + <% end %> + <% end %> + <% end %> + <% if @files.present? %> + <% JSON.parse(@files).each do |id| %> + <% file = DmsfFile.find_by_id id %> + <% if file %> + <%= dmsf_file_path(file, :download => '', :only_path => false) %> + <% end %> + <% end %> + <% end %> +<% end %> \ No newline at end of file diff --git a/config/locales/cs.yml b/config/locales/cs.yml index dc929b33..72a59aeb 100644 --- a/config/locales/cs.yml +++ b/config/locales/cs.yml @@ -284,6 +284,7 @@ cs: text_email_doc_follows: takto text_email_doc_deleted_subject: "Dokumenty projektu %{project} smazány" text_email_doc_deleted: právě smazal dokumety projektu + label_links_only: pouze odkazy my: blocks: diff --git a/config/locales/de.yml b/config/locales/de.yml index 84b150af..5a189d54 100644 --- a/config/locales/de.yml +++ b/config/locales/de.yml @@ -279,6 +279,7 @@ de: label_notifications_off: Notifications off field_target_file: Target file title_download_entries: Download entries + label_links_only: links only my: blocks: diff --git a/config/locales/en.yml b/config/locales/en.yml index 05d51cce..00b15479 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -288,6 +288,7 @@ en: text_email_doc_follows: as follows text_email_doc_deleted_subject: "Documents of %{project} deleted" text_email_doc_deleted: has just deleted documents of + label_links_only: links only my: blocks: diff --git a/config/locales/es.yml b/config/locales/es.yml index 382fcd1f..a5545ab3 100644 --- a/config/locales/es.yml +++ b/config/locales/es.yml @@ -285,6 +285,7 @@ es: text_email_doc_follows: as follows text_email_doc_deleted_subject: "Documents of %{project} deleted" text_email_doc_deleted: has just deleted documents of + label_links_only: links only my: blocks: diff --git a/config/locales/fr.yml b/config/locales/fr.yml index 26f45b56..b6d41304 100644 --- a/config/locales/fr.yml +++ b/config/locales/fr.yml @@ -288,6 +288,7 @@ fr: text_email_doc_follows: as follows text_email_doc_deleted_subject: "Documents of %{project} deleted" text_email_doc_deleted: has just deleted documents of + label_links_only: links only my: blocks: diff --git a/config/locales/ja.yml b/config/locales/ja.yml index 158cdf37..52f445f9 100644 --- a/config/locales/ja.yml +++ b/config/locales/ja.yml @@ -285,6 +285,7 @@ ja: text_email_doc_follows: as follows text_email_doc_deleted_subject: "Documents of %{project} deleted" text_email_doc_deleted: has just deleted documents of + label_links_only: links only my: blocks: diff --git a/config/locales/ru.yml b/config/locales/ru.yml index 12c61bd0..f5243495 100644 --- a/config/locales/ru.yml +++ b/config/locales/ru.yml @@ -285,6 +285,7 @@ ru: text_email_doc_follows: as follows text_email_doc_deleted_subject: "Documents of %{project} deleted" text_email_doc_deleted: has just deleted documents of + label_links_only: links only my: blocks: diff --git a/config/locales/sl.yml b/config/locales/sl.yml index 87465bb5..37e1e287 100644 --- a/config/locales/sl.yml +++ b/config/locales/sl.yml @@ -285,6 +285,7 @@ sl: text_email_doc_follows: as follows text_email_doc_deleted_subject: "Documents of %{project} deleted" text_email_doc_deleted: has just deleted documents of + label_links_only: links only my: blocks: diff --git a/config/locales/zh.yml b/config/locales/zh.yml index d781224c..954d6048 100644 --- a/config/locales/zh.yml +++ b/config/locales/zh.yml @@ -286,6 +286,7 @@ zh: text_email_doc_follows: as follows text_email_doc_deleted_subject: "Documents of %{project} deleted" text_email_doc_deleted: has just deleted documents of + label_links_only: links only my: blocks: diff --git a/config/routes.rb b/config/routes.rb index d0b863df..25f8e33d 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -34,6 +34,7 @@ RedmineApp::Application.routes.draw do post '/projects/:id/dmsf/entries', :controller => 'dmsf', :action => 'entries_operation' post '/projects/:id/dmsf/entries/delete', :controller => 'dmsf', :action => 'delete_entries' post '/projects/:id/dmsf/entries/email', :controller => 'dmsf', :action => 'entries_email' + get '/projects/:id/dmsf/entries/download_email_entries', :controller => 'dmsf', :action => 'download_email_entries', :as => 'download_email_entries' get '/projects/:id/dmsf/lock', :controller => 'dmsf', :action => 'lock', :as => 'lock_dmsf' get '/projects/:id/dmsf/unlock', :controller => 'dmsf', :action => 'unlock', :as => 'unlock_dmsf' get '/projects/:id/dmsf/', :controller => 'dmsf', :action => 'show', :as => 'dmsf_folder' diff --git a/init.rb b/init.rb index 6e12719e..d9975a94 100644 --- a/init.rb +++ b/init.rb @@ -57,7 +57,7 @@ Redmine::Plugin.register :redmine_dmsf do permission :user_preferences, {:dmsf_state => [:user_pref_save]} permission :view_dmsf_files, - {:dmsf => [:entries_operation, :entries_email], + {:dmsf => [:entries_operation, :entries_email, :download_email_entries], :dmsf_files => [:show], :dmsf_files_copy => [:new, :create, :move], :dmsf_workflows => [:log]},