finish the first version of patch_file

This commit is contained in:
chiu committed 2019-09-28 19:48:59 +08:00
commit a2d46ca121
398 files changed
+12910

No files matched your search

View File
Whitespace-only changes.
+89
View File
@@ -0,0 +1,89 @@
# encoding: utf-8
require 'fileutils'
class Filefield
include Mongoid::Document
include Mongoid::Timestamps
field :title, :type=> String ,default:"patchfile"
field :file_show, :type=> Hash ,default:{"ckeditor"=>0,"site-preference(two-site-logo)"=>0,"member"=>0}
after_update :updatefile
def enabled_for?(lang)
if lang.nil?
return true
else
return self.choose_lang.include?(lang)
end
end
def lastpath dir_path
dir_path = dir_path.gsub('\\','/')
dir_arr= dir_path.split('/')
dir_str=''
dir_arr.delete_at(-1)
dir_arr.each_with_index do |path,index|
dir_str = dir_str + path
if index != dir_arr.count - 1
dir_str = dir_str +"/"
end
end
return dir_str
end
def updatefile
self.file_show.each do |key,value|
if value == 1
@model_path = ENV['PWD'] + '/app/models/'
@helper_path = ENV['PWD'] + '/app/helpers/'
@views_path = ENV['PWD'] + '/app/views/'
@controller_path = ENV['PWD'] + '/app/controllers/'
if key == "ckeditor"
app_path = File.expand_path(__dir__)
app_path = lastpath(app_path)
app_path = lastpath(app_path)
@ckeditor_path = ENV['PWD'] + '/app/assets/javascripts/ckeditor/'
puts 'copying CKEDITOR'
@aa = FileUtils.cd(app_path+'/updatefiles/')
puts @aa.to_s
begin
FileUtils.cp('config.js.erb' , @ckeditor_path)
puts @ckeditor_path
rescue
puts 'error copy'
end
elsif key == "site-preference(two-site-logo)"
app_path = File.expand_path(__dir__)
app_path = lastpath(app_path)
app_path = lastpath(app_path)
puts app_path
puts 'copying site'
FileUtils.cd(app_path+'/updatefiles/')
begin
FileUtils.cp('site.rb' , @model_path)
FileUtils.cp('application_helper.rb' , @helper_path)
FileUtils.cd('admin/')
FileUtils.cp('preference.html.erb',@views_path+'/admin/sites/')
rescue
puts 'error copy'
end
elsif key == "member"
app_path = File.expand_path(__dir__)
app_path = lastpath(app_path)
app_path = lastpath(app_path)
FileUtils.cd(app_path+'/updatefiles/')
puts 'copying member'
begin
FileUtils.cp('member_profile.rb' , @model_path)
FileUtils.cd('admin')
FileUtils.cp('members_controller.rb' , @controller_path+'/admin/')
rescue
puts 'error copy'
end
end
FileUtils.cd(Rails.root.to_s)
end
end
end
end
+227
View File
@@ -0,0 +1,227 @@
class Fontfile
include Mongoid::Document
include Mongoid::Timestamps
include OrbitModel::Status
include OrbitModel::Impression
# encoding: utf-8
include OrbitTag::Taggable
include OrbitCategory::Categorizable
field :title ,type:String ,default:""
field :is_delete_font_file, type:String,default: 'false'
field :old_title ,type:String ,default:""
#field :font_file ,type:String ,default:""
field :font_link ,type:String ,default:""
field :old_font_link ,type:String ,default:""
mount_uploader :font_file, AssetUploader
field :font_file_name, type:String ,default:""
field :font_dir, type:String ,default:""
field :old_font_file_name, type:String ,default:""
field :old_font_dir, type:String ,default:""
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)}
#has_many :filefields, :autosave => true, :dependent => :destroy
# accepts_nested_attributes_for :filefields, :allow_destroy => true
# before_create
#before_update :set_old_link#,:set_old_title
#before_save :set_old_title
#before_create :set_expire
after_update :change_font_name,:set_css_link,:change_font_settings
#after_save :change_font_name
def change_font_settings
if self.old_font_file_name == self.font_file_name && self.is_delete_font_file =='true'
self.font_file_name = ""
self.is_delete_font_file = 'false'
self.save
elsif self.is_delete_font_file =='true'
self.is_delete_font_file = 'false'
end
template_path = ENV['PWD'] + '/app/templates'
all_template = Dir.glob(template_path+'/*/')
all_template.each do |folder|
next if !Dir.exist?(folder+"partial/")
if folder.split('/')[-1] != 'mobile'
begin
Dir.chdir(folder+"partial/")
@file_name = "_head.html.erb"
@filetowrite
if !File.file?(@file_name)
@filetowrite = File.new(@file_name,"w")
@filetowrite.close
end
@file_content = File.read(@file_name)
if self.old_font_dir != '' && self.old_font_file_name != '' && self.old_font_file_name != self.font_file_name
if self.old_font_dir != ""
Dir.chdir(self.old_font_dir)
File.delete(self.old_font_file_name) if File.exist?(self.old_font_file_name)
end
Dir.chdir(folder+"partial/")
@font_link_str = "<link href=\'/assets/"+"#{self.old_font_file_name}"+"\' type=\'text/css\' rel=\'stylesheet\'>"
if @file_content.include? @font_link_str
@file_content = @file_content.gsub(@font_link_str,'')
@filetowrite = File.open(@file_name,"w")
@filetowrite.write(@file_content)
@filetowrite.close
end
elsif self.old_font_link != ""&& self.old_font_link != self.font_link
@font_link_str = "<link href=\'"+"#{self.old_font_link}"+"\' rel=\'stylesheet\'>"
if @file_content.include? @font_link_str
@file_content = @file_content.gsub(@font_link_str,'')
@filetowrite = File.open(@file_name,"w")
@filetowrite.write(@file_content)
@filetowrite.close
end
end
rescue
puts folder
puts 'error copy'
end
end
end
Dir.chdir(Rails.root.to_s)
end
def set_css_link
template_path = ENV['PWD'] + '/app/templates'
all_template = Dir.glob(template_path+'/*/')
all_template.each do |folder|
next if !Dir.exist?(folder+"partial")
if folder.split('/')[-1] != 'mobile'
begin
Dir.chdir(folder+"partial/")
@file_name = "_head.html.erb"
@filetowrite
if !File.file?(@file_name)
@filetowrite = File.new(@file_name,"w")
@filetowrite.close
end
@file_content = File.read(@file_name)
@file_content = @file_content.force_encoding('UTF-8').gsub('%20',' ')
if self.font_dir != '' && self.font_file_name != ''
@font_link_str = "<link href=\'/assets/"+"#{self.font_file_name}"+"\' type=\'text/css\' rel=\'stylesheet\'>"
@str_to_search = '/assets/'+ self.font_file_name.to_s
if @file_content.include? @str_to_search
@str_start = @file_content.index(@str_to_search)
@istrue = 0
@str_match = 'href'.reverse
@index = 0
for @i in (0...@str_start.to_i).to_a.reverse
next if @file_content[@i] == ' ' || @file_content[@i] == '=' || @file_content[@i] == '\''
if @file_content[@i] == @str_match[@index]
@index = @index + 1
else
break
end
if @index == @str_match.length
@istrue = 1
break
end
end
if @istrue == 0
open(@file_name, 'a') { |f|
f.puts @font_link_str
}
end
else
open(@file_name, 'a') { |f|
f.puts @font_link_str
}
end
elsif self.font_link != ""
@font_link_str = "<link href=\'"+"#{self.font_link}"+"\' rel=\'stylesheet\'>"
@str_to_search = self.font_link.to_s
if @file_content.include? @str_to_search
@str_start = @file_content.index(@str_to_search)
@istrue = 0
@str_match = 'href'.reverse
@index = 0
for @i in (0...@str_start).to_a.reverse
next if @file_content[@i] == ' ' || @file_content[@i] == '=' || @file_content[@i] == '\''
if @file_content[@i] == @str_match[@index]
@index = @index + 1
else
break
end
if @index == @str_match.length
@istrue = 1
break
end
end
if @istrue == 0
open(@file_name, 'a') { |f|
f.puts @font_link_str
}
end
else
open(@file_name, 'a') { |f|
f.puts @font_link_str
}
end
end
rescue
puts folder
puts 'error copy'
end
end
end
end
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
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)
@field.delete_font = @font_old_name
@field.update
@field = Patchfilefield.where("title"=>"patchfile").first
@field.set_un_used_font
@field.set_used_font
@field.update
end
end
end
def email_members
MemberProfile.find(self.email_member_ids) rescue []
end
def email_addresses
addresses = self.email_members.collect{|member| member.email} rescue []
addresses = addresses +[self.other_mailaddress] if !self.other_mailaddress.blank?
addresses.flatten
end
def email
mail = Email.find(self.email_id) rescue nil
end
def expired?
(self.deadline < Time.now) rescue false
end
def destroy_email
mail = Email.find(self.email_id) rescue nil
mail.destroy if !mail.nil?
end
def self.remove_expired_status
self.where(:is_top => true, :top_end_date.ne => nil, :top_end_date.lt => Time.now).each do |b|
b.is_top = false
b.top_end_date = nil
b.update
end
end
end
+308
View File
@@ -0,0 +1,308 @@
class Patchfilefield
include Mongoid::Document
include Mongoid::Timestamps
include OrbitModel::Status
include OrbitModel::Impression
# encoding: utf-8
include OrbitTag::Taggable
include OrbitCategory::Categorizable
#include Slug
field :font_array , :type => Hash, default:{}
field :new_add_font, type: String, default:""
field :delete_font, type: String, default:""
field :used_font , :type => Array, default:[]
field :un_used_font , :type => Array, default:[]
#field :title, as: :slug_title, type: String, localize: true
field :title ,type:String ,default:""
field :subtitle, localize: true
field :text, localize: true
#field :create_user_id
#field :update_user_id
# field :public, :type => Boolean, :default => true
#field :postdate , :type => DateTime, :default => Time.now
#field :deadline , :type => DateTime
# field :rss2_sn
#field :approved, :type => Boolean, :default => false
#field :is_preview, :type => Boolean, :default => false
#field :expirable_created_at, type: DateTime
# field :rejected, :type => Boolean, :default => false
#field :reapproval, :type => Boolean, :default => false
# field :rejection_reason
#field :is_external_link, :type => Boolean, :default => false
#field :external_link
#field :email_id
#field :email_sent, :type => Boolean, :default => false
# field :email_sentdate , :type => DateTime
# field :email_member_ids
#field :other_mailaddress
# field :image_description, localize: true
# field :top_end_date, :type => DateTime
# mount_uploader :image, ImageUploader
#has_many :bulletin_links, :autosave => true, :dependent => :destroy
# has_many :bulletin_files, :autosave => true, :dependent => :destroy
#accepts_nested_attributes_for :bulletin_files, :allow_destroy => true
# accepts_nested_attributes_for :bulletin_links, :allow_destroy => true
# 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
after_update :set_config
after_save :set_config
#before_create :set_expire
def get_font_to_store
@ckeditor_path = ENV['PWD'] + '/app/assets/javascripts/ckeditor'
@filedata = File.read(@ckeditor_path+"/config.js.erb")
@name_to_search ="config.font_names"
@index_of_font = @filedata.index(@name_to_search)
@font_str = ""
@end_character
if @index_of_font != nil
@inex_start_to_search = @index_of_font + @name_to_search.length
#@str_start = 0 #if @str_start=0 =>not reach the start of font string
@str_start = @inex_start_to_search
if (@filedata[@inex_start_to_search] != "\'" && @filedata[@inex_start_to_search] != "\"")
for @str_start in @inex_start_to_search..@filedata.length
break if (@filedata[@str_start] == "\'" || @filedata[@str_start] == "\"")
end
end
@end_character = @filedata[@str_start]
@str_end = @str_start+1
if @str_start+1 < @filedata.length
@str_end = @str_start+1
for @str_end in @str_start+1..@filedata.length
break if @filedata[@str_end] == @end_character || @str_end == @str_start
if @filedata[@str_end] == "="
@str_end = @str_start
break
end
end
if @str_end != @str_start
@font_str = @filedata[@str_start+1...@str_end]
else
@font_str = ""
end
end
@font_array = @font_str.split("\;")
@font_array.map!{|i| i.strip}
@font_array.delete("")
@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
@i = 1
end
break if @i == 1
end
if @i == 0
@count = self.font_array.count
self.font_array = self.font_array.merge({@count =>{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
@i = 1
end
break if @i == 1
end
if @i == 0
@count = self.font_array.count
self.font_array = self.font_array.merge({@count =>{self.new_add_font.strip => true}})
end
#self.cuurent_used_font push true
self.new_add_font = ""
end
end
# def set_expire
# self.expirable_created_at = Time.now if self.is_preview
# return true
# 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
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
end
end
@use_font.delete("")
self.used_font = @use_font
end
def check_font_state
@ckeditor_path = ENV['PWD'] + '/app/assets/javascripts/ckeditor'
@filedata = File.read(@ckeditor_path+"/config.js.erb")
@name_to_search ="config.font_names"
@index_of_font = @filedata.index(@name_to_search)
@font_str = ""
@end_character
@str_start
@str_end
@font_array
if @index_of_font != nil
@inex_start_to_search = @index_of_font + @name_to_search.length
@str_start = @inex_start_to_search
if (@filedata[@inex_start_to_search] != "\'" && @filedata[@inex_start_to_search] != "\"")
for @str_start in @inex_start_to_search..@filedata.length
break if (@filedata[@str_start] == "\'" || @filedata[@str_start] == "\"")
end
end
@end_character = @filedata[@str_start]
@str_end = @str_start+1
if @str_start+1 < @filedata.length
@str_end = @str_start+1
for @str_end in @str_start+1..@filedata.length
break if @filedata[@str_end] == @end_character || @str_end == @str_start
if @filedata[@str_end] == "="
@str_end = @str_start
break
end
end
if @str_end != @str_start
@font_str = @filedata[@str_start+1...@str_end]
else
@font_str = ""
end
end
@font_array = @font_str.split("\;")
@font_array.map!{|i| i.strip}
@font_array.delete("")
if !check_eql_array(@font_array,self.used_font)
return false
else
return true
end
end
end
def set_config
@istrue = check_font_state
if @istrue == false
@delete_font_array = []
self.un_used_font.each do |font_to_delete|
if @font_array.include? font_to_delete.strip
@delete_font_array.push font_to_delete.strip
end
end
@delete_font_array.push(self.delete_font) if self.delete_font != ""
self.delete_font = ""
puts "111111111111111111111111111111111111111111111111"
puts @delete_font_array
@add_font_array = []
@use_font = self.used_font rescue []
@use_font.each do |font_to_add|
if !(@font_array.include? font_to_add.strip)
@add_font_array.push font_to_add.strip
end
end
if @add_font_array != [] || @delete_font_array != []
@ckeditor_path = ENV['PWD'] + '/app/assets/javascripts/ckeditor'
Dir.chdir(@ckeditor_path)
if !File.file?("config_back.js.erb")
@filetowrite = File.new("config_back.js.erb","w")
if @add_font_array != []
@add_font_array.each do |new_add_font|
@filedata = @filedata.insert(@str_end,"\;"+new_add_font)
end
end
@delete_font_array.each do |font_to_delete|
@str_above_end = @filedata.index(font_to_delete) - 1
@str_above_start
for @str_above_start in (0..@str_above_end).to_a.reverse
if @filedata[@str_above_start] =="\'" || @filedata[@str_above_start] =="\""
break
end
break if @filedata[@str_above_start] == "\;"
end
@str_to_delete = (( @filedata[@str_above_start..@str_above_end].include? "\;") ? @filedata[@str_above_start..@str_above_end] : "")+font_to_delete
@filedata =@filedata.sub(@str_to_delete,"")
for @i in @str_start+1...@str_end
next if @filedata[@i] == " "
break if @filedata[@i] != "\;"
@filedata.slice!(@i)
end
end
puts self.used_font
puts @filedata
#render :html => @delete_font_array.to_s+" "+ @filedata and return
@filetowrite.write(@filedata)
@filetowrite.close
@filetocover = File.open("config.js.erb","w")
@filetocover.write(@filedata)
@filetocover.close
File.delete("config_back.js.erb")
else
@filedata1 = File.read("config.js.erb")
@filedata2 = File.read("config_back.js.erb")
if @filedata1.length < @filedata2.length
@filetocover = File.open("config.js.erb","w")
@filetocover.write(@filedata2)
@filetocover.close
end
File.delete("config_back.js.erb")
end
Dir.chdir(Rails.root.to_s)
end
end
end
def check_eql_array(array1,array2)
if array1.count == array2.count
array1.each do |item|
if !(array2.include? item)
return false
end
end
return true
else
return false
end
end
def update_user
User.find(update_user_id) rescue nil
end
def update_user=(user)
self.update_user_id = user.id
end
def email_members
MemberProfile.find(self.email_member_ids) rescue []
end
def email_addresses
addresses = self.email_members.collect{|member| member.email} rescue []
addresses = addresses +[self.other_mailaddress] if !self.other_mailaddress.blank?
addresses.flatten
end
def email
mail = Email.find(self.email_id) rescue nil
end
def expired?
(self.deadline < Time.now) rescue false
end
def destroy_email
mail = Email.find(self.email_id) rescue nil
mail.destroy if !mail.nil?
end
def self.remove_expired_status
self.where(:is_top => true, :top_end_date.ne => nil, :top_end_date.lt => Time.now).each do |b|
b.is_top = false
b.top_end_date = nil
b.save
end
end
end