class UniversalTablesController < ApplicationController 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? table_heads = table.table_columns.where(:display_in_index => true).asc(:order).collect do |tc| search = "" sort = "hide" title_class = "" case tc.type when "date","period","image" search = "hide" end title_class = title_class + "no-sort" if sort == "hide" title_class = title_class + " no-search" if search == "hide" { "title" => tc.title, "type" => tc.type, "key" => tc.key, "search" => search, "sort" => sort, "title-class" => title_class } end tablecolumns = table.table_columns.where(:display_in_index => true).asc(:order) rows = [] if params["column"].present? 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 columns = column.column_entries.any_of(:"text.en" => regex).or(:"text.zh_tw" => regex).or(:"content.en" => regex).or(:"content.zh_tw" => regex) entries = [] columns.each do |column| entries << column.table_entry end entries = Kaminari.paginate_array(entries).page(params["page_no"]).per(OrbitHelper.page_data_count) else entries = table.table_entries.page(params["page_no"]).per(OrbitHelper.page_data_count) end entries.each do |te| cols = [] 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 = ce.date.strftime("%Y-%m-%d %H:%M") when "period" text = (ce.period_from.strftime("%Y-%m-%d") rescue "") + " ~ " + (ce.period_to.strftime("%Y-%m-%d") rescue "") text = "" if text.starts_with?("~") when "image" text = "" end if column.is_link_to_show text = "#{text}" end cols << { "text" => text } end end rows << { "columns" => cols } end end { "head-columns" => table_heads, "rows" => rows, "total_pages" => entries.total_pages, "extras" => { "table-name" => table.title, "url" => "/#{I18n.locale.to_s}#{page.url}" } } 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" text = ce.date.strftime("%Y-%m-%d %H:%M") when "period" text = (ce.period_from.strftime("%Y-%m-%d") rescue "") + " ~ " + (ce.period_to.strftime("%Y-%m-%d") rescue "") text = "" if text.starts_with?("~") when "image" text = "" end rows << { "title" => ct.title, "text" => text, "order" => ct.order } end sorted = rows.sort{ |k,v| k["order"] <=> v["order"] } { "entry" => sorted } end end