patch_file/app/models/patchfilefield.rb

235 lines
6.8 KiB
Ruby
Raw Normal View History

2019-09-28 11:48:59 +00:00
class Patchfilefield
2022-10-16 11:56:14 +00:00
include Mongoid::Document
include Mongoid::Timestamps
include OrbitModel::Status
include OrbitModel::Impression
# encoding: utf-8
include OrbitTag::Taggable
include OrbitCategory::Categorizable
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 ,type:String ,default:""
field :subtitle, localize: true
field :text, localize: true
2019-09-28 11:48:59 +00:00
# before_destroy :destroy_email
2022-10-16 11:56:14 +00:00
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 :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_save :set_config
#before_create :set_expire
def delete_empty_font
self.font_array.each do |font,istrue|
if font.strip == ""
self.font_array.delete(font)
end
end
end
2022-10-16 11:56:14 +00:00
def get_font_to_store
@ckeditor_path = ENV['PWD'] + '/app/assets/javascripts/ckeditor'
@filedata = File.read(@ckeditor_path+"/config.js.erb") rescue return
@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 = @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
2019-09-28 11:48:59 +00:00
end
2022-10-16 11:56:14 +00:00
@end_character = @filedata[@str_start]
2019-09-28 11:48:59 +00:00
@str_end = @str_start+1
2022-10-16 11:56:14 +00:00
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 = ""
2019-09-28 11:48:59 +00:00
end
end
2022-10-16 11:56:14 +00:00
@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.keys.each do |each_font|
if each_font == font
@i = 1
end
break if @i == 1
end
if @i == 0
self.font_array = self.font_array.merge({font => true})
end
end
2019-09-28 11:48:59 +00:00
end
end
2022-10-16 11:56:14 +00:00
if self.new_add_font.strip !=""
2019-09-28 11:48:59 +00:00
@i = 0
2022-10-16 11:56:14 +00:00
self.font_array.keys.each do |each_font|
if each_font == self.new_add_font
@i = 1
2019-09-28 11:48:59 +00:00
end
2022-10-16 11:56:14 +00:00
break if @i == 1
2019-09-28 11:48:59 +00:00
end
2022-10-16 11:56:14 +00:00
if @i == 0
self.font_array = self.font_array.merge({self.new_add_font.strip => true})
2019-09-28 11:48:59 +00:00
end
2022-10-16 11:56:14 +00:00
self.new_add_font = ""
2019-09-28 11:48:59 +00:00
end
end
2022-10-16 11:56:14 +00:00
def set_un_used_font
@un_use_font = []
self.font_array.each do |font,istrue|
if istrue == false
@un_use_font.push font.strip
end
2019-09-28 11:48:59 +00:00
end
2022-10-16 11:56:14 +00:00
self.un_used_font = @un_use_font
2019-09-28 11:48:59 +00:00
end
2022-10-16 11:56:14 +00:00
def set_used_font
@use_font = []
self.font_array.each do |font,istrue|
if istrue == true
@use_font.push font.strip
end
2019-09-28 11:48:59 +00:00
end
2022-10-16 11:56:14 +00:00
@use_font.delete("")
self.used_font = @use_font
2019-09-28 11:48:59 +00:00
end
2022-10-16 11:56:14 +00:00
def check_font_state
@ckeditor_path = ENV['PWD'] + '/app/assets/javascripts/ckeditor'
@filedata = File.read(@ckeditor_path+"/config.js.erb") rescue return
@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
2019-09-28 11:48:59 +00:00
end
2022-10-16 11:56:14 +00:00
@end_character = @filedata[@str_start]
2019-09-28 11:48:59 +00:00
@str_end = @str_start+1
2022-10-16 11:56:14 +00:00
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 = ""
2019-09-28 11:48:59 +00:00
end
end
2022-10-16 11:56:14 +00:00
@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
2019-09-28 11:48:59 +00:00
else
2022-10-16 11:56:14 +00:00
return true
2019-09-28 11:48:59 +00:00
end
end
end
2022-10-16 11:56:14 +00:00
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
2019-09-28 11:48:59 +00:00
end
2022-10-16 11:56:14 +00:00
@delete_font_array.push(self.delete_font) if self.delete_font != ""
self.delete_font = ""
@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
2019-09-28 11:48:59 +00:00
end
2022-10-16 11:56:14 +00:00
end
if @add_font_array != [] || @delete_font_array != []
@ckeditor_path = ENV['PWD'] + '/app/assets/javascripts/ckeditor'
Dir.chdir(@ckeditor_path) do
File.open("custom_fonts.txt","w+") do |f|
f.write(self.used_font.join(";"))
end
puts "writing........."
File.open("config.js.erb","a+") do |f|
f.puts ""
end
2019-09-28 11:48:59 +00:00
end
end
end
end
2022-10-16 11:56:14 +00:00
def check_eql_array(array1,array2)
if array1.count == array2.count
array1.each do |item|
if !(array2.include? item)
return false
end
2019-09-28 11:48:59 +00:00
end
2022-10-16 11:56:14 +00:00
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
2019-09-28 11:48:59 +00:00
end
end
end