orbit_4_2_upgrade/app/controllers/upgrade_controller.rb

96 lines
2.4 KiB
Ruby

class UpgradeController < ApplicationController
def index
data = nil
case params[:module]
when "announcement"
data = get_announcements
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"] = smart_convertor(b.text_translations["en"]) if !b.text_translations["en"].nil?
text["zh_tw"] = smart_convertor(b.text_translations["zh_tw"]) if !b.text_translations["en"].nil?
{
"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 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