2009-05-22 14:13:46 +02:00
class PhotosController < ApplicationController
2010-04-13 21:10:40 +02:00
#skip_before_filter :verify_authenticity_token
2009-08-03 16:39:33 +02:00
before_filter :check_public_access
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-28 18:02:19 +02:00
if params [ :tag_id ] && params [ :album_id ]
2010-08-07 13:30:53 +02:00
@photos = Tag . find_by_title ( params [ :tag_id ] ) . photos . find ( :all , :conditions = > [ 'photos.album_id = :album' , { :album = > Album . find ( params [ :album_id ] ) } ] , :order = > " photos.id ASC " )
2009-06-28 18:02:19 +02:00
elsif params [ :tag_id ]
2010-08-07 13:30:53 +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-07-07 22:14:58 +02:00
#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" )
2009-08-13 13:25:43 +02:00
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
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 )
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
2010-04-13 21:10:40 +02:00
@photo = Photo . new ( params [ :photo ] )
2009-06-02 00:08:57 +02:00
respond_to do | format |
2009-06-03 01:33:39 +02:00
if @photo . save
2010-04-13 21:10:40 +02:00
format . html { render :text = > " FILEID: " + @photo . file . 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 ] )
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-28 18:02:19 +02:00
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! "
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
2009-06-10 01:22:48 +02:00
@photos = params [ :photos ] [ :photo ]
2009-06-18 15:38:37 +02:00
@photos . each do | photo_item |
photo = Photo . find ( photo_item [ 0 ] )
2009-06-28 18:02:19 +02:00
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
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