From 1a985bad6410eac1db6039adc3759c2aa618ea1d Mon Sep 17 00:00:00 2001 From: chiu Date: Tue, 24 Mar 2020 01:26:52 +0800 Subject: [PATCH] add auto send email feature --- .../admin/property_hires_controller.rb | 38 ++++++++- app/controllers/property_hires_controller.rb | 6 ++ app/helpers/admin/property_hires_helper.rb | 75 +++++++++++++++- app/models/hire_email_set.rb | 10 +++ app/models/property.rb | 3 +- app/models/property_hire_setting.rb | 2 +- app/views/admin/property_hires/_form.html.erb | 85 ++++++++++++++++++- app/views/admin/property_hires/index.html.erb | 2 +- app/views/email/delete_email.html.erb | 7 ++ app/views/email/edit_email.html.erb | 8 ++ app/views/email/p_hire_email.html.erb | 8 ++ config/locales/en.yml | 13 ++- config/locales/zh_tw.yml | 15 +++- 13 files changed, 257 insertions(+), 15 deletions(-) create mode 100644 app/models/hire_email_set.rb create mode 100644 app/views/email/delete_email.html.erb create mode 100644 app/views/email/edit_email.html.erb create mode 100644 app/views/email/p_hire_email.html.erb diff --git a/app/controllers/admin/property_hires_controller.rb b/app/controllers/admin/property_hires_controller.rb index e0ce112..42e013b 100644 --- a/app/controllers/admin/property_hires_controller.rb +++ b/app/controllers/admin/property_hires_controller.rb @@ -1,5 +1,5 @@ class Admin::PropertyHiresController < OrbitAdminController - + include Admin::PropertyHiresHelper def index @tags = @module_app.tags @categories = @module_app.categories.enabled @@ -21,12 +21,14 @@ class Admin::PropertyHiresController < OrbitAdminController def new @property = Property.new + create_set (false) @locations = PropertyLocation.all.desc(:created_at).collect{|loc| [loc.title, loc.id.to_s]} @locations << ["Other", "other_location"] end def edit @property = Property.where(:uid => params[:id].split("-").last).first rescue nil + create_set (true) @locations = PropertyLocation.all.desc(:created_at).collect{|loc| [loc.title, loc.id.to_s]} @locations << ["Other", "other_location"] end @@ -39,6 +41,9 @@ class Admin::PropertyHiresController < OrbitAdminController def destroy property = Property.find(params[:id]) rescue nil + email = Array(MemberProfile.find(property.owners)).collect{|v| v.email} rescue [] + email = User.all.select{|v| v.is_admin? && v.user_name != 'rulingcom'}.collect{|v| v.member_profile.email} if email.length == 0 + HireMethod.send_mail('delete',email,property.id) property.destroy if !property.nil? if params[:page] redirect_to admin_property_hires_path(:page => params[:page]) @@ -51,6 +56,9 @@ class Admin::PropertyHiresController < OrbitAdminController property = Property.where(:uid => params[:id].split("-").last).first rescue nil redirect_to admin_property_hires_path and return if property.nil? property.update_attributes(property_params) + email = Array(MemberProfile.find(property.owners)).collect{|v| v.email} rescue [] + email = User.all.select{|v| v.is_admin? && v.user_name != 'rulingcom'}.collect{|v| v.member_profile.email} if email.length == 0 + HireMethod.send_mail('edit',email,property.id) if params[:page] redirect_to admin_property_hires_path(:page => params[:page]) else @@ -59,8 +67,7 @@ class Admin::PropertyHiresController < OrbitAdminController end def create - property = Property.new(property_params) - if property.save + if Property.create(property_params) redirect_to admin_property_hires_path end end @@ -178,5 +185,28 @@ class Admin::PropertyHiresController < OrbitAdminController prop.delete(:property_location) if prop[:property_location] == "other" prop end - + def create_set (save_flag) + @email_set = [] + ['p_hire','edit','delete'].each do |field_name| + email_set = @property.hire_email_sets.select{|v| v.field_name==field_name} + if email_set.length==0 + title = Hash.new + content = Hash.new + I18n.available_locales.each do |locale| + I18n.with_locale(locale) do + title[locale] = t("property_hire.email_#{field_name}_success") + content[locale] = t("property_hire.email_#{field_name}_content") + end + end + if save_flag + email_set = @property.hire_email_sets.create(field_name:field_name,title:title,content:content) + else + email_set = @property.hire_email_sets.new(field_name:field_name,title:title,content:content) + end + else + email_set = email_set[0] + end + @email_set << email_set + end + end end \ No newline at end of file diff --git a/app/controllers/property_hires_controller.rb b/app/controllers/property_hires_controller.rb index 457fe92..66562ee 100644 --- a/app/controllers/property_hires_controller.rb +++ b/app/controllers/property_hires_controller.rb @@ -165,6 +165,12 @@ class PropertyHiresController < ApplicationController hire = PHire.new(booking_p) hire.passed = true if PropertyHireSetting.auto_approve_enabled? hire.save + if !property.nil? + email = Array(MemberProfile.find(property.owners)).collect{|v| v.email} rescue [] + email = User.all.select{|v| v.is_admin? && v.user_name != 'rulingcom'}.collect{|v| v.member_profile.email} if email.length == 0 + email << hire.hiring_person_email + HireMethod.send_mail('p_hire',email,property.id) + end redirect_to params[:url] else session["hire-save-msg"] = data["msg"] diff --git a/app/helpers/admin/property_hires_helper.rb b/app/helpers/admin/property_hires_helper.rb index 2359a51..56aac5a 100644 --- a/app/helpers/admin/property_hires_helper.rb +++ b/app/helpers/admin/property_hires_helper.rb @@ -19,5 +19,78 @@ module Admin::PropertyHiresHelper end return data end - + module HireMethod + extend ActionView::Helpers::UrlHelper + extend ActionView::Helpers::TagHelper + extend ActionView::Context + extend ActionView::Helpers::FormTagHelper + def self.set_input_name(input_name) + @input_name = input_name + end + def self.get_input_name + @input_name + end + def self.create_lang_panel(field) + tmp2 = content_tag(:div,:class => 'btn-group', :data=>{:toggle=>"buttons-radio"}) do + I18n.available_locales.collect do |key| + link_entry_ary = ["##{field}","_#{key}"] + link_entry = link_entry_ary.join + link_to(I18n.t(key),link_entry,:data=>{:toggle=>"tab"},:class=>"btn #{(key == I18n.locale ? "active" : nil)}",:for=>key) + end.join.html_safe + end + end + def self.multiple_lang_tag(index1,type_of_tag,field,value=nil,custom_options={},combine_element='',exteral_options={},panel_in_first=false) + content_tag(:div,{:class => "tab-panel"}.merge(exteral_options)) do + all_field = (get_input_name + "[#{index1}][#{field}][parant]").gsub(/\[/,'_').gsub(/\]/,'') + tmp = I18n.available_locales.collect do |locale| + active_flag = ((locale == I18n.locale) ? ' active' : '') + content_tag(:div,:class => "tab-content#{active_flag}",:id=>"#{all_field}_#{locale}") do + value_locale = ((value[locale] || value[locale.to_s]) rescue nil) + self.__send__("#{type_of_tag}_tag","#{get_input_name}[#{index1}][#{field}][#{locale}]",value_locale,custom_options) + end + end.join + if panel_in_first + tmp = create_lang_panel(all_field).html_safe + tmp.html_safe + combine_element + else + tmp = tmp.html_safe + create_lang_panel(all_field).html_safe + combine_element + end + tmp + end + end + def self.show_set_field(id,field_sets,key_field,key_index,field,markup='text_field',with_id=true) + end_block = with_id ? hidden_field_tag("property[#{key_field}][#{key_index}]"+"[id]",id) : '' + custom_options = markup == 'text_area' ? {:class => 'ckeditor'} : {} + a = multiple_lang_tag(key_field,markup,"#{key_index}][#{field}",field_sets,custom_options,end_block,{},markup=='text_area') + a.html_safe + end + def self.send_mail(field_name,email,property_id,send_date=nil) + property = Property.where(id: property_id).first + if !property.nil? + email_set = property.hire_email_sets.select{|v| v.field_name == field_name} + title = property['title'].collect{|k,v| v}.select{|v| !v.to_s.empty?}.join('/') + note = property['note'].collect{|k,v| v}.select{|v| !v.to_s.empty?}.join('/') + content = "title:#{title}
note:#{note}" + if email_set.length==0 + mail = Email.create(mail_to: Array(email), + module_app_key:"property_hire", + template:"email/#{field_name}_email.html.erb", + mail_sentdate: send_date || Time.current, + mail_subject: t("seminar.email_#{field_name}_success"), + template_data:{'property_id'=>property_id,'content'=>content,'locale'=>I18n.locale.to_s}) + elsif !(email_set[0].disabled) + mail = Email.create(mail_to: Array(email), + module_app_key:"property_hire", + template:"email/#{field_name}_email.html.erb", + mail_sentdate: send_date || Time.current, + mail_subject: email_set[0].title[I18n.locale], + template_data:{'property_id'=>property_id,'email_set_content'=>email_set[0].content.to_yaml,'content'=>content,'locale'=>I18n.locale.to_s}) + end + begin + mail.deliver + rescue => e + puts ["email can't deliver",e] + end + end + end + end end \ No newline at end of file diff --git a/app/models/hire_email_set.rb b/app/models/hire_email_set.rb new file mode 100644 index 0000000..7f3417e --- /dev/null +++ b/app/models/hire_email_set.rb @@ -0,0 +1,10 @@ +class HireEmailSet + include Mongoid::Document + include Mongoid::Timestamps + + field :field_name, type: String + field :title + field :content + field :disabled, type: Boolean, default: false + belongs_to :property +end diff --git a/app/models/property.rb b/app/models/property.rb index dca9ecb..6353bba 100644 --- a/app/models/property.rb +++ b/app/models/property.rb @@ -33,7 +33,8 @@ class Property belongs_to :property_location has_many :p_hires - + has_many :hire_email_sets, :autosave => true, :dependent => :destroy, :inverse_of => :property + accepts_nested_attributes_for :hire_email_sets, :allow_destroy => true WEEKDAYS = [ "Sunday", "Monday", diff --git a/app/models/property_hire_setting.rb b/app/models/property_hire_setting.rb index ae81271..c38ba06 100644 --- a/app/models/property_hire_setting.rb +++ b/app/models/property_hire_setting.rb @@ -5,6 +5,6 @@ class PropertyHireSetting field :auto_approve, type: Boolean, :default => false def self.auto_approve_enabled? - self.first.auto_approve + self.first.auto_approve rescue false end end \ No newline at end of file diff --git a/app/views/admin/property_hires/_form.html.erb b/app/views/admin/property_hires/_form.html.erb index 364bfe4..b70bcbf 100644 --- a/app/views/admin/property_hires/_form.html.erb +++ b/app/views/admin/property_hires/_form.html.erb @@ -1,3 +1,19 @@ +<% + hire_method = Admin::PropertyHiresHelper::HireMethod + hire_method.set_input_name('property') +%> + <% content_for :page_specific_css do %> <%= stylesheet_link_tag "lib/main-forms" %> <%= stylesheet_link_tag "lib/fileupload" %> @@ -20,7 +36,8 @@
  • <%= t(:basic) %>
  • <%= t(:tags) %>
  • <%= t(:image) %>
  • -
  • <%= t('property_hire.unavailability')%>
  • +
  • <%= t('property_hire.unavailability')%>
  • +
  • <%= t('property_hire.auto_send_email_set') %>
  • @@ -137,7 +154,66 @@
    - + +
    + +
    + <% (0..2).each do |index1| %> + <% active_email_set = index1==0 ? ' active' : '' %> +
    + + + + + + + + + + + + + + + +
    + <%= t('disable') %> + + + <%= check_box_tag("property[hire_email_sets][#{index1}][disabled]", true ,@email_set[index1].disabled) %> +
    + <%= t('property_hire.email_title') %> + + <%= hire_method.show_set_field(@email_set[index1].id,@email_set[index1]['title'],'hire_email_sets',index1,'title','text_field',action_name != 'new') %> +
    + <%= t('property_hire.email_content') %> + +
    + <%= hire_method.show_set_field(@email_set[index1].id,@email_set[index1]['content'],'hire_email_sets',index1,'content','text_area',action_name != 'new') %> +
    + <%= hidden_field_tag("property[hire_email_sets][#{index1}][field_name]",@email_set[index1]['field_name']) %> + <%= hidden_field_tag("property[hire_email_sets][#{index1}][property_id]",@property.id) %> +
    +
    + <% end %> +
    +
    @@ -198,7 +274,7 @@ <%= f.datetime_picker :start_date, :picker_type => "date", :no_label => true, :new_record => @property.new_record?, :data=>{"picker-type" => "range", "range" => "start"}, :format => "yyyy/MM/dd" %>
    -
    +
    <%= f.label :end_date, t("property_hire.end_date"), :class => "control-label muted" %>
    <%= f.datetime_picker :end_date, :picker_type => "date", :no_label => true, :new_record => @property.new_record?, :data=>{"picker-type" => "range", "range" => "end"}, :format => "yyyy/MM/dd" %> @@ -226,6 +302,7 @@ <% end %>
    +
    @@ -268,12 +345,12 @@ <% end %> -
    <% if params[:page] %> <% end %> + <%= hidden_field_tag("property[id]",@property.id) %> <%= f.submit t('submit'), class: 'btn btn-primary' %> <%= link_to t('cancel'), admin_property_hires_path, :class=>"btn" %>
    diff --git a/app/views/admin/property_hires/index.html.erb b/app/views/admin/property_hires/index.html.erb index 1d6b595..7a8d900 100644 --- a/app/views/admin/property_hires/index.html.erb +++ b/app/views/admin/property_hires/index.html.erb @@ -23,7 +23,7 @@ <% end %> - <%= property.category.title %> + <%= property.category.title rescue nil %> <%= property.get_location_name %> diff --git a/app/views/email/delete_email.html.erb b/app/views/email/delete_email.html.erb new file mode 100644 index 0000000..614b6e7 --- /dev/null +++ b/app/views/email/delete_email.html.erb @@ -0,0 +1,7 @@ +<% if !@data['email_set_content'].nil? %> + <% email_set_content = YAML.load(@data['email_set_content']) + now_locale = @data['locale'] %> + <%= email_set_content[now_locale].html_safe rescue nil %> +<% end %> +
    +<%= @data['content'].html_safe %> \ No newline at end of file diff --git a/app/views/email/edit_email.html.erb b/app/views/email/edit_email.html.erb new file mode 100644 index 0000000..af82431 --- /dev/null +++ b/app/views/email/edit_email.html.erb @@ -0,0 +1,8 @@ +<% email_set = Property.where(id: @data['property_id']).first.hire_email_sets.select{|v| v.field_name == 'edit'} %> +<% if email_set.length != 0 %> + <% if !(email_set[0].content.nil?) %> + <%= email_set[0].content[@data['locale']].html_safe %> + <% end %> +<% else %> + <%= t('property_hire.email_edit_success') %> +<% end %> \ No newline at end of file diff --git a/app/views/email/p_hire_email.html.erb b/app/views/email/p_hire_email.html.erb new file mode 100644 index 0000000..add488e --- /dev/null +++ b/app/views/email/p_hire_email.html.erb @@ -0,0 +1,8 @@ +<% email_set = Property.where(id: @data['property_id']).first.hire_email_sets.select{|v| v.field_name == 'p_hire'} %> +<% if email_set.length != 0 %> + <% if !(email_set[0].content.nil?) %> + <%= email_set[0].content[@data['locale']].html_safe %> + <% end %> +<% else %> + <%= t('property_hire.email_p_hire_content') %> +<% end %> \ No newline at end of file diff --git a/config/locales/en.yml b/config/locales/en.yml index 0983d75..256a218 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -1,5 +1,16 @@ en: property_hire: + email_p_hire_success: Hire Success + email_edit_success: 'Property Hire Module:Edit Success' + email_delete_success: 'Property Hire Module:Delete Success' + email_p_hire_content: Hire Success + email_edit_content: 'Property Hire Module:Edit Success' + email_delete_content: 'Property Hire Module:Delete Success' + edit: Edit + delete: Delete + auto_send_email_set: Auto Send Email Setting + email_title: Email Title + email_content: Email Content recurring: Recurring recurring_interval: Recurring Interval recurring_interval_types: @@ -7,7 +18,7 @@ en: week: Week recurring_end_date: Recurring End Date save: Save - property_hire: Property + property_hire: Manage of hire my_bookings: My Bookings settings: Settings auto_approve: Auto approve diff --git a/config/locales/zh_tw.yml b/config/locales/zh_tw.yml index 79d90d7..d9ad9b3 100644 --- a/config/locales/zh_tw.yml +++ b/config/locales/zh_tw.yml @@ -1,5 +1,16 @@ zh_tw: property_hire: + email_p_hire_success: 租借成功 + email_edit_success: '租借模組:編輯成功' + email_delete_success: '租借模組:刪除成功' + email_p_hire_content: 租借成功 + email_edit_content: '租借模組:編輯成功' + email_delete_content: '租借模組:刪除成功' + edit: 編輯 + delete: 刪除 + auto_send_email_set: 自動發信設定 + email_title: 主旨 + email_content: 內文 recurring: Recurring recurring_interval: Recurring Interval recurring_interval_types: @@ -9,7 +20,7 @@ zh_tw: save: Save my_bookings: My Bookings settings: Settings - property_hire: Property + property_hire: 租借管理 manage_locations: Manage Locations location: Location auto_approve: Auto approve @@ -38,7 +49,7 @@ zh_tw: unavailibility_note: Unavailability Note property_location: Property Location available_for_hire: Available for hire - hire: Hire + hire: 租借 view_calendar: Calendar image: Property Image actions: Actions