video_pro/app/controllers/video_pros_controller.rb

277 lines
10 KiB
Ruby

class VideoProsController < ApplicationController
def self.custom_widget_data
ac = ActionController::Base.new
ac.render_to_string("video_pros/custom_widget_data",:locals=>{:@custom_data_field=>@custom_data_field,:@field_name=>@field_name})
end
def show
params = OrbitHelper.params
uid = params[:uid]
video_image = VideoImage.where(:uid => uid).first
snapshot_url = video_image && video_image.get_snapshot_url
return if snapshot_url.nil?
video_image.view_count += 1
video_image.inc_count
{
"video_image" => video_image
}
end
def show_api
uid = params[:uid]
@video_image = VideoImage.where(:uid => uid).first
snapshot_url = @video_image && @video_image.get_snapshot_url
if snapshot_url.nil?
render :file => "#{Rails.root}/app/views/errors/404.html", :layout => false, :status => :not_found, :formats => [:html]
return
end
@video_image.view_count += 1
@video_image.inc_count
render :layout=>false
end
def index
params = OrbitHelper.params
module_app = ModuleApp.where(:key=>'video_pro').first
page = OrbitHelper.page rescue Page.where(:url=>params[:url]).first
page_categories = OrbitHelper.page_categories
categories = [{:name=>I18n.t("video_pro.all_category"), :value=>"all"}]
if page_categories.blank? || page_categories.include?('all')
categories_for_page = module_app.categories
else
categories_for_page = Category.where(:id=>page_categories)
end
categories += categories_for_page.map do |c|
{:name=>c.title, :value=>c.id.to_s}
end
page_tags = OrbitHelper.page_tags
tags = [{:name=>I18n.t("video_pro.all_tag"), :value=>"all"}]
if page_tags.blank? || page_tags.include?('all')
tags_for_page = module_app.tags
else
tags_for_page = Tag.where(:id=>page_tags)
end
tags += tags_for_page.map do |c|
{:name=>c.name, :value=>c.id.to_s}
end
selected_category = params["category"]
if selected_category.present? && selected_category != 'all'
categories.each do |h|
if h[:value] == selected_category
h[:is_selected] = "selected"
break
end
end
filter_categories = [selected_category]
else
filter_categories = page_categories
end
@video_pro_setting = VideoProSetting.first
post_agencies = []
id_name_mapping = User.where(:id.in=>@video_pro_setting.exist_user_ids.to_a).pluck(:id, "member_name.#{I18n.locale}").map{|k,v| [k.to_s, (v ? v.values.last : nil)]}.to_h
if @video_pro_setting.enable_name_mapping
@video_pro_setting.user_mapping.to_h.each do |id, name_trans|
id_name_mapping[id] = name_trans
end
end
@video_pro_setting.exist_user_ids.each do |user_id|
name_trans = id_name_mapping[user_id]
post_agencies << {:name=>name_trans, :value=>"m-#{user_id}"} if name_trans.present?
end
tmp_post_agencies = @video_pro_setting.post_agencies
@video_pro_setting.exist_post_agencies.each do |idx|
name_trans = tmp_post_agencies[idx]
post_agencies << {:name=>name_trans, :value=>"p-#{idx}"} if name_trans.present?
end
selected_post_agency = params["poster"]
if selected_post_agency.blank? && post_agencies.count > 0 && params["layout_type"].include?('post_agency')
selected_post_agency = post_agencies[0][:value]
end
if selected_post_agency.present?
post_agencies.each do |h|
if h[:value] == selected_post_agency
h[:is_selected] = "selected"
break
end
end
end
post_agencies = post_agencies.map do |post_agency|
{"option"=>"<option value=\"#{post_agency[:value]}\" #{post_agency[:is_selected]}>#{post_agency[:name]}</option>"}
end
video_images = VideoImage.where(:title.nin => ["",nil])
page_url = "/#{I18n.locale}#{(page.get_url rescue page.url)}"
page_tabs = [
{"klass"=>"", "url"=>page_url, "title"=>I18n.t('video_pro.latest_ranking')},
{"klass"=>"", "url"=>page_url + '?hot=hot', "title"=>I18n.t('video_pro.top_ranking')}
]
if params[:hot] == 'hot'
video_images = video_images.order(:view_count=>-1)
page_tabs[1]["klass"] = "now_view"
else
page_tabs[0]["klass"] = "now_view"
end
selected_tag = params['tags']
page_title = page.name
if selected_tag.present?
tags.each do |h|
if h[:value] == selected_tag
h[:is_selected] = "selected"
break
end
end
filter_tags = [selected_tag]
tag = Tag.where(id: selected_tag).first
if tag
page_title = tag.name
end
else
filter_tags = OrbitHelper.page_tags
end
video_images = video_images.can_display_and_sorted.filter_by_categories(filter_categories).filter_by_tags(filter_tags)
search_value = params[:search].to_s.strip
if search_value.present?
keywords = search_value.split(/\s+(?=(?:[^"]*"[^"]*")*[^"]*$)/)
regex = Regexp.union(keywords.map{|word| Regexp.new(".*"+Regexp.escape(word)+".*", "i")})
video_images = video_images.where({"title" => regex})
end
if selected_post_agency.present?
selected_type = selected_post_agency[0]
if selected_type == 'm'
video_images = video_images.where(:use_override_post_agency=>false, :update_user_id=>selected_post_agency.split('-').last)
elsif selected_type == 'p'
video_images = video_images.where(:use_override_post_agency=>true, :override_post_agency=>selected_post_agency.split('-').last)
end
end
custom_data_field = (page.custom_data_field || {} rescue {})
videos = get_videos(video_images, OrbitHelper.url_to_show(''), custom_data_field)
hd_icon_url = "/assets/video_pro/icon_hd.png"
view_icon_url = "/assets/video_pro/icon_view.png"
search_placeholder = I18n.t("video_pro.search_placeholder")
search_text1 = I18n.t("video_pro.search_text1")
search_text2 = I18n.t("video_pro.search_text2")
categories = categories.map do |category|
{"option"=>"<option value=\"#{category[:value]}\" #{category[:is_selected]}>#{category[:name]}</option>"}
end
tags = tags.map do |tag|
{"option"=>"<option value=\"#{tag[:value]}\" #{tag[:is_selected]}>#{tag[:name]}</option>"}
end
{
"extras" => {
"search_placeholder" => search_placeholder,
"search_text1" => search_text1,
"search_text2" => search_text2,
"search_value" => search_value,
"keyword_trans" => I18n.t("video_pro.keyword"),
"view_count_trans" => I18n.t("video_pro.view_count"),
"hd_icon_url" => hd_icon_url,
"view_icon_url" => view_icon_url,
"more" => "More",
"page-title" => page_title
},
"categories" => categories,
"tags" => tags,
"post_agencies" => post_agencies,
"page_tabs" => page_tabs,
"videos" => videos,
"total_pages" => video_images.total_pages
}
end
def widget
subpart = OrbitHelper.get_current_widget
if subpart.widget_type.include?('jqcloud')
tags = subpart.tags
if tags.include?('all')
tags = Tag.where(:module_app_ids.in=>ModuleApp.where(:key=>'video_pro').pluck(:id)).to_a
else
tags = Tag.where(:id.in=>tags).to_a
end
show_url = OrbitHelper.widget_item_url('')
jqcloud_words = tags.map do |tag|
{
"text" => tag.name,
"weight"=> Tagging.where(:tag_id=>tag).count,
"link" => "#{show_url}?tags=#{tag.id}"
}
end.sort_by{|h| h["weight"]}.reverse!
{
"extras" => {
"jqcloud_words" => jqcloud_words.to_json
}
}
else
video_images = VideoImage.where(:title.nin => ["",nil]).can_display_and_sorted.filter_by_widget_categories.to_a
custom_data_field = subpart.custom_data_field || {}
@video_pro_setting = VideoProSetting.first
videos = get_videos(video_images, OrbitHelper.widget_item_url(''), custom_data_field)
hd_icon_url = "/assets/video_pro/icon_hd.png"
view_icon_url = "/assets/video_pro/icon_view.png"
{
"extras" => {
"keyword_trans" => I18n.t("video_pro.keyword"),
"view_count_trans" => I18n.t("video_pro.view_count"),
"hd_icon_url" => hd_icon_url,
"view_icon_url" => view_icon_url,
"more" => "More",
},
"videos" => videos
}
end
end
private
def get_videos(video_images, show_url, custom_data_field={})
@autoplay_video = custom_data_field[:autoplay_video] rescue "0"
@hide_video_tools = custom_data_field[:hide_video_tools] == "1" rescue false
videos = []
video_images.each_with_index do |video_image,i|
if video_image.language_enabled.include?(I18n.locale.to_s)
snapshot_url = video_image.get_snapshot_url
next if snapshot_url.nil?
video_post_agency = video_image.post_agency(@video_pro_setting)
video_postdate = video_image.postdate.to_s
video_category = video_image.category_title
video_tags = video_image.tags
video_tag = (video_tags.count > 0 ? video_tags.first.name : video_category)
statuses = video_image.statuses_with_classname
video_desc = video_image.desc
video_desc_escape = video_desc.gsub('"',"&quot;") rescue ""
video_title = video_image.title.to_s
video_title_escape = video_title.gsub('"','&quot;')
video_keyword = video_image.keyword.to_s
if video_image.exchange_item == "1"
video_url = video_image.embed_url
video_webm_url = ""
video_type = "youtube"
elsif video_image.exchange_item == "2"
video_url = video_image.video_file.url rescue nil
video_type = "jplayer"
video_webm_url = video_image.video_file_webm.url rescue nil
has_jplayer = true
end
slug = video_image.to_param
api_url = "/xhr/video_pro/show_api/#{slug}"
videos << {
"api_url" => api_url,
"video_show_url" => show_url + slug,
"video_category" => video_category,
"video_tag" => video_tag,
"statuses" => statuses,
"video_post_agency" => video_post_agency,
"video_postdate" => video_postdate,
"video_url" => video_url,
"video_webm_url" => video_webm_url,
"video_type" => video_type,
"snapshot_url" => snapshot_url,
"video_title" => video_title,
"video_title_escape" => video_title_escape,
"video_desc" => video_desc,
"video_keyword" => video_keyword,
"view_count" => video_image.view_count
}
end
end
videos
end
end