From 44188cb6144ec2ac4e8b4724bee148978e5681fd Mon Sep 17 00:00:00 2001 From: BOHUNG Date: Sun, 29 Sep 2019 12:18:19 +0800 Subject: [PATCH] fix bug about recovery font and delete font and add edit all font --- .../admin/patchfiles_controller.rb | 48 +++++++++++++++---- app/models/fontfile.rb | 21 ++++---- app/models/patchfilefield.rb | 38 ++++++--------- .../admin/patchfiles/editckeditor.html.erb | 47 +++++++++++++++--- .../admin/patchfiles/showckeditor.html.erb | 3 +- 5 files changed, 103 insertions(+), 54 deletions(-) diff --git a/app/controllers/admin/patchfiles_controller.rb b/app/controllers/admin/patchfiles_controller.rb index 39f30fe..baa25a3 100644 --- a/app/controllers/admin/patchfiles_controller.rb +++ b/app/controllers/admin/patchfiles_controller.rb @@ -76,22 +76,50 @@ class Admin::PatchfilesController < OrbitAdminController def newfont if params["patchfilefield"]["initial_btn"] == "true" #render :html => @field.id and return - @field_to_copy = Patchfilefield.where("title"=>@app_title + "_back").first - @id = @field.id - @field = @field_to_copy - @field.id = @id - @field.title = @app_title + @field_to_copy = Patchfilefield.where("title"=>(@app_title + "_back")).take_while{true}.last + @field_to_copy.attributes.each do |copy_field,value| + if copy_field != "_id" || copy_field != "title" + @field[copy_field] = value + end + end @field.update + @field = Patchfilefield.where("title"=>@app_title).first + @field.update + @field = Patchfilefield.where("title"=>@app_title).first + @font_arr = [] + @field.font_array.each do |font,istrue| + @font_arr.push font + end + @font_arr.each do |font,istrue| + if (@field_to_copy.font_array.keys.include? font) == false + @field.font_array.delete(font) + @field.delete_font = font + @field.update + @field = Patchfilefield.where("title"=>@app_title).first + end + end + redirect_to admin_patchfiles_editckeditor_path and return + elsif params["patchfilefield"]["delete_btn"] == "true" + params["patchfilefield"]["font_array"] = {} if params["patchfilefield"]["font_array"].nil? == true + params["patchfilefield"]["font_array"].each do |font,istrue| + if istrue =="1" + @field.font_array.delete(font) + @field.delete_font = font + @field.update + @field = Patchfilefield.where("title"=>@app_title).first + end + end redirect_to admin_patchfiles_editckeditor_path and return else params["patchfilefield"]["new_add_font"] = params["patchfilefield"]["new_add_font"].gsub('\"','').gsub("\'",'').gsub(";",'') @field.new_add_font = params["patchfilefield"]["new_add_font"] rescue (redirect_to admin_patchfiles_showckeditor_path and return) @key_arr = @field.font_array.keys - params["patchfilefield"]["font_array"].each_with_index do |(font,istrue),index| + params["patchfilefield"]["font_array"] = {} if params["patchfilefield"]["font_array"].nil? == true + params["patchfilefield"]["font_array"].each do |font,istrue| if istrue =="0" - @field.font_array[@key_arr[index]][font] = false + @field.font_array[font] = false elsif istrue =="1" - @field.font_array[@key_arr[index]][font] = true + @field.font_array[font] = true end end @field.update @@ -138,9 +166,9 @@ class Admin::PatchfilesController < OrbitAdminController end end def create_first_field - if Patchfilefield.where("title"=>@app_title + "_back").take_while{true}.count == 0 + if Patchfilefield.where("title"=>(@app_title + "_back")).take_while{true}.count == 0 @field = Patchfilefield.new() - @field.title=@app_title +"_back" + @field.title = @app_title +"_back" @field.save end @field diff --git a/app/models/fontfile.rb b/app/models/fontfile.rb index 043eeb5..a68725e 100644 --- a/app/models/fontfile.rb +++ b/app/models/fontfile.rb @@ -168,22 +168,17 @@ class Fontfile def change_font_name if self.old_title !="" && self.old_title != self.title @field = Patchfilefield.where("title"=>"patchfile").first - @index @font_old_name - @field.font_array.each do |index,font_hash| - font_hash.each do |font,istrue| - if font == self.old_title - @index = index - @font_old_name = font - break - end + @field.font_array.each do |font,istrue| + if font == self.old_title + @font_old_name = font + break end - break if @index != nil end - if @font_old_name.nil? == false && @index.nil? == false - @istrue = @field.font_array[@index.to_s][@font_old_name] - @field.font_array[@index.to_s][self.title] = @istrue - @field.font_array[@index.to_s].delete(@font_old_name) + if @font_old_name.nil? == false + @istrue = @field.font_array[@font_old_name] + @field.font_array[self.title] = @istrue + @field.font_array.delete(@font_old_name) @field.delete_font = @font_old_name @field.update @field = Patchfilefield.where("title"=>"patchfile").first diff --git a/app/models/patchfilefield.rb b/app/models/patchfilefield.rb index 5917e38..9b2180d 100644 --- a/app/models/patchfilefield.rb +++ b/app/models/patchfilefield.rb @@ -24,11 +24,9 @@ class Patchfilefield after_save :set_config #before_create :set_expire def delete_empty_font - self.font_array.each do |index,font_hash| - font_hash.each do |font,istrue| - if font.strip == "" - self.font_array.delete(index.to_s) - end + self.font_array.each do |font,istrue| + if font.strip == "" + self.font_array.delete(font) end end end @@ -70,30 +68,28 @@ class Patchfilefield @font_array.each do |font| @i = 0 if font != self.delete_font - self.font_array.values.each do |each_font| - if each_font.keys.first == font + self.font_array.keys.each do |each_font| + if each_font == font @i = 1 end break if @i == 1 end if @i == 0 - @count = (self.font_array.keys[-1].to_i + 1).to_s - self.font_array = self.font_array.merge({@count =>{font => true}}) + self.font_array = self.font_array.merge({font => true}) end end end end if self.new_add_font.strip !="" @i = 0 - self.font_array.values.each do |each_font| - if each_font.keys.first == self.new_add_font + self.font_array.keys.each do |each_font| + if each_font == self.new_add_font @i = 1 end break if @i == 1 end if @i == 0 - @count = (self.font_array.keys[-1].to_i + 1).to_s - self.font_array = self.font_array.merge({@count =>{self.new_add_font.strip => true}}) + self.font_array = self.font_array.merge({self.new_add_font.strip => true}) end self.new_add_font = "" end @@ -104,22 +100,18 @@ class Patchfilefield # end def set_un_used_font @un_use_font = [] - self.font_array.each do |index,font_hash| - font_hash.each do |font,istrue| - if istrue == false - @un_use_font.push font.strip - end + self.font_array.each do |font,istrue| + if istrue == false + @un_use_font.push font.strip end end self.un_used_font = @un_use_font end def set_used_font @use_font = [] - self.font_array.each do |index,font_hash| - font_hash.each do |font,istrue| - if istrue == true - @use_font.push font.strip - end + self.font_array.each do |font,istrue| + if istrue == true + @use_font.push font.strip end end @use_font.delete("") diff --git a/app/views/admin/patchfiles/editckeditor.html.erb b/app/views/admin/patchfiles/editckeditor.html.erb index df80cc3..5a377ba 100644 --- a/app/views/admin/patchfiles/editckeditor.html.erb +++ b/app/views/admin/patchfiles/editckeditor.html.erb @@ -1,16 +1,19 @@

CKEDITOR現有字形

+ + +
<%=form_for @field,:url=>{:controller=>"patchfiles" ,:action=>"newfont"} do |f|%> -
+
<%= f.fields_for :font_array do |fontfield|%> <% @arr = @field.font_array.keys %> <% for @i in @arr %>
- <% if @field.font_array[@i.to_s].values.first == true%> - <%=fontfield.check_box @field.font_array[@i.to_s].keys.first,{:checked=>true,:id=>"checkbox"+@i.to_s,:style=>"float:left;"} %> + <% if @field.font_array[@i.to_s] == true%> + <%=fontfield.check_box @i.to_s,{:checked=>true,:id=>"checkbox"+@i.to_s,:style=>"float:left;"} %> <% else %> - <%=fontfield.check_box @field.font_array[@i.to_s].keys.first,{:id=>"checkbox"+@i.to_s,:style=>"float:left;"} %> + <%=fontfield.check_box @i.to_s,{:id=>"checkbox"+@i.to_s,:style=>"float:left;"} %> <% end %> - <%=fontfield.label @field.font_array[@i.to_s].keys.first,{:for=>"checkbox"+@i.to_s,:style=>"float:right;"}%> + <%=fontfield.label @i.to_s ,{:for=>"checkbox"+@i.to_s,:style=>"float:right;"}%>
<%end%> <%end%> @@ -20,8 +23,20 @@ <%=f.submit "#{t(:updatefont)}",{:id=>"submitbtn"}%> <%=f.text_field :initial_btn, {:id=>"hidden-initial",:type => "hidden",:value=>"false"} %> + + <%=f.text_field :delete_btn, {:id=>"hidden-delete",:type => "hidden",:value=>"false"} %> <%end%> - +
  • + CKEDITOR現有字形 + <% @ckicktosee_str = (I18n.locale.to_s == "en") ? "click me to see all font which CKEDITOR have now" : "點擊我來查看編輯器所有目前擁有的字形" %> + <%= @ckicktosee_str %> +
      + <% @field.font_array.each do |font,istrue|%> + <% @inform = (I18n.locale.to_s == "en") ? "click me to edit" : "點擊我來編輯" %> +
    • <%= font %>
    • + <%end%> +
    +