From 4f9cd8e40fa86ee0f7bfaf24930384777e0899f5 Mon Sep 17 00:00:00 2001 From: chiu Date: Tue, 4 Aug 2020 17:04:04 +0800 Subject: [PATCH] finish email reminder --- .../admin/venue_admin_controller.rb | 11 ++++ .../venue_management_contracts_controller.rb | 2 +- .../venue_management_invitings_controller.rb | 2 +- ...enue_management_memorabilias_controller.rb | 2 +- .../admin/venue_managements_controller.rb | 2 +- app/helpers/venue_admin_helper.rb | 26 +++++++++ app/models/concerns/venue_link_file.rb | 55 +++++++++++++++++++ app/models/venue_management_email.rb | 9 +++ app/models/venue_management_main.rb | 7 ++- app/views/email/reminder_email.html.erb | 3 + 10 files changed, 114 insertions(+), 5 deletions(-) create mode 100644 app/controllers/admin/venue_admin_controller.rb create mode 100644 app/helpers/venue_admin_helper.rb create mode 100644 app/models/venue_management_email.rb create mode 100644 app/views/email/reminder_email.html.erb diff --git a/app/controllers/admin/venue_admin_controller.rb b/app/controllers/admin/venue_admin_controller.rb new file mode 100644 index 0000000..f87b394 --- /dev/null +++ b/app/controllers/admin/venue_admin_controller.rb @@ -0,0 +1,11 @@ +# encoding: utf-8 +class Admin::VenueAdminController < OrbitAdminController + #alias_method :org_datetime_picker, :datetime_picker + #include OrbitFormHelper + def initialize(*) + super + ActionView::Helpers::FormBuilder.send(:prepend,VenueAdminHelper) + @venue = 'venue' + end + +end \ No newline at end of file diff --git a/app/controllers/admin/venue_management_contracts_controller.rb b/app/controllers/admin/venue_management_contracts_controller.rb index 18d19f6..5333c0e 100644 --- a/app/controllers/admin/venue_management_contracts_controller.rb +++ b/app/controllers/admin/venue_management_contracts_controller.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -class Admin::VenueManagementContractsController < OrbitAdminController +class Admin::VenueManagementContractsController < Admin::VenueAdminController include Admin::VenueManagementsHelper before_action ->(module_app = @app_title) { set_variables module_app } before_action :set_venue_management_contract, only: [:edit, :update, :destroy] diff --git a/app/controllers/admin/venue_management_invitings_controller.rb b/app/controllers/admin/venue_management_invitings_controller.rb index d1a0c9f..3abd408 100644 --- a/app/controllers/admin/venue_management_invitings_controller.rb +++ b/app/controllers/admin/venue_management_invitings_controller.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -class Admin::VenueManagementInvitingsController < OrbitAdminController +class Admin::VenueManagementInvitingsController < Admin::VenueAdminController include Admin::VenueManagementsHelper before_action ->(module_app = @app_title) { set_variables module_app } before_action :set_venue_management_inviting, only: [:edit, :update, :destroy] diff --git a/app/controllers/admin/venue_management_memorabilias_controller.rb b/app/controllers/admin/venue_management_memorabilias_controller.rb index 3592cc2..5a1410f 100644 --- a/app/controllers/admin/venue_management_memorabilias_controller.rb +++ b/app/controllers/admin/venue_management_memorabilias_controller.rb @@ -1,4 +1,4 @@ -class Admin::VenueManagementMemorabiliasController < OrbitAdminController +class Admin::VenueManagementMemorabiliasController < Admin::VenueAdminController include Admin::VenueManagementsHelper before_action ->(module_app = @app_title) { set_variables module_app } before_action :set_venue_management_memorabilia, only: [:edit, :update, :destroy] diff --git a/app/controllers/admin/venue_managements_controller.rb b/app/controllers/admin/venue_managements_controller.rb index 7980fe0..04e2820 100644 --- a/app/controllers/admin/venue_managements_controller.rb +++ b/app/controllers/admin/venue_managements_controller.rb @@ -1,5 +1,5 @@ # encoding: utf-8 -class Admin::VenueManagementsController < OrbitAdminController +class Admin::VenueManagementsController < Admin::VenueAdminController require 'axlsx' helper Admin::VenueManagementsFieldHelper diff --git a/app/helpers/venue_admin_helper.rb b/app/helpers/venue_admin_helper.rb new file mode 100644 index 0000000..bfd9761 --- /dev/null +++ b/app/helpers/venue_admin_helper.rb @@ -0,0 +1,26 @@ +module VenueAdminHelper + 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? + style_attr = '' + if !@object.send(arg[0].to_s+'_reminder') + style_attr = " style=\"display: none;\"" + end + onclick_func = "function reminder_switch(ele){ + if ($(ele).prop('checked')){ + $(ele).parents('.controls').find('.email_reminder').show() + }else{ + $(ele).parents('.controls').find('.email_reminder').hide() + } + };reminder_switch(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 + end + tp = content_tag(:div, tp , :style=>"float: left;")+ tp2 + end + tp + end +end \ No newline at end of file diff --git a/app/models/concerns/venue_link_file.rb b/app/models/concerns/venue_link_file.rb index e8771c3..40c36ea 100644 --- a/app/models/concerns/venue_link_file.rb +++ b/app/models/concerns/venue_link_file.rb @@ -1,6 +1,18 @@ module VenueLinkFile extend ActiveSupport::Concern + 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 included do + self.include VenueMethod 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") @@ -8,5 +20,48 @@ module VenueLinkFile 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 + 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 + 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 + self.venue_management_emails.each do |venue_mail| + email = venue_mail.email + if !email.nil? + email.destroy + 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 + 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 + 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, + template_data:{'title'=>title,'send_time'=>send_time,'locale'=>I18n.locale}) + 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 end end \ No newline at end of file diff --git a/app/models/venue_management_email.rb b/app/models/venue_management_email.rb new file mode 100644 index 0000000..0121ea0 --- /dev/null +++ b/app/models/venue_management_email.rb @@ -0,0 +1,9 @@ +class VenueManagementEmail + include Mongoid::Document + include Mongoid::Timestamps + + field :email_id + def email + Email.find(self.email_id) rescue nil + end +end \ No newline at end of file diff --git a/app/models/venue_management_main.rb b/app/models/venue_management_main.rb index cb8d22b..f8b930f 100644 --- a/app/models/venue_management_main.rb +++ b/app/models/venue_management_main.rb @@ -57,6 +57,9 @@ class VenueManagementMain accepts_nested_attributes_for :venue_management_email_sets, :allow_destroy => true accepts_nested_attributes_for :venue_management_signup_field_customs, :allow_destroy => true include VenueLinkFile + def manager_emails + MemberProfile.where(:id.in => self.manager_ids).collect{|v| v.email} + end def self.time_range(date1 = null, date2 = null) if !date1.blank? @@ -88,5 +91,7 @@ class VenueManagementMain value = values.detect {|value| value.venue_management_signup_field_id == field_id} rescue nil value ? value : nil end - + def case_no + '' + end end diff --git a/app/views/email/reminder_email.html.erb b/app/views/email/reminder_email.html.erb new file mode 100644 index 0000000..65c1171 --- /dev/null +++ b/app/views/email/reminder_email.html.erb @@ -0,0 +1,3 @@ +<%= @data['title'] %> +
+<%= @data['send_time'].strftime("%Y/%m/%d") %> \ No newline at end of file