From 7eb02f0d1c13b39b5c47eaf01c0272fb15fb5979 Mon Sep 17 00:00:00 2001 From: BOHUNG Date: Sat, 28 Sep 2019 23:38:15 +0800 Subject: [PATCH] fix some bug and add the feature that recovery ckeditor font list --- .../admin/patchfiles_controller.rb | 39 ++++++++++----- app/models/patchfilefield.rb | 19 ++++++-- .../admin/patchfiles/editckeditor.html.erb | 47 +++++++++++-------- 3 files changed, 67 insertions(+), 38 deletions(-) diff --git a/app/controllers/admin/patchfiles_controller.rb b/app/controllers/admin/patchfiles_controller.rb index 382c64e..39f30fe 100644 --- a/app/controllers/admin/patchfiles_controller.rb +++ b/app/controllers/admin/patchfiles_controller.rb @@ -15,10 +15,6 @@ class Admin::PatchfilesController < OrbitAdminController @categories = @module_app.categories.enabled @filter_fields = filter_fields(@categories, @tags) @table_fields = [:status, :category, :title, :start_date, :end_date, :last_modified] - #@file_to_show= Filefield.new - #@file_to_show.save - #@file_to_show = Filefield.find().first - #@file_to_show = @file.file_show if request.xhr? render :partial => "index" end @@ -78,17 +74,29 @@ class Admin::PatchfilesController < OrbitAdminController redirect_to admin_patchfiles_showckeditor_path end def newfont - 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) - params["patchfilefield"]["font_array"].each_with_index do |(font,istrue),index| - if istrue =="0" - @field.font_array[index.to_s][font] = false - elsif istrue =="1" - @field.font_array[index.to_s][font] = true + 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.update + 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| + if istrue =="0" + @field.font_array[@key_arr[index]][font] = false + elsif istrue =="1" + @field.font_array[@key_arr[index]][font] = true + end end + @field.update + redirect_to admin_patchfiles_showckeditor_path and return end - @field.update - redirect_to admin_patchfiles_showckeditor_path end def getfont @ckeditor_path = ENV['PWD'] + '/app/assets/javascripts/ckeditor' @@ -130,6 +138,11 @@ class Admin::PatchfilesController < OrbitAdminController end end def create_first_field + if Patchfilefield.where("title"=>@app_title + "_back").take_while{true}.count == 0 + @field = Patchfilefield.new() + @field.title=@app_title +"_back" + @field.save + end @field if Patchfilefield.where("title"=>@app_title).take_while{true}.count == 0 @field = Patchfilefield.new() diff --git a/app/models/patchfilefield.rb b/app/models/patchfilefield.rb index 5f57f48..5917e38 100644 --- a/app/models/patchfilefield.rb +++ b/app/models/patchfilefield.rb @@ -17,12 +17,21 @@ class Patchfilefield # before_destroy :destroy_email scope :can_display, ->{where(:is_hidden=>false,:is_preview => false).any_of({:postdate.lt=>Time.now, :deadline.gt=>Time.now},{:postdate.lt=>Time.now, :deadline=>nil}).order_by([:is_top, :desc],[:postdate, :desc])} scope :is_approved, ->{where(:approved => true)} - before_create :get_font_to_store,:set_un_used_font , :set_used_font - before_update :get_font_to_store,:set_un_used_font , :set_used_font - before_save :get_font_to_store,:set_un_used_font , :set_used_font + before_create :delete_empty_font ,:get_font_to_store,:set_un_used_font , :set_used_font + before_update :delete_empty_font ,:get_font_to_store,:set_un_used_font , :set_used_font + before_save :delete_empty_font ,:get_font_to_store,:set_un_used_font , :set_used_font after_update :set_config 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 + end + end + end def get_font_to_store @ckeditor_path = ENV['PWD'] + '/app/assets/javascripts/ckeditor' @filedata = File.read(@ckeditor_path+"/config.js.erb") @@ -68,7 +77,7 @@ class Patchfilefield break if @i == 1 end if @i == 0 - @count = self.font_array.count + @count = (self.font_array.keys[-1].to_i + 1).to_s self.font_array = self.font_array.merge({@count =>{font => true}}) end end @@ -83,7 +92,7 @@ class Patchfilefield break if @i == 1 end if @i == 0 - @count = self.font_array.count + @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}}) end self.new_add_font = "" diff --git a/app/views/admin/patchfiles/editckeditor.html.erb b/app/views/admin/patchfiles/editckeditor.html.erb index ec0d393..df80cc3 100644 --- a/app/views/admin/patchfiles/editckeditor.html.erb +++ b/app/views/admin/patchfiles/editckeditor.html.erb @@ -1,26 +1,26 @@

CKEDITOR現有字形

<%=form_for @field,:url=>{:controller=>"patchfiles" ,:action=>"newfont"} do |f|%> -<% @i = 0 %> -
- <%= f.fields_for :font_array do |fontfield|%> - <% @count = @field.font_array.count %> - <% for @i in 0...@count %> -
- <% 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;"} %> - <% else %> - <%=fontfield.check_box @field.font_array[@i.to_s].keys.first,{: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;"}%> -
+
+ <%= 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;"} %> + <% else %> + <%=fontfield.check_box @field.font_array[@i.to_s].keys.first,{: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;"}%> +
+ <%end%> <%end%> - <%end%> -
-
-
<%=f.text_field :new_add_font,{:id=>"newfont",:placeholder=>t(:newfont)}%>
-<%=f.submit "#{t(:updatefont)}"%> +
+
+
<%=f.text_field :new_add_font,{:id=>"newfont",:placeholder=>t(:newfont)}%>
+ <%=f.submit "#{t(:updatefont)}",{:id=>"submitbtn"}%> + + <%=f.text_field :initial_btn, {:id=>"hidden-initial",:type => "hidden",:value=>"false"} %> <%end%> -<%@field.font_array%> \ No newline at end of file + + \ No newline at end of file