diff --git a/app/controllers/admin/personal_plugin_fields_controller.rb b/app/controllers/admin/personal_plugin_fields_controller.rb index bd55948..72e4399 100644 --- a/app/controllers/admin/personal_plugin_fields_controller.rb +++ b/app/controllers/admin/personal_plugin_fields_controller.rb @@ -129,7 +129,7 @@ class Admin::PersonalPluginFieldsController < OrbitMemberController "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 = @personal_plugin_field.backend_fields["index"].to_a rescue [] backend_index_fields_contents = backend_index_fields.map do |field_name| if field_name == slug_title "\r\n <%= link_to #{plugin_template}.#{field_name}, page_for_#{plugin_template}(#{plugin_template}), target: \"blank\" %>\r\n" + @@ -147,7 +147,7 @@ class Admin::PersonalPluginFieldsController < OrbitMemberController "<%= #{plugin_template}.#{field_name} %>" end end - backend_profile_fields = @personal_plugin_field.backend_fields["profile"] rescue [] + backend_profile_fields = @personal_plugin_field.backend_fields["profile"].to_a rescue [] backend_profile_fields_contents = backend_profile_fields.map do |field_name| if field_name == slug_title "\r\n <%= link_to #{plugin_template}.#{field_name}, page_for_#{plugin_template}(#{plugin_template}), target: \"blank\" %>\r\n" + @@ -165,9 +165,9 @@ class Admin::PersonalPluginFieldsController < OrbitMemberController "<%= #{plugin_template}.#{field_name} %>" end end - col_name_to_show = @personal_plugin_field.frontend_fields["member_show"] rescue [] - 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 [] + col_name_to_show = @personal_plugin_field.frontend_fields["member_show"].to_a rescue [] + col_name_to_show_in_show_page = @personal_plugin_field.frontend_fields["show"].to_a rescue [] + col_name_to_show_in_index_page = @personal_plugin_field.frontend_fields["index"].to_a rescue [] extra_translate_title = col_name_to_show_in_index_page.map{|field_name| #在個人外掛前台index頁面的欄位翻譯名稱hash ["th-#{field_name}","I18n.t(\"#{@personal_plugin_field.module_name}.#{field_name}\")"] } @@ -250,11 +250,32 @@ class Admin::PersonalPluginFieldsController < OrbitMemberController related_modal_name = @personal_plugin_field.related_modal_name[i] field_names.map{|field_name| "<%= #{related_modal_name}.#{field_name} %>"} } - member_methods_define = primary_modal_fields.select{|f| f[:field_type] == "member"}.map{|field_value| + member_methods_define = primary_modal_fields.select{|f| (f[:field_type] == "member" rescue false)}.map{|field_value| ["def #{field_value[:field_name].pluralize}", " MemberProfile.find(self.#{field_value[:field_name].singularize}_ids) rescue []", "end"] }.flatten + date_time_strftime = {"date"=>"%Y/%m/%d","date_time"=>"%Y/%m/%d %H:%M","year_month"=>"%Y/%m"} + periodic_methods_define = primary_modal_fields.select{|f| (f[:periodic_time] == "1" rescue false)}.map{|field_value| + strftime_string = "" + if date_time_strftime.keys.include?(field_value[:field_type]) + strftime_string = ".strftime(\"#{date_time_strftime[field_value[:field_type]]}\")" + end + ["def #{field_value[:field_name]}", + " \"\#{self.#{field_value[:field_name]}_start#{strftime_string}} ~ \#{self.#{field_value[:field_name]}_end#{strftime_string}}\"", + "end"] + }.flatten + related_periodic_methods_define = @personal_plugin_field.related_modal_fields.map{|field_values| + field_values.select{|f| (f[:periodic_time] == "1" rescue false)}.map{|field_value| + strftime_string = "" + if date_time_strftime.keys.include? field_value[:field_type] + strftime_string = ".strftime(\"#{date_time_strftime[field_value[:field_type]]}\")" + end + ["def #{field_value[:field_name]}", + " \"\#{self.#{field_value[:field_name]}_start#{strftime_string}} ~ \#{self.#{field_value[:field_name]}_end#{strftime_string}}\"", + "end"] + }.flatten + } analysis_field_name = @personal_plugin_field.backend_fields[:analysis][0] rescue "" analysis_field_name = "year" if analysis_field_name.blank? analysis_field_input_fields = "" @@ -314,7 +335,9 @@ class Admin::PersonalPluginFieldsController < OrbitMemberController "before_save_codes" => before_save_codes, "time_fields_text" => time_fields.to_s, "iterate_step_text" => iterate_step_text, - "plugin_template_related_members" => plugin_template_related_members.to_s + "plugin_template_related_members" => plugin_template_related_members.to_s, + "periodic_methods_define" => periodic_methods_define, + "related_periodic_methods_define" => related_periodic_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} @@ -355,26 +378,35 @@ class Admin::PersonalPluginFieldsController < OrbitMemberController field_type = field_value[:field_type] next if field_name.blank? next if field_type == "file" || field_type == "link" - if (field_value[:localize] == "1" rescue false) + localize = (field_value[:localize] == "1" rescue false) + periodic = (field_value[:periodic_time] == "1" rescue false) + input_field = generate_input_field(field_type,field_name,localize,extra_field_name,plugin_template,periodic) + if localize 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) + def generate_input_field(field_type,field_name,localize = false,extra_field_name = nil, plugin_template = nil,periodic = false) personal_plugin_template = @personal_plugin_field.module_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 + org_field_name = field_name + if periodic + if localize + field_name = "periodic_time_field" + else + field_name_text = "\"periodic_time_field\"" + end + end placeholder = "#{personal_plugin_template}.#{field_name}" placeholder = "#{personal_plugin_template}.#{extra_field_name}.#{field_name}" if extra_field_name if localize @@ -401,6 +433,15 @@ class Admin::PersonalPluginFieldsController < OrbitMemberController if localize input_field.prepend("<%= f.fields_for :#{field_name}_translations do |f| %>\r\n ").concat("\r\n<% end %>") end + if periodic + input_fields = [] + input_fields[0] = input_field.gsub("periodic_time_field","#{org_field_name}_start") + input_fields[1] = input_field.gsub("periodic_time_field","#{org_field_name}_end") + input_field = ["start","end"].map.with_index{|f,i| + "<%=t(\"#{personal_plugin_template}.extend_translate.#{f}_#{field_type}\")%>" + + input_fields[i] + }.join("") + end input_field end def datetime_picker_text(plugin_template,field_name_text,format = "yyyy/MM/dd hh:mm",timepicker = false) @@ -420,7 +461,7 @@ class Admin::PersonalPluginFieldsController < OrbitMemberController default = "DateTime.now.year" when "date" type = "Date" - default = "Date.parse(DateTime.now.to_s)" + default = "Date.today" when "time" type = "String" default = "\"\"" @@ -436,11 +477,23 @@ class Admin::PersonalPluginFieldsController < OrbitMemberController field_name = "#{field_name.singularize}_ids" end 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 = "" + if field_value[:periodic_time] == "1" && no_localize_types.include?(field_value[:field_type]) + field_text = [] + field_text[0] = "field :#{field_name}_start, :type => #{type}, :default => #{default}" + field_text[0] += ", :localize => true" if field_value[:localize] == "1" && !no_localize_types.include?(field_value[:field_type]) + field_text[0] += ", as: :slug_title" if field_value[:slug_title] == "1" + field_text[1] = "field :#{field_name}_end, :type => #{type}, :default => #{default}" + field_text[1] += ", :localize => true" if field_value[:localize] == "1" && !no_localize_types.include?(field_value[:field_type]) + field_text[1] += ", as: :slug_title" if field_value[:slug_title] == "1" + else + 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" + end field_text end + fields_text = fields_text.flatten fields_text.select{|t| t.present?}.join("\r\n") end def replace_dirs(dirs,is_array = true) diff --git a/app/helpers/admin/personal_plugin_fields_helper.rb b/app/helpers/admin/personal_plugin_fields_helper.rb index 8db3643..73999f7 100644 --- a/app/helpers/admin/personal_plugin_fields_helper.rb +++ b/app/helpers/admin/personal_plugin_fields_helper.rb @@ -1,4 +1,5 @@ module Admin::PersonalPluginFieldsHelper + include OrbitBackendHelper def thead_field(field) return I18n.t("personal_plugin_generator.#{field}") end diff --git a/app/models/personal_plugin_field.rb b/app/models/personal_plugin_field.rb index eb0549c..a4a6728 100644 --- a/app/models/personal_plugin_field.rb +++ b/app/models/personal_plugin_field.rb @@ -15,7 +15,7 @@ class PersonalPluginField field :log_text, :type => String, :default => "" field :fields_order, :type => Hash, :default => {} field :copy_id - before_save :change_extensions,:check_modal_name + before_save :change_extensions,:check_modal_name,:auto_add_display_fields after_destroy do delete_plugin("downloaded_extensions.rb",self.module_name) restart_server @@ -80,6 +80,25 @@ class PersonalPluginField return false end end + def auto_add_display_fields + change_primary_fields = self.primary_modal_fields.map{|f| f["field_name"]} - self.primary_modal_fields_was.map{|f| f["field_name"]} + change_related_fields = [] + self.related_modal_fields.each_with_index do |field_values,i| + old_field_values = self.related_modal_fields_was[i].to_a rescue [] + related_modal_name = self.related_modal_name[i].to_s + change_related_fields = change_related_fields + (field_values.map{|f| f["field_name"]} - old_field_values.map{|f| f["field_name"]}).select{|f| f.present?}.map{|f| "#{related_modal_name}.#{f}"} + end + change_fields = change_primary_fields + change_related_fields + change_fields = change_fields.select{|f| f.present?} + if change_fields.count > 0 + ["index","profile"].each do |f| + self.backend_fields[f] = self.backend_fields[f].to_a + change_fields + end + ["index","show","member_show"].each do |f| + self.frontend_fields[f] = self.frontend_fields[f].to_a + change_fields + end + end + end def self.get_modal_names name_routes = Rails.application.routes.named_routes.map{|k,v| k}.select{|s| s.to_s[0..4] == "admin"}.map{|s| s.to_s.split('admin_').last} modal_names = name_routes.map{|n| n.classify}.select{|n| (n.constantize rescue nil)} diff --git a/app/views/admin/personal_plugin_fields/_form.html.erb b/app/views/admin/personal_plugin_fields/_form.html.erb index 56a7051..aaed93a 100644 --- a/app/views/admin/personal_plugin_fields/_form.html.erb +++ b/app/views/admin/personal_plugin_fields/_form.html.erb @@ -172,9 +172,9 @@ }) <% related_modal_fields_html = "