announcement-test/app/helpers/admin/announcements_helper.rb

181 lines
5.4 KiB
Ruby
Raw Normal View History

require "net/http"
require "uri"
require 'json'
2014-04-03 03:18:02 +00:00
module Admin::AnnouncementsHelper
2014-05-02 10:21:51 +00:00
def page_for_bulletin(bulletin)
2014-05-28 07:37:56 +00:00
ann_page = nil
pages = Page.where(:module=>'announcement')
pages.each do |page|
if page.categories.count ==1
if page.categories.include?(bulletin.category.id.to_s)
ann_page = page
end
end
break if !ann_page.nil?
end
if ann_page.nil?
pages.each do |page|
if page.categories.include?(bulletin.category.id.to_s)
ann_page = page
end
break if !ann_page.nil?
end
end
ann_page = pages.first if ann_page.nil?
request.protocol+(request.host_with_port+ann_page.url+'/'+bulletin.to_param).gsub('//','/') rescue "/"
2014-05-02 10:21:51 +00:00
end
2014-09-22 08:32:30 +00:00
2016-04-21 17:28:37 +00:00
def import_this_announcement(row,categories,tags)
value = {}
anns = Bulletin.new
row.cells.each_with_index do |cell,index|
next if cell.nil?
val = cell.value
next if val.nil? || val == ""
case index
when 0
anns.category = categories[val.to_i]
when 1
new_tags = []
if (val.include?(",") rescue false)
ts = val.split(",")
ts.each do |t|
new_tags << tags[t.to_i]
end
else
new_tags << tags[val.to_i]
end
anns.tags=new_tags
when 2
anns.postdate = val
when 3
anns.deadline = val
when 4
anns.is_top = (val.to_i == 1 ? true : false)
when 5
anns.is_hot = (val.to_i == 1 ? true : false)
when 6
anns.is_hidden = (val.to_i == 1 ? true : false)
when 7
anns.remote_image_url = val
when 8
value["en"] = val
when 9
value["zh_tw"] = val
anns.image_description_translations = value
value = {}
when 10
value["en"] = val
when 11
value["zh_tw"] = val
anns.title_translations = value
value = {}
when 12
value["en"] = val
when 13
value["zh_tw"] = val
anns.subtitle_translations = value
value = {}
when 14
value["en"] = val
when 15
value["zh_tw"] = val
anns.text_translations = value
value = {}
when 16
links = val.split(";")
desc_en = row.cells[17].value.split(";")
desc_zh_tw = row.cells[18].value.split(";")
links.each_with_index do |link,i|
bl = BulletinLink.new
bl.url = link.strip
bl.title_translations = {"en" => desc_en[i], "zh_tw" => desc_zh_tw[i]}
bl.bulletin_id = anns.id
bl.save
end
when 19
files = val.split(";")
desc_en = row.cells[20].value.split(";")
desc_zh_tw = row.cells[21].value.split(";")
alt_en = row.cells[22].value.split(";")
alt_zh_tw = row.cells[23].value.split(";")
files.each_with_index do |file, i|
bf = BulletinFile.new
bf.remote_file_url = file.strip rescue nil
bf.title_translations = {"en" => (desc_en[i] rescue ""), "zh_tw" => (desc_zh_tw[i] rescue "")}
bf.description_translations = {"en" => (alt_en[i] rescue ""), "zh_tw" => (alt_zh_tw[i] rescue "")}
bf.bulletin_id = anns.id
bf.save
end
2016-04-21 17:28:37 +00:00
end
end
anns.create_user_id = current_user.id.to_s
anns.update_user_id = current_user.id.to_s
anns.approved = true
anns.save
end
def download_tmp_xml(url)
xml = File.join(Rails.root, "tmp", "ann_cc_ntu.xml")
open(xml, 'wb') do |fo|
fo.print open(url).read
end
end
def import_from_tmp_xml(file)
xml = Nokogiri::XML(file)
return if xml.nil?
announcements = []
xml.xpath("//channel").xpath("//item").each do |anns|
announcements << {
:title => (anns>"title").text,
:category => (anns>"category").text,
:postdate => (anns>"pubDate").text,
:text => (anns>"description").text,
:rss2_sn => (anns>"link").text.split("=").last
}
end
announcements.each do |anns|
ma = ModuleApp.where(:key => "announcement").first
cat = Category.where(:title => anns[:category]).first rescue nil
if cat.nil?
cat = Category.create(:title_translations => {"en" => anns[:category], "zh_tw" => anns[:category]}, :module_app_id => ma.id)
end
ann = Bulletin.where(:rss2_sn => anns[:rss2_sn]).first rescue nil
if ann.nil?
ann = Bulletin.new(:title_translations => {"en" => "", "zh_tw" => anns[:title]}, :postdate => anns[:postdate], :subtitle_translations => {"en" => "", "zh_tw" => anns[:title]}, :text_translations => {"en" => "", "zh_tw" => anns[:text]}, :rss2_sn => anns[:rss2_sn], :category_id => cat.id, :approved => true, :create_user_id => current_user.id)
else
ann.update_attributes(:title_translations => {"en" => "", "zh_tw" => anns[:title]}, :postdate => anns[:postdate], :subtitle_translations => {"en" => "", "zh_tw" => anns[:title]}, :text_translations => {"en" => "", "zh_tw" => anns[:text]})
end
ann.save
end
File.delete(file)
end
2014-09-22 08:32:30 +00:00
def load_access_level
2014-10-20 10:53:31 +00:00
if (current_user.is_admin? rescue false)
2014-09-22 08:32:30 +00:00
@access_level = "admin"
2014-10-20 10:53:31 +00:00
elsif (current_user.is_manager?(@module_app) rescue false)
2014-09-22 08:32:30 +00:00
@access_level = "manager"
2014-10-20 10:53:31 +00:00
else
@access_level = "users"
2014-09-22 08:32:30 +00:00
end
end
def user_can_approve?
case @access_level
when "admin"
return true
when "manager"
return true
else
return false
end
end
2014-04-03 03:18:02 +00:00
end