From db5913562608e9df90c629d8dbb72c9f2c38454f Mon Sep 17 00:00:00 2001 From: BOHUNG Date: Mon, 3 Feb 2020 16:47:49 +0800 Subject: [PATCH] add export record feature --- .../stylesheets/admin/cancerpredict.css | 33 ++++++++++++ .../admin/cancerpredicts_controller.rb | 54 ++++++++++++++++++- app/helpers/admin/cancerpredicts_helper.rb | 4 +- .../_export_cancer_tool_record.xlsx.axlsx | 35 ++++++++++++ .../admin/cancerpredicts/showSubmit.html.erb | 4 +- config/locales/en.yml | 1 + config/locales/zh_tw.yml | 1 + config/routes.rb | 1 + lib/cancerpredict/engine.rb | 5 ++ 9 files changed, 134 insertions(+), 4 deletions(-) create mode 100644 app/views/admin/cancerpredicts/_export_cancer_tool_record.xlsx.axlsx diff --git a/app/assets/stylesheets/admin/cancerpredict.css b/app/assets/stylesheets/admin/cancerpredict.css index 68b079f..50070f8 100644 --- a/app/assets/stylesheets/admin/cancerpredict.css +++ b/app/assets/stylesheets/admin/cancerpredict.css @@ -52,4 +52,37 @@ table .checkbox{ .label_left{ float: left; padding-right: 0.5em; +} +.PageDetails { + padding-top: 0.1875em; + padding-bottom: 0.1875em; + font-weight: bold; + font-size: 0.875em; + border-bottom: 0.0625em solid #ddd; + overflow: hidden; + text-align: right; + color: black; + font-size: 1.3em; +} +.PageList { + margin: 0em 0em 0 0.5em; + font-weight: bold; + font-size: 0.875em; + overflow: hidden; + list-style: none; + display: inline; + background: none; + color: #999; +} +ol { + margin: 0 0 0.5em 0; + min-height: 0.625em; + height: auto !important; + height: 0.625em; + padding: 0; + clear: both; + float: right; +} +.PageList li { + display: inline; } \ No newline at end of file diff --git a/app/controllers/admin/cancerpredicts_controller.rb b/app/controllers/admin/cancerpredicts_controller.rb index 3d19715..df2b65e 100644 --- a/app/controllers/admin/cancerpredicts_controller.rb +++ b/app/controllers/admin/cancerpredicts_controller.rb @@ -4,9 +4,10 @@ class Admin::CancerpredictsController < OrbitAdminController require 'rubyXL' require 'fileutils' require "axlsx" - helper Admin::CancerpredictsHelper + #include Admin::CancerpredictsHelper before_action ->(module_app = @app_title) { set_variables module_app } before_action :create_first_field + helper Admin::CancerpredictsHelper #before_action :load_access_levels def initialize super @@ -473,6 +474,21 @@ class Admin::CancerpredictsController < OrbitAdminController end end end + @pagination = create_pagination(@page_num.to_i) + end + def export_cancer_tool_record + @cancer_records = Cancerpredictrecord.where("title"=>@app_title).asc(:id) + dir_path = 'tmp/cancer_tool/' + #FileUtils.rm_r(dir_path, :force => true) if Dir.exist?(dir_path) + FileUtils.mkdir dir_path if !Dir.exist?(dir_path) #create dir for storing tmp_file if dir doesn't exist + @filename = ("#{Time.now.year}_%02s_%02s_export_cancer_tool_record.xlsx"%[Time.now.month,Time.now.day]).gsub(" ","0") + Dir.chdir(dir_path) do + File.open(@filename, 'w') do |f| + f.write render_to_string( :handlers=> [:axlsx], :formats=> [:xlsx] ,:partial=> 'export_cancer_tool_record.xlsx',:locals=> {:@results=>@cancer_records} ) + end + end + tmp_filename_data = File.read(dir_path +@filename) + send_data(tmp_filename_data, type: 'application/xlsx', disposition: 'attachment', filename: @filename) end def create_first_field if Cancerpredictfields.where("title"=>(@app_title + "_back")).take_while{true}.count == 0 @@ -500,4 +516,40 @@ class Admin::CancerpredictsController < OrbitAdminController @form_to_show = Cancerpredictfields.where("title"=>@app_title).first end end + def create_pagination(page=1,fields=Cancerpredictrecord.all,extra_params="") + page = 1 if page == 0 + per_page_num = 10.0 + all_page_num = (fields.count / per_page_num).ceil + all_page_num = 1 if all_page_num == 0 + pagination = '
'+((I18n.locale.to_s == 'zh_tw') ? "第 #{page} 頁" : "page #{page}") +'/'+ ((I18n.locale.to_s == 'zh_tw') ? "共 #{all_page_num} 頁" : "Total #{all_page_num} page(s)")+'
    '+ + ((page == 1) ? '
  1. ['+I18n.t('cancerpredict.prev_page')+']
  2. ' : '
  3. ['+I18n.t('cancerpredict.prev_page')+']
  4. ') + if all_page_num > 7 + if page <= (all_page_num / 2) + if page <= 7 + (1..page).to_a.each{|page_num| pagination += ((page_num == page) ? '
  5. ['+page_num.to_s+']
  6. ' : '
  7. '+page_num.to_s+'
  8. ')} + else + (1..3).to_a.each{|page_num| pagination += ((page_num == page) ? '
  9. ['+page_num.to_s+']
  10. ' : '
  11. '+page_num.to_s+'
  12. ')} + pagination +='
  13. ...
  14. ' + (page-3..page).to_a.each{|page_num| pagination += ((page_num == page) ? '
  15. ['+page_num.to_s+']
  16. ' : '
  17. '+page_num.to_s+'
  18. ')} + end + (page+1..page+3).to_a.each{|page_num| pagination += ((page_num == page) ? '
  19. ['+page_num.to_s+']
  20. ' : '
  21. '+page_num.to_s+'
  22. ')} + pagination +='
  23. ...
  24. ' + (all_page_num-2..all_page_num).to_a.each{|page_num| pagination += ((page_num == page) ? '
  25. ['+page_num.to_s+']
  26. ' : '
  27. '+page_num.to_s+'
  28. ')} + else + (1..3).to_a.each{|page_num| pagination += ((page_num == page) ? '
  29. ['+page_num.to_s+']
  30. ' : '
  31. '+page_num.to_s+'
  32. ')} + pagination +='
  33. ...
  34. ' + (page-3..page).to_a.each{|page_num| pagination += ((page_num == page) ? '
  35. ['+page_num.to_s+']
  36. ' : '
  37. '+page_num.to_s+'
  38. ')} + if all_page_num - page <= 7 + (page+1..all_page_num).to_a.each{|page_num| pagination += ((page_num == page) ? '
  39. ['+page_num.to_s+']
  40. ' : '
  41. '+page_num.to_s+'
  42. ')} + else + (page+1..page+3).to_a.each{|page_num| pagination += ((page_num == page) ? '
  43. ['+page_num.to_s+']
  44. ' : '
  45. '+page_num.to_s+'
  46. ')} + pagination +='
  47. ...
  48. ' + (all_page_num-2..all_page_num).to_a.each{|page_num| pagination += ((page_num == page) ? '
  49. ['+page_num.to_s+']
  50. ' : '
  51. '+page_num.to_s+'
  52. ')} + end + end + else + (1..all_page_num).to_a.each{|page_num| pagination += ((page_num == page) ? '
  53. ['+page_num.to_s+']
  54. ' : '
  55. '+page_num.to_s+'
  56. ')} + end + pagination += (((page==all_page_num) ? '
  57. ['+I18n.t('cancerpredict.next_page')+']
  58. ' : '
  59. ['+I18n.t('cancerpredict.next_page')+']
  60. ')+'
') + end end \ No newline at end of file diff --git a/app/helpers/admin/cancerpredicts_helper.rb b/app/helpers/admin/cancerpredicts_helper.rb index 659972b..57c1789 100644 --- a/app/helpers/admin/cancerpredicts_helper.rb +++ b/app/helpers/admin/cancerpredicts_helper.rb @@ -4,7 +4,7 @@ module Admin::CancerpredictsHelper per_page_num = 10.0 all_page_num = (fields.count / per_page_num).ceil pagination = '
'+((I18n.locale.to_s == 'zh_tw') ? "第 #{page} 頁" : "page #{page}") +'/'+ ((I18n.locale.to_s == 'zh_tw') ? "共 #{all_page_num} 頁" : "Total #{all_page_num} page(s)")+'
    '+ - ((page == 1) ? '
  1. ['+t('cancerpredict.prev_page')+']
  2. ' : '
  3. ['+t('cancerpredict.prev_page')+']
  4. ') + ((page == 1) ? '
  5. ['+I18n.t('cancerpredict.prev_page')+']
  6. ' : '
  7. ['+I18n.t('cancerpredict.prev_page')+']
  8. ') if all_page_num > 7 if page <= (all_page_num / 2) if page <= 7 @@ -32,6 +32,6 @@ module Admin::CancerpredictsHelper else (1..all_page_num).to_a.each{|page_num| pagination += ((page_num == page) ? '
  9. ['+page_num.to_s+']
  10. ' : '
  11. '+page_num.to_s+'
  12. ')} end - pagination += (((page==all_page_num) ? '
  13. ['+t('cancerpredict.next_page')+']
  14. ' : '
  15. ['+t('cancerpredict.next_page')+']
  16. ')+'
') + pagination += (((page==all_page_num) ? '
  • ['+I18n.t('cancerpredict.next_page')+']
  • ' : '
  • ['+I18n.t('cancerpredict.next_page')+']
  • ')+'') end end diff --git a/app/views/admin/cancerpredicts/_export_cancer_tool_record.xlsx.axlsx b/app/views/admin/cancerpredicts/_export_cancer_tool_record.xlsx.axlsx new file mode 100644 index 0000000..8fe89b1 --- /dev/null +++ b/app/views/admin/cancerpredicts/_export_cancer_tool_record.xlsx.axlsx @@ -0,0 +1,35 @@ +# encoding: utf-8 + +wb = xlsx_package.workbook +wb.add_worksheet(name: "school_class") do |sheet| + row = [] + title = sheet.styles.add_style(:bg_color => "FFFF00",:border=>{ :style => :thin, :color => "666666" },:alignment=>{ :horizontal => :center,:vertical => :center ,:wrap_text => true}) + column = sheet.styles.add_style(:alignment=>{ :horizontal => :center,:vertical => :center ,:wrap_text => true},:border=>{ :style => :thin, :color => "666666" }) + row << ((I18n.locale=="en") ? 'submit time' : '提交時間') + @fields = @results.first.names.keys + @fields.each do |field_name| + row << @results.first.names[field_name][I18n.locale.to_s].split('
    ')[0] + end + #export data start + export_data_rows = [] + @results.each do |result| + temp_row = [] + temp_row << result.created_at.to_s + result.result.each do |key,value| + if key != "locale" + if result.values[key][I18n.locale.to_s].length == 0 + temp_row << value + else + temp_row << (result.values[key][I18n.locale.to_s][value.to_i-1] rescue value) + end + end + end + export_data_rows << temp_row + end + + #export data end + sheet.add_row row , :style=> title,:widths => row.map{|cell| (cell.length* 11/ 2).round} + export_data_rows.each do |infos_row| + sheet.add_row infos_row , :style=> column + end +end \ No newline at end of file diff --git a/app/views/admin/cancerpredicts/showSubmit.html.erb b/app/views/admin/cancerpredicts/showSubmit.html.erb index 251581a..d15ede6 100644 --- a/app/views/admin/cancerpredicts/showSubmit.html.erb +++ b/app/views/admin/cancerpredicts/showSubmit.html.erb @@ -1,3 +1,5 @@ + + @@ -42,7 +44,7 @@ <%end%>
    -<%=create_pagination(@page_num).html_safe%> +<%=@pagination.html_safe%>