# encoding: utf-8
class Admin::CancerpredictsController < OrbitAdminController
require 'spreadsheet'
require 'rubyXL'
require 'fileutils'
require "axlsx"
require "csv"
#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
@app_title = "cancerpredict"
end
def index
@tags = @module_app.tags
@categories = @module_app.categories.enabled
@filter_fields = filter_fields(@categories, @tags)
@table_fields = [:status, :category, :title, :start_date, :end_date, :last_modified]
@head_images_id = @form_to_show.head_images_id
@title_images_id = @form_to_show.title_images_id
@head_images = []
@title_images = []
@head_images_id.each{|image_id| @head_images.push Headimages.find_by(:id=>image_id.to_s)} rescue nil
@title_images_id.each{|image_id| @title_images.push Headimages.find_by(:id=>image_id.to_s)} rescue nil
@head_new_image = Headimages.new(:cancerpredictfields_id => @form_to_show.id)
end
def edit
other_in_use_locales = Site.first.in_use_locales.map{|l| l.to_s}
other_in_use_locales.delete(params[:locale])
if !(params["cancerpredictfields"].nil?)
org_nums = @form_to_show.form_show_was.keys
remain_org_nums = params["cancerpredictfields"]["form_show"].values.map{|property| property["old_num"]}.select{|n| n.present?}
delete_nums = org_nums - remain_org_nums
if !delete_nums.blank?
delete_nums.each do |delete_num|
@form_to_show.form_show.delete(delete_num)
end
end
params["cancerpredictfields"]["form_show"].each do |num,property|
property.each do |key,value|
if key.include?("_file") && (!value[:temp_file].blank? rescue false)
if value[:id].nil?
mapping_file = CancerPredictMappingFile.create(value)
else
mapping_file = CancerPredictMappingFile.find(value[:id])
mapping_file.temp_file = value[:temp_file]
mapping_file.save
end
@form_to_show.form_show[num.to_s][key.to_s] = mapping_file.id
elsif key.include?("_file")
if value[:_destroy] == "1"
mapping_file = CancerPredictMappingFile.find(value[:id])
mapping_file.destroy
@form_to_show.form_show[num.to_s][key.to_s] = ""
end
end
next if key.include?("_file")
if (key != "old_num")
if value != "0" && value != "1"
if key != params[:locale]
@form_to_show.form_show[num.to_s] = {} if @form_to_show.form_show[num.to_s].nil?
type = Cancerpredictfields::FIELDINFO[key.to_s].constantize rescue String
if @form_to_show.form_show[num.to_s][key.to_s].class != Array && type != Array
@form_to_show.form_show[num.to_s][key.to_s] = value
else
if value.length > 2
@form_to_show.form_show[num.to_s][key.to_s] = YAML.load(value)
else
@form_to_show.form_show[num.to_s][key.to_s] = []
end
end
else
value.each do |sub_property,sub_value|
type = Cancerpredictfields::FIELDINFO[sub_property.to_s].constantize rescue String
@form_to_show.form_show[ num.to_s ][ sub_property ] = {} if @form_to_show.form_show[num.to_s][ sub_property ].nil?
if @form_to_show.form_show[ num.to_s ][ sub_property ][params[:locale].to_s].class != Array && type != Array
@form_to_show.form_show[ num.to_s ][ sub_property ][params[:locale].to_s] = sub_value
else
if sub_value.length > 2
@form_to_show.form_show[ num.to_s ][ sub_property ][params[:locale].to_s] = YAML.load(sub_value)
else
@form_to_show.form_show[ num.to_s ][ sub_property ][params[:locale].to_s] = []
end
end
if(property["old_num"].present? && property["old_num"] != num.to_s)
other_in_use_locales.each do |locale|
@form_to_show.form_show[ num.to_s ][ sub_property ][locale] = @form_to_show.form_show_was[ property["old_num"] ][ sub_property ][locale]
end
end
end
end
else
@form_to_show.form_show[num.to_s][key.to_s] = value.to_i
end
end
end
end
largest_num = org_nums.map{|n| n.to_i}.sort.last
if (params["cancerpredictfields"]["form_show"].keys.last.to_i + 1 ) <= largest_num
( (params["cancerpredictfields"]["form_show"].keys.last.to_i + 1 ) .. (largest_num)).each do |i|
@form_to_show.form_show.delete i.to_s
end
end
if !params["cancerpredictfields"]["form_show_in_result"].nil?
org_nums = @form_to_show.form_show_in_result_was.keys
remain_org_nums = params["cancerpredictfields"]["form_show_in_result"].values.map{|property| property["old_num"]}.select{|n| n.present?}
delete_nums = org_nums - remain_org_nums
if !delete_nums.blank?
delete_nums.each do |delete_num|
@form_to_show.form_show_in_result.delete(delete_num)
end
end
params["cancerpredictfields"]["form_show_in_result"].each do |num,property|
property.each do |key,value|
if key.include?("_file") && (!value[:temp_file].blank? rescue false)
if value[:id].nil?
mapping_file = CancerPredictMappingFile.create(value)
else
mapping_file = CancerPredictMappingFile.find(value[:id])
mapping_file.temp_file = value[:temp_file]
mapping_file.save
end
@form_to_show.form_show[num.to_s][key.to_s] = mapping_file.id
elsif key.include?("_file")
if value[:_destroy] == "1"
mapping_file = CancerPredictMappingFile.find(value[:id])
mapping_file.destroy
@form_to_show.form_show[num.to_s][key.to_s] = ""
end
end
next if key.include?("_file")
if (key != "old_num")
if value != "0" && value != "1"
if key != params[:locale]
if @form_to_show.form_show_in_result[num.to_s][key.to_s].class != Array
@form_to_show.form_show_in_result[num.to_s][key.to_s] = value
else
if value.length > 2
@form_to_show.form_show_in_result[num.to_s][key.to_s] = YAML.load(value)
else
@form_to_show.form_show_in_result[num.to_s][key.to_s] = []
end
end
else
value.each do |sub_property,sub_value|
if @form_to_show.form_show_in_result[ num.to_s ][ sub_property ][params[:locale].to_s].class != Array
@form_to_show.form_show_in_result[ num.to_s ][ sub_property ][params[:locale].to_s] = sub_value
else
if sub_value.length > 2
@form_to_show.form_show_in_result[ num.to_s ][ sub_property ][params[:locale].to_s] = YAML.load(sub_value)
else
@form_to_show.form_show_in_result[ num.to_s ][ sub_property ][params[:locale].to_s] = []
end
end
if(property["old_num"] != nil && property["old_num"] != num.to_s)
other_in_use_locales.each do |locale|
@form_to_show.form_show_in_result[ num.to_s ][ sub_property ][locale] = @form_to_show.form_show_in_result_was[ property["old_num"] ][ sub_property ][locale]
end
end
end
end
else
@form_to_show.form_show_in_result[num.to_s][key.to_s] = value.to_i
end
end
end
end
largest_num = org_nums.map{|n| n.to_i}.sort.last
if (params["cancerpredictfields"]["form_show_in_result"].keys.last.to_i + 1 ) <= largest_num
( (params["cancerpredictfields"]["form_show_in_result"].keys.last.to_i + 1 ) .. (largest_num)).each do |i|
@form_to_show.form_show_in_result.delete i.to_s
end
end
end
@create_items = ['title_texts','form_result_is_right','text_descibe','years','table_above_texts','text_above_texts','surgery_only_texts','extra_texts','extra_therapy_texts','danger_texts','texts_between_Result_and_result_block','prediction_formula']
params_cancer = params.require("cancerpredictfields").permit!
@create_items.each do |item|
if (@form_to_show[item].class == BSON::Document) || (@form_to_show.send(item).class == Hash)
item_hash = @form_to_show[item]
item_hash = item_hash.merge(params_cancer[item])
@form_to_show[item] = item_hash
elsif @form_to_show[item].class == Array
@form_to_show[item] = YAML.load(params_cancer[item])
@form_to_show[item] = [] if @form_to_show[item].class != Array
else
@form_to_show[item] = params_cancer[item]
end
end
@size = ['small','medium','large']
@size.each{|size| @form_to_show[size] = params["cancerpredictfields"][size]}
@file_path = Rails.root.to_s + '/app/assets/images/predict_tool'
if !Dir.exist? @file_path
FileUtils.mkdir_p @file_path
end
@images = params["cancerpredictfields"]['head_images']
@head_images_id = @form_to_show.head_images_id
@delete_index = []
if !@images.nil?
@images.each do |num,image|
if num.to_i < @head_images_id.length && @head_images_id.length != 0
if image['remove_image'] == "1"
@delete_index.push num.to_i
else
@image_id = @head_images_id[num.to_i]
@image = Headimages.find_by(:id=>@image_id.to_s)
if !image['sort_number'].nil?
@image.sort_number = image['sort_number'].to_i
@image.save
end
next if image['temp_file'] == nil
@file_name = image['temp_file'].original_filename.gsub('(','_').gsub(')','_').gsub(' ','_') rescue next
@image.sort_number = image['sort_number'].to_i
@image.temp_file = image['temp_file']
@image.save
end
else
@file_name = image['temp_file'].original_filename.gsub('(','_').gsub(')','_').gsub(' ','_') rescue next
@headimages = Headimages.new(:cancerpredictfields_id => @form_to_show.id,:title => @app_title+'head_images')
@headimages.temp_file = image['temp_file']
@headimages.sort_number = image['sort_number'].to_i if !image['sort_number'].nil?
@headimages.save
@form_to_show.head_images_id.push @headimages.id
end
end
end
@delete_index.reverse!
@delete_index.each do |i|
@image_id = @head_images_id[i.to_i]
Headimages.find_by(:id => @image_id ).destroy rescue next
@form_to_show.head_images_id.delete(@image_id)
end
@images = params["cancerpredictfields"]['title_images']
@title_images_id = @form_to_show.title_images_id
@delete_index = []
if !@images.nil?
@images.each do |num,image|
if num.to_i < @title_images_id.length && @title_images_id.length != 0
if image['remove_image'] == "1"
@delete_index.push num.to_i
else
@image_id = @title_images_id[num.to_i]
@image = Headimages.find_by(:id=>@image_id.to_s)
if !image['sort_number'].nil?
@image.sort_number = image['sort_number'].to_i
@image.save
end
next if image['temp_file'] == nil
@file_name = image['temp_file'].original_filename.gsub('(','_').gsub(')','_').gsub(' ','_') rescue next
@image.sort_number = image['sort_number'].to_i
@image.temp_file = image['temp_file']
@image.save
end
else
@file_name = image['temp_file'].original_filename.gsub('(','_').gsub(')','_').gsub(' ','_') rescue next
@headimages = Headimages.new(:cancerpredictfields_id => @form_to_show.id,:title => @app_title+'title_images' )
@headimages.temp_file = image['temp_file']
@headimages.sort_number = image['sort_number'].to_i if !image['sort_number'].nil?
@headimages.save
@form_to_show.title_images_id.push @headimages.id
end
end
end
@delete_index.reverse!
@delete_index.each do |i|
@image_id = @title_images_id[i.to_i]
Headimages.find_by(:id => @image_id ).destroy rescue next
@form_to_show.title_images_id.delete(@image_id)
end
file_ids = @form_to_show.form_show.values.map{|property| [property[:variable],property[:cancer_predict_mapping_file]]}.select{|k,f| f.present?}.to_h
mapping_data_from_csv = {}
if !file_ids.blank?
file_ids.each do |k,v|
mapping_data_from_csv[k] = read_mapping_file(v)
end
end
@form_to_show.mapping_data_from_csv = mapping_data_from_csv.to_json
@form_to_show.save
fork do
@form_to_show.auto_write_predict_js
end
end
@index = 0
Dir.chdir("public") do
while File.exist?('cancerfield_back'+@index.to_s+'.txt')
@index += 1
end
end
Dir.chdir("public") do
@site_locales = Site.last.in_use_locales.each do |locale|
I18n.with_locale(locale) do
@file_tmp = File.new('cancer_tool_table_tmp_'+locale.to_s+'.txt', 'w')
tmp_table_texts = create_table(locale)
@file_tmp.write(tmp_table_texts)
@file_tmp.close
end
end
end
Dir.chdir("public") do
@file_back = File.open('cancerfield_back'+@index.to_s+'.txt', 'w')
@file_back.write(@form_to_show.attributes)
@file_back.close
@file_org = File.open('cancerfield_org'+@index.to_s+'.txt', 'w')
@file_org.write(Cancerpredictfields.where("title"=>@app_title+'_back').first.attributes)
@file_org.close
end
redirect_to admin_cancerpredicts_path
end
def create_table(current_locale)
create_first_field
current_site = Site.last
@size = {}
@size_name = ['small','medium','large']
@size_name.each{|name| @size[name] = @form_to_show[name]['font_size']}
@size_active_size = ''
@size.each{|size_key,size_value| (@form_to_show[size_key]['active'].to_i == 1) ? ( @size_active_size = size_value ): nil }
@table_str = '
'
@table_str += '
'+@form_to_show.text_descibe[current_locale] +'
'
@table_str += '
'+''+t('cancerpredict.font_size').titleize+': '
@size.each{|size_key,size_value| @table_str += (''+t('cancerpredict.'+size_key).titleize+' ' )}
@table_str += '
'
@table_str_left = '
'
@table_str_right = '
'
@form_to_show.form_show.each do |num,property|
@field_property = {}
property.each do |key,value|
@value= value
@disp_value
if @value.class == BSON::Document || @value.class == Hash
@disp_value = @value[current_locale] rescue ""
else
@disp_value = @value
end
@disp_value = "" if @disp_value.nil?
@field_property[key] = @disp_value
end
if @field_property['right'] == 0
next if @field_property["name"].blank?
@table_str_left += '
'
@table_str_left += '
'
@table_str_left += @field_property["name"]
@table_str_left += ' '
if @field_property["comment_text"] != ""
@table_str_left += '
'
@table_str_left += '
'
else
@site_locales = current_site.in_use_locales
@site_locales.delete(current_locale)
@site_locales.each do |locale|
next if property["comment_text"][locale.to_s] == ""
@field_property["comment_text"] = property["comment_text"][locale.to_s]
break
end
if @field_property["comment_text"].present?
@table_str_left += '
'
@table_str_left += '
'
else
@table_str_left += '
'
end
end
if @field_property["is_num"] == 1
if @field_property["is_float"] == 1
@table_str_left += '
'
else
if @field_property["range"] == []
@table_str_left += '
'
else
@table_str_left += '
'
end
end
else
@table_str_left += '
'
@field_property["choice_fields"].each do |create_choice|
@table_str_left += ''+create_choice.titleize+' '
@table_str_left += ' '
end
@table_str_left += '
'
end
if @field_property["hint"].to_s != ""
@table_str_left +='
'+@field_property["hint"]+'
'
end
@table_str_left += '
'
else
@table_str_right += '
'
break if @field_property["name"] == ""
@table_str_right += '
'
@table_str_right += @field_property["name"]
@table_str_right += ' '
if @field_property["comment_text"] != ""
@table_str_right += '
'
@table_str_right += '
'
else
@site_locales = current_site.in_use_locales
@site_locales.delete(current_locale)
@site_locales.each do |locale|
next if property["comment_text"][locale.to_s] == ""
@field_property["comment_text"] = property["comment_text"][locale.to_s]
break
end
if @field_property["comment_text"] != ""
@table_str_right += '
'
@table_str_right += '
'
else
@table_str_right += '
'
end
end
if @field_property["is_num"] == 1
if @field_property["is_float"] == 1
@table_str_right += '
'
else
if @field_property["range"] == []
@table_str_left += '
'
else
@table_str_right += '
'
end
end
else
@table_str_right += '
'
@field_property["choice_fields"].each do |create_choice|
@table_str_right += ''+create_choice.titleize+' '
@table_str_right += ' '
end
@table_str_right += '
'
end
if @field_property["hint"].to_s != ""
@table_str_right +='
'+@field_property["hint"]+'
'
end
@table_str_right += '
'
end
end
@table_str_left += '
'
@table_str_right += '
'
@table_str += (@table_str_left+@table_str_right)
@table_str +='
'
@table_result_str = '
'+t("cancerpredict.table.result").to_s+'
'
@tab_name = ['table','text']
@table_result_str +='
' + @form_to_show.texts_between_Result_and_result_block[current_locale]+'
'
@table_result_str += '
'
@tab_name.each_with_index{|name,index| @table_result_str += ('
')}
@table_result_str += '
'
@tab_name.each{|name| @table_result_str += '
'}
@table_result_str += '
'
@table_button = '
')
@table_result_choice_fileds = '' if @form_to_show.form_result_is_right == 1
@table_result_choice_fileds = '
' if @form_to_show.form_result_is_right == 0
@table_result_choice_fileds += '
'+t('cancerpredict.table.Therapy_choice').to_s+'
'
@table_result_choice_fileds += '
'
@form_to_show.form_show_in_result.each do |num,property|
@field_property = {}
property.each do |key,value|
@value= value
if @value.class == BSON::Document || @value.class == Hash
@disp_value = @value[current_locale.to_s] rescue ""
else
@disp_value = @value
end
@field_property[key] = @disp_value
end
@table_result_choice_fileds += '
'
break if @field_property["name"] == ""
@table_result_choice_fileds += '
'
@table_result_choice_fileds += @field_property["name"]
@table_result_choice_fileds += ' '
if @field_property["comment_text"] != ""
@table_result_choice_fileds += '
'
@table_result_choice_fileds += '
'
else
@site_locales = current_site.in_use_locales
@site_locales.delete(current_locale)
@site_locales.each do |locale|
next if property["comment_text"][locale.to_s] == ""
@field_property["comment_text"] = property["comment_text"][locale.to_s]
break
end
if @field_property["comment_text"] != ""
@table_result_choice_fileds += '
'
@table_result_choice_fileds += '
'
else
@table_result_choice_fileds += '
'
end
end
if @field_property["is_num"] == 1
if @field_property["is_float"] == 1
@table_result_choice_fileds += '
'
else
if @field_property["range"] == []
@table_str_left += '
'
else
@table_result_choice_fileds += '
'
@table_str_left += '
'
@please_choice = (current_locale.to_s == "zh_tw") ? "請選擇" : "Please choice"
@table_result_choice_fileds += '
'+@please_choice+' '
if @field_property["choice_fields"] != []
@field_property["choice_fields"].each do |choice|
@table_result_choice_fileds += ('' + choice.to_s.titleize + ' ')
end
end
for @num in @field_property["range"][0] .. @field_property["range"][1]
@table_result_choice_fileds += ('' + @num.to_s + ' ')
end
@table_result_choice_fileds += ''
end
end
else
@table_result_choice_fileds += '
'
@field_property["choice_fields"].each do |create_choice|
@table_result_choice_fileds += ''+create_choice.titleize+' '
@table_result_choice_fileds += ' '
end
@table_result_choice_fileds += '
'
end
if @field_property["hint"].to_s != ""
@table_result_choice_fileds +='
'+@field_property["hint"]+'
'
end
@table_result_choice_fileds += '
'
end
@table_result_choice_fileds += '
'
@table_str += (@table_button+'
'+@table_result_str+@table_result_choice_fileds+'
'+'
')
return @table_str
end
def showSubmit
@page_num = params[:page] || 1
@results = Cancerpredictrecord.where("title"=>@app_title).desc(:id).page(@page_num).per(10)
@variables = []
@names = []
@form_to_show.form_show.each do |num,property|
property.each do |key,value|
if key == "variable"
@variables.push value
elsif key == "name"
@names.push value[I18n.locale.to_s]
end
end
end
@result_variables = []
@result_names = []
@indexs=[]
@choicenames=[]
if @results.length != 0
@results[0].result.each{|key,value| @result_variables.push key} rescue nil
@result_variables.each do |variable|
@index = @variables.index(variable.to_s)
@result_names.push @names[@index] if !@index.nil?
@indexs.push @index if !@index.nil?
end
@indexs.each do |index|
if @form_to_show.form_show[index.to_s]["is_num"] == 1 || @form_to_show.form_show[index.to_s]["is_float"] == 1
@choicenames.push ""
else
@choicenames.push @form_to_show.form_show[index.to_s]["choice_fields"][I18n.locale.to_s]
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
@form_to_show = Cancerpredictfields.new()
@form_to_show.title = @app_title +"_back"
@form_to_show.save
end
@form_to_show
if Cancerpredictfields.where("title"=>@app_title).take_while{true}.count == 0
@form_to_show = Cancerpredictfields.new()
@form_to_show.title=@app_title
@form_to_show.save
@form_to_show = Cancerpredictfields.where("title"=>@app_title).first
Dir.chdir("public") do
@site_locales = Site.last.in_use_locales.each do |locale|
I18n.with_locale(locale) do
@file_tmp = File.new('cancer_tool_table_tmp_'+locale.to_s+'.txt', 'w')
tmp_table_texts = create_table(locale)
@file_tmp.write(tmp_table_texts)
@file_tmp.close
end
end
end
else
@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) ? '['+I18n.t('cancerpredict.prev_page')+'] ' : ' ['+I18n.t('cancerpredict.prev_page')+'] ')
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) ? '['+page_num.to_s+'] ' : ' '+page_num.to_s+' ')}
else
(1..3).to_a.each{|page_num| pagination += ((page_num == page) ? '['+page_num.to_s+'] ' : ' '+page_num.to_s+' ')}
pagination +='... '
(page-3..page).to_a.each{|page_num| pagination += ((page_num == page) ? '['+page_num.to_s+'] ' : ' '+page_num.to_s+' ')}
end
(page+1..page+3).to_a.each{|page_num| pagination += ((page_num == page) ? '['+page_num.to_s+'] ' : ' '+page_num.to_s+' ')}
pagination +='... '
(all_page_num-2..all_page_num).to_a.each{|page_num| pagination += ((page_num == page) ? '['+page_num.to_s+'] ' : ' '+page_num.to_s+' ')}
else
(1..3).to_a.each{|page_num| pagination += ((page_num == page) ? '['+page_num.to_s+'] ' : ' '+page_num.to_s+' ')}
pagination +='... '
(page-3..page).to_a.each{|page_num| pagination += ((page_num == page) ? '['+page_num.to_s+'] ' : ' '+page_num.to_s+' ')}
if all_page_num - page <= 7
(page+1..all_page_num).to_a.each{|page_num| pagination += ((page_num == page) ? '['+page_num.to_s+'] ' : ' '+page_num.to_s+' ')}
else
(page+1..page+3).to_a.each{|page_num| pagination += ((page_num == page) ? '['+page_num.to_s+'] ' : ' '+page_num.to_s+' ')}
pagination +='... '
(all_page_num-2..all_page_num).to_a.each{|page_num| pagination += ((page_num == page) ? '['+page_num.to_s+'] ' : ' '+page_num.to_s+' ')}
end
end
else
(1..all_page_num).to_a.each{|page_num| pagination += ((page_num == page) ? '['+page_num.to_s+'] ' : ' '+page_num.to_s+' ')}
end
pagination += (((page==all_page_num) ? '['+I18n.t('cancerpredict.next_page')+'] ' : ' ['+I18n.t('cancerpredict.next_page')+'] ')+' ')
end
def read_mapping_file(mapping_file_id)
mapping_file = CancerPredictMappingFile.find(mapping_file_id) rescue nil
if !mapping_file.nil?
csv_rows = CSV.read(mapping_file.temp_file.file.path)
titles = csv_rows[0]
infos = {}
titles.each_with_index do |title,i|
infos[title] = []
csv_rows[1..-1].each do |row|
infos[title] << row[i].to_f
end
end
return infos
else
return {}
end
end
def auto_write_predict_js
js_code = "var map_values , mapping_hash , temp_index ,temp_value , index , closest_value;\r\n"
mapping_data_from_csv = YAML.load(@form_to_show.mapping_data_from_csv) rescue {}
#js_code += "\t\t\t\tvar mapping_data_from_csv = #{mapping_data_from_csv.to_s.gsub("=>",":")};\r\n"
variable_keys = []
@form_to_show.form_show.each do |num,property|
@variable = property[:variable]
if @variable.present?
if property[:is_num] == 1
js_code += "\t\t\t\tresult['#{@variable}'] = Number(result_json['#{@variable}']);\r\n"
elsif property[:choice_fields].present?
if property[:map_values].class == Array && property[:choice_fields].class == Array && property[:map_values].length == property[:choice_fields].length
js_code += "\t\t\t\tmap_values = #{property[:map_values]};\r\n"
js_code += "\t\t\t\tresult['#{@variable}'] = map_values[Number(result_json['#{@variable}'']) - 1];\r\n"
else
if property[:revert_value].to_i != 1
js_code += "\t\t\t\tresult['#{@variable}'] = Number(result_json['#{@variable}']) - 1;\r\n"
else
js_code += "\t\t\t\tresult['#{@variable}'] = (#{property[:choice_fields].length} - Number(result_json['#{@variable}']));\r\n"
end
end
end
variable_keys.push(@variable)
if property[:cancer_predict_mapping_file].present?
if (mapping_data_from_csv != {} && !mapping_data_from_csv[@variable].blank?)
variable_keys.concat(mapping_data_from_csv[@variable].keys)
js_code += "\t\t\t\tmapping_hash = mapping_data_from_csv['#{@variable}'];\r\n"
js_code += "\t\t\t\ttemp_index = 0;\r\n"
js_code += "\t\t\t\ttemp_value = result[#{@variable}];\r\n"
js_code += "\t\t\t\tindex = 0;
$.each(mapping_hash,function(k,v){
if( i == 0 ){
var index_val = v.indexOf(temp_value);
if( index_val != -1 ){
temp_index = index_val;
}else{
closest_value = v.get_nearest_value(temp_value);
temp_index = v.indexOf(closest_value)
}
}
result[k] = v[temp_index];
index++;
});\r\n"
end
end
end
end
formula = @form_to_show.prediction_formula.gsub("\r\n"," ").gsub("^","**")
variable_keys.each do |k|
formula = formula.gsub(/#{k}?(-|\+|\*|\s|\=)/){ "result[\"#{k}\"]#{$1}" }
end
formula_variables = formula.enum_for(:scan,/([^\=]*)?=/).map { Regexp.last_match[1] }.map{|s| s.strip.split(/(\s*|;\r\n)/).last}
js_code = "function calculate_first_lpv(result_json){
result = {};
#{js_code}
try{
result['lpv'] = (#{formula});
}catch(e){result['lpv'] = \"error\"};
console.log(result['lpv']);
#{formula_variables.map{|v| "result['lpv_variable']['#{v}'] = #{v};"}.join("\r\n\t\t\t\t") }
return result;
};
function calculate_and_change_result_value(obj){
obj.servive_ratio_arr = [];
for(var i = 0;i