gallery/app/controllers/admin/galleries_controller.rb

290 lines
8.2 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
def index
@tags = @module_app.tags
categories = @module_app.categories.enabled
@filter_fields = filter_fields(categories, @tags)
@filter_fields.delete(:status)
@albums = Album.where(:order => -1).desc(:created_at).with_categories(filters("category")).with_tags(filters("tag"))
@albums = search_data(@albums,[:name])
albums = Album.where(:order.gt => -1).asc(:order).with_categories(filters("category")).with_tags(filters("tag"))
albums = search_data(albums,[:name])
@albums = @albums.concat(albums)
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_image
@album = Album.find(params[:album_id])
@files = params['files']
a = Array.new
@files.each do |file|
@image = @album.album_images.new
@image.file = file
@image.tags = @album.tags rescue []
@image.save!
a << {"thumbnail_url"=>@image.file.thumb.url,"url"=>admin_image_path(@image)}
end
render :json=>{"files"=>a}.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 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