photomix/app/controllers/photos_controller.rb

158 lines
4.8 KiB
Ruby
Raw Normal View History

2009-05-22 14:13:46 +02:00
class PhotosController < ApplicationController
2009-08-03 16:39:33 +02:00
before_filter :check_public_access
2012-07-24 23:26:16 +02:00
skip_before_filter :authenticate_user!, :only => [:index, :show]
2009-05-22 21:04:41 +02:00
def index
if params[:tag_id] && params[:album_id]
@tag = Tag.find_by_title!( params[:tag_id] )
2012-07-24 23:26:16 +02:00
@photos = @tag.photos.where(:conditions => ['photos.album_id = :album', {:album => Album.find(params[:album_id] ) } ]).order("photos.id ASC")
elsif params[:tag_id]
@tag = Tag.find_by_title!( params[:tag_id] )
2012-07-24 23:26:16 +02:00
@photos = @tag.photos.order("photos.id ASC")
2009-06-09 00:30:22 +02:00
elsif params[:album_id]
@album = Album.find( params[:album_id])
2012-07-24 23:26:16 +02:00
@photos = @album.photos.order("photos.id ASC")
2009-06-02 00:08:57 +02:00
elsif params[:q]
#search = params[:q]
#search = search.split("AND").map{|q|q.strip}
#@photos = Photo.find(:all, :select => 'DISTINCT photos.id, photos.album_id, photos.title, photos.path', :limit => 20, :conditions => { :tags => {:title => search}}, :joins => 'LEFT OUTER JOIN photo_tags ON photos.id = photo_tags.photo_id LEFT OUTER JOIN tags ON photo_tags.tag_id = tags.id', :include => [:album], :order => "photos.title ASC" )
params[:q].split(" AND ").each {|q|
qphotos = Photo.find(:all, :limit => 20, :conditions => [ "photos.description LIKE :q OR photos.title LIKE :q OR photos.id IN ( SELECT photo_id FROM photo_tags LEFT OUTER JOIN tags ON photo_tags.tag_id = tags.id WHERE tags.title = :t) ", { :q => '%' + q + '%', :t => q } ], :include => :album, :order => "photos.id ASC" )
if @photos
@photos = @photos & qphotos
else
@photos = qphotos
end
}
2009-06-02 00:08:57 +02:00
else
2012-07-29 00:33:03 +02:00
@photos = Photo.popular.page(@page).per(@per_page)
2009-06-02 00:08:57 +02:00
end
respond_to do |format|
format.html
format.json { render :json => @photos }
format.xml { render :xml => @photos }
end
end
def untouched
2009-06-09 00:30:22 +02:00
if params[:album_id]
2009-06-16 01:22:51 +02:00
@album = Album.find( params[:album_id])
2009-06-09 00:30:22 +02:00
@photos = @album.photos.untouched
else
@photos = Photo.untouched()
end
2009-05-22 21:04:41 +02:00
respond_to do |format|
format.html
format.json { render :json => @photos }
format.xml { render :xml => @photos }
end
end
def show
2009-06-11 13:05:09 +02:00
@photo = Photo.find( params[:id] )
previous_rs = Photo.previous( @photo.id, @photo.album )
2009-07-06 23:38:20 +02:00
@previous = previous_rs.first unless previous_rs.empty?
2009-06-11 13:05:09 +02:00
next_rs = Photo.next( @photo.id, @photo.album )
2009-07-06 23:38:20 +02:00
@next = next_rs.first unless next_rs.empty?
2009-05-22 21:04:41 +02:00
respond_to do |format|
format.html
format.json { render :json => @photo }
format.xml { render :xml => @photo }
end
end
2009-07-29 14:55:43 +02:00
def scan
require "scan"
2009-08-11 11:34:40 +02:00
ScanFiles.Scan(false)
2009-07-29 14:55:43 +02:00
redirect_to(root_path)
end
2009-05-22 21:04:41 +02:00
def new
@photo = Photo.new
end
2009-06-09 00:30:22 +02:00
def upload
2009-06-16 01:22:51 +02:00
@album = Album.find( params[:album_id])
2009-06-09 00:30:22 +02:00
end
2009-05-22 21:04:41 +02:00
def create
@photo = Photo.new(params[:photo])
2012-07-24 23:26:16 +02:00
@photo.attachment = params[:file]
2009-06-02 00:08:57 +02:00
respond_to do |format|
2009-06-03 01:33:39 +02:00
if @photo.save
2012-07-24 23:26:16 +02:00
format.html { render :text => "FILEID:" + @photo.attachment.album.url }
2009-06-03 01:33:39 +02:00
format.xml { render :nothing => true }
else
format.html { render :text => "ERRORS:" + @photo.errors.full_messages.join(" "), :status => 500 }
format.xml { render :xml => @photo.errors, :status => 500 }
end
2009-05-22 21:04:41 +02:00
end
end
def edit
@photo = Photo.find( params[:id])
2012-07-24 23:26:16 +02:00
@tags = Tag.all.map { |tag| tag.title }.join('\',\'')
2009-05-22 21:04:41 +02:00
end
2009-06-09 00:30:22 +02:00
def edit_multiple
if params[:album_id]
2009-06-18 15:38:37 +02:00
@album = Album.find( params[:album_id] )
@photos = @album.photos
2009-06-09 00:30:22 +02:00
else
@photos = Photo.find( params[:photo_ids] )
end
end
2009-05-22 21:04:41 +02:00
def update
@photo = Photo.find( params[:id])
if @photo.update_attributes(params[:photo])
2009-06-09 00:30:22 +02:00
flash[:notice] = "Photo updated!"
if params[:collection_id]
redirect_to collection_album_photo_path( params[:collection_id], params[:album_id], @photo )
elsif params[:album_id]
redirect_to album_photo_path( params[:album_id], @photo )
else
redirect_to @photo
end
2009-05-22 21:04:41 +02:00
else
render :action => :edit
end
end
2009-06-09 00:30:22 +02:00
def update_multiple
@photos = params[:photos][:photo]
2009-06-18 15:38:37 +02:00
@photos.each do |photo_item|
photo = Photo.find( photo_item[0] )
if photo_item[1][:_delete] == "1"
photo.destroy
else
photo.title = photo_item[1][:title]
photo.tag_list = photo_item[1][:tags]
photo.save
end
2009-06-09 00:30:22 +02:00
end
flash[:notice] = "Updated photos!"
redirect_to photos_path
end
2009-05-22 21:04:41 +02:00
2009-06-02 00:08:57 +02:00
def destroy
2009-05-22 21:04:41 +02:00
@photo = Photo.find( params[:id])
2009-06-02 00:08:57 +02:00
@album = @photo.album
if @photo.destroy
if params[:collection_id]
redirect_to collection_album_path( params[:collection_id], @album )
else
redirect_to @album
end
2009-05-22 21:04:41 +02:00
else
redirect_to @photo
end
end
2012-07-29 00:33:03 +02:00
def rate
@photo = Photo.find(params[:id])
@photo.rate(params[:stars], current_user, params[:dimension])
render :json => {:id => @photo.wrapper_dom_id(params), :width => 125}
end
2009-05-22 14:13:46 +02:00
end