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 = "
#{current_user.name} #{t("event_ann.rejected_annoucement")} : #{event_ann.rejection_reason} #{t("event_ann.click_here_to_see")}
" 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 = "#{t("event_ann.submitted_new_event_ann", :poster => current_user.name)}
#{t("event_ann.approval_event_ann_title")} : #{event_ann.title_translations[locale]}
#{t("event_ann.click_here_to_see")} : #{url}
#{t("event_ann.updated_annoucement", :poster => current_user.name)}
#{t("event_ann.approval_event_ann_title")} : #{event_ann.title_translations[locale]}
#{t("event_ann.click_here_to_see")} : #{url}