From 03a22138af277ed3d8c460ff62884f775a618359 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B1=E5=8D=9A=E4=BA=9E?= Date: Sun, 21 Jul 2024 15:26:48 +0800 Subject: [PATCH] Add file column type. --- .../universal_tables_controller.rb | 72 +++-- app/models/column_entry.rb | 45 +++ app/models/column_entry_file.rb | 36 +++ app/models/u_table.rb | 2 +- .../universal_tables/_file_field.html.erb | 289 ++++++++++++++++++ .../universal_tables/_form_file.html.erb | 65 ++++ .../export_structure.xlsx.axlsx | 4 + .../admin/universal_tables/show.html.erb | 8 + .../universal_tables/download_file.html.erb | 147 +++++++++ .../export_filtered.xlsx.axlsx | 4 +- .../redirect_to_file.html.erb | 17 ++ app/views/utable_export/export.xlsx.axlsx | 15 +- config/locales/en.yml | 5 +- config/locales/zh_tw.yml | 5 +- config/routes.rb | 1 + 15 files changed, 677 insertions(+), 38 deletions(-) create mode 100644 app/models/column_entry_file.rb create mode 100644 app/views/admin/universal_tables/_file_field.html.erb create mode 100644 app/views/admin/universal_tables/_form_file.html.erb create mode 100644 app/views/universal_tables/download_file.html.erb create mode 100644 app/views/universal_tables/redirect_to_file.html.erb diff --git a/app/controllers/universal_tables_controller.rb b/app/controllers/universal_tables_controller.rb index ec4c2c7..bf3b739 100644 --- a/app/controllers/universal_tables_controller.rb +++ b/app/controllers/universal_tables_controller.rb @@ -82,22 +82,7 @@ class UniversalTablesController < ApplicationController tablecolumns.each do |column| ce = te.column_entries.where(:table_column_id => column.id).first rescue nil if !ce.nil? - text = "" - case ce.type - when "text" - text = ce.text - when "integer" - text = ce.number - when "editor" - text = ce.content - when "date" - text = format_date(ce.date, column.date_format) - when "period" - text = format_date(ce.period_from, column.date_format) + " ~ " + format_date(ce.period_to, column.date_format) - text = "" if text.starts_with?(" ~") - when "image" - text = "" - end + text = ce.get_frontend_text(column) if column.is_link_to_show text = "#{text}" end @@ -172,6 +157,14 @@ class UniversalTablesController < ApplicationController text = "" if text.starts_with?(" ~") when "image" text = host_url + ce.image.thumb.url + when "file" + file_links = [] + locale = I18n.locale.to_s + ce.column_entry_files.desc(:sort_number).each do |entry_file| + next unless entry_file.choose_lang_display(locale) + file_links << (host_url + entry_file.get_link) + end + text = file_links.join("\r\n") end cols << {"text" => text} else @@ -233,23 +226,7 @@ class UniversalTablesController < ApplicationController entry.column_entries.each do |ce| ct = ce.table_column - text = "" - case ce.type - when "text" - text = ce.text - when "integer" - text = ce.number - when "editor" - text = ce.content - when "date" - text = format_date(ce.date, ce.table_column.date_format) - when "period" - text = format_date(ce.period_from, ce.table_column.date_format) + " ~ " + format_date(ce.period_from, ce.table_column.date_format) - text = "" if text.starts_with?(" ~") - when "image" - text = "" - end - + text = ce.get_frontend_text(ct) rows << { "title" => ct.title, "text" => text, @@ -266,4 +243,33 @@ class UniversalTablesController < ApplicationController } } end + + def download_file + file_id = params[:file] + file = ColumnEntryFile.find(file_id) rescue nil + if !file.nil? && file.file.present? + file.inc(download_count: 1) + @url = file.file.url + begin + @path = file.file.file.file rescue "" + @filename = File.basename(@path) + @ext = @filename.split(".").last + if @ext == "png" || @ext == "jpg" || @ext == "bmp" || @ext == "pdf" + render "download_file",:layout=>false + else + if (current_site.accessibility_mode rescue false) + render "redirect_to_file",:layout=>false + else + user_agent = request.user_agent.downcase + @escaped_file_name = user_agent.match(/(msie|trident)/) ? CGI::escape(@filename) : @filename + send_file(@path, :type=>"application/octet-stream", :filename => @escaped_file_name, :x_sendfile=> true) + end + end + rescue + redirect_to @url + end + else + render :file => "#{Rails.root}/app/views/errors/404.html", :layout => false, :status => :not_found + end + end end diff --git a/app/models/column_entry.rb b/app/models/column_entry.rb index 1d80c45..704c7db 100644 --- a/app/models/column_entry.rb +++ b/app/models/column_entry.rb @@ -2,6 +2,9 @@ class ColumnEntry include Mongoid::Document include Mongoid::Timestamps + include Admin::UniversalTablesHelper + include ActionView::Helpers::NumberHelper + field :text, :localize => true field :content, :localize => true field :date, type: DateTime @@ -11,6 +14,10 @@ class ColumnEntry mount_uploader :image, ImageUploader + has_many :column_entry_files, :autosave => true, :dependent => :destroy + accepts_nested_attributes_for :column_entry_files, :allow_destroy => true + after_save :save_column_entry_files + belongs_to :table_entry, index: true belongs_to :table_column, index: true @@ -23,4 +30,42 @@ class ColumnEntry def type self.table_column.type end + + def save_column_entry_files + return if @skip_callback + self.column_entry_files.each do |t| + if t.should_destroy + t.destroy + end + end + end + + def get_frontend_text(column) + text = "" + case self.type + when "text" + text = self.text + when "integer" + text = self.number + when "editor" + text = self.content + when "date" + text = format_date(self.date, column.date_format) + when "period" + text = format_date(self.period_from, column.date_format) + " ~ " + format_date(self.period_to, column.date_format) + text = "" if text.starts_with?(" ~") + when "image" + text = "" + when "file" + locale = I18n.locale.to_s + text = "" + end + text + end end \ No newline at end of file diff --git a/app/models/column_entry_file.rb b/app/models/column_entry_file.rb new file mode 100644 index 0000000..48073fa --- /dev/null +++ b/app/models/column_entry_file.rb @@ -0,0 +1,36 @@ +class ColumnEntryFile + + include Mongoid::Document + include Mongoid::Timestamps + + mount_uploader :file, AssetUploader + + field :file_title, localize: true + # field :description + field :download_count, type: Integer, default: 0 + field :choose_lang, :type => Array, :default => I18n.available_locales.map{|l| l.to_s} + + field :should_destroy, :type => Boolean + field :sort_number, :type => Integer + + # default_scope asc(:sort_number) + + def choose_lang_display(lang) + self.file.present? && self.choose_lang.include?(lang) + end + + def get_file_title + _file_title = self.file_title + if _file_title.blank? && self.file.present? + _file_title = self[:file] + end + _file_title + end + + def get_link + "/xhr/universal_table/download?file=#{self.id}" + end + + belongs_to :column_entry, index: true + +end diff --git a/app/models/u_table.rb b/app/models/u_table.rb index 6669399..95c3e9d 100644 --- a/app/models/u_table.rb +++ b/app/models/u_table.rb @@ -17,7 +17,7 @@ class UTable accepts_nested_attributes_for :table_columns, :allow_destroy => true - FIELD_TYPES = ["text", "integer", "editor", "image", "date", "period"] + FIELD_TYPES = ["text", "integer", "editor", "image", "date", "period", "file"] DATE_FORMATS = ["yyyy/MM/dd hh:mm", "yyyy/MM/dd","yyyy/MM", "yyyy"] def default_ordered if self.ordered_with_created_at diff --git a/app/views/admin/universal_tables/_file_field.html.erb b/app/views/admin/universal_tables/_file_field.html.erb new file mode 100644 index 0000000..77446d7 --- /dev/null +++ b/app/views/admin/universal_tables/_file_field.html.erb @@ -0,0 +1,289 @@ +<% # encoding: utf-8 %> +<% content_for :page_specific_css do %> + +<% end %> +<% content_for :page_specific_javascript do %> + <%= javascript_include_tag "lib/file-type" %> + <%= javascript_include_tag "lib/module-area" %> + <%= javascript_include_tag "lib/jquery-ui-sortable.min" %> +<% end %> + +
+ <%= f.label :text, column.title, :class => "control-label" %> +
+

+ <%= hidden_field_tag 'column_entry_file_field_count', file_field.column_entry_files.count %> + <%= t(:add) %> +

+
+ +
+ + <% if file_field && !file_field.column_entry_files.blank? %> +
+ <% file_field.column_entry_files.desc(:sort_number).each_with_index do |column_entry_file, i| %> + <%= f.fields_for :column_entry_files, column_entry_file do |f| %> + <%= render :partial => 'form_file', :locals => {:f => f, :i => i,:form_file => column_entry_file} %> + <% end %> + <% end %> +
+
+ <% end %> +
+
+
+ <%=t("universal_table.drag_file_to_here")%> +
+
+
+ +
+ <% if !file_field.new_record? %> + <%= f.hidden_field :id %> + <% else %> + <%= f.hidden_field :table_column_id, :value => column.id %> + <% end %> + + +<% content_for :page_specific_javascript do %> + +<% end %> \ No newline at end of file diff --git a/app/views/admin/universal_tables/_form_file.html.erb b/app/views/admin/universal_tables/_form_file.html.erb new file mode 100644 index 0000000..3ef82de --- /dev/null +++ b/app/views/admin/universal_tables/_form_file.html.erb @@ -0,0 +1,65 @@ +<% if form_file.new_record? %> +
+<% else %> +
+ + <% if form_file.file.blank? %> + <%= t(:no_file) %> + <% else %> + <%= link_to content_tag(:i) + form_file.file_identifier, form_file.file.url, {:class => 'file-link file-type', :target => '_blank', :title => form_file.file_identifier} %> + <% end %> +<% end %> +
+ + + + <% @site_in_use_locales.each_with_index do |locale, i| %> + <%= locale %>"> + <%= f.fields_for :file_title_translations do |f| %> + <%= f.text_field locale, :class => "input-medium", placeholder: t('file.name'), :value => (form_file.file_title_translations[locale] rescue nil) %> + <% end %> + + <% end %> + + + + + + <%= hidden_field_tag "#{f.object_name}[choose_lang][]", '' %> + + + <% if form_file.new_record? %> + + + <%= f.hidden_field :sort_number, :value => "new_column_entry_file_sort_order_XXX", :class => "input-mini" %> + + <% else %> + + <%= f.hidden_field :id %> + <%= f.hidden_field :sort_number , :class => "file-sort-number-field" %> + + <%= f.hidden_field :_destroy, :value => nil, :class => 'should_destroy' %> + + Downloaded <%= form_file.download_count %> time<%= form_file.download_count > 1 ? "s" : "" %>. + <% end %> +
+
+ diff --git a/app/views/admin/universal_tables/export_structure.xlsx.axlsx b/app/views/admin/universal_tables/export_structure.xlsx.axlsx index e05085b..55fa3e3 100644 --- a/app/views/admin/universal_tables/export_structure.xlsx.axlsx +++ b/app/views/admin/universal_tables/export_structure.xlsx.axlsx @@ -43,6 +43,10 @@ wb.add_worksheet(name: "Structure") do |sheet| row << column.title + "-To" row1 << column.key row2 << column.type + " : " + column.date_format.upcase + "-period_to" + when "file" + row << column.title + row1 << column.key + row2 << "Please leave this column blank. Upload the files manually." end end diff --git a/app/views/admin/universal_tables/show.html.erb b/app/views/admin/universal_tables/show.html.erb index 2f6d084..acd439f 100644 --- a/app/views/admin/universal_tables/show.html.erb +++ b/app/views/admin/universal_tables/show.html.erb @@ -53,6 +53,14 @@ <% if !ce.period_from.nil? %> <%= format_date(ce.period_from, column.date_format) %> ~ <%= format_date(ce.period_to, column.date_format) %> <% end %> + <% when "file" %> + <% locale = I18n.locale.to_s %> +
    + <% ce.column_entry_files.desc(:sort_number).each do |entry_file| %> + <% next unless entry_file.choose_lang_display(locale) %> +
  1. <%= link_to entry_file.get_file_title, entry_file.file.url, target: "_blank" %>
  2. + <% end %> +
<% end %> <% else %>   diff --git a/app/views/universal_tables/download_file.html.erb b/app/views/universal_tables/download_file.html.erb new file mode 100644 index 0000000..f6e4d90 --- /dev/null +++ b/app/views/universal_tables/download_file.html.erb @@ -0,0 +1,147 @@ +<% if @ext == 'pdf' %> + <%= render partial: 'archives/viewer' %> +<% else %> + + + + <%=@filename%> + <%= stylesheet_link_tag "archive/download_file.css" %> + + +

<%=@filename%>

+ <% if @ext != "png" && @ext != "jpg" && @ext != "bmp" %> + + + <%=@filename%> + + <% else %> + <%=@filename%> + + <% end %> + + +<% end %> \ No newline at end of file diff --git a/app/views/universal_tables/export_filtered.xlsx.axlsx b/app/views/universal_tables/export_filtered.xlsx.axlsx index 92deb06..2e29d66 100644 --- a/app/views/universal_tables/export_filtered.xlsx.axlsx +++ b/app/views/universal_tables/export_filtered.xlsx.axlsx @@ -8,11 +8,13 @@ wb.add_worksheet(name: "Table") do |sheet| headings = @tablecolumns.collect{|tc| tc.title} sheet.add_row headings, :style => heading + wrap = sheet.styles.add_style alignment: {wrap_text: true} + @rows.each do |r| row = [] r["columns"].each do |col| row << col["text"] end - sheet.add_row row + sheet.add_row row, style: wrap end end \ No newline at end of file diff --git a/app/views/universal_tables/redirect_to_file.html.erb b/app/views/universal_tables/redirect_to_file.html.erb new file mode 100644 index 0000000..0968d5c --- /dev/null +++ b/app/views/universal_tables/redirect_to_file.html.erb @@ -0,0 +1,17 @@ + + + <%=@filename%> + + + +
+

<%=@filename%>

+ <% download_text = t('download') + " " + @filename %> +

<%=download_text%>

+

+ <%=t('close')%> +

+
+ + + \ No newline at end of file diff --git a/app/views/utable_export/export.xlsx.axlsx b/app/views/utable_export/export.xlsx.axlsx index 4bd04ae..e3e57e5 100644 --- a/app/views/utable_export/export.xlsx.axlsx +++ b/app/views/utable_export/export.xlsx.axlsx @@ -5,6 +5,7 @@ wb = xlsx_package.workbook wb.add_worksheet(name: "Structure") do |sheet| heading = sheet.styles.add_style(:b => true, :locked => true) type = sheet.styles.add_style(:i => true) + wrap = sheet.styles.add_style alignment: {wrap_text: true} row = [] row1 = [] @@ -40,6 +41,10 @@ wb.add_worksheet(name: "Structure") do |sheet| row << column.title + "-From ~ To" row1 << column.key row2 << column.type + " : " + column.date_format.upcase + "-period_from ~ period_to" + when "file" + row << column.title + row1 << column.key + row2 << "Please leave this column blank. Upload the files manually." end end @@ -90,9 +95,17 @@ wb.add_worksheet(name: "Structure") do |sheet| when "yyyy" row << (column.period_from.strftime("%Y")rescue "") + " ~ " + (column.period_to.strftime("%Y") rescue "") end + when "file" + file_links = [] + locale = I18n.locale.to_s + column.column_entry_files.desc(:sort_number).each do |entry_file| + next unless entry_file.choose_lang_display(locale) + file_links << (url + entry_file.get_link) + end + row << file_links.join("\r\n") end end - sheet.add_row row + sheet.add_row row, style: wrap end end \ No newline at end of file diff --git a/config/locales/en.yml b/config/locales/en.yml index a8d1598..2ac7437 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -17,4 +17,7 @@ en: sort_number: Sort Number created_at: Created Time edit_sort: Edit Sorting - manual_update_sort: Manually Update Sorting \ No newline at end of file + manual_update_sort: Manually Update Sorting + drag_file_to_here: Drag file to here + show_lang: Language + downloaded_times: Downloaded Times \ No newline at end of file diff --git a/config/locales/zh_tw.yml b/config/locales/zh_tw.yml index 9879a56..aaef0cc 100644 --- a/config/locales/zh_tw.yml +++ b/config/locales/zh_tw.yml @@ -17,4 +17,7 @@ zh_tw: sort_number: 排序數 created_at: 創建時間 edit_sort: 編輯排序 - manual_update_sort: 手動更新排序 \ No newline at end of file + manual_update_sort: 手動更新排序 + drag_file_to_here: 拖移檔案到此 + show_lang: 呈現語系 + downloaded_times: 下載次數 \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 82754f0..eb58870 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -21,6 +21,7 @@ Rails.application.routes.draw do end end get "/xhr/universal_table/export", to: 'universal_tables#export_filtered' + get "/xhr/universal_table/download", to: "universal_tables#download_file" end end