From 327dd7fdac2c5d87a213461b4cf5dcb9f45d0b91 Mon Sep 17 00:00:00 2001 From: chiu Date: Fri, 7 Aug 2020 22:03:30 +0800 Subject: [PATCH] add feature to connect with calendar --- .../admin/venue_admin_controller.rb | 4 +++ app/helpers/venue_admin_helper.rb | 24 +++++++++++-- app/models/concerns/venue_link_file.rb | 34 ++++++++++++++++++- config/locales/en.yml | 4 +++ config/locales/zh_tw.yml | 4 +++ 5 files changed, 66 insertions(+), 4 deletions(-) diff --git a/app/controllers/admin/venue_admin_controller.rb b/app/controllers/admin/venue_admin_controller.rb index f87b394..ec1d341 100644 --- a/app/controllers/admin/venue_admin_controller.rb +++ b/app/controllers/admin/venue_admin_controller.rb @@ -2,6 +2,10 @@ class Admin::VenueAdminController < OrbitAdminController #alias_method :org_datetime_picker, :datetime_picker #include OrbitFormHelper + before_action :set_calendar_types,only: [:edit,:new] + def set_calendar_types + @calendar_types = CalendarType.all.collect{|v| [v.title,v.id.to_s]} + end def initialize(*) super ActionView::Helpers::FormBuilder.send(:prepend,VenueAdminHelper) diff --git a/app/helpers/venue_admin_helper.rb b/app/helpers/venue_admin_helper.rb index bfd9761..c3884fb 100644 --- a/app/helpers/venue_admin_helper.rb +++ b/app/helpers/venue_admin_helper.rb @@ -1,14 +1,20 @@ module VenueAdminHelper + include ActionView::Helpers::FormOptionsHelper include OrbitFormHelper alias :org_datetime_picker :datetime_picker def datetime_picker(*arg,**args) tp = org_datetime_picker(*arg,**args) venue = @template.instance_variable_get(:@venue) if !venue.nil? + base_name = @object.class.to_s.underscore + calendar_types = @template.instance_variable_get(:@calendar_types) style_attr = '' + calendar_show = !@object['calendar_dict'][arg[0]].nil? + hide_style = " style=\"display: none;\"" if !@object.send(arg[0].to_s+'_reminder') - style_attr = " style=\"display: none;\"" + style_attr = hide_style end + style_attr2 = calendar_show ? '' : hide_style onclick_func = "function reminder_switch(ele){ if ($(ele).prop('checked')){ $(ele).parents('.controls').find('.email_reminder').show() @@ -16,10 +22,22 @@ module VenueAdminHelper $(ele).parents('.controls').find('.email_reminder').hide() } };reminder_switch(this)" + onclick_func1 = "function reminder_switch_calendar(ele){ + if ($(ele).prop('checked')){ + $(ele).parents('.controls').find('.calendar_div').show() + }else{ + $(ele).parents('.controls').find('.calendar_div').hide() + } + };reminder_switch_calendar(this)" tp2 = content_tag(:div, :style => "float: left;flex-wrap: wrap;display: inline-flex;margin-left: 1em;align-items: center;") do - self.check_box((arg[0].to_s+'_reminder').to_sym,:onclick => onclick_func) + "寄送Email提醒
: 提前#{self.number_field((arg[0].to_s+'_reminder_day').to_sym, :class => 'input-mini')}天寄送
".html_safe + self.check_box((arg[0].to_s+'_reminder').to_sym,:onclick => onclick_func) + "#{I18n.t('venue_management.send_email_reminder')}
: #{I18n.t('venue_management.before')}#{self.number_field((arg[0].to_s+'_reminder_day').to_sym, :class => 'input-mini')}#{I18n.t('venue_management.day_send_email')}
".html_safe end - tp = content_tag(:div, tp , :style=>"float: left;")+ tp2 + calendar_type_key = "#{base_name}[calendar_data][type][#{arg[0]}]" + calendar_type = @object['calendar_data']['type'][arg[0]] rescue nil + tp3 = content_tag(:div, :style => "float: left;flex-wrap: wrap;display: inline-flex;margin-left: 1em;align-items: center;") do + check_box_tag("#{base_name}[calendar_data][key][]",arg[0],calendar_show,:onclick => onclick_func1) + "#{I18n.t('venue_management.add_to_calendar')}
: #{select_tag(calendar_type_key,options_for_select(calendar_types,calendar_type))}
".html_safe + end + tp = content_tag(:div, tp , :style=>"float: left;")+ tp2 + tp3 end tp end diff --git a/app/models/concerns/venue_link_file.rb b/app/models/concerns/venue_link_file.rb index 4a6fd0c..04f0619 100644 --- a/app/models/concerns/venue_link_file.rb +++ b/app/models/concerns/venue_link_file.rb @@ -26,10 +26,43 @@ module VenueLinkFile self.field v.name+'_reminder_day', type: Integer end end + self.field :calendar_data,type: Hash,default: {type:{},key:[]} + self.field :calendar_dict,type: Hash,default: {} + def calendar_id(field) + self.calendar_dict[field] + end 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 + 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 + 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} + if key_id.nil? + self.calendar_dict[key] = Event.create(update_dict.merge(create_user_id: current_user_id)).id + else + calendar = Event.find(key_id) + calendar.update_attributes(update_dict) + end + end + end self.venue_management_emails.each do |venue_mail| email = venue_mail.email if !email.nil? @@ -37,7 +70,6 @@ module VenueLinkFile end end self.venue_management_emails = [] - venue_main = self.class.to_s == 'VenueManagementMain' ? self : VenueManagementMain.find(self.venue_management_main_id) manager_emails = venue_main.manager_emails self.fields.values.each do|v| if v.type==Date || v.type==DateTime diff --git a/config/locales/en.yml b/config/locales/en.yml index dbc989d..7c5d01e 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -17,6 +17,10 @@ en: verification_failed: Verification Failed #驗證碼錯誤 venue_management: + send_email_reminder: Send email reminder + before: Before + day_send_email: day send email + add_to_calendar: Add to calendar manager: Manager contractor: Contractor land_number: Land number diff --git a/config/locales/zh_tw.yml b/config/locales/zh_tw.yml index 5fcc2b8..c4669a8 100644 --- a/config/locales/zh_tw.yml +++ b/config/locales/zh_tw.yml @@ -17,6 +17,10 @@ zh_tw: verification_failed: 驗證碼錯誤 venue_management: + send_email_reminder: 寄送Email提醒 + before: 提前 + day_send_email: 天寄送 + add_to_calendar: 加入行事曆 manager: 管理人 contractor: 廠商 land_number: 地號