# encoding: utf-8 class Admin::HncCancerPredictsController < OrbitAdminController require "spreadsheet" require "rubyXL" require "fileutils" require "axlsx" require "csv" #include Admin::HncCancerPredictsHelper before_action ->(module_app = @app_title) { set_variables module_app } before_action :create_first_field helper Admin::HncCancerPredictsHelper #before_action :load_access_levels def initialize super @app_title = "hnc_cancer_predict" end def yaml_load(v) YAML.load(v) end def titleize(v) if v.is_a?(String) if v.match(/[ \t]*\d/) v else v.titleize end else v.to_s end 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] @hnc_head_images_id = @form_to_show.hnc_head_images_id @title_images_id = @form_to_show.title_images_id @hnc_head_images = [] @title_images = [] @hnc_head_images_id.each { |image_id| @hnc_head_images.push HncHeadimages.find_by(:id => image_id.to_s) } rescue nil @title_images_id.each { |image_id| @title_images.push HncHeadimages.find_by(:id => image_id.to_s) } rescue nil @head_new_image = HncHeadimages.new(:hnc_cancer_predictfields_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["hnc_cancer_predictfields"].nil?) if !(params["hnc_cancer_predictfields"]["form_show"].nil?) params["hnc_cancer_predictfields"]["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 = HncCancerPredictMappingFile.create(value) else mapping_file = HncCancerPredictMappingFile.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 = HncCancerPredictMappingFile.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 key != params[:locale] @form_to_show.form_show[num.to_s] = {} if @form_to_show.form_show[num.to_s].nil? type = HncCancerPredictfields::FIELDINFO[key.to_s] if type && HncCancerPredictfields::Field_relations[type] type = HncCancerPredictfields::Field_relations[type] end type = type.constantize rescue String if type != Array if type == Fixnum @form_to_show.form_show[num.to_s][key.to_s] = value.to_i rescue 0 elsif type == Float @form_to_show.form_show[num.to_s][key.to_s] = value.to_f rescue 0.0 else @form_to_show.form_show[num.to_s][key.to_s] = value end 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 = HncCancerPredictfields::FIELDINFO[sub_property.to_s] if type && HncCancerPredictfields::Field_relations[type] type = HncCancerPredictfields::Field_relations[type] end type = type.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 type != Array if type == Fixnum @form_to_show.form_show[num.to_s][sub_property][params[:locale].to_s] = sub_value.to_i rescue 0 elsif type == Float @form_to_show.form_show[num.to_s][sub_property][params[:locale].to_s] = sub_value.to_f rescue 0.0 else @form_to_show.form_show[num.to_s][sub_property][params[:locale].to_s] = sub_value end else if sub_value.length > 2 puts [num, sub_property] @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 end end end org_nums = @form_to_show.form_show_was.keys remain_org_nums = params["hnc_cancer_predictfields"]["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 @form_to_show.form_show = @form_to_show.form_show.values.map.with_index { |v, i| [i.to_s, v] }.to_h else @form_to_show.form_show = {} end if !params["hnc_cancer_predictfields"]["form_show_in_result"].nil? params["hnc_cancer_predictfields"]["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 = HncCancerPredictMappingFile.create(value) else mapping_file = HncCancerPredictMappingFile.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 = HncCancerPredictMappingFile.find(value[:id]) mapping_file.destroy @form_to_show.form_show_in_result[num.to_s][key.to_s] = "" end end next if key.include?("_file") if (key != "old_num") if key != params[:locale] @form_to_show.form_show_in_result[num.to_s] = {} if @form_to_show.form_show_in_result[num.to_s].nil? type = HncCancerPredictfields::FIELDINFO[key.to_s] if type && HncCancerPredictfields::Field_relations[type] type = HncCancerPredictfields::Field_relations[type] end type = type.constantize rescue String if type != Array if type == Fixnum @form_to_show.form_show_in_result[num.to_s][key.to_s] = value.to_i rescue 0 elsif type == Float @form_to_show.form_show_in_result[num.to_s][key.to_s] = value.to_f rescue 0.0 else @form_to_show.form_show_in_result[num.to_s][key.to_s] = value end 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| type = HncCancerPredictfields::FIELDINFO[sub_property.to_s] if type && HncCancerPredictfields::Field_relations[type] type = HncCancerPredictfields::Field_relations[type] end type = type.constantize rescue String @form_to_show.form_show_in_result[num.to_s][sub_property] = {} if @form_to_show.form_show_in_result[num.to_s][sub_property].nil? if type != Array if type == Fixnum @form_to_show.form_show_in_result[num.to_s][sub_property][params[:locale].to_s] = sub_value.to_i rescue 0 elsif type == Float @form_to_show.form_show_in_result[num.to_s][sub_property][params[:locale].to_s] = sub_value.to_f rescue 0.0 else @form_to_show.form_show_in_result[num.to_s][sub_property][params[:locale].to_s] = sub_value end 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"].present? && 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_was[property["old_num"]][sub_property][locale] end end end end end end end org_nums = @form_to_show.form_show_in_result_was.keys remain_org_nums = params["hnc_cancer_predictfields"]["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 @form_to_show.form_show_in_result = @form_to_show.form_show_in_result.values.map.with_index { |v, i| [i.to_s, v] }.to_h else @form_to_show.form_show_in_result = {} 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", "hidden_variables", "advance_mode", "years_settings"] params_cancer = params.require("hnc_cancer_predictfields").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 if params_cancer[item].class != Array @form_to_show[item] = yaml_load(params_cancer[item]) rescue [] else @form_to_show[item] = params_cancer[item] end @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["hnc_cancer_predictfields"][size].to_h } @file_path = Rails.root.to_s + "/app/assets/images/predict_tool" if !Dir.exist? @file_path FileUtils.mkdir_p @file_path end @images = params["hnc_cancer_predictfields"]["hnc_head_images"] @hnc_head_images_id = @form_to_show.hnc_head_images_id @delete_index = [] if !@images.nil? @images.each do |num, image| if num.to_i < @hnc_head_images_id.length && @hnc_head_images_id.length != 0 if image["remove_image"] == "1" @delete_index.push num.to_i else @image_id = @hnc_head_images_id[num.to_i] @image = HncHeadimages.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 @hnc_headimages = HncHeadimages.new(:hnc_cancer_predictfields_id => @form_to_show.id, :title => @app_title + "hnc_head_images") @hnc_headimages.temp_file = image["temp_file"] @hnc_headimages.sort_number = image["sort_number"].to_i if !image["sort_number"].nil? @hnc_headimages.save @form_to_show.hnc_head_images_id.push @hnc_headimages.id end end end @delete_index.reverse! @delete_index.each do |i| @image_id = @hnc_head_images_id[i.to_i] HncHeadimages.find_by(:id => @image_id).destroy rescue next @form_to_show.hnc_head_images_id.delete(@image_id) end @images = params["hnc_cancer_predictfields"]["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 = HncHeadimages.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 @hnc_headimages = HncHeadimages.new(:hnc_cancer_predictfields_id => @form_to_show.id, :title => @app_title + "title_images") @hnc_headimages.temp_file = image["temp_file"] @hnc_headimages.sort_number = image["sort_number"].to_i if !image["sort_number"].nil? @hnc_headimages.save @form_to_show.title_images_id.push @hnc_headimages.id end end end @delete_index.reverse! @delete_index.each do |i| @image_id = @title_images_id[i.to_i] HncHeadimages.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 + @form_to_show.form_show_in_result.values).map { |property| [property[:variable], property[:hnc_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 @site_locales = Site.last.in_use_locales.each do |locale| I18n.with_locale(locale) do File.open(HncCancerPredictfields::ToolTableMap[I18n.locale], "w") do |f| tmp_table_texts = create_table(locale) f.write(tmp_table_texts) end end end idx = 0 while File.exist?("public/hnc_field_back#{idx}.txt") idx += 1 end File.open("public/hnc_field_back#{idx}.txt", "w") do |f| f.write(@form_to_show.attributes) end File.open("public/hnc_field_org#{idx}.txt", "w") do |f| f.write(HncCancerPredictfields.where("title" => @app_title + "_back").first.attributes) end redirect_to admin_hnc_cancer_predicts_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 = '