diff --git a/app/controllers/admin/universal_tables_controller.rb b/app/controllers/admin/universal_tables_controller.rb index a35bcb7..3f01dfc 100755 --- a/app/controllers/admin/universal_tables_controller.rb +++ b/app/controllers/admin/universal_tables_controller.rb @@ -122,6 +122,11 @@ end render :json => {"status" => running}.to_json end + def checkforimportthread + utable = UTable.find(params[:utable_id]) + render :json => {"currentCount" => utable.current_xlsx_value, "totalCount" => utable.table_entries.count}.to_json + end + def export_data I18n.locale = :zh_tw table = UTable.find(params[:id]) @@ -135,131 +140,45 @@ end render :json => {"success" => true, "title" => title}.to_json end -def import_data_from_excel - site_in_use_locales = @site_in_use_locales.sort - workbook = RubyXL::Parser.parse(params["import_data"].tempfile) - response = {} - current_locale = I18n.locale - table = UTable.find(params["universal_table_id"]) rescue nil - if table.nil? - render json: { success: false, msg: "Table not found." }.to_json and return - end + def import_data_from_excel + site_in_use_locales = @site_in_use_locales.sort + workbook = RubyXL::Parser.parse(params["import_data"].tempfile) + response = {} + current_locale = I18n.locale + table = UTable.find(params["universal_table_id"]) rescue nil + if table.nil? + render json: { success: false, msg: "Table not found." }.to_json and return + end + table.set(current_xlsx_value: 0) + sheet = workbook[0] + if sheet.count > 503 + render json: { success: false, msg: "More than 500 entries. Please split the entries in different files." }.to_json and return + end - sheet = workbook[0] - if sheet.count > 503 - render json: { success: false, msg: "More than 500 entries. Please split the entries in different files." }.to_json and return - end + uploaded_io = params[:import_data] + safe_filename = uploaded_io.original_filename.gsub(/[^0-9A-Za-z.\-_]/, '_') + table_id = params[:universal_table_id] + site_locales = @site_in_use_locales.join(",") # e.g., "en,zh" - # 前三列是欄位名、key、格式描述 - column_titles = sheet[0].cells.map { |c| c&.value.to_s.strip } - column_keys = sheet[1].cells.map { |c| c&.value.to_s.strip } - column_types = sheet[2].cells.map { |c| c&.value.to_s.strip } + unless uploaded_io + render json: { success: false, msg: "No file uploaded." } and return + end - # 準備欄位對應 - columns = column_keys.uniq.map.with_index do |key, i| - tc = table.table_columns.where(key: key).first - [i, tc] - end.to_h + # Save to tmp path + tmp_path = Rails.root.join('tmp', "import_#{Time.now.to_i}_#{safe_filename}") + File.open(tmp_path, 'wb') do |file| + file.write(uploaded_io.read) + end + Rails.logger.info "rake universal_table_tasks:import[#{tmp_path},#{table_id},#{site_locales}]" + # Call the Rake task with file path + system("rake universal_table_tasks:import[#{tmp_path},#{table_id},#{site_locales}]") - sheet.each_with_index do |row, i| - next if i < 3 || row.cells.compact.map { |c| c.value.to_s.strip }.all?(&:blank?) - uid_val = row[0]&.value.to_s.strip rescue nil - te = uid_val.present? ? - TableEntry.where(uid: uid_val, u_table_id: table.id).first_or_initialize : - TableEntry.new - te.u_table = table - skip = 0 - - tc_idx = 0 - - row.cells.each_with_index do |cell, col_idx| - next if skip > 0 && (skip -= 1) >= 0 - - val = cell&.value - tc = columns[tc_idx] - tc_idx += 1 - next if tc.nil? - - ce = te.column_entries.where(table_column_id: tc.id).first - ce = ColumnEntry.new(table_column_id: tc.id) if ce.nil? - case tc.type - when "text", "editor" - v = {} - site_in_use_locales.each_with_index do |locale, offset| - v[locale.to_s] = row[col_idx + offset]&.value.to_s rescue "" - end - skip = site_in_use_locales.size - 1 - if tc.type == "text" - ce.text_translations = v - else - ce.content_translations = v - end - - when "integer" - ce.number = val.present? ? val.to_i : nil - - when "image" - ce.remote_image_url = val if val.present? - when "file" - file_urls = val.to_s.split(";").map(&:strip) - file_titles = row[col_idx + 1]&.value.to_s.split(";").map(&:strip) - skip = 1 - - ce.column_entry_files.destroy_all - ce.column_entry_files = [] - - file_urls.each_with_index do |remote_url, file_idx| - next if remote_url.blank? - file = ColumnEntryFile.new - file.remote_file_url = remote_url - - # 處理多語言標題 - titles = {} - site_in_use_locales.each do |locale| - titles[locale.to_s] = file_titles[file_idx] rescue file.file.file.filename - end - file.file_title_translations = titles - file.save! - ce.column_entry_files << file - end - when "date" - ce.date = val - - when "period" - skip = 1 - ce.period_from = val - ce.period_to = row[col_idx + 1]&.value rescue nil - end - - ce.save! - te.column_entries << ce - end - - # hashtags (倒數第2欄) - if row.cells.count >= 2 - tags_text = row.cells[-2]&.value.to_s rescue "" - create_get_table_tags(te, tags_text.split(";")) - end - - # related_entries (倒數第1欄) - if row.cells.count >= 1 - related_uids = row.cells[-1]&.value.to_s.split(";").map(&:strip) - related_ids = TableEntry.where(:uid.in => related_uids).pluck(:id) - te.related_entries = related_ids.join(",") - end - - te.save! - te.fix_have_data - te.uid = uid_val if uid_val.present? - te.save! - end - - render json: { - success: true, - count: table.table_entries.count, - id: table.id.to_s - }.to_json -end + render json: { + success: true, + totalCount: sheet.count - 3, + id: table.id.to_s + }.to_json + end def new_entry uid = params[:universal_table_id].split("-").last @@ -422,4 +341,4 @@ end def is_uuid?(str) !!(str =~ /\A[\da-f]{24}\z/i || str =~ /\A[\da-f]{8}-([\da-f]{4}-){3}[\da-f]{12}\z/i) end -end \ No newline at end of file +end diff --git a/app/models/u_table.rb b/app/models/u_table.rb index 082263a..b577d05 100755 --- a/app/models/u_table.rb +++ b/app/models/u_table.rb @@ -11,6 +11,7 @@ class UTable field :ordered_with_created_at, type: Boolean, default: true field :created_at_order_direction, type: String, default: 'desc' + field :current_xlsx_value, type: Integer, default: 0 has_many :table_columns, :dependent => :destroy has_many :table_entries, :dependent => :destroy diff --git a/app/views/admin/universal_tables/index.html.erb b/app/views/admin/universal_tables/index.html.erb index 6a3a52d..838e9cd 100755 --- a/app/views/admin/universal_tables/index.html.erb +++ b/app/views/admin/universal_tables/index.html.erb @@ -4,26 +4,7 @@
<%= render 'index'%>
- + + +