2015-11-13 13:10:00 +00:00
|
|
|
class Admin::UniversalTablesController < OrbitAdminController
|
|
|
|
def index
|
2015-12-17 13:58:04 +00:00
|
|
|
@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
|
2024-06-11 14:45:53 +00:00
|
|
|
|
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
|
2024-06-11 14:45:53 +00:00
|
|
|
|
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}
|
2015-11-18 15:51:49 +00:00
|
|
|
if params[:q].present?
|
2022-02-24 09:31:45 +00:00
|
|
|
@entries = search_data(@table)
|
2015-11-18 15:51:49 +00:00
|
|
|
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)
|
2015-11-18 15:51:49 +00:00
|
|
|
end
|
2015-11-13 13:10:00 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2025-06-13 10:04:44 +00:00
|
|
|
def get_entries
|
|
|
|
table = UTable.where(:uid => params["uid"]).first rescue nil
|
|
|
|
data = []
|
|
|
|
|
|
|
|
if table && params[:q].present?
|
|
|
|
entries = search_data(table, 50)
|
|
|
|
ma = ModuleApp.find_by_key("universal_table")
|
|
|
|
|
|
|
|
entries.each do |entry|
|
|
|
|
rows = []
|
|
|
|
entry.column_entries.each do |ce|
|
|
|
|
ct = ce.table_column
|
|
|
|
next if ct.nil?
|
|
|
|
next if ct.display_in_index == false
|
|
|
|
|
|
|
|
text = ce.get_frontend_text(ct)
|
|
|
|
next if text.blank?
|
|
|
|
|
|
|
|
# 包含連結的欄位處理
|
|
|
|
url = ct.is_link_to_show ? OrbitHelper.cal_url_to_show(ma, entry) : nil
|
|
|
|
rows << {
|
|
|
|
"title" => ct.title,
|
|
|
|
"text" => text,
|
|
|
|
"url" => url
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
# 加入 hashtags 欄位
|
|
|
|
# rows << {
|
|
|
|
# "title" => I18n.t("universal_table.hashtags"),
|
|
|
|
# "text" => entry.tags_for_frontend,
|
|
|
|
# "url" => nil
|
|
|
|
# }
|
|
|
|
|
|
|
|
# 加入主輸出結構
|
|
|
|
data << {
|
|
|
|
"id" => entry.id.to_s,
|
|
|
|
"link" => OrbitHelper.cal_url_to_show(ma, entry),
|
|
|
|
"text" => entry.column_entries.map(&:text).compact.reject(&:blank?).join(" / "),
|
|
|
|
"fields" => rows
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
render json: data
|
|
|
|
end
|
|
|
|
|
2025-05-13 13:05:19 +00:00
|
|
|
|
2025-06-10 16:10:57 +00:00
|
|
|
def get_mindmaps
|
|
|
|
utable = UTable.where(:uid => params['table']).first
|
|
|
|
mindmaps = utable.mind_maps.map do |m|
|
|
|
|
{
|
|
|
|
"title" => m.title,
|
|
|
|
"id" => m.id.to_s
|
|
|
|
}
|
|
|
|
end
|
|
|
|
render :json => mindmaps.to_json
|
|
|
|
end
|
|
|
|
|
2015-12-17 13:58:04 +00:00
|
|
|
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
|
|
|
|
|
2025-06-30 06:30:13 +00:00
|
|
|
def import_data_from_excel
|
|
|
|
site_in_use_locales = @site_in_use_locales.sort
|
|
|
|
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?
|
|
|
|
render json: { success: false, msg: "Table not found." }.to_json and return
|
|
|
|
end
|
|
|
|
|
|
|
|
sheet = workbook[0]
|
|
|
|
if sheet.count > 503
|
|
|
|
render json: { success: false, msg: "More than 500 entries. Please split the entries in different files." }.to_json and return
|
|
|
|
end
|
|
|
|
|
|
|
|
# 前三列是欄位名、key、格式描述
|
|
|
|
column_titles = sheet[0].cells.map { |c| c&.value.to_s.strip }
|
|
|
|
column_keys = sheet[1].cells.map { |c| c&.value.to_s.strip }
|
|
|
|
column_types = sheet[2].cells.map { |c| c&.value.to_s.strip }
|
|
|
|
|
|
|
|
# 準備欄位對應
|
|
|
|
columns = column_keys.uniq.map.with_index do |key, i|
|
|
|
|
tc = table.table_columns.where(key: key).first
|
|
|
|
[i, tc]
|
|
|
|
end.to_h
|
|
|
|
|
|
|
|
sheet.each_with_index do |row, i|
|
|
|
|
next if i < 3 || row.cells.compact.map { |c| c.value.to_s.strip }.all?(&:blank?)
|
|
|
|
uid_val = row[0]&.value.to_s.strip rescue nil
|
|
|
|
te = uid_val.present? ?
|
|
|
|
TableEntry.where(uid: uid_val, u_table_id: table.id).first_or_initialize :
|
|
|
|
TableEntry.new
|
|
|
|
te.u_table = table
|
|
|
|
skip = 0
|
|
|
|
|
|
|
|
tc_idx = 0
|
|
|
|
|
|
|
|
row.cells.each_with_index do |cell, col_idx|
|
|
|
|
next if skip > 0 && (skip -= 1) >= 0
|
|
|
|
|
|
|
|
val = cell&.value
|
|
|
|
tc = columns[tc_idx]
|
|
|
|
tc_idx += 1
|
|
|
|
next if tc.nil?
|
|
|
|
|
|
|
|
ce = te.column_entries.where(table_column_id: tc.id).first
|
|
|
|
ce = ColumnEntry.new(table_column_id: tc.id) if ce.nil?
|
|
|
|
case tc.type
|
|
|
|
when "text", "editor"
|
|
|
|
v = {}
|
|
|
|
site_in_use_locales.each_with_index do |locale, offset|
|
|
|
|
v[locale.to_s] = row[col_idx + offset]&.value.to_s rescue ""
|
|
|
|
end
|
|
|
|
skip = site_in_use_locales.size - 1
|
|
|
|
if tc.type == "text"
|
|
|
|
ce.text_translations = v
|
|
|
|
else
|
|
|
|
ce.content_translations = v
|
|
|
|
end
|
|
|
|
|
|
|
|
when "integer"
|
|
|
|
ce.number = val.present? ? val.to_i : nil
|
|
|
|
|
|
|
|
when "image"
|
|
|
|
ce.remote_image_url = val if val.present?
|
|
|
|
when "file"
|
|
|
|
file_urls = val.to_s.split(";").map(&:strip)
|
|
|
|
file_titles = row[col_idx + 1]&.value.to_s.split(";").map(&:strip)
|
|
|
|
skip = 1
|
|
|
|
|
|
|
|
ce.column_entry_files.destroy_all
|
|
|
|
ce.column_entry_files = []
|
|
|
|
|
|
|
|
file_urls.each_with_index do |remote_url, file_idx|
|
|
|
|
next if remote_url.blank?
|
|
|
|
file = ColumnEntryFile.new
|
|
|
|
file.remote_file_url = remote_url
|
|
|
|
|
|
|
|
# 處理多語言標題
|
|
|
|
titles = {}
|
|
|
|
site_in_use_locales.each do |locale|
|
|
|
|
titles[locale.to_s] = file_titles[file_idx] rescue file.file.file.filename
|
|
|
|
end
|
|
|
|
file.file_title_translations = titles
|
|
|
|
file.save!
|
|
|
|
ce.column_entry_files << file
|
|
|
|
end
|
|
|
|
when "date"
|
|
|
|
ce.date = val
|
|
|
|
|
|
|
|
when "period"
|
|
|
|
skip = 1
|
|
|
|
ce.period_from = val
|
|
|
|
ce.period_to = row[col_idx + 1]&.value rescue nil
|
|
|
|
end
|
|
|
|
|
|
|
|
ce.save!
|
|
|
|
te.column_entries << ce
|
|
|
|
end
|
|
|
|
|
|
|
|
# hashtags (倒數第2欄)
|
|
|
|
if row.cells.count >= 2
|
|
|
|
tags_text = row.cells[-2]&.value.to_s rescue ""
|
|
|
|
create_get_table_tags(te, tags_text.split(";"))
|
|
|
|
end
|
|
|
|
|
|
|
|
# related_entries (倒數第1欄)
|
|
|
|
if row.cells.count >= 1
|
|
|
|
related_uids = row.cells[-1]&.value.to_s.split(";").map(&:strip)
|
|
|
|
related_ids = TableEntry.where(:uid.in => related_uids).pluck(:id)
|
|
|
|
te.related_entries = related_ids.join(",")
|
|
|
|
end
|
|
|
|
|
|
|
|
te.save!
|
|
|
|
te.fix_have_data
|
|
|
|
te.uid = uid_val if uid_val.present?
|
|
|
|
te.save!
|
|
|
|
end
|
|
|
|
|
|
|
|
render json: {
|
|
|
|
success: true,
|
|
|
|
count: table.table_entries.count,
|
|
|
|
id: table.id.to_s
|
|
|
|
}.to_json
|
|
|
|
end
|
2015-12-17 13:58:04 +00:00
|
|
|
|
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)
|
2025-06-16 12:08:15 +00:00
|
|
|
create_get_table_tags(entry)
|
2015-11-13 13:10:00 +00:00
|
|
|
entry.save
|
2024-08-11 02:15:35 +00:00
|
|
|
entry.fix_have_data
|
2015-11-13 13:10:00 +00:00
|
|
|
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
|
2024-08-11 02:16:31 +00:00
|
|
|
redirect_to (request.referrer || admin_universal_table_path(table))
|
2015-11-13 13:10:00 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def update_entry
|
|
|
|
entry = TableEntry.find(params[:id])
|
2025-05-28 14:12:51 +00:00
|
|
|
create_get_table_tags(entry)
|
2015-11-13 13:10:00 +00:00
|
|
|
entry.update_attributes(table_entry_params)
|
2024-08-11 02:15:35 +00:00
|
|
|
entry.fix_have_data # when new column insert
|
2015-11-13 13:10:00 +00:00
|
|
|
table = entry.u_table
|
2024-08-11 02:16:31 +00:00
|
|
|
redirect_to admin_universal_table_path(:id=> table.to_param, :page => params[:page])
|
2015-11-13 13:10:00 +00:00
|
|
|
end
|
|
|
|
|
2025-06-16 12:08:15 +00:00
|
|
|
def toggle_entries
|
|
|
|
ids = params[:ids]
|
|
|
|
hidden = params[:status] == "hide"
|
|
|
|
TableEntry.where(:id.in => ids).update_all(is_hidden: hidden)
|
|
|
|
render :json => {"success" => true}.to_json
|
|
|
|
end
|
|
|
|
|
2015-11-13 13:10:00 +00:00
|
|
|
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
|
2016-01-27 05:08:32 +00:00
|
|
|
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
|
|
|
|
|
2025-05-13 13:05:19 +00:00
|
|
|
def search_data(table, per=10)
|
2015-11-18 15:51:49 +00:00
|
|
|
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
|
2024-06-11 14:45:53 +00:00
|
|
|
columns = columns.uniq{|col| col.table_entry_id}
|
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)
|
2025-06-16 12:08:15 +00:00
|
|
|
entries = TableEntry.where(:u_table_id=>table.id).can_display.sorting(params: params,table: table,column_entries: columns,page_num: params[:page],per: per)
|
2015-11-18 15:51:49 +00:00
|
|
|
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
|
2025-05-28 14:12:51 +00:00
|
|
|
|
2025-06-19 13:49:27 +00:00
|
|
|
def create_get_table_tags(entry,new_tags=nil)
|
|
|
|
if new_tags.nil?
|
|
|
|
new_tags = params["table_tags"].split(",")
|
|
|
|
end
|
2025-05-28 14:12:51 +00:00
|
|
|
tags = []
|
|
|
|
entry.table_tags = []
|
|
|
|
new_tags.each do |tag|
|
|
|
|
if is_uuid?(tag) === false
|
|
|
|
tt = TableTag.new
|
2025-06-03 14:41:03 +00:00
|
|
|
tt.u_table_id = entry.u_table.id
|
2025-06-19 12:12:54 +00:00
|
|
|
tt.title = tag.downcase.strip
|
2025-05-28 14:12:51 +00:00
|
|
|
tt.save
|
|
|
|
entry.table_tags << tt
|
|
|
|
else
|
|
|
|
tt = TableTag.find(tag)
|
|
|
|
entry.table_tags << tt
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return tags
|
|
|
|
end
|
|
|
|
def is_uuid?(str)
|
|
|
|
!!(str =~ /\A[\da-f]{24}\z/i || str =~ /\A[\da-f]{8}-([\da-f]{4}-){3}[\da-f]{12}\z/i)
|
|
|
|
end
|
2025-06-30 06:30:13 +00:00
|
|
|
end
|