universal_table/app/controllers/admin/universal_tables_controller.rb

293 lines
9.0 KiB
Ruby
Raw Normal View History

2015-11-13 13:10:00 +00:00
class Admin::UniversalTablesController < OrbitAdminController
def index
@table_fields = ["universal_table.table_name","universal_table.created_time","universal_table.total_no_of_entries", "universal_table.import_from_excel"]
2015-11-13 13:10:00 +00:00
2022-02-25 06:49:00 +00:00
@tables = UTable.where(:title.ne => "")
.order_by(sort)
.with_categories(filters("category"))
2015-11-13 13:10:00 +00:00
end
2022-02-25 06:49:00 +00:00
def update_sort
uid = params[:universal_table_id].split("-").last
@table = UTable.where(:uid => uid).first rescue nil
if !@table.nil?
ids = params[:ids]
ids = ids.reverse if @table.sort_number_order_direction=='desc'
ids.each_with_index do |id,i|
TableEntry.where(id: id).update(sort_number: i)
end
@entries = TableEntry.where(u_table_id: @table.id).sorting(params: params,table: @table)
@columns = @table.table_columns.asc(:order)
@table_fields = ['universal_table.sort_number']+@columns.collect{|tc| tc.title} + ['universal_table.created_at']
render 'update_sort',layout: false
end
end
2015-11-13 13:10:00 +00:00
2022-02-25 06:49:00 +00:00
def edit_sort
uid = params[:universal_table_id].split("-").last
@table = UTable.where(:uid => uid).first rescue nil
if !@table.nil?
@entries = TableEntry.where(u_table_id: @table.id).sorting(params: params,table: @table)
@columns = @table.table_columns.asc(:order)
@table_fields = ['universal_table.sort_number']+@columns.collect{|tc| tc.title} + ['universal_table.created_at']
end
end
2015-11-13 13:10:00 +00:00
def show
uid = params[:id].split("-").last
@table = UTable.where(:uid => uid).first rescue nil
if !@table.nil?
@columns = @table.table_columns.asc(:order)
@table_fields = @columns.collect{|tc| tc.title}
if params[:q].present?
2022-02-24 09:31:45 +00:00
@entries = search_data(@table)
else
2022-02-25 06:49:00 +00:00
@entries = TableEntry.where(u_table_id: @table.id).sorting(params: params,table: @table,page_num: params[:page],per: 10)
end
2015-11-13 13:10:00 +00:00
end
end
def export_structure
uid = params[:universal_table_id].split("-").last
@table = UTable.where(:uid => uid).first rescue nil
respond_to do |format|
format.xlsx {
response.headers['Content-Disposition'] = "attachment; filename=#{@table.title.downcase.underscore}.xlsx"
}
end
end
2017-04-13 10:50:32 +00:00
def checkforthread
running = !File.exists?("public/uploads/utable_export/#{params[:utable_id]}/#{params[:utable_title]}.xlsx")
render :json => {"status" => running}.to_json
end
def export_data
I18n.locale = :zh_tw
table = UTable.find(params[:id])
title = table.title.gsub(/[ "'*@#$%^&()+=;:.,?>|\\\/<~_!:,、。!?;「」〈〉【】/]/,'')
f = "public/uploads/utable_export/#{table.id}/#{title}.xlsx"
File.delete(f) if File.exists?(f)
url = request.host_with_port
Thread.new do
system("rake universal_table_tasks:prepare_download[#{table.id},#{url}] >> #{Rails.root}/log/rake.log &")
end
render :json => {"success" => true, "title" => title}.to_json
end
def import_data_from_excel
workbook = RubyXL::Parser.parse(params["import_data"].tempfile)
response = {}
current_locale = I18n.locale
table = UTable.find(params["universal_table_id"]) rescue nil
if !table.nil?
sheet = workbook[0]
if sheet.count <= 503
2022-04-01 08:38:50 +00:00
columns = sheet[1].cells.collect.with_index{|c,i|
c.value.blank? ? table.table_columns.where(:title => sheet[0].cells[i].value.to_s.split("-").first.strip).first : table.table_columns.where(:key => c.value.to_s).first
}
languages = sheet[2].cells.collect{|c|
c.value.split("-").last rescue nil
}
sheet.each_with_index do |row, i|
next if i < 3
te = TableEntry.new
te.u_table = table
skip = 0
row.cells.each_with_index do |cell,index|
if skip > 1
skip = skip - 1
next
end
skip = 0
val = cell.value rescue nil
tc = columns[index]
if !tc.nil?
ce = ColumnEntry.new
case tc.type
when "text"
v = {}
2015-12-18 07:33:43 +00:00
@site_in_use_locales.sort.each_with_index do |locale,x|
v[locale.to_s] = row.cells[index + x].value rescue nil
skip = skip + 1
end
ce.text_translations = v
2022-02-25 06:49:00 +00:00
when "integer"
ce.number = (val.blank? ? nil : val)
when "editor"
v = {}
2015-12-18 07:33:43 +00:00
@site_in_use_locales.sort.each_with_index do |locale,x|
v[locale.to_s] = row.cells[index + x].value rescue nil
skip = skip + 1
end
ce.content_translations = v
when "date"
ce.date = val
when "period"
if tc.date_format == "yyyy" && !val.nil?
val = val.to_s + "/01/01"
end
skip = 2
ce.period_from = val
val = row.cells[index + 1].value rescue nil
if tc.date_format == "yyyy" && !val.nil?
val = val.to_s + "/01/01"
end
ce.period_to = val
end
ce.table_column_id = tc.id
ce.save
te.column_entries << ce
end
end
te.save
end
response["success"] = true
response["count"] = table.table_entries.count
response["id"] = table.id.to_s
else
response["success"] = false
response["msg"] = "More than 500 entries. Please split the entries in different files."
end
else
response["success"] = false
response["msg"] = "Table not found."
end
render :json => response.to_json
end
2015-11-13 13:10:00 +00:00
def new_entry
uid = params[:universal_table_id].split("-").last
@table = UTable.where(:uid => uid).first rescue nil
if !@table.nil?
@columns = @table.table_columns.asc(:order)
@entries = @table.table_entries
@entry = TableEntry.new
end
end
def add_entry
entry = TableEntry.new(table_entry_params)
entry.save
table = UTable.find(params[:table_entry][:u_table_id])
redirect_to admin_universal_table_new_entry_path(table)
end
def edit_entry
2015-11-16 13:47:27 +00:00
id = params[:universal_table_id].split("-").last
@entry = TableEntry.where(:uid => id).first
2015-11-13 13:10:00 +00:00
@table = @entry.u_table
if !@table.nil?
@columns = @table.table_columns.asc(:order)
@entries = @table.table_entries
end
end
def delete_entry
entry = TableEntry.find(params[:universal_table_id])
table = entry.u_table
entry.destroy
redirect_to admin_universal_table_path(table)
end
def update_entry
entry = TableEntry.find(params[:id])
entry.update_attributes(table_entry_params)
table = entry.u_table
redirect_to admin_universal_table_path(table)
end
def new
@table = UTable.new
end
def create
category = Category.new
2016-02-16 06:53:28 +00:00
title_for_category = params[:u_table][:title_translations]
if title_for_category["en"] == "" && title_for_category["zh_tw"] != ""
title_for_category["en"] = title_for_category["zh_tw"]
elsif title_for_category["zh_tw"] == "" && title_for_category["en"] != ""
title_for_category["zh_tw"] = title_for_category["en"]
end
category.title_translations = title_for_category
2015-11-13 13:10:00 +00:00
category.module_app = @module_app
category.save
p = table_params
p[:category_id] = category.id
table = UTable.new(p)
table.save
redirect_to admin_universal_tables_path
end
def edit
uid = params[:id].split("-").last
@table = UTable.where(:uid => uid).first
end
def update
uid = params[:id].split("-").last
p = table_params
table = UTable.where(:uid => uid).first
2016-01-15 07:01:07 +00:00
cat = table.category rescue nil
if cat.nil?
cat = Category.new
cat.module_app = @module_app
end
2016-02-16 06:53:28 +00:00
title_for_category = p[:title_translations]
if title_for_category["en"] == "" && title_for_category["zh_tw"] != ""
title_for_category["en"] = title_for_category["zh_tw"]
elsif title_for_category["zh_tw"] == "" && title_for_category["en"] != ""
title_for_category["zh_tw"] = title_for_category["en"]
end
cat.title_translations = title_for_category
2015-11-13 13:10:00 +00:00
cat.save
2016-01-15 07:11:39 +00:00
p[:category_id] = cat.id
table.update_attributes(p)
table.save
2015-11-13 13:10:00 +00:00
redirect_to admin_universal_tables_path
end
def destroy
uid = params[:id].split("-").last
table = UTable.where(:uid => uid).first
table.category.destroy if !table.category.nil?
2015-11-13 13:10:00 +00:00
table.destroy
redirect_to admin_universal_tables_path
end
private
def search_data(table)
keywords = params["q"]
keywords = keywords.strip.nil? ? keywords : keywords.strip
regexes = keywords.split(/\s+(?=(?:[^"]*"[^"]*")*[^"]*$)/)
regex = Regexp.union(regexes.map{|word| Regexp.new(".*"+word+".*", "i")})
column = table.table_columns.where(:type.in => ["text","editor"])
columns = []
column.each do |c|
columns = (columns | c.column_entries.any_of(:"text.en" => regex).or(:"text.zh_tw" => regex).or(:"content.en" => regex).or(:"content.zh_tw" => regex))
end
2022-03-05 13:41:58 +00:00
columns_count = columns.count
columns_count = 1 if columns_count==0
columns = Kaminari.paginate_array(columns,limit: columns_count)
2022-02-24 11:34:53 +00:00
entries = TableEntry.where(:u_table_id=>table.id).sorting(params: params,table: table,column_entries: columns,page_num: params[:page],per: 10)
end
2015-11-13 13:10:00 +00:00
def table_params
params.require(:u_table).permit!
end
def table_entry_params
params.require(:table_entry).permit!
end
end