class Admin::PersonalPluginFieldsController < OrbitAdminController require 'fileutils' include Admin::PersonalPluginFieldsHelper before_action :set_personal_plugin_field, only: [:show, :edit , :update, :destroy, :fields_setting, :update_fields_setting,:generate_plugin] def index @personal_plugin_fields = PersonalPluginField.order_by(:created_at=>'desc').page(params[:page]).per(10) end def new @member = MemberProfile.find_by(:uid=>params['uid']) rescue nil @personal_plugin_field = PersonalPluginField.new end def create personal_plugin_field = PersonalPluginField.create(personal_plugin_field_params) redirect_to params[:referer_url] end def edit end def destroy @personal_plugin_field.destroy redirect_to admin_personal_plugin_fields_path(:page => params[:page]) end def update @personal_plugin_field.update_attributes(personal_plugin_field_params) @personal_plugin_field.save redirect_to params[:referer_url] end def fields_setting end def update_fields_setting field_params = params.require(:personal_plugin_field).permit! rescue {} @personal_plugin_field.update_attributes(field_params) @personal_plugin_field.save redirect_to params[:referer_url] end def generate_plugin template_dir_path = File.expand_path("../../../views/admin/personal_plugin_fields/template_generator/", __FILE__) + "/" cp_template_dir_path = "#{Rails.root}/tmp/#{@personal_plugin_field.module_name}/" FileUtils.rm_rf(cp_template_dir_path) FileUtils.cp_r(template_dir_path,cp_template_dir_path) dirs = Dir.glob("#{cp_template_dir_path}*/") files = Dir.glob("#{cp_template_dir_path}*").select { |fn| File.file?(fn) } in_use_locales = Site.first.in_use_locales 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 yml_text = File.read(yml_file) translate_hash = {} @personal_plugin_field.primary_modal_fields.each do |field_value| if field_value[:field_name] && (field_value[:translation_name][locale].present? rescue false) translate_hash[field_value[:field_name]] = field_value[:translation_name][locale] end end @personal_plugin_field.related_modal_name.each_with_index do |related_modal_name,i| field_values = @personal_plugin_field.related_modal_fields[i].to_a rescue [] sub_hash = {} field_values.each do |field_value| if field_value[:field_name] && (field_value[:translation_name][locale].present? rescue false) sub_hash[field_value[:field_name]] = field_value[:translation_name][locale] end end if related_modal_name.present? && sub_hash.present? translate_hash[related_modal_name] = sub_hash end end 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) File.open(yml_file,'w+') do |f| f.write(yml_text) end end plugin_template_related_files_fields = @personal_plugin_field.primary_modal_fields.select{|field_value| field_value[:field_type] == "file" rescue false}.map{|v| v[:field_name]} plugin_template_related_files_fields = plugin_template_related_files_fields.map{|field_name| "has_many :#{field_name.pluralize}, :dependent => :destroy, :autosave => true\n" + "accepts_nested_attributes_for :#{field_name.pluralize}, :allow_destroy => true\n" }.join("\n") 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 [] 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}\")"] }.to_h col_fields = "" col_fields = @personal_plugin_field.primary_modal_fields.map do |field_value| type = "String" if field_value[:field_type] field_text = "field :#{field_value[:field_name]}, :type => #{type}, :default => {}" field_text += ", :localize => true" if field_value[:localize] end col_fields = col_fields.join("\r\n ") col_related_fields = "" @match_pattern = {"personal_plugin_template" => @personal_plugin_field.module_name, "plugin_template" => @personal_plugin_field.primary_modal_name, "plugin_template_related" => @personal_plugin_field.related_modal_name.select{|n| n.present?}, "plugin_template_related_files_fields" => plugin_template_related_files_fields, "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頁面的顯示欄位 "extra_translate_title" => extra_translate_title.to_s, "col_fields" => col_fields } @match_pattern = @match_pattern.sort_by{|k,v| -k.length}.sort_by{|k,v| (v.class != Array) ? 1 : 0} replace_files(files) #Thread.new do dirs.each do |dir| replace_dir(dir) end #end render :html => @match_pattern end def replace_dir(dir) dir = replace_file(dir) return true if dir.blank? sub_dirs = Dir.glob("#{dir}/*/") files = Dir.glob("#{dir}/*").select { |fn| File.file?(fn) } replace_files(files) if sub_dirs.present? sub_dirs.each do |sub_dir| replace_dir(sub_dir) end else return true end end def replace_files(files) files.each do |file| replace_file(file) end end def replace_file(file) isfile = File.file?(file) path = File.dirname(file) file_name = File.basename(file) new_filename = replace_text_with_pattern(file_name,true) if new_filename.blank? FileUtils.rm_rf("#{path}/#{file_name}") elsif file_name != new_filename FileUtils.mv("#{path}/#{file_name}", "#{path}/#{new_filename}") end if new_filename.blank? return "" else if isfile file_text = File.read("#{path}/#{new_filename}") file_text = replace_text_with_pattern(file_text) File.open("#{path}/#{new_filename}",'w+'){|f| f.write(file_text)} end return "#{path}/#{new_filename}" end end def replace_text_with_pattern(text,is_file=false,i = nil) new_text = text @match_pattern.each do |k,v| vv = v vv = vv[i] if i if vv.class == String if i && vv == "" new_text = "" puts k else new_text = gsub_text_by_key_value(new_text,k,vv) end elsif vv.class == Array if is_file && v.count == 0 && (new_text.include?(k) || new_text.include?(k.classify)) new_text = "" break end new_text = new_text.gsub(/<%[ ]*parse_again_start[ ]*%>((?:(?!<%[ ]*parse_again_start[ ]*%>).)+)<%[ ]*parse_again_end[ ]*%>/m) do |ff| puts ff ff = ff.strip result = (0...vv.count).map {|i| replace_text_with_pattern(ff,false,i) }.join("\r\n") end end end return new_text 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.pluralize.classify) text = text.gsub(k.classify,v.classify) end private def personal_plugin_field_params personal_plugin_field_params = params.require(:personal_plugin_field).permit! rescue {} if personal_plugin_field_params[:related_modal_name].nil? 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 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} else personal_plugin_field_params[:related_modal_fields] = [] end return personal_plugin_field_params end def set_personal_plugin_field path = request.path.split('/') if path.last.include? '-' uid = path[-1].split("-").last uid = uid.split("?").first else uid = path[-2].split("-").last uid = uid.split("?").first end @personal_plugin_field = PersonalPluginField.find_by(:uid => uid) rescue PersonalPluginField.find(params[:id] || params[:personal_plugin_field_id]) end end