venue_management/app/models/concerns/venue_link_file.rb

110 lines
5.5 KiB
Ruby
Raw Permalink Normal View History

2020-08-02 13:33:41 +00:00
module VenueLinkFile
extend ActiveSupport::Concern
2020-08-04 09:04:04 +00:00
module VenueMethod
def get_trans(field)
class_name = self.class.to_s
if class_name == 'VenueManagementMain'
I18n.t("venue_management.#{field}")
else
class_name = class_name.underscore.gsub('venue_management','vm')
I18n.t("#{class_name}.#{field}")
end
end
end
2020-08-02 13:33:41 +00:00
included do
2020-08-04 09:04:04 +00:00
self.include VenueMethod
2020-08-02 13:33:41 +00:00
tmp = self.to_s.underscore.to_sym
VenueManagementFile.send(:belongs_to,tmp, :class_name => self.to_s, :foreign_key => "#{tmp}_id")
VenueManagementLink.send(:belongs_to,tmp, :class_name => self.to_s, :foreign_key => "#{tmp}_id")
2020-09-22 15:19:59 +00:00
VenueManagementReminder.send(:belongs_to,tmp, :class_name => self.to_s, :foreign_key => "#{tmp}_id")
2020-08-02 13:33:41 +00:00
self.has_many :venue_management_files, :autosave => true, :dependent => :destroy
self.accepts_nested_attributes_for :venue_management_files, :allow_destroy => true
self.has_many :venue_management_links, :autosave => true, :dependent => :destroy
self.accepts_nested_attributes_for :venue_management_links, :allow_destroy => true
2020-09-22 15:19:59 +00:00
self.has_many :venue_management_reminders, :autosave => true, :dependent => :destroy
self.accepts_nested_attributes_for :venue_management_reminders, :allow_destroy => true
2020-08-04 09:04:04 +00:00
self.fields.values.each do|v|
if v.type==Date || v.type==DateTime
self.field v.name+'_reminder', type: Boolean,default: false
self.field v.name+'_reminder_day', type: Integer
end
end
2020-08-15 13:40:31 +00:00
self.field :calendar_data,type: Hash,default: {type:{},key:[],page_id: {}}
2020-08-07 14:03:30 +00:00
self.field :calendar_dict,type: Hash,default: {}
2020-08-15 13:40:31 +00:00
def to_calendar_param
venue_main = self.class.to_s == 'VenueManagementMain' ? self : VenueManagementMain.find(self.venue_management_main_id)
2020-09-22 15:19:59 +00:00
self.case_no.to_s+'-'+self.id.to_s+'?method=show_'+self.class.to_s.underscore.gsub('venue_management_','').gsub('main','data')
2020-08-15 13:40:31 +00:00
end
2020-08-07 14:03:30 +00:00
def calendar_id(field)
self.calendar_dict[field]
end
2020-08-04 09:04:04 +00:00
self.has_many :venue_management_emails, :autosave => true, :dependent => :destroy
self.accepts_nested_attributes_for :venue_management_emails, :allow_destroy => true
VenueManagementEmail.send(:belongs_to,tmp, :class_name => self.to_s, :foreign_key => "#{tmp}_id")
self.before_save do
2020-08-07 14:03:30 +00:00
venue_main = self.class.to_s == 'VenueManagementMain' ? self : VenueManagementMain.find(self.venue_management_main_id)
calendar_keys = Array(self.calendar_data['key'])
(self.calendar_dict.keys - calendar_keys).each do |key_deleted|
event = Event.where(:id => self.calendar_dict[key_deleted]).first
event.model_class = nil
event.destroy
self.calendar_dict.delete(key_deleted)
end
if !calendar_keys.blank?
calendar_keys.each do |key|
key_id = self.calendar_id(key)
current_user_id = OrbitHelper.current_user.id rescue nil
in_use_locales = Site.first.in_use_locales
title_tp = {}
note_tp = {}
in_use_locales.each do |locale|
title_tp[locale] = venue_main['title'][locale] + ' ' + self.get_trans(key)
note_tp[locale] = self.case_no.blank? ? {} : I18n.t('vm_inviting.case_no') +': ' + self.case_no
end
2020-08-15 13:40:31 +00:00
update_dict = {key: key,model_class: self.class.to_s,model_id: self.id,update_user_id: current_user_id,calendar_type_id: calendar_data['type'][key],all_day: true,start: self.send(key),end: self.send(key),title_translations: title_tp,note_translations: note_tp,module_key: 'venue_management',model_page_id: calendar_data['page_id'][key]}
2020-08-07 14:03:30 +00:00
if key_id.nil?
self.calendar_dict[key] = Event.create(update_dict.merge(create_user_id: current_user_id)).id
else
2020-12-16 10:21:24 +00:00
calendar = Event.find(key_id) rescue nil
calendar.update_attributes(update_dict) if !calendar.nil?
if calendar.nil?
self.calendar_dict[key] = Event.create(update_dict.merge(create_user_id: current_user_id)).id
end
2020-08-07 14:03:30 +00:00
end
end
end
2020-08-04 09:04:04 +00:00
self.venue_management_emails.each do |venue_mail|
email = venue_mail.email
if !email.nil?
email.destroy
end
end
self.venue_management_emails = []
manager_emails = venue_main.manager_emails
self.fields.values.each do|v|
if v.type==Date || v.type==DateTime
if self.send(v.name+'_reminder')
title = venue_main.title + ' ' + self.case_no.to_s + ' ' + self.get_trans(v.name)
send_time = self.send(v.name)-self.send(v.name+'_reminder_day').day rescue nil
2020-11-03 12:59:13 +00:00
send_time = DateTime.parse(send_time.strftime("%Y/%m/%d 00:00:00 %z")) rescue send_time
2020-08-04 09:04:04 +00:00
if !send_time.nil?
new_email = Email.create(mail_to: manager_emails,
module_app_key:"venue_management",
template:"email/reminder_email.html.erb",
mail_sentdate: send_time,
mail_subject: title,
2020-08-04 09:13:30 +00:00
template_data:{'title'=>title,'send_time'=>self.send(v.name),'locale'=>I18n.locale})
2020-08-04 09:04:04 +00:00
self.venue_management_emails << VenueManagementEmail.new(email_id: new_email.id)
end
end
end
end
end
Email.before_destroy do
email = VenueManagementEmail.where(:email_id=>self.id).first
if !email.nil?
email.destroy
end
end
2020-08-02 13:33:41 +00:00
end
end