module EventNewsHelper def self.complementaryColor(my_hex) if my_hex[0] == '#' my_hex = my_hex[1..-1] end rgb = my_hex.split(//).each_slice(my_hex.length/3).map{|v| v.join} comp = rgb.map{|a| (255 - a.to_i(16)).to_s(16).rjust(2,'0')} '#'+comp.join end def self.lighten_color(my_hex,percent) if my_hex[0] == '#' my_hex = my_hex[1..-1] end rgb = my_hex.split(//).each_slice(my_hex.length/3).map{|v| v.join} comp = rgb.collect do |a| tmp = a.to_i(16)*(1+percent/100.0) tmp = 255 if tmp>255 tmp = 0 if tmp < 0 tmp.to_i.to_s(16).rjust(2,'0') end '#'+comp.join end def data_to_human_type(a,set_tag_ids=nil) statuses = a.statuses_with_classname.collect do |status| { "status" => status["name"], "status-class" => "status-#{status['classname']}" } end files = a.event_news_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.event_news_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 == "" ? "event_news 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 event_time_formated = a.event_time_formated { "event_news_links" => links, "event_news_files" => files, "title" => a.title, "speaker-css" => (a.speaker.blank? ? "display: none;" : ""), "host-css" => (a.host.blank? ? "display: none;" : ""), "place-css" => (a.place.blank? ? "display: none;" : ""), "event-time-css" => (event_time_formated.blank? ? "display: none;" : ""), "notes-css" => (a.notes.blank? ? "display: none;" : ""), "event-time-formated" => event_time_formated, "speaker" => a.speaker, "place" => a.place, "host" => a.host, "notes" => a.notes, "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" => event_time_formated, "event_start_date" => a.event_date_frontend, "event_end_date" => a.event_end_date_frontend, "event_date" => event_time_formated, "author" => author, "link_to_show" => link_to_show, "target" => target, "img_src" => image_url || "/assets/announcement-default.jpg", "img_description" => desc } end def get_feed_annc(type,site_source,locale,categories=nil,max_len=nil) ma_key = 'event_news_mod' 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) data end def get_feed_event_news(type,site_source=nil,categories=nil,max_len=nil) locale = OrbitHelper.get_site_locale.to_s if !(defined? SiteFeedAnnc).nil? fans = get_feed_annc(type,site_source,locale,categories,max_len) else feed_anns = OrbitHelper.get_feed_for_module(type) fans = [] feed_anns.each do |fa| next if !site_source.nil? && site_source != fa["source-site-title"] status = { "status" => "#{fa["source-site-title"]}", "status-class" => "status-source" } files = fa["event_news_files"].collect{|bf| { "file_url" => bf["file_url"], "file_title" => fa["file_title_translations"][locale] }} rescue [] links = fa["event_news_links"].map{|link| { "link_url" => link["link_url"], "link_title" => link["link_title_translations"][locale] } } rescue [] event_time_formated = a.event_time_formated s = DateTime.parse(fa["event_date"]) rescue nil e = DateTime.parse(fa["event_end_date"]) rescue nil date_parse_format = fa["all_day"] ? '%Y-%m-%d' : '%Y-%m-%d %H:%M' if s.blank? && e.blank? event_time_formated = "" elsif s.blank? event_time_formated = s.strftime(date_parse_format) elsif e.blank? event_time_formated = "~ " + e.strftime(date_parse_format) else if s.to_date == e.to_date date_str = s.strftime('%Y-%m-%d') s_time = s.strftime('%H:%M') e_time = e.strftime('%H:%M') event_time_formated = "#{date_str} #{s_time} ~ #{e_time}" else event_time_formated = s.strftime(date_parse_format) + " ~ " + e.strftime(date_parse_format) end end x = { "event_news_links" => links, "event_news_files" => files, "event_carousel_images" => fa["event_carousel_images"].to_a, "title" => fa["title_translations"][locale], "speaker-css" => (fa["speaker_translations"][locale].blank? ? "display: none;" : ""), "host-css" => (fa["host_translations"][locale].blank? ? "display: none;" : ""), "place-css" => (fa["place_translations"][locale].blank? ? "display: none;" : ""), "event-time-css" => (event_time_formated.blank? ? "display: none;" : ""), "notes-css" => (fa["notes_translations"][locale].blank? ? "display: none;" : ""), "event-time-formated" => event_time_formated, "speaker" => (fa["speaker_translations"][locale].to_s rescue ""), "place" => (fa["place_translations"][locale].to_s rescue ""), "host" => (fa["host_translations"][locale].to_s rescue ""), "notes" => (fa["notes_translations"][locale].to_s rescue ""), "subtitle" => fa["subtitle_translations"][locale], "statuses" => [status], "category" => fa["category"], "postdate" => (DateTime.parse(fa["event_date"]) rescue DateTime.now), "event_date" => (DateTime.parse(fa["event_date"]) rescue DateTime.now), "author" => fa["author"], "source-site" => "#{fa["source-site-title"]}", "source-site-title" => fa["source-site-title"], "source-site-link" => fa["source-site"], "link_to_show" => OrbitHelper.url_to_show(fa["params"]), "target" => "_self", "img_src" => fa["image"]["thumb"] || "/assets/event_news-default.jpg", "img_description" => fa["image_description_translations"][locale], "more" => t("event_news.more"), "view_count" => "" } if (!x["title"].empty? rescue false) fans << x end end end fans end def filter_by_keywords(sorted,keywords,stime,etime) kflag = keywords.blank? sflag = stime.blank? eflag = etime.blank? stime = stime.to_s.split('/') stime = Time.zone.local(*stime) rescue nil etime = etime.to_s.split('/') etime = Time.zone.local(*etime) rescue nil if !kflag || !sflag || !eflag sorted.select{|anns| if kflag flag = true else if anns["source-site"].present? title = Nokogiri::HTML(anns["title"].to_s).text else title = Nokogiri::HTML(anns.title.to_s).text end flag = title.include?(keywords.to_s) end if sflag && !eflag flag = flag && (anns.event_date<=etime) elsif !sflag && eflag flag = flag && (anns.event_date>=stime) elsif !sflag && !eflag flag = flag && (anns.event_date>=stime) && (anns.event_date<=etime) end flag } else sorted end end def get_sorted_event_news(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 = [] if @type == "show_widget" tags = @tags categories = @categories else page = OrbitHelper.page rescue nil page = Page.where(url:params['url']).first if page.nil? tags = page.tags tags = params[:tags] if params[:tags].present? categories = params['category']=='all' ? (page.categories || []) : (Array(params['category']) rescue (page.categories || [])) if params['category'].present? && tags.blank? tags = ["all"] end module_app = ModuleApp.where(key: 'event_news_mod').first @enable_search_flag = false @show_option_items = nil @show_today_data_first = false if module_app && page.respond_to?(:select_option_items) @show_option_items = module_app.show_option_items if !@show_option_items.nil? page.select_option_items.each do |select_option_item| value = YAML.load(select_option_item.value)[I18n.locale] case select_option_item.field_name when @show_option_items.keys[1].to_s if value == I18n.t('yes_') @enable_search_flag = true end when @show_option_items.keys[3].to_s if value == I18n.t('yes_') @show_today_data_first = true end end end end end if categories.include? 'all' @categories = module_app.categories else cat_maps = Category.where(:id.in => categories).collect{|cat| [cat.id.to_s, cat]}.to_h @categories = categories.map{|v| cat_maps[v.to_s]}.compact end end event_news_list = [] if !params["source"].present? event_news = @show_today_data_first ? EventNews.can_display_and_sorted_according_today : EventNews.can_display_and_sorted event_news = event_news .where(:title.nin => ["",nil]) .filter_by_categories(categories,false).filter_by_tags(tags) 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?} event_news = event_news.where(:create_user_id.in=>user_ids) end end event_news_list = event_news.to_a if !(defined? SiteFeed).nil? if @type != "show_widget" feeds_anns = get_feed_event_news("index",nil,nil,page_number*page_data_count) else feeds_anns = [] end end else if @type != "show_widget" feeds_anns = get_feed_event_news("index",params["source"],nil,page_number*page_data_count) else feeds_anns = [] end end if !feeds_anns.blank? if event_news_list.count != 0 top_anns = event_news_list.select{|v| v.is_top} + feeds_anns.select{|v| v['is_top']} rest_all_anns = feeds_anns.select{|v| v['is_top'] != true} + event_news_list.select{|v| !v.is_top} all_sorted = sort_event_news(top_anns) + sort_event_news(rest_all_anns) else all_sorted = sort_event_news(feeds_anns.select{|v| v['is_top']}) + sort_event_news(feeds_anns.select{|v| v['is_top'] != true}) end all_filter = filter_by_keywords(all_sorted,params[:keywords],params[:stime],params[:etime]) else all_filter = filter_by_keywords(event_news_list,params[:keywords],params[:stime],params[:etime]) 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 = all_filter.count total_pages = page_data_count == 0 ? 1 : (annc_count.to_f / page_data_count).ceil [sorted,total_pages] end def sort_event_news(event_news_list) if @show_today_data_first || !EventNewsHelper.is_postdate_sort_first event_news_list = event_news_list.sort_by { |event_news| tmp1 = event_news["event_date"].blank? tmp2 = event_news["postdate"].blank? [ tmp1 ? 0 : 1, tmp1 ? nil : event_news["event_date"].to_time, tmp2 ? 0 : 1, tmp2 ? nil : event_news["postdate"].to_time ] } if !@show_today_data_first event_news_list = event_news_list.reverse end else event_news_list = event_news_list.sort_by { |event_news| tmp1 = event_news["event_date"].blank? tmp2 = event_news["postdate"].blank? [ tmp2 ? 0 : 1, tmp2 ? nil : event_news["postdate"].to_time, tmp1 ? 0 : 1, tmp1 ? nil : event_news["event_date"].to_time ] }.reverse end return event_news_list end def render_view_for_event_news(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', 'event_news_mod', "#{filename}.html.erb") if !File.exist?(f) f = File.join(Rails.root, 'app', 'templates', "#{@key}", 'modules', 'event_news_mod', "index.html.erb") if !File.exist?(f) return "