From cd94b958c147960cc76b76f0a2fa43f67511d64c Mon Sep 17 00:00:00 2001 From: Karel Picman Date: Wed, 19 Mar 2014 12:43:11 +0100 Subject: [PATCH] #236 Documents tagging --- app/controllers/dmsf_controller.rb | 76 +++++++++++++++++-- app/helpers/dmsf_helper.rb | 14 ++++ app/models/dmsf_file.rb | 5 ++ app/models/dmsf_file_revision_custom_field.rb | 10 +++ app/models/dmsf_folder.rb | 2 +- app/views/dmsf/show.html.erb | 12 ++- 6 files changed, 110 insertions(+), 9 deletions(-) diff --git a/app/controllers/dmsf_controller.rb b/app/controllers/dmsf_controller.rb index b705f29d..997e329b 100644 --- a/app/controllers/dmsf_controller.rb +++ b/app/controllers/dmsf_controller.rb @@ -40,17 +40,70 @@ class DmsfController < ApplicationController @workflows_available = DmsfWorkflow.where(['project_id = ? OR project_id IS NULL', @project.id]).count > 0 unless @folder - @subfolders = @project.dmsf_folders.visible - @files = @project.dmsf_files.visible + if params[:custom_field_id].present? && params[:custom_value].present? + @subfolders = [] + DmsfFolder.where(:project_id => @project.id).visible.each do |f| + f.custom_field_values.each do |v| + if v.custom_field_id == params[:custom_field_id].to_i + if v.custom_field.compare_values?(v.value, params[:custom_value]) + @subfolders << f + break + end + end + end + end + @files = [] + DmsfFile.where(:project_id => @project.id).visible.each do |f| + r = f.last_revision + if r + r.custom_field_values.each do |v| + if v.custom_field_id == params[:custom_field_id].to_i + if v.custom_field.compare_values?(v.value, params[:custom_value]) + @files << f + break + end + end + end + end + end + @dir_links = [] + DmsfLink.where(:project_id => @project.id, :target_type => DmsfFolder.model_name).visible.each do |l| + l.target_folder.custom_field_values.each do |v| + if v.custom_field_id == params[:custom_field_id].to_i + if v.custom_field.compare_values?(v.value, params[:custom_value]) + @dir_links << l + break + end + end + end + end + @file_links = [] + DmsfLink.where(:project_id => @project.id, :target_type => DmsfFile.model_name).visible.each do |l| + r = l.target_file.last_revision + if r + r.custom_field_values.each do |v| + if v.custom_field_id == params[:custom_field_id].to_i + if v.custom_field.compare_values?(v.value, params[:custom_value]) + @file_links << l + break + end + end + end + end + end + else + @subfolders = @project.dmsf_folders.visible + @files = @project.dmsf_files.visible + @dir_links = @project.folder_links.visible + @file_links = @project.file_links.visible + end @locked_for_user = false - @dir_links = @project.folder_links - @file_links = @project.file_links else @subfolders = @folder.subfolders.visible - @files = @folder.files.visible + @files = @folder.files.visible + @dir_links = @folder.folder_links.visible + @file_links = @folder.file_links.visible @locked_for_user = @folder.locked_for_user? - @dir_links = @folder.folder_links - @file_links = @folder.file_links end @files.sort! do |a,b| @@ -75,6 +128,15 @@ class DmsfController < ApplicationController end def entries_operation + # Tag filter + if params[:dmsf_folder] && params[:dmsf_folder][:custom_field_values].present? + redirect_to dmsf_folder_path( + :id => @project, + :custom_field_id => params[:dmsf_folder][:custom_field_values].first[0], + :custom_value => params[:dmsf_folder][:custom_field_values].first[1]) + return + end + # Download/Email selected_folders = params[:subfolders].present? ? params[:subfolders] : [] selected_files = params[:files].present? ? params[:files] : [] selected_dir_links = params[:dir_links] diff --git a/app/helpers/dmsf_helper.rb b/app/helpers/dmsf_helper.rb index 08f28bf0..447745c9 100644 --- a/app/helpers/dmsf_helper.rb +++ b/app/helpers/dmsf_helper.rb @@ -76,4 +76,18 @@ module DmsfHelper return obj end + # Return custom field html tag corresponding to its format + def custom_field_tag_ex(prefix, custom_value, data) + custom_value.custom_field.format.edit_tag self, + custom_field_tag_id(prefix, custom_value.custom_field), + custom_field_tag_name(prefix, custom_value.custom_field), + custom_value, + {:class => "#{custom_value.custom_field.field_format}_cf}"}.merge(data) + end + + # Return custom field tag with its label tag + def custom_field_tag_with_label_ex(name, custom_value, options={}, data={}) + custom_field_label_tag(name, custom_value, options) + custom_field_tag_ex(name, custom_value, data) + end + end diff --git a/app/models/dmsf_file.rb b/app/models/dmsf_file.rb index 8e295868..c8d925c8 100644 --- a/app/models/dmsf_file.rb +++ b/app/models/dmsf_file.rb @@ -44,6 +44,11 @@ class DmsfFile < ActiveRecord::Base :conditions => {:target_type => DmsfFile.model_name}, :dependent => :destroy scope :visible, lambda {|*args| where(DmsfFile.visible_condition(args.shift || User.current, *args)).readonly(false)} + scope :by_tag, + lambda { |id, value| { + :joins => 'JOIN custom_values ON dmsf_folders.id = custom_values.customized_id', + :conditions => ["custom_values.custom_field_id = ? AND custom_values.value = ?", id, value] + }} validates :name, :presence => true validates_format_of :name, :with => DmsfFolder.invalid_characters, diff --git a/app/models/dmsf_file_revision_custom_field.rb b/app/models/dmsf_file_revision_custom_field.rb index 9f6b564a..2793b424 100644 --- a/app/models/dmsf_file_revision_custom_field.rb +++ b/app/models/dmsf_file_revision_custom_field.rb @@ -18,7 +18,17 @@ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. class DmsfFileRevisionCustomField < CustomField + def type_name :menu_dmsf end + + def compare_values?(x, y) + if x.is_a?(Array) && y.is_a?(Array) && !y.empty? + x.include? y[0] + else + x == y + end + end + end \ No newline at end of file diff --git a/app/models/dmsf_folder.rb b/app/models/dmsf_folder.rb index 577a25f1..adfd6b20 100644 --- a/app/models/dmsf_folder.rb +++ b/app/models/dmsf_folder.rb @@ -43,7 +43,7 @@ class DmsfFolder < ActiveRecord::Base :conditions => {:entity_type => 1}, :dependent => :destroy - scope :visible, lambda {|*args| {:conditions => '' }} #For future use, however best to be referenced now + scope :visible, lambda {|*args| {:conditions => '' }} #For future use, however best to be referenced now acts_as_customizable diff --git a/app/views/dmsf/show.html.erb b/app/views/dmsf/show.html.erb index 90c7541d..957a2609 100644 --- a/app/views/dmsf/show.html.erb +++ b/app/views/dmsf/show.html.erb @@ -109,8 +109,18 @@ <%= submit_tag(l(:submit_email), :title => l(:title_send_checked_by_email), :name => 'email_entries') %> <% if @file_manipulation_allowed && @folder_manipulation_allowed && !@locked_for_user %> - <% end %> + <% end %> + <% values = @folder ? @folder.custom_field_values : @parent ? @parent.custom_field_values : DmsfFolder.new(:project => @project).custom_field_values %> + <% unless values.empty? %> +
+ <%= custom_field_tag_with_label_ex( + :dmsf_folder, + CustomValue.new(:custom_field_id => params[:custom_field_id].present? ? params[:custom_field_id] : values.first.custom_field_id, :value => params[:custom_value]), + {}, + :onchange => 'this.form.submit(); return false;') %> +
+ <% end %>