433 lines
19 KiB
Ruby
433 lines
19 KiB
Ruby
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?
|
|
asia_data = nil
|
|
else
|
|
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 ? asia_data.total_pages : 0),
|
|
'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 = '<ul>'
|
|
value.each do |v|
|
|
tmp += "<li>#{v}</li>"
|
|
end
|
|
tmp += '</ul>'
|
|
value = tmp
|
|
end
|
|
# file_fields = []
|
|
# link_fields = []
|
|
# member_fields = []
|
|
# if file_fields.include?(field)
|
|
# files = asia_teacher.send(field.pluralize)
|
|
# value = files.map do |file|
|
|
# url = file.file.url
|
|
# title = (file.title.blank? ? File.basename(file.file.path) : file.title)
|
|
# "<li><a href='#{url}'' target='_blank'>#{title}</li>"
|
|
# end
|
|
# value = value.join("")
|
|
# elsif link_fields.include?(field)
|
|
# links = asia_teacher.send(field.pluralize)
|
|
# value = links.map do |link|
|
|
# url = link.url
|
|
# title = (link.title.blank? ? url : link.title)
|
|
# "<li><a href='#{url}'' target='_blank'>#{title}</li>"
|
|
# end
|
|
# value = value.join("")
|
|
# elsif member_fields.include?(field)
|
|
# members = asia_teacher.send(field.pluralize)
|
|
# value = members.map{|m|
|
|
# path = OrbitHelper.url_to_plugin_show(m.to_param, 'member') rescue '#'
|
|
# ((text_only rescue false) ? m.name : "<a href='#{path}'>#{m.name}</a>")
|
|
# }
|
|
# join_text = (text_only rescue false) ? "," : "<br>"
|
|
# value = value.join(join_text)
|
|
# elsif field == "authors"
|
|
# value = get_authors_show(asia_teacher)
|
|
# end
|
|
# strftime_hash = {}
|
|
# if strftime_hash.keys.include?(field)
|
|
# value = value.strftime(strftime_hash[field]) rescue value
|
|
# end
|
|
# if field == "teacher"
|
|
# link = OrbitHelper.url_to_plugin_show(asia_teacher.to_param,'asia_database')
|
|
# tmp_title = value
|
|
# url_to_plugin_show_blank = OrbitHelper.instance_variable_get(:@url_to_plugin_show_blank)
|
|
# value = url_to_plugin_show_blank ? tmp_title : "<a href='#{link}' target='_blank' title='#{tmp_title}'>#{tmp_title}</a>"
|
|
# end
|
|
return value
|
|
end
|
|
def get_fields_for_index
|
|
@page = Page.find(params[:page_id]) rescue nil
|
|
@fields_to_show = ["sid", "academy", "department", "teacher", "url", "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"]
|
|
@fields_to_show = @fields_to_show.map { |fs| [t("asia_database.#{fs}"), fs] }
|
|
if @page.present? && @page.custom_string_field == 'table'
|
|
@default_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"]
|
|
else
|
|
@default_fields_to_show = ["year", "teacher"]
|
|
end
|
|
render layout: false
|
|
end
|
|
|
|
def save_index_fields
|
|
page = Page.find(params[:page_id]) rescue nil
|
|
page.custom_array_field = params[:keys]
|
|
page.save
|
|
render json: { 'success' => true }.to_json
|
|
end
|
|
def filter_keywords(asia_data,select_field,keywords)
|
|
# member_fields = []
|
|
# file_fields = []
|
|
# link_fields = []
|
|
# if select_field == nil
|
|
# asia_data = asia_data.where(:slug_title=>/#{gsub_invalid_character(keywords)}/)
|
|
# elsif select_field == "member_profile"
|
|
# ms = MemberProfile.all.select{|m| m.name.include?(keywords)}
|
|
# asia_data = asia_data.where(:member_profile_id.in=>ms.map{|m| m.id})
|
|
# elsif member_fields.include?(select_field)
|
|
# ms = MemberProfile.all.select{|m| m.name.include?(keywords)}
|
|
# m_ids = ms.map{|m| m.id.to_s }
|
|
# tmp_asia_data = asia_data.select{|p| (p.send("#{select_field.singularize}_ids") & m_ids).count != 0}
|
|
# asia_data = asia_data.where(:id.in=>tmp_asia_data.map{|p| p.id})
|
|
if select_field.split(".").count > 1
|
|
relate_name = select_field.split(".").first
|
|
field_name = select_field.split(".").last.gsub(/^\$+/, '')
|
|
relate = relate_name.camelize.constantize
|
|
relate_ids = relate.where(field_name=>/#{gsub_invalid_character(keywords)}/).pluck(:id)
|
|
asia_data = asia_data.where("#{relate_name.singularize}_id"=>{'$in'=>relate_ids})
|
|
elsif (asia_data.klass.fields[select_field].options[:type] == Date rescue false)
|
|
keywords = keywords.split(/[\/\-]/)
|
|
if keywords.count > 1
|
|
Date.parse(keywords.join("/"))
|
|
else
|
|
start_date = Date.parse(keywords[0] + "/1/1")
|
|
end_date = Date.parse(keywords[0] + "/12/31")
|
|
asia_data = asia_data.where(select_field=>{'$gte'=>start_date,'$lte'=>end_date})
|
|
end
|
|
elsif (asia_data.klass.fields[select_field].options[:type] == DateTime rescue false)
|
|
keywords = keywords.split(/[\/\-]/)
|
|
if keywords.count > 1
|
|
DateTime.parse(keywords.join("/"))
|
|
elsif keywords[0].include?(":")
|
|
tmp_asia_data = asia_data.select{|p| (p.send(select_field).strftime('%Y/%m/%d %H:%M').include?(keywords[0]) rescue false)}
|
|
asia_data = asia_data.where(:id.in=>tmp_asia_data.map{|p| p.id})
|
|
else
|
|
start_date = DateTime.parse(keywords[0] + "/1/1 00:00")
|
|
end_date = DateTime.parse(keywords[0] + "/12/31 23:59")
|
|
asia_data = asia_data.where(select_field=>{'$gte'=>start_date,'$lte'=>end_date})
|
|
end
|
|
elsif (asia_data.klass.fields[select_field].options[:type] == Integer rescue false)
|
|
tmp_asia_data = asia_data.select{|p| p.send(select_field).to_s.include?(keywords)}
|
|
asia_data = asia_data.where(:id.in=>tmp_asia_data.map{|p| p.id})
|
|
# elsif file_fields.include?(select_field)
|
|
# file_field = select_field.camelize.constantize
|
|
# ids1 = file_field.where(:file=>/#{gsub_invalid_character(keywords)}/).pluck(:id)
|
|
# ids2 = file_field.where(:title=>/#{gsub_invalid_character(keywords)}/).pluck(:id)
|
|
# ids = ids1 + ids2
|
|
# tmp_asia_data = asia_data.select{|p| (p.send("#{select_field}_ids") & ids).count != 0}
|
|
# asia_data = asia_data.where(:id.in=>tmp_asia_data.map{|p| p.id})
|
|
# elsif link_fields.include?(select_field)
|
|
# link_field = select_field.camelize.constantize
|
|
# ids1 = link_field.where(:title=>/#{gsub_invalid_character(keywords)}/).pluck(:id)
|
|
# ids2 = link_field.where(:url=>/#{gsub_invalid_character(keywords)}/).pluck(:id)
|
|
# ids = ids1 + ids2
|
|
# tmp_asia_data = asia_data.select{|p| (p.send("#{select_field}_ids") & ids).count != 0}
|
|
# asia_data = asia_data.where(:id.in=>tmp_asia_data.map{|p| p.id})
|
|
else
|
|
if asia_data.klass == AsiaTeacher
|
|
asia_data = asia_data.any_of([
|
|
{
|
|
select_field=> /#{gsub_invalid_character(keywords)}/
|
|
},{
|
|
teacher => /#{gsub_invalid_character(keywords)}/
|
|
}
|
|
])
|
|
else
|
|
asia_data = asia_data.any_of([
|
|
{
|
|
select_field=> /#{gsub_invalid_character(keywords)}/
|
|
},{
|
|
:asia_teacher_id.in => AsiaTeacher.where(teacher: /#{gsub_invalid_character(keywords)}/).pluck(:id)
|
|
}
|
|
])
|
|
end
|
|
end
|
|
return asia_data
|
|
end
|
|
def gsub_invalid_character(text)
|
|
::Regexp.escape(text.to_s)
|
|
end
|
|
def teacher_frontend_text(value)
|
|
tmp_value = value.teacher
|
|
"<a class=\"teacher_url link-primary\" target=\"_blank\" href=\"#{value.url}\" title=\"#{tmp_value}\">#{tmp_value}</a>"
|
|
end
|
|
end |