adbanner-test/app/controllers/ad_banners_controller.rb

214 lines
9.9 KiB
Ruby
Raw Permalink Normal View History

2014-04-11 07:47:33 +00:00
class AdBannersController < ApplicationController
def self.custom_widget_data
ac = ActionController::Base.new
2023-07-05 01:42:03 +00:00
ac.instance_variable_set(:@custom_data_field, @custom_data_field)
ac.instance_variable_set(:@field_name, @field_name)
ac.render_to_string("ad_banners/custom_widget_data")
end
2014-04-11 07:47:33 +00:00
def widget
2014-05-23 05:51:04 +00:00
adbanner = Banner.find(OrbitHelper.widget_custom_value)
2021-04-10 09:12:40 +00:00
widget = OrbitHelper.get_current_widget
if widget.widget_type.include?("_video")
2021-04-10 09:12:40 +00:00
return video_widget(adbanner)
else
return image_widget(adbanner)
end
end
private
def image_widget(adbanner)
2016-05-18 08:29:39 +00:00
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 ? '<div class="cycle-overlay"></div><div class="cycle-pager"></div>' : ""
2022-07-31 02:27:31 +00:00
banner_title = b.title.to_s
banner_title_escape = banner_title.gsub('"','&quot;')
alt_title = (banner_title_escape.blank? ? "Banner image" : banner_title_escape)
2015-05-11 10:19:57 +00:00
2016-05-18 08:29:39 +00:00
if b.link_open == "new_window"
target = "_blank"
else
target = ""
end
if !b.out_link.blank?
2022-01-24 02:49:16 +00:00
image_link += "\" style=\"cursor:pointer;\" onload=\"var el=this;(function(){if((el.parentElement==undefined||el.parentElement.tagName!='A')&&$('<p>'+$(el).parents('.w-ba-banner__slide').eq(0).attr('data-overlay-template')+'</p>').find('a').length==0){el.outerHTML=('<a href=#{b.out_link} target=#{target}>'+el.outerHTML+'</a>');}})()\""
end
2022-07-04 09:47:28 +00:00
context = (b.context.tr('"',"'") rescue "")
2016-05-18 08:29:39 +00:00
images << {
2021-06-18 02:03:21 +00:00
"slide_number" => (i+1),
2016-05-18 08:29:39 +00:00
"image_link" => image_link,
2022-07-31 02:27:31 +00:00
"image_alt" => "Banner image",
"title" => banner_title_escape,
2016-05-18 08:29:39 +00:00
"alt_title" => alt_title,
"class" => klass,
"height" => adbanner.height,
"width" => adbanner.width,
"caption" => caption,
2022-07-04 09:47:28 +00:00
"context" => context,
"desc" => context,
2016-05-18 08:29:39 +00:00
"link" => b.out_link || "#",
"target" => target
}
end
2014-05-23 05:51:04 +00:00
end
2015-11-25 13:28:27 +00:00
base_image = adbanner.base_image.nil? ? 1 : adbanner.base_image
2014-05-23 05:51:04 +00:00
{
"extras" => {
2014-04-15 07:51:02 +00:00
"ad_fx" => adbanner.ad_fx,
"speed" => adbanner.speed,
2022-07-31 02:27:31 +00:00
"title" => adbanner.title.to_s.gsub('"','&quot;'),
2015-10-12 07:33:45 +00:00
"banner-height" => adbanner.height,
"banner-width" => adbanner.width,
2015-11-25 13:28:27 +00:00
"base_image" => (base_image - 1),
2015-10-06 07:30:59 +00:00
"timeout" => (adbanner.timeout * 1000),
2022-07-04 09:47:28 +00:00
"more" => "More"
2014-05-23 05:51:04 +00:00
},
"images" => images
2014-04-11 07:47:33 +00:00
}
end
2021-04-10 09:12:40 +00:00
def video_widget(adbanner)
subpart = OrbitHelper.get_current_widget
custom_data_field = subpart.custom_data_field || {}
2023-07-05 01:42:03 +00:00
data_autoplay = custom_data_field[:autoplay_video] rescue "0"
@is_autoplay_video = (data_autoplay == "1")
@hide_video_tools = (custom_data_field[:hide_video_tools] == "1" rescue false)
2021-04-10 09:12:40 +00:00
images = []
has_jplayer = false
2021-04-10 09:12:40 +00:00
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 ? '<div class="cycle-overlay"></div><div class="cycle-pager"></div>' : ""
klass = i == 0 ? "active" : ""
2021-04-10 15:16:17 +00:00
if ad_b.link_open == "new_window"
target = "_blank"
else
target = ""
end
context = ad_b.context.tr('"',"'") rescue ""
2022-07-31 02:27:31 +00:00
banner_title = ad_b.title.to_s
banner_title_escape = banner_title.gsub('"','&quot;')
alt_title = (banner_title_escape.blank? ? "Banner image" : banner_title_escape)
2021-04-10 09:12:40 +00:00
if ad_b.exchange_item == "1"
2021-08-25 08:54:12 +00:00
image_html = "<div class=\"w-ad-banner__slide w-ba-banner__slide #{klass}\"
2022-07-06 01:23:13 +00:00
data-link=\"#{ad_b.out_link || "#"}\"
2022-07-31 02:27:31 +00:00
data-cycle-title=\"#{banner_title_escape}\"
2021-04-10 15:16:17 +00:00
data-cycle-desc=\"#{context}\"
2022-07-31 02:27:31 +00:00
data-overlay-template=\"<h3>#{banner_title_escape}</h3><p>#{context}</p>\"
2021-04-10 15:16:17 +00:00
data-target=\"#{target}\"
>
<a href=\"#{ad_b.out_link}\" target=\"#{target}\" title=\"#{alt_title}\">
<img class='w-ad-banner__image' src='#{image_link}'>
</a>
2022-07-31 02:27:31 +00:00
<div class='w-ad-banner__caption_text'>#{banner_title}</div>
2021-04-10 15:16:17 +00:00
</div>"
elsif ad_b.exchange_item == "3"
klass = (i == 0 ? "active" : "")
2021-10-22 15:31:10 +00:00
video_url = ad_b.video_file.url rescue nil
video_webm_url = ad_b.video_file_webm.url rescue nil
2022-01-05 07:52:40 +00:00
@file_poster = ad_b.video_snapshot.url rescue nil
2022-07-31 02:27:31 +00:00
title = (banner_title.blank? ? File.basename(video_url) : banner_title)
2023-07-05 01:42:03 +00:00
@autoplay_video = (@is_autoplay_video && i == 0)
@apply_autoplay_script = @is_autoplay_video
@is_frontend = true
2021-09-02 02:40:58 +00:00
image_html = "<div class=\"w-ad-banner__slide w-ba-banner__slide #{klass} jplayer_slide\"
2022-07-06 01:23:13 +00:00
data-link=\"#{ad_b.out_link || "#"}\"
2022-07-31 02:27:31 +00:00
data-cycle-title=\"#{banner_title_escape}\"
data-cycle-desc=\"#{context}\"
2022-07-31 02:27:31 +00:00
data-overlay-template=\"<h3>#{banner_title_escape}</h3><p>#{context}</p>\"
data-target=\"#{target}\"
style=\"height: 100%;\"
2023-07-05 01:42:03 +00:00
data-autoplay=\"#{data_autoplay}\"
>
2023-07-05 01:42:03 +00:00
#{ 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}, layout: false).to_str }
</div>"
has_jplayer = true
elsif ad_b.exchange_item == "2"
2021-04-10 09:12:40 +00:00
youtube_url = format_url(ad_b.youtube,i)
2021-09-02 02:40:58 +00:00
image_html = "<div class=\"w-ad-banner__slide w-ba-banner__slide #{klass} youtube youtube_slide\"
2021-04-10 15:16:17 +00:00
data-yt-binded=\"0\"
data-youtube-id=\"ytplayer#{i}\"
2022-07-06 01:23:13 +00:00
data-link=\"#{ad_b.out_link || "#"}\"
2022-07-31 02:27:31 +00:00
data-cycle-title=\"#{banner_title_escape}\"
2021-04-10 15:16:17 +00:00
data-cycle-desc=\"#{context}\"
2022-07-31 02:27:31 +00:00
data-overlay-template=\"<h3>#{banner_title_escape}</h3><p>#{context}</p>\"
2021-04-10 15:16:17 +00:00
data-target=\"#{target}\"
2023-07-05 01:42:03 +00:00
data-autoplay=\"#{data_autoplay}\"
2021-04-10 15:16:17 +00:00
>
<div style=\"padding-bottom: 56.25%;position: relative;height: 0;\">
<iframe height=\"100%\" width=\"100%\"
2021-04-10 16:34:57 +00:00
src=\"#{youtube_url}?enablejsapi=1\" title=\"youtube\"
style=\"position: absolute;\"
data-yt-api-binded=\"0\" allowfullscreen>
2021-04-10 15:16:17 +00:00
</iframe>
</div>
<div class='w-ad-banner__caption_text'>#{ad_b.title}</div>
</div>"
2021-04-10 09:12:40 +00:00
end
images << {
2021-06-18 02:03:21 +00:00
"slide_number" => (i+1),
2021-04-10 09:12:40 +00:00
"html" => image_html
}
end
end
extra_before_html = ""
extra_after_html = ""
if has_jplayer
2022-08-21 08:43:10 +00:00
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
2022-06-26 14:24:44 +00:00
base_image = adbanner.base_image.nil? ? 1 : adbanner.base_image
2022-07-31 02:27:31 +00:00
adbanner_title_escape = adbanner.title.to_s.gsub('"','&quot;')
2021-04-10 09:12:40 +00:00
{
"extras" => {
"ad_fx" => adbanner.ad_fx,
"speed" => adbanner.speed,
2022-07-31 02:27:31 +00:00
"title" => adbanner_title_escape,
2022-06-26 14:24:44 +00:00
"base_image" => (base_image - 1),
2021-04-10 15:16:17 +00:00
"timeout" => (adbanner.timeout * 1000),
"more" => "More",
2022-07-31 02:27:31 +00:00
"desc" => adbanner_title_escape,
2022-07-13 07:00:48 +00:00
"extra_before_html" => extra_before_html,
"extra_after_html" => extra_after_html,
2021-11-15 08:54:12 +00:00
"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){
2021-11-15 08:45:04 +00:00
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\");
2021-11-15 08:54:12 +00:00
if (current_cycle.hasClass(\"jplayer_slide\")){
2021-11-15 08:45:04 +00:00
current_cycle.find('.jp-jplayer').jPlayer(\"mute\", true).jPlayer(\"play\",0);
}else{
current_cycle.find('iframe').data(\"yt_player\").mute().playVideo();
}
}
}",
2023-05-03 03:14:31 +00:00
"extra_document_ready_script" => ((@autoplay_video == "1") ? "(opts.slides.eq(0).find('iframe,.jp-jplayer').length != 0) && opts.API.pause() && opts.API.jump(0);" : "")
2021-04-10 09:12:40 +00:00
},
"images" => images
}
end
def format_url(url,index)
uri = URI.parse(url)
url_params = {}
2022-10-04 05:14:34 +00:00
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
2021-04-10 09:12:40 +00:00
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
2014-04-11 07:47:33 +00:00
end