require "net/http" require "uri" require 'json' module Admin::EventNewsHelper def page_for_event_news(event_news) ann_page = nil pages = Page.where(:module=>'event_news') pages.each do |page| if page.categories.count ==1 if page.categories.include?(event_news.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?(event_news.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+'/'+event_news.to_param).gsub('//','/') rescue "/" end def import_this_event_news(row,categories,tags) value = {} anns = EventNews.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.event_date = val when 3 anns.postdate = val when 4 anns.deadline = val when 5 anns.is_top = (val.to_i == 1 ? true : false) when 6 anns.is_hot = (val.to_i == 1 ? true : false) when 7 anns.is_hidden = (val.to_i == 1 ? true : false) when 8 anns.remote_image_url = val when 9 value["en"] = val when 10 value["zh_tw"] = val anns.image_description_translations = value value = {} when 11 value["en"] = val when 12 value["zh_tw"] = val anns.title_translations = value value = {} when 13 value["en"] = val when 14 value["zh_tw"] = val anns.speaker_translations = value value = {} when 15 value["en"] = val when 16 value["zh_tw"] = val anns.host_translations = value value = {} when 17 value["en"] = val when 18 value["zh_tw"] = val anns.subtitle_translations = value value = {} when 19 value["en"] = val when 20 value["zh_tw"] = val anns.text_translations = value value = {} when 21 value["en"] = val when 22 value["zh_tw"] = val anns.notes_translations = value value = {} when 23 links = val.split(";") rescue [] desc_en = row.cells[24].value.split(";") rescue [] desc_zh_tw = row.cells[25].value.split(";") rescue [] links.each_with_index do |link,i| bl = EventNewsLink.new bl.url = link.strip bl.title_translations = {"en" => desc_en[i], "zh_tw" => desc_zh_tw[i]} bl.event_news_id = anns.id bl.save end when 26 files = val.split(";") rescue [] desc_en = row.cells[27].value.split(";") rescue [] desc_zh_tw = row.cells[28].value.split(";") rescue [] alt_en = row.cells[29].value.split(";") rescue [] alt_zh_tw = row.cells[30].value.split(";") rescue [] files.each_with_index do |file, i| bf = EventNewsFile.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.event_news_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(event_news,locale) user = User.find(event_news.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_news/#{event_news.id}/edit" datatosend = "
#{current_user.name} #{t("event_news.rejected_event_news")} : #{event_news.rejection_reason} #{t("event_news.click_here_to_see")}
" mail = Email.new(:mail_to => email, :mail_subject => "EventNews rejected公告未通過 : #{event_news.title_translations[locale]}.", :template => "email/event_news_email.html.erb", :template_data => {"html" => datatosend}) mail.save mail.deliver rescue nil end end end def send_notification_mail_to_managers(event_news, type, locale) users = [] if @event_news_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_news_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_news_setting.email_to.include?("approvers") approvers = User.find(@event_news_setting.approvers).to_a rescue [] auths = Authorization.where(:category_id => event_news.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_news, type, locale) # sleep(1) end end end def send_email(name, useremail, event_news, type, locale) url = "http://#{request.host_with_port}/admin/event_news?url=#{page_for_event_news(event_news).sub("http://" + request.host_with_port, "")}&id=#{event_news.id}" case type when "approval" datatosend = "#{t("event_news.submitted_new_event_news", :poster => current_user.name)}
#{t("event_news.approval_event_news_title")} : #{event_news.title_translations[locale]}
#{t("event_news.click_here_to_see")} : #{url}
#{t("event_news.updated_event_news", :poster => current_user.name)}
#{t("event_news.approval_event_news_title")} : #{event_news.title_translations[locale]}
#{t("event_news.click_here_to_see")} : #{url}