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
|
|
|
|
page = Page.where(:page_id => params[:page_id]).first
|
|
|
|
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
|
|
|
|
params_column = params["column"].to_s.gsub("\"",'')
|
|
|
|
params_q = params["q"].to_s.gsub("\"",'')
|
2020-04-29 07:59:52 +00:00
|
|
|
params_no = params["page_no"].to_s.gsub("\"",'')
|
2015-11-13 13:10:00 +00:00
|
|
|
table_heads = table.table_columns.where(:display_in_index => true).asc(:order).collect do |tc|
|
|
|
|
search = ""
|
2015-11-18 15:51:49 +00:00
|
|
|
sort_class = "sort"
|
|
|
|
sort = ""
|
2020-04-29 05:29:13 +00:00
|
|
|
form_field = "<input type=\"hidden\" name=\"authenticity_token\" value=\"#{csrf_value}\"><input type='search' class='form-control' name='q' placeholder='Search keyword'>"
|
2015-11-18 15:51:49 +00:00
|
|
|
query_string = ""
|
2020-04-29 05:37:14 +00:00
|
|
|
query_string = "&column=#{params_column}&q=#{params_q}" if params["column"].present?
|
2020-04-29 07:59:52 +00:00
|
|
|
query_string = query_string + "&page_no=#{params_no}" if params["page_no"].present?
|
2015-11-18 15:51:49 +00:00
|
|
|
sort_url = "/#{I18n.locale.to_s}#{page.url}?sortcolumn=#{tc.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
|
|
|
|
select_values = tc.column_entries.distinct(:text).uniq
|
2020-04-29 05:29:13 +00:00
|
|
|
form_field = "<input type=\"hidden\" name=\"authenticity_token\" value=\"#{csrf_value}\"><select class='form-control' name='q'>"
|
2015-11-18 15:51:49 +00:00
|
|
|
select_values.each do |sv|
|
|
|
|
form_field = form_field + "<option value='#{sv[I18n.locale.to_s]}'>#{sv[I18n.locale.to_s]}</option>"
|
|
|
|
end
|
|
|
|
form_field = form_field + "</select>"
|
|
|
|
end
|
2015-11-13 13:10:00 +00:00
|
|
|
end
|
|
|
|
|
2015-11-18 15:51:49 +00:00
|
|
|
if params["sortcolumn"] == tc.key
|
|
|
|
sort_class = params["sort"] == "asc" ? "sort-desc" : "sort-asc"
|
|
|
|
sort_url = "/#{I18n.locale.to_s}#{page.url}?sortcolumn=#{tc.key}&sort=#{params["sort"] == "asc" ? "desc" : "asc"}#{query_string}"
|
|
|
|
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"
|
|
|
|
|
|
|
|
{
|
|
|
|
"title" => tc.title,
|
|
|
|
"type" => tc.type,
|
|
|
|
"key" => tc.key,
|
|
|
|
"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
|
|
|
|
}
|
|
|
|
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?
|
|
|
|
text = ""
|
|
|
|
case ce.type
|
|
|
|
when "text"
|
|
|
|
text = ce.text
|
|
|
|
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"
|
|
|
|
text = "<img src='#{ce.image.thumb.url}' class='image-preview' />"
|
|
|
|
end
|
|
|
|
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 = ""
|
|
|
|
if params["column"].present?
|
|
|
|
reset = ""
|
|
|
|
if !OrbitHelper.current_user.nil?
|
|
|
|
p = {}
|
|
|
|
params.each do |k,v|
|
|
|
|
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
|
|
|
|
{
|
|
|
|
"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?
|
|
|
|
@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
|
|
|
|
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"
|
|
|
|
text = "http://#{request.host_with_port}" + ce.image.thumb.url
|
|
|
|
end
|
|
|
|
cols << {"text" => text}
|
|
|
|
else
|
|
|
|
cols << {"text" => ""}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
@rows << {
|
|
|
|
"columns" => cols
|
|
|
|
}
|
|
|
|
end
|
|
|
|
excel_name = table.title + ".xlsx"
|
|
|
|
excel_name = 'attachment; filename="' + excel_name + '"'
|
|
|
|
|
|
|
|
end
|
|
|
|
respond_to do |format|
|
|
|
|
format.xlsx {
|
|
|
|
response.headers['Content-Disposition'] = excel_name
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_entries(params, table, page, paginated=true)
|
|
|
|
entries = []
|
|
|
|
if params["column"].present?
|
2015-11-13 13:10:00 +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(:key => params["column"]).first
|
2015-11-18 15:51:49 +00:00
|
|
|
if params["sort"].present?
|
|
|
|
column_to_sort = table.table_columns.where(:key => params["sortcolumn"]).first
|
|
|
|
case column_to_sort.type
|
|
|
|
when "text"
|
|
|
|
field_name = :text
|
|
|
|
when "editor"
|
|
|
|
field_name = :content
|
|
|
|
when "date"
|
|
|
|
field_name = :date
|
|
|
|
end
|
|
|
|
if params["column"] == params["sortcolumn"]
|
|
|
|
columns = column.column_entries.any_of(:"text.en" => regex).or(:"text.zh_tw" => regex).or(:"content.en" => regex).or(:"content.zh_tw" => regex).order_by(field_name => params["sort"])
|
|
|
|
else
|
|
|
|
temp = column.column_entries.any_of(:"text.en" => regex).or(:"text.zh_tw" => regex).or(:"content.en" => regex).or(:"content.zh_tw" => regex)
|
|
|
|
columns = []
|
|
|
|
temp.each do |c|
|
|
|
|
columns << c.table_entry.column_entries.where(:table_column_id => column_to_sort.id).first
|
|
|
|
end
|
|
|
|
sorted_columns = column_to_sort.column_entries.order_by(field_name => params["sort"]).to_a
|
|
|
|
columns = sorted_columns & columns
|
|
|
|
end
|
|
|
|
else
|
|
|
|
columns = column.column_entries.any_of(:"text.en" => regex).or(:"text.zh_tw" => regex).or(:"content.en" => regex).or(:"content.zh_tw" => regex)
|
|
|
|
end
|
2015-11-13 13:10:00 +00:00
|
|
|
columns.each do |column|
|
|
|
|
entries << column.table_entry
|
|
|
|
end
|
2015-11-18 15:51:49 +00:00
|
|
|
entries = entries.sort{|k,v| v["created_at"] <=> k["created_at"]} if !params["sort"].present?
|
2016-09-21 12:25:41 +00:00
|
|
|
entries = Kaminari.paginate_array(entries).page(params["page_no"]).per(OrbitHelper.page_data_count) if(paginated)
|
2015-11-13 13:10:00 +00:00
|
|
|
else
|
2015-11-18 15:51:49 +00:00
|
|
|
if params["sort"].present?
|
|
|
|
column_to_sort = table.table_columns.where(:key => params["sortcolumn"]).first
|
|
|
|
case column_to_sort.type
|
|
|
|
when "text"
|
|
|
|
field_name = :text
|
|
|
|
when "editor"
|
|
|
|
field_name = :content
|
|
|
|
when "date"
|
|
|
|
field_name = :date
|
|
|
|
end
|
|
|
|
columns = column_to_sort.column_entries.order_by(field_name => params["sort"])
|
|
|
|
columns.each do |column|
|
|
|
|
entries << column.table_entry
|
|
|
|
end
|
|
|
|
|
2016-09-21 12:25:41 +00:00
|
|
|
entries = Kaminari.paginate_array(entries).page(params["page_no"]).per(OrbitHelper.page_data_count) if(paginated)
|
|
|
|
else
|
|
|
|
entries = table.table_entries.desc(:created_at).page(params["page_no"]).per(OrbitHelper.page_data_count) if(paginated)
|
2015-11-13 13:10:00 +00:00
|
|
|
end
|
|
|
|
end
|
2016-09-21 12:25:41 +00:00
|
|
|
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
|
|
|
|
text = ""
|
|
|
|
case ce.type
|
|
|
|
when "text"
|
|
|
|
text = ce.text
|
|
|
|
when "editor"
|
|
|
|
text = ce.content
|
|
|
|
when "date"
|
2015-11-18 15:51:49 +00:00
|
|
|
text = format_date(ce.date, ce.table_column.date_format)
|
2015-11-13 13:10:00 +00:00
|
|
|
when "period"
|
2015-11-18 15:51:49 +00:00
|
|
|
text = format_date(ce.period_from, ce.table_column.date_format) + " ~ " + format_date(ce.period_from, ce.table_column.date_format)
|
|
|
|
text = "" if text.starts_with?(" ~")
|
2015-11-13 13:10:00 +00:00
|
|
|
when "image"
|
|
|
|
text = "<img src='#{ce.image.thumb.url}' class='image-preview' />"
|
|
|
|
end
|
2015-11-18 15:51:49 +00:00
|
|
|
|
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"] }
|
|
|
|
{
|
|
|
|
"entry" => sorted
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|