Fix bugs.

Add copy page.
This commit is contained in:
BoHung Chiu 2021-03-03 20:24:10 +08:00
parent 92eb18951c
commit 6feba1e50c
11 changed files with 123 additions and 18 deletions

View File

@ -11,7 +11,21 @@ class Admin::PersonalPluginFieldsController < OrbitAdminController
@member = MemberProfile.find_by(:uid=>params['uid']) rescue nil
@personal_plugin_field = PersonalPluginField.new
end
def copy
@member = MemberProfile.find_by(:uid=>params['uid']) rescue nil
attributes = PersonalPluginField.find(params[:personal_plugin_field_id]).attributes rescue {}
attributes = attributes.except("_id")
copy_attributes = {}
attributes.each do |k,v|
if (PersonalPluginField.fields[k].options[:localize] rescue false)
copy_attributes["#{k}_translations"] = v
else
copy_attributes[k] = v
end
end
#render :html => attributes and return
@personal_plugin_field = PersonalPluginField.new(copy_attributes)
end
def create
personal_plugin_field = PersonalPluginField.create(personal_plugin_field_params)
redirect_to params[:referer_url]
@ -75,7 +89,7 @@ class Admin::PersonalPluginFieldsController < OrbitAdminController
I18n.with_locale(locale) do
author_name_translation = I18n.t("personal_plugins.author") if author_name_translation.blank?
end
translate_hash["author_name_translation"] = author_name_translation
translate_hash["member_profile"] = author_name_translation
col_name_translate_yaml = ""
if translate_hash.present?
col_name_translate_yaml = translate_hash.to_yaml.gsub("---\n", '')
@ -111,7 +125,7 @@ class Admin::PersonalPluginFieldsController < OrbitAdminController
" </div>\r\n "
elsif field_name.include?(".")
"<%= #{plugin_template}.#{field_name} rescue \"\" %>"
elsif fields.include?(field_name) || plugin_template_related_members.include?(field_name) #file or link or member
elsif fields.include?(field_name) || plugin_template_related_members.include?(field_name) || field_name == "member_profile" #file or link or member
"<%= #{plugin_template}.display_field(\"#{field_name}\").html_safe rescue \"\" %>"
else
"<%= #{plugin_template}.#{field_name} %>"
@ -184,6 +198,9 @@ class Admin::PersonalPluginFieldsController < OrbitAdminController
"#{@blank_text}}",
"#{@blank_text}join_text = (text_only rescue false) ? \",\" : \"<br>\"",
"#{@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 = \"<a href='\#{path}'>\#{value.name}</a>\"",
"end",
"strftime_hash = #{datetime_fields}",
"if strftime_hash.keys.include?(field)",
@ -274,12 +291,16 @@ class Admin::PersonalPluginFieldsController < OrbitAdminController
replace_dirs(dirs,false)
#end
copy_templates("#{cp_template_dir_path}modules/#{@personal_plugin_field.module_name}/")
render :html => @logs#@match_pattern#@logs.join("<br>").html_safe#@match_pattern
#render :html => @logs#@match_pattern#@logs.join("<br>").html_safe#@match_pattern
@personal_plugin_field.update(:log_text=>"")
render :json => {:success=>true,:url=>"/admin/#{plugin_template.pluralize}/",:name=>@personal_plugin_field.title}
rescue => e
@match_pattern = {"parse_again_start"=>"","parse_again_end"=>"","col_name_translate_yaml"=>""}
replace_files(files)
replace_dirs(dirs,false)
render :html => e.backtrace.join("<br>")
error = e.backtrace.join("<br>")
@personal_plugin_field.update(:log_text=>error)
render :json => {:success=>false,:error=>error}
end
end
def copy_templates(source)
@ -405,7 +426,6 @@ class Admin::PersonalPluginFieldsController < OrbitAdminController
end
sub_dirs = Dir.glob("#{dir}/*/")
files = Dir.glob("#{dir}/*").select { |fn| File.file?(fn) }
@logs << files.to_s if current_index
replace_files(files,current_index)
if sub_dirs.present?
sub_dirs.each do |sub_dir|
@ -450,7 +470,6 @@ class Admin::PersonalPluginFieldsController < OrbitAdminController
if is_array
file_text = replace_text_with_pattern(file_text,false,i,nil,true)
elsif current_index
@logs << sub_file
file_text = replace_text_with_pattern(file_text,false,current_index,nil,true)
else
file_text = replace_text_with_pattern(file_text)
@ -479,9 +498,6 @@ class Admin::PersonalPluginFieldsController < OrbitAdminController
new_text = gsub_text_by_key_value(new_text,k,vv)
end
elsif vv.class == Array
if i
@logs << i
end
if is_file
if v.count == 0
new_text = ""
@ -524,6 +540,16 @@ class Admin::PersonalPluginFieldsController < OrbitAdminController
text = text.gsub(k.classify + "s"){|ff| v.classify.pluralize.gsub("\n","\n#{@blank_texts[i]}")}
text = text.gsub(k.classify){|ff| v.classify.gsub("\n","\n#{@blank_texts[i]}")}
end
def get_all_gem_plugins
extention_files = ["downloaded_extensions.rb","built_in_extensions.rb"]
gem_plugins = extention_files.map{|f| (File.read(f).scan(/\n\s*gem\s*["'](.*)["']\s*,/).flatten rescue [])}.flatten
end
def check_plugin_exist
gem_plugins = get_all_gem_plugins
can_install = !gem_plugins.include?(params[:plugin_name])
can_install = (PersonalPluginField.find(params[:id]).module_name == params[:plugin_name] rescue false) if !can_install
render :json => {:can_install => can_install }
end
private
def personal_plugin_field_params
personal_plugin_field_params = params.require(:personal_plugin_field).permit! rescue {}

View File

@ -12,4 +12,16 @@ class PersonalPluginField
field :related_modal_fields, :type => Array, :default => [] #ex: [[],[]]
field :backend_fields, :type => Hash, :default => {}
field :frontend_fields, :type => Hash, :default => {}
field :log_text, :type => String, :default => ""
before_save :check_plugin_exist
def get_all_gem_plugins
extention_files = ["downloaded_extensions.rb","built_in_extensions.rb"]
gem_plugins = extention_files.map{|f| (File.read(f).scan(/\n\s*gem\s*["'](.*)["']\s*,/).flatten rescue [])}.flatten
end
def check_plugin_exist
gem_plugins = get_all_gem_plugins
can_install = !gem_plugins.include?(self.module_name)
can_install = (self.class.where(:module_name=>self.module_name,:id.ne=>self.id).count == 0 rescue false) if !can_install
return can_install
end
end

View File

@ -228,13 +228,26 @@
}
})
}
$.ajaxSetup({async: false});
$(".main-forms").submit(function(){
if($("#primary_modal_plane .slug_title:checked").length == 0){
alert("<%=thead_field('please_choose_one_slug_title')%>");
}else if($("#primary_modal_plane .slug_title:checked").length > 1){
alert("<%=thead_field('slug_title_can_only_choose_one')%>");
}else{
return true;
var can_install;
$.post("<%=check_plugin_exist_admin_personal_plugin_field_path%>",{plugin_name: $("#personal_plugin_field_module_name").val(), id: "<%=f.object.id %>"}).done(function(data){
console.log(data);
if(data["can_install"]){
can_install = true;
}else{
$("#personal_plugin_field_module_name").css('border', '2px solid red');
alert("<%=thead_field("please_change_module_name")%>");
window.location.href = "#" + $(".main-forms")[0].id;
can_install = false;
}
})
return can_install;
}
$('tr >:last-child').css( 'border', '2px solid red');
return false;

View File

@ -1,9 +1,11 @@
<% @personal_plugin_fields.each do |personal_plugin_field| %>
<tr id="<%= dom_id personal_plugin_field %>" class="with_action">
<td><%= personal_plugin_field.title %></td>
<td><%= personal_plugin_field.module_name %></td>
<td><%= link_to t(:edit) ,edit_admin_personal_plugin_field_path(personal_plugin_field.id),:class=> "btn btn-primary" %>
<%= link_to thead_field('fields_display_setting') ,admin_personal_plugin_field_fields_setting_path(personal_plugin_field.id),:class=> "btn btn-primary" %>
<%= link_to thead_field('generate_plugin') ,admin_personal_plugin_field_generate_plugin_path(personal_plugin_field.id),:class=> "btn btn-primary" %>
<%= link_to thead_field(:copy) ,admin_personal_plugin_field_copy_path(personal_plugin_field.id),:class=> "btn btn-primary" %>
<a data-confirm="Are you sure?" data-method="delete" href="<%=admin_personal_plugin_field_path(personal_plugin_field.id) %>" rel="nofollow" class="delete btn btn-primary"><%= t(:delete_) %></a>
</td>
</tr>
<% end %>

View File

@ -18,6 +18,9 @@
<th><%="#{related_modal_name}-#{field_value[:translation_name][I18n.locale] rescue ""}-#{field_value[:field_name]}"%></th>
<% end %>
<% end %>
<% author_name_translation = @personal_plugin_field.author_name_translations[locale] rescue ""
author_name_translation = I18n.t("personal_plugins.author") if author_name_translation.blank? %>
<th><%= author_name_translation %></th>
<% end %>
</tr>
</thead>
@ -40,6 +43,7 @@
</td>
<% end %>
<% end %>
<td><%= check_box_tag "#{f.object_name}[]", "member_profile" , (object.send(root_name)[page_name].include?("member_profile") rescue false),:id=>nil %></td>
<% end %>
<% end %>
<% end %>

View File

@ -0,0 +1,5 @@
<%= form_for @personal_plugin_field, url: admin_personal_plugin_fields_path, html: {class: "form-horizontal main-forms previewable"} do |f| %>
<fieldset>
<%= render partial: 'form', locals: {f: f} %>
</fieldset>
<% end %>

View File

@ -2,6 +2,7 @@
<thead>
<tr>
<th><%= thead_field("personal_plugin_name") %></th>
<th><%= thead_field("module_name") %></th>
<th><%= thead_field("action") %></th>
</tr>
</thead>
@ -9,7 +10,14 @@
<%= render 'personal_plugin_fields' %>
</tbody>
</table>
<div id='dialog-confirm' title="<%= thead_field("generate_plugin") %>" style="display: none;">
<div style="clear:both;"></div>
<div id="info_texts">
<image src="/assets/preloader.gif" id="preloader"></image>
<div id="generating_plugin"><%=thead_field("generating_plugin")%></div>
<div id="info_logs"></div>
</div>
</div>
<div class="bottomnav clearfix">
<div class="action pull-right">
<%= link_to content_tag(:i, nil, :class => 'icon-plus icon-white') + t(:new_), new_admin_personal_plugin_field_path, :class => 'btn btn-primary' %>
@ -17,4 +25,27 @@
<div class="pagination pagination-centered">
<%= content_tag :div, paginate(@personal_plugin_fields), class: "pagination pagination-centered" %>
</div>
</div>
</div>
<script type="text/javascript">
$(".generate_plugin").off("click").on("click",function(){
$( "#dialog-confirm" ).dialog({
resizable: true,
minHeight: 300,
maxHeight: 400,
modal: true,
width: '80%',
close: function(){$( this ).dialog( "close" );},
open: function(){$("#preloader").css("display",'');$("#generating_plugin").css("display",'');$( "#info_logs" ).html("");}
})
var url = $(this).data("url");
$.get(url).done(function(data){
$("#preloader").css("display",'none');
$("#generating_plugin").css("display",'none');
if(data["success"]){
$( "#info_logs" ).html("<span><%=thead_field("goto")%><a target=\"_blank\" href=\""+data["url"]+"\" title=\"<%=thead_field("goto")%> "+data["name"]+"\">"+data["name"]+"</a></span>")
}else{
$( "#info_logs" ).html(data["error"]);
}
})
})
</script>

View File

@ -40,4 +40,8 @@ en:
generate_plugin: Generate plugin
member: Member
link: Link
analysis: Analysis Page
analysis: Analysis Page
goto: "Go to "
generating_plugin: Generating plugin
please_change_module_name: "Please change module name(This name already exist)."
copy: Copy

View File

@ -40,4 +40,8 @@ zh_tw:
generate_plugin: 生成個人外掛
member: 會員
link: 連結
analysis: 分析頁面
analysis: 分析頁面
goto: "前往 "
generating_plugin: 正在生成個人外掛中...
please_change_module_name: "請更改模組名稱(此名稱已存在)"
copy: 複製

View File

@ -7,7 +7,11 @@ Rails.application.routes.draw do
post 'update_fields_setting' , to: 'personal_plugin_fields#update_fields_setting'
patch 'update_fields_setting' , to: 'personal_plugin_fields#update_fields_setting'
get 'generate_plugin' , to: 'personal_plugin_fields#generate_plugin'
get 'copy' , to: 'personal_plugin_fields#copy'
end
end
resource :personal_plugin_field do
post 'check_plugin_exist' ,to: 'personal_plugin_fields#check_plugin_exist'
end
end
end
end

View File

@ -156,7 +156,7 @@ parse_again_end
<% else %>
<div class="control-group big-group">
<label class="control-label muted"><%= t("personal_plugin_template.author_name_translation") %></label>
<label class="control-label muted"><%= t("personal_plugin_template.member_profile") %></label>
<div class="controls">
<% 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'} %>