Fix bugs.

This commit is contained in:
bohung committed 2021-03-01 23:44:52 +08:00
1 parent 231ac9c79f
commit fea246f017
21 files changed
+325 -152

No files matched your search

@@ -70,14 +70,18 @@ class Admin::PersonalPluginFieldsController < OrbitAdminController
if related_modal_name.present? && sub_hash.present?
translate_hash[related_modal_name] = sub_hash
end
#sub_hash["modal_name"] = "關聯欄位翻譯"
end
author_name_translation = @personal_plugin_field.author_name_translations[locale] rescue ""
I18n.with_locale(locale) do
author_name_translation = I18n.t("personal_plugins.author") if author_name_translation.blank?
end
translate_hash["author_name_translation"] = author_name_translation
col_name_translate_yaml = ""
if translate_hash.present?
col_name_translate_yaml = translate_hash.to_yaml.gsub("---\n", '')
end
blank_text = yml_text.split(/(\r\n|\n)/).select{|t| t.include?"col_name_translate_yaml"}.first.split('col_name_translate_yaml').first rescue ""
col_name_translate_yaml = col_name_translate_yaml.gsub("\n","\n#{blank_text}")
yml_text = yml_text.gsub("col_name_translate_yaml",col_name_translate_yaml)
yml_text = gsub_text_by_key_value(yml_text,"col_name_translate_yaml",col_name_translate_yaml)
yml_text = yml_text.gsub("personal_plugin_template_translate",@personal_plugin_field.title_translations[locale])
File.open(yml_file,'w+') do |f|
f.write(yml_text)
@@ -89,20 +93,22 @@ class Admin::PersonalPluginFieldsController < OrbitAdminController
plugin_template_related_files_text = plugin_template_related_files.to_s
plugin_template_related_files_fields = plugin_template_related_files.map{|field_name|
"has_many :#{field_name.pluralize}, :dependent => :destroy, :autosave => true\r\n" +
"#{@blank_text}accepts_nested_attributes_for :#{field_name.pluralize}, :allow_destroy => true"
}.join("\r\n#{@blank_text}")
"accepts_nested_attributes_for :#{field_name.pluralize}, :allow_destroy => true"
}.join("\r\n")
plugin_template = @personal_plugin_field.primary_modal_name
backend_index_fields = @personal_plugin_field.backend_fields["index"] rescue []
backend_index_fields_contents = backend_index_fields.map do |field_name|
if field_name == slug_title
"<%= link_to #{plugin_template}.#{field_name}, page_for_#{plugin_template}(#{plugin_template}), target: \"blank\" %>\r\n " +
"<div class=\"quick-edit\">
<ul class=\"nav nav-pills hide\">
<li><%= link_to t('edit'), edit_admin_#{plugin_template}_path(#{plugin_template},:page => params[:page]) %></li>
<li><%= link_to t(:delete_), admin_#{plugin_template}_path(id: #{plugin_template}.id, :page => params[:page]), method: :delete, data: { confirm: 'Are you sure?' } %></li>
</ul>
</div>\r\n "
else
"<%= link_to #{plugin_template}.#{field_name}, page_for_#{plugin_template}(#{plugin_template}), target: \"blank\" %>\r\n" +
"<div class=\"quick-edit\">"+
" <ul class=\"nav nav-pills hide\">"+
" <li><%= link_to t('edit'), edit_admin_#{plugin_template}_path(#{plugin_template},:page => params[:page]) %></li>"+
" <li><%= link_to t(:delete_), admin_#{plugin_template}_path(id: #{plugin_template}.id, :page => params[:page]), method: :delete, data: { confirm: 'Are you sure?' } %></li>"+
"</ul>"+
"</div>\r\n"
elsif field_name.include?(".")
"<%= #{plugin_template}.#{field_name} rescue \"\" %>"
else
"<%= #{plugin_template}.#{field_name} %>"
end
end
@@ -110,31 +116,73 @@ class Admin::PersonalPluginFieldsController < OrbitAdminController
col_name_to_show_in_show_page = @personal_plugin_field.frontend_fields["show"] rescue []
col_name_to_show_in_index_page = @personal_plugin_field.frontend_fields["index"] rescue []
extra_translate_title = col_name_to_show_in_index_page.map{|field_name| #在個人外掛前台index頁面的欄位翻譯名稱hash
["th-#{field_name}","I18n.locale(\"#{@personal_plugin_field.module_name}.#{field_name}\")"]
["th-#{field_name}","I18n.t(\"#{@personal_plugin_field.module_name}.#{field_name}\")"]
}
col_fields = get_fields_text(@personal_plugin_field.primary_modal_fields)
col_related_fields = @personal_plugin_field.related_modal_fields.map{|field_values| get_fields_text(field_values)}
locale_fields = []
none_locale_fields = []
locale_fields_input_fields = []
none_locale_fields_input_fields = []
plugin_template_related = @personal_plugin_field.related_modal_name.select{|n| n.present?}
plugin_template_related_main_field = @personal_plugin_field.related_modal_fields.map{|field_values| field_values.map{|field_value| field_value[:field_name]}.select{|t| t.present?}.first }
@personal_plugin_field.primary_modal_fields.each do |field_value|
field_name = field_value[:field_name]
field_type = field_value[:field_type]
next if field_name.blank?
next if field_type == "file"
if (field_value[:localize] == "1" rescue false)
locale_fields << field_name
input_field = generate_input_field(field_type,field_name,true)
locale_fields_input_fields << input_field
else
none_locale_fields << field_name
input_field = generate_input_field(field_type,field_name)
none_locale_fields_input_fields << input_field
plugin_template_related_main_field = @personal_plugin_field.related_modal_fields.map{|field_values|
slug_titles = field_values.select{|field_value| (field_value[:slug_title] == "1" rescue false)}
if slug_titles.count == 0
slug_titles = field_values
end
slug_titles.map{|field_value| field_value[:field_name]}.select{|t| t.present?}.first
}
locale_fields , none_locale_fields , locale_fields_input_fields,none_locale_fields_input_fields = get_input_fields(@personal_plugin_field.primary_modal_fields)
related_locale_fields = []
related_none_locale_fields = []
related_locale_fields_input_fields = []
related_none_locale_fields_input_fields = []
@personal_plugin_field.related_modal_fields.each_with_index do |field_values,i|
related_modal_name = @personal_plugin_field.related_modal_name[i]
f1 , f2 , f3 , f4 = get_input_fields(field_values,related_modal_name,related_modal_name)
related_locale_fields << f1
related_none_locale_fields << f2
related_locale_fields_input_fields << f3
related_none_locale_fields_input_fields << f4
end
backend_profile_fields = backend_index_fields
backend_profile_fields_contents = backend_index_fields_contents
datetime_field_types_hash = {"year_month"=>"%Y/%m","date"=>"%Y/%m/%d","time"=>"%H:%M","date_time"=>"%Y/%m/%d %H:%M"}
datetime_fields = @personal_plugin_field.primary_modal_fields.select{|field_value| datetime_field_types_hash.keys.include?(field_value[:field_type])}.map{|field_value|
[field_value[:field_name],datetime_field_types_hash[field_value[:field_type]]]
}.to_h
value_case_codes = ["value = #{plugin_template}.send(field) rescue \"\"",
"if field.include?(\".\")",
"#{@blank_text}value = #{plugin_template}",
"#{@blank_text}field.split(\".\").each{|f| value = value.send(f) rescue nil }",
"end",
"file_fields = #{plugin_template_related_files}",
"if file_fields.include?(field)",
"#{@blank_text}files = #{plugin_template}.send(field.pluralize)",
"#{@blank_text}value = files.map do |file|",
"#{@blank_text * 2}url = file.file.url",
"#{@blank_text * 2}title = (file.title.blank? ? File.basename(file.file.path) : file.title)",
"#{@blank_text * 2}\"<li><a href='\#{url}'' target='_blank'>\#{title}</li>\"",
"#{@blank_text}end",
"#{@blank_text}value = value.join(\"\")",
"end",
"strftime_hash = #{datetime_fields}",
"if strftime_hash.keys.include?(field)",
"#{@blank_text}value = value.strftime(strftime_hash[field]) rescue value",
"end"
].join("\r\n")
display_field_code = value_case_codes + "\r\n" +
"value = \"<a href='\#{OrbitHelper.url_to_plugin_show(#{plugin_template}.to_param,'#{@personal_plugin_field.module_name}')}' target='_blank'>\#{value}</a>\" if field == \"#{slug_title}\"\r\n" +
"value"
value_case_codes += "\r\nvalue"
related_backend_index_fields = @personal_plugin_field.related_modal_fields.map{|field_values|
field_values.map{|field_value| (field_value[:field_name] rescue nil)}.select{|t| t.present?}
}
related_backend_index_fields_contents = related_backend_index_fields.map.with_index{|field_names,i|
related_modal_name = @personal_plugin_field.related_modal_name[i]
field_names.map{|field_name| "<%= #{related_modal_name}.#{field_name} %>"}
}
member_methods_define = @personal_plugin_field.primary_modal_fields.select{|f| f[:field_type] == "member" && f[:field_name].present?}.map{|field_value|
["def #{field_value[:field_name].pluralize}",
" MemberProfile.find(self.#{field_value[:field_name].singularize}_ids) rescue []",
"end"]
}.flatten
@match_pattern = {"personal_plugin_template" => @personal_plugin_field.module_name,
"plugin_template" => plugin_template,
"plugin_template_related" => plugin_template_related,
@@ -148,6 +196,8 @@ class Admin::PersonalPluginFieldsController < OrbitAdminController
"plugin_template_file" => plugin_template_related_files,
"plugin_template_sort_hash" => "nil",
"col_name_to_show_in_index_page_arr" => col_name_to_show_in_index_page,
"related_backend_index_fields" => related_backend_index_fields,
"related_backend_index_fields_contents" => related_backend_index_fields_contents,
"backend_index_fields" => backend_index_fields,
"backend_index_fields_contents" => backend_index_fields_contents,
"plugin_template_related_files_text" => plugin_template_related_files_text,
@@ -155,32 +205,74 @@ class Admin::PersonalPluginFieldsController < OrbitAdminController
"none_locale_fields" => none_locale_fields,
"none_locale_fields_input_fields" => none_locale_fields_input_fields,
"locale_fields_input_fields" => locale_fields_input_fields,
"plugin_template_related_main_field" => plugin_template_related_main_field
"related_locale_fields" => related_locale_fields,
"related_none_locale_fields" => related_none_locale_fields,
"related_none_locale_fields_input_fields" => related_none_locale_fields_input_fields,
"related_locale_fields_input_fields" => related_locale_fields_input_fields,
"plugin_template_related_main_field" => plugin_template_related_main_field,
"backend_profile_fields" => backend_profile_fields,
"backend_profile_fields_contents" => backend_profile_fields_contents,
"value_case_codes" => value_case_codes,
"display_field_code" => display_field_code,
"member_methods_define" => member_methods_define
}
max_length = @match_pattern.keys.map{|k| k.length}.max
@match_pattern = @match_pattern.sort_by{|k,v| (v.class != Array) ? (max_length - k.length) : -k.length}
@logs = []
replace_files(files)
#Thread.new do
dirs.each do |dir|
replace_dir(dir)
end
replace_dirs(dirs,false)
#end
render :html => @logs.join("<br>").html_safe#@match_pattern
copy_templates("#{cp_template_dir_path}modules/#{@personal_plugin_field.module_name}/")
render :html => @logs#@match_pattern#@logs.join("<br>").html_safe#@match_pattern
end
def generate_input_field(field_type,field_name,localize = false)
def copy_templates(source)
templates = Dir.glob('app/templates/*/').select{|f| File.basename(f) != 'mobile'}
templates.each do |template|
FileUtils.cp_r(source,"#{template}modules/.")
end
end
def get_input_fields(field_values,extra_field_name=nil,plugin_template = nil)
none_locale_fields = []
locale_fields = []
locale_fields_input_fields = []
none_locale_fields_input_fields = []
@index = 0
field_values.each do |field_value|
field_name = field_value[:field_name]
field_type = field_value[:field_type]
next if field_name.blank?
next if field_type == "file"
if (field_value[:localize] == "1" rescue false)
locale_fields << field_name
input_field = generate_input_field(field_type,field_name,true,extra_field_name,plugin_template)
locale_fields_input_fields << input_field
else
none_locale_fields << field_name
input_field = generate_input_field(field_type,field_name,false,extra_field_name,plugin_template)
none_locale_fields_input_fields << input_field
end
@index += 1
end
return locale_fields , none_locale_fields , locale_fields_input_fields,none_locale_fields_input_fields
end
def generate_input_field(field_type,field_name,localize = false,extra_field_name = nil, plugin_template = nil)
personal_plugin_template = @personal_plugin_field.module_name
plugin_template = @personal_plugin_field.primary_modal_name
plugin_template = @personal_plugin_field.primary_modal_name if plugin_template.nil?
input_field = ""
field_value_text = "@#{plugin_template}.#{field_name}"
field_name_text = ":#{field_name}"
field_name_text = "locale" if localize
placeholder = "#{personal_plugin_template}.#{field_name}"
placeholder = "#{personal_plugin_template}.#{extra_field_name}.#{field_name}" if extra_field_name
if localize
field_value_text = "@#{plugin_template}.#{field_name}_translations[locale]"
end
case field_type
when "year"
input_field = datetime_picker_text(plugin_template,field_name_text, "yyyy")#"<%= select_year((#{field_value_text} ? #{field_value_text}.to_i : DateTime.now.year), {:start_year => DateTime.now.year + 5, :end_year => 1930}, {:name => '#{plugin_template}[#{field_name}]',:class => 'span1'} ) %>"
when "year_month"
input_field = datetime_picker_text(plugin_template,field_name_text, "yyyy/MM")
when "date"
input_field = datetime_picker_text(plugin_template,field_name_text, "yyyy/MM/dd")
when "time"
@@ -188,12 +280,14 @@ class Admin::PersonalPluginFieldsController < OrbitAdminController
when "date_time"
input_field = datetime_picker_text(plugin_template,field_name_text)
when "text_editor"
input_field = "<%= f.text_area #{field_name_text}, class: \"input-block-level ckeditor\", placeholder: t(\"#{personal_plugin_template}.#{field_name}\"), value: (#{field_value_text} rescue nil) %>"
input_field = "<%= f.text_area #{field_name_text}, class: \"input-block-level ckeditor\", placeholder: t(\"#{placeholder}\"), value: (#{field_value_text} rescue nil) %>"
when "member"
input_field = "<%= render partial: 'admin/member_selects/email_selection_box', locals: {field: '#{plugin_template}[#{field_name.singularize}_ids][]', email_members: @#{plugin_template}.#{field_name.pluralize},index:'#{@index}',select_name:'#{field_name.pluralize}'} %>"
else #text_field
input_field = "<%= f.text_field #{field_name_text}, class: \"input-block-level\", placeholder: t(\"#{personal_plugin_template}.#{field_name}\"), value: (#{field_value_text} rescue nil) %>"
input_field = "<%= f.text_field #{field_name_text}, class: \"input-block-level\", placeholder: t(\"#{placeholder}\"), value: (#{field_value_text} rescue nil) %>"
end
if localize
input_field.prepend("<%= f.fields_for :#{field_name}_translations do |f| %>\r\n ").concat("<% end %>\r\n ")
input_field.prepend("<%= f.fields_for :#{field_name}_translations do |f| %>\r\n ").concat("\r\n<% end %>")
end
input_field
end
@@ -207,6 +301,7 @@ class Admin::PersonalPluginFieldsController < OrbitAdminController
next if field_value[:field_type] == "file" || field_value[:field_name].blank?
type = "String"
default = "\"\""
field_name = field_value[:field_name]
case field_value[:field_type]
when "date"
type = "Date"
@@ -217,45 +312,56 @@ class Admin::PersonalPluginFieldsController < OrbitAdminController
when "date_time"
type = "DateTime"
default = "DateTime.now"
when "year_month"
type = "DateTime"
default = "DateTime.now"
when "member"
type = "Array"
default = "[]"
field_name = "#{field_name.singularize}_ids"
end
no_localize_types = ["date","time","date_time"]
field_text = "field :#{field_value[:field_name]}, :type => #{type}, :default => #{default}"
field_text += ", :localize => true" if field_value[:localize] && !no_localize_types.include?(field_value[:field_type])
no_localize_types = ["date","time","date_time","year_month"]
field_text = "field :#{field_name}, :type => #{type}, :default => #{default}"
field_text += ", :localize => true" if field_value[:localize] == "1" && !no_localize_types.include?(field_value[:field_type])
field_text += ", as: :slug_title" if field_value[:slug_title] == "1"
field_text
end
fields_text.join("\r\n#{@blank_text}")
fields_text.select{|t| t.present?}.join("\r\n")
end
def replace_dirs(dirs)
dirs.each do |dir|
replace_dir(dir)
def replace_dirs(dirs,is_array = true)
dirs.each_with_index do |dir,i|
if is_array
replace_dir(dir,i)
else
replace_dir(dir)
end
end
end
def replace_dir(dir)
dir = replace_file(dir)
return true if dir.select{|d| d.present?}.blank?
if dir.count > 1
def replace_dir(dir,current_index = nil)
dir = replace_file(dir,current_index)
return true if dir.blank?
if dir.class == Array
replace_dirs(dir)
else
dir = dir[0]
return true
end
sub_dirs = Dir.glob("#{dir}/*/")
files = Dir.glob("#{dir}/*").select { |fn| File.file?(fn) }
replace_files(files)
@logs << files.to_s if current_index
replace_files(files,current_index)
if sub_dirs.present?
sub_dirs.each do |sub_dir|
replace_dir(sub_dir)
replace_dir(sub_dir,current_index)
end
else
return true
end
end
def replace_files(files)
def replace_files(files,current_index = nil)
files.each do |file|
replace_file(file)
replace_file(file,current_index)
end
end
def replace_file(file)
def replace_file(file,current_index = nil)
isfile = File.file?(file)
path = File.dirname(file)
file_name = File.basename(file)
@@ -265,6 +371,7 @@ class Admin::PersonalPluginFieldsController < OrbitAdminController
elsif file_name != new_filename
if new_filename.class == Array
new_filename.each do |f|
next if f.blank?
FileUtils.cp_r("#{path}/#{file_name}", "#{path}/#{f}")
end
FileUtils.rm_rf("#{path}/#{file_name}")
@@ -273,31 +380,39 @@ class Admin::PersonalPluginFieldsController < OrbitAdminController
end
end
if new_filename.blank?
return [""]
return ""
else
is_array = new_filename.class == Array
new_filename = Array(new_filename)
if isfile
new_filename.each_with_index do |sub_file,i|
files = Array(new_filename)
files.each_with_index do |sub_file,i|
next if File.extname(sub_file).match(/(png|jpg)/i)
file_text = File.read("#{path}/#{sub_file}")
if is_array
file_text = replace_text_with_pattern(file_text,false,i)
file_text = replace_text_with_pattern(file_text,false,i,nil,true)
elsif current_index
@logs << sub_file
file_text = replace_text_with_pattern(file_text,false,current_index,nil,true)
else
file_text = replace_text_with_pattern(file_text)
end
File.open("#{path}/#{sub_file}",'w+'){|f| f.write(file_text)}
end
end
return new_filename.map{|sub_file| "#{path}/#{sub_file}" if sub_file.present?}
if is_array
return new_filename.map{|sub_file| "#{path}/#{sub_file}" if sub_file.present?}.select{|f| f.present?}
else
return "#{path}/#{new_filename}"
end
end
end
def replace_text_with_pattern(text,is_file=false,i = nil)
def replace_text_with_pattern(text,is_file=false,i = nil,sub_i = nil,inner = false)
new_text = text
@match_pattern.each do |k,v|
next if !include_key(new_text,k)
vv = v
vv = vv[i] if i && vv.class == Array
vv = vv[sub_i] if sub_i && vv.class == Array
if vv.class == String
if i && vv == "" && is_file
new_text = ""
@@ -305,6 +420,9 @@ class Admin::PersonalPluginFieldsController < OrbitAdminController
new_text = gsub_text_by_key_value(new_text,k,vv)
end
elsif vv.class == Array
if i
@logs << i
end
if is_file
if v.count == 0
new_text = ""
@@ -323,7 +441,11 @@ class Admin::PersonalPluginFieldsController < OrbitAdminController
start_index = 2 if parse_content[0..1] == "\r\n"
end_index = end_index - 2 if parse_content[(end_index - 2)..(end_index - 1)] == "\r\n"
parse_content = parse_content.slice(start_index,end_index)
result = (0...vv.count).map {|i| replace_text_with_pattern(parse_content,false,i) }.join("")
if inner
result = (0...vv.count).map {|ii| replace_text_with_pattern(parse_content,false,i,ii,true) }.join("")
else
result = (0...vv.count).map {|ii| replace_text_with_pattern(parse_content,false,ii) }.join("")
end
end
result
end
@@ -335,10 +457,13 @@ class Admin::PersonalPluginFieldsController < OrbitAdminController
return text.include?(key) || text.include?(key.classify)
end
def gsub_text_by_key_value(text,k,v)
text = text.gsub(k + "s",v.pluralize)
text = text.gsub(k ,v )
text = text.gsub(k.classify + "s",v.classify.pluralize)
text = text.gsub(k.classify,v.classify)
@blank_texts = []
text.gsub(/\n(\s*)[^\n]*(#{k}|#{k.classify})/m) {|ff| @blank_texts << $1.gsub(/(\r\n|\n)/,'')}
i = 0
text = text.gsub(k + "s"){|ff| v.pluralize.gsub("\n","\n#{@blank_texts[i]}")}
text = text.gsub(k ){|ff| v.gsub("\n","\n#{@blank_texts[i]}")}
text = text.gsub(k.classify + "s"){|ff| v.classify.pluralize.gsub("\n","\n#{@blank_texts[i]}")}
text = text.gsub(k.classify){|ff| v.classify.gsub("\n","\n#{@blank_texts[i]}")}
end
private
def personal_plugin_field_params
+1
View File
@@ -3,6 +3,7 @@ class PersonalPluginField
include Mongoid::Timestamps
include OrbitModel::Status
include MemberHelper
field :author_name, :type => String, :default => "",:localize => true
field :title, :type => String, :default => "",:localize => true
field :module_name, :type => String, :default => ""
field :primary_modal_name, :type => String, :default => ""
@@ -50,6 +50,15 @@
<% end %>
</div>
</div>
<!-- author_translation_name -->
<div class="control-group input-title">
<label class="control-label muted"><%= t("personal_plugin_generator.author_translation_name") %></label>
<div class="controls">
<%= f.fields_for :author_name_translations do |f| %>
<%= f.text_field locale, class: "input-block-level", placeholder: t("personal_plugin_generator.author_translation_name"), value: (@personal_plugin_field.author_name_translations[locale] rescue nil) %>
<% end %>
</div>
</div>
</div>
<% end %>
</div>
@@ -108,8 +117,8 @@
</div>
</div>
</div>
<% field_types = ["text_field","text_editor","file","select","year","date","time","date_time"].map{|field| [thead_field(field),field]}
field_types1 = field_types.select{|a| a[1] != "file"}
<% field_types = ["text_field","text_editor","file","select","year","year_month","date","time","date_time","member"].map{|field| [thead_field(field),field]}
field_types1 = field_types.select{|a| a[1] != "file" && a[1] != "member"}
%>
<div class="tab-content language-area">
<div id="primary_modal_plane">