class Admin::OlympiamanagementsController < OrbitAdminController require 'spreadsheet' require 'rubyXL' require 'fileutils' require "axlsx" before_action :create_first_fields helper Admin::OlympiamanagementsHelper load File.expand_path(__dir__)+'/zipgenerator.rb' def index end def add_school_data SchoolDataFields.create if SchoolDataFields.all.count == 0 @SchoolDataField = SchoolDataFields.first @olympia_school_data_field = (params[:id].nil? ? OlympiaSchoolDataFields.new : ((OlympiaSchoolDataFields.find(params[:id]).nil? rescue true) ? OlympiaSchoolDataFields.new : OlympiaSchoolDataFields.find(params[:id]))) end def update_sign_up_data if params[:type] == "delete_old_data" else render :html => params and return end end def update_school_data if params[:type] == 'class_setting' @ClassSettingField = (params[:id].nil? ? ClassSettingFields.new : ((ClassSettingFields.find(params[:id]).nil? rescue true) ? ClassSettingFields.new : ClassSettingFields.find(params[:id]))) @ClassSettingField.class_number_range = params[:class_number_range].map{|num| num.to_i} @ClassSettingField.enrollment_available = params[:enrollment_available].to_i @ClassSettingField.save render :json => {'success'=>true} and return elsif params[:type] == 'delete_class_setting' if !params[:id].to_s.blank? begin ClassSettingFields.find(params[:id]).destroy rescue puts 'not_found id='+params[:id]+' in ClassSettingFields' end redirect_to :back end elsif params[:type] == 'delete_school_data' if !params[:id].to_s.blank? begin OlympiaSchoolDataFields.find(params[:id]).destroy rescue puts 'not_found id='+params[:id]+' in OlympiaSchoolDataFields' end redirect_to :back end elsif params[:type] == 'add_school_data' @olympia_school_data_field = (params[:id].nil? ? OlympiaSchoolDataFields.new : ((OlympiaSchoolDataFields.find(params[:id]).nil? rescue true) ? OlympiaSchoolDataFields.new : OlympiaSchoolDataFields.find(params[:id]))) params['add_school_data'].keys.each do |field| @olympia_school_data_field[field] = params['add_school_data'][field] end @olympia_school_data_field.save redirect_to admin_olympiamanagements_school_data_list_path elsif params[:type] == 'import_class_setting' if params[:upload_file].nil? redirect_to admin_olympiamanagements_import_editing_number_of_school_class_path+'?error=true' else import_class_setting end elsif params[:type] == 'approved' @olympia_school_data_field = OlympiaSchoolDataFields.find(params[:id]) rescue nil if @olympia_school_data_field.nil? render_404_html else @olympia_school_data_field.approved = true @olympia_school_data_field.save redirect_to :back end elsif params[:type] == 'unapproved' @olympia_school_data_field = OlympiaSchoolDataFields.find(params[:id]) rescue nil if @olympia_school_data_field.nil? render_404_html else @olympia_school_data_field.approved = false @olympia_school_data_field.save redirect_to :back end elsif params[:type] == 'sign_up_setting' @sign_up_setting = SignUpSetting.find(params[:id]) rescue SignUpSetting.new if @sign_up_setting.nil? render_404_html else params["sign_up_setting"].each do |key,value| @sign_up_setting[key] = value end @sign_up_setting.save redirect_to :back end else render :html => params and return end end def render_404_html render :html => File.read('app/views/errors/404.html').html_safe end def school_data_list @SchoolDataField = SchoolDataFields.first @SchoolDataname = @SchoolDataField.school_data_fields.map{|field| field.keys[0]} page_num = params[:page] || 1 @OlympiaSchoolDataFields = OlympiaSchoolDataFields.all.asc(:id) @OlympiaSchoolDataFields = @OlympiaSchoolDataFields.where("school_name" => /#{params[:school_name]}/) if !params[:school_name].to_s.blank? @OlympiaSchoolDataFields = @OlympiaSchoolDataFields.where("school_code" => /#{params[:school_code]}/) if !params[:school_code].to_s.blank? @OlympiaSchoolDataFields = @OlympiaSchoolDataFields.page(page_num).per(10) end def class_setting page_num = params[:page] || 1 @class_setting_list = ClassSettingFields.all.asc(:enrollment_available).page(page_num).per(10) @ClassSettingField = (params[:id].nil? ? nil : ((ClassSettingFields.find(params[:id]).nil? rescue true) ? nil : ClassSettingFields.find(params[:id]))) end def import_class_setting if !params[:upload_file].blank? flag = read_xlsx_temp_file_and_import_class_setting(params[:upload_file].tempfile,params[:upload_file].original_filename) if flag == true redirect_to admin_olympiamanagements_school_data_list_path else render 'import_error_msg' end end end def read_xlsx_temp_file_and_import_class_setting(tempfile,filename) import_class_setting_check(tempfile,filename) if !@error_msg.empty? return false else @error_msg = Array.new @filename = filename.nil? ? tempfile.path : filename workbook = RubyXL::Parser.parse(tempfile) sheet = workbook[0].sheet_data.rows first_index = 0 sheet.each_with_index do |raw_row,index| row = raw_row.cells.map{ |cell| cell.value rescue nil} if row.nil? && first_index != -1 first_index += 1 next elsif row.nil? next end if index == first_index || first_index != -1 first_index = -1 else #@error_msg << @row_index_hash and return false @olympia_school_data_field = OlympiaSchoolDataFields.where(:school_code=> row[@row_index_hash["school_code"]]).first @olympia_school_data_field = OlympiaSchoolDataFields.new() if @olympia_school_data_field.nil? @row_index_hash.each do |key,index| @olympia_school_data_field[key] = row[index] end @olympia_school_data_field.save end end return true end end def import_class_setting_check(tempfile,filename) @profile_data_row = [ "school_code", "school_name" , "school_address" ,"class_number","enrollment_limited","school_contact_person_name","department_job_title","office_tel_number","fax","mobile_number","email","enrollment"] @error_msg = Array.new @filename = filename.nil? ? tempfile.path : filename @row_index_hash = {} begin workbook = RubyXL::Parser.parse(tempfile) rescue @error_msg << filename+'格式錯誤' return @error_msg end sheet = workbook[0].sheet_data.rows first_index = 0 sheet.each_with_index do |raw_row,index| begin row = raw_row.cells.map{ |cell| cell.value rescue nil} rescue @error_msg << filename+'格式錯誤' return @error_msg end if row.nil? && first_index != -1 next elsif row.nil? next end if index == first_index || first_index != -1 @field_data = row delete_array = [] @profile_data_row.each do |row_key| if !@field_data.include?(t('olympiamanagement.'+row_key)) delete_array << row_key else @row_index_hash[row_key] = @field_data.index(t('olympiamanagement.'+row_key)) end end delete_array.each{|element| @profile_data_row.delete(element)} first_index = -1 else #@error_msg << @row_index_hash and return if row[@row_index_hash["school_code"]] == "" @error_msg << @filename+"中#{make_alpha_from_numbers(@row_index_hash['school_code'])}#{index+1}欄位學校代碼不符(該欄位值不可為空)。" end end end end def make_alpha_from_numbers(number) @numeric = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; if number < @numeric.size @a = @numeric[number] else @dev_by = (number/@numeric.size).floor @a = "#{make_alpha_from_numbers(@dev_by-1)}#{make_alpha_from_numbers(number-(@dev_by*@numeric.size))}"; end end def sign_up_setting @sign_up_setting = (SignUpSetting.all.length == 0) ? (SignUpSetting.new) : (SignUpSetting.last) @site_locales = Site.first.in_use_locales @site_locales.delete(I18n.locale) @site_locales.insert(0,I18n.locale) end def export_school_connection_data @OlympiaSchoolDataFields = OlympiaSchoolDataFields.all.asc(:id) dir_path = 'tmp/olypiamanagement/' #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 = 'school_export.xlsx' Dir.chdir(dir_path) do File.open(@filename, 'w') do |f| f.write render_to_string( :handlers=> [:axlsx], :formats=> [:xlsx] ,:partial=> 'school_export.xlsx',:locals=> {:@OlympiaSchoolDataFields=>@OlympiaSchoolDataFields} ) end end tmp_filename_data = File.read(dir_path +@filename) send_data(tmp_filename_data, type: 'application/xlsx', disposition: 'attachment', filename: @filename) end def sign_up_student_data_list page_num = params[:page] || 1 @field_infos = StudentDataField.first.student_data_fields @sign_up_setting_id = SignUpSetting.last.id @student_fields = OlympiaStudentDataField.where(:olympia_school_data_fields_id=>params[:olympia_school_id],:sign_up_setting_id=>@sign_up_setting_id).asc(:id).page(page_num).per(10) end def export_sign_up_student_data # @OlympiaSchoolDataFields = OlympiaSchoolDataFields.all.asc(:id) # dir_path = 'tmp/olypiamanagement/' # #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 = 'school_export.xlsx' # Dir.chdir(dir_path) do # File.open(@filename, 'w') do |f| # f.write render_to_string( :handlers=> [:axlsx], :formats=> [:xlsx] ,:partial=> 'school_export.xlsx',:locals=> {:@OlympiaSchoolDataFields=>@OlympiaSchoolDataFields} ) # end # end # tmp_filename_data = File.read(dir_path +@filename) # send_data(tmp_filename_data, type: 'application/xlsx', disposition: 'attachment', filename: @filename) end def download_scan_file_of_certificate @sign_up_setting_id = SignUpSetting.last.id @student_data_list = OlympiaStudentDataField.where(:sign_up_setting_id=>@sign_up_setting_id) @student_data_image_path_arr = @student_data_list.flat_map{|student_data| student_data.olympia_student_images}.map{|student_image| student_image.student_file.file.file} zip_path = "tmp/student_images/#{@sign_up_setting_id}/" tmp_filename = '' FileUtils.rm_r(zip_path, :force => true) if Dir.exist?(zip_path) FileUtils.mkdir_p zip_path #create dir for storing tmp_file @student_data_image_path_arr.each do |image_path| @pathname = Pathname.new(image_path) @temp_name = @pathname.basename Dir.chdir(@pathname.dirname.to_s) do @index = 1 @original_filename =@pathname.basename.to_s @new_pathname = Pathname.new("#{Rails.root}/#{zip_path}#{@original_filename}") while @new_pathname.exist? do @new_pathname = Pathname.new(@new_pathname.to_s.insert(@new_pathname.to_s.rindex('.'),"-#{@index}")) @index += 1 end #a[0...b].concat('-1').concat(a[b..-1]) FileUtils.cp(@original_filename,@new_pathname) end end @filename = Pathname.new(zip_path).parent.to_s+"/#{Time.now.strftime('%Y_%m_%d')}_student_images.zip" zip_file= ZipFileGenerator.new(zip_path,@filename) begin zip_file.write rescue File.delete(@filename) zip_file.write end tmp_filename_data = File.read(@filename) send_data(tmp_filename_data, type: 'application/zip', disposition: 'attachment', filename: Pathname.new(@filename).basename.to_s) end def import_editing_number_of_school_class @olympia_school_data_field = OlympiaSchoolDataFields.new end def create_first_fields StudentDataField.create if StudentDataField.all.length == 0 SchoolDataFields.create if StudentDataField.all.length == 0 SignUpSetting.create if SignUpSetting.all.length == 0 end def download_import_file @OlympiaSchoolDataFields = OlympiaSchoolDataFields.all.asc(:id) dir_path = 'tmp/olypiamanagement/' #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 = 'import_schoo_class_setting.xlsx' Dir.chdir(dir_path) do File.open(@filename, 'w') do |f| f.write render_to_string( :handlers=> [:axlsx], :formats=> [:xlsx] ,:partial=> 'import_schoo_class_setting.xlsx',:locals=> {:@OlympiaSchoolDataFields=>@OlympiaSchoolDataFields} ) end end tmp_filename_data = File.read(dir_path +@filename) send_data(tmp_filename_data, type: 'application/xlsx', disposition: 'attachment', filename: "#{Time.now.year}#{Time.now.month}#{Time.now.day}_import_schoo_class_setting.xlsx") end end