This commit is contained in:
BoHung Chiu 2022-07-27 13:57:10 +08:00
parent 5f24254ae3
commit 5659e0531e
1 changed files with 92 additions and 92 deletions

View File

@ -1,15 +1,15 @@
class Admin::PersonalPluginFieldsController < OrbitMemberController
require 'fileutils'
include Admin::PersonalPluginFieldsHelper
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)
@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
@personal_plugin_field = PersonalPluginField.new
end
def download
zip_path = "tmp/"
@ -40,7 +40,7 @@ class Admin::PersonalPluginFieldsController < OrbitMemberController
end
end
#render :html => attributes and return
@personal_plugin_field = PersonalPluginField.new(copy_attributes)
@personal_plugin_field = PersonalPluginField.new(copy_attributes)
end
def create
personal_plugin_field = PersonalPluginField.create(personal_plugin_field_params)
@ -226,7 +226,7 @@ class Admin::PersonalPluginFieldsController < OrbitMemberController
"#{@blank_text}members = #{plugin_template}.send(field.pluralize)",
"#{@blank_text}value = members.map{|m|",
"#{@blank_text*2}path = OrbitHelper.url_to_plugin_show(m.to_param, 'member') rescue '#'",
"#{@blank_text*2}((text_only rescue false) ? m.name : \"<a href='\#{path}'>\#{m.name}</a>\")",
"#{@blank_text*2}((text_only rescue false) ? m.name : \"<a href='\#{path}'>\#{m.name}</a>\")",
"#{@blank_text}}",
"#{@blank_text}join_text = (text_only rescue false) ? \",\" : \"<br>\"",
"#{@blank_text}value = value.join(join_text)",
@ -252,11 +252,11 @@ class Admin::PersonalPluginFieldsController < OrbitMemberController
}
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|
" 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]]}\")"
@ -267,24 +267,24 @@ class Admin::PersonalPluginFieldsController < OrbitMemberController
" \"\#{#{field_value[:field_name]}_start} ~ \#{#{field_value[:field_name]}_end}\"",
"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|
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 = ""
personal_plugin_template = @personal_plugin_field.module_name
iterate_step_text = "1.minute"
if analysis_field_name.present?
field_type = primary_modal_fields.select{|f| f[:field_name] == analysis_field_name}.first[:field_type] rescue "date_time"
" \"\#{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 = ""
personal_plugin_template = @personal_plugin_field.module_name
iterate_step_text = "1.minute"
if analysis_field_name.present?
field_type = primary_modal_fields.select{|f| f[:field_name] == analysis_field_name}.first[:field_type] rescue "date_time"
analysis_field_input_fields = ["start","end"].map{|f|
"<span><%=t(\"#{personal_plugin_template}.extend_translate.#{f}_#{field_type}\")%></span>" +
@ -300,15 +300,15 @@ class Admin::PersonalPluginFieldsController < OrbitMemberController
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")
plugin_template_sort_hash = {}
@personal_plugin_field.backend_fields[:sort_asc].to_a.each do |f|
plugin_template_sort_hash[f.to_sym] = 1
end rescue nil
@personal_plugin_field.backend_fields[:sort_desc].to_a.each do |f|
@personal_plugin_field.backend_fields[:sort_asc].to_a.each do |f|
plugin_template_sort_hash[f.to_sym] = 1
end rescue nil
@personal_plugin_field.backend_fields[:sort_desc].to_a.each do |f|
plugin_template_sort_hash[f.to_sym] = -1
end rescue nil
if @personal_plugin_field.backend_fields[:sort_desc].to_a.count != 0
plugin_template_sort_hash[:id] = -1
end
end rescue nil
if @personal_plugin_field.backend_fields[:sort_desc].to_a.count != 0
plugin_template_sort_hash[:id] = -1
end
@match_pattern = {"personal_plugin_template" => personal_plugin_template,
"plugin_template" => plugin_template,
"plugin_template_related" => plugin_template_related,
@ -689,54 +689,54 @@ class Admin::PersonalPluginFieldsController < OrbitMemberController
render :json => {:can_install => can_install }
end
def add_plugin(extention_file,plugin_name)
txt = File.read(extention_file) rescue nil
if txt.nil?
File.open(extention_file,'w+'){|f| f.write("")}
txt = ""
end
txt_scan = txt.scan(/^[\n]*gem\s*["']#{plugin_name}["'].*\n/)
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}\"\r\n"
File.open(extention_file,'w+') do |f|
f.write(txt)
end
end
end
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/,'')
if txt_change != txt
File.open(extention_file,'w+') do |f|
f.write(txt_change)
end
end
end
def check_modal_name
primary_modal_names = PersonalPluginField.where(:id.ne=>params[:id]).pluck(:primary_modal_name)
related_modal_names = PersonalPluginField.where(:id.ne=>params[:id]).pluck(:related_modal_name).flatten.uniq
other_modal_names = primary_modal_names + related_modal_names
personal_plugin_field = PersonalPluginField.where(:id=>params[:id]).first
all_modal_names = PersonalPluginField.get_modal_names_cache
if personal_plugin_field.present?
except_modals = Dir.glob("tmp/#{personal_plugin_field.module_name}/app/models/*.rb").map{|f|
fn = File.basename(f)
fn.slice(0,fn.length - 3)
}
all_modal_names = all_modal_names - except_modals
end
all_modal_names = all_modal_names + other_modal_names
all_modal_names = all_modal_names.uniq
self_modal_names = params[:modal_names]
invalid_modal_names = self_modal_names.select{|n| all_modal_names.include?(n)}
if invalid_modal_names.count != 0
render :json => {:success=>false,:invalid_modal_names=>invalid_modal_names}
else
render :json => {:success=>true}
end
end
txt = File.read(extention_file) rescue nil
if txt.nil?
File.open(extention_file,'w+'){|f| f.write("")}
txt = ""
end
txt_scan = txt.scan(/^[\n]*gem\s*["']#{plugin_name}["'].*\n/)
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"
File.open(extention_file,'w+') do |f|
f.write(txt)
end
end
end
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/,'')
if txt_change != txt
File.open(extention_file,'w+') do |f|
f.write(txt_change)
end
end
end
def check_modal_name
primary_modal_names = PersonalPluginField.where(:id.ne=>params[:id]).pluck(:primary_modal_name)
related_modal_names = PersonalPluginField.where(:id.ne=>params[:id]).pluck(:related_modal_name).flatten.uniq
other_modal_names = primary_modal_names + related_modal_names
personal_plugin_field = PersonalPluginField.where(:id=>params[:id]).first
all_modal_names = PersonalPluginField.get_modal_names_cache
if personal_plugin_field.present?
except_modals = Dir.glob("tmp/#{personal_plugin_field.module_name}/app/models/*.rb").map{|f|
fn = File.basename(f)
fn.slice(0,fn.length - 3)
}
all_modal_names = all_modal_names - except_modals
end
all_modal_names = all_modal_names + other_modal_names
all_modal_names = all_modal_names.uniq
self_modal_names = params[:modal_names]
invalid_modal_names = self_modal_names.select{|n| all_modal_names.include?(n)}
if invalid_modal_names.count != 0
render :json => {:success=>false,:invalid_modal_names=>invalid_modal_names}
else
render :json => {:success=>true}
end
end
private
def personal_plugin_field_params
personal_plugin_field_params = params.require(:personal_plugin_field).permit! rescue {}
@ -759,19 +759,19 @@ class Admin::PersonalPluginFieldsController < OrbitMemberController
def set_personal_plugin_field
PersonalPluginField.get_modal_names_cache
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])
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
def bundle_install
Bundler.with_clean_env { system("cd #{Rails.root} && bundle install") }
%x(kill -s USR2 `cat tmp/pids/unicorn.pid`)
sleep 2
Bundler.with_clean_env { system("cd #{Rails.root} && bundle install") }
%x(kill -s USR2 `cat tmp/pids/unicorn.pid`)
sleep 2
end
end