366 lines
		
	
	
		
			15 KiB
		
	
	
	
		
			Ruby
		
	
	
	
			
		
		
	
	
			366 lines
		
	
	
		
			15 KiB
		
	
	
	
		
			Ruby
		
	
	
	
class AnnouncementsController < ApplicationController
 | 
						|
  include AnnouncementsHelper
 | 
						|
  def index
 | 
						|
    Bulletin.remove_expired_status
 | 
						|
    announcements,sorted = get_sorted_annc
 | 
						|
    total_pages = sorted.total_pages
 | 
						|
 | 
						|
    anns = []
 | 
						|
    sorted.each do |a|
 | 
						|
      if !a["source-site"].present?
 | 
						|
        statuses = a.statuses_with_classname.collect do |status|
 | 
						|
          {
 | 
						|
            "status" => status["name"],
 | 
						|
            "status-class" => "status-#{status['classname']}"
 | 
						|
          }
 | 
						|
        end
 | 
						|
        files = a.bulletin_files.map{|file|  { "file_url" =>  file.file.url, "file_title" => (file.title.blank? ? File.basename(file.file.path) : file.title rescue '') } if file.enabled_for?(locale) } rescue []
 | 
						|
        files.delete(nil)
 | 
						|
        links = a.bulletin_links.map{|link|  { "link_url" => link.url, "link_title" => (link.title.blank? ? link.url : link.title) } } rescue []
 | 
						|
        author = User.find(a.create_user_id).member_profile.name rescue ""
 | 
						|
        desc = a.image_description
 | 
						|
        desc = (desc.nil? || desc == "" ? "announcement image" : desc)
 | 
						|
        link_to_show = a.is_external_link ? a.external_link :  OrbitHelper.url_to_show(a.to_param)
 | 
						|
        target = a.is_external_link ? "_blank" : "_self"
 | 
						|
        doc = Nokogiri::HTML(a.title)
 | 
						|
        title = doc.text.empty? ? 'no content' : doc.text
 | 
						|
        anns << {
 | 
						|
          "bulletin_links" => links,
 | 
						|
          "bulletin_files" => files,
 | 
						|
          "title" => a.title,
 | 
						|
          "source-site" => "",
 | 
						|
          "source-site-title" => "",
 | 
						|
          "source-site-link" => "",
 | 
						|
          "subtitle" => a.subtitle,
 | 
						|
          "statuses" => statuses,
 | 
						|
          "category" => a.category.title,
 | 
						|
          "postdate" => a.postdate,
 | 
						|
          "author" => author,
 | 
						|
          "is_top" => (a.is_top? ? 1 : 0),
 | 
						|
          "link_to_show" => link_to_show+"\" title=\"#{title}\"",
 | 
						|
          "target" => target,
 | 
						|
          "img_src" => a.image.thumb.url || "/assets/announcement-default.jpg",
 | 
						|
          "img_description" => desc,
 | 
						|
          "more" => t(:more_plus),
 | 
						|
          "view_count" => a.view_count
 | 
						|
        }
 | 
						|
      else
 | 
						|
        anns << a
 | 
						|
      end
 | 
						|
    end
 | 
						|
    #If no data , hide title&table
 | 
						|
    if announcements.count == 0
 | 
						|
      display = "hide"
 | 
						|
    end
 | 
						|
    # anns = anns.concat(feeds_anns)
 | 
						|
    # total_pages = announcements.total_pages
 | 
						|
 | 
						|
 | 
						|
    {
 | 
						|
      "announcements" => anns,
 | 
						|
      "extras" => {
 | 
						|
        "widget-title" =>t('announcement.announcement'),
 | 
						|
        "title-head" => t('announcement.table.title'),
 | 
						|
        "date-head" => t('announcement.table.date'),
 | 
						|
        "status-head" => t('announcement.table.status'),
 | 
						|
        "author-head" => t('announcement.table.author'),
 | 
						|
        "subtitle-head" => t('announcement.table.sub_title'),
 | 
						|
        "category-head" => t('announcement.table.category'),
 | 
						|
        "link-head" => t('announcement.table.link'),
 | 
						|
        "file-head" => t('announcement.table.file'),
 | 
						|
        "view-count-head" => t('announcement.table.view_count'),
 | 
						|
        "display" => display
 | 
						|
      },
 | 
						|
      "total_pages" => total_pages
 | 
						|
    }
 | 
						|
 | 
						|
  end
 | 
						|
 | 
						|
  def random_announcement_widget
 | 
						|
    uid = OrbitHelper.params[:uid] rescue ""
 | 
						|
    tags = OrbitHelper.widget_tags
 | 
						|
    categories = OrbitHelper.widget_categories || []
 | 
						|
    announcements = Bulletin.where(:title.ne => "",:is_preview.in=>[false,nil],:uid.ne => uid, :category_id.in => categories).and(:title.ne => nil).can_display.is_approved.filter_by_tags(tags).sample(OrbitHelper.widget_data_count)
 | 
						|
 | 
						|
    pack_data(announcements, [])
 | 
						|
  end
 | 
						|
 | 
						|
  def widget
 | 
						|
    Bulletin.remove_expired_status
 | 
						|
    uid = OrbitHelper.params[:uid] rescue ""
 | 
						|
    tags = OrbitHelper.widget_tags
 | 
						|
    announcements = Bulletin.where(:title.ne => "",:is_preview.in=>[false,nil],:uid.ne => uid).and(:title.ne => nil, :is_top.ne => true).can_display.is_approved.filter_by_widget_categories([],false).filter_by_tags(tags).to_a
 | 
						|
    top_anns = Bulletin.where(:title.ne => "",:is_preview.in=>[false,nil], :is_top => true, :uid.ne => uid).and(:title.ne => nil).can_display.is_approved.filter_by_widget_categories([],false).filter_by_tags(tags).to_a
 | 
						|
    pack_data(announcements, top_anns)
 | 
						|
  end
 | 
						|
 | 
						|
  def tag_cloud
 | 
						|
    ma = ModuleApp.where(:key => "announcement").first
 | 
						|
    temp = []
 | 
						|
    ma.tags.each do |tag|
 | 
						|
      t1 = tag.taggings.collect{|t| t.taggable_id.to_s}
 | 
						|
      count = Bulletin.where(:id.in => t1).can_display.count
 | 
						|
      temp << {
 | 
						|
        "tag-name" => tag.name,
 | 
						|
        "count" => count,
 | 
						|
        "tag-url" => OrbitHelper.widget_more_url + "?tags[]=" + tag.id.to_s
 | 
						|
      }
 | 
						|
    end
 | 
						|
    max = temp.max_by{|t| t["count"]}["count"]
 | 
						|
    tags = []
 | 
						|
    temp.each do |tag|
 | 
						|
      if tag["count"] > 0
 | 
						|
        percent = (tag["count"] * 100) / max
 | 
						|
        font_size = ((percent / 10).round) + 16
 | 
						|
        tag["font-size"] = font_size
 | 
						|
        tags << tag
 | 
						|
      end
 | 
						|
    end
 | 
						|
    {
 | 
						|
      "tags" => tags,
 | 
						|
      "extras" => {}
 | 
						|
    }
 | 
						|
  end
 | 
						|
 | 
						|
  def pack_data(announcements, top_anns = [])
 | 
						|
    page = Page.where(:module => "announcement").first rescue nil
 | 
						|
    subpart = OrbitHelper.get_current_widget
 | 
						|
    @image_version = 'thumb'
 | 
						|
    if subpart.methods.include? 'select_options'.to_sym
 | 
						|
      ModuleApp.all.select{|tmp| tmp.key.to_s=='announcement'}.each do |modile_app|
 | 
						|
        @show_options = modile_app.show_options rescue nil
 | 
						|
      end
 | 
						|
      subpart.select_options.each do |select_option|
 | 
						|
        if !(@show_options.nil?) && select_option.field_name == @show_options.keys.first.to_s
 | 
						|
          value = YAML.load(select_option.value)
 | 
						|
          if value[I18n.locale] == t('announcement.small_size')
 | 
						|
            @image_version = 'thumb'
 | 
						|
          elsif value[I18n.locale] == t('announcement.medium_size')
 | 
						|
            @image_version = 'mobile'
 | 
						|
          elsif value[I18n.locale] == t('announcement.orignal_size')
 | 
						|
            @image_version = 'orignal'
 | 
						|
          end
 | 
						|
        end
 | 
						|
      end
 | 
						|
    end
 | 
						|
    feeds_anns = get_feed_announcements("widget")
 | 
						|
    announcements = announcements + feeds_anns
 | 
						|
    if !feeds_anns.blank?
 | 
						|
      sorted = announcements.sort{ |k,v| v["postdate"] <=> k["postdate"] }
 | 
						|
      sorted = top_anns + sorted
 | 
						|
      sorted = Kaminari.paginate_array(sorted).page(1).per(OrbitHelper.widget_data_count) rescue []
 | 
						|
    else
 | 
						|
      announcements = top_anns + announcements
 | 
						|
      sorted = Kaminari.paginate_array(announcements).page(1).per(OrbitHelper.widget_data_count) rescue []
 | 
						|
    end
 | 
						|
    anns = []
 | 
						|
    sorted.each do |a|
 | 
						|
      if !a["source-site"].present?
 | 
						|
        statuses = a.statuses_with_classname.collect do |status|
 | 
						|
          {
 | 
						|
            "status" => status["name"],
 | 
						|
            "status-class" => "status-#{status['classname']}"
 | 
						|
          }
 | 
						|
        end
 | 
						|
        files = a.bulletin_files.map{|file|  { "file_url" =>  file.file.url, "file_title" => (file.title.blank? ? File.basename(file.file.path) : file.title rescue '') } if file.enabled_for?(locale) } rescue []
 | 
						|
        files.delete(nil)
 | 
						|
        links = a.bulletin_links.map{|link|  { "link_url" => link.url, "link_title" => (link.title.blank? ? link.url : link.title) } } rescue []
 | 
						|
        author = User.find(a.create_user_id).member_profile.name rescue ""
 | 
						|
        desc = a.image_description
 | 
						|
        desc = (desc.nil? || desc == "" ? "announcement image" : desc)
 | 
						|
        link_to_show = a.is_external_link ? a.external_link : OrbitHelper.widget_item_url(a.to_param)
 | 
						|
        target = a.is_external_link ? "_blank" : "_self"
 | 
						|
        if @image_version == 'thumb'
 | 
						|
          image_url = a.image.thumb.url
 | 
						|
        elsif @image_version == 'mobile'
 | 
						|
          image_url = a.image.mobile.url
 | 
						|
        else
 | 
						|
          image_url = a.image.url
 | 
						|
        end
 | 
						|
        anns << {
 | 
						|
          "bulletin_links" => links,
 | 
						|
          "bulletin_files" => files,
 | 
						|
          "title" => a.title,
 | 
						|
          "source-site" => "",
 | 
						|
          "source-site-title" => "",
 | 
						|
          "source-site-link" => "",
 | 
						|
          "subtitle" => a.subtitle,
 | 
						|
          "statuses" => statuses,
 | 
						|
          "category" => a.category.title,
 | 
						|
          "postdate" => a.postdate,
 | 
						|
          "author" => author,
 | 
						|
          "link_to_show" => link_to_show,
 | 
						|
          "target" => target,
 | 
						|
          "img_src" => image_url || "/assets/announcement-default.jpg",
 | 
						|
          "img_description" => desc
 | 
						|
        }
 | 
						|
      else
 | 
						|
        anns << a
 | 
						|
      end
 | 
						|
    end
 | 
						|
    mp = (anns[0]["img_src"] rescue "")
 | 
						|
    mpd = (anns[0]["img_description"] rescue "")
 | 
						|
 | 
						|
    {
 | 
						|
      "announcements" => anns,
 | 
						|
      "extras" => {
 | 
						|
        "more_url"=>OrbitHelper.widget_more_url,
 | 
						|
        "main_picture" => mp,
 | 
						|
        "main_picture_description" => mpd,
 | 
						|
        "title-head" => t('announcement.table.title'),
 | 
						|
        "date-head" => t('announcement.table.date'),
 | 
						|
        "author-head" => t('announcement.table.author'),
 | 
						|
        "status-head" => t('announcement.table.status'),
 | 
						|
        "subtitle-head" => t('announcement.table.sub_title'),
 | 
						|
        "category-head" => t('announcement.table.category'),
 | 
						|
        "link-head" => t('announcement.table.link'),
 | 
						|
        "file-head" => t('announcement.table.file'),
 | 
						|
        "read_more" => ("/#{I18n.locale.to_s + page.url}" rescue "")
 | 
						|
      }
 | 
						|
    }
 | 
						|
  end
 | 
						|
 | 
						|
  def show_local_announcement(uid, is_preview)
 | 
						|
    locale = OrbitHelper.get_site_locale.to_s
 | 
						|
    if is_preview
 | 
						|
      announcement = Bulletin.where(:uid => uid).first
 | 
						|
    else
 | 
						|
      announcement = Bulletin.can_display.where(:uid => uid).first
 | 
						|
    end
 | 
						|
    url_to_edit = OrbitHelper.user_can_edit?(announcement) ? "/admin/announcements/#{announcement.id.to_s}/edit" : ""
 | 
						|
 | 
						|
    access_level = OrbitHelper.user_access_level?
 | 
						|
 | 
						|
    if !announcement.approved && (access_level != "manager" && access_level != "admin")
 | 
						|
      if AnnouncementSetting.is_pro?
 | 
						|
        if !(access_level == "sub_manager" && AnnouncementSetting.first.approvers.include?(OrbitHelper.current_user.id.to_s))
 | 
						|
          return {}
 | 
						|
        end
 | 
						|
      elsif access_level != "sub_manager"
 | 
						|
        return {}
 | 
						|
      end
 | 
						|
    end
 | 
						|
 | 
						|
    return {} if announcement.category.disable
 | 
						|
 | 
						|
    tags = announcement.tags.map{|tag|  {
 | 
						|
      "tag" => tag.name ,
 | 
						|
      "url" => OrbitHelper.page_for_tag(tag)
 | 
						|
    } } rescue []
 | 
						|
    files = announcement.bulletin_files.map{|file|  { "file_url" =>  file.file.url, "file_title" => (file.title.blank? ? URI.unescape(File.basename(file.file.path)) : file.title rescue '') } if file.enabled_for?(locale) } rescue []
 | 
						|
    files.delete(nil)
 | 
						|
    files.each do |file|
 | 
						|
      if file["file_url"] =="" || file["file_url"] == nil
 | 
						|
        files.delete(file)
 | 
						|
      end
 | 
						|
    end
 | 
						|
    links = announcement.bulletin_links.map{|link|  { "link_url" => link.url, "link_title" => (link.title.blank? ? link.url : link.title) } } rescue []
 | 
						|
    update_user = announcement.update_user.member_profile.name rescue ""
 | 
						|
    desc = announcement.image_description
 | 
						|
    desc = (desc.nil? || desc == "" ? "announcement image" : desc)
 | 
						|
 | 
						|
    request = OrbitHelper.request
 | 
						|
    meta_desc = announcement.subtitle.nil? || announcement.subtitle == ""  ? announcement.text[0..200] : announcement.subtitle
 | 
						|
    OrbitHelper.render_meta_tags([{"property" => "og:title", "content" => announcement.title},{"property" => "og:site_name", "content" => Site.first.title},{"property" => "og:url", "content" => request.original_url.split("?").first},{"property" => "og:description", "content" => meta_desc},{"property" => "og:image", "content" => "#{request.base_url}#{announcement.image.url}"},{"property" => "og:type", "content" => "Article"}])
 | 
						|
 | 
						|
    subtitle_ann = nil
 | 
						|
    img_src = nil
 | 
						|
    img_description = nil
 | 
						|
    subtitle_ann = announcement.subtitle if announcement.display_subtitle?
 | 
						|
    img_src = (announcement.image.thumb.url || "/assets/announcement-default.jpg") if announcement.display_img?
 | 
						|
    img_description = announcement.image_description if (announcement.image_description.present?) && (announcement.display_img?)
 | 
						|
 | 
						|
    {
 | 
						|
      "tags" => tags,
 | 
						|
      "bulletin_files" => files,
 | 
						|
      "bulletin_links" => links,
 | 
						|
      "data" => {
 | 
						|
        "title" => announcement.title,
 | 
						|
        "subtitle_ann" => subtitle_ann,
 | 
						|
        "update_user" => update_user,
 | 
						|
        "updated_at" => announcement.postdate.strftime('%Y-%m-%d %H:%M'),
 | 
						|
        "body" =>announcement.text,
 | 
						|
        "image" => announcement.image.url,
 | 
						|
        "img_src" => img_src,
 | 
						|
        "img_description" => img_description,
 | 
						|
        "alt_title" => desc
 | 
						|
      },
 | 
						|
      "impressionist" => (announcement.is_preview ? nil : announcement),
 | 
						|
      "url_to_edit"=>url_to_edit
 | 
						|
    }
 | 
						|
  end
 | 
						|
 | 
						|
  def show_feed_announcement(uid)
 | 
						|
    announcement = OrbitHelper.get_from_feed(uid)
 | 
						|
    locale = OrbitHelper.get_site_locale.to_s
 | 
						|
    url_to_edit = "#"
 | 
						|
    return {} if announcement.blank?
 | 
						|
    tags = []
 | 
						|
 | 
						|
    announcement["tags"].each{|tag|
 | 
						|
      t = Tag.where(:name => tag["name_translations"][locale]).first rescue nil
 | 
						|
      if t.nil?
 | 
						|
        I18n.locale = (locale == "en" ? :zh_tw : :en)
 | 
						|
        t = Tag.where(:name => tag["name_translations"][locale]).first rescue nil
 | 
						|
        I18n.locale = locale.to_sym
 | 
						|
      end
 | 
						|
      tags << {
 | 
						|
        "tag" => tag["name_translations"][locale],
 | 
						|
        "url" => (t.nil? ?  "#" : OrbitHelper.page_for_tag(t))
 | 
						|
      }
 | 
						|
    }
 | 
						|
 | 
						|
    files = announcement["bulletin_files"].map{|file|  { "file_url" =>  file["url"], "file_title" => (file["title_translations"][locale] == "" ? URI.unescape(File.basename(file["url"])) : file["title_translations"][locale] rescue '') } } rescue []
 | 
						|
 | 
						|
    files.each do |file|
 | 
						|
      if file["file_url"] =="" || file["file_url"] == nil
 | 
						|
        files.delete(file)
 | 
						|
      end
 | 
						|
    end
 | 
						|
    links = announcement["bulletin_links"].map{|link|  { "link_url" => link["url"], "link_title" => (link["title_translations"][locale] == "" ? link["url"] : link["title_translations"][locale]) } } rescue []
 | 
						|
 | 
						|
    update_user = announcement["author"]
 | 
						|
    desc = announcement["image_description_translations"][locale] rescue ""
 | 
						|
    desc = (desc.nil? || desc == "" ? "announcement image" : desc)
 | 
						|
 | 
						|
    request = OrbitHelper.request
 | 
						|
    if announcement["subtitle_translations"].present?
 | 
						|
      meta_desc = announcement["subtitle_translations"][locale] != "" ? announcement["subtitle_translations"][locale] : announcement["text_translations"][locale][0..200] rescue ""
 | 
						|
    else
 | 
						|
      meta_desc = ""
 | 
						|
    end
 | 
						|
 | 
						|
    OrbitHelper.render_meta_tags([{"property" => "og:title", "content" => announcement["title_translations"][locale]},{"property" => "og:site_name", "content" => Site.first.title},{"property" => "og:url", "content" => request.original_url.split("?").first},{"property" => "og:description", "content" => meta_desc},{"property" => "og:image", "content" => announcement["image"]["original"]},{"property" => "og:type", "content" => "Article"}])
 | 
						|
 | 
						|
    datetime = DateTime.parse(announcement["postdate"])
 | 
						|
 | 
						|
    {
 | 
						|
      "tags" => tags,
 | 
						|
      "bulletin_files" => files,
 | 
						|
      "bulletin_links" => links,
 | 
						|
      "data" => {
 | 
						|
        "title" => announcement["title_translations"][locale],
 | 
						|
        "update_user" => update_user,
 | 
						|
        "updated_at" => datetime.strftime('%Y-%m-%d %H:%M'),
 | 
						|
        "body" => announcement["text_translations"][locale],
 | 
						|
        "image" => announcement["image"]["original"],
 | 
						|
        "alt_title" => desc
 | 
						|
      },
 | 
						|
      "impressionist" => nil,
 | 
						|
      "url_to_edit" => url_to_edit
 | 
						|
    }
 | 
						|
  end
 | 
						|
 | 
						|
  def show
 | 
						|
    params = OrbitHelper.params
 | 
						|
    uid = params[:uid]
 | 
						|
    if OrbitHelper.is_object_from_feed?(uid)
 | 
						|
      show_feed_announcement(uid)
 | 
						|
    else
 | 
						|
      show_local_announcement(uid, (params["preview"] == "true" ? true : false))
 | 
						|
    end
 | 
						|
  end
 | 
						|
 | 
						|
 | 
						|
end
 |