gallery/app/controllers/galleries_controller.rb

240 lines
9.2 KiB
Ruby

class GalleriesController < ApplicationController
find_tag = Tag.all.select{|value| value.name==I18n.t('gallery.not_show_desc')}
if find_tag.length==0
module_app_id = ModuleApp.where(:key=>"gallery").first[:_id]
tags = ModuleApp.where(:key=>"gallery").first.tags
tag0 = Tag.new(is_default: false,module_app_ids: [module_app_id])
tag1 = Tag.new(is_default: false,module_app_ids: [module_app_id])
nowlocale = I18n.locale
I18n.available_locales.each do |locale|
I18n.locale = locale
tag0.name = I18n.t('gallery.show_desc')
tag1.name = I18n.t('gallery.not_show_desc')
end
I18n.locale = nowlocale
tag0.save
tag1.save
tags << tag0
tags << tag1
elsif find_tag.length>1
show_tags = Tag.all.select{|value| value.name==I18n.t('gallery.show_desc')}
show_tags.each_with_index do |show_tag,index1|
if index1>0
if show_tag.taggings.count==0
show_tag.delete
end
end
end
find_tag.each_with_index do |not_show_tag,index1|
if index1>0
if not_show_tag.taggings.count==0
not_show_tag.delete
end
end
end
end
def index
@album_setting = AlbumSetting.first
page_no = OrbitHelper.page_number
data_count = OrbitHelper.page_data_count
albums = Album.filter_by_categories.filter_by_tags.asc(:order).page(page_no).per(data_count)
album_color_map = AlbumColor.where(:album_id.in=> albums.pluck(:id)).pluck(:album_id,:color,:album_card_background_color,:album_card_text_color).map{|v| [v[0],v[1..-1]]}.to_h
galleries = albums.collect do |a|
doc = Nokogiri::HTML(a.description.to_s)
alt_text = doc.text.empty? ? 'gallery image' : doc.text
colors = album_color_map[a.id] || []
thumb_src = a.cover_path || "/assets/gallery/default.jpg"
{
"album-name" => a.name,
"album-description" => a.description,
"alt_title" => alt_text,
"link_to_show" => OrbitHelper.url_to_show(a.to_param),
"thumb-src" => thumb_src,
"src" => thumb_src.sub('thumb_','') ,
"thumb-large-src" => thumb_src.sub("thumb_","thumb_large_"),
"mobile-src" => thumb_src.sub("thumb_","mobile_"),
"theater-src" => thumb_src.sub("thumb_","theater_"),
"album_color" => iterate_data(colors[1],colors[0],@album_setting.album_card_background_color,'transparent'),
"album_card_text_color" => iterate_data(colors[2],@album_setting.album_card_text_color)
}
end
{
"albums" => galleries,
"extras" => {"widget-title"=>"Gallery"},
"total_pages" => albums.total_pages
}
end
def show
@album_setting = AlbumSetting.first
params = OrbitHelper.params
album = Album.find_by_param(params[:uid].to_s)
flag = show_desc?
colors = (AlbumColor.where(:album_id=> album.id).pluck(:color,:album_card_background_color,:album_card_text_color)[0])||[] rescue []
images = album.album_images.asc(:order).collect do |a|
alt_text = (a.description.blank? ? "gallery image" : Nokogiri::HTML(a.description.to_s).text())
{
"image-description" => (flag ? a.description : ''),
"image_short_description" => a.title,
"alt_title" => alt_text,
"link_to_show" => "#" + a.id.to_s,
"src" => a.file.url,
"thumb-src" => a.file.thumb.url,
"thumb-large-src" => a.file.thumb_large.url,
"mobile-src" => a.file.mobile.url,
"theater-src" => a.file.theater.url,
"album_color" => iterate_data(colors[1],colors[0],@album_setting.album_card_background_color,'transparent'),
"album_card_text_color" => iterate_data(colors[2],@album_setting.album_card_text_color)
}
end
{
"images" => images,
"data" => {"album-title"=>album.name,
"album-description" => (flag ? "<p><span>#{album.description}</span></p>" : "")}
}
end
def iterate_data(*args)
tmp = nil
args.each do |arg|
if !arg.blank? && arg != 'transparent'
tmp = arg
break
end
end
tmp
end
def album_widget
@album_setting = AlbumSetting.first
params = OrbitHelper.params
tags = OrbitHelper.widget_tags.empty? ? ["all"] : OrbitHelper.widget_tags
albums = Album.filter_by_widget_categories(OrbitHelper.widget_categories,false).filter_by_tags(tags)
album_color_map = AlbumColor.where(:album_id.in=> albums.pluck(:id)).pluck(:album_id,:color,:album_card_background_color,:album_card_text_color).map{|v| [v[0],v[1..-1]]}.to_h
galleries = albums.collect.with_index do |a,i|
doc = Nokogiri::HTML(a.description.to_s)
alt_text = doc.text.empty? ? 'gallery image' : doc.text.strip
colors = album_color_map[a.id] || []
thumb_src = a.cover_path || "/assets/gallery/default.jpg"
cover_image = AlbumImage.find(a.cover) rescue a.album_images.first
image_description = a.description
image_short_description = a.name
{
"link_text" => a.name,
"album-name" => a.name,
"album-description" => a.description,
"alt_title" => alt_text,
"link_to_show" => OrbitHelper.widget_more_url + "/" + a.to_param + "#" + (cover_image.id.to_s rescue ""),
"src" => thumb_src.sub("thumb_",""),
"thumb-src" => thumb_src,
"thumb-large-src" => thumb_src.sub("thumb_","thumb_large_"),
"mobile-src" => thumb_src.sub("thumb_","mobile_"),
"theater-src" => thumb_src.sub("thumb_","theater_"),
"image_description" => image_description,
"image_short_description" => image_short_description,
"album_color" => iterate_data(colors[1],colors[0],@album_setting.album_card_background_color,'transparent'),
"album_card_text_color" => iterate_data(colors[2],@album_setting.album_card_text_color),
"i" => i
}
end
{
"images" => galleries,
"extras" => {"widget-title"=>"Gallery","more_url" => OrbitHelper.widget_more_url}
}
end
def widget
@album_setting = AlbumSetting.first
tags = OrbitHelper.widget_tags.empty? ? ["all"] : OrbitHelper.widget_tags
albums = Album.filter_by_widget_categories(OrbitHelper.widget_categories,false).filter_by_tags(tags).to_a
album_ids = []
albums_map = {}
albums.each do |ab|
album_ids << ab.id
albums_map[ab.id] = ab
end
album_color_map = AlbumColor.where(:album_id.in=> album_ids).pluck(:album_id,:color,:album_card_background_color,:album_card_text_color).map{|v| [v[0],v[1..-1]]}.to_h
params = OrbitHelper.params
counts = OrbitHelper.widget_data_count
images = AlbumImage.where(:album_id.in=>album_ids).desc(:id).limit(counts *5).sample(counts)
images = images.each_with_index.collect do |a,i|
colors = album_color_map[a.album_id] || []
alt_text = (a.description.blank? ? "gallery image" : Nokogiri::HTML(a.description).text().strip)
f = a.file
ab = albums_map[a.album_id]
{
"link_text" => "",
"link_to_show" => OrbitHelper.widget_more_url + "/" + ab.to_param + "#" + a.id.to_s,
"alt_title" => alt_text,
"src" => f.url,
"thumb-src" => f.thumb.url,
"thumb-large-src" => f.thumb_large.url,
"image_description" => a.description,
"image_short_description" => a.title,
"mobile-src" => f.mobile.url,
"theater-src" => f.theater.url,
"album-name" => ab.name_translations[I18n.locale],
"album_color" => iterate_data(colors[1],colors[0],@album_setting.album_card_background_color,'transparent'),
"album_card_text_color" => iterate_data(colors[2],@album_setting.album_card_text_color),
"i" => i
}
end
{
"images" => images,
"extras" => {"widget-title"=>"Gallery","more_url" => OrbitHelper.widget_more_url}
}
end
def imgs(album_id)
album = Album.find(album_id)
images = album.album_images.asc(:order)
output = Array.new
images.each do |values|
alt_text = (values.description.nil? || values.description == "" ? "gallery image" : values.description)
output << { _id: values.id.to_s,
description: values.description,
title: values.title,
alt_title: alt_text,
url: values.file.url,
file: values.file.as_json[:file],
gallery_album_id: values.album_id,
tags: values.tags}
end
return output
end
def theater
if params[:id].include?('page=')
album = Album.where(uid: params[:id].sub('page=','')).first
albumid = album.id
image = album.album_images.first
else
image = AlbumImage.find(params[:id])
albumid = image.album_id
album = Album.find(albumid)
end
images = album.album_images.asc(:order)
data = {
"album" => album,
"image" => image.id.to_s,
"images" => imgs(albumid)
}
render :json => {"data" => data}.to_json
end
private
def show_desc?
tags = OrbitHelper.page_tags if tags.blank?
tags = [tags].flatten.uniq
flag = true
tag_temp = Tag.all.select{|value| tags.include? value.id.to_s}
tag_temp_length = 0
tag_temp.each do |value|
if value.name==I18n.t('gallery.show_desc')
flag = true
elsif value.name==I18n.t('gallery.not_show_desc')
flag = false
end
end
flag
end
end