class UniversalTablesController < ApplicationController include Admin::UniversalTablesHelper 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? reset = "hide" 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("\"",'') params_no = params["page_no"].to_s.gsub("\"",'') table_heads = table.table_columns.where(:display_in_index => true).asc(:order).collect do |tc| search = "" sort_class = "sort" sort = "" form_field = "" query_string = "" query_string = "&column=#{params_column}&q=#{params_q}" if params["column"].present? query_string = query_string + "&page_no=#{params_no}" if params["page_no"].present? sort_url = "/#{I18n.locale.to_s}#{page.url}?sortcolumn=#{tc.key}&sort=asc#{query_string}" title_class = "" case tc.type when "date" search = "hide" 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.#{I18n.locale.to_s}") form_field = "" end when "integer" if tc.make_categorizable select_values = tc.column_entries.distinct(:number) form_field = "" end end 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" title_class = title_class + " no-search" if search == "hide" { "title" => tc.title, "type" => tc.type, "key" => tc.key, "search" => search, "form-field" => form_field, "sort" => sort, "sort-class" => sort_class, "sort-url" => sort_url, "title-class" => title_class } end tablecolumns = table.table_columns.where(:display_in_index => true).asc(:order) rows = [] 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 "integer" text = ce.number 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 = "" end if column.is_link_to_show text = "#{text}" 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 = "Export" 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? host_url = Site.first.root_url if host_url == "http://" host_url = request.protocol + request.host_with_port end @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 "integer" text = ce.number 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 = host_url + 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? keywords = params["q"] keywords = keywords.strip.nil? ? keywords : keywords.strip column = table.table_columns.where(:key => params["column"]).first if column.make_categorizable regex = Regexp.new(".*"+keywords+".*", "i") else regexes = keywords.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) if paginated entries = TableEntry.where(:u_table_id=>table.id).sorting(params: params,table: table,column_entries: columns,page_num: params["page_no"],per: OrbitHelper.page_data_count) else entries = TableEntry.where(:u_table_id=>table.id).sorting(params: params,table: table,column_entries: columns,paginated: false) end else if paginated entries = TableEntry.where(:u_table_id=>table.id).sorting(params: params,table: table,page_num: params["page_no"],per: OrbitHelper.page_data_count) else entries = TableEntry.where(:u_table_id=>table.id).sorting(params: params,table: table,paginated: false) end end entries 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 "integer" text = ce.number when "editor" text = ce.content when "date" text = format_date(ce.date, ce.table_column.date_format) when "period" 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?(" ~") when "image" text = "" end rows << { "title" => ct.title, "text" => text, "order" => ct.order } if text != "" end sorted = rows.sort{ |k,v| k["order"] <=> v["order"] } entry.inc(view_count: 1) { "entry" => sorted, "extras" => { "view_count_head": I18n.t("view_count"), "view_count" => entry.view_count } } end end