319 lines
11 KiB
Ruby
319 lines
11 KiB
Ruby
|
require "net/http"
|
||
|
require "uri"
|
||
|
require 'json'
|
||
|
|
||
|
module Admin::EventAnnsHelper
|
||
|
def page_for_bulletin(bulletin_event)
|
||
|
ann_page = nil
|
||
|
pages = Page.where(:module=>'event_ann')
|
||
|
|
||
|
pages.each do |page|
|
||
|
if page.categories.count ==1
|
||
|
if page.categories.include?(bulletin_event.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_event.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_event.to_param).gsub('//','/') rescue "/"
|
||
|
end
|
||
|
|
||
|
def import_this_event_ann(row,categories,tags)
|
||
|
value = {}
|
||
|
eanns = BulletinEvent.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
|
||
|
eanns.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
|
||
|
eanns.tags=new_tags
|
||
|
when 2
|
||
|
eanns.postdate = val
|
||
|
when 3
|
||
|
eanns.deadline = val
|
||
|
when 4
|
||
|
eanns.is_top = (val.to_i == 1 ? true : false)
|
||
|
when 5
|
||
|
eanns.is_hot = (val.to_i == 1 ? true : false)
|
||
|
when 6
|
||
|
eanns.is_hidden = (val.to_i == 1 ? true : false)
|
||
|
when 7
|
||
|
eanns.remote_image_url = val
|
||
|
when 8
|
||
|
value["en"] = val
|
||
|
when 9
|
||
|
value["zh_tw"] = val
|
||
|
eanns.image_description_translations = value
|
||
|
value = {}
|
||
|
when 10
|
||
|
value["en"] = val
|
||
|
when 11
|
||
|
value["zh_tw"] = val
|
||
|
eanns.title_translations = value
|
||
|
value = {}
|
||
|
when 12
|
||
|
value["en"] = val
|
||
|
when 13
|
||
|
value["zh_tw"] = val
|
||
|
eanns.subtitle_translations = value
|
||
|
value = {}
|
||
|
when 14
|
||
|
value["en"] = val
|
||
|
when 15
|
||
|
value["zh_tw"] = val
|
||
|
eanns.speaker_translations = value
|
||
|
value = {}
|
||
|
when 16
|
||
|
value["en"] = val
|
||
|
when 17
|
||
|
value["zh_tw"] = val
|
||
|
eanns.host_translations = value
|
||
|
value = {}
|
||
|
when 18
|
||
|
value["en"] = val
|
||
|
when 19
|
||
|
value["zh_tw"] = val
|
||
|
eanns.text_translations = value
|
||
|
value = {}
|
||
|
when 20
|
||
|
value["zh_tw"] = val
|
||
|
eanns.notes_translations = value
|
||
|
value = {}
|
||
|
when 21
|
||
|
value["en"] = val
|
||
|
when 22
|
||
|
links = val.split(";") rescue []
|
||
|
desc_en = row.cells[23].value.split(";") rescue []
|
||
|
desc_zh_tw = row.cells[24].value.split(";") rescue []
|
||
|
links.each_with_index do |link,i|
|
||
|
bl = BulletinLinkEvent.new
|
||
|
bl.url = link.strip
|
||
|
bl.title_translations = {"en" => desc_en[i], "zh_tw" => desc_zh_tw[i]}
|
||
|
bl.bulletin_id = eanns.id
|
||
|
bl.save
|
||
|
end
|
||
|
when 25
|
||
|
files = val.split(";") rescue []
|
||
|
desc_en = row.cells[26].value.split(";") rescue []
|
||
|
desc_zh_tw = row.cells[27].value.split(";") rescue []
|
||
|
alt_en = row.cells[28].value.split(";") rescue []
|
||
|
alt_zh_tw = row.cells[29].value.split(";") rescue []
|
||
|
files.each_with_index do |file, i|
|
||
|
bf = BulletinFileEvent.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 = eanns.id
|
||
|
bf.save
|
||
|
end
|
||
|
end
|
||
|
end
|
||
|
eanns.create_user_id = current_user.id.to_s
|
||
|
eanns.update_user_id = current_user.id.to_s
|
||
|
eanns.approved = true
|
||
|
eanns.save
|
||
|
end
|
||
|
|
||
|
def send_rejection_email(event_ann,locale)
|
||
|
user = User.find(event_ann.create_user_id) rescue nil
|
||
|
if !user.nil?
|
||
|
email = user.member_profile.email
|
||
|
if !email.nil? && email != ""
|
||
|
url = "http://#{request.host_with_port}/admin/event_anns/#{event_ann.id}/edit"
|
||
|
datatosend = "<h3>Hello #{user.name},</h3><p>#{current_user.name} #{t("event_ann.rejected_annoucement")} : #{event_ann.rejection_reason} <a href='#{url}'> #{t("event_ann.click_here_to_see")}</a></p>"
|
||
|
mail = Email.new(:mail_to => email, :mail_subject => "EventAnn rejected活動與演講公告未通過 : #{event_ann.title_translations[locale]}.", :template => "email/event_ann_email.html.erb", :template_data => {"html" => datatosend})
|
||
|
mail.save
|
||
|
mail.deliver rescue nil
|
||
|
end
|
||
|
end
|
||
|
end
|
||
|
|
||
|
def send_notification_mail_to_managers(event_ann, type, locale)
|
||
|
users = []
|
||
|
if @event_ann_setting.email_to.include?("managers")
|
||
|
authorizations = Authorization.where(:module_app_id => @module_app.id)
|
||
|
users = authorizations.collect do |auth|
|
||
|
auth.user
|
||
|
end
|
||
|
end
|
||
|
if @event_ann_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 @event_ann_setting.email_to.include?("approvers")
|
||
|
approvers = User.find(@event_ann_setting.approvers).to_a rescue []
|
||
|
auths = Authorization.where(:category_id => event_ann.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 != ""
|
||
|
send_email(user.name, email, event_ann, type, locale)
|
||
|
# sleep(1)
|
||
|
end
|
||
|
end
|
||
|
end
|
||
|
|
||
|
def send_email(name, useremail, event_ann, type, locale)
|
||
|
url = "http://#{request.host_with_port}/admin/event_anns?url=#{page_for_bulletin(event_ann).sub("http://" + request.host_with_port, "")}&id=#{event_ann.id}"
|
||
|
|
||
|
case type
|
||
|
when "approval"
|
||
|
datatosend = "<h3>#{t("event_ann.approval_mail_hi", :name => name)},</h3><p>#{t("event_ann.submitted_new_event_ann", :poster => current_user.name)}<br /><br />#{t("event_ann.approval_event_ann_title")} : #{event_ann.title_translations[locale]} <br /> #{t("event_ann.click_here_to_see")} : <a href='#{url}'> #{url} </a></p>"
|
||
|
when "reapproval"
|
||
|
datatosend = "<h3>#{t("event_ann.approval_mail_hi", :name => name)},</h3><p>#{t("event_ann.updated_annoucement", :poster => current_user.name)}<br /><br />#{t("event_ann.approval_event_ann_title")} : #{event_ann.title_translations[locale]} <br /> #{t("event_ann.click_here_to_see")} : <a href='#{url}'> #{url} </a></p>"
|
||
|
end
|
||
|
email = Email.new(:mail_to => useremail, :mail_subject => " #{t("event_ann.event_ann_subject")} : #{event_ann.title_translations[locale]}.", :template => "email/event_ann_email.html.erb", :template_data => {"html" => datatosend})
|
||
|
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?
|
||
|
event_anns = []
|
||
|
xml.xpath("//channel").xpath("//item").each do |eanns|
|
||
|
event_anns << {
|
||
|
:title => (eanns>"title").text,
|
||
|
:category => (eanns>"category").text,
|
||
|
:postdate => (eanns>"pubDate").text,
|
||
|
:text => (eanns>"description").text,
|
||
|
:rss2_sn => (eanns>"link").text.split("=").last
|
||
|
}
|
||
|
end
|
||
|
event_anns.each do |eanns|
|
||
|
ma = ModuleApp.where(:key => "event_ann").first
|
||
|
cat = Category.where(:title => eanns[:category]).first rescue nil
|
||
|
if cat.nil?
|
||
|
cat = Category.create(:title_translations => {"en" => eanns[:category], "zh_tw" => eanns[:category]}, :module_app_id => ma.id)
|
||
|
end
|
||
|
ann = BulletinEvent.where(:rss2_sn => eanns[:rss2_sn]).first rescue nil
|
||
|
if ann.nil?
|
||
|
ann = BulletinEvent.new(:title_translations => {"en" => "", "zh_tw" => eanns[:title]}, :postdate => eanns[:postdate], :subtitle_translations => {"en" => "", "zh_tw" => eanns[:title]}, :text_translations => {"en" => "", "zh_tw" => eanns[:text]}, :rss2_sn => eanns[:rss2_sn], :category_id => cat.id, :approved => true, :create_user_id => current_user.id)
|
||
|
else
|
||
|
ann.update_attributes(:title_translations => {"en" => "", "zh_tw" => eanns[:title]}, :postdate => eanns[:postdate], :subtitle_translations => {"en" => "", "zh_tw" => eanns[:title]}, :text_translations => {"en" => "", "zh_tw" => eanns[:text]})
|
||
|
end
|
||
|
ann.save
|
||
|
end
|
||
|
File.delete(file)
|
||
|
end
|
||
|
|
||
|
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 = BulletinEvent.where(:rss2_sn => itme.xpath('wp:post_id').text ).first rescue nil
|
||
|
if bu.nil?
|
||
|
bu = BulletinEvent.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
|
||
|
|
||
|
elsif i_cate["domain"].to_s == "post_tag"
|
||
|
|
||
|
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
|
||
|
|
||
|
bu.tags = tag
|
||
|
end
|
||
|
end
|
||
|
|
||
|
bu.save
|
||
|
end
|
||
|
|
||
|
end
|
||
|
end
|
||
|
File.delete(xmlfile)
|
||
|
end
|
||
|
|
||
|
def load_access_level
|
||
|
if (current_user.is_admin? rescue false)
|
||
|
@access_level = "admin"
|
||
|
elsif (current_user.is_manager?(@module_app) rescue false)
|
||
|
@access_level = "manager"
|
||
|
else
|
||
|
@access_level = "users"
|
||
|
end
|
||
|
end
|
||
|
|
||
|
def user_can_approve?(eanns=nil)
|
||
|
can_approve = false
|
||
|
setting = EventAnnSetting.first
|
||
|
case @access_level
|
||
|
when "admin"
|
||
|
can_approve = true
|
||
|
when "manager"
|
||
|
can_approve = true
|
||
|
else
|
||
|
can_approve = false
|
||
|
end
|
||
|
if !can_approve
|
||
|
if !eanns.nil?
|
||
|
if setting.approvers.include?(current_user.id.to_s)
|
||
|
if (current_user.approved_categories_for_module(@module_app).include?(eanns.category) rescue false)
|
||
|
can_approve = true
|
||
|
end
|
||
|
end
|
||
|
else
|
||
|
can_approve = setting.approvers.include?(current_user.id.to_s)
|
||
|
end
|
||
|
end
|
||
|
can_approve
|
||
|
end
|
||
|
|
||
|
end
|