| Class | Admin::PhotosController |
| In: |
app/controllers/admin/photos_controller.rb
|
| Parent: | Sadmin::BaseController |
Controlador para administrar las fotos de la fototeca
Añadir una foto a un álbum
# File app/controllers/admin/photos_controller.rb, line 101
101: def add_to_album
102: @photo = Photo.find(params[:id])
103: respond_to do |format|
104: format.js do
105: render :update do |page|
106: if params[:album_id].present?
107: @photo.albums << Album.find(params[:album_id])
108: page.replace "albums_for_#{@photo.id}", :partial => "albums_select", :locals => {:photo => @photo}
109: end
110: page.visual_effect :highlight, "albums_for_#{@photo.id}"
111: end
112: end
113: end
114: end
Lista de tags para el campo de auto-complete
# File app/controllers/admin/photos_controller.rb, line 161
161: def auto_complete_for_photo_tag_list_es
162: auto_complete_for_tag_list(params[:photo][:tag_list_es])
163: if @tags.length > 0
164: render :inline => "<%= content_tag(:ul, @tags.map {|t| content_tag(:li, t.nombre)}) %>"
165: else
166: render :nothing => true
167: end
168: end
Modificar los atributos de varias fotos
# File app/controllers/admin/photos_controller.rb, line 74
74: def batch_edit
75: @photos = Photo.find(params[:ids])
76: end
Actualizar los atributos de varias fotos
# File app/controllers/admin/photos_controller.rb, line 79
79: def batch_update
80: @photos = Photo.find(params[:photos].keys.collect(&:to_i))
81:
82: if params[:cancel].blank?
83: begin
84: Photo.transaction do
85: @photos.each do |photo|
86: photo.update_attributes!(params[:photos][photo.id.to_s])
87: end
88: flash[:notice] = "Las fotos se han actualizado correctamente"
89: redirect_to admin_albums_path
90: end
91: rescue
92: flash[:error] = "Las fotos no se han actualizado"
93: render :action => "batch_edit"
94: end
95: else
96: redirect_to admin_albums_path
97: end
98: end
Importar fotos nuevas, tanto en un álbum nuevo como en uno existente
# File app/controllers/admin/photos_controller.rb, line 35
35: def create
36: @photo = Photo.new(params[:photo].merge(:file_path => params[:dir_path], :dir_path => params[:dir_path]))
37:
38: if @photo.valid? && params[:dir_path].present?
39: if params[:album_id].to_i == 0
40: @album = Album.create(:title_es => params[:photo][:title_es], :title_eu => params[:photo][:title_eu], :title_en => params[:photo][:title_en])
41: else
42: @album = Album.find(params[:album_id])
43: end
44:
45: import_photos
46: else
47: @photo.errors.add_to_base("No ha indicado ningún directorio") if params[:dir_path].blank?
48: render :action => "new" and return
49: end
50: end
Eliminar una foto
# File app/controllers/admin/photos_controller.rb, line 140
140: def destroy
141: @album = Album.find(params[:album_id]) if params[:album_id]
142: @photo = Photo.find(params[:id])
143: if @photo.destroy
144: flash[:notice] = "La foto ha sido eliminada"
145: if @album
146: redirect_to admin_album_path(@album)
147: else
148: redirect_to orphane_admin_photos_path
149: end
150: else
151: flash[:error] = "La foto no ha podido ser eliminada"
152: if @album
153: redirect_to admin_album_photo_path(@album, @photo)
154: else
155: redirect_to orphane_admin_photos_path
156: end
157: end
158: end
Modificar una foto
# File app/controllers/admin/photos_controller.rb, line 53
53: def edit
54: @album = Album.find(params[:album_id]) if params[:album_id]
55: @photo = Photo.find(params[:id])
56: end
Busca fotos en el directorio especificado
# File app/controllers/admin/photos_controller.rb, line 117
117: def find_photos
118: p = Photo.new(:dir_path => params[:dir_path])
119: if !p.valid? && p.errors.on("dir_path")
120: render :update do |page|
121: page.replace_html :find_photos, :text => "<span style='color:red'>Directorio incorrecto.<br/> #{p.errors.on('dir_path')}</span>"
122: end
123: else
124: dir = Photo::PHOTOS_PATH
125:
126: logger.info "Buscando fotos #{dir + params[:dir_path] + "*.jpg"}"
127: if (found_files = Dir.glob(dir + params[:dir_path] + "*.jpg") + Dir.glob(dir + params[:dir_path] + "solo_irekia/*.jpg")) && found_files.length>0
128: render :update do |page|
129: page.replace_html :find_photos, :text => "<span style='color:green'>#{found_files.length} fotos encontradas</span>"
130: end
131: else
132: render :update do |page|
133: page.replace_html :find_photos, :text => "<span style='color:red'>fotos NO encontradas</span>"
134: end
135: end
136: end
137: end
Listado de fotos que no pertenecen a ningún álbum
# File app/controllers/admin/photos_controller.rb, line 15
15: def orphane
16: @photos = Photo.paginate :page => params[:page], :order => "created_at DESC",
17: :conditions => "NOT EXISTS (SELECT 1 FROM album_photos WHERE album_photos.photo_id=photos.id)"
18: end
Vista de una foto
# File app/controllers/admin/photos_controller.rb, line 21
21: def show
22: if params[:album_id]
23: @album = Album.find(params[:album_id])
24: @photo = @album.photos.find(params[:id])
25:
26: all_photos = @album.photos.ordered_by_title
27: @next_photo = all_photos[all_photos.index(@photo) + 1]
28: @prev_photo = all_photos[all_photos.index(@photo) - 1] unless all_photos.index(@photo) == 0
29: else
30: @photo = Photo.find(params[:id])
31: end
32: end
Actualizar los atributos de una foto
# File app/controllers/admin/photos_controller.rb, line 59
59: def update
60: @album = Album.find(params[:album_id]) if params[:album_id]
61: @photo = Photo.find(params[:id])
62: if @photo.update_attributes(params[:photo])
63: if @album && @album.photos.exists?(@photo.id)
64: redirect_to admin_photo_path(@photo, :album_id => @album.id)
65: else
66: redirect_to admin_photo_path(@photo)
67: end
68: else
69: render :action => "new"
70: end
71: end