class AnnouncementsController < ApplicationController ViewRootDir = File.expand_path("../../views", __FILE__) include AnnouncementsHelper DefaultImgSrc = "/assets/announcement-default.jpg\" onerror=\"this.src="/assets/announcement-default-2.jpg";this.onerror='';" def initialize super @app_title = 'announcement' #self.request = OrbitHelper.request end def annc_depts_translations if defined?(OrbitHelper::SharedHash) && OrbitHelper::SharedHash OrbitHelper::SharedHash['announcement'][:annc_depts_translations] rescue AnnouncementSetting.first.annc_depts_translations else AnnouncementSetting.first.annc_depts_translations rescue {} end end def enable_annc_dept if defined?(OrbitHelper::SharedHash) && OrbitHelper::SharedHash OrbitHelper::SharedHash['announcement'][:enable_annc_dept] rescue AnnouncementSetting.first.enable_annc_dept else AnnouncementSetting.first.enable_annc_dept rescue false end end def render_400 render(:file => "#{ViewRootDir}/announcements/400.html", :layout => false, :status => 400, :formats => [:html]) end def render_404 render(:file => "#{Rails.root}/app/views/errors/404.html", :layout => false, :status => 404, :formats => [:html]) end def comment @bulletin = Bulletin.where(:uid=>params[:uid]).first comment_val = params['comment'] if !@bulletin.nil? && @bulletin.open_comment_for_user(OrbitHelper.current_user) && !comment_val.blank? account_id = OrbitHelper.current_user.member_profile.id.to_s rescue 'visitor' b = BulletinComment.new(ip: request.remote_ip,comment: comment_val,account_id: account_id) b.bulletin_id = @bulletin.id b.save render :json => {} else render_404 end end def index @type = 'index' Bulletin.remove_expired_status params = @params = OrbitHelper.params page = @page || Page.where(url: params['url']).first @enable_search_flag = false @image_version = 'thumb' if page.respond_to?(:select_option_items) module_app = ModuleApp.where(key: 'announcement').first @show_option_items = nil if module_app && page.respond_to?(:select_option_items) @show_option_items = module_app.show_option_items end I18n.with_locale(:en) do page.select_option_items.each do |select_option_item| if !(@show_option_items.nil?) case select_option_item.field_name when @show_option_items.keys[1].to_s value = YAML.load(select_option_item.value) if value[:en] == t('announcement.yes') @enable_search_flag = true end when @show_option_items.keys[2].to_s value = YAML.load(select_option_item.value) tmp = value[:en] if tmp == t('announcement.small_size') @image_version = 'thumb' elsif tmp == t('announcement.medium_size') @image_version = 'mobile' elsif tmp == t('announcement.orignal_size') @image_version = 'orignal' end end end end end end sorted,total_pages = get_sorted_annc if sorted.nil? sorted = [] total_pages = 0 end annc_depts = [] tmp_enable_annc_dept = (enable_annc_dept rescue false) if tmp_enable_annc_dept annc_depts = annc_depts_translations[locale] rescue [] end anns = sorted.collect do |a| if a["source-site"].blank? statuses = a.statuses_with_classname.collect do |status| { "status" => status["name"], "status-class" => "status-#{status['classname']}" } end locale = OrbitHelper.get_site_locale.to_s files = a.bulletin_files.to_fronted(locale) links = a.bulletin_links.map{|link| { "link_url" => link.url, "link_title" => (link.title.blank? ? link.url : link.title) } } rescue [] author = tmp_enable_annc_dept ? annc_depts[a.annc_dept] : User.find(a.update_user_id).member_name rescue (User.find(a.update_user_id).member_profile.name rescue "") desc = a.image_description desc = (desc.blank? ? "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" title = a.title_plain_text title = 'no content' if title.blank? 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 { "department" => author, "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 rescue ""), "postdate" => a.postdate, "author" => author, "is_top" => (a.is_top? ? 1 : 0), "link_to_show" => link_to_show+"\" #{(link_to_show[0] == '/' rescue true) ? '' : 'target="_blank"'} title=\"#{title}", "target" => target, "img_src" => image_url || DefaultImgSrc, "img_description" => desc, "more" => t("announcement.more"), "view_count" => a.view_count, "orbithashtags" => a.hashtags_for_frontend, "tag" => a.tags_for_frontend } else a end end if anns.count == 0 && params.any?{|k,v| v.class==Array ? v.any?{|v1| v1.to_s.match(/\*|\(|\)/)} : v.to_s.match(/\*|\(|\)/)} return nil end #If no data , hide title&table if sorted.count == 0 display = "hide" end # anns = anns.concat(feeds_anns) # total_pages = announcements.total_pages page = Page.where(url:params['url']).first @annc_page_title = nil if (params['category'] != page.categories rescue true) @annc_page_title = Category.find(Array(params['category']).first).title rescue nil end if (params["tags"] != page.tags && !params["tags"].blank? && params["tags"].count == 1 && params["tags"][0] != "all" rescue true) @annc_page_title = Tag.find(Array(params['tags']).first).name rescue nil end { "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, "department-head" => t('announcement.table.department'), "annc-dept-head" => t("announcement.annc_dept"), "page-title" => @annc_page_title }, "total_pages" => total_pages } end def random_announcement_widget pack_data(true) end def widget pack_data() 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_and_sorted.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(is_random=false) cats = OrbitHelper.widget_categories || [] tags = OrbitHelper.widget_tags || [] tags = ['all'] if tags.blank? subpart = OrbitHelper.get_current_widget get_tabs_option anns = [] use_tag = false @annc_depts = [] @tmp_enable_annc_dept = (enable_annc_dept rescue false) if @tmp_enable_annc_dept @annc_depts = annc_depts_translations[locale] rescue [] end if @tab_option == 0 anns = get_anncs_for_pack_data(cats,tags,nil,is_random) later_process = false else later_process = true if cats.count != 1 || tags == ["all"] cats.each do |cat| anns = anns + get_anncs_for_pack_data([cat],tags,'',is_random, later_process) end else tags.each do |tag| anns = anns + get_anncs_for_pack_data(cats,[tag],tag,is_random, later_process) end use_tag = true end end mp = (anns[0]["img_src"] rescue "") mpd = (anns[0]["img_description"] rescue "") if @tab_option == 1 if use_tag tags = ["all"] + tags else cats = ["all"] + cats end anns = sort_announcements(anns) process_anns(anns) end cats = cats.uniq tags = tags.uniq tags_translations = (subpart.get_tags_translations(false,false) rescue nil) tags_translations = Tag.where(:id.in=>tags).map{|t| [t.id.to_s,t.name]} if tags_translations.nil? if tags.include?("all") tags_translations = [["all",I18n.t(:all)]] + tags_translations end tags_translations = tags_translations.to_h cats_translations = (subpart.get_cats_translations(false,false) rescue nil) cats_translations = Category.where(:id.in=>cats).map{|c| [c.id.to_s,c.title]} if cats_translations.nil? if cats.include?("all") cats_translations = [["all",I18n.t(:all)]] + cats_translations end cats_translations = cats_translations.to_h if cats.include?("all") cats_relations = cats_translations.clone cats_relations['all'] = 'all' else cats_relations = cats_translations end page_url = (subpart.get_read_more_page_url rescue nil) locale = I18n.locale.to_s if page_url.nil? home_page = subpart.page_part.page rescue Page.first page = nil if home_page.respond_to?(:find_page) page = home_page.find_page(:page_id=> subpart.read_more_page_id,:enabled_for=>locale).first rescue nil page = home_page.find_page(:module=>"announcement",:enabled_for=>locale).first rescue nil if page.nil? else page = Page.where(:page_id=> subpart.read_more_page_id,:enabled_for=>locale).first rescue nil page = Page.where(:module=>"announcement",:enabled_for=>locale).first rescue nil if page.nil? end page_url = "/#{locale}#{(page.get_url rescue page.url)}" end all_cats = cats.dup all_cats.delete "all" if all_cats.count == 0 all_cats = ["all"] end all_tags = tags.dup all_tags.delete "all" if all_tags.count == 0 all_tags = ["all"] end max_all_count = [OrbitHelper.widget_data_count,anns.count].min if @tab_option != 0 OrbitHelper.set_widget_title(OrbitHelper.widget_title + "
" + "" ) filter_attr = (use_tag ? 'data-tags' : 'data-category') extra_html = ' ' else read_more_text = I18n.t("announcement.more") if cats.count == 1 && cats[0] != "all" begin read_more_text = I18n.t("announcement.more_") + ((all_tags.count == 1 && all_tags[0] != 'all') ? tags_translations[tags[0]] : cats_translations[cats[0]]) rescue nil end end extra_html = " " end if (@read_more_option != 0 rescue false) extra_html += " " end extra_after_html = "" if @all_setting_option == 0 && @tab_option == 1 extra_after_html = " " end if @tab_option == 0 read_more_url = "#{page_url}" rescue "" read_more_url = read_more_url + "?" + {"category"=>all_cats,"tags"=>all_tags}.to_param if read_more_url != "" extra_after_html += " " end { "announcements" => anns, "extras" => { "more_url"=>read_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'), "annc-dept-head" => t("announcement.annc_dept"), "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" => read_more_url, "read_more_text" => "read more", "extra_brefore_html" => extra_html, "extra_after_html" => extra_after_html } } end def get_tabs_option subpart = OrbitHelper.get_current_widget tab_options = ["not_enable_tabs","enable_tabs_with_categories_include_all","enable_tabs_with_categories"] read_more_options = ['default','upper_left','lower_left','upper_right','lower_right', 'display_none'] all_setting_options = ['the_same_as_data_count','display_all_in_other_tabs'] @tab_option = 0 @read_more_option = 0 @all_setting_option = 0 @image_version = 'thumb' @show_options = nil if subpart.methods.include? 'select_options'.to_sym module_app = ModuleApp.where(key: 'announcement').first if module_app @show_options = module_app.show_options end I18n.with_locale(:en) do subpart.select_options.each do |select_option| if !(@show_options.nil?) value = YAML.load(select_option.value) tmp = value[:en] case select_option.field_name when @show_options.keys[0].to_s if tmp == t('announcement.small_size') @image_version = 'thumb' elsif tmp == t('announcement.medium_size') @image_version = 'mobile' elsif tmp == t('announcement.orignal_size') @image_version = 'orignal' end when @show_options.keys[1].to_s tab_options.each_with_index do |option,i| if tmp == t("announcement.#{option}") @tab_option = i break end end when @show_options.keys[2].to_s read_more_options.each_with_index do |option,i| if tmp == t("announcement.#{option}") @read_more_option = i break end end when @show_options.keys[3].to_s all_setting_options.each_with_index do |option,i| if tmp == t("announcement.#{option}") @all_setting_option = i break end end end end end end end end def process_anns(anns) anns.each{|a| a["postdate"] = a["postdate"].in_time_zone(Time.zone.utc_offset / 3600).strftime('%Y-%m-%d %H:%M') if a["postdate"] } end def get_anncs_for_pack_data(cats,tags,set_tags=nil,is_random = false,later_process=false) if @annc_depts.nil? @annc_depts = [] @tmp_enable_annc_dept = (enable_annc_dept rescue false) if @tmp_enable_annc_dept @annc_depts = annc_depts_translations[locale] rescue [] end end if tags.blank? tags = ["all"] end subpart = OrbitHelper.get_current_widget widget_data_count = OrbitHelper.widget_data_count anns_cache = AnnsCache.where(parent_id: subpart.id.to_s + cats.to_s + tags.to_s + widget_data_count.to_s,locale: I18n.locale.to_s) devide_flag = (!(defined? SiteFeed).nil?) && (SiteFeed.count != 0) anns_cache.all_of([{:invalid_date.ne=>nil},{:invalid_date.lte => Time.now}]).destroy count = anns_cache.count if count > 1 anns_cache.limit(count-1).destroy end if anns_cache.count == 0 || is_random Bulletin.remove_expired_status uid = OrbitHelper.params[:uid] rescue "" anns_for_locale = Bulletin.where(:title.nin => ["",nil], :uid.ne => uid).is_approved_and_show.filter_cats_and_tags(cats,tags) sorted_anns = anns_for_locale.can_display_and_sorted if !is_random sorted_anns = sorted_anns.limit(widget_data_count) first_deadline = sorted_anns.pluck(:deadline).compact.sort[0] first_postdate = anns_for_locale.open_in_future.limit(1).pluck(:postdate)[0] invalid_date = [first_postdate,first_deadline].compact.sort[0] if devide_flag now_anns = sorted_anns.to_a top_anns = now_anns.select{|v| v.is_top}.map{|v| data_to_human_type(v,set_tags)} not_top_anns = now_anns.select{|v| !v.is_top}.map{|v| data_to_human_type(v,set_tags)} AnnsCache.create(parent_id: subpart.id.to_s + cats.to_s + tags.to_s + widget_data_count.to_s,locale: I18n.locale.to_s,filter_result: {top: top_anns,not_top: not_top_anns},invalid_date: invalid_date) else anns = sorted_anns.map{|v| data_to_human_type(v,set_tags)} AnnsCache.create(parent_id: subpart.id.to_s + cats.to_s + tags.to_s + widget_data_count.to_s,locale: I18n.locale.to_s,filter_result: anns,invalid_date: invalid_date) end else invalid_date = Time.now + 3.second if devide_flag anns = sorted_anns.sample(widget_data_count) top_anns = anns.select{|v| v.is_top}.map{|v| data_to_human_type(v,set_tags)} not_top_anns = anns.select{|v| !v.is_top}.map{|v| data_to_human_type(v,set_tags)} else anns = sorted_anns.sample(widget_data_count).map{|v| data_to_human_type(v,set_tags)} end end elsif devide_flag c = anns_cache.first now_anns = c.filter_result invalid_date = c.invalid_date top_anns = now_anns[:top] not_top_anns = now_anns[:not_top] else c = anns_cache.first invalid_date = c.invalid_date anns = c.filter_result end if @page_invalid_time.nil? @page_invalid_time = invalid_date elsif !invalid_date.nil? && @page_invalid_time>invalid_date @page_invalid_time = invalid_date end if devide_flag feeds_anns, _ = get_feed_announcements("widget",nil,cats,widget_data_count - top_anns.count) top_anns += feeds_anns.select{|v| v['is_top']} top_anns = sort_announcements(top_anns) rest_count = widget_data_count - top_anns.count if rest_count <= 0 anns = top_anns.take(widget_data_count) else rest_all_anns = feeds_anns.select{|v| !v['is_top']} + not_top_anns.take(rest_count) rest_anns = sort_announcements(rest_all_anns).take(rest_count) anns = top_anns + rest_anns end end unless later_process process_anns(anns) end anns end def get_file @url = request.path begin if @url.match(/\/\.\./) render :file => "#{Rails.root}/app/views/errors/404.html", :layout => false, :status => :not_found, :content_type => 'text/html' return end file = BulletinFile.find(params[:id]) if File.basename(file.file.path) != URI.decode(params[:f_name]) render :file => "#{Rails.root}/app/views/errors/403.html", :layout => false, :status => :not_found, :content_type => 'text/html' return end @url = file.file.url if file.can_access?(OrbitHelper.current_user) @path = file.file.path rescue "" @filename = @path.split("/").last @ext = @path.split("/").last.to_s.split(".").last if @ext == "png" || @ext == "jpg" || @ext == "bmp" || @ext == "pdf" render "archives/download_file.html",:layout=>false else if (current_site.accessibility_mode rescue false) render "archives/redirect_to_file.html",:layout=>false return else user_agent = request.user_agent.downcase @escaped_file_name = user_agent.match(/(msie|trident)/) ? CGI::escape(@filename) : @filename send_file(@path, :type=>"application/octet-stream", :filename => @escaped_file_name, :x_sendfile=> true) return end end else render :file => "#{Rails.root}/app/views/errors/403.html", :layout => false, :status => :not_found, :content_type => 'text/html' return end rescue => e puts [e, e.backtrace] render :file => "#{Rails.root}/app/views/errors/404.html", :layout => false, :status => :not_found, :content_type => 'text/html' return end 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_and_sorted.where(:uid => uid).first end announcement = Bulletin.where(:uid => uid).first if announcement.nil? return nil if announcement.nil? @bulletin = announcement 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 rescue false) if !announcement.deadline.nil? && announcement.deadline < Time.now return { "tags" => [], "bulletin_files" => [], "bulletin_links" => [], "data" => { "title" => announcement.title, "subtitle_ann" => '', "update_user" => '', "updated_at" => '', "image" => '', "img_src" => '', "img_description" => '', "hide_class" => ' hide', "alt_title" => '', "carousel_html" => '', "sub_anncs_text" => '', "body" => "#{I18n.t('announcement.expired')}| #{announcement.get_sub_annc_title_trans} | #{display_sub_annc_date ? "#{I18n.t("announcement.table.date")} | " : ''}
|---|---|
| #{sub_annc.title} | #{(display_sub_annc_date ? (''+sub_annc.display_postdate+' | ') : '')}