From 06ec0ff07711b4e9866bf408b43374aae1b56fd6 Mon Sep 17 00:00:00 2001 From: BOHUNG Date: Thu, 19 Mar 2020 23:39:28 +0800 Subject: [PATCH] Add export feature and import feature. --- app/controllers/admin/writings_controller.rb | 20 +++ app/helpers/admin/personal_writings_helper.rb | 51 ++++++++ app/views/admin/writings/analysis.html.erb | 114 ++++++++++++++++++ .../admin/writings/analysis_report.html.erb | 1 + .../admin/writings/download_excel.xlsx.axlsx | 43 +++++++ .../admin/writings/excel_format.xlsx.axlsx | 45 ++----- config/routes.rb | 3 + lib/personal_writing/engine.rb | 2 +- 8 files changed, 240 insertions(+), 39 deletions(-) create mode 100644 app/views/admin/writings/analysis.html.erb create mode 100644 app/views/admin/writings/analysis_report.html.erb create mode 100644 app/views/admin/writings/download_excel.xlsx.axlsx diff --git a/app/controllers/admin/writings_controller.rb b/app/controllers/admin/writings_controller.rb index 6b1d457..9ed489d 100644 --- a/app/controllers/admin/writings_controller.rb +++ b/app/controllers/admin/writings_controller.rb @@ -20,7 +20,27 @@ class Admin::WritingsController < OrbitMemberController render :layout => false end end + def analysis_report + role = params[:role_id] + year_start = params[:year_start].to_i + year_end = params[:year_end].to_i + graph_by = params[:graph_by] + @data = get_chart_data(year_start,year_end,role,params[:graph_by]) + + render :layout => false + end + + def download_excel + year_start = params[:year_start].to_i + year_end = params[:year_end].to_i + @data = get_data_for_excel(year_start,year_end) + respond_to do |format| + format.xlsx { + response.headers['Content-Disposition'] = 'attachment; filename="writings.xlsx"' + } + end + end def create if !writing_params['member_profile_id'].blank? diff --git a/app/helpers/admin/personal_writings_helper.rb b/app/helpers/admin/personal_writings_helper.rb index 6bf3781..974c912 100644 --- a/app/helpers/admin/personal_writings_helper.rb +++ b/app/helpers/admin/personal_writings_helper.rb @@ -63,5 +63,56 @@ module Admin::PersonalWritingsHelper writing.member_profile = mp writing.save end + def get_data_for_excel(year_start,year_end) + data = [] + roles = Role.where(:disabled => false, :title.ne => "", :title.ne => nil).asc(:key) + roles.each do |role| + d = {} + d["name"] = role.title + mps = role.member_profile_ids + # d1 = DateTime.new(year_start,1,1,0,0) + # d2 = DateTime.new(year_end,12,31,23,59) + d["data"] = Writing.where(:year.gte => year_start.to_s, :year.lte => year_end.to_s, :member_profile_id.in => mps) rescue [] + data << d + end + return data + end + def get_chart_data(year_start,year_end,role,type) + case type + when "category" + jls = ResearchCategory.all + end + finaldata = [] + role = Role.find(role) rescue nil + mps = [] + if !role.nil? + mps = role.member_profile_ids + end + jls.each do |jl| + data = {} + data["name"] = jl.title + data["data"] = {} + (year_start..year_end).each do |year| + # d1 = DateTime.new(year,1,1,0,0) + # d2 = DateTime.new(year,12,31,23,59) + t = jl.researches.where(:year => year.to_s, :member_profile_id.in => mps).count rescue 0 + data["data"][year.to_s] = t + end + finaldata << data + end + data = {"name" => "N/A", "data" => {}} + (year_start..year_end).each do |year| + # d1 = DateTime.new(year,1,1,0,0) + # d2 = DateTime.new(year,12,31,23,59) + case type + when "category" + t = Writing.where(:year => year, :member_profile_id.in => mps, :project_type_id => nil).count rescue 0 + end + + data["data"][year.to_s] = t + end + finaldata << data + finaldata + end end \ No newline at end of file diff --git a/app/views/admin/writings/analysis.html.erb b/app/views/admin/writings/analysis.html.erb new file mode 100644 index 0000000..7198bb3 --- /dev/null +++ b/app/views/admin/writings/analysis.html.erb @@ -0,0 +1,114 @@ +<% # encoding: utf-8 %> +<% content_for :page_specific_css do %> + <%= stylesheet_link_tag "lib/main-forms" %> + <%= stylesheet_link_tag "lib/fileupload" %> + <%= stylesheet_link_tag "lib/main-list" %> + <%= stylesheet_link_tag "lib/main-form-col2" %> + +<% end %> +<% content_for :page_specific_javascript do %> + <%= javascript_include_tag "//www.google.com/jsapi", "chartkick"%> + <%= javascript_include_tag "justgage.1.0.1.min" %> + <%= javascript_include_tag "raphael.2.1.0.min" %> + <%= javascript_include_tag "validator" %> +<% end %> +
+
+
+
+
+ +
+ <%= select_year(DateTime.now.year - 5, {:start_year => DateTime.now.year, :end_year => 1950}, {:name => 'start_year', :class => "span1"} ) %> + - + <%= select_year(DateTime.now.year, {:start_year => DateTime.now.year, :end_year => 1950}, {:name => 'end_year', :class => "span1"} ) %> +
+
+
+ +
+ <%= t("personal_writing.project_category") %> +
+
+
+
+ + Export +
+
+
+
+ <% Role.where(:disabled => false, :title.ne => "", :title.ne => nil).asc(:key).each do |role| %> +
+

<%= role.title %>

+
+ loading +
+
+ <% end %> +
+
+ + + + + diff --git a/app/views/admin/writings/analysis_report.html.erb b/app/views/admin/writings/analysis_report.html.erb new file mode 100644 index 0000000..b4e8aa4 --- /dev/null +++ b/app/views/admin/writings/analysis_report.html.erb @@ -0,0 +1 @@ +<%= column_chart @data, :id => params[:role_id], :height => "350px", :xtitle => "Year", :ytitle => "Total number" %> \ No newline at end of file diff --git a/app/views/admin/writings/download_excel.xlsx.axlsx b/app/views/admin/writings/download_excel.xlsx.axlsx new file mode 100644 index 0000000..81daed1 --- /dev/null +++ b/app/views/admin/writings/download_excel.xlsx.axlsx @@ -0,0 +1,43 @@ +# encoding: utf-8 + +wb = xlsx_package.workbook +@data.each_with_index do |role,idx| + data = role["data"] + wb.add_worksheet(name: role["name"] + "-" + idx.to_s) do |sheet| + + heading = sheet.styles.add_style(:b => true, :locked => true) + + row = ["Name",t(:category)] + + @site_in_use_locales.each do |locale| + row << t("personal_writing.writing_title") + " - " + t(locale.to_s) + end + @site_in_use_locales.each do |locale| + row << t("personal_writing.other") + " - " + t(locale.to_s) + end + + row << t("personal_writing.year") + + row << t("personal_writing.month") + + row << t("personal_writing.url") + + sheet.add_row row, :style => heading + + + data.each do |proj| + row = [(proj.member_profile_id.class == Array ? proj.member_profile_id.map{|member_id| MemberProfile.find(member_id).name rescue ""} : [proj.member_profile.name])] + row << (proj.category.title rescue "") + @site_in_use_locales.each do |locale| + row << proj.writing_title_translations[locale.to_s] + end + @site_in_use_locales.each do |locale| + row << proj.other_translations[locale.to_s] + end + row << proj.year + row << proj.month + row << proj.url + sheet.add_row row + end + end +end \ No newline at end of file diff --git a/app/views/admin/writings/excel_format.xlsx.axlsx b/app/views/admin/writings/excel_format.xlsx.axlsx index 7aa1b04..09a2f40 100644 --- a/app/views/admin/writings/excel_format.xlsx.axlsx +++ b/app/views/admin/writings/excel_format.xlsx.axlsx @@ -15,62 +15,31 @@ wb.add_worksheet(name: "Writing") do |sheet| row1 << "" row2 << "" - row << t("personal_writing.lab_title") + " - " + t("en") + row << t("personal_writing.writing_title") + " - " + t("en") row1 << "textfield" row2 << "" - row << t("personal_writing.lab_title") + " - " + t("zh_tw") + row << t("personal_writing.writing_title") + " - " + t("zh_tw") row1 << "textfield" row2 << "" - row << t("personal_writing.location") + " - " + t("en") + row << t("personal_writing.other") + " - " + t("en") row1 << "textfield" row2 << "" - row << t("personal_writing.location") + " - " + t("zh_tw") - row1 << "textfield" - row2 << "" - - row << t("personal_writing.participating_professor") + " - " + t("en") - row1 << "textfield" - row2 << "" - row << t("personal_writing.participating_professor") + " - " + t("zh_tw") - row1 << "textfield" - row2 << "" - - row << t("personal_writing.participating_student") + " - " + t("en") - row1 << "textfield" - row2 << "" - row << t("personal_writing.participating_student") + " - " + t("zh_tw") + row << t("personal_writing.other") + " - " + t("zh_tw") row1 << "textfield" row2 << "" row << t("personal_writing.year") row1 << "number" row2 << "Example : 2015 or 2014 or 1987" - - row << t("personal_writing.extension_no") - row1 << "textfield" - row2 << "" - - row << t("personal_writing.research_direction") - row1 << "textfield" - row2 << "" - - row << t("personal_writing.facility") - row1 << "textfield" - row2 << "" + row << t("personal_writing.month") + row1 << "number" + row2 << "Example : 1 or 2 or 12" row << t("personal_writing.url") row1 << "textfield" row2 << "http://domain.com/path" - row << t("personal_writing.keywords") - row1 << "textfield" - row2 << "Example: keyword1,keyword2" - - row << t("personal_writing.note") - row1 << "textarea" - row2 << "" - sheet.add_row row, :style => heading sheet.add_row row1 sheet.add_row row2, :style => example diff --git a/config/routes.rb b/config/routes.rb index 71fbf69..42b39e3 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -9,6 +9,9 @@ Rails.application.routes.draw do resources :writings do collection do get 'toggle_hide' => 'writings#toggle_hide' + get 'analysis' + get 'analysis_report' + get "download_excel" end end diff --git a/lib/personal_writing/engine.rb b/lib/personal_writing/engine.rb index 233a073..d43a750 100644 --- a/lib/personal_writing/engine.rb +++ b/lib/personal_writing/engine.rb @@ -4,7 +4,7 @@ module PersonalWriting OrbitApp.registration "PersonalWriting",:type=> 'ModuleApp' do module_label 'module_name.personal_writing' base_url File.expand_path File.dirname(__FILE__) - personal_plugin :enable => true, :sort_number => '30', :app_name=>"Writing", :intro_app_name=>"PersonalWritingIntro",:path=>"/plugin/personal_writing/profile",:front_path=>"/profile",:admin_path=>"/admin/writings",:i18n=>'module_name.personal_writing', :module_app_name=>'PersonalWriting' + personal_plugin :enable => true, :sort_number => '30', :app_name=>"Writing", :intro_app_name=>"PersonalWritingIntro",:path=>"/plugin/personal_writing/profile",:front_path=>"/profile",:admin_path=>"/admin/writings",:i18n=>'module_name.personal_writing', :module_app_name=>'PersonalWriting', :analysis=> true, :analysis_path => "/admin/writings/analysis" version "0.1" desktop_enabled true