Add copy feature.

This commit is contained in:
邱博亞 2024-05-14 08:59:52 +08:00
parent 76cd916a26
commit bd8f88afa3
9 changed files with 275 additions and 15 deletions

View File

@ -45,6 +45,12 @@ class Admin::PropertyHiresController < OrbitAdminController
@bookings = PHire.where(:hiring_person_id => current_user.member_profile.id.to_s).desc(:created_at).page(params[:page]).per(10)
end
def copy
@property = Property.where(:uid => params[:id].split("-").last).first.clone_new #rescue nil
create_set (false)
@locations = PropertyLocation.all.desc(:created_at).collect{|loc| [loc.title, loc.id.to_s]}
@locations << ["Other", "other_location"]
end
def new
@property = Property.new
create_set (false)
@ -116,7 +122,11 @@ class Admin::PropertyHiresController < OrbitAdminController
end
def create
if Property.create(property_params)
tmp_property_params = property_params
if tmp_property_params["copy_id"] && tmp_property_params["clone_p_hires"].blank?
tmp_property_params["except_clone_relations"] = ["p_hires","p_hire_field_values"]
end
if Property.create(tmp_property_params)
redirect_to admin_property_hires_path
end
end

View File

@ -59,6 +59,8 @@ class Property
field :enable_fields_sort , type: Boolean, default: false
field :custom_field_names, type: Array
field :default_field_names, type: Array
field :copy_id
field :except_clone_relations, :type=>Array, :default => []
belongs_to :property_location
has_many :p_hires
has_many :hire_email_sets, :autosave => true, :dependent => :destroy, :inverse_of => :property
@ -108,11 +110,17 @@ class Property
max_position = -1 if max_position.nil?
self.order_position = max_position + 1
@no_validate = true
self.default_field_names = self.get_all_fields(true)
self.custom_field_names = self.get_all_fields(true)
if self.copy_id.present?
self.clone_new(true)
self.created_at = DateTime.now
self.updated_at = DateTime.now
else
self.default_field_names = self.get_all_fields(true)
self.custom_field_names = self.get_all_fields(true)
end
end
before_save do
unless @no_validate
unless @no_validate || self.new_record?
self.custom_field_names = [] if self.custom_field_names.nil?
self.default_field_names = self.get_all_fields(true)
self.class::FIELDSNAME.each do |f|
@ -532,8 +540,8 @@ class Property
def carousel_image_width
(self.custom_carousel_image_width.blank? ? PropertyHireSetting.first.carousel_image_width : self.custom_carousel_image_width) rescue "75%"
end
def get_attribute_value(attribute_field, signup_id)
SeminarSignupValue.find_by(seminar_signup_field_id: attribute_field.id, seminar_signup_id: signup_id)
def get_attribute_value(attribute_field, p_hire_id)
PHireFieldValue.find_by(p_hire_field_id: attribute_field.id, p_hire_id: p_hire_id)
end
def get_attribute_values(attribute_type=nil)
@ -574,4 +582,231 @@ class Property
user = OrbitHelper.current_user
(!disable_no_logins_view_calendar && self.can_be_hired) || user
end
def clone_new(clone_mode=false)
@records_all = {}
if clone_mode
clone_target = self.class.find(object.copy_id) rescue nil
else
clone_target = self
end
property,clone_target = clone_new_for_object(self,clone_target,clone_mode)
property
end
def fix_uploader(clone_relation, r, f)
if !@clone_mode || (clone_relation[f].blank? && clone_relation.send(f).blank?)
clone_relation[f] = r[f]
if @clone_mode
clone_relation.send(f).retrieve_from_store!(r[f])
else
org_id = clone_relation.id
clone_relation.id = r.id
clone_relation.send(f).retrieve_from_store!(r[f])
clone_relation.id = org_id
end
source_filepath = r.send(f).file.file
if @clone_mode
dest_filepath = clone_relation.send(f).file.file
FileUtils.mkdir_p(File.dirname(dest_filepath))
FileUtils.cp(source_filepath,dest_filepath)
end
elsif (clone_relation.send(f).file rescue nil)
clone_relation[f] = File.basename(clone_relation.send(f).file.file.to_s)
end
end
def clone_new_for_object(object,clone_target=nil,clone_mode=false,fix_only=false)
@except_clone_relations ||= self.except_clone_relations
if clone_mode || fix_only
new_object = object
clone_target = object.class.find(object.copy_id) rescue nil if clone_target.nil?
else
clone_target = object if clone_target.nil?
new_object = object.dup
end
return if @except_clone_relations.include?(new_object.class.to_s.underscore)
@records_all["#{new_object.class.to_s.underscore}_ids"] = {} if @records_all["#{new_object.class.to_s.underscore}_ids"].nil?
begin
@records_all["#{new_object.class.to_s.underscore}_ids"][clone_target.id] = new_object
rescue
nil
end
if !clone_target.nil? && !new_object.nil?
unless fix_only
if clone_mode
initialize_fields = []
if new_object.fields.keys.include?("uid")
new_object.generate_uid
end
else
initialize_fields = ["uid","created_at","updated_at"]
end
initialize_fields.each do |f|
new_object.send("#{f}=",nil) if new_object.fields.keys.include?(f)
end
end
relations_fields = clone_target.relations.except("impressions").keys
all_fields = clone_target.fields.keys - relations_fields
all_fields = all_fields - relations_fields.map{|k| "#{k}_id"}
all_fields = all_fields - relations_fields.map{|k| "#{k.singularize}_ids"}
new_object_class_name = new_object.class.to_s.underscore
unless @parent_level
unsort_relation_keys = clone_target.relations.keys - ['taggings']
fields_to_delete = [new_object_class_name]
tmp_relations_fields = [new_object_class_name]
while relations_fields.count > 0
tmp_singularize_relations_fields = tmp_relations_fields.map{|f| f.singularize}
approve_append = nil
relations_fields.each do |k|
belongs_to_class = clone_target.relations[k].class_name.constantize.relations.select{|k,v| v.macro == :belongs_to}.keys
has_many_class = clone_target.relations[k].class_name.constantize.relations.select{|k,v| v.macro.to_s.start_with?('has') }.keys
if (belongs_to_class - tmp_singularize_relations_fields).count == 0
other_has_many_class = (has_many_class - unsort_relation_keys)
if other_has_many_class.count == 0
tmp_relations_fields << k
else
org_k = k.to_s
result = other_has_many_class.map do |k|
belongs_to_class = k.classify.constantize.relations.select{|kk,v| v.macro == :belongs_to}.keys
has_many_class = k.classify.constantize.relations.select{|kk,v| v.macro.to_s.start_with?('has') }.keys
if (belongs_to_class - tmp_singularize_relations_fields - [org_k]).count == 0
true
else
fields_to_delete = fields_to_delete.concat(belongs_to_class)
tmp_relations_fields.concat(belongs_to_class)
false
end
end
if result.select{|t| !t}.count == 0
if (fields_to_delete.map{|f| f.pluralize} - tmp_relations_fields).count == 0
tmp_relations_fields << k
elsif clone_target.relations[k].class_name.constantize.fields.keys.include?("key")
tmp_relations_fields << k
elsif (clone_target.relations[k].class_name.constantize.relations.keys.map{|f| f.singularize} & fields_to_delete).count != 0
approve_append = k
end
end
end
elsif !unsort_relation_keys.include?(clone_target.relations[k].class_name.underscore) && !unsort_relation_keys.include?(clone_target.relations[k].class_name.underscore.pluralize)
tmp_relations_fields << k
end
end
tmp_relations_fields << approve_append if approve_append.present?
approve_append = nil
relations_fields = relations_fields - tmp_relations_fields
relations_fields -= @relations_fields if @relations_fields
end
relations_fields = tmp_relations_fields
fields_to_delete.each{|f| relations_fields.delete(f)}
end
if @parent_level
relations_fields -= @relations_fields
else
@clone_mode = clone_mode
@relations_fields = relations_fields
end
@parent_level = true
if clone_mode
all_fields.each do |f|
next if f == "uid"
unless new_object.send("#{f}_changed?") && new_object.send("#{f}_changed_from_default?")
unless fix_only
new_object.send("#{f}=",clone_target.send(f))
end
if new_object.class.uploaders.include?(f.to_sym)
fix_uploader(new_object, clone_target, f)
end
end
end
else
all_fields.each do |f|
if new_object.class.uploaders.include?(f.to_sym)
fix_uploader(new_object, clone_target, f)
end
end
end
relations_fields.each do |f|
no_dup_flag = false
if clone_target.relations[f].macro == :belongs_to || clone_target.relations[f].macro == :has_one
no_dup_flag = new_object.send(f).present?
elsif clone_target.relations[f].macro == :has_many || clone_target.relations[f].macro == :has_and_belongs_to_many
no_dup_flag = new_object.send(f).to_a.count != 0
elsif clone_target.relations[f].macro == :embeds_many #Fix localize fields
if new_object.send(f).to_a.count != 0
need_fix_fields = new_object.send(f).to_a[0].fields.select{|k,v| (v.options[:localize] rescue false)}.keys
locale = I18n.locale.to_s
embeded_records = new_object.send(f).map do |embeded_record|
need_fix_fields.each do |f|
if (embeded_record[f][locale].class != String rescue false)
embeded_record.send("#{f}_translations=",embeded_record[f][locale])
else
embeded_record.send("#{f}_translations=",embeded_record[f])
end
end
embeded_record
end
new_object.send("#{f}=",embeded_records)
end
end
if clone_target.relations[f].macro == :belongs_to || clone_target.relations[f].class_name == "MemberProfile"
if f == 'taggable'
map_f = @taggable_name
else
map_f = f
end
if @records_all["#{map_f}_ids"].nil?
new_object.send("#{f}_id=",clone_target.send("#{f}_id"))
else
obj = @records_all["#{map_f}_ids"][clone_target.send("#{f}_id")]
if obj
new_object.send("#{f}_id=", obj.id)
new_object.send("#{f}=", obj)
end
end
elsif clone_target.relations[f].macro == :has_one
next if @except_clone_relations.include?(f)
need_clone_relation = clone_target.send(f)
next if need_clone_relation.nil?
clone_relation = new_object.send(f)
if clone_relation.nil?
clone_relation, need_clone_relation = clone_new_for_object(need_clone_relation.dup, need_clone_relation, clone_mode, fix_only)
else
clone_relation, r = clone_new_for_object(clone_relation, r, clone_mode, true)
end
new_object.send("#{f}=",clone_relation)
elsif clone_target.relations[f].macro == :has_many || clone_target.relations[f].macro == :has_and_belongs_to_many
next if @except_clone_relations.include?(f)
clone_relations = []
need_clone_relations = clone_target.send(f).asc(:_id).to_a
file_flag = false
if f == 'taggings'
@taggable_name = new_object.class.to_s.underscore
end
need_clone_relations.each_with_index do |r,i|
clone_relation = new_object.send(f)[i]
if clone_relation.nil?
clone_relation, r = clone_new_for_object(r.dup, r, clone_mode, fix_only)
else
clone_relation, r = clone_new_for_object(clone_relation, r, clone_mode, true)
end
clone_relations << clone_relation
end
if !no_dup_flag || (no_dup_flag && file_flag)
new_object_relations = new_object.send(f).to_a
new_object_relations_count = new_object_relations.count
if new_object_relations_count != 0
if clone_relations.count > new_object_relations_count
clone_relations = clone_relations[0...new_object_relations_count]
else
clone_relations = clone_relations.concat(new_object.send(f)[clone_relations.count...new_object_relations_count])
end
new_object.send("#{f}=",clone_relations)
else
new_object.send("#{f}=",clone_relations)
end
end
end
end
new_object.copy_id = clone_target.id if new_object.fields.keys.include?("copy_id")
return new_object, clone_target
end
end
end

View File

@ -1,6 +1,7 @@
<%
hire_method = Admin::PropertyHiresHelper::HireMethod
hire_method.set_input_name('property')
with_id = !(@property.new_record?)
%>
<style type="text/css">
.tab-panel > .tab-content{
@ -309,7 +310,7 @@
<%= 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') %>
<%= hire_method.show_set_field(@email_set[index1].id,@email_set[index1]['title'],'hire_email_sets',index1,'title','text_field',with_id) %>
</td>
</tr>
<tr>
@ -318,10 +319,12 @@
</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') %>
<%= hire_method.show_set_field(@email_set[index1].id,@email_set[index1]['content'],'hire_email_sets',index1,'content','text_area',with_id) %>
</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) %>
<% unless @property.new_record? %>
<%= hidden_field_tag("property[hire_email_sets][#{index1}][property_id]",@property.id) %>
<% end %>
</td>
</tr>
</tbody>
@ -338,9 +341,7 @@
<%= f.check_box :set_unavailibility %>
</div>
</div>
<% if @property.new_record? %>
<div id="set_unavailibility_div" style="display: none;">
<% elsif @property.set_unavailibility %>
<% if @property.set_unavailibility %>
<div id="set_unavailibility_div">
<% else %>
<div id="set_unavailibility_div" style="display: none;">
@ -621,7 +622,9 @@
<% referer = request.referer rescue nil %>
<% referer = get_referer_url if referer.blank? || request.host != URI.parse(URI.encode(referer)).host %>
<input type="hidden" name="referer_url" value="<%= referer %>">
<%= hidden_field_tag("property[id]",@property.id) %>
<% unless @property.new_record? %>
<%= hidden_field_tag("property[id]", @property.id) %>
<% end %>
<%= f.submit t('submit'), class: 'btn btn-primary' %>
<%= link_to t('cancel'), referer, :class=>"btn" %>
</div>

View File

@ -1,4 +1,4 @@
<% if form_file.nil? || form_file.new_record? %>
<% if form_file.nil? || form_file.file.blank? %>
<div class="fileupload fileupload-new start-line" data-provides="fileupload">
<% else %>
<div class="fileupload fileupload-exists start-line" data-provides="fileupload">
@ -16,7 +16,7 @@
</span>
<div class="uneditable-input input-medium">
<i class="icon-file fileupload-exists"></i>
<span class="fileupload-preview"><%= (form_file.nil? || form_file.new_record? || form_file.file.blank?) ? t(:select_file) : t(:change_file) %></span>
<span class="fileupload-preview"><%= (form_file.nil? || form_file.file.blank?) ? t(:select_file) : t(:change_file) %></span>
</div>
</label>
<span class="add-on icons-pencil" title='<%= t(:alternative) %>'></span>

View File

@ -15,6 +15,7 @@
<div class="quick-edit">
<ul class="nav nav-pills">
<li><a href="<%= edit_admin_property_hire_path(property, :page => params[:page]) %>"><%= t(:edit) %></a></li>
<li><a href="<%= copy_admin_property_hire_path(property, :page => params[:page]) %>"><%= t("property_hire.copy") %></a></li>
<li><a href="<%= custom_fields_admin_property_hire_path(property) %>"><%= t("property_hire.custom_fields") %></a></li>
<li><a href="<%= fields_display_order_admin_property_hire_path(property) %>"><%= t("property_hire.fields_display_order") %></a></li>
<li><a href="<%= admin_property_hire_path(property.id, :page => params[:page]) %>" data-method="delete" data-confirm="Are you sure?"><%= t(:delete_) %></a></li>

View File

@ -0,0 +1,6 @@
<%= form_for @property , :url => {:action => "create"}, html: {class: "form-horizontal main-forms"} do |f| %>
<fieldset>
<%= f.hidden_field :copy_id %>
<%= render :partial => "form", locals: {:f => f} %>
</fieldset>
<% end %>

View File

@ -3,8 +3,10 @@ en:
markups:
hint_text: Hint Text
restful_actions:
copy: Copy
fields_display_order: "Fields display order"
property_hire:
copy: Copy
unfilled: Unfilled
hire_time: Reservation time
back_to_hire_page: "Back to Reserve Page"

View File

@ -3,8 +3,10 @@ zh_tw:
markups:
hint_text: 提示文字
restful_actions:
copy: 複製
fields_display_order: "欄位顯示順序"
property_hire:
copy: 複製
unfilled: 未填寫
hire_time: 預約時間
back_to_hire_page: "回到預約頁面"

View File

@ -9,6 +9,7 @@ Rails.application.routes.draw do
namespace :admin do
resources :property_hires do
member do
get 'copy'
get "edit_location"
patch "update_location"
delete "destroy_location"