photomix/app/controllers/photos_controller.rb

125 lines
3.6 KiB
Ruby
Raw Normal View History

2009-05-22 14:13:46 +02:00
class PhotosController < ApplicationController
2009-06-09 00:30:22 +02:00
before_filter :require_role_admin, :only => [:untouched, :upload, :new, :create, :edit, :update, :destroy]
2009-05-22 21:04:41 +02:00
def index
2009-06-02 00:08:57 +02:00
if params[:tag_id]
2009-06-16 14:03:32 +02:00
@photos = Tag.find_by_title( params[:tag_id] ).photos.find(:all, :order => "photos.id ASC")
2009-06-09 00:30:22 +02:00
elsif params[:album_id]
2009-06-16 01:43:53 +02:00
@photos = Album.find( params[:album_id]).photos.find(:all, :order => "photos.id ASC")
2009-06-02 00:08:57 +02:00
elsif params[:q]
2009-06-16 01:46:00 +02:00
@photos = 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 LIKE :q) ", { :q => '%' + params[:q] + '%' } ], :include => :album, :order => "photos.id ASC" )
2009-06-02 00:08:57 +02:00
else
2009-06-16 01:43:53 +02:00
@photos = Photo.find(:all, :order => "photos.id ASC")
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 )
@previous = previous_rs.first if !previous_rs.empty?
next_rs = Photo.next( @photo.id, @photo.album )
@next = next_rs.first if !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
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
2009-06-02 00:08:57 +02:00
respond_to do |format|
@photo = Photo.new(params[:photo])
if params[:Filedata]
@photo.swf_uploaded_data = params[:Filedata]
2009-06-03 01:33:39 +02:00
if @photo.save
format.html { render :text => "FILEID:" + @photo.path_modified_public("album") }
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-06-02 00:08:57 +02:00
else
if @photo.save
flash[:notice] = 'Created'
format.html { redirect_to(@photo) }
format.xml { render :xml => @photo }
else
format.html { render :action => "new" }
format.xml { render :xml => @photo.errors }
end
end
2009-05-22 21:04:41 +02:00
end
end
def edit
@photo = Photo.find( params[:id])
2009-06-16 21:43:03 +02:00
@tags = Tag.find(: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]
@photos = Album.find( params[:album_id] ).photos
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!"
2009-05-22 21:04:41 +02:00
redirect_to @photo
else
render :action => :edit
end
end
2009-06-09 00:30:22 +02:00
def update_multiple
@photos = params[:photos][:photo]
@photos.each do |photo_id|
photo = Photo.find( photo_id )
photo.update_attributes!(params[:photos][:photo][photo_id].reject { |k,v| v.blank? })
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
redirect_to @album
2009-05-22 21:04:41 +02:00
else
redirect_to @photo
end
end
2009-05-22 14:13:46 +02:00
end