gallery/app/controllers/admin/galleries_controller.rb

238 lines
6.2 KiB
Ruby
Raw Normal View History

class Admin::GalleriesController < ApplicationController
before_filter :setup_vars
before_action :authenticate_user, :log_user_action, :except => "imgs"
layout "back_end"
2014-05-01 07:06:48 +00:00
def index
@albums = Album.all
@tags = @module_app.tags
end
def show
@album = Album.find(params[:id])
@images = @album.album_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])
@tags = @module_app.tags
@categories = @module_app.categories
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
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.all
@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,
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 new_images
if params[:last_image_id] != ""
@lastimage = AlbumImage.find(params[:last_image_id])
@album = Album.find(params[:album_id])
@newimages = @album.album_images.where(:created_at.gt => @lastimage.created_at)
else
@album = Album.find(params[:album_id])
@newimages = @album.album_images
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(tagsToDestroy,tagsToAdd)
@album.update_attributes(album_params)
redirect_to "/admin/galleries/"+@album.id.to_s
end
def update_children_image_tag(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 = AlbumImage.all
@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
2014-05-01 07:06:48 +00:00
end
end