325 lines
10 KiB
Ruby
325 lines
10 KiB
Ruby
module Admin::NewsHelper
|
|
InUseLocales = (Site.first.in_use_locales rescue [:en,:zh_tw])
|
|
def page_for_news_bulletin(news_bulletin)
|
|
ann_page = nil
|
|
pages = Page.where(:module=>'news')
|
|
|
|
pages.each do |page|
|
|
if page.categories.count ==1
|
|
if page.categories.include?(news_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?(news_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+'/'+news_bulletin.to_param).gsub('//','/') rescue "/"
|
|
end
|
|
|
|
def get_response(uri)
|
|
http = Net::HTTP.new(uri.host, uri.port)
|
|
if uri.scheme == 'https'
|
|
http.use_ssl = true
|
|
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
|
end
|
|
timeout = 5
|
|
http.open_timeout = timeout #set read_timeout to avoid web die caused by no response
|
|
http.ssl_timeout = timeout
|
|
http.read_timeout = 1
|
|
req = Net::HTTP::Get.new(uri.request_uri)
|
|
res = http.request(req)
|
|
if res.code == "301" || res.code == "302"
|
|
location = res['Location']
|
|
cookie = res['Set-Cookie']
|
|
headers = {
|
|
'Cookie' => cookie,
|
|
}
|
|
if location[0] == "/"
|
|
uri = URI.parse("#{uri.scheme}://#{uri.host}#{location}")
|
|
else
|
|
uri = URI.parse(location)
|
|
end
|
|
res = Net::HTTP.get_response(uri,nil,headers)
|
|
end
|
|
return res
|
|
end
|
|
|
|
def restore_remote_file(uri,save_path)
|
|
response = get_response(uri)
|
|
uri_params = CGI.parse(response['Content-Disposition']) rescue {}
|
|
if save_path[-1] == "/"
|
|
filename = ""
|
|
if uri_params["filename*"].present?
|
|
filename = uri_params["filename*"][0].split("'").last rescue ""
|
|
end
|
|
if uri_params["filename"].present? && filename.blank?
|
|
filename = uri_params["filename"][0].to_s.gsub(/[\'\"]/,'') rescue ""
|
|
end
|
|
if filename.blank?
|
|
filename = response.uri.to_s.split('/').last.split('?').first
|
|
end
|
|
save_path = save_path + filename
|
|
end
|
|
if File.exist?(save_path)
|
|
File.open(save_path,"w+b"){|f| f.write(response.body)}
|
|
else
|
|
FileUtils.mkdir_p(File.dirname(save_path))
|
|
File.open(save_path,"w+b"){|f| f.write(response.body)}
|
|
end
|
|
return save_path
|
|
end
|
|
|
|
def localize_data(data)
|
|
return InUseLocales.map{|locale| [locale, data] }.to_h
|
|
end
|
|
|
|
def import_this_news(row,categories,tags)
|
|
value = {}
|
|
anns = NewsBulletin.new
|
|
row.cells.each_with_index do |cell,index|
|
|
val = cell.nil? ? nil : cell.value rescue nil
|
|
case index
|
|
when 0
|
|
anns.category = categories[val.to_s.strip]
|
|
when 1
|
|
val = val.to_s
|
|
new_tags = []
|
|
if (val.include?(",") rescue false)
|
|
ts = val.split(",")
|
|
ts.each do |t|
|
|
new_tags << tags[t.strip]
|
|
end
|
|
else
|
|
new_tags << tags[val.strip]
|
|
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 if val.present?
|
|
anns.image_description_translations = value.clone
|
|
when 9
|
|
value["zh_tw"] = val if val.present?
|
|
anns.image_description_translations = value.clone
|
|
value = {}
|
|
when 10
|
|
value["en"] = val if val.present?
|
|
anns.title_translations = value.clone
|
|
when 11
|
|
value["zh_tw"] = val if val.present?
|
|
anns.title_translations = value.clone
|
|
value = {}
|
|
when 12
|
|
value["en"] = val if val.present?
|
|
anns.subtitle_translations = value.clone
|
|
when 13
|
|
value["zh_tw"] = val if val.present?
|
|
anns.subtitle_translations = value.clone
|
|
value = {}
|
|
when 14, 15
|
|
if index == 14
|
|
tmp_locale = 'en'
|
|
else
|
|
tmp_locale = 'zh_tw'
|
|
end
|
|
if val.present?
|
|
doc = Nokogiri::HTML.fragment(val)
|
|
doc.css('[src]').each do |el|
|
|
src = el.attributes['src'].value.to_s rescue ""
|
|
if src.present?
|
|
a = Asset.new
|
|
uri = URI.parse(URI.encode(src))
|
|
uri_params = CGI::parse(uri.query) rescue {}
|
|
if uri.query.present?
|
|
filename = uri_params["title"][0] + (File.extname(uri_params["filename"][0])) rescue ""
|
|
else
|
|
filename = src.split("/").last
|
|
end
|
|
save_path = "public/" + a.data.store_path + filename
|
|
begin
|
|
save_path = restore_remote_file(uri,save_path)
|
|
rescue
|
|
next
|
|
end
|
|
filename = File.basename(save_path)
|
|
a["data"] = URI.decode(filename)
|
|
a.title_translations = localize_data(filename)
|
|
if (a.save! rescue false)
|
|
el.attributes['src'].value = URI.decode(a.data.url)
|
|
end
|
|
end
|
|
end
|
|
doc.css('[href]').each do |el|
|
|
href = el.attributes['href'].value.to_s rescue ""
|
|
if href.present?
|
|
a = Asset.new
|
|
uri = URI.parse(URI.encode(href))
|
|
uri_params = CGI::parse(uri.query) rescue {}
|
|
if uri.query.present?
|
|
filename = uri_params["title"][0] + (File.extname(uri_params["filename"][0])) rescue ""
|
|
else
|
|
filename = href.split("/").last
|
|
end
|
|
save_path = "public/" + a.data.store_path + filename
|
|
begin
|
|
save_path = restore_remote_file(uri,save_path)
|
|
rescue => e
|
|
puts [e.to_s, e.backtrace[0..10].to_s]
|
|
next
|
|
end
|
|
filename = File.basename(save_path)
|
|
a["data"] = URI.decode(filename)
|
|
a.title_translations = localize_data(filename)
|
|
if (a.save! rescue false)
|
|
el.attributes['href'].value = URI.decode(a.data.url)
|
|
end
|
|
end
|
|
end
|
|
value[tmp_locale] = doc.to_s
|
|
end
|
|
anns.text_translations = value.clone
|
|
if index == 15
|
|
value = {}
|
|
end
|
|
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 = NewsBulletinLink.new
|
|
bl.url = link.strip
|
|
bl.title_translations = {"en" => desc_en[i], "zh_tw" => desc_zh_tw[i]}
|
|
bl.news_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 = NewsBulletinFile.new
|
|
bf.remote_file_url = file.strip rescue nil
|
|
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.news_bulletin_id = anns.id
|
|
bf.save
|
|
end
|
|
when 24
|
|
if val.present?
|
|
tmp = val.split("-", 2).map{|v| v.strip}
|
|
unit_text = tmp[0]
|
|
dept_text = tmp[1]
|
|
if unit_text
|
|
unit = Unit.where(:name=>/\s*#{unit_text}\s*/).first
|
|
if unit.nil?
|
|
unit = Unit.create(:name_translations=>localize_data(unit_text))
|
|
end
|
|
else
|
|
unit = nil
|
|
end
|
|
if unit && dept_text
|
|
dept = unit.departments.where(:name=>/\s*#{dept_text}\s*/).first
|
|
if dept.nil?
|
|
dept = unit.departments.create(:name_translations=>localize_data(dept_text))
|
|
end
|
|
else
|
|
dept = nil
|
|
end
|
|
anns.unit = unit
|
|
anns.department = dept
|
|
end
|
|
when 25
|
|
anns.view_count = (val.to_i rescue 0)
|
|
end
|
|
end
|
|
current_user_id = current_user.id
|
|
anns.create_user_id = current_user_id
|
|
anns.update_user_id = current_user_id
|
|
anns.approved = true
|
|
anns.save
|
|
end
|
|
|
|
def send_rejection_email(news)
|
|
user = User.find(news.create_user_id) rescue nil
|
|
if !user.nil?
|
|
email = user.member_profile.email
|
|
if !email.nil? && email != ""
|
|
url = page_for_news_bulletin(news)
|
|
mail = Email.new(:mail_to => email, :mail_subject => "News rejected : #{news.title}.", :template => "email/rejection_email.html.erb", :template_data => {"url" => url, "rejector" => current_user.name, "name" => user.name, "reason" => news.rejection_reason})
|
|
mail.deliver
|
|
end
|
|
end
|
|
end
|
|
|
|
def send_notification_mail_to_managers(news, type)
|
|
authorizations = Authorization.where(:module_app_id => @module_app.id)
|
|
users = authorizations.collect do |auth|
|
|
auth.user
|
|
end
|
|
users.each do |user|
|
|
email = user.member_profile.email
|
|
if !email.nil? && email != ""
|
|
send_email(user.name, email, news, type)
|
|
sleep(2)
|
|
end
|
|
end
|
|
users = Workgroup.where(:key => "admin").first.users rescue []
|
|
users.each do |user|
|
|
email = user.member_profile.email
|
|
if !email.nil? && email != ""
|
|
send_email(user.name, email, news, type)
|
|
sleep(2)
|
|
end
|
|
end
|
|
end
|
|
|
|
def send_email(name, useremail, news, type)
|
|
url = page_for_news_bulletin(news)
|
|
template = (type == "approval" ? "email/new_news_email.html.erb" : "email/reapproval_news_email.html.erb")
|
|
email = Email.new(:mail_to => useremail, :mail_subject => "校園新聞請審核通知", :template => template, :template_data => {"url" => url, "submitter" => current_user.name, "name" => name})
|
|
email.deliver
|
|
end
|
|
|
|
def load_access_level
|
|
if current_user.is_admin?
|
|
@access_level = "admin"
|
|
elsif current_user.is_manager?(@module_app)
|
|
@access_level = "manager"
|
|
end
|
|
end
|
|
|
|
def user_can_approve?
|
|
case @access_level
|
|
when "admin"
|
|
return true
|
|
when "manager"
|
|
return true
|
|
else
|
|
return false
|
|
end
|
|
end
|
|
|
|
end
|