diff --git a/app/controllers/admin/personal_plugin_fields_controller.rb b/app/controllers/admin/personal_plugin_fields_controller.rb index 4f10da3..6060be0 100644 --- a/app/controllers/admin/personal_plugin_fields_controller.rb +++ b/app/controllers/admin/personal_plugin_fields_controller.rb @@ -64,10 +64,67 @@ class Admin::PersonalPluginFieldsController < OrbitMemberController end def update_fields_setting field_params = params.require(:personal_plugin_field).permit! rescue {} + field_params[:fields_order] = field_params[:fields_order].map do |k, v| + [k, v.sort_by{|k,vv| k.to_i}.map{|vv| vv[1].to_i}] + end.to_h @personal_plugin_field.update_attributes(field_params) @personal_plugin_field.save redirect_to params[:referer_url] end + def scan_word_block(word) + words_idx = [] + tmp = [] + is_word = false + word.split('').each_with_index do |w, i| + if w.match(/\w/) + if !is_word + tmp << i + is_word = true + end + elsif is_word + tmp << i - 1 + words_idx << tmp + tmp = [] + is_word = false + end + end + if tmp.count == 1 + tmp << word.length - 1 + words_idx << tmp + tmp = [] + end + last_idx = word.length - 1 + new_res = [] + words_idx_reverse = words_idx.reverse + words_idx_reverse.each_with_index do |v, i| + s = v[0] + e = v[1] + tmp_str = word[s..e] + tmp = word[e+1..last_idx] + need_find_quote = tmp.match(/['"]/) + need_find_quote = need_find_quote ? need_find_quote[0] : nil + tmp_str += tmp + prev_word_range = words_idx_reverse[i + 1] + start_idx = 0 + if prev_word_range + start_idx = prev_word_range[1] + 1 + end + if need_find_quote + tmp = word[start_idx...s].match(/(,\s+|)[,#{need_find_quote}]+\s*$/) + else + tmp = word[start_idx...s].match(/(,\s+|)\s*$/) + end + if tmp + tmp = tmp[0] + tmp_str = tmp + tmp_str + last_idx = s - 1 - tmp.length + else + last_idx = s - 1 + end + new_res << tmp_str + end + new_res.reverse + end def generate_plugin template_dir_path = File.expand_path("../../../../template_generator/", __FILE__) + "/" cp_template_dir_path = "#{Rails.root}/tmp/#{@personal_plugin_field.module_name}/" @@ -78,6 +135,7 @@ class Admin::PersonalPluginFieldsController < OrbitMemberController begin in_use_locales = Site.first.in_use_locales primary_modal_fields = @personal_plugin_field.primary_modal_fields.select{|f| (f[:field_name].present? rescue false)} + primary_modal_fields << {:field_name=>"authors", :translation_name=>{"zh_tw"=>"全部作者", "en"=>"Authors"}, :field_type=>"text_editor", :localize=>"1", :slug_title=>"0", :periodic_time=>"0"} if primary_modal_fields.select{|f| f[:field_name] == "authors"}.count == 0 yml_files = Dir.glob("#{cp_template_dir_path}config/locales/*.yml") yml_files.each do |yml_file| locale = yml_file.split("/").last.split(".yml").first @@ -125,20 +183,66 @@ class Admin::PersonalPluginFieldsController < OrbitMemberController plugin_template_related_links_text = plugin_template_related_links.to_s fields = plugin_template_related_files + plugin_template_related_links plugin_template_related_files_fields = fields.map{|field_name| - "has_many :#{field_name.pluralize}, :dependent => :destroy, :autosave => true\r\n" + + "has_many :#{field_name.pluralize}, :dependent => :destroy, :autosave => true\n" + "accepts_nested_attributes_for :#{field_name.pluralize}, :allow_destroy => true" - }.join("\r\n") + }.join("\n") plugin_template = @personal_plugin_field.primary_modal_name + all_fields = primary_modal_fields.map{|f| f[:field_name]} + ["member_profile"] + @personal_plugin_field.related_modal_name.each_with_index do |k,i| + all_fields += @personal_plugin_field.related_modal_fields[i].to_a.map{|f| "#{k}.#{f[:field_name]}"} + end + @personal_plugin_field.frontend_fields.each do |k,v| + @personal_plugin_field.frontend_fields[k] = v & all_fields + end + @personal_plugin_field.backend_fields.each do |k,v| + @personal_plugin_field.backend_fields[k] = v & all_fields + end + date_fields = [] + year_month_fields = [] + date_time_fields = [] + primary_modal_fields.each do |field_value| + if (field_value[:field_type].present? rescue false) + case field_value[:field_type] + when "date" + date_fields << field_value[:field_name] + when "year_month" + year_month_fields << field_value[:field_name] + when "date_time" + date_time_fields << field_value[:field_name] + end + end + end + @personal_plugin_field.related_modal_name.each_with_index do |k,i| + @personal_plugin_field.related_modal_fields[i].to_a.each do |field_value| + field_name = "#{k}.#{field_value[:field_name]}" + if (field_value[:field_type].present? rescue false) + case field_value[:field_type] + when "date" + date_fields << field_name + when "year_month" + year_month_fields << field_name + when "date_time" + date_time_fields << field_name + end + end + end + end 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" + - "