class AdBannersController < ApplicationController def self.custom_widget_data ac = ActionController::Base.new ac.render_to_string("ad_banners/custom_widget_data",:locals=>{:@custom_data_field=>@custom_data_field,:@field_name=>@field_name}) end def widget adbanner = Banner.find(OrbitHelper.widget_custom_value) widget = OrbitHelper.get_current_widget if widget.widget_type.include?("_video") return video_widget(adbanner) else return image_widget(adbanner) end end private def image_widget(adbanner) images = [] adbanner.ad_images.can_display.asc(:sort_number,:created_at).each.with_index do |b,i| if b.language_enabled.include?(I18n.locale.to_s) image_link = OrbitHelper.is_mobile_view ? b.file.mobile.url : b.file.url klass = i == 0 ? "active" : "" caption = i == 0 ? '
' : "" banner_title = b.title.to_s banner_title_escape = banner_title.gsub('"','"') alt_title = (banner_title_escape.blank? ? "Banner image" : banner_title_escape) if b.link_open == "new_window" target = "_blank" else target = "" end if !b.out_link.blank? image_link += "\" style=\"cursor:pointer;\" onload=\"var el=this;(function(){if((el.parentElement==undefined||el.parentElement.tagName!='A')&&$('

'+$(el).parents('.w-ba-banner__slide').eq(0).attr('data-overlay-template')+'

').find('a').length==0){el.outerHTML=(''+el.outerHTML+'');}})()\"" end context = (b.context.tr('"',"'") rescue "") images << { "slide_number" => (i+1), "image_link" => image_link, "image_alt" => "Banner image", "title" => banner_title_escape, "alt_title" => alt_title, "class" => klass, "height" => adbanner.height, "width" => adbanner.width, "caption" => caption, "context" => context, "desc" => context, "link" => b.out_link || "#", "target" => target } end end base_image = adbanner.base_image.nil? ? 1 : adbanner.base_image { "extras" => { "ad_fx" => adbanner.ad_fx, "speed" => adbanner.speed, "title" => adbanner.title.to_s.gsub('"','"'), "banner-height" => adbanner.height, "banner-width" => adbanner.width, "base_image" => (base_image - 1), "timeout" => (adbanner.timeout * 1000), "more" => "More" }, "images" => images } end def video_widget(adbanner) subpart = OrbitHelper.get_current_widget custom_data_field = subpart.custom_data_field || {} @autoplay_video = custom_data_field[:autoplay_video] rescue "0" @hide_video_tools = custom_data_field[:hide_video_tools] == "1" rescue false images = [] has_jplayer = false adbanner.ad_images.can_display.asc(:sort_number).each_with_index do |ad_b,i| if ad_b.language_enabled.include?(I18n.locale.to_s) image_link = OrbitHelper.is_mobile_view ? ad_b.file.mobile.url : ad_b.file.url caption = i == 0 ? '
' : "" klass = i == 0 ? "active" : "" if ad_b.link_open == "new_window" target = "_blank" else target = "" end context = ad_b.context.tr('"',"'") rescue "" banner_title = ad_b.title.to_s banner_title_escape = banner_title.gsub('"','"') alt_title = (banner_title_escape.blank? ? "Banner image" : banner_title_escape) if ad_b.exchange_item == "1" image_html = "
#{banner_title_escape}

#{context}

\" data-target=\"#{target}\" >
#{banner_title}
" elsif ad_b.exchange_item == "3" klass = (i == 0 ? "active" : "") video_url = ad_b.video_file.url rescue nil video_webm_url = ad_b.video_file_webm.url rescue nil @file_poster = ad_b.video_snapshot.url rescue nil title = (banner_title.blank? ? File.basename(video_url) : banner_title) image_html = "
#{banner_title_escape}

#{context}

\" data-target=\"#{target}\" style=\"height: 100%;\" data-autoplay=\"#{@autoplay_video}\" > #{ render_to_string(partial: "admin/ad_images/jplayer",locals:{i: (subpart.id.to_s+i.to_s),file_name: title,file_url: video_url,file_webm_url: video_webm_url,:@autoplay_video=>(@autoplay_video == "1" && i == 0),:@apply_autoplay_script=>true,:@hide_video_tools=>@hide_video_tools,:@is_frontend=>true,:@file_poster=>@file_poster}, layout: false).to_str }
" has_jplayer = true elsif ad_b.exchange_item == "2" youtube_url = format_url(ad_b.youtube,i) image_html = "
#{banner_title_escape}

#{context}

\" data-target=\"#{target}\" data-autoplay=\"#{@autoplay_video}\" >
#{ad_b.title}
" end images << { "slide_number" => (i+1), "html" => image_html } end end extra_before_html = "" extra_after_html = "" if has_jplayer extra_before_html = "\n" + ActionController::Base.helpers.stylesheet_link_tag("banner/jplayer.blue.monday.min.css") + "\n" + ActionController::Base.helpers.javascript_include_tag("banner/jquery.jplayer.min.js") + "\n" end base_image = adbanner.base_image.nil? ? 1 : adbanner.base_image adbanner_title_escape = adbanner.title.to_s.gsub('"','"') { "extras" => { "ad_fx" => adbanner.ad_fx, "speed" => adbanner.speed, "title" => adbanner_title_escape, "base_image" => (base_image - 1), "timeout" => (adbanner.timeout * 1000), "more" => "More", "desc" => adbanner_title_escape, "extra_before_html" => extra_before_html, "extra_after_html" => extra_after_html, "extra_ready_script" => "if (i == 0 && banner_wrap.find('.w-ad-banner__slide').index($(iframe).parents('.w-ad-banner__slide').eq(0))==0 && banner_wrap.find('[data-autoplay=1]').length != 0){ event.target.mute().playVideo(); }", "extra_state_chnage_script" => "if(cyclediv.find('[data-autoplay=1]').length != 0){ if(event.data == YT.PlayerState.ENDED){ var current_cycle; cyclediv.cycle(\"pause\"); cyclediv.cycle(\"next\"); current_cycle = cyclediv.find(\".cycle-slide-active\"); if (current_cycle.hasClass(\"jplayer_slide\")){ current_cycle.find('.jp-jplayer').jPlayer(\"mute\", true).jPlayer(\"play\",0); }else{ current_cycle.find('iframe').data(\"yt_player\").mute().playVideo(); } } }", "extra_document_ready_script" => ((@autoplay_video == "1") ? "opts.API.pause() && opts.API.jump(0);" : "") }, "images" => images } end def format_url(url,index) uri = URI.parse(url) url_params = {} if !uri.nil? && uri.host == "www.youtube.com" if uri.path.start_with?('/embed/') url_params['v'] = uri.path.split('/embed/').last else params = CGI.parse(uri.query.to_s) v = params.blank? ? nil : params['v'].first url_params['v'] = v end end # url = "http://www.youtube.com/v/#{url_params["v"]}/?version=3&playerapiid=ytplayer#{index}" url = "https://www.youtube.com/embed/#{url_params["v"]}" end end