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

308 lines
11 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)
2021-09-15 16:12:26 +00:00
if !bulletin.is_external_link || bulletin.external_link.blank?
ann_page = nil
pages = Page.where(:module=>'announcement').select{|page| page.enabled_for.include?(I18n.locale.to_s)}
2021-09-15 16:12:26 +00:00
pages.each do |page|
if page.categories.count ==1
if (page.categories.include?(bulletin.category.id.to_s) rescue false)
ann_page = page
end
2014-05-28 07:37:56 +00:00
end
2021-09-15 16:12:26 +00:00
break if !ann_page.nil?
2014-05-28 07:37:56 +00:00
end
2021-09-15 16:12:26 +00:00
if ann_page.nil?
pages.each do |page|
if (page.categories.include?(bulletin.category.id.to_s) rescue false)
ann_page = page
end
break if !ann_page.nil?
2014-05-28 07:37:56 +00:00
end
end
2021-09-15 16:12:26 +00:00
ann_page = pages.first if ann_page.nil?
request.protocol+(request.host_with_port+ann_page.url+'/'+bulletin.to_param).gsub('//','/') rescue "#"
else
bulletin.external_link
end
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|
2021-07-16 08:26:28 +00:00
val = cell.nil? ? nil : cell.value rescue nil
2016-04-21 17:28:37 +00:00
case index
when 0
2023-09-24 05:45:40 +00:00
anns.category = categories[val.to_s.strip]
2016-04-21 17:28:37 +00:00
when 1
2023-09-24 05:45:40 +00:00
val = val.to_s
2016-04-21 17:28:37 +00:00
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
2023-09-24 05:45:40 +00:00
value["en"] = val if val.present?
2021-07-16 10:42:33 +00:00
anns.image_description_translations = value.clone
2016-04-21 17:28:37 +00:00
when 9
2023-09-24 05:45:40 +00:00
value["zh_tw"] = val if val.present?
2021-07-16 10:42:33 +00:00
anns.image_description_translations = value.clone
2016-04-21 17:28:37 +00:00
value = {}
when 10
2023-09-24 05:45:40 +00:00
value["en"] = val if val.present?
2021-07-16 10:42:33 +00:00
anns.title_translations = value.clone
2016-04-21 17:28:37 +00:00
when 11
2023-09-24 05:45:40 +00:00
value["zh_tw"] = val if val.present?
2021-07-16 10:42:33 +00:00
anns.title_translations = value.clone
2016-04-21 17:28:37 +00:00
value = {}
when 12
2023-09-24 05:45:40 +00:00
value["en"] = val if val.present?
2021-07-16 10:42:33 +00:00
anns.subtitle_translations = value.clone
2016-04-21 17:28:37 +00:00
when 13
2023-09-24 05:45:40 +00:00
value["zh_tw"] = val if val.present?
2021-07-16 10:42:33 +00:00
anns.subtitle_translations = value.clone
2016-04-21 17:28:37 +00:00
value = {}
when 14
2023-09-24 05:45:40 +00:00
value["en"] = val if val.present?
2021-07-16 10:42:33 +00:00
anns.text_translations = value.clone
2016-04-21 17:28:37 +00:00
when 15
2023-09-24 05:45:40 +00:00
value["zh_tw"] = val if val.present?
2021-07-16 10:42:33 +00:00
anns.text_translations = value.clone
2016-04-21 17:28:37 +00:00
value = {}
when 16
links = val.split(";") rescue []
desc_en = row.cells[17].value.split(";") rescue []
desc_zh_tw = row.cells[18].value.split(";") rescue []
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(";") rescue []
desc_en = row.cells[20].value.split(";") rescue []
desc_zh_tw = row.cells[21].value.split(";") rescue []
alt_en = row.cells[22].value.split(";") rescue []
alt_zh_tw = row.cells[23].value.split(";") rescue []
files.each_with_index do |file, i|
bf = BulletinFile.new
bf.remote_file_url = file.strip rescue nil
2021-08-09 04:23:55 +00:00
bf.title_translations = {"en" => (alt_en[i] rescue ""), "zh_tw" => (alt_zh_tw[i] rescue "")}
bf.description_translations = {"en" => (desc_en[i] rescue ""), "zh_tw" => (desc_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
2020-05-04 15:14:02 +00:00
def send_rejection_email(announcement,locale)
user = User.find(announcement.create_user_id) rescue nil
if !user.nil?
email = user.member_profile.email
if !email.nil? && email != ""
url = "http://#{request.host_with_port}/admin/announcements/#{announcement.id}/edit"
2016-08-02 10:41:29 +00:00
datatosend = "<h3>Hello #{user.name},</h3><p>#{current_user.name} #{t("announcement.rejected_annoucement")} : #{announcement.rejection_reason} <a href='#{url}'> #{t("announcement.click_here_to_see")}</a></p>"
2020-05-04 15:14:02 +00:00
mail = Email.new(:mail_to => email, :mail_subject => "Announcement rejected公告未通過 : #{announcement.title_translations[locale]}.", :template => "email/announcement_email.html.erb", :template_data => {"html" => datatosend})
2020-03-22 13:47:21 +00:00
mail.save
mail.deliver rescue nil
end
end
end
2020-05-04 15:14:02 +00:00
def send_notification_mail_to_managers(announcement, type, locale)
users = []
if @announcement_setting.email_to.include?("managers")
authorizations = Authorization.where(:module_app_id => @module_app.id)
users = authorizations.collect do |auth|
auth.user
end
end
if @announcement_setting.email_to.include?("admins")
wg = Workgroup.where(:key => "admin").first
admins = User.where(:workgroup_id => wg.id)
users.delete(nil)
users = users.concat(admins.to_a)
end
if @announcement_setting.email_to.include?("approvers")
approvers = User.find(@announcement_setting.approvers).to_a rescue []
auths = Authorization.where(:category_id => announcement.category_id).collect{|a| a.user}
users = users.concat(approvers & auths)
end
users.each do |user|
email = user.member_profile.email
if !email.nil? && email != ""
2020-05-04 15:14:02 +00:00
send_email(user.name, email, announcement, type, locale)
# sleep(1)
end
end
end
2020-05-04 15:14:02 +00:00
def send_email(name, useremail, announcement, type, locale)
url = "http://#{request.host_with_port}/admin/announcements?url=#{page_for_bulletin(announcement).sub("http://" + request.host_with_port, "")}&id=#{announcement.id}"
case type
when "approval"
2020-05-04 15:14:02 +00:00
datatosend = "<h3>#{t("announcement.approval_mail_hi", :name => name)},</h3><p>#{t("announcement.submitted_new_announcement", :poster => current_user.name)}<br /><br />#{t("announcement.approval_announcement_title")} : #{announcement.title_translations[locale]} <br /> #{t("announcement.click_here_to_see")} : <a href='#{url}'> #{url} </a></p>"
when "reapproval"
2020-05-04 15:14:02 +00:00
datatosend = "<h3>#{t("announcement.approval_mail_hi", :name => name)},</h3><p>#{t("announcement.updated_annoucement", :poster => current_user.name)}<br /><br />#{t("announcement.approval_announcement_title")} : #{announcement.title_translations[locale]} <br /> #{t("announcement.click_here_to_see")} : <a href='#{url}'> #{url} </a></p>"
end
2020-05-04 15:14:02 +00:00
email = Email.new(:mail_to => useremail, :mail_subject => " #{t("announcement.announcement_subject")} : #{announcement.title_translations[locale]}.", :template => "email/announcement_email.html.erb", :template_data => {"html" => datatosend})
2020-03-22 13:47:21 +00:00
email.save
email.deliver rescue nil
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
2017-01-26 06:30:59 +00:00
def import_from_wordpress(xmlfile)
xml_file = File.read(xmlfile)
doc = Nokogiri::XML.parse(xml_file)
doc.xpath("//channel").each do|channel_data|
channel_data.xpath('//item').each do|itme|
bu = Bulletin.where(:rss2_sn => itme.xpath('wp:post_id').text ).first rescue nil
if bu.nil?
bu = Bulletin.new
bu.approved = true
bu.rss2_sn = itme.xpath('wp:post_id').text
bu.title_translations = {"en" => itme.xpath('title').text, "zh_tw" => itme.xpath('title').text}
bu.text_translations = {"en" => itme.xpath('content:encoded').text, "zh_tw" => itme.xpath('content:encoded').text}
bu.postdate = itme.xpath('wp:post_date').text
itme.xpath('category').each do |i_cate|
if i_cate["domain"].to_s == "category"
cat = @module_app.categories.where(:title => i_cate.text.to_s).first rescue nil
if cat.nil?
cat = Category.new
cat.module_app = @module_app
cat.title_translations = {"en" => i_cate.text.to_s, "zh_tw" => i_cate.text.to_s}
cat.save
end
bu.category = cat
2017-01-26 06:30:59 +00:00
elsif i_cate["domain"].to_s == "post_tag"
2017-01-26 06:30:59 +00:00
tag = Tag.where(:name => i_cate.text.to_s ).first rescue nil
if tag.nil?
tag = Tag.new
tag.name_translations = {"en" => i_cate.text.to_s, "zh_tw" => i_cate.text.to_s}
tag.module_app_ids << @module_app.id
tag.save
end
2017-01-26 06:30:59 +00:00
bu.tags = tag
end
end
bu.save
end
end
2017-01-26 06:30:59 +00:00
end
File.delete(xmlfile)
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?(anns=nil)
can_approve = false
setting = AnnouncementSetting.first
2014-09-22 08:32:30 +00:00
case @access_level
when "admin"
can_approve = true
2014-09-22 08:32:30 +00:00
when "manager"
can_approve = true
2014-09-22 08:32:30 +00:00
else
can_approve = false
end
if !can_approve
if !anns.nil?
if setting.approvers.include?(current_user.id.to_s)
if (current_user.approved_categories_for_module(@module_app).include?(anns.category) rescue false)
can_approve = true
end
end
else
can_approve = setting.approvers.include?(current_user.id.to_s)
end
2014-09-22 08:32:30 +00:00
end
can_approve
2014-09-22 08:32:30 +00:00
end
2014-04-03 03:18:02 +00:00
end