2015-11-13 13:10:00 +00:00
|
|
|
class UniversalTablesController < ApplicationController
|
2015-11-18 15:51:49 +00:00
|
|
|
include Admin::UniversalTablesHelper
|
2015-11-13 13:10:00 +00:00
|
|
|
def index
|
|
|
|
params = OrbitHelper.params
|
|
|
|
table = UTable.where(:category_id => OrbitHelper.page_categories.first).first rescue nil
|
2024-08-11 02:17:53 +00:00
|
|
|
page = OrbitHelper.page rescue Page.where(:page_id => params[:page_id]).first
|
|
|
|
searchable_columns = []
|
2015-11-13 13:10:00 +00:00
|
|
|
if !table.nil?
|
2015-11-18 15:51:49 +00:00
|
|
|
reset = "hide"
|
2020-04-29 05:37:14 +00:00
|
|
|
csrf_value = (0...46).map { ('a'..'z').to_a[rand(26)] }.join
|
2024-08-08 15:53:10 +00:00
|
|
|
params_q = params["q"].to_h.each{|_, v| v.gsub!("\"", '')} rescue {}
|
2020-04-29 07:59:52 +00:00
|
|
|
params_no = params["page_no"].to_s.gsub("\"",'')
|
2024-08-08 15:53:10 +00:00
|
|
|
if params_q.present?
|
|
|
|
query_string = '&' + params_q.map{|k, v| ["q[#{k}]",v]}.to_h.to_query
|
|
|
|
else
|
|
|
|
query_string = ''
|
|
|
|
end
|
|
|
|
query_string += "&page_no=#{params_no}" if params_no.present?
|
|
|
|
csrf_input = "<input type=\"hidden\" name=\"authenticity_token\" value=\"#{csrf_value}\">"
|
2015-11-13 13:10:00 +00:00
|
|
|
table_heads = table.table_columns.where(:display_in_index => true).asc(:order).collect do |tc|
|
2024-08-08 15:53:10 +00:00
|
|
|
field_key = tc.key
|
|
|
|
field_value = params_q[field_key]
|
|
|
|
field_value = nil if field_value.blank?
|
2015-11-13 13:10:00 +00:00
|
|
|
search = ""
|
2015-11-18 15:51:49 +00:00
|
|
|
sort_class = "sort"
|
|
|
|
sort = ""
|
2024-08-08 15:53:10 +00:00
|
|
|
form_field = "#{csrf_input}<input type=\"search\" class=\"form-control\" name=\"q[#{field_key}]\" value=\"#{field_value}\" placeholder=\"Search keyword\">"
|
|
|
|
sort_url = "/#{I18n.locale.to_s}#{page.url}?sortcolumn=#{field_key}&sort=asc#{query_string}"
|
2015-11-13 13:10:00 +00:00
|
|
|
title_class = ""
|
|
|
|
case tc.type
|
2015-11-18 15:51:49 +00:00
|
|
|
when "date"
|
2015-11-13 13:10:00 +00:00
|
|
|
search = "hide"
|
2015-11-18 15:51:49 +00:00
|
|
|
when "period","image"
|
|
|
|
sort_class = "sort hide"
|
|
|
|
search = "hide"
|
|
|
|
sort_url = "#"
|
|
|
|
sort = "hide"
|
|
|
|
when "editor"
|
|
|
|
sort_url = "#"
|
|
|
|
sort = "hide"
|
|
|
|
sort_class = "sort hide"
|
|
|
|
when "text"
|
|
|
|
if tc.make_categorizable
|
2022-03-08 14:38:23 +00:00
|
|
|
select_values = tc.column_entries.distinct("text.#{I18n.locale.to_s}")
|
2024-08-08 15:53:10 +00:00
|
|
|
form_field = "#{csrf_input}<select class=\"form-control\" name=\"q[#{field_key}]\">"
|
2015-11-18 15:51:49 +00:00
|
|
|
select_values.each do |sv|
|
2024-08-08 15:53:10 +00:00
|
|
|
if field_value && sv == field_value
|
|
|
|
selected = " selected"
|
|
|
|
else
|
|
|
|
selected = ""
|
|
|
|
end
|
|
|
|
form_field += "<option value=\"#{sv}\"#{selected}>#{sv}</option>"
|
2015-11-18 15:51:49 +00:00
|
|
|
end
|
2024-08-08 15:53:10 +00:00
|
|
|
form_field += "</select>"
|
2015-11-18 15:51:49 +00:00
|
|
|
end
|
2022-02-25 06:49:00 +00:00
|
|
|
when "integer"
|
|
|
|
if tc.make_categorizable
|
|
|
|
select_values = tc.column_entries.distinct(:number)
|
2024-08-08 15:53:10 +00:00
|
|
|
form_field = "#{csrf_input}<select class=\"form-control\" name=\"q[#{field_key}]\">"
|
2022-02-25 06:49:00 +00:00
|
|
|
select_values.each do |sv|
|
2024-08-08 15:53:10 +00:00
|
|
|
if field_value && sv == field_value
|
|
|
|
selected = " selected"
|
|
|
|
else
|
|
|
|
selected = ""
|
|
|
|
end
|
|
|
|
form_field += "<option value=\"#{sv}\"#{selected}>#{sv}</option>"
|
2022-02-25 06:49:00 +00:00
|
|
|
end
|
2024-08-08 15:53:10 +00:00
|
|
|
form_field += "</select>"
|
2022-02-25 06:49:00 +00:00
|
|
|
end
|
2015-11-13 13:10:00 +00:00
|
|
|
end
|
|
|
|
|
2024-08-08 15:53:10 +00:00
|
|
|
if params["sortcolumn"] == field_key
|
2015-11-18 15:51:49 +00:00
|
|
|
sort_class = params["sort"] == "asc" ? "sort-desc" : "sort-asc"
|
2024-08-08 15:53:10 +00:00
|
|
|
sort_url = "/#{I18n.locale.to_s}#{page.url}?sortcolumn=#{field_key}&sort=#{params["sort"] == "asc" ? "desc" : "asc"}#{query_string}"
|
2015-11-18 15:51:49 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
title_class = title_class + "no-sort" if sort == "sort"
|
2015-11-13 13:10:00 +00:00
|
|
|
title_class = title_class + " no-search" if search == "hide"
|
|
|
|
|
2024-08-11 02:17:53 +00:00
|
|
|
col = {
|
2015-11-13 13:10:00 +00:00
|
|
|
"title" => tc.title,
|
|
|
|
"type" => tc.type,
|
2024-08-08 15:53:10 +00:00
|
|
|
"key" => field_key,
|
2015-11-13 13:10:00 +00:00
|
|
|
"search" => search,
|
2015-11-18 15:51:49 +00:00
|
|
|
"form-field" => form_field,
|
2015-11-13 13:10:00 +00:00
|
|
|
"sort" => sort,
|
2015-11-18 15:51:49 +00:00
|
|
|
"sort-class" => sort_class,
|
|
|
|
"sort-url" => sort_url,
|
2015-11-13 13:10:00 +00:00
|
|
|
"title-class" => title_class
|
|
|
|
}
|
2024-08-11 02:17:53 +00:00
|
|
|
if tc.is_searchable
|
|
|
|
searchable_columns << col
|
|
|
|
end
|
|
|
|
col
|
2015-11-13 13:10:00 +00:00
|
|
|
end
|
2020-04-29 05:41:07 +00:00
|
|
|
tablecolumns = table.table_columns.where(:display_in_index => true).asc(:order)
|
2015-11-13 13:10:00 +00:00
|
|
|
rows = []
|
2016-09-21 12:25:41 +00:00
|
|
|
entries = get_entries(params, table, page)
|
|
|
|
total_pages = entries.total_pages
|
|
|
|
entries.each do |te|
|
|
|
|
cols = []
|
|
|
|
sort_value = ""
|
|
|
|
tablecolumns.each do |column|
|
|
|
|
ce = te.column_entries.where(:table_column_id => column.id).first rescue nil
|
|
|
|
if !ce.nil?
|
2024-07-21 07:26:48 +00:00
|
|
|
text = ce.get_frontend_text(column)
|
2016-09-21 12:25:41 +00:00
|
|
|
if column.is_link_to_show
|
|
|
|
text = "<a href='#{OrbitHelper.url_to_show("-" + te.uid)}'>#{text}</a>"
|
|
|
|
end
|
|
|
|
|
|
|
|
cols << {"text" => text}
|
|
|
|
else
|
|
|
|
cols << {"text" => ""}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
rows << {
|
|
|
|
"columns" => cols
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
export_button = ""
|
2024-08-08 15:53:10 +00:00
|
|
|
if get_query(params).present?
|
2016-09-21 12:25:41 +00:00
|
|
|
reset = ""
|
|
|
|
if !OrbitHelper.current_user.nil?
|
|
|
|
p = {}
|
2024-06-11 14:45:53 +00:00
|
|
|
params.each do |k,v|
|
2016-09-21 12:25:41 +00:00
|
|
|
p[k] = v if ["q","column","sort","sortcolumn"].include?(k)
|
|
|
|
end
|
|
|
|
export_button = "<a href='/xhr/universal_table/export.xlsx?cat=#{OrbitHelper.page_categories.first}&page_id=#{page.page_id}&#{p.to_param}'>Export</a>"
|
|
|
|
end
|
|
|
|
total_entries = t("universal_table.total_number_of_entries", :total_number => entries.total_count)
|
|
|
|
elsif params["sortcolumn"].present?
|
|
|
|
reset = ""
|
|
|
|
end
|
|
|
|
{
|
2024-08-11 02:17:53 +00:00
|
|
|
"searchable-columns" => searchable_columns,
|
2016-09-21 12:25:41 +00:00
|
|
|
"head-columns" => table_heads,
|
|
|
|
"rows" => rows,
|
|
|
|
"total_pages" => total_pages,
|
|
|
|
"extras" => {
|
|
|
|
"total_entries" => total_entries,
|
|
|
|
"table-name" => table.title,
|
|
|
|
"export_button" => export_button,
|
|
|
|
"reset" => reset,
|
|
|
|
"url" => "/#{I18n.locale.to_s}#{page.url}"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def export_filtered
|
|
|
|
table = UTable.where(:category_id => params[:cat]).first rescue nil
|
|
|
|
page = Page.where(:page_id => params[:page_id]).first
|
|
|
|
if !table.nil?
|
2024-07-21 07:22:24 +00:00
|
|
|
host_url = Site.first.root_url
|
|
|
|
if host_url == "http://"
|
|
|
|
host_url = request.protocol + request.host_with_port
|
|
|
|
end
|
2016-09-21 12:25:41 +00:00
|
|
|
@rows = []
|
|
|
|
@tablecolumns = table.table_columns.where(:display_in_index => true).asc(:order)
|
|
|
|
entries = get_entries(params, table, page, false)
|
|
|
|
entries.each do |te|
|
|
|
|
cols = []
|
|
|
|
sort_value = ""
|
|
|
|
@tablecolumns.each do |column|
|
|
|
|
ce = te.column_entries.where(:table_column_id => column.id).first rescue nil
|
|
|
|
if !ce.nil?
|
|
|
|
text = ""
|
|
|
|
case ce.type
|
|
|
|
when "text"
|
|
|
|
text = ce.text
|
2022-02-25 06:49:00 +00:00
|
|
|
when "integer"
|
|
|
|
text = ce.number
|
2016-09-21 12:25:41 +00:00
|
|
|
when "editor"
|
|
|
|
text = ce.content
|
|
|
|
when "date"
|
|
|
|
text = format_date(ce.date, column.date_format)
|
|
|
|
when "period"
|
|
|
|
text = format_date(ce.period_from, column.date_format) + " ~ " + format_date(ce.period_to, column.date_format)
|
|
|
|
text = "" if text.starts_with?(" ~")
|
|
|
|
when "image"
|
2024-07-21 07:22:24 +00:00
|
|
|
text = host_url + ce.image.thumb.url
|
2024-07-21 07:26:48 +00:00
|
|
|
when "file"
|
|
|
|
file_links = []
|
|
|
|
locale = I18n.locale.to_s
|
|
|
|
ce.column_entry_files.desc(:sort_number).each do |entry_file|
|
|
|
|
next unless entry_file.choose_lang_display(locale)
|
|
|
|
file_links << (host_url + entry_file.get_link)
|
|
|
|
end
|
|
|
|
text = file_links.join("\r\n")
|
2016-09-21 12:25:41 +00:00
|
|
|
end
|
|
|
|
cols << {"text" => text}
|
|
|
|
else
|
|
|
|
cols << {"text" => ""}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
@rows << {
|
|
|
|
"columns" => cols
|
|
|
|
}
|
|
|
|
end
|
|
|
|
excel_name = table.title + ".xlsx"
|
|
|
|
excel_name = 'attachment; filename="' + excel_name + '"'
|
2024-06-11 14:45:53 +00:00
|
|
|
|
2016-09-21 12:25:41 +00:00
|
|
|
end
|
|
|
|
respond_to do |format|
|
|
|
|
format.xlsx {
|
|
|
|
response.headers['Content-Disposition'] = excel_name
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-08-08 15:53:10 +00:00
|
|
|
def get_query(params)
|
|
|
|
if params["column"].present?
|
|
|
|
q = {params["column"] => params["q"]}
|
|
|
|
else
|
|
|
|
q = params["q"]
|
|
|
|
end
|
|
|
|
if q.present?
|
|
|
|
q.map{|k,v| [k, v.to_s.strip] if v.present?}.compact.to_h
|
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-09-21 12:25:41 +00:00
|
|
|
def get_entries(params, table, page, paginated=true)
|
|
|
|
entries = []
|
2024-08-08 15:53:10 +00:00
|
|
|
q = get_query(params)
|
|
|
|
if q.present?
|
|
|
|
table_entry_ids = nil
|
|
|
|
q.each do |k, v|
|
|
|
|
column = table.table_columns.where(:key => k).first
|
|
|
|
next if column.nil?
|
|
|
|
if column.make_categorizable
|
|
|
|
regex = Regexp.new(".*"+v+".*", "i")
|
|
|
|
else
|
|
|
|
regexes = v.split(/\s+(?=(?:[^"]*"[^"]*")*[^"]*$)/)
|
|
|
|
regex = Regexp.union(regexes.map{|word| Regexp.new(".*"+word+".*", "i")})
|
|
|
|
end
|
|
|
|
if column.type == "text"
|
|
|
|
columns = column.column_entries.any_of([{"text.en" => regex}, {"text.zh_tw" => regex}])
|
|
|
|
elsif column.type == "editor"
|
|
|
|
columns = column.column_entries.any_of([{"content.en" => regex}, {"content.zh_tw" => regex}])
|
|
|
|
end
|
|
|
|
# .or(:"content.en" => regex).or(:"content.zh_tw" => regex)
|
|
|
|
tmp = columns.pluck(:table_entry_id)
|
|
|
|
if table_entry_ids.nil?
|
|
|
|
table_entry_ids = tmp
|
|
|
|
else
|
|
|
|
table_entry_ids = table_entry_ids & tmp
|
|
|
|
end
|
2022-02-24 09:31:45 +00:00
|
|
|
end
|
2024-08-08 15:53:10 +00:00
|
|
|
if table_entry_ids.nil?
|
2024-08-11 02:15:35 +00:00
|
|
|
entries = TableEntry.where(:u_table_id=>table.id, "have_data.#{I18n.locale}" => {"$in" => [nil, true]})
|
2024-08-08 15:53:10 +00:00
|
|
|
else
|
2024-08-11 02:15:35 +00:00
|
|
|
entries = TableEntry.where(:id.in=> table_entry_ids, "have_data.#{I18n.locale}" => {"$in" => [nil, true]})
|
2024-06-11 14:45:53 +00:00
|
|
|
end
|
2024-08-08 15:53:10 +00:00
|
|
|
entries = TableEntry.sorted(entries: entries, params: params, table: table, paginated: paginated)
|
2022-02-24 11:34:53 +00:00
|
|
|
if paginated
|
2024-08-08 15:53:10 +00:00
|
|
|
entries = entries.page(params["page_no"]).per(OrbitHelper.page_data_count)
|
2022-02-24 09:31:45 +00:00
|
|
|
end
|
|
|
|
else
|
2022-02-24 11:34:53 +00:00
|
|
|
if paginated
|
2024-08-11 02:15:35 +00:00
|
|
|
entries = TableEntry.where(:u_table_id=>table.id, "have_data.#{I18n.locale}" => {"$in" => [nil, true]}).sorting(params: params,table: table,page_num: params["page_no"],per: OrbitHelper.page_data_count)
|
2015-11-13 13:10:00 +00:00
|
|
|
else
|
2024-08-11 02:15:35 +00:00
|
|
|
entries = TableEntry.where(:u_table_id=>table.id, "have_data.#{I18n.locale}" => {"$in" => [nil, true]}).sorting(params: params,table: table,paginated: false)
|
2015-11-13 13:10:00 +00:00
|
|
|
end
|
2022-02-24 09:31:45 +00:00
|
|
|
end
|
|
|
|
entries
|
2015-11-13 13:10:00 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def show
|
|
|
|
params = OrbitHelper.params
|
|
|
|
entry = TableEntry.where(:uid => params[:uid]).first rescue nil
|
|
|
|
rows = []
|
|
|
|
|
|
|
|
entry.column_entries.each do |ce|
|
|
|
|
ct = ce.table_column
|
2024-07-21 07:26:48 +00:00
|
|
|
text = ce.get_frontend_text(ct)
|
2015-11-13 13:10:00 +00:00
|
|
|
rows << {
|
|
|
|
"title" => ct.title,
|
|
|
|
"text" => text,
|
|
|
|
"order" => ct.order
|
2015-11-18 15:51:49 +00:00
|
|
|
} if text != ""
|
2015-11-13 13:10:00 +00:00
|
|
|
end
|
|
|
|
sorted = rows.sort{ |k,v| k["order"] <=> v["order"] }
|
2024-07-21 07:23:29 +00:00
|
|
|
entry.inc(view_count: 1)
|
2015-11-13 13:10:00 +00:00
|
|
|
{
|
2024-07-21 07:23:29 +00:00
|
|
|
"entry" => sorted,
|
|
|
|
"extras" => {
|
2024-07-22 10:12:06 +00:00
|
|
|
"view_count_head" => I18n.t("view_count"),
|
2024-07-21 07:23:29 +00:00
|
|
|
"view_count" => entry.view_count
|
|
|
|
}
|
2015-11-13 13:10:00 +00:00
|
|
|
}
|
|
|
|
end
|
2024-07-21 07:26:48 +00:00
|
|
|
|
|
|
|
def download_file
|
|
|
|
file_id = params[:file]
|
|
|
|
file = ColumnEntryFile.find(file_id) rescue nil
|
|
|
|
if !file.nil? && file.file.present?
|
|
|
|
file.inc(download_count: 1)
|
|
|
|
@url = file.file.url
|
|
|
|
begin
|
|
|
|
@path = file.file.file.file rescue ""
|
|
|
|
@filename = File.basename(@path)
|
|
|
|
@ext = @filename.split(".").last
|
|
|
|
if @ext == "png" || @ext == "jpg" || @ext == "bmp" || @ext == "pdf"
|
|
|
|
render "download_file",:layout=>false
|
|
|
|
else
|
|
|
|
if (current_site.accessibility_mode rescue false)
|
|
|
|
render "redirect_to_file",:layout=>false
|
|
|
|
else
|
|
|
|
user_agent = request.user_agent.downcase
|
|
|
|
@escaped_file_name = user_agent.match(/(msie|trident)/) ? CGI::escape(@filename) : @filename
|
|
|
|
send_file(@path, :type=>"application/octet-stream", :filename => @escaped_file_name, :x_sendfile=> true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
rescue
|
|
|
|
redirect_to @url
|
|
|
|
end
|
|
|
|
else
|
|
|
|
render :file => "#{Rails.root}/app/views/errors/404.html", :layout => false, :status => :not_found
|
|
|
|
end
|
|
|
|
end
|
2015-11-13 13:10:00 +00:00
|
|
|
end
|