orbit_4_2_upgrade/app/controllers/upgrade_controller.rb

271 lines
7.1 KiB
Ruby

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
when "gallery"
data = get_albums
when "members"
data = get_members
else
data = {"success" => false, "msg" => "Module not found."}
end
render :json => data.to_json
end
private
def get_albums
ma = ModuleApp.find_by_key("gallery")
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
albums = Album.all.collect do |album|
images = album.album_images.collect do |image|
{
"title" => image.title,
"description_translations" => image.description_translations,
"cover" => (album.cover == image.id.to_s ? true : false),
"file" => request.protocol + request.host_with_port + image.file.url
}
end
{
"title_translations" => album.name_translations,
"description_translations" => album.description_translations,
"category" => album.category.title_translations,
"images" => images
}
end
return {"albums" => albums, "categories" => categories, "tags" => tags, "success" => true}
end
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
bss = Bulletin.all.page(params[:page]).per(25)
bulletins = bss.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, "total-pages" => bss.total_pages , "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
ars = ArchiveFile.all.page(params[:page]).per(25)
archives = ars.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, "total-pages" => ars.total_pages, "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_members
profiles = User.where(:user_id.ne => "rulingcom").or(:user_id.ne => nil).page(params[:page]).per(50)
members = profiles.collect do |profile|
{
"user_id" => profile.user_id,
"token" => profile.encrypted_password,
"first_name" => profile.first_name_translations,
"last_name" => profile.last_name_translations,
"sex" => profile.sex == "unknown" ? nil : profile.sex,
"sid" => profile.sid,
"office_tel" => profile.office_tel,
"avatar" => File.basename(User.last.avatar.url) != "ga-logo.png" ? request.protocol + request.host_with_port + profile.avatar.url : nil
}
end
return {"members" => members, "total-pages" => profiles.total_pages, "success" => true}
end
def get_pages
ma = ModuleApp.find_by_key("page_content")
pages = ma.pages.collect do |p|
menu_enabled_for = []
menu_enabled_for << "en" if p.menu_enabled_for["en"]
menu_enabled_for << "zh_tw" if p.menu_enabled_for["zh_tw"]
sitemap_enabled = []
sitemap_enabled << "en" if p.sitemap_enabled["en"]
sitemap_enabled << "zh_tw" if p.sitemap_enabled["zh_tw"]
content_translations = {}
if p.page_contexts.count > 0
if !p.page_contexts.first.context.blank?
content_translations["en"] = smart_convertor p.page_contexts.first.context_translations["en"]
content_translations["zh_tw"] = smart_convertor p.page_contexts.first.context_translations["zh_tw"]
end
end
{
"page_id" => p.name,
"enabled_for" => p.enabled_for,
"menu_enabled_for" => menu_enabled_for,
"title_translations" => p.title_translations,
"enabled_for_sitemap" => sitemap_enabled,
"content_translations" => content_translations
}
end
return {"pages" => pages, "success" => true}
end
def smart_convertor(text)
return text if text.blank?
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