gallery/app/controllers/admin/galleries_controller.rb

633 lines
20 KiB
Ruby

require 'rubyXL'
class Admin::GalleriesController < OrbitAdminController
include Admin::GalleriesHelper
before_filter :setup_vars
before_action :authenticate_user, :except => "imgs"
before_action :log_user_action
layout :compute_layout
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 compute_layout
if action_name== 'index' && !params['page_no'].nil?
false
else
'back_end'
end
end
def rotate_images
begin
image_ids = params['image_ids'].split(',')
images = AlbumImage.find(image_ids)
count = images.length
rot_ang = params[:rotate_angle].to_i
images.each_with_index do |image,index|
if !(image.album_crops.first.nil?)
w_and_h = image.file.get_w_and_h
cords = image.album_crops.map{|v| [[v.crop_x.to_f,v.crop_y.to_f],
[v.crop_x.to_f,v.crop_y.to_f+v.crop_h.to_f],
[v.crop_w.to_f+v.crop_x.to_f,v.crop_y.to_f],
[v.crop_w.to_f+v.crop_x.to_f,v.crop_y.to_f+v.crop_h.to_f]]}[0]
rot_diff = rot_ang/90
if (rot_diff) % 2 == 1
new_w = (w_and_h[1]).to_f
new_h = (w_and_h[0]).to_f
else
new_w = (w_and_h[0]).to_f
new_h = (w_and_h[1]).to_f
end
rot_times = rot_diff % 4
deg = (90.0*rot_times/180*Math::PI)
cords = cords.collect do |cord|
x = cord[0]-w_and_h[0].to_f/2
y = cord[1]-w_and_h[1].to_f/2
[x*Math.cos(deg)-y*Math.sin(deg)+new_w/2,x*Math.sin(deg)+y*Math.cos(deg)+new_h/2]
end
all_x = cords.collect do |cord|
cord[0]
end
all_y = cords.collect do |cord|
cord[1]
end
image.album_crops.first.update_attributes(crop_x: all_x.min,crop_y: all_y.min,crop_w: all_x.max - all_x.min,crop_h: all_y.max - all_y.min)
end
end
variable = AlbumVariable.first
if variable.nil?
variable = AlbumVariable.new
end
variable.finish = false
variable.save!
Thread.new do
variable = AlbumVariable.first
images.each_with_index do |image,index|
variable.progress_percent = (index*100.0/count).floor.to_s+'%'
image.file.rotate_ang(rot_ang)
all_version = image.file.versions.map{|k,v| k}
begin
variable.progress_filename = image[:file].to_s
all_version.each do |version|
if !(version.to_s == 'resized' && crop_but_no_backup(image))
image.file.recreate_versions! version
image.save!
end
end
rescue => e
variable.progress_filename = e.inspect.to_s
puts e.inspect
end
variable.save!
end
variable.finish = true
variable.save!
end
rescue => e
puts e.inspect
end
redirect_to :controller=> 'images' ,:action => 'crop_process'
end
def save_crop
begin
images = AlbumImage.all.select{|value| params[:id].include? value.id.to_s}
id = images.first.album_id.to_s
x = params['x']
y = params['y']
w = params['w']
h = params['h']
cords = x.zip(y,w,h)
count = cords.length
images.each_with_index do |image,index|
cord = cords[index]
if image.album_crops.first.nil?
image.album_crops.create(crop_x: cord[0],crop_y: cord[1],crop_w: cord[2],crop_h: cord[3])
else
image.album_crops.first.update_attributes(crop_x: cord[0],crop_y: cord[1],crop_w: cord[2],crop_h: cord[3])
end
end
variable = AlbumVariable.first
if variable.nil?
variable = AlbumVariable.new
end
variable.finish = false
variable.save!
Thread.new do
variable = AlbumVariable.first
images.each_with_index do |image,index|
variable.progress_percent = (index*100.0/count).floor.to_s+'%'
all_version = image.file.versions.map{|k,v| k}
begin
#org_fname = image.file.path
#org_extname = File.extname(org_fname)
#org_fname.slice! org_extname
#FileUtils.cp(org_fname + org_extname, org_fname + '_resized' + org_extname)
variable.progress_filename = image[:file].to_s
all_version.each do |version|
if !(version.to_s == 'resized' && crop_but_no_backup(image))
image.file.recreate_versions! version
image.save!
end
end
rescue => e
variable.progress_filename = e.inspect.to_s
puts e.inspect
end
variable.save!
end
variable.finish = true
variable.save!
end
rescue => e
puts e.inspect
end
redirect_url = "/admin/galleries/crop_process"
render :json => {'href' => redirect_url}.to_json
end
def call_translate
text = params['text']
render :json => {'translate' => "#{t(text.to_s)}" }.to_json
end
def recreate_image
notalive = ((AlbumVariable.first.notalive.nil? ? true : AlbumVariable.first.notalive) rescue true)
if notalive
if !params['album_id'].to_s.empty?
variable = AlbumVariable.first
if variable.nil?
variable = AlbumVariable.new
end
variable.finish = false
variable.save!
choice_ids = params['album_id'].split(',')
albums = Album.all.select { |value| (choice_ids.include? value.id.to_s)}
if !(params['use_default']=='true')
color = params['color_choice'].to_s.empty? ? 'transparent' : params['color_choice']
albums.each do |album|
if album.album_colors.first.nil?
album.album_colors.create('color' => color)
else
album.album_colors.first.update_attributes('color' => color, 'updated_at' => Time.now)
end
end
end
count = 0
albums.each{|album| count+=album.album_images.count }
Thread.new do
i = 0
albums.each do |album|
album.album_images.each do |image|
error = nil
all_version = image.file.versions.map{|k,v| k}
begin
all_version.each do |version|
if !(version.to_s == 'resized' && crop_but_no_backup(image))
image.file.recreate_versions! version
image.save!
end
end
rescue => error
end
variable = AlbumVariable.first
variable.progress_percent = (i*100.0/count).floor.to_s+'%'
if !error.nil?
variable.progress_filename = error.inspect.to_s.encode('UTF-8', invalid: :replace, undef: :replace, replace: '?')
sleep(1)
else
variable.progress_filename = image[:file].to_s
end
variable.save!
i+=1
end
end
variable = AlbumVariable.first
variable.finish = true
variable.save!
end
else
variable = AlbumVariable.first
variable.finish = true
variable.progress_filename = ''
variable.progress_percent = '100%'
variable.save!
end
end
end
def finish_recreate
variable = AlbumVariable.first
variable.progress_percent = '0%'
variable.progress_filename = ''
variable.notalive = true
variable.save!
render :text => ''
end
def recreate_progress
progress_percent = AlbumVariable.first.progress_percent rescue '0%'
progress_filename = AlbumVariable.first.progress_filename rescue ''
finish = AlbumVariable.first.finish rescue false
render :json => {'percent' => progress_percent, 'filename' => progress_filename, 'finish' => finish }.to_json
end
def filter_album
@tags = @module_app.tags
categories = @module_app.categories.enabled
@filter_fields = filter_fields(categories, @tags)
@filter_fields.delete(:status)
albums1 = Album.where(:order => -1).desc(:created_at).with_categories(filters("category")).with_tags(filters("tag"))
albums1 = search_data(albums1,[:name])
albums2 = Album.where(:order.gt => -1).asc(:order).with_categories(filters("category")).with_tags(filters("tag"))
albums2 = search_data(albums2,[:name])
@fiter_albums = albums1.concat(albums2)
end
def index
album_length = Album.all.count
album_sort = Album.all.asc(:created_at)
if album_sort.first.album_colors.first.nil?
album_sort.each_with_index do |album|
if album.album_colors.first.nil?
album.album_colors.create('color' => 'transparent')
end
end
end
@url = request.original_fullpath
if params['page_no'].nil?
@show_script = true
@albums = filter_album.take 50
else
@show_script = false
start = (params['page_no'].to_i - 1)*50
@albums = filter_album[start...start+50]
end
if AlbumColor.count!=0
if AlbumColor.all.desc('updated_at').first[:color] == 'transparent'
@color_save = ''
else
@color_save = AlbumColor.desc('updated_at').first[:color]
end
else
@color_save = 'transparent'
end
if request.xhr?
render :partial => "album", :collection => @albums
end
end
def show
@album = Album.find(params[:id])
@images = @album.album_images.where(:order => -1).desc(:created_at)
images = @album.album_images.where(:order.gt => -1).asc(:order)
@images = @images.concat(images)
image_content = []
@images.each do |image|
image_content << {"id" => image.id.to_s, "description" => image.description_translations,"tags" => image.tags.collect{|t| t.id.to_s}}
end
@tags = @module_app.tags
@photos_data = {"galleries" => image_content}.to_json
end
def new
@album = Album.new
@tags = @module_app.tags
@categories = @module_app.categories
end
def create
album = Album.new(album_params)
album.save!
redirect_to admin_galleries_path
end
def destroy
album = Album.find(params[:id])
album.destroy
redirect_to admin_galleries_path
end
def edit
@album = Album.find(params[:id])
if can_edit_or_delete?(@album)
@tags = @module_app.tags
@categories = @module_app.categories
else
render_401
end
end
def import
@album = Album.find(params[:id])
end
def importimages
album = Album.find(params[:id])
workbook = RubyXL::Parser.parse(params["import_file"].tempfile)
sheet = workbook[0]
if sheet.count <= 203
sheet.each_with_index do |row, i|
next if i < 3
v = row.cells.first.value
next if v == "" || v.nil?
import_this_image(row, album)
end
redirect_to admin_gallery_path(album.id)
else
redirect_to import_admin_gallery_path(:error => "1")
end
end
def excel_format
@album = Album.find(params[:id])
respond_to do |format|
format.xlsx {
response.headers['Content-Disposition'] = 'attachment; filename="gallery_import_format.xlsx"'
}
end
end
def set_cover
if params[:set_cover] == "true"
album = Album.find(params[:album_id])
image = AlbumImage.find(params[:image_id])
album.update_attributes({:cover_path => image.file.thumb.url, :cover=>params[:image_id]})
else
album = Album.find(params[:album_id])
album.update_attributes({:cover_path => nil, :cover=>"default"})
end
render :json =>{"success"=>true}.to_json
end
def get_photoData_json
@album = Album.find(params[:id])
@images = @album.album_images.where(:order => -1).desc(:created_at)
images = @album.album_images.where(:order.gt => -1).asc(:order)
@images = @images.concat(images)
image_content = []
@images.each do |image|
image_content << {"id" => image.id.to_s, "description" => image.description_translations,"tags" => image.tags.collect{|t| t.id.to_s}}
end
render :json=>{"galleries" => image_content}.to_json
end
def get_album_json
albums = Album.all
output = Array.new
albums.each do |album|
tag_names = Array.new
album.tag_ids.each do |tag|
tag_names << get_tags.include?(tag)
end
if album.cover_path
cover_path = album.cover_path
else
cover_path = "/assets/gallery/default.jpg"
end
output << {
album_cover_file: "http://#{request.host_with_port}"+cover_path,
album_name: album.name,
album_tag_names: tag_names,
album_link:"http://#{request.host_with_port}#{panel_gallery_back_end_album_get_imgs_json_path(album)}",
}
end
render :json=>JSON.pretty_generate(output)
end
def get_imgs_json
album = Album.find(params[:album_id])
images = album.album_images.all
output = Array.new
images.each do |image|
tags = Array.new
image.tag_ids.each do |tag|
tags << get_tags.include?(tag)
end
if image.file.theater.present?
@image_file = image.file.theater.url
else
@image_file = image.file.url
end
output << {
image_title: image.title,
image_description: image.description,
image_file: { url: "http://#{request.host_with_port}#{@image_file}",
thumb: "http://#{request.host_with_port}#{image.file.thumb.to_s}"},
image_tag_names: tags}
end
render :json=>JSON.pretty_generate(output)
end
def imgs
@album = Album.find(params[:gallery_id])
@tag_names = Array.new
@images = @album.album_images.asc(:order)
@output = Array.new
@images.each do |values|
@output << { _id: values.id.to_s,
theater_link: admin_image_path(values),
description: values.description,
title: values.title,
file: values.file.as_json[:file],
gallery_album_id: values.album_id,
order: values.order,
tags: values.tags}
end
render :json=>{"images" => @output, "tags" => @album.tags}.to_json
end
def upload_process
if AlbumUnprocess.first.upload_success
album_unprocess = AlbumUnprocess.all
count = album_unprocess.count
Thread.new do
begin
album_unprocess.each_with_index do |un_image,i|
album = AlbumImage.all.select{|value| value.id.to_s == un_image.image_id.to_s}[0]
album.file = un_image.save_var
album.save!
file = un_image.save_var.tempfile
file.close
File.delete file.path
un_image.delete
variable = AlbumVariable.first
variable.progress_filename = album[:file]
variable.progress_percent = ((i+1)*100.0/count).floor.to_s + '%'
variable.save!
end
rescue => e
puts ['err',e.inspect]
end
variable = AlbumVariable.first
variable.finish = true
variable.save!
end
album_temp = AlbumUnprocess.first
album_temp.upload_success = false
album_temp.save!
end
end
def start_upload_process
album_temp = AlbumUnprocess.first
album_temp.upload_success = true
album_temp.save!
render :json => {}.to_json
end
def init_upload
variable = AlbumVariable.first
if variable.nil?
variable = AlbumVariable.new
end
variable.count = params['all_length'].to_i
variable.finish = false
variable.progress_percent = '0%'
variable.progress_filename = 'processing!!'
variable.notalive = true
variable.save!
render :json => {}.to_json
end
def upload_image
album = Album.find(params[:album_id])
files = params['files']
album_unprocess = AlbumUnprocess.new()
files.each do |file|
image = album.album_images.new
image.tags = (album.tags rescue [])
if AlbumVariable.first.count==1
image.file = file
else
album_unprocess.image_id = image.id
album_unprocess.upload_success = false
album_unprocess.save_var = file
album_unprocess.save!
ObjectSpace.undefine_finalizer(file.tempfile)
end
image.save!
end
render :json=>{"files"=>[{}]}.to_json
end
def last_image_id
album = Album.find(params[:albumid])
lastimage = album.album_images.last
render :json => {"last_image_id" => lastimage.id.to_s}.to_json
end
def new_images
@album = Album.find(params[:album_id])
if params[:last_image_id].present?
lastimage = AlbumImage.find(params[:last_image_id])
@newimages = @album.album_images.where(:created_at.gt => lastimage.created_at,:order => -1).desc(:created_at)
else
@newimages = @album.album_images.where(:order => -1).desc(:created_at)
end
render :layout=>false
end
def images_tags
album = Album.find(params[:album_id])
tags = Array.new
images = album.album_images.all
images.each do |image|
tags << {"id"=>image.id, "tags" => image.tags}
end
render :json=>tags.to_json
end
def update
album = Album.find(params[:id])
tagsToDestroy = []
tagsToAdd = []
new_tags = params[:album][:tags] || []
old_tags = album.tags.collect{|t| t.id.to_s}
old_tags.each do |tag|
if !new_tags.include?(tag)
tagsToDestroy << tag
end
end
if new_tags != nil
new_tags.each do |tag|
if !old_tags.include?(tag)
tagsToAdd << tag
end
end
end
update_children_image_tag(album,tagsToDestroy,tagsToAdd)
album.update_attributes(album_params)
redirect_to "/admin/galleries/"+album.id.to_s
end
def update_children_image_tag(album,tagsToDestroy,tagsToAdd)
# tagsToDestroy will contain all tag ids which have to be deleted from the galley_images
# tagsToAdd will contain all tag ids which ve to be added in tall album_images
images = album.album_images
images.each do |image|
image_tags = image.tags.collect{|t| t.id.to_s}
tagsToAdd.each do |tag|
image_tags << tag
end
tagsToDestroy.each do |tag|
if image_tags.include?tag
image_tags.delete(tag)
end
end
image.tags = image_tags
image.save
end
end
private
def crop_but_no_backup image
fname = image.file.path
extension = File.extname(fname)
base_name = fname.chomp(extension)
base_name += ('_resized'+ extension)
File.file?(base_name)
end
def setup_vars
@module_app = ModuleApp.where(:key=>"gallery").first
end
def album_params
p = params.require(:album).permit!
p["tags"] = p["tags"] || []
p
end
end