adbanner-test/app/controllers/ad_banners_controller.rb

178 lines
8.2 KiB
Ruby

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 ? '<div class="cycle-overlay"></div><div class="cycle-pager"></div>' : ""
alt_title = (b.title.nil? || b.title == "" ? "ad-banner image" : b.title)
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.outerHTML=('<a href=#{b.out_link} target=#{target}>'+el.outerHTML+'</a>');}})()\""
end
images << {
"slide_number" => (i+1),
"image_link" => image_link,
"image_alt" => "ad banner image",
"title" => b.title,
"alt_title" => alt_title,
"class" => klass,
"height" => adbanner.height,
"width" => adbanner.width,
"caption" => caption,
"context" => (b.context.tr('"',"'") rescue ""),
"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,
"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] == "1" rescue false
@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
alt_text = (ad_b.title.nil? || ad_b.title == "" ? "ad-banner image" : ad_b.title)
caption = i == 0 ? '<div class="cycle-overlay"></div><div class="cycle-pager"></div>' : ""
klass = i == 0 ? "active" : ""
if ad_b.link_open == "new_window"
target = "_blank"
else
target = ""
end
context = ad_b.context.tr('"',"'") rescue ""
alt_title = (ad_b.title.blank? ? "ad-banner image" : ad_b.title)
if ad_b.exchange_item == "1"
image_html = "<div class=\"w-ad-banner__slide w-ba-banner__slide #{klass}\"
data-link=\"#{ad_b.out_link || "#"}'\"
data-cycle-title=\"#{ad_b.title}\"
data-cycle-desc=\"#{context}\"
data-overlay-template=\"<h3>#{ad_b.title}</h3><p>#{context}</p>\"
data-target=\"#{target}\"
>
<a href=\"#{ad_b.out_link}\" target=\"#{target}\" title=\"#{alt_title}\">
<img class='w-ad-banner__image' src='#{image_link}'>
</a>
<div class='w-ad-banner__caption_text'>#{ad_b.title}</div>
</div>"
elsif ad_b.exchange_item == "3"
klass = (i == 0 ? "active" : "")
video_url = ad_b.video_file.url
title = (ad_b.title.blank? ? File.basename(video_file) : ad_b.title)
image_html = "<div class=\"w-ad-banner__slide w-ba-banner__slide #{klass} jplayer_slide\"
data-link=\"#{ad_b.out_link || "#"}'\"
data-cycle-title=\"#{ad_b.title}\"
data-cycle-desc=\"#{context}\"
data-overlay-template=\"<h3>#{ad_b.title}</h3><p>#{context}</p>\"
data-target=\"#{target}\"
style=\"height: 100%;\"
>
#{ render_to_string(partial: "admin/ad_images/jplayer",locals:{i: i,file_name: title,file_url: video_url,:@autoplay_video=>(@autoplay_video && i == 0),:@apply_autoplay_script=>@autoplay_video,:@hide_video_tools=>@hide_video_tools}, layout: false).to_str }
</div>"
has_jplayer = true
elsif ad_b.exchange_item == "2"
youtube_url = format_url(ad_b.youtube,i)
image_html = "<div class=\"w-ad-banner__slide w-ba-banner__slide #{klass} youtube youtube_slide\"
data-yt-binded=\"0\"
data-youtube-id=\"ytplayer#{i}\"
data-link=\"#{ad_b.out_link || "#"}'\"
data-cycle-title=\"#{ad_b.title}\"
data-cycle-desc=\"#{context}\"
data-overlay-template=\"<h3>#{ad_b.title}</h3><p>#{context}</p>\"
data-target=\"#{target}\"
>
<div style=\"padding-bottom: 56.25%;position: relative;height: 0;\">
<iframe height=\"100%\" width=\"100%\"
src=\"#{youtube_url}?enablejsapi=1\" title=\"youtube\"
style=\"position: absolute;\"
data-yt-api-binded=\"0\" allowfullscreen>
</iframe>
</div>
<div class='w-ad-banner__caption_text'>#{ad_b.title}</div>
</div>"
end
images << {
"slide_number" => (i+1),
"html" => image_html
}
end
end
extra_before_html = ""
extra_after_html = ""
if has_jplayer
extra_before_html = "<link href=\"/assets/ad_banner/jplayer.blue.monday.min.css\" rel=\"stylesheet\">"
extra_after_html = "<script src=\"/assets/ad_banner/jquery.jplayer.min.js\"></script>"
end
{
"extras" => {
"ad_fx" => adbanner.ad_fx,
"speed" => adbanner.speed,
"title" => adbanner.title,
"timeout" => (adbanner.timeout * 1000),
"more" => "More",
"desc" => adbanner.title,
"extra_brefore_html" => extra_before_html,
"extra_after_html" => extra_after_html,
"extra_ready_script" => (@autoplay_video ? "if(i == 0) event.target.mute().playVideo();" : ""),
"extra_state_chnage_script" => (@autoplay_video ? "if(event.data == YT.PlayerState.ENDED){ var current_cycle; cyclediv.cycle(\"pause\") && cyclediv.cycle(\"next\") && (current_cycle=cyclediv.find(\".cycle-slide-active\"), (current_cycle.hasClass(\"jplayer_slide\") ? current_cycle.find('.jp-jplayer').jPlayer(\"mute\", true).jPlayer(\"play\",0) : current_cycle.find('iframe').data(\"yt_player\").mute().playVideo()));}" : ""),
"extra_document_ready_script" => (@autoplay_video ? "opts.API.pause() && opts.API.jump(0);" : "")
},
"images" => images
}
end
def format_url(url,index)
uri = URI.parse(url)
ps = uri.query.to_s.split("&")
url_params = {}
ps.each do |p|
x = p.split("=")
url_params[x.first] = x.last
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