olympiamanagement/app/controllers/admin/olympiamanagements_controll...

262 lines
10 KiB
Ruby

class Admin::OlympiamanagementsController < OrbitAdminController
require 'spreadsheet'
require 'rubyXL'
require 'fileutils'
require "axlsx"
helper Admin::OlympiamanagementsHelper
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|
#if
@olympia_school_data_field[field] = params['add_school_data'][field]
#elsif
#ClassSettingFields.all.select{|field| field.class_number_range[0]<6 && field.class_number_range[1] > 6}
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
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 export_sign_up_student_data
# @OlympiaSchoolDataFields = OlympiaSchoolDataFields.all
# 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
end
def import_editing_number_of_school_class
@olympia_school_data_field = OlympiaSchoolDataFields.new
end
def download_import_file
@OlympiaSchoolDataFields = OlympiaSchoolDataFields.all
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