From a33340c4fc5303a774b1ad52f6918cb4d78bd2d0 Mon Sep 17 00:00:00 2001 From: "vit.jonas@gmail.com" Date: Tue, 16 Aug 2011 12:18:36 +0000 Subject: [PATCH] * finished Issue 134: Create possibility to copy file even to different project git-svn-id: http://redmine-dmsf.googlecode.com/svn/trunk/redmine_dmsf@220 5e329b0b-a2ee-ea63-e329-299493fc886d --- app/controllers/dmsf_files_controller.rb | 1 + app/controllers/dmsf_files_copy_controller.rb | 129 ++++++++++++++++++ app/models/dmsf_file.rb | 11 ++ app/views/dmsf_files/show.html.erb | 12 ++ app/views/dmsf_files_copy/new.html.erb | 47 +++++++ config/locales/cs.yml | 10 +- config/locales/de.yml | 9 +- config/locales/en-GB.yml | 7 + config/locales/en-IS.yml | 7 + config/locales/en.yml | 7 + config/locales/es.yml | 7 + config/locales/fr.yml | 7 + config/locales/ja.yml | 9 ++ config/locales/ru.yml | 11 +- config/locales/zh.yml | 9 ++ init.rb | 4 +- 16 files changed, 282 insertions(+), 5 deletions(-) create mode 100644 app/controllers/dmsf_files_copy_controller.rb create mode 100644 app/views/dmsf_files_copy/new.html.erb diff --git a/app/controllers/dmsf_files_controller.rb b/app/controllers/dmsf_files_controller.rb index 1175a7f5..1a190a59 100644 --- a/app/controllers/dmsf_files_controller.rb +++ b/app/controllers/dmsf_files_controller.rb @@ -219,6 +219,7 @@ class DmsfFilesController < ApplicationController def find_file @file = DmsfFile.find(params[:id]) @project = @file.project + rescue end def find_revision diff --git a/app/controllers/dmsf_files_copy_controller.rb b/app/controllers/dmsf_files_copy_controller.rb new file mode 100644 index 00000000..3d33485d --- /dev/null +++ b/app/controllers/dmsf_files_copy_controller.rb @@ -0,0 +1,129 @@ +# Redmine plugin for Document Management System "Features" +# +# Copyright (C) 2011 Vít Jonáš +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + +class DmsfFilesCopyController < ApplicationController + unloadable + + menu_item :dmsf + + before_filter :find_file + before_filter :authorize + + def new + @target_project = DmsfFile.allowed_target_projects_on_copy.detect {|p| p.id.to_s == params[:target_project_id]} if params[:target_project_id] + @target_project ||= @project if User.current.allowed_to?(:file_manipulation, @project) + if DmsfFile.allowed_target_projects_on_copy.blank? + flash.now[:warning] = l(:warning_no_project_to_copy_file_to) + else + @target_project ||= DmsfFile.allowed_target_projects_on_copy[0] + end + + @target_folder = DmsfFolder.find(params[:target_folder_id]) unless params[:target_folder_id].blank? + @target_folder ||= @file.folder if @target_project == @project + + render :layout => !request.xhr? + end + + def create + if request.post? + @target_project = DmsfFile.allowed_target_projects_on_copy.detect {|p| p.id.to_s == params[:target_project_id]} if params[:target_project_id] + unless @target_project + render_403 + return + end + @target_folder = DmsfFolder.find(params[:target_folder_id]) unless params[:target_folder_id].blank? + if !@target_folder.nil? && @target_folder.project != @target_project + raise DmsfAccessError, l(:error_entry_project_does_not_match_current_project) + end + + name = @file.name + + file = DmsfFile.find_file_by_name(@target_project, @target_folder, name) + if file.nil? + file = DmsfFile.new + file.project = @target_project + file.name = name + file.folder = @target_folder + file.notification = !Setting.plugin_redmine_dmsf["dmsf_default_notifications"].blank? + else + if file.locked_for_user? + flash[:error] = l(:error_file_is_locked) + redirect_to :action => "new", :id => @file, :target_project_id => @target_project, :target_folder_id => @target_folder + return + end + end + + last_revision = @file.last_revision + + new_revision = DmsfFileRevision.new + new_revision.folder = @target_folder + new_revision.file = file + new_revision.user = User.current + new_revision.name = name + new_revision.title = @file.title + new_revision.description = @file.description + new_revision.comment = l(:comment_copied_from, :source => "#{@project.identifier}:#{@file.dmsf_path_str}") + new_revision.source_revision = last_revision + new_revision.major_version = last_revision.major_version + new_revision.minor_version = last_revision.minor_version + new_revision.workflow = last_revision.workflow + new_revision.mime_type = last_revision.mime_type + new_revision.size = last_revision.size + new_revision.disk_filename = last_revision.disk_filename + + if file.save && new_revision.save + file.reload + else + flash[:error] = error_messages_for(file) + redirect_to :action => "new", :id => @file, :target_project_id => @target_project, :target_folder_id => @target_folder + return + end + + flash[:notice] = l(:notice_file_copied) + log_activity(file, "was copied (is copy)") + + if params[:move] + if User.current.allowed_to?(:file_manipulation, @project) + if @file.delete + flash[:notice] = l(:notice_file_moved) + log_activity(@file, "was deleted") + DmsfMailer.deliver_files_deleted(User.current, [@file]) + else + flash[:error] = l(:error_file_is_locked) + end + else + flash[:error] = l(:error_user_has_not_right_delete_file) + end + end + + redirect_to :controller => "dmsf_files", :action => "show", :id => file + end + end + + private + + def log_activity(file, action) + Rails.logger.info "#{Time.now.strftime('%Y-%m-%d %H:%M:%S')} #{User.current.login}@#{request.remote_ip}/#{request.env['HTTP_X_FORWARDED_FOR']}: #{action} dmsf://#{file.project.identifier}/#{file.id}/#{file.last_revision.id}" + end + + def find_file + @file = DmsfFile.find(params[:id]) + @project = @file.project + end + +end diff --git a/app/models/dmsf_file.rb b/app/models/dmsf_file.rb index da95146d..292b10dd 100644 --- a/app/models/dmsf_file.rb +++ b/app/models/dmsf_file.rb @@ -184,6 +184,17 @@ class DmsfFile < ActiveRecord::Base #end end + # Returns an array of projects that current user can copy file to + def self.allowed_target_projects_on_copy + projects = [] + if User.current.admin? + projects = Project.visible.all + elsif User.current.logged? + User.current.memberships.each {|m| projects << m.project if m.roles.detect {|r| r.allowed_to?(:file_manipulation)}} + end + projects + end + # To fullfill searchable module expectations def self.search(tokens, projects=nil, options={}) tokens = [] << tokens unless tokens.is_a?(Array) diff --git a/app/views/dmsf_files/show.html.erb b/app/views/dmsf_files/show.html.erb index d2454158..61912280 100644 --- a/app/views/dmsf_files/show.html.erb +++ b/app/views/dmsf_files/show.html.erb @@ -31,6 +31,8 @@ <% end %> <% end %> <% end %> +   + <%= link_to(image_tag("copy.png"), {:controller => :dmsf_files_copy, :action => "new", :id => @file }, :title => l(:title_copy_or_move)) %> <% path = @file.folder.nil? ? [] : @file.folder.dmsf_path %> @@ -172,5 +174,15 @@ sUrl = "jquery.dataTables/#{I18n.locale.to_s.downcase}.json" if I18n.locale && ! <%= javascript_include_tag "jquery.dataTables/jquery.dataTables.min.js", :plugin => "redmine_dmsf" %> <% end %> \ No newline at end of file diff --git a/app/views/dmsf_files_copy/new.html.erb b/app/views/dmsf_files_copy/new.html.erb new file mode 100644 index 00000000..cbd5079b --- /dev/null +++ b/app/views/dmsf_files_copy/new.html.erb @@ -0,0 +1,47 @@ +<% html_title(l(:dmsf)) %> + +
+
+ +<% path = @file.folder.nil? ? [] : @file.folder.dmsf_path %> +

+<%= render(:partial => "/dmsf/path", :locals => {:path => path}) %> +/ +<%= link_to(h(@file.title), {:controller => "dmsf_files", :action => "show", :id=> @file}) %> +<%= image_tag("notify.png", :plugin => "redmine_dmsf", :title => l(:title_notifications_active)) if @file.notification %> +

+ +<% unless DmsfFile.allowed_target_projects_on_copy.blank? + form_tag({:action => "create", :id => @file}, :id => "copyForm") do |f| %> +
+ <%= error_messages_for("file") %> +

+ + <%= select_tag("target_project_id", + project_tree_options_for_select(DmsfFile.allowed_target_projects_on_copy, :selected => @target_project), + :onchange => remote_function(:url => { :action => 'new' }, + :method => :get, + :update => 'content', + :with => "Form.serialize('copyForm')")) %> +

+

+ <%= label_tag("target_folder_id", l(:label_target_folder) + ":") %> + <%= select_tag("target_folder_id", + options_for_select(DmsfFolder.directory_tree(@target_project), + :selected => (@target_folder.id unless @target_folder.nil?))) %> +

+
+ + <%= submit_tag(l(:button_copy)) %> + <%= submit_tag(l(:button_move), :name => 'move') if User.current.allowed_to?(:file_manipulation, @project) %> + <% end %> +<% end %> + +<% content_for :header_tags do %> + <%= stylesheet_link_tag "dmsf", :plugin => "redmine_dmsf" %> + <%= robot_exclusion_tag %> + <%= javascript_include_tag "jquery-1.6.1.min.js", :plugin => "redmine_dmsf" %> + +<% end %> diff --git a/config/locales/cs.yml b/config/locales/cs.yml index 9a3c0917..83fe9d89 100644 --- a/config/locales/cs.yml +++ b/config/locales/cs.yml @@ -165,4 +165,12 @@ cs: :heading_access_last: "Poslední" :label_dmsf_updated: "DMSF změněno" :title_total_size_of_all_files: "Celková velikost všech souborů v adresáři" - :project_module_dmsf: "DMSF" \ No newline at end of file + :project_module_dmsf: "DMSF" + :warning_no_project_to_copy_file_to: "Neexistuje projekt, do kterého můžete kopírovat" + :comment_copied_from: "Zkopírováno z %{source}" + :notice_file_copied: "Soubor zkopírován" + :notice_file_moved: "Soubor přesunut" + :label_target_project: "Cílový projekt" + :label_target_folder: "Cílový adresář" + :title_copy_or_move: "Kopírovat/Přesunout" + \ No newline at end of file diff --git a/config/locales/de.yml b/config/locales/de.yml index 86950112..5e89a4e7 100644 --- a/config/locales/de.yml +++ b/config/locales/de.yml @@ -167,4 +167,11 @@ de: #Not translated :title_total_size_of_all_files: "Total size of all files under this folder" :project_module_dmsf: "DMSF" - \ No newline at end of file + :warning_no_project_to_copy_file_to: "No project to copy file to" + :comment_copied_from: "Copied from %{source}" + :notice_file_copied: "File copied" + :notice_file_moved: "File moved" + :label_target_project: "Target project" + :label_target_folder: "Target folder" + :title_copy_or_move: "Copy/Move" + \ No newline at end of file diff --git a/config/locales/en-GB.yml b/config/locales/en-GB.yml index fa37fb20..611e4756 100644 --- a/config/locales/en-GB.yml +++ b/config/locales/en-GB.yml @@ -162,4 +162,11 @@ en-GB: :label_dmsf_updated: "DMSF updated" :title_total_size_of_all_files: "Total size of all files under this folder" :project_module_dmsf: "DMSF" + :warning_no_project_to_copy_file_to: "No project to copy file to" + :comment_copied_from: "Copied from %{source}" + :notice_file_copied: "File copied" + :notice_file_moved: "File moved" + :label_target_project: "Target project" + :label_target_folder: "Target folder" + :title_copy_or_move: "Copy/Move" \ No newline at end of file diff --git a/config/locales/en-IS.yml b/config/locales/en-IS.yml index fa37fb20..611e4756 100644 --- a/config/locales/en-IS.yml +++ b/config/locales/en-IS.yml @@ -162,4 +162,11 @@ en-GB: :label_dmsf_updated: "DMSF updated" :title_total_size_of_all_files: "Total size of all files under this folder" :project_module_dmsf: "DMSF" + :warning_no_project_to_copy_file_to: "No project to copy file to" + :comment_copied_from: "Copied from %{source}" + :notice_file_copied: "File copied" + :notice_file_moved: "File moved" + :label_target_project: "Target project" + :label_target_folder: "Target folder" + :title_copy_or_move: "Copy/Move" \ No newline at end of file diff --git a/config/locales/en.yml b/config/locales/en.yml index 4417e02b..e456c34b 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -166,4 +166,11 @@ en: :label_dmsf_updated: "DMSF updated" :title_total_size_of_all_files: "Total size of all files under this folder" :project_module_dmsf: "DMSF" + :warning_no_project_to_copy_file_to: "No project to copy file to" + :comment_copied_from: "Copied from %{source}" + :notice_file_copied: "File copied" + :notice_file_moved: "File moved" + :label_target_project: "Target project" + :label_target_folder: "Target folder" + :title_copy_or_move: "Copy/Move" \ No newline at end of file diff --git a/config/locales/es.yml b/config/locales/es.yml index cb3a2395..01d57928 100644 --- a/config/locales/es.yml +++ b/config/locales/es.yml @@ -164,4 +164,11 @@ es: :label_dmsf_updated: "DMSF updated" :title_total_size_of_all_files: "Total size of all files under this folder" :project_module_dmsf: "DMSF" + :warning_no_project_to_copy_file_to: "No project to copy file to" + :comment_copied_from: "Copied from %{source}" + :notice_file_copied: "File copied" + :notice_file_moved: "File moved" + :label_target_project: "Target project" + :label_target_folder: "Target folder" + :title_copy_or_move: "Copy/Move" \ No newline at end of file diff --git a/config/locales/fr.yml b/config/locales/fr.yml index 86bdcf9c..6af8bfab 100644 --- a/config/locales/fr.yml +++ b/config/locales/fr.yml @@ -162,4 +162,11 @@ fr: :label_dmsf_updated: "DMSF updated" :title_total_size_of_all_files: "Total size of all files under this folder" :project_module_dmsf: "DMSF" + :warning_no_project_to_copy_file_to: "No project to copy file to" + :comment_copied_from: "Copied from %{source}" + :notice_file_copied: "File copied" + :notice_file_moved: "File moved" + :label_target_project: "Target project" + :label_target_folder: "Target folder" + :title_copy_or_move: "Copy/Move" \ No newline at end of file diff --git a/config/locales/ja.yml b/config/locales/ja.yml index d7ffe71e..84efb7b7 100644 --- a/config/locales/ja.yml +++ b/config/locales/ja.yml @@ -165,4 +165,13 @@ ja: :heading_access_last: "最終アクセス" :title_total_size_of_all_files: "このフォルダにある全ファイルの合計サイズ" :project_module_dmsf: "DMSF" + +# Not translated + :warning_no_project_to_copy_file_to: "No project to copy file to" + :comment_copied_from: "Copied from %{source}" + :notice_file_copied: "File copied" + :notice_file_moved: "File moved" + :label_target_project: "Target project" + :label_target_folder: "Target folder" + :title_copy_or_move: "Copy/Move" \ No newline at end of file diff --git a/config/locales/ru.yml b/config/locales/ru.yml index f1541529..d4a34a2a 100644 --- a/config/locales/ru.yml +++ b/config/locales/ru.yml @@ -166,4 +166,13 @@ ru: :label_dmsf_updated: "Модуль Документооборота обновлен" :title_total_size_of_all_files: "Суммарный объем файлов в каталоге" :project_module_dmsf: "Документооборот" - \ No newline at end of file + +# Not translated + :warning_no_project_to_copy_file_to: "No project to copy file to" + :comment_copied_from: "Copied from %{source}" + :notice_file_copied: "File copied" + :notice_file_moved: "File moved" + :label_target_project: "Target project" + :label_target_folder: "Target folder" + :title_copy_or_move: "Copy/Move" + \ No newline at end of file diff --git a/config/locales/zh.yml b/config/locales/zh.yml index 06e88127..1f8e5d91 100644 --- a/config/locales/zh.yml +++ b/config/locales/zh.yml @@ -167,4 +167,13 @@ zh: :label_dmsf_updated: "DMSF updated" :title_total_size_of_all_files: "文件夹所有文件总大小" :project_module_dmsf: "文档管家" + +# Not translated + :warning_no_project_to_copy_file_to: "No project to copy file to" + :comment_copied_from: "Copied from %{source}" + :notice_file_copied: "File copied" + :notice_file_moved: "File moved" + :label_target_project: "Target project" + :label_target_folder: "Target folder" + :title_copy_or_move: "Copy/Move" \ No newline at end of file diff --git a/init.rb b/init.rb index 17b00782..943b8e8e 100644 --- a/init.rb +++ b/init.rb @@ -29,7 +29,7 @@ Redmine::Plugin.register :redmine_dmsf do name "DMSF" author "Vít Jonáš" description "Document Management System Features" - version "1.0.0" + version "1.1.0 devel" url "http://code.google.com/p/redmine-dmsf/" author_url "mailto:vit.jonas@gmail.com" @@ -55,7 +55,7 @@ Redmine::Plugin.register :redmine_dmsf do permission :view_dmsf_folders, {:dmsf => [:show]} permission :user_preferences, {:dmsf_state => [:user_pref_save]} permission :view_dmsf_files, {:dmsf => [:entries_operation, :entries_email], - :dmsf_files => [:show]} + :dmsf_files => [:show], :dmsf_files_copy => [:new, :create]} permission :folder_manipulation, {:dmsf => [:new, :create, :delete, :edit, :save, :edit_root, :save_root]} permission :file_manipulation, {:dmsf_files => [:create_revision, :delete, :lock, :unlock], :dmsf_upload => [:upload_files, :upload_file, :commit_files]}