From 5b4386fe7caef5190f70338b226519dd232d70df Mon Sep 17 00:00:00 2001 From: bohung Date: Tue, 23 Aug 2022 13:35:31 +0800 Subject: [PATCH] Fix a lots. --- .../personal_plugin_fields_controller.rb | 332 ++++++++++++++---- app/models/personal_plugin_field.rb | 43 +++ .../personal_plugin_fields/_form.html.erb | 28 +- .../_render_fields_check_table.html.erb | 33 +- .../_render_table.html.erb | 41 ++- .../fields_setting.html.erb | 19 +- config/locales/en.yml | 2 + config/locales/zh_tw.yml | 2 + personal_plugin_generator.gemspec | 6 +- .../.keep | 0 .../application.js | 0 .../application.css | 0 .../personal_plugin_templates_controller.rb | 60 +++- .../helpers/admin/plugin_templates_helper.rb | 41 +++ .../app/models/plugin_template.rb | 34 +- .../admin/plugin_templates/_form.html.erb | 2 +- .../admin/plugin_templates/index.html.erb | 2 +- .../get_fields_for_index.html.erb | 48 +++ template_generator/config/routes.rb | 2 + .../lib/personal_plugin_template/engine.rb | 2 +- .../personal_plugin_template/index.html.erb | 12 +- .../index_search1.html.erb | 12 +- 22 files changed, 580 insertions(+), 141 deletions(-) rename template_generator/app/assets/images/{personal_course => personal_plugin_template}/.keep (100%) rename template_generator/app/assets/javascripts/{personal_course => personal_plugin_template}/application.js (100%) rename template_generator/app/assets/stylesheets/{personal_course => personal_plugin_template}/application.css (100%) create mode 100644 template_generator/app/views/personal_plugin_templates/get_fields_for_index.html.erb 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" + - "
\r\n"+ - " \r\n"+ - "
\r\n " + "\n <%= link_to #{plugin_template}.#{field_name}, page_for_#{plugin_template}(#{plugin_template}), target: \"blank\" %>\n" + + "
\n"+ + " \n"+ + "
\n " + elsif date_fields.include?(field_name) + "<%= #{plugin_template}.#{field_name}.strftime('%Y/%m/%d') rescue \"\" %>" + elsif year_month_fields.include?(field_name) + "<%= #{plugin_template}.#{field_name}.strftime('%Y/%m') rescue \"\" %>" + elsif date_time_fields.include?(field_name) + "<%= #{plugin_template}.#{field_name}.strftime('%Y/%m/%d %H:%M') rescue \"\" %>" elsif field_name.include?(".") "<%= #{plugin_template}.#{field_name} rescue \"\" %>" elsif fields.include?(field_name) || plugin_template_related_members.include?(field_name) || field_name == "member_profile" #file or link or member @@ -150,13 +254,13 @@ class Admin::PersonalPluginFieldsController < OrbitMemberController 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" + - "
\r\n"+ - " \r\n"+ - "
\r\n " + "\n <%= link_to #{plugin_template}.#{field_name}, page_for_#{plugin_template}(#{plugin_template}), target: \"blank\" %>\n" + + "
\n"+ + " \n"+ + "
\n " elsif field_name.include?(".") "<%= #{plugin_template}.#{field_name} rescue \"\" %>" elsif fields.include?(field_name) || plugin_template_related_members.include?(field_name) || field_name == "member_profile" #file or link or member @@ -230,19 +334,24 @@ class Admin::PersonalPluginFieldsController < OrbitMemberController "#{@blank_text}}", "#{@blank_text}join_text = (text_only rescue false) ? \",\" : \"
\"", "#{@blank_text}value = value.join(join_text)", - "elsif field == \"member_profile\" && value.present?", - "#{@blank_text}path = OrbitHelper.url_to_plugin_show(value.to_param, 'member') rescue '#'", - "#{@blank_text}value = \"\#{value.name}\"", + "elsif field == \"member_profile\" || field == \"authors\"", + "#{@blank_text}value = get_authors_show(#{plugin_template})", "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 = \"\#{value}\" if field == \"#{slug_title}\"\r\n" + + ].join("\n") + enable_one_line_title = @personal_plugin_field.enable_one_line_title && @personal_plugin_field.one_line_title_format.present? + tmp_title = enable_one_line_title ? "(title_is_paper_format ? #{plugin_template}.create_link : value)" : "value" + display_field_code = value_case_codes + "\n" + + "if field == \"#{slug_title}\"\n" + + "#{@blank_text}link = OrbitHelper.url_to_plugin_show(#{plugin_template}.to_param,'#{@personal_plugin_field.module_name}')\n" + + "#{@blank_text}tmp_title = #{tmp_title}\n"+ + "#{@blank_text}value = link == '#' ? tmp_title : \"\#{tmp_title}\"\n" + + "end\n" + "value" - value_case_codes += "\r\nvalue" + value_case_codes += "\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?} } @@ -298,7 +407,7 @@ class Admin::PersonalPluginFieldsController < OrbitMemberController end period_fields = primary_modal_fields.select{|f| (f[:periodic_time] == "1" rescue false)}.map{|f| f[:field_name]} time_fields = primary_modal_fields.select{|f| f[:field_type] == "time"}.map{|f| f[:field_name]} - before_save_codes = ""#time_fields.map{|f| "self.#{f} = parse_time(self.#{f}.strftime('%H:%M'))"}.join("\r\n") + before_save_codes = ""#time_fields.map{|f| "self.#{f} = parse_time(self.#{f}.strftime('%H:%M'))"}.join("\n") plugin_template_sort_hash = {} @personal_plugin_field.backend_fields[:sort_asc].to_a.each do |f| plugin_template_sort_hash[f.to_sym] = 1 @@ -309,10 +418,73 @@ class Admin::PersonalPluginFieldsController < OrbitMemberController if @personal_plugin_field.backend_fields[:sort_desc].to_a.count != 0 plugin_template_sort_hash[:id] = -1 end + all_fields_types = { + "member_profile"=>"authors", + "authors" => "authors" + } + all_fields_to_show = [] + @personal_plugin_field.primary_modal_fields.each do |f| + field_name = f[:field_name] + if field_name.present? + all_fields_types[field_name] = f[:field_type] + all_fields_to_show << field_name + end + end + @personal_plugin_field.related_modal_name.each_with_index do |k,i| + @personal_plugin_field.related_modal_fields[i].to_a.map do |f| + field_name = f[:field_name] + if field_name.present? + field_name = "#{k}.#{field_name}" + all_fields_types[field_name] = f[:field_type] + all_fields_to_show << field_name + end + end + end + all_fields_to_show_in_index = @personal_plugin_field.get_sorted_fields(:frontend_fields, :index, all_fields_to_show) + one_line_title_format_code = [] + scan_code = scan_word_block(@personal_plugin_field.one_line_title_format) + one_line_title_format_code << "title = ''" + scan_code.each do |c| + left_space = c.match(/^[\s\'\",]+/) + right_space = c.match(/[\s\'\",]+$/) + variable_name = c.sub(/^[\s\'\",]+/,'').sub(/[\s\'\",]+$/, '') + if variable_name.present? + field_type = all_fields_types[variable_name] + if field_type + if_condition = "if self.#{variable_name}.present?" + variable_contents = "self.#{variable_name}" + if field_type == 'date' || field_type == 'year_month' || field_type == 'date_time' + variable_contents += ".strftime('%b. %Y')" + elsif field_type == 'year' + variable_contents += '.to_s' + elsif field_type == 'member' + variable_contents = "MemberProfile.where(:id.in=>self.#{variable_name}_ids).pluck(:tmp_name).map{|h| h[I18n.locale]}.to_sentence({:locale=>:en})" + elsif field_type == 'authors' + variable_contents = "get_authors_text(self, true, :en)" + if_condition += ' || self.member_profile_id.present?' + end + if left_space || right_space + left_space = left_space ? left_space[0] : '' + right_space = right_space ? right_space[0] : '' + one_line_title_format_code << "title += \"#{left_space.gsub('"','\\"')}\#{#{variable_contents}}#{right_space.gsub('"','\\"')}\" #{if_condition}" + else + one_line_title_format_code << "title += #{variable_contents} #{if_condition}" + end + end + end + end + one_line_title_format_code << "title.sub(/^\\s*,/,'').gsub(/,(\\s*,)+/,',')" + one_line_title_format_code = one_line_title_format_code.join("\n") + col_name_to_show_short = [] + col_name_to_show_short << analysis_field_name + col_name_to_show_short << slug_title @match_pattern = {"personal_plugin_template" => personal_plugin_template, "plugin_template" => plugin_template, "plugin_template_related" => plugin_template_related, "plugin_template_related_files_fields" => plugin_template_related_files_fields, + "all_fields_to_show" => all_fields_to_show , #所有該模組的欄位 + "all_fields_to_show_in_index" => all_fields_to_show_in_index , #所有該模組的欄位在前台index頁面 + "col_name_to_show_short" => col_name_to_show_short.to_s, #在會員前台頁面的顯示欄位 "col_name_to_show" => col_name_to_show.to_s, #在會員前台頁面的顯示欄位 "col_name_to_show_in_show_page" => col_name_to_show_in_show_page.to_s, #在個人外掛前台show頁面的顯示欄位 "col_name_to_show_in_index_page" => col_name_to_show_in_index_page.to_s, #在個人外掛前台index頁面的顯示欄位 @@ -351,10 +523,15 @@ class Admin::PersonalPluginFieldsController < OrbitMemberController "plugin_template_related_members" => plugin_template_related_members.to_s, "periodic_methods_define" => periodic_methods_define, "related_periodic_methods_define" => related_periodic_methods_define, - "period_fields_text" => period_fields.to_s + "period_fields_text" => period_fields.to_s, + "enable_one_line_title" => enable_one_line_title.to_s, + "one_line_title_format_code" => one_line_title_format_code, + "is_one_line_title" => (enable_one_line_title ? [true] : []), #used in parse_again block if condition + "slug_title_text" => slug_title } 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} + @match_pattern = @match_pattern.sort_by{|k,v| -k.length} + #sort_by{|k,v| (v.class != Array) ? (max_length - k.length) : -k.length} @logs = [] replace_files(files) #Thread.new do @@ -447,7 +624,7 @@ class Admin::PersonalPluginFieldsController < OrbitMemberController 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("\r\n<% end %>") + input_field.prepend("<%= f.fields_for :#{field_name}_translations do |f| %>\n ").concat("\n<% end %>") end if periodic input_fields = [] @@ -510,7 +687,7 @@ class Admin::PersonalPluginFieldsController < OrbitMemberController field_text end fields_text = fields_text.flatten - fields_text.select{|t| t.present?}.join("\r\n") + fields_text.select{|t| t.present?}.join("\n") end def replace_dirs(dirs,is_array = true) dirs.each_with_index do |dir,i| @@ -611,55 +788,62 @@ class Admin::PersonalPluginFieldsController < OrbitMemberController break end end - new_text = new_text.gsub(/[ \t]*parse_again_start((?:(?!parse_again_start).)+)parse_again_end[ \t]*(\r\n|\n)/m) do |ff| + new_text = new_text.gsub(/[ \t]*parse_again_start((?:(?!parse_again_start).)+)parse_again_end[ \t]*(\r\n|\n|$)/m) do |ff| @parse_again_mode = true parse_content = $1 #parse_again block contents result = ff match_count = parse_content.match(/^ *\* *\d+/m) match_count = match_count ? match_count[0] : nil + has_exist_condition = parse_content.split("\n")[0].match(/if[ ]+\w+/).present? exist_condition = parse_content.split("\n")[0].match(/if[ ]+#{::Regexp.escape(k)}(?=\.| |$)/) - extra_cond = nil - if exist_condition - exist_condition = exist_condition[0] - parse_content = parse_content[ parse_content.index(exist_condition) + exist_condition.length..-1] - extra_cond = parse_content.match(/^\.count *(\!|\<|\>|\=)(\=|) *\d+/) - if extra_cond - extra_cond = extra_cond[0] - exist_condition += extra_cond - parse_content = parse_content[extra_cond.length..-1] - exist_condition = eval("vv"+extra_cond) ? exist_condition : nil - elsif vv.count == 0 - exist_condition = nil + if has_exist_condition && exist_condition.nil? #if this block is for other variables, then not proccessing + @parse_again_mode = false + result + else + extra_cond = nil + if exist_condition + exist_condition = exist_condition[0] + parse_content = parse_content[ parse_content.index(exist_condition) + exist_condition.length..-1] + extra_cond = parse_content.match(/^\.count *(\!|\<|\>|\=)(\=|) *\d+/) + if extra_cond + extra_cond = extra_cond[0] + exist_condition += extra_cond + parse_content = parse_content[extra_cond.length..-1] + exist_condition = eval("vv"+extra_cond) ? exist_condition : nil + elsif vv.count == 0 + exist_condition = nil + end + if exist_condition.nil? + match_count = nil + parse_content = "" + result = "" + end + elsif match_count + parse_content = parse_content[match_count.length..-1] end - if exist_condition.nil? - match_count = nil - parse_content = "" - result = "" + parse_content = parse_content.sub(/[ \t]+\z/m, '').sub(/\A(\r\n|\n)/, '') + if include_key(parse_content,k) + vv_count = vv.count + if match_count + vv_count = [vv_count, match_count.strip[1..-1].to_i].min + end + 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 + elsif match_count || exist_condition + count = 1 + if match_count + count = match_count.strip[1..-1].to_i + end + result = (0...count).map {|ii| parse_content }.join("") end - elsif match_count - parse_content = parse_content[match_count.length..-1] + @parse_again_mode = false + result end - parse_content = parse_content.sub(/[ \t]+\z/m, '').sub(/\A(\r\n|\n)/, '') - if include_key(parse_content,k) - vv_count = vv.count - if match_count - vv_count = [vv_count, match_count.strip[1..-1].to_i].min - end - 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 - elsif match_count || exist_condition - count = 1 - if match_count - count = match_count.strip[1..-1].to_i - end - result = (0...count).map {|ii| parse_content }.join("") - end - @parse_again_mode = false - result end + new_text = gsub_text_by_key_value(new_text,k,vv.to_s) end end return new_text @@ -672,6 +856,12 @@ class Admin::PersonalPluginFieldsController < OrbitMemberController if !@parse_again_mode text.gsub(/\n(\s+)[^\n]*(#{k}|#{k.classify})/m) {|ff| @blank_texts << $1.gsub(/(\r\n|\n)/,'')} end + if k == 'display_field_code' + puts nil + puts "display_field_code: @parse_again_mode: #{@parse_again_mode}" + print @blank_texts + puts nil + end 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]}")} @@ -698,7 +888,7 @@ class Admin::PersonalPluginFieldsController < OrbitMemberController if txt_scan.count != 1 delete_plugin(extention_file,plugin_name) txt = File.read(extention_file) rescue "" - txt = txt + "\r\ngem \"#{plugin_name}\", path: \"#{Rails.root}/tmp/#{plugin_name}\"\n" + txt = txt + "\ngem \"#{plugin_name}\", path: \"#{Rails.root}/tmp/#{plugin_name}\"\n" File.open(extention_file,'w+') do |f| f.write(txt) end @@ -707,7 +897,7 @@ class Admin::PersonalPluginFieldsController < OrbitMemberController def delete_plugin(extention_file,plugin_name) txt = File.read(extention_file) rescue nil return if txt.nil? - txt_change = txt.gsub(/^[\n]*gem\s*["']#{plugin_name}["'].*\n/,'') + txt_change = txt.gsub(/^[\s#]*gem\s*["']#{plugin_name}["'].*(\n|$)/m,'') if txt_change != txt File.open(extention_file,'w+') do |f| f.write(txt_change) @@ -744,12 +934,12 @@ class Admin::PersonalPluginFieldsController < OrbitMemberController personal_plugin_field_params[:related_modal_name] = [] end if personal_plugin_field_params[:primary_modal_fields] - personal_plugin_field_params[:primary_modal_fields] = personal_plugin_field_params[:primary_modal_fields].values + personal_plugin_field_params[:primary_modal_fields] = personal_plugin_field_params[:primary_modal_fields].sort_by{|k, h| h[:order].to_i}.map{|v| v[1]} else personal_plugin_field_params[:primary_modal_fields] = [] end if personal_plugin_field_params[:related_modal_fields] - personal_plugin_field_params[:related_modal_fields] = personal_plugin_field_params[:related_modal_fields].values.map{|h| h.values} + personal_plugin_field_params[:related_modal_fields] = personal_plugin_field_params[:related_modal_fields].values.map{|h| h.sort_by{|k, h| h[:order].to_i}.map{|v| v[1]}} else personal_plugin_field_params[:related_modal_fields] = [] end diff --git a/app/models/personal_plugin_field.rb b/app/models/personal_plugin_field.rb index de9cf10..43c2284 100644 --- a/app/models/personal_plugin_field.rb +++ b/app/models/personal_plugin_field.rb @@ -3,6 +3,8 @@ class PersonalPluginField include Mongoid::Timestamps include OrbitModel::Status include MemberHelper + field :one_line_title_format, :type => String, :default => "" + field :enable_one_line_title, :type => Boolean, :default => false field :author_name, :type => String, :default => "",:localize => true field :title, :type => String, :default => "",:localize => true field :module_name, :type => String, :default => "" @@ -81,6 +83,47 @@ class PersonalPluginField return false end end + def get_sorted_fields(root_name, page_name, tds=nil) + if self.fields_order.present? + fields_order = (0...tds.count).to_a + if (self.fields_order["#{root_name}_#{page_name}"].present? rescue false) + self.fields_order["#{root_name}_#{page_name}"].to_a.each_with_index do |order,i| + fields_order[i] = order.to_i + end + end + if tds.respond_to?(:values) + tds = tds.values + end + tds = tds.sort_by.with_index{|td,i| fields_order[i]} + else + field_values = self[root_name][page_name] rescue [] + if field_values.present? + if tds.respond_to?(:values) + new_tds = {} + field_values.each do |field_value| + new_tds[field_value] = tds[field_value] if tds[field_value] + end + tds.each do |k,v| + if new_tds[k].nil? + new_tds[k] = v + end + end + tds = new_tds.values + else + new_tds = field_values.clone + tds.each do |v| + new_tds << v unless new_tds.include?(v) + end + tds = new_tds + end + else + if tds.respond_to?(:values) + tds = tds.values + end + end + end + tds + end def auto_add_display_fields change_primary_fields = self.primary_modal_fields.map{|f| f["field_name"]} - self.primary_modal_fields_was.to_a.map{|f| f["field_name"]} change_related_fields = [] diff --git a/app/views/admin/personal_plugin_fields/_form.html.erb b/app/views/admin/personal_plugin_fields/_form.html.erb index 7f172e2..9754922 100644 --- a/app/views/admin/personal_plugin_fields/_form.html.erb +++ b/app/views/admin/personal_plugin_fields/_form.html.erb @@ -4,10 +4,15 @@ <%= stylesheet_link_tag "lib/fileupload" %> <%= stylesheet_link_tag "lib/main-list" %> <%= stylesheet_link_tag "lib/main-form-col2" %> + <%= stylesheet_link_tag "lib/togglebox"%> <% end %> <% content_for :page_specific_javascript do %> @@ -102,7 +107,14 @@ - +
+ +
+ +
+ <%= f.text_field :one_line_title_format, (f.object.enable_one_line_title ? {} : {:disabled=>'disabled',:id=>'one_line_title_format'}) %> +
+
@@ -151,7 +163,15 @@ <%= link_to t('cancel'), request.referer, :class=>"btn" %> \ No newline at end of file diff --git a/app/views/admin/personal_plugin_fields/fields_setting.html.erb b/app/views/admin/personal_plugin_fields/fields_setting.html.erb index 825169f..eed0b36 100644 --- a/app/views/admin/personal_plugin_fields/fields_setting.html.erb +++ b/app/views/admin/personal_plugin_fields/fields_setting.html.erb @@ -17,6 +17,7 @@ <%= javascript_include_tag "lib/file-type" %> <%= javascript_include_tag "lib/module-area" %> <% end %> +

<%= @personal_plugin_field.title %>

<%= form_for @personal_plugin_field, url: admin_personal_plugin_field_update_fields_setting_path(@personal_plugin_field), html: {class: "form-horizontal main-forms previewable"} do |f| %>
@@ -100,19 +101,11 @@ var old_index = ui.item.attr("index"); var indices = [new_index,old_index].sort(); for(var new_i=indices[0];new_i<=indices[1];new_i++){ - var old_i = ui.item.parent().find(">").eq(new_i).attr("index"); - var org_i = ui.item.parent().find(">").eq(new_i).data("index"); - ui.item.parent().find(">").eq(new_i).attr("index",new_i); - ui.item.parent().parent().siblings("tbody").find("tr:eq(1) >").eq(org_i).val(new_i); - } - var tds = ui.item.parent().parent().siblings("tbody").find("tr:eq(0) >"); - if(tds.length){ - if(new_index > old_index){ //向後移動 - tds.eq(new_index).after(tds.eq(old_index)) - } - else if(new_index < old_index){ //向前移動 - tds.eq(new_index).before(tds.eq(old_index)) - } + var td = ui.item.parent().find(">").eq(new_i); + var old_i =td.attr("index"); + var org_i = td.data("index"); + td.attr("index",new_i); + td.find('.fields_order_hidden_input').val(new_i); } } }); diff --git a/config/locales/en.yml b/config/locales/en.yml index a4f8cc7..959d0f0 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -4,6 +4,8 @@ en: restful_actions: fields_setting: Fields Setting personal_plugin_generator: + enable: Enable + one_line_title_format: "Paper Format Title" download: Download author_translation_name: Author translation name personal_plugin_generator: Personal Plugin Generate diff --git a/config/locales/zh_tw.yml b/config/locales/zh_tw.yml index 3f570ec..7c89925 100644 --- a/config/locales/zh_tw.yml +++ b/config/locales/zh_tw.yml @@ -4,6 +4,8 @@ zh_tw: restful_actions: fields_setting: 欄位設定 personal_plugin_generator: + enable: 啟用 + one_line_title_format: "論文格式標題" download: 下載 author_translation_name: 著作人翻譯名稱 personal_plugin_generator: 個人外掛生成 diff --git a/personal_plugin_generator.gemspec b/personal_plugin_generator.gemspec index d4490d1..3c0de17 100644 --- a/personal_plugin_generator.gemspec +++ b/personal_plugin_generator.gemspec @@ -1,10 +1,10 @@ $:.push File.expand_path("../lib", __FILE__) # Maintain your gem's version: -require "rails" +# require "rails" require "personal_plugin_generator/version" -require "personal_plugin_generator/engine" -PersonalPluginGenerator.git_reset('origin','update') +# require "personal_plugin_generator/engine" +# PersonalPluginGenerator.git_reset('origin','update') # Describe your gem and declare its dependencies: Gem::Specification.new do |s| s.name = "personal_plugin_generator" diff --git a/template_generator/app/assets/images/personal_course/.keep b/template_generator/app/assets/images/personal_plugin_template/.keep similarity index 100% rename from template_generator/app/assets/images/personal_course/.keep rename to template_generator/app/assets/images/personal_plugin_template/.keep diff --git a/template_generator/app/assets/javascripts/personal_course/application.js b/template_generator/app/assets/javascripts/personal_plugin_template/application.js similarity index 100% rename from template_generator/app/assets/javascripts/personal_course/application.js rename to template_generator/app/assets/javascripts/personal_plugin_template/application.js diff --git a/template_generator/app/assets/stylesheets/personal_course/application.css b/template_generator/app/assets/stylesheets/personal_plugin_template/application.css similarity index 100% rename from template_generator/app/assets/stylesheets/personal_course/application.css rename to template_generator/app/assets/stylesheets/personal_plugin_template/application.css diff --git a/template_generator/app/controllers/personal_plugin_templates_controller.rb b/template_generator/app/controllers/personal_plugin_templates_controller.rb index bc16b51..b51dd15 100644 --- a/template_generator/app/controllers/personal_plugin_templates_controller.rb +++ b/template_generator/app/controllers/personal_plugin_templates_controller.rb @@ -1,26 +1,46 @@ class PersonalPluginTemplatesController < ApplicationController + include Admin::PluginTemplatesHelper def index params = OrbitHelper.params plugin_templates = PluginTemplate.sort_for_frontend - fields_to_show = col_name_to_show_in_index_page + page = OrbitHelper.page rescue Page.where(page_id: params[:page_id]).first + parse_again_start * 1 if is_one_line_title.count > 0 + title_is_paper_format = true + if page.custom_string_field == 'table' + title_is_paper_format = false + fields_to_show = page.custom_array_field rescue [] + if fields_to_show.blank? + fields_to_show = col_name_to_show_in_index_page + end + else + fields_to_show = col_name_to_show_short + end + parse_again_end + parse_again_start * 1 if is_one_line_title.count == 0 + title_is_paper_format = false + fields_to_show = page.custom_array_field rescue [] + if fields_to_show.blank? + fields_to_show = col_name_to_show_in_index_page + end + parse_again_end if params[:keywords].present? plugin_templates = filter_keywords(plugin_templates,params[:selectbox],params[:keywords]) end plugin_templates = plugin_templates.page(params[:page_no]).per(OrbitHelper.page_data_count) plugin_templates_list = plugin_templates.collect do |plugin_template| - member = plugin_template.member_profile - - fields_hash = fields_to_show.map{|field| [field, get_display_field(plugin_template,field)]}.to_h - - path = OrbitHelper.url_to_plugin_show(member.to_param, 'member') rescue '#' - fields_hash["authors"] = "#{member.name}" rescue "" - fields_hash["link_to_show"] = OrbitHelper.url_to_show(plugin_template.to_param) - fields_hash + {'jps' => fields_to_show.map{|field| {"value"=> get_display_field(plugin_template,field, title_is_paper_format)}}} end extras = extra_translate_title choice_show = [] + headers = [] fields_to_show.each do |fs| + col = 2 + col = 3 if fs == 'slug_title_text' + headers << { + 'head-title' => t("personal_plugin_template.#{fs}"), + 'col' => col + } choice_show << t("personal_plugin_template.#{fs}") end choice_value = fields_to_show @@ -44,6 +64,7 @@ class PersonalPluginTemplatesController < ApplicationController extras["widget-title"] = I18n.t("module_name.personal_plugin_template") { "plugin_templates" => plugin_templates_list, + "headers" => headers, "extras" => extras, "total_pages" => plugin_templates.total_pages, 'choice' => choice @@ -57,10 +78,29 @@ class PersonalPluginTemplatesController < ApplicationController {"plugin_datas"=>plugin.get_plugin_data(fields_to_show)} end - def get_display_field(plugin_template,field) + def get_display_field(plugin_template,field, title_is_paper_format=false) + text_only = false display_field_code return value end + def get_fields_for_index + @page = Page.find(params[:page_id]) rescue nil + @fields_to_show = all_fields_to_show_in_index + @fields_to_show = @fields_to_show.map { |fs| [t("personal_plugin_template.#{fs}"), fs] } + if @page.present? && @page.custom_string_field == 'table' + @default_fields_to_show = col_name_to_show_in_index_page + else + @default_fields_to_show = col_name_to_show_short + end + render layout: false + end + + def save_index_fields + page = Page.find(params[:page_id]) rescue nil + page.custom_array_field = params[:keys] + page.save + render json: { 'success' => true }.to_json + end def filter_keywords(plugin_templates,select_field,keywords) member_fields = plugin_template_related_members file_fields = plugin_template_related_files_text diff --git a/template_generator/app/helpers/admin/plugin_templates_helper.rb b/template_generator/app/helpers/admin/plugin_templates_helper.rb index ecfa7a8..281a1dd 100644 --- a/template_generator/app/helpers/admin/plugin_templates_helper.rb +++ b/template_generator/app/helpers/admin/plugin_templates_helper.rb @@ -2,6 +2,47 @@ module Admin::PluginTemplatesHelper include OrbitBackendHelper include OrbitFormHelper alias :org_datetime_picker :datetime_picker + def get_authors_text(plugin_template, is_to_sentence=false, locale=nil) + authors_text = Nokogiri::HTML(plugin_template.authors.to_s).text + split_text = authors_text.match(/[、,,\/]/) + split_text = split_text.nil? ? '/' : split_text[0] + full_authors_names = get_member(plugin_template).collect(&:name) + if authors_text.present? + authors_names = authors_text.split(split_text).select{|a| !(full_authors_names.include?(a.strip()))} + full_authors_names += authors_names + end + if is_to_sentence + full_authors_names.to_sentence({:locale=>locale}) + else + full_authors_names.join(split_text) + end + end + def get_authors_show(plugin_template, is_to_sentence=false, locale=nil) + authors_text = Nokogiri::HTML(plugin_template.authors.to_s).text + split_text = authors_text.match(/[、,,\/]/) + split_text = split_text.nil? ? '/' : split_text[0] + full_authors_names = [] + full_authors = get_member(plugin_template).collect do |member| + member_name = member.name + full_authors_names << member_name + "#{member_name}" + end + if authors_text.present? + authors_names = authors_text.split(split_text).select{|a| !(full_authors_names.include?(a.strip()))} + full_authors += authors_names + end + if is_to_sentence + full_authors.to_sentence({:locale=>locale}) + else + full_authors.join(split_text) + end + end + def get_member(plugin_template) + Array(MemberProfile.where(:id.in=>Array(plugin_template).collect(&:member_profile_id).flatten)) + end + def get_member_show(plugin_template) + get_member(plugin_template).collect{|member| "#{member.name}"}.join('/') + end def datetime_picker(*arg,**args) org_datetime_picker(arg,args) end diff --git a/template_generator/app/models/plugin_template.rb b/template_generator/app/models/plugin_template.rb index 5e142aa..8d0b3e6 100644 --- a/template_generator/app/models/plugin_template.rb +++ b/template_generator/app/models/plugin_template.rb @@ -3,6 +3,7 @@ class PluginTemplate include Mongoid::Timestamps include OrbitModel::Status include MemberHelper + include Admin::PluginTemplatesHelper include Slug col_fields @@ -31,9 +32,34 @@ class PluginTemplate def parse_time(time_str) DateTime.parse("0000-01-01 " + time_str) end + parse_again_start * 1 if is_one_line_title.count > 0 + def create_link + one_line_title_format_code + end + parse_again_end def self.get_plugin_datas_to_member(datas) - - fields_to_show = col_name_to_show + page = Page.where(:module => "personal_plugin_template").first rescue nil + parse_again_start * 1 if is_one_line_title.count > 0 + title_is_paper_format = true + if !page.nil? && page.custom_string_field == "table" + title_is_paper_format = false + if !page.custom_array_field.blank? + fields_to_show = page.custom_array_field + else + fields_to_show = col_name_to_show + end + else + fields_to_show = col_name_to_show_short + end + parse_again_end + parse_again_start * 1 if is_one_line_title.count == 0 + title_is_paper_format = false + if !page.nil? && !page.custom_array_field.blank? + fields_to_show = page.custom_array_field + else + fields_to_show = col_name_to_show + end + parse_again_end fields_to_remove = [] @@ -60,7 +86,7 @@ class PluginTemplate pd_data = [] fields_to_show.collect do |t| - pd_data << { "data_title" => p.display_field(t) } + pd_data << { "data_title" => p.display_field(t, false, title_is_paper_format) } end parse_again_start * 1 if plugin_template_related.count > 0 { @@ -106,7 +132,7 @@ class PluginTemplate } end - def display_field(field,text_only=false) + def display_field(field,text_only=false,title_is_paper_format=false) plugin_template = self display_field_code end diff --git a/template_generator/app/views/admin/plugin_templates/_form.html.erb b/template_generator/app/views/admin/plugin_templates/_form.html.erb index 2594bcf..d4d2137 100644 --- a/template_generator/app/views/admin/plugin_templates/_form.html.erb +++ b/template_generator/app/views/admin/plugin_templates/_form.html.erb @@ -159,7 +159,7 @@
<% members = !@plugin_template.member_profile.nil? ? @plugin_template.member_profile.to_a : [] %> - <%= render partial: 'admin/member_selects/email_selection_box', locals: {field: 'plugin_template[member_profile_id]', email_members: members,index:'0',select_name:'member_profile_id'} %> + <%= render partial: 'admin/member_selects/email_selection_box', locals: {field: 'plugin_template[member_profile_id][]', email_members: members,index:'0',select_name:'member_profile_id'} %>
diff --git a/template_generator/app/views/admin/plugin_templates/index.html.erb b/template_generator/app/views/admin/plugin_templates/index.html.erb index e1b30ef..c0393e7 100644 --- a/template_generator/app/views/admin/plugin_templates/index.html.erb +++ b/template_generator/app/views/admin/plugin_templates/index.html.erb @@ -14,7 +14,7 @@
<%= link_to content_tag(:i, nil, :class => 'icon-plus icon-white') + t(:new_), new_admin_plugin_template_path, :class => 'btn btn-primary' %> - parse_again_start * 1 if plugin_template_related.count == 0 + parse_again_start * 1 if plugin_template_related <%= link_to content_tag(:i, nil, :class => 'icon-cog icon-white') + t('setting'), admin_plugin_template_setting_path, :class => 'btn btn-primary pull-right' %> parse_again_end
diff --git a/template_generator/app/views/personal_plugin_templates/get_fields_for_index.html.erb b/template_generator/app/views/personal_plugin_templates/get_fields_for_index.html.erb new file mode 100644 index 0000000..3db06e9 --- /dev/null +++ b/template_generator/app/views/personal_plugin_templates/get_fields_for_index.html.erb @@ -0,0 +1,48 @@ +<% if !@page.nil? %> +
+
+
    + <% if @page.custom_array_field.blank? %> + <% @default_fields_to_show.each do |fs| %> +
  • <%= t("personal_plugin_template.#{fs}") %>
  • + <% end %> + <% else %> + <% @page.custom_array_field.each do |fs| %> +
  • <%= t("personal_plugin_template.#{fs}") %>
  • + <% end %> + <% end %> +
+
+ +
+ +
+ +
+ <%= select_tag "fields_to_show_for_pp", options_for_select(@fields_to_show), prompt: "---Select something---" %> +
+
+ Add Field + + +
+
+ +<% else %> +

Page not found.

+<% end %> \ No newline at end of file diff --git a/template_generator/config/routes.rb b/template_generator/config/routes.rb index 9ddb588..ea1c749 100644 --- a/template_generator/config/routes.rb +++ b/template_generator/config/routes.rb @@ -28,5 +28,7 @@ Rails.application.routes.draw do resources :plugin_template_relateds parse_again_end end + get "/xhr/personal_plugin_template/get_fields_for_index" => "personal_plugin_templates#get_fields_for_index" + post "/xhr/personal_plugin_template/save_index_fields" => "personal_plugin_templates#save_index_fields" end end \ No newline at end of file diff --git a/template_generator/lib/personal_plugin_template/engine.rb b/template_generator/lib/personal_plugin_template/engine.rb index 7542eb6..18a9fb2 100644 --- a/template_generator/lib/personal_plugin_template/engine.rb +++ b/template_generator/lib/personal_plugin_template/engine.rb @@ -4,7 +4,7 @@ module PersonalPluginTemplate OrbitApp.registration "PersonalPluginTemplate",:type=> 'ModuleApp' do module_label 'module_name.plugin_templates' base_url File.expand_path File.dirname(__FILE__) - personal_plugin :enable => true, :sort_number => '35', :app_name=>"PluginTemplate", :intro_app_name=>"PluginTemplateIntro",:path=>"/plugin/personal_plugin_template/profile",:front_path=>"/profile",:admin_path=>"/admin/plugin_templates/",:i18n=>'module_name.plugin_templates', :module_app_name=>'PluginTemplate', :analysis => true, :analysis_path => "/admin/plugin_templates/analysis" + personal_plugin :enable => true, :sort_number => '35', :app_name=>"PluginTemplate", :intro_app_name=>"PluginTemplateIntro",:path=>"/plugin/personal_plugin_template/profile",:front_path=>"/profile",:admin_path=>"/admin/plugin_templates/",:i18n=>'module_name.plugin_templates', :module_app_name=>'PluginTemplate', :one_line_title => enable_one_line_title, :field_modifiable => true, :analysis => true, :analysis_path => "/admin/plugin_templates/analysis" version "0.1" desktop_enabled true diff --git a/template_generator/modules/personal_plugin_template/index.html.erb b/template_generator/modules/personal_plugin_template/index.html.erb index cf5d8c8..12e4f07 100644 --- a/template_generator/modules/personal_plugin_template/index.html.erb +++ b/template_generator/modules/personal_plugin_template/index.html.erb @@ -1,17 +1,13 @@ - - parse_again_start - - parse_again_end + + - - parse_again_start - - parse_again_end + +

{{widget-title}}

{{th-col_name_to_show_in_index_page_arr}}
{{head-title}}
{{col_name_to_show_in_index_page_arr}}
{{value}}
diff --git a/template_generator/modules/personal_plugin_template/index_search1.html.erb b/template_generator/modules/personal_plugin_template/index_search1.html.erb index 805706d..e820df4 100644 --- a/template_generator/modules/personal_plugin_template/index_search1.html.erb +++ b/template_generator/modules/personal_plugin_template/index_search1.html.erb @@ -26,17 +26,13 @@ $( ".selectbox" ).ready(function() { - - parse_again_start - - parse_again_end + + - - parse_again_start - - parse_again_end + +

{{widget-title}}

{{th-col_name_to_show_in_index_page_arr}}
{{head-title}}
{{col_name_to_show_in_index_page_arr}}
{{value}}