class AsiaDatabasesController < ApplicationController include Admin::AsiaDatabasesHelper ExtraFields = { 'discipline_expertise'=> ['discipline_expertise'], 'project'=> ['project_name', 'start_date', 'sponsor'], 'paper'=> ['paper_name', 'issue_year', 'journal_name'], 'patent'=> ['patent_name', 'certification_date', 'patent_country'], 'tec_transfer'=> ['tech_transfer_name', 'start_date', 'receiving_unit'], 'exhibition'=> ['exhibition_name', 'exhibition_date', 'host'] } def render_404 render :file => "#{Rails.root}/app/views/errors/404.html", :layout => false, :status => :not_found, :content_type => 'text/html' end def get_data(params, for_frontend=false) # choice unit choice_unit = [] selected_unit = params[:unit] selected_academy = nil if selected_unit == 'all' choice_unit << {"choice_value"=> 'all', "choice_show"=> I18n.t('asia_database.extend_translate.all'), 'choice_select'=> 'selected'} AsiaAcademy.all.each do |academy| choice_unit << {"choice_value"=> academy.uid, "choice_show"=> academy.academy_name, 'choice_select'=>''} end else choice_unit << {"choice_value"=> 'all', "choice_show"=> I18n.t('asia_database.extend_translate.all'), 'choice_select'=> ''} AsiaAcademy.all.each do |academy| uid = academy.uid if uid == selected_unit selected_academy = academy choice_select = 'selected' else choice_select = '' end choice_unit << {"choice_value"=> uid, "choice_show"=> academy.academy_name, 'choice_select'=> choice_select} end end # choice field choice_field = [] fields_to_select = ["patent", "project", "paper", "discipline_expertise", "tec_transfer", "exhibition"] fields_to_select.each do |fs| choice_field << {'choice_show'=> t("asia_database.extend_translate.#{fs}"), 'choice_value' => fs, 'choice_select' => ''} end selected_field_int = 0 selected_field = fields_to_select[0] choice_field.each_with_index do |h, i| field_name = h['choice_value'] if field_name == params[:field] h['choice_select'] = 'selected' selected_field = field_name selected_field_int = i break end end fields_to_show = ['academy', 'department', 'teacher'] if selected_field == "discipline_expertise" fields_to_show += ExtraFields[selected_field] table_headers = fields_to_show.map do |fs| { 'head-title' => t("asia_database.#{fs}") } end else table_headers = fields_to_show.map do |fs| { 'head-title' => t("asia_database.#{fs}") } end fields_to_show = fields_to_show.map{|fs| "asia_teacher.#{fs}"} extra_fields = ExtraFields[selected_field] fields_to_show += extra_fields table_headers += extra_fields.map do |fs| { 'head-title' => t("asia_database.asia_#{selected_field}.#{fs}") } end end search_field = nil if selected_field_int == 3 #Discipline Expertise if selected_academy asia_data = selected_academy.asia_teachers else asia_data = AsiaTeacher.all end search_field = 'discipline_expertise' elsif selected_field_int == 1 #Project if selected_academy asia_data = AsiaProject.where(:asia_teacher_id.in=>selected_academy.asia_teacher_ids) else asia_data = AsiaProject.all end search_field = 'project_name' elsif selected_field_int == 2 #Paper if selected_academy asia_data = AsiaPaper.where(:asia_teacher_id.in=>selected_academy.asia_teacher_ids) else asia_data = AsiaPaper.all end search_field = 'paper_name' elsif selected_field_int == 0 #Patent if selected_academy asia_data = AsiaPatent.where(:asia_teacher_id.in=>selected_academy.asia_teacher_ids) else asia_data = AsiaPatent.all end search_field = 'patent_name' elsif selected_field_int == 4 #Technology Transfer if selected_academy asia_data = AsiaTecTransfer.where(:asia_teacher_id.in=>selected_academy.asia_teacher_ids) else asia_data = AsiaTecTransfer.all end search_field = 'tech_transfer_name' elsif selected_field_int == 5 #Exhibition if selected_academy asia_data = AsiaExhibition.where(:asia_teacher_id.in=>selected_academy.asia_teacher_ids) else asia_data = AsiaExhibition.all end search_field = 'exhibition_name' end asia_data = asia_data.sort_order if params[:keywords].present? asia_data = filter_keywords(asia_data,search_field,params[:keywords]) end if for_frontend asia_data = asia_data.page(params[:page_no]).per(OrbitHelper.page_data_count) end asia_data_list = [] if !params['keywords'].nil? if for_frontend asia_data_list = asia_data.collect do |asia_record| {'jps' => fields_to_show.map{|field| {"value"=> get_display_field(asia_record,field)}}} end else all_count = asia_data.count store_record_id = @store_record.id asia_data_list = asia_data.collect.with_index do |asia_record, i| AsiaExportStore.where(:id=>store_record_id).update_all(:finish_percent=> ((i + 1) * 100.0 / all_count).round(1)) fields_to_show.map{|field| get_raw_field(asia_record, field)} end end end return asia_data_list, asia_data, table_headers, choice_unit, choice_field end def index params = OrbitHelper.params # page = OrbitHelper.page rescue Page.where(page_id: params[:page_id]).first asia_data_list, asia_data, table_headers, choice_unit, choice_field = get_data(params, true) download_excel_text = t('asia_database.extend_translate.download_excel') select_field_text = t('asia_database.extend_translate.search_field') select_text = t('asia_database.extend_translate.search_class') search_text = t('asia_database.extend_translate.word_to_search') @_request = OrbitHelper.request csrf_value = form_authenticity_token extras = {} #{"th-academy" => I18n.t("asia_database.academy"), "th-department" => I18n.t("asia_database.department"), "th-teacher" => I18n.t("asia_database.teacher"), "th-discipline_expertise" => I18n.t("asia_database.discipline_expertise"), "th-asia_project.project_name" => I18n.t("asia_database.asia_project.project_name"), "th-asia_project.start_date" => I18n.t("asia_database.asia_project.start_date"), "th-asia_project.sponsor" => I18n.t("asia_database.asia_project.sponsor"), "th-asia_paper.paper_name" => I18n.t("asia_database.asia_paper.paper_name"), "th-asia_paper.issue_year" => I18n.t("asia_database.asia_paper.issue_year"), "th-asia_paper.journal_name" => I18n.t("asia_database.asia_paper.journal_name"), "th-asia_patent.patent_name" => I18n.t("asia_database.asia_patent.patent_name"), "th-asia_patent.certification_date" => I18n.t("asia_database.asia_patent.certification_date"), "th-asia_patent.patent_country" => I18n.t("asia_database.asia_patent.patent_country"), "th-asia_tec_transfer.tech_transfer_name" => I18n.t("asia_database.asia_tec_transfer.tech_transfer_name"), "th-asia_tec_transfer.start_date" => I18n.t("asia_database.asia_tec_transfer.start_date"), "th-asia_tec_transfer.receiving_unit" => I18n.t("asia_database.asia_tec_transfer.receiving_unit"), "th-asia_exhibition.exhibition_name" => I18n.t("asia_database.asia_exhibition.exhibition_name"), "th-asia_exhibition.exhibition_date" => I18n.t("asia_database.asia_exhibition.exhibition_date"), "th-asia_exhibition.host" => I18n.t("asia_database.asia_exhibition.host")} tmp_id = Time.now.to_i.to_s + '-' + csrf_value.gsub(/[\n\/]/, '') extras = extras.merge({ 'url' => '/' + I18n.locale.to_s + params[:url], 'download_url' => "/xhr/asia_database/download_excel?tmp_id=#{tmp_id}", 'export_url' => '/xhr/asia_database/export_excel', 'check_url' => "/xhr/asia_database/get_store_status?tmp_id=#{tmp_id}", 'download_excel_text' => download_excel_text, 'select_field_text' => select_field_text, 'select_text' => select_text, 'search_text' => search_text, 'search_value' => params[:keywords].to_s.gsub(/\"/,''), 'search_trans' => t('asia_database.extend_translate.search'), 'keywords_trans' => t('asia_database.extend_translate.keywords'), 'clear_trans' => t('asia_database.extend_translate.clear'), 'export_trans' => t('asia_database.extend_translate.export'), 'preparing_trans' => t('asia_database.extend_translate.preparing'), 'download_trans' => t('asia_database.extend_translate.download'), 'unit_trans' => t('asia_database.extend_translate.unit'), 'csrf_value' => csrf_value, 'tmp_id' => tmp_id }) extras["widget-title"] = I18n.t("module_name.asia_database") { "asia_databases" => asia_data_list, "headers" => table_headers, "extras" => extras, "total_pages" => asia_data.total_pages, 'choice_unit' => choice_unit, 'choice_field' => choice_field } end def export_excel tmp_id = params['tmp_id'] if tmp_id.blank? render(:nothing => true) and return end AsiaExportStore.where(:tmp_id.ne=>tmp_id, :updated_at.lte=> Time.now.to_i - 3600 * 24).destroy filter_str = params.except('action', 'controller', 'tmp_id').to_query @store_record = AsiaExportStore.any_of([{:tmp_id=>tmp_id}, {:filter_str=>filter_str}]).first if @store_record.nil? @store_record = AsiaExportStore.new(:tmp_id=>tmp_id) else if @store_record.filter_str == filter_str render :json => {success: true} and return end @store_record.reinit end @store_record.filter_str = filter_str @store_record.save Thread.new do begin @asia_data_list, asia_data, @table_headers, choice_unit, choice_field = get_data(params) store_record_id = @store_record.id AsiaExportStore.where(:id=>store_record_id).update_all(:status=>1) @excel_title = I18n.t("module_name.asia_database") excel_contents = render_to_string( handlers: [:axlsx], formats: [:xlsx] ,partial: 'export_excel.xlsx',locals: {:@asia_data_list=> @asia_data_list, :@table_headers=> @table_headers, :@excel_title=> @excel_title} ) File.open(@store_record.file_path, 'w') do |f| f.write excel_contents end AsiaExportStore.where(:id=>store_record_id).update_all(:status=>2) rescue => e puts [e.to_s, e.backtrace] end end render :json => {success: true} end def download_excel tmp_id = params['tmp_id'] @store_record = AsiaExportStore.where(:tmp_id=>tmp_id).first if @store_record.nil? || @store_record.status != 2 render_404 else send_file(@store_record.file_path,:filename=>'export_excel.xlsx') end end def get_store_status tmp_id = params['tmp_id'] @store_record = AsiaExportStore.where(:tmp_id=>tmp_id).first if @store_record.nil? render :json => {:finish_percent=> 0, :status=> 0} else render :json => {:finish_percent=> @store_record.finish_percent, :status=> @store_record.status} end end def show params = OrbitHelper.params plugin = AsiaTeacher.where(:is_hidden=>false).find_by(uid: params[:uid].to_s) fields_to_show = ["academy", "department", "teacher", "discipline_expertise", "asia_project.project_name", "asia_project.start_date", "asia_project.sponsor", "asia_paper.paper_name", "asia_paper.issue_year", "asia_paper.journal_name", "asia_patent.patent_name", "asia_patent.certification_date", "asia_patent.patent_country", "asia_tec_transfer.tech_transfer_name", "asia_tec_transfer.start_date", "asia_tec_transfer.receiving_unit", "asia_exhibition.exhibition_name", "asia_exhibition.exhibition_date", "asia_exhibition.host"] {"plugin_datas"=>plugin.get_plugin_data(fields_to_show)} end def get_raw_field(asia_teacher, field) text_only = false value = asia_teacher.send(field) rescue "" if field.include?(".") value = asia_teacher field.split(".").each{|f| value = value.send(f) rescue nil} end if value.class == Array value = value.join("\n") end return value end def get_display_field(model,field) text_only = false value = "" if field.include?(".") field_level = field.split(".") field_level[0...-1].each do |f| model = model.send(f) rescue model end if field_level[-1] == 'teacher' value = teacher_frontend_text(model) else value = model.send(field_level[-1]) end else if field == 'teacher' value = teacher_frontend_text(model) else value = model.send(field) end end if value.class == Array tmp = '