add auto send email feature

This commit is contained in:
chiu 2020-03-24 01:26:52 +08:00
parent 0bd0a61986
commit 1a985bad64
13 changed files with 257 additions and 15 deletions

View File

@ -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

View File

@ -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"]

View File

@ -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}<br>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

View File

@ -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

View File

@ -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",

View File

@ -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

View File

@ -1,3 +1,19 @@
<%
hire_method = Admin::PropertyHiresHelper::HireMethod
hire_method.set_input_name('property')
%>
<style type="text/css">
.tab-panel > .tab-content{
display: none;
}
.tab-panel > .tab-content.active{
display: block;
}
.tab-panel{
display: flex;
flex-wrap: wrap;
}
</style>
<% content_for :page_specific_css do %>
<%= stylesheet_link_tag "lib/main-forms" %>
<%= stylesheet_link_tag "lib/fileupload" %>
@ -20,7 +36,8 @@
<li class="active"><a href="#basic" data-toggle="tab"><%= t(:basic) %></a></li>
<li><a href="#tag" data-toggle="tab"><%= t(:tags) %></a></li>
<li><a href="#imageupload" data-toggle="tab"><%= t(:image) %></a></li>
<li><a href="#unavailability" data-toggle="tab"><%= t('property_hire.unavailability')%></a></li>
<li><a href="#unavailability" data-toggle="tab"><%= t('property_hire.unavailability')%></a></li>
<li><a href="#auto_send_email_set" data-toggle="tab"><%= t('property_hire.auto_send_email_set') %></a></li>
</ul>
<!-- Module -->
<div class="tab-content module-area">
@ -137,7 +154,66 @@
</div>
</div>
</div>
<!-- Email Set Module -->
<div class="tab-pane fade" id="auto_send_email_set" style="padding: 1.2em;">
<ul class="nav nav-pills module-nav">
<li class="active">
<a href="#email_set0" data-toggle="tab">
<%= t('property_hire.hire') %>
</a>
</li>
<li class="">
<a href="#email_set1" data-toggle="tab">
<%= t('property_hire.edit') %>
</a>
</li>
<li class="">
<a href="#email_set2" data-toggle="tab">
<%= t('property_hire.delete') %>
</a>
</li>
</ul>
<div class="tab-content">
<% (0..2).each do |index1| %>
<% active_email_set = index1==0 ? ' active' : '' %>
<div class="tab-pane<%= active_email_set %>" id="email_set<%= index1 %>" style="padding: 1.2em;">
<table style="width:100%;">
<tbody>
<tr>
<td>
<%= t('disable') %>
</td>
<td>
<input type="hidden" class="field_set" name='<%= "property[hire_email_sets][#{index1}][disabled]" %>' value="false">
<%= check_box_tag("property[hire_email_sets][#{index1}][disabled]", true ,@email_set[index1].disabled) %>
</td>
</tr>
<tr>
<td>
<%= t('property_hire.email_title') %>
</td>
<td>
<%= hire_method.show_set_field(@email_set[index1].id,@email_set[index1]['title'],'hire_email_sets',index1,'title','text_field',action_name != 'new') %>
</td>
</tr>
<tr>
<td>
<%= t('property_hire.email_content') %>
</td>
<td>
<div class="form-group">
<%= hire_method.show_set_field(@email_set[index1].id,@email_set[index1]['content'],'hire_email_sets',index1,'content','text_area',action_name != 'new') %>
</div>
<%= 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) %>
</td>
</tr>
</tbody>
</table>
</div>
<% end %>
</div>
</div>
<!-- unavailability -->
<div class="tab-pane fade" id="unavailability">
<div class="control-group">
@ -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" %>
</div>
</div>
<div class="control-group">
<div class="control-group">
<%= f.label :end_date, t("property_hire.end_date"), :class => "control-label muted" %>
<div class="controls">
<%= 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 %>
</div>
</div>
</div>
</div>
<!-- Language Tabs -->
<div class="nav-name"><strong><%= t(:language) %></strong></div>
@ -268,12 +345,12 @@
</div>
<% end %>
</div>
</div>
<!-- Form Actions -->
<div class="form-actions">
<% if params[:page] %>
<input type="hidden" name="page" value="<%= 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" %>
</div>

View File

@ -23,7 +23,7 @@
<% end %>
</td>
<td>
<%= property.category.title %>
<%= property.category.title rescue nil %>
</td>
<td>
<%= property.get_location_name %>

View File

@ -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 %>
<br>
<%= @data['content'].html_safe %>

View File

@ -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 %>

View File

@ -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 %>

View File

@ -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

View File

@ -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