class OlympiamanagementsController < PagesController include ActionView::Context #vary important (only add this can access @@session from view) include ApplicationHelper before_action :set_sign_up_setting #include "#{Rails.root}/app/helpers/application_helper.rb" def initialize super @app_title = "olympiamanagement" end def set_sign_up_setting @sign_up_setting = SignUpSetting.last data_arr = [{:url=>'/olympiamanagements/school_connection_data',:name=>t('olympiamanagement.school_connection_data')}, {:url=>'/olympiamanagements/add_sign_up_student_data',:name=>t('olympiamanagement.add_sign_up_student_data')}, {:url=>'/olympiamanagements/sign_up_student_data_list',:name=>t('olympiamanagement.sign_up_student_data_list')}, {:url=>'/olympiamanagements/edit_password',:name=>t('olympiamanagement.edit_password')}, {:url=>'/olympiamanagements/print_registration_form',:name=>t('olympiamanagement.print_registration_form')}, {:url=>'/olympiamanagements/logout',:name=>t('olympiamanagement.logout')}] @breadcumb = render_to_string(:formats=> [:html] ,:partial=>'breadcumb',:locals=>{:data_arr=>data_arr}) end def index uid = OrbitHelper.params[:uid] rescue "" tags = OrbitHelper.widget_tags categories = OrbitHelper.widget_categories || [] set_sign_up_setting @time_arr = ["year","month","day","o_clock","minute"] start_time_str = @time_arr.map{|t| @sign_up_setting["start_"+t]}.join(' ') end_time_str = @time_arr.map{|t| @sign_up_setting["end_"+t]}.join(' ') time_now_str = Time.now.strftime("%Y %m %d %H %M") @instructions_name = {} Site.first.in_use_locales.each do |locale| I18n.with_locale(locale) do @instructions_name[locale.to_s] = I18n.t('olympiamanagement.registration_instructions') end end @page = Page.where(:layout=>"olympia_managements_index",:parent_page_id=>(Page.where(:name=> ((I18n.locale.to_s == "en") ? "Home" : "首頁")).first.id)).first @instructions_pages = Page.where(:parent_page_id=>@page.id).select{|page| page.name_translations==@instructions_name} case OrbitHelper.params[:layout_type] when "olympia_managements_instructions_index" if !(time_now_str <= end_time_str && time_now_str >= start_time_str) @olympiamanagement_instruction = @sign_up_setting.registration_instructions[I18n.locale.to_s].to_s @sign_up_setting.attributes.to_h.each do |key,value| @olympiamanagement_instruction = @olympiamanagement_instruction.gsub("{{#{key}}}",value.to_s) end if !OrbitHelper.current_user.nil? @edit_link = '/admin/olympiamanagements/sign_up_setting#registration_instructions' @edit_text = (I18n.locale.to_s == "zh_tw") ? "編輯" : "Edit" if !OrbitHelper.current_user.nil? @olympiamanagement_instruction += '
' end end data_arr = @page.child_page.map{|page| {:url=>"/#{I18n.locale}#{page.url}",:name=>page.name}} @breadcumb = render_to_string(:formats=> [:html] ,:partial=>'breadcumb',:locals=>{:data_arr=>data_arr}) @olympiamanagement_instruction = @breadcumb + @olympiamanagement_instruction.html_safe else if !@instructions_pages.empty? @instructions_pages.each{|page| page.destroy} end @olympiamanagement_instruction = '#{I18n.t('olympiamanagement.please_set_school_connect_data')}
".html_safe) else render_contents_in_index_page(@breadcumb) end #if @flag = false end end def sign_up_student_data_list end def edit_password if !session[:olympia_login_id].to_s.blank? if params[:confirm_password].nil? && params[:new_password].nil? @page_content = render_to_string(:formats=> [:html] ,:partial=>'edit_password.html',:locals=>{:@error=>(@@error rescue nil)}) render_contents_in_index_page(@breadcumb+@page_content) else @@error = nil if params[:confirm_password].to_s != params[:new_password].to_s @@error = "confirm_password_error" elsif params[:confirm_password].to_s.blank? && params[:new_password].to_s.blank? @@error = "password_all_empty" elsif params[:confirm_password].to_s.blank? @@error = "confirm_password_empty" elsif params[:new_password].to_s.blank? @@error = "new_password_empty" else @olympia_school_data_field = OlympiaSchoolDataFields.where(:account_number=>session[:olympia_login_id]).first @olympia_school_data_field.password = params[:new_password] @olympia_school_data_field.save end redirect_to edit_password_olympiamanagements_path end else @page = Page.where(:layout=>"olympia_managements_index",:parent_page_id=>(Page.where(:name=> ((I18n.locale.to_s == "en") ? "Home" : "首頁")).first.id)).first redirect_to "/#{I18n.locale}#{@page.url}" end end def print_registration_form end def logout session[:olympia_login_id] = "" @page = Page.where(:layout=>"olympia_managements_index",:parent_page_id=>(Page.where(:name=> ((I18n.locale.to_s == "en") ? "Home" : "首頁")).first.id)).first redirect_to "/#{I18n.locale}#{@page.url}"#render :html => session.to_hash end def widget uid = OrbitHelper.params[:uid] rescue "" tags = OrbitHelper.widget_tags categories = OrbitHelper.widget_categories || [] @sign_up_setting = SignUpSetting.last @olympiamanagement_instruction = @sign_up_setting.registration_instructions[I18n.locale.to_s].to_s @sign_up_setting.attributes.to_h.each do |key,value| @olympiamanagement_instruction = @olympiamanagement_instruction.gsub("{{#{key}}}",value.to_s) end { "olympiamanagement" => [], "extras"=>{"instructions"=> @olympiamanagement_instruction} } end def download_school_code @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 = 'download_school_code.xlsx' Dir.chdir(dir_path) do File.open(@filename, 'w') do |f| f.write render_to_string( :handlers=> [:axlsx], :formats=> [:xlsx] ,:partial=> 'school_code.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}_download_school_code.xlsx") end def render_contents_in_index_page(contents=@breadcumb) doc = Nokogiri::HTML(render_final_page("page",Page.where(:layout=>"olympia_managements_index").first,true), nil, "UTF-8") viewarea = doc.css("*[data-content='true']")[0] viewarea.inner_html = contents render :html => doc.to_html.html_safe end def updata_school_data @readonly_fields = ['account_number','password','school_name','school_code','school_address','class_number','enrollment_limited'] @olympia_school_data_field = OlympiaSchoolDataFields.where(:account_number=>params[:olympia_user_name]).first redirect_to :back if @olympia_school_data_field.nil? params['add_school_data'].keys.each do |field| if !@readonly_fields.include?(field) @olympia_school_data_field[field] = params['add_school_data'][field] end end @olympia_school_data_field.save redirect_to add_sign_up_student_data_olympiamanagements_path #render_contents_in_index_page(@olympia_school_data_field.attributes.to_s) end def updata_student_data #render :html => '123' end end