class UpgradeController < ApplicationController def index data = nil case params[:module] when "announcement" data = get_announcements when "archive" data = get_archives when "link" data = get_links when "page" data = get_pages else data = {"success" => false, "msg" => "Module not found."} end render :json => data.to_json end private def get_announcements ma = ModuleApp.find_by_key("announcement") categories = ma.categories.collect do |c| { "title" => c.title_translations, "disable" => c.disable } end tags = ma.tags.collect do |t| { "name" => t.name_translations, "is_default" => t.is_default } end bulletins = Bulletin.all.collect do |b| image = request.protocol + request.host_with_port + b.image.url if !b.image.nil? && b.image.to_s != "sign-in-logo.png" links = b.bulletin_links.collect do |bl| { "title" => bl.title_translations, "url" => bl.url } end files = b.bulletin_files.collect do |bf| file = request.protocol + request.host_with_port + bf.file.url rescue nil { "title" => bf.title_translations, "description" => bf.description_translations, "file" => file } end ts = b.tags.collect do |t| { "name" => t.name_translations } end text = {"en" => "", "zh_tw" => ""} text["en"] = (b.text_translations["en"].nil? ? "" :smart_convertor(b.text_translations["en"])) text["zh_tw"] = (b.text_translations["zh_tw"].nil? ? "" : smart_convertor(b.text_translations["zh_tw"])) { "id" => b.id.to_s, "title" => b.title_translations, "subtitle" => b.subtitle_translations, "text" => text, "public" => b.public, "postdate" => b.postdate, "deadline" => b.deadline, "email_sent" => b.email_sent, "email_sentdate" => b.email_sentdate, "other_mailaddress" => b.other_mailaddress, "category" => b.category.title_translations, "tags" => ts, "image" => image, "links" => links, "files" => files } end return {"announcements" => bulletins, "categories" => categories, "tags" => tags, "success" => true} end def get_archives ma = ModuleApp.find_by_key("archive") categories = ma.categories.collect do |c| { "title" => c.title_translations, "disable" => c.disable } end tags = ma.tags.collect do |t| { "name" => t.name_translations, "is_default" => t.is_default } end archives = ArchiveFile.all.collect do |a| files = a.archive_file_multiples.collect do |af| file = request.protocol + request.host_with_port + af.file.url rescue nil { "file_title" => af.file_title_translations, "file" => file } end ts = a.tags.collect do |t| { "name" => t.name_translations } end { "id" => a.id.to_s, "title" => a.title_translations, "category" => a.category.title_translations, "tags" => ts, "files" => files } end return {"archives" => archives, "categories" => categories, "tags" => tags, "success" => true} end def get_links ma = ModuleApp.find_by_key("web_resource") categories = ma.categories.collect do |c| { "title" => c.title_translations, "disable" => c.disable } end tags = ma.tags.collect do |t| { "name" => t.name_translations, "is_default" => t.is_default } end links = WebLink.all.collect do |b| ts = b.tags.collect do |t| { "name" => t.name_translations } end { "id" => b.id.to_s, "title" => b.title_translations, "category" => b.category.title_translations, "tags" => ts, "context" => b.context_translations, "url" => b.url_translations } end return {"links" => links, "categories" => categories, "tags" => tags, "success" => true} end def get_pages ma = ModuleApp.find_by_key("page_content") pages = PageContext.all.collect do |p| # context = {"en" => "", "zh_tw" => ""} # context["en"] = (p.context_translations["en"].nil? ? "" : smart_convertor(p.context_translations["en"])) # context["zh_tw"] = (p.context_translations["zh_tw"].nil? ? "" : smart_convertor(p.context_translations["zh_tw"])) { "id" => p.id.to_s, "context" => p.context, "page_id" => p.page_id } end return {"pages" => pages, "success" => true} end def smart_convertor(text) html_string = text links = html_string.scan(/img.*?src="(.*?)"/i) links.each do |link| l = link.first new_link = nil if l.starts_with?("/") new_link = request.protocol + request.host_with_port + l elsif l.starts_with?("..") l1 = l.gsub("../","") new_link = request.protocol + request.host_with_port + "/" + l1 end html_string = html_string.sub(l,new_link) if !new_link.nil? end return html_string end end