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| val = cell.nil? ? nil : 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 anns.image_description_translations = value when 10 value["zh_tw"] = val anns.image_description_translations = value value = {} when 11 value["en"] = val anns.title_translations = value when 12 value["zh_tw"] = val anns.title_translations = value value = {} when 13 value["en"] = val anns.speaker_translations = value when 14 value["zh_tw"] = val anns.speaker_translations = value value = {} when 15 value["en"] = val anns.host_translations = value when 16 value["zh_tw"] = val anns.host_translations = value value = {} when 17 value["en"] = val anns.subtitle_translations = value when 18 value["zh_tw"] = val anns.subtitle_translations = value value = {} when 19 value["en"] = val anns.text_translations = value when 20 value["zh_tw"] = val anns.text_translations = value value = {} when 21 value["en"] = val anns.notes_translations = value 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 when 31 value["en"] = val anns.place_translations = value when 32 value["zh_tw"] = val anns.place_translations = value value = {} 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 = "

Hello #{user.name},

#{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.approval_mail_hi", :name => name)},

#{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}

" when "reapproval" datatosend = "

#{t("event_news.approval_mail_hi", :name => name)},

#{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}

" end email = Email.new(:mail_to => useremail, :mail_subject => " #{t("event_news.event_news_subject")} : #{event_news.title_translations[locale]}.", :template => "email/event_news_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_news = [] xml.xpath("//channel").xpath("//item").each do |anns| event_news << { :title => (anns>"title").text, :category => (anns>"category").text, :postdate => (anns>"pubDate").text, :text => (anns>"description").text, :rss2_sn => (anns>"link").text.split("=").last } end event_news.each do |anns| ma = ModuleApp.where(:key => "event_news").first cat = Category.where(:title => anns[:category]).first rescue nil if cat.nil? cat = Category.create(:title_translations => {"en" => anns[:category], "zh_tw" => anns[:category]}, :module_app_id => ma.id) end ann = EventNews.where(:rss2_sn => anns[:rss2_sn]).first rescue nil if ann.nil? ann = EventNews.new(:title_translations => {"en" => "", "zh_tw" => anns[:title]}, :postdate => anns[:postdate], :subtitle_translations => {"en" => "", "zh_tw" => anns[:title]}, :text_translations => {"en" => "", "zh_tw" => anns[:text]}, :rss2_sn => anns[:rss2_sn], :category_id => cat.id, :approved => true, :create_user_id => current_user.id) else ann.update_attributes(:title_translations => {"en" => "", "zh_tw" => anns[:title]}, :postdate => anns[:postdate], :subtitle_translations => {"en" => "", "zh_tw" => anns[:title]}, :text_translations => {"en" => "", "zh_tw" => anns[: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 = EventNews.where(:rss2_sn => itme.xpath('wp:post_id').text ).first rescue nil if bu.nil? bu = EventNews.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?(anns=nil) can_approve = false setting = EventNewsSetting.first case @access_level when "admin" can_approve = true when "manager" can_approve = true else can_approve = false end if !can_approve if !anns.nil? if setting.approvers.include?(current_user.id.to_s) if (current_user.approved_categories_for_module(@module_app).include?(anns.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