require "net/http" require "uri" require 'json' module Admin::AnnouncementsHelper def page_for_bulletin(bulletin) 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)} pages.each do |page| if page.categories.count ==1 if (page.categories.include?(bulletin.category.id.to_s) rescue false) 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) rescue false) 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 "#" else bulletin.external_link end end def import_this_announcement(row,categories,tags) value = {} anns = Bulletin.new row.cells.each_with_index do |cell,index| val = cell.nil? ? nil : cell.value rescue nil if [8,9,10,11,12,13,14,15].exclude?(index) next if val.blank? end 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 anns.image_description_translations = value.clone when 9 value["zh_tw"] = val anns.image_description_translations = value.clone value = {} when 10 value["en"] = val anns.title_translations = value.clone when 11 value["zh_tw"] = val anns.title_translations = value.clone value = {} when 12 value["en"] = val anns.subtitle_translations = value.clone when 13 value["zh_tw"] = val anns.subtitle_translations = value.clone value = {} when 14 value["en"] = val anns.text_translations = value.clone when 15 value["zh_tw"] = val anns.text_translations = value.clone 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 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 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 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" datatosend = "
#{current_user.name} #{t("announcement.rejected_annoucement")} : #{announcement.rejection_reason} #{t("announcement.click_here_to_see")}
" mail = Email.new(:mail_to => email, :mail_subject => "Announcement rejected公告未通過 : #{announcement.title_translations[locale]}.", :template => "email/announcement_email.html.erb", :template_data => {"html" => datatosend}) mail.save mail.deliver rescue nil end end end 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 != "" send_email(user.name, email, announcement, type, locale) # sleep(1) end end end 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" datatosend = "#{t("announcement.submitted_new_announcement", :poster => current_user.name)}
#{t("announcement.approval_announcement_title")} : #{announcement.title_translations[locale]}
#{t("announcement.click_here_to_see")} : #{url}
#{t("announcement.updated_annoucement", :poster => current_user.name)}
#{t("announcement.approval_announcement_title")} : #{announcement.title_translations[locale]}
#{t("announcement.click_here_to_see")} : #{url}