This commit is contained in:
BOHUNG 2020-02-06 00:16:48 +08:00
parent ca31360f65
commit 42150a399e
5 changed files with 109 additions and 19 deletions

View File

@ -437,7 +437,11 @@ class Admin::OlympiamanagementsController < OrbitAdminController
@sign_up_setting = SignUpSetting.where(:active=>true).last @sign_up_setting = SignUpSetting.where(:active=>true).last
else else
@sign_up_setting = SignUpSetting.find(session[:sign_up_setting_id]) rescue nil @sign_up_setting = SignUpSetting.find(session[:sign_up_setting_id]) rescue nil
@sign_up_setting = SignUpSetting.where(:active=>true).last if @sign_up_setting.nil? end
if SignUpSetting.where(:active=>true).last.nil?
@sign_up_setting = SignUpSetting.last if @sign_up_setting.nil?
@sign_up_setting.active = true
@sign_up_setting.save
end end
end end
def download_import_file def download_import_file

View File

@ -179,7 +179,12 @@ class OlympiamanagementsController < ApplicationController
@page_content = render_to_string(:formats=> [:html] ,:partial=>'edit_school_data.html',:locals=>{:@school_data_fields=>@school_data_fields,:@olympia_school_data_field=>@olympia_school_data_field}) @page_content = render_to_string(:formats=> [:html] ,:partial=>'edit_school_data.html',:locals=>{:@school_data_fields=>@school_data_fields,:@olympia_school_data_field=>@olympia_school_data_field})
render_contents_in_index_page(@breadcumb+@page_content)#redirect_to :back render_contents_in_index_page(@breadcumb+@page_content)#redirect_to :back
else else
redirect_to :back begin
redirect_to :back
rescue
@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 end
end end
def add_sign_up_student_data def add_sign_up_student_data
@ -541,6 +546,90 @@ class OlympiamanagementsController < ApplicationController
end end
format_date(final_html_for_render,(original_view == "home" ? "home" : page.module)) rescue final_html_for_render format_date(final_html_for_render,(original_view == "home" ? "home" : page.module)) rescue final_html_for_render
end end
def render_widget_for_frontend(controller_name, widget_method, widget_file, subpart_id=nil)
def widget_parsing_repeats_again(elements,d,level)
newhtml = []
oldhtml = []
elements.each do |el|
html_to_render = ""
data_name = el.attr("data-list")
wrap_elements = el.css("*[data-list][data-level='#{level}']")
if d["#{data_name}"]
d["#{data_name}"].each_with_index do |item,i|
element = el.inner_html
if wrap_elements.count > 0
htmls = widget_parsing_repeats_again(wrap_elements,d["#{data_name}"][i], level + 1)
htmls[0].each_with_index do |html,i|
element = element.gsub(html,htmls[1][i])
end
end
item.each do |key,value|
if !value.kind_of?(Array)
value = value.nil? ? "" : value
element = element.gsub("{{#{key}}}",value.to_s.html_safe)
element = element.gsub("%7B%7B#{key}%7D%7D",value.to_s)
end
end
html_to_render = html_to_render + element
end
temp = el.to_s
oldhtml << temp
temp = temp.gsub(el.inner_html, html_to_render)
newhtml << temp
end
end
[oldhtml,newhtml]
end
@key = Site.first.template if @key.nil?
controller_name = controller_name.downcase.singularize
f = File.join(Rails.root, 'app', 'templates', "#{@key}", 'modules', "#{controller_name}", "_#{widget_file}.html.erb");
if !File.exists?f
f = File.join(Rails.root, 'app', 'templates', "#{@key}", 'widgets', "#{controller_name}", "_#{widget_file}.html.erb");
if !File.exists?f
f = File.join(Rails.root, 'app', 'templates', "#{@key}", 'modules', "#{controller_name}", "_widget.html.erb");
if File.exists?f
f = File.join("#{@key}", 'modules', "#{controller_name}", "_widget.html.erb");
else
return "<div class='well'>Maybe the administrator has changed the theme, please select the widget design again from the page settings.</div>".html_safe
end
else
f = File.join("#{@key}", 'widgets', "#{controller_name}", "_#{widget_file}.html.erb");
end
else
f = File.join("#{@key}", 'modules', "#{controller_name}", "_#{widget_file}.html.erb");
end
s = render_to_string(f)
doc = Nokogiri::HTML(s, nil, "UTF-8")
if !subpart_id.nil?
doc.css("body").children.first.set_attribute("data-subpart-id", subpart_id) rescue nil
end
wrap_elements = doc.css("*[data-repeat]")
controller = "#{controller_name.capitalize.pluralize}_controller".classify.constantize.new
OrbitHelper.set_current_widget_module controller_name
begin
data = controller.send("#{widget_method}") #rescue nil
rescue Exception => e
write_widget_debug_file(e,controller_name,widget_method,subpart_id) if Site::DEBUG
end
if !data.nil?
wrap_elements = doc.css("*[data-list][data-level='0']")
htmls = widget_parsing_repeats_again(wrap_elements,data,1)
html = doc.to_s
htmls[0].each_with_index do |h,i|
html = html.gsub(h,htmls[1][i])
end
extras = data['extras'] || {}
extras["subpart-id"] = subpart_id if !subpart_id.nil?
extras["widget-title"] = OrbitHelper.widget_title
extras.each do |key,value|
html = html.gsub("{{#{key}}}",value.to_s.html_safe)
html = html.gsub("%7B%7B#{key}%7D%7D",value.to_s)
end
html.html_safe
else
return "<div class='well'>No content to show.</div>".html_safe
end
end
def update_school_data def update_school_data
@readonly_fields = ['account_number','password','school_name','school_code','school_address','class_number','enrollment_limited'] @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 @olympia_school_data_field = OlympiaSchoolDataFields.where(:account_number=>params[:olympia_user_name]).first

View File

@ -4,8 +4,6 @@ class OlympiaSchoolDataFields
include OrbitModel::Status include OrbitModel::Status
include OrbitModel::Impression include OrbitModel::Impression
# encoding: utf-8 # encoding: utf-8
include OrbitTag::Taggable
include OrbitCategory::Categorizable
field :account_number , type: String , default: '' field :account_number , type: String , default: ''
field :password , type: String , default: '' field :password , type: String , default: ''
field :school_name , type: String , default: '' field :school_name , type: String , default: ''
@ -19,9 +17,9 @@ class OlympiaSchoolDataFields
field :fax , type: String , default: '' field :fax , type: String , default: ''
field :mobile_number , type: String , default: '' field :mobile_number , type: String , default: ''
field :email , type: String , default: '' field :email , type: String , default: ''
field :enrollment , type: Hash , default: 0 field :enrollment , type: Fixnum , default: 0
field :approved , type: Boolean , default: true field :approved , type: Boolean , default: true
field :sign_up_setting_id , type: String , default: SignUpSetting.where(:active=>true).last.id.to_s field :sign_up_setting_id , type: String , default: (SignUpSetting.where(:active=>true).last.id.to_s rescue "")
after_destroy do |record| after_destroy do |record|
OlympiaStudentDataField.where(:olympia_school_data_fields_id => record.id).destroy_all OlympiaStudentDataField.where(:olympia_school_data_fields_id => record.id).destroy_all
end end
@ -49,10 +47,6 @@ class OlympiaSchoolDataFields
self.sign_up_setting_id = SignUpSetting.where(:active=>true).last.id.to_s self.sign_up_setting_id = SignUpSetting.where(:active=>true).last.id.to_s
self.save! self.save!
end end
sign_up_setting = SignUpSetting.find(self.sign_up_setting_id) rescue nil
if sign_up_setting.nil?
self.destroy
end
end end
end end
end end

View File

@ -39,7 +39,17 @@ class SignUpSetting
:en=>""} :en=>""}
field :active , type: Boolean , default: true field :active , type: Boolean , default: true
after_save :change_all_password , :check_active after_save :change_all_password , :check_active
after_create :auto_create_schools after_destroy do |record|
OlympiaSchoolDataFields.where(:sign_up_setting_id => record.id).destroy_all
end
after_create do |record|
SignUpSetting.where(:id.ne=>record.id).each{|record| record.update(:active=>false)}
@sign_up_setting = SignUpSetting.where(:id.ne=>record.id).last
@schools = OlympiaSchoolDataFields.where(:sign_up_setting_id=>@sign_up_setting.id).take_while{true}
@schools.each do |school|
OlympiaSchoolDataFields.create(school.attributes.except(*['_id','enrollment',"view_count","created_at","updated_at","sign_up_setting_id"]).merge({:sign_up_setting_id => record.id}))
end
end
def change_all_password def change_all_password
if self.set_default_password && !self.default_password.blank? if self.set_default_password && !self.default_password.blank?
OlympiaSchoolDataFields.where(:sign_up_setting_id=>self.id).each do |school_field| OlympiaSchoolDataFields.where(:sign_up_setting_id=>self.id).each do |school_field|
@ -50,13 +60,6 @@ class SignUpSetting
self.save self.save
end end
end end
def auto_create_schools
@sign_up_setting = SignUpSetting.where(:id.ne=>self.id).last
@schools = OlympiaSchoolDataFields.where(:sign_up_setting_id=>@sign_up_setting.id)
@schools.each do |school|
OlympiaSchoolDataFields.create(school.attributes.except(*['_id','enrollment']))
end
end
def check_active def check_active
if self.active if self.active
SignUpSetting.where(:id.ne=>self.id).each do |setting| SignUpSetting.where(:id.ne=>self.id).each do |setting|

View File

@ -4,7 +4,7 @@ module Olympiamanagement
initializer "olympiamanagement" do initializer "olympiamanagement" do
OrbitApp.registration "Olympiamanagement", :type => "ModuleApp" do OrbitApp.registration "Olympiamanagement", :type => "ModuleApp" do
base_url File.expand_path File.dirname(__FILE__) base_url File.expand_path File.dirname(__FILE__)
taggable "olympiamanagementfields" set_keyword_contstraints ['/olympiamanagements/']
categorizable categorizable
authorizable authorizable
widget_methods ["widget"] widget_methods ["widget"]