Application_Form/app/models/application_form_template_s...

138 lines
7.7 KiB
Ruby
Raw Normal View History

2022-03-10 02:11:18 +00:00
class ApplicationFormTemplateSetting
2021-06-17 10:07:43 +00:00
include Mongoid::Document
include Mongoid::Timestamps
2021-11-30 01:57:52 +00:00
DefaultMenu = ["introduction","news","registration","registered_participants","submission","album"]
2022-03-10 02:11:18 +00:00
PagesInfo = {"introduction" => "application_forms_page",
2021-11-30 01:57:52 +00:00
"news" => "custom_announcement",
2022-03-10 02:11:18 +00:00
"registration" => "application_form_registration",
"registered_participants" => "application_form_registered_participants",
"submission" => "application_form_submission",
2021-11-30 01:57:52 +00:00
"album" => "custom_gallery"}
2022-03-10 02:11:18 +00:00
belongs_to :application_form_main
2021-06-17 10:07:43 +00:00
field :enable_custom_template, type: Boolean, default: false
field :display_menu, type: Array, default: ["introduction","news","registration","submission","album"]
field :announcement_limit, type: Integer , default: 4
field :album_limit, type: Integer , default: 4
field :website_title, type: String , default: "", localize: true
field :footer_info, type: String , default: "", localize: true
field :introduction, type: String , default: "", localize: true
2021-10-26 14:01:36 +00:00
field :background_style, type: String , default: "1"
field :content_style, type: String , default: "1"
2022-03-10 02:11:18 +00:00
has_many :application_form_banner_images, :autosave => true, :dependent => :destroy
accepts_nested_attributes_for :application_form_banner_images, :allow_destroy => true
2021-10-26 14:01:36 +00:00
after_create do
2022-03-10 02:11:18 +00:00
if self.application_form_main
@application_form_page_root = Page.where(:parent_page_id=>self.application_form_main_id).first
2021-10-26 14:01:36 +00:00
available_locales = Site.first.in_use_locales rescue I18n.available_locales
available_locales = available_locales.map{|l| l.to_s}
2022-03-10 02:11:18 +00:00
if @application_form_page_root.nil?
@application_form_page_root = Page.create(:parent_page_id=>self.application_form_main_id,:module=>"application_forms_home",:name_translations=>@application_form.title_translations,:url=>"/home",:page_id=>"home",:enabled_for=>available_locales,:bind_uid=>@application_form.uid,:bind_module_app=>"application_form",:bind_model=>"ApplicationFormMain",:menu_enabled_for=>available_locales)
2021-10-26 14:01:36 +00:00
else
2022-03-10 02:11:18 +00:00
@application_form_page_root.update(:name_translations=>@application_form.title_translations,:module=>"application_forms_home",:url=>"/home",:page_id=>"home",:enabled_for=>available_locales,:bind_uid=>@application_form.uid,:bind_module_app=>"application_form",:bind_model=>"ApplicationFormMain",:menu_enabled_for=>available_locales)
2021-10-26 14:01:36 +00:00
end
2022-03-10 02:11:18 +00:00
pages_info = {"introduction" => "application_forms_page",
2021-10-26 14:01:36 +00:00
"news" => "custom_announcement",
2022-03-10 02:11:18 +00:00
"registration" => "application_form_registration",
"submission" => "application_form_submission",
2021-10-26 14:01:36 +00:00
"album" => "custom_gallery"}
relation_model = {"news" => "CustomBulletin","album"=>"CustomAlbum"}
pages_info.each do |page_id , page_module|
2022-03-10 02:11:18 +00:00
application_form_page = @application_form_page_root.child_page.where(:page_id=>page_id).first
page_trans = available_locales.map{|l| [l,I18n.with_locale(l){I18n.t("application_form.menu.#{page_id}")}]}.to_h
2021-10-26 14:01:36 +00:00
available_locales_tmp = available_locales.clone
if relation_model[page_id]
relation = relation_model[page_id].constantize rescue nil
if relation
2022-03-10 02:11:18 +00:00
if relation.where(:bind_uid=>@application_form.uid).count == 0
2021-10-26 14:01:36 +00:00
available_locales_tmp = []
end
end
end
2022-03-10 02:11:18 +00:00
if application_form_page.nil?
application_form_page = @application_form_page_root.child_page.create(:name_translations=>page_trans,:page_id=>page_id,:url=>"/home/#{page_id}",:enabled_for=>available_locales_tmp,:module=>page_module,:menu_enabled_for=>available_locales_tmp)
2021-10-26 14:01:36 +00:00
else
2022-03-10 02:11:18 +00:00
application_form_page.update(:name_translations=>page_trans,:page_id=>page_id,:url=>"/home/#{page_id}",:module=>page_module)
2021-10-26 14:01:36 +00:00
end
end
end
end
2021-07-14 04:57:47 +00:00
after_save do
if self.display_menu_changed?
2022-03-10 02:11:18 +00:00
@application_form_page_root = Page.where(:parent_page_id=>self.application_form_main_id).first
2021-07-14 04:57:47 +00:00
display_menu = self.display_menu
available_locales = Site.first.in_use_locales rescue I18n.available_locales
available_locales = available_locales.map{|l| l.to_s}
2021-11-30 01:57:52 +00:00
pages_info = self.class::PagesInfo
2022-03-10 02:11:18 +00:00
if @application_form_page_root
2021-07-14 04:57:47 +00:00
default_display_menu = self.class::DefaultMenu
default_display_menu.each do |page_id|
2022-03-10 02:11:18 +00:00
page = @application_form_page_root.child_page.where(:page_id=>page_id).first
2021-07-14 04:57:47 +00:00
if page
2021-11-30 01:57:52 +00:00
if display_menu.include?(page_id)
page.update(:enabled_for=>available_locales,:menu_enabled_for=>available_locales)
else
page.update(:enabled_for=>[],:menu_enabled_for=>[])
end
else
page_module = pages_info[page_id]
2022-03-10 02:11:18 +00:00
application_form_page = @application_form_page_root.child_page.where(:page_id=>page_id).first
page_trans = available_locales.map{|l| [l,I18n.with_locale(l){I18n.t("application_form.menu.#{page_id}")}]}.to_h
2021-11-30 01:57:52 +00:00
available_locales_tmp = available_locales.clone
2022-03-10 02:11:18 +00:00
@application_form_page_root.child_page.create(:name_translations=>page_trans,:page_id=>page_id,:url=>"/home/#{page_id}",:enabled_for=>available_locales_tmp,:module=>page_module,:menu_enabled_for=>available_locales_tmp)
2021-07-14 04:57:47 +00:00
end
end
end
end
end
2021-06-17 10:07:43 +00:00
def self.initialize_variables
app_path = Pathname.new(__FILE__).dirname.dirname
@@background_style_infos = {}
2022-03-10 02:11:18 +00:00
Dir.glob("#{app_path}/assets/stylesheets/application_form/background_style/*/").each do |folder|
2021-06-17 10:07:43 +00:00
files = Dir.glob("#{folder}/*")
2021-07-14 04:57:47 +00:00
image_file = files.select{|f| File.basename(f).match(/jpg|gif|png|jpeg/i)}
folder_name = File.basename(folder)
image_file = image_file.select{|f| File.basename(f).include?(folder_name)}.first
2021-06-17 10:07:43 +00:00
key = File.basename(folder)
2022-03-10 02:11:18 +00:00
image_path = "/assets/application_form/background_style/#{key}/#{File.basename(image_file) rescue ''}"
2021-10-26 14:01:36 +00:00
default_logo = nil
if File.exist?("#{folder}logo.jpg")
default_logo = "#{folder}logo.jpg".split("app")[1..-1].join("app").sub("/stylesheets/","/")
end
@@background_style_infos[key] = {
2021-06-17 10:07:43 +00:00
"image"=>image_path,
2021-10-26 14:01:36 +00:00
"css"=> get_folder_content(folder,"css"),
"js"=> get_folder_content(folder,"js"),
"default_logo"=> default_logo
2021-06-17 10:07:43 +00:00
}
end
@@background_style_infos = @@background_style_infos.sort_by {|k, v| (k.match(/\d+/)[0].to_i rescue 0)}.to_h
@@content_style_infos = {}
2022-03-10 02:11:18 +00:00
Dir.glob("#{app_path}/assets/stylesheets/application_form/content_style/*/").each do |folder|
2021-06-17 10:07:43 +00:00
files = Dir.glob("#{folder}/*")
2021-07-14 04:57:47 +00:00
image_file = files.select{|f| File.basename(f).match(/jpg|gif|png|jpeg/i)}
folder_name = File.basename(folder)
image_file = image_file.select{|f| File.basename(f).include?(folder_name)}.first
2021-06-17 10:07:43 +00:00
key = File.basename(folder)
2022-03-10 02:11:18 +00:00
image_path = "/assets/application_form/content_style/#{key}/#{File.basename(image_file) rescue ''}"
2021-06-17 10:07:43 +00:00
template_info = (JSON.parse(File.read("#{folder}/info.json")) rescue {"template"=> "horizontal"})
@@content_style_infos[key] = {
"image"=>image_path,
"css"=> get_folder_content(folder,"css"),
"js"=> get_folder_content(folder,"js"),
2021-07-14 04:57:47 +00:00
"info"=>template_info,
2022-03-10 02:11:18 +00:00
"template"=>"#{app_path}/assets/stylesheets/application_form/templates/#{template_info["template"]}/"
2021-06-17 10:07:43 +00:00
}
end
@@content_style_infos = @@content_style_infos.sort_by {|k, v| (k.match(/\d+/)[0].to_i rescue 0)}.to_h
end
def self.get_folder_content(folder,content)
return Dir.glob("#{folder}#{content}/*").map{|content| content.split("app")[1..-1].join("app").sub("/stylesheets/",'/')}
end
def self.background_style_infos
@@background_style_infos
end
def self.content_style_infos
@@content_style_infos
end
initialize_variables
end