patch_file/app/controllers/admin/patchfiles_controller.rb

255 lines
9.4 KiB
Ruby
Raw Normal View History

2019-09-28 11:48:59 +00:00
# encoding: utf-8
require 'rubyXL'
class Admin::PatchfilesController < OrbitAdminController
include Admin::PatchfilesHelper
before_action :getfont , :create_first_field
#before_action :load_access_levels
2020-01-13 06:54:42 +00:00
layout "structure"
2019-09-28 11:48:59 +00:00
def initialize
super
@app_title = "patchfile"
end
def repair_module
ModuleApp.all.map{|v| v}.group_by{|v| [v.key,v.title]}.select{|k,v| Array(v).length>1}.each do |k,vs|
array_vs = Array(vs)
cats = array_vs.collect{|v| v.categories}.flatten.uniq
tags = array_vs.collect{|v| v.tags}.flatten.uniq
auths = array_vs.collect{|v| v.authorizations}.flatten.uniq
array_vs.reverse.each_with_index do |v,i|
if i == 0
tags = tags.collect do |tag|
tag.module_app_ids = [v.id]
2020-02-25 03:31:19 +00:00
tag.save!
tag
end
cats = cats.collect do |cat|
cat.module_app_id = v.id
2020-02-25 03:31:19 +00:00
cat.save!
cat
end
auths = auths.collect do |auth|
auth.module_app_id = v.id
2020-02-25 03:31:19 +00:00
auth.save!
auth
end
else
v.tag_ids = []
2020-02-25 03:31:19 +00:00
v.categories = nil
v.authorizations = nil
v.destroy
end
end
end
keys = OrbitWidget.all.map{|w| w.key}.uniq
keep_ids = []
keys.each do |key|
keep_ids << OrbitWidget.where(:key=>key).desc(:id).first.id
end
orbit_widgets = OrbitWidget.all.to_a
orbit_widgets.each{|w| w.destroy if(!keep_ids.include? w.id)}
home_page = Page.where(:url=>'/').asc(:id).first
check_urls = ['/sitemap','/termofuse']
check_urls.each do |check_url|
check_url_ids = Page.where(:url=>check_url,:parent_page_id=>home_page.id).desc(:id).to_a
check_url_ids[1..-1].each{|uniq_page| uniq_page.destroy}
end
2022-10-16 11:55:32 +00:00
render :plain => 'repair finish'
end
2019-09-28 11:48:59 +00:00
def index
#@tags = @module_app.tags
2020-01-13 03:23:34 +00:00
#@categories = @module_app.categories.enabled
#@filter_fields = filter_fields(@categories, @tags)
#@table_fields = [:status, :category, :title, :start_date, :end_date, :last_modified]
2019-09-28 11:48:59 +00:00
if request.xhr?
render :partial => "index"
end
end
def edit
params["filefield"]["file_show"].each do |name,status|
@file_to_show.file_show[name][:status] = status.to_i
end
params["filefield"]["sub_file_fields"].each do |name,files|
files.each do |index,file_and_status|
file_and_status.each do |file_name,status|
@file_to_show["file_show"][name]["files"][file_name]["status"] = status.to_i
end
end
end
if params["filefield"]["refresh_btn"] == "true"
@last_message = @file_to_show.last_message
@active_file_arr = @file_to_show.file_show.values.map{|hash| hash.values.first}.map{|hash| hash.keys.zip(hash.values.map{|hash| hash[:status]}).to_h}.select{|hash| hash.values.include?(1)}.flat_map{|hash| hash.values.map.with_index{|status,i| (status != 1) ? nil : i}.select{|index| !index.nil?}.map{|index| hash.keys[index]}}
@file_to_show.destroy
@file_to_show = Filefield.new()
@file_to_show.last_message = @last_message
@active_file_arr.each{|active_file| @file_to_show.file_show.keys.each{|file_group| (@file_to_show.file_show[file_group][:files].keys.include?(active_file)) ? (@file_to_show.file_show[file_group][:files][active_file][:status] = 1) : nil }}
@file_to_show.save
end
if params["filefield"]["recovery_btn"] == "true"
@file_to_show.recovery = "true"
else
@file_to_show.recovery = "false"
end
@file_to_show.num = 0
@file_to_show.update
@file_to_show = Filefield.where("title"=>@app_title).first
redirect_to admin_patchfiles_path and return
2019-09-28 11:48:59 +00:00
end
def showckeditor
end
def editckeditor
end
def editfontfile
end
def update
if params[:id] == "editfontfile"
@field_name
params["fontfile"]["font_file"] = "" if params["fontfile"]["font_file"].nil? == true
params["fontfile"].each do |update_key,update_value|
if update_key != "font_file"
@font_settings["old_"+update_key] = @font_settings[update_key] rescue nil
@font_settings[update_key] = update_value
else
@file = params["fontfile"]["font_file"] if params["fontfile"]["font_file"] != ""
@file_path = Rails.root.to_s + '/app/assets/fonts'
2022-11-01 13:07:41 +00:00
if !@file.nil?
@file_name = File.basename(@file.original_filename)
@file_content = @file.read
@file_content.force_encoding('UTF-8')
2022-10-16 11:55:32 +00:00
File.open("#{@file_path}/#{@file_name}","w+") do |f|
f.write(@file_content)
end
else
@file_name = @font_settings.font_file_name
end
@field_name = {"font_file_name" => @file_name,"font_dir" => @file_path}
@field_name.each do |name,value|
@font_settings["old_" + name.to_s] = @font_settings[name.to_s]
@font_settings[name.to_s] = value.to_s
end
end
end
@font_settings.update
end
redirect_to admin_patchfiles_showckeditor_path
2019-09-28 11:48:59 +00:00
end
def newfont
if params["patchfilefield"]["initial_btn"] == "true"
#render :html => @field.id and return
@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(";",'').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"] = {} if params["patchfilefield"]["font_array"].nil? == true
params["patchfilefield"]["font_array"].each do |font,istrue|
if istrue =="0"
@field.font_array[font] = false
elsif istrue =="1"
@field.font_array[font] = true
end
end
@field.update
redirect_to admin_patchfiles_showckeditor_path and return
end
2019-09-28 11:48:59 +00:00
end
def getfont
@font_array = []
@ckeditor_path = ENV['PWD'] + '/app/assets/javascripts/ckeditor'
custom_fonts_file = "#{@ckeditor_path}/custom_fonts.txt"
@font_str = ""
if File.exist?(custom_fonts_file)
@font_str = File.read(custom_fonts_file).strip rescue ""
else
@filedata = File.read(@ckeditor_path+"/config.js.erb") rescue return
@font_str = @filedata.scan(/(config.font_names\s*=\s*(\"([^"]*)\"|\'([^']*)\'))/m).map{|a| (a[3] rescue nil)}.compact[-1]
end
@font_array = @font_str.split("\;")
@font_array.map!{|i| i.strip}
@font_array.delete("")
@font_array
end
def create_first_field
@app_title = "patchfile"
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()
@field.title=@app_title
@field.save
@field = Patchfilefield.where("title"=>@app_title).first
else
@field = Patchfilefield.where("title"=>@app_title).first
end
if params[:font] != nil
if Fontfile.where("title"=>params[:font]).first.nil? == true
@font_settings = Fontfile.new
@font_settings.title = params[:font]
@font_settings.save
end
@font_settings = Fontfile.where("title"=>params[:font]).first
end
if Filefield.where("title"=>@app_title).take_while{true}.count == 0
@file_to_show = Filefield.new()
@file_to_show.title=@app_title
@file_to_show.save
@file_to_show = Filefield.where("title"=>@app_title).first
else
@default_field = Filefield.new()
@file_to_show = Filefield.where("title"=>@app_title).first
@default_field.file_show.each do |name,property|
if @file_to_show.file_show[name] == nil
@file_to_show.file_show[name] = property
@file_to_show.save
@file_to_show = Filefield.where("title"=>@app_title).first
end
end
@default_field.file_show.each do |name,property|
property[:files].each do |file_name,sub_property|
if @file_to_show.file_show[name][:files][file_name] == nil
@file_to_show.file_show[name][:files][file_name] = sub_property
@file_to_show.save
@file_to_show = Filefield.where("title"=>@app_title).first
end
end
end
end
end
2019-09-28 11:48:59 +00:00
end