module AnnouncementsHelper extend self def data_to_human_type(a,set_tag_ids=nil,tmp_enable_annc_dept=false,annc_depts=[]) tmp_enable_annc_dept = @tmp_enable_annc_dept if @tmp_enable_annc_dept annc_depts = @annc_depts if @annc_depts 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 = tmp_enable_annc_dept ? annc_depts[a.annc_dept] : User.find(a.update_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)) rescue "" 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 doc = Nokogiri::HTML(a.title) title = doc.text.empty? ? 'no content' : doc.text { "is_top" => a.is_top, "bulletin_links" => links, "bulletin_files" => files, "sort_number" => a.sort_number, "title" => a.title, "source-site" => "", "source-site-title" => "", "source-site-link" => "", "subtitle" => a.subtitle, "statuses" => statuses, "category" => (a.category.title rescue ""), "tag_ids" => (set_tag_ids.nil? ? (a.tag_ids.map{|id| id.to_s}.to_s.gsub('"',"'") rescue '[]') : set_tag_ids), "postdate" => a.postdate, "author" => author, "link_to_show" => link_to_show+"\" #{(link_to_show[0] == '/' rescue true) ? '' : 'target="_blank"'} title=\"#{title}", "target" => target, "img_src" => image_url || AnnouncementsController::DefaultImgSrc, "img_description" => desc } end def get_feed_annc(type,site_source,locale,categories=nil,max_len=nil,sort_maps=nil,extra_match_cond=nil) ma_key = 'announcement' if categories.nil? if type == "index" categories = Array(OrbitHelper.page_categories) elsif type == "widget" categories = Array(OrbitHelper.widget_categories) else categories = [] end end categories = ["all"] if categories.length==0 data = SiteFeedAnnc.get_feed_cache( ma_key, categories, site_source, locale, type=='widget', max_len, sort_maps, extra_match_cond ) data end def get_feed_announcements(type,site_source=nil,categories=nil,max_len=nil,extra_match_cond=[]) locale = OrbitHelper.get_site_locale.to_s feeds = [] feeds_count = 0 if !(defined? SiteFeedAnnc).nil? sort_maps = {is_top: :desc, postdate: :desc, id: :desc} match_cond = { "is_hidden" => {"$ne" => true}, "$and" => [ {"postdate" => {"$lte"=> Time.now}}, { "$or" => [ {"deadline" => {"$gte"=> Time.now}}, {"deadline" => nil} ] } ], "title" => {"$gt"=>""} } if !extra_match_cond.empty? match_cond["$and"] += extra_match_cond end feeds, feeds_count = get_feed_annc(type,site_source,locale,categories,max_len,sort_maps,match_cond) end return feeds, feeds_count end def get_sorted_annc(data_count=nil) params = OrbitHelper.params locale = OrbitHelper.get_site_locale.to_s page_number = OrbitHelper.page_number.to_i page_number = 1 if page_number == 0 page_data_count = data_count || OrbitHelper.page_data_count.to_i feeds_anns = [] page = OrbitHelper.page if @type == "show_widget" tags = @tags categories = @categories else tags = page.tags tags = params[:tags] if params[:tags].present? categories = params['category']=='all' ? (page.categories || []) : (params['category'].nil? ? (page.categories || []) : Array(params['category'])) if params['category'].present? && tags.blank? tags = ["all"] end end announcements_list = [] announcements = [] feeds_count = 0 extra_match_cond = [] if !params[:keywords].blank? extra_match_cond << { "title_plain_text" => OrbitHelper.get_keyword_regex(params[:keywords]) } end if !params[:stime].blank? stime = OrbitHelper.get_time_from_str(params[:stime]) extra_match_cond << { "postdate" => {"$gte" => stime} } end if !params[:etime].blank? etime = OrbitHelper.get_time_from_str(params[:etime]) + 1.days extra_match_cond << { "postdate" => {"$lt" => etime} } end if !params["source"].present? announcements = Bulletin.can_display_and_sorted .filter_by_categories(categories, false).filter_by_tags(tags) .where(:title.nin => ["",nil]) if !extra_match_cond.empty? announcements = announcements.and(extra_match_cond) end if @type == "show_widget" if !params[:uids].blank? member_profile = MemberProfile.any_in(:uid=>params[:uids]) user_ids = member_profile.map{|m| m.user.id rescue nil}.select{|id| !id.nil?} announcements = announcements.where(:update_user_id.in=>user_ids) end end if !(defined? SiteFeed).nil? && @type != "show_widget" feeds_anns, feeds_count = get_feed_announcements("index",nil,categories,page_number*page_data_count,extra_match_cond) end elsif @type != "show_widget" feeds_anns, feeds_count = get_feed_announcements("index",params["source"],categories,page_number*page_data_count,extra_match_cond) end if !feeds_anns.blank? top_anns = announcements.select{|v| v.is_top} + feeds_anns.select{|v| v['is_top']} rest_all_anns = announcements.select{|v| !v.is_top} + feeds_anns.select{|v| v['is_top'] != true} all_filter = sort_announcements(top_anns) + sort_announcements(rest_all_anns) else all_filter = announcements end if page_data_count != 0 sorted = all_filter[(page_number-1)*page_data_count...page_number*page_data_count] else sorted = all_filter end annc_count = announcements.count + feeds_count total_pages = page_data_count == 0 ? 1 : (annc_count.to_f / page_data_count).ceil [sorted,total_pages] end def sort_announcements(announcements) if enable_manually_sort announcements = announcements.sort_by { |announcement| tmp1 = announcement["postdate"].blank? [ -announcement['sort_number'].to_i, tmp1 ? 0 : 1, tmp1 ? nil : announcement["postdate"].to_time ] }.reverse else announcements = announcements.sort_by { |announcement| tmp1 = announcement["postdate"].blank? [ tmp1 ? 0 : 1, tmp1 ? nil : announcement["postdate"].to_time ] }.reverse end return announcements end def render_view_for_annc(overridehtml=nil) @key = Site.first.template def render_link_to_edit(html, url_to_edit) if html.scan("{{link_to_edit}}").length == 0 html = url_to_edit.blank? ? html : html + "
" else html = url_to_edit.blank? ? html.gsub("{{link_to_edit}}","") : html.gsub("{{link_to_edit}}","") end return html end def parsing_repeats_again(elements,d,level) newhtml = [] oldhtml = [] elements.each do |el| html_to_render = "" data_name = el.attr("data-list") wrap_elements = el.css("*[data-list][data-level='#{level}']") if d[data_name] d[data_name].each_with_index do |item,i| element = el.inner_html if wrap_elements.count > 0 htmls = parsing_repeats_again(wrap_elements,d[data_name][i], level + 1) htmls[0].each_with_index do |html,i| element = element.gsub(html,htmls[1][i]) end end item.each do |key,value| if !value.kind_of?(Array) value = value.nil? ? "" : value element = element.gsub("{{#{key}}}",value.to_s.html_safe) element = element.gsub("%7B%7B#{key}%7D%7D",value.to_s.html_safe) element = render_link_to_edit(element, value) if key.eql?("url_to_edit") end end html_to_render = html_to_render + element end temp = el.to_s oldhtml << temp temp = temp.gsub(el.inner_html, html_to_render) newhtml << temp end end [oldhtml,newhtml] end if @target_action == "index" filename = File.basename(overridehtml.nil? ? params[:layout_type] : overridehtml) f = File.join(Rails.root, 'app', 'templates', "#{@key}", 'modules', 'announcement', "#{filename}.html.erb") if !File.exist?f f = File.join(Rails.root, 'app', 'templates', "#{@key}", 'modules', 'announcement', "index.html.erb") if !File.exist?f return "