module CustomAnnouncementsHelper 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 set_image_version_for_widget subpart = OrbitHelper.get_current_widget @image_version = 'thumb' if subpart.methods.include? 'select_options'.to_sym ModuleApp.all.select{|tmp| tmp.key.to_s=='custom_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) tmp = value[:en] I18n.with_locale(:en) do if tmp == t('custom_announcement.small_size') @image_version = 'thumb' elsif tmp == t('custom_announcement.medium_size') @image_version = 'mobile' elsif tmp == t('custom_announcement.orignal_size') @image_version = 'orignal' end end end end end 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.custom_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.custom_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 == "" ? "custom_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 { "custom_bulletin_links" => links, "custom_bulletin_files" => files, "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, "target" => target, "img_src" => image_url || "/assets/custom_announcement-default.jpg", "img_description" => desc } end def get_feed_annc(type,site_source,locale) ma_key = 'custom_announcement' if type == "index" categories = Array(OrbitHelper.page_categories) elsif type == "widget" categories = Array(OrbitHelper.widget_categories) else categories = [] end if categories.include?("all") feeds = SiteFeedCustomAnnc.where(:channel_key => ma_key) else feeds = SiteFeedCustomAnnc.where(:channel_key => ma_key, :merge_with_category.in => categories) end if feeds.count > 0 temp_ids = [] data = feeds.collect do |feed| feed.all_contents_for_feed(site_source,locale,type=='widget') end.flatten.compact else data = [] end data end def get_feed_custom_announcements(type,site_source=nil) locale = OrbitHelper.get_site_locale.to_s if !(defined? SiteFeedCustomAnnc).nil? fans = get_feed_annc(type,site_source,locale) else feed_custom_anns = OrbitHelper.get_feed_for_module(type) fans = [] feed_custom_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["custom_bulletin_files"].collect{|bf| { "file_url" => bf["url"], "file_title" => (fa["title_translations"][locale].blank? ? File.basename(fa["url"]) : fa["title_translations"][locale] rescue '') }} rescue [] links = fa["custom_bulletin_links"].map{|link| { "link_url" => link["url"], "link_title" => (link["title_translations"][locale].blank? ? link["url"] : link["title_translations"][locale]) } } rescue [] x = { "custom_bulletin_links" => links, "custom_bulletin_files" => files, "custom_bulletin_carousel_images" => fa["custom_bulletin_carousel_images"].to_a, "title" => fa["title_translations"][locale], "subtitle" => fa["subtitle_translations"][locale], "statuses" => [status], "category" => fa["category"], "postdate" => fa["postdate"], "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/custom_announcement-default.jpg", "img_description" => fa["image_description_translations"][locale], "more" => t(:more_plus), "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{|custom_anns| if kflag flag = true else if custom_anns["source-site"].present? title = Nokogiri::HTML(custom_anns["title"].to_s).text else title = Nokogiri::HTML(custom_anns.title.to_s).text end flag = title.include?(keywords.to_s) end if sflag && !eflag flag = flag && (custom_anns.postdate<=etime) elsif !sflag && eflag flag = flag && (custom_anns.postdate>=stime) elsif !sflag && !eflag flag = flag && (custom_anns.postdate>=stime) && (custom_anns.postdate<=etime) end flag } else sorted end 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 = OrbitHelper.page rescue nil unless page page = Page.where(url:params['url']).first end page_number = 1 if page_number == 0 page_data_count = data_count || OrbitHelper.page_data_count.to_i feeds_custom_anns = [] custom_data_field = page.custom_data_field rescue nil if custom_data_field.nil? custom_data_field = {} elsif custom_data_field["bind_module_app"] custom_data_field["custom_module"] = custom_data_field["bind_module_app"] custom_data_field.delete('bind_module_app') end 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 || []) : (Array(params['category']) rescue (page.categories || [])) if params['category'].present? tags = ["all"] end end if !params["source"].present? if @type == "show_widget" if params[:uids].blank? custom_announcements = CustomBulletin.where(:title.nin => ["",nil],:is_preview.in=>[false,nil]).where(custom_data_field) .can_display_and_sorted.is_approved .filter_by_categories(categories,false).filter_by_tags(tags).to_a else member_prfile = MemberProfile.any_in(:uid=>params[:uids]) user_ids = member_prfile.map{|m| m.user.id rescue nil}.select{|id| !id.nil?} custom_announcements = CustomBulletin.where(:title.nin => ["",nil],:is_preview.in=>[false,nil],:create_user_id.in=>user_ids).where(custom_data_field) .can_display_and_sorted.is_approved .filter_by_categories(categories,false).filter_by_tags(tags).to_a end else custom_announcements = CustomBulletin.where(:title.nin => ["",nil],:is_preview.in=>[false,nil]).where(custom_data_field) .can_display_and_sorted.is_approved .filter_by_categories(categories,false).filter_by_tags(tags).to_a end if !(defined? SiteFeed).nil? if @type != "show_widget" feeds_custom_anns = get_feed_custom_announcements("index") else feeds_custom_anns = [] end end else custom_announcements = [] if @type != "show_widget" feeds_custom_anns = get_feed_custom_announcements("index",params["source"]) else feeds_custom_anns = [] end end if !feeds_custom_anns.blank? if custom_announcements.count != 0 top_custom_anns = custom_announcements.select{|v| v.is_top} + feeds_custom_anns.select{|v| v['is_top']} rest_all_custom_anns = feeds_custom_anns.select{|v| v['is_top'] != true} + custom_announcements.select{|v| !v.is_top} rest_custom_anns = rest_all_custom_anns.sort{|v1,v2| v2["postdate"]<=>v1["postdate"]} all_sorted = top_custom_anns.sort{|v1,v2| v2["postdate"]<=>v1["postdate"]} + rest_custom_anns else all_sorted = feeds_custom_anns.select{|v| v['is_top']}.sort{|v1,v2| v2["postdate"]<=>v1["postdate"]} + feeds_custom_anns.select{|v| v['is_top'] != true}.sort{|v1,v2| v2["postdate"]<=>v1["postdate"]} end all_filter = filter_by_keywords(all_sorted,params[:keywords],params[:stime],params[:etime]) else all_filter = filter_by_keywords(custom_announcements,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 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 = overridehtml.nil? ? params[:layout_type] : overridehtml f = File.join(Rails.root, 'app', 'templates', "#{@key}", 'modules', 'custom_announcement', "#{filename}.html.erb") if !File.exist?(f) f = File.join(Rails.root, 'app', 'templates', "#{@key}", 'modules', 'custom_announcement', "index.html.erb") if !File.exist?(f) return "