class Admin::UniversalTablesController < OrbitAdminController def index @table_fields = ["universal_table.table_name","universal_table.created_time","universal_table.total_no_of_entries", "universal_table.import_from_excel"] @tables = UTable.where(:title.ne => "") .order_by(sort) .with_categories(filters("category")) end 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 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 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} if params[:q].present? @entries = search_data(@table) else @entries = TableEntry.where(u_table_id: @table.id).sorting(params: params,table: @table,page_num: params[:page],per: 10) end end end 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 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 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 def checkforthread running = !File.exists?("public/uploads/utable_export/#{params[:utable_id]}/#{params[:utable_title]}.xlsx") render :json => {"status" => running}.to_json end def checkforimportthread utable = UTable.find(params[:utable_id]) render :json => { "currentCount" => utable.current_xlsx_value, "totalCount" => utable.table_entries.count, "id" => utable.id.to_s }.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 def import_data_from_excel 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 table.set(current_xlsx_value: 0) 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 uploaded_io = params[:import_data] safe_filename = uploaded_io.original_filename.gsub(/[^0-9A-Za-z.\-_]/, '_') table_id = params[:universal_table_id] site_locales = Site.first.in_use_locales.join(":") # e.g., "en,zh" unless uploaded_io render json: { success: false, msg: "No file uploaded." } and return end # Save to tmp path tmp_path = Rails.root.join('tmp', "import_#{Time.now.to_i}_#{safe_filename}") File.open(tmp_path, 'wb') do |file| file.write(uploaded_io.read) end Rails.logger.info "rake universal_table_tasks:import['#{tmp_path}','#{table_id}','#{site_locales}']" # Call the Rake task with file path Thread.new do system("rake universal_table_tasks:import['#{tmp_path}','#{table_id}','#{site_locales}'] >> #{Rails.root}/log/rake.log &") end render json: { success: true, totalCount: sheet.count - 3 }.to_json end 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) entry.save entry.fix_have_data table = UTable.find(params[:table_entry][:u_table_id]) redirect_to admin_universal_table_new_entry_path(table) end def edit_entry id = params[:universal_table_id].split("-").last @entry = TableEntry.where(:uid => id).first @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 redirect_to (request.referrer || admin_universal_table_path(table)) end def update_entry entry = TableEntry.find(params[:id]) entry.update_attributes(table_entry_params) entry.fix_have_data # when new column insert table = entry.u_table redirect_to admin_universal_table_path(:id=> table.to_param, :page => params[:page]) end 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 def new @table = UTable.new end def create category = Category.new 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 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 cat = table.category rescue nil if cat.nil? cat = Category.new cat.module_app = @module_app end 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 cat.save p[:category_id] = cat.id table.update_attributes(p) table.save redirect_to admin_universal_tables_path end def destroy uid = params[:id].split("-").last table = UTable.where(:uid => uid).first table.category.destroy if !table.category.nil? table.destroy redirect_to admin_universal_tables_path end private def search_data(table, per=10) 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 columns = columns.uniq{|col| col.table_entry_id} columns_count = columns.count columns_count = 1 if columns_count==0 columns = Kaminari.paginate_array(columns,limit: columns_count) entries = TableEntry.where(:u_table_id=>table.id).can_display.sorting(params: params,table: table,column_entries: columns,page_num: params[:page],per: per) end def table_params params.require(:u_table).permit! end def table_entry_params params.require(:table_entry).permit! end end