Newer
Older
# licensed under the Affero General Public License version 3 or later. See
Daniel Vincent Grippi
a validé
before_filter :authenticate_user!
helper_method :object_aspect_ids, :parent, :photo, :additional_photos, :next_photo, :previous_photo, :ownership
maxwell
a validé
@post_type = :photos
@person = Person.find_by_id(params[:person_id])
maxwell
a validé
if @person
@profile = @person.profile
@contact = current_user.contact_for(@person)
@is_contact = @person != current_user.person && @contact
Raphael
a validé
@aspects_with_person = []
maxwell
a validé
if @contact
@aspects_with_person = @contact.aspects
@contacts_of_contact = @contact.contacts
@contacts_of_contact_count = @contact.contacts.count
else
@contact = Contact.new
@contacts_of_contact = []
@contacts_of_contact_count = 0
maxwell
a validé
end
@posts = current_user.posts_from(@person).where(:type => 'Photo').paginate(:page => params[:page])
maxwell
a validé
render 'people/show'
maxwell
a validé
flash[:error] = I18n.t 'people.show.does_not_exist'
redirect_to people_path
Michael Sofaer
a validé
raise unless params[:photo][:aspect_ids]
if params[:photo][:aspect_ids] == "all"
params[:photo][:aspect_ids] = current_user.aspects.collect{|x| x.id}
danielvincent
a validé
elsif params[:photo][:aspect_ids].is_a?(Hash)
params[:photo][:aspect_ids] = params[:photo][:aspect_ids].values
params[:photo][:user_file] = file_handler(params)
@photo = current_user.build_post(:photo, params[:photo])
if @photo.save
aspects = current_user.aspects_from_ids(params[:photo][:aspect_ids])
unless @photo.pending
current_user.add_to_streams(@photo, aspects)
current_user.dispatch_post(@photo, :to => params[:photo][:aspect_ids])
end
if params[:photo][:set_profile_photo]
profile_params = {:image_url => @photo.url(:thumb_large),
:image_url_medium => @photo.url(:thumb_medium),
:image_url_small => @photo.url(:thumb_small)}
current_user.update_profile(profile_params)
end
format.json{ render(:layout => false , :json => {"success" => true, "data" => @photo}.to_json )}
respond_with @photo, :location => photos_path, :error => message
Daniel Vincent Grippi
a validé
end
message = I18n.t 'photos.create.type_error'
respond_with @photo, :location => photos_path, :error => message
rescue CarrierWave::IntegrityError
message = I18n.t 'photos.create.integrity_error'
respond_with @photo, :location => photos_path, :error => message
rescue RuntimeError => e
message = I18n.t 'photos.create.runtime_error'
respond_with @photo, :location => photos_path, :error => message
raise e
danielvincent
a validé
def make_profile_photo
author_id = current_user.person.id
@photo = Photo.where(:id => params[:photo_id], :author_id => author_id).first
danielvincent
a validé
if @photo
profile_hash = {:image_url => @photo.url(:thumb_large),
:image_url_medium => @photo.url(:thumb_medium),
:image_url_small => @photo.url(:thumb_small)}
if current_user.update_profile(profile_hash)
respond_to do |format|
format.js{ render :json => { :photo_id => @photo.id,
:image_url => @photo.url(:thumb_large),
:image_url_medium => @photo.url(:thumb_medium),
:image_url_small => @photo.url(:thumb_small),
danielvincent
a validé
:status => 201}
end
else
MrZYX
a validé
render :nothing => true, :status => 422
danielvincent
a validé
end
else
MrZYX
a validé
render :nothing => true, :status => 422
danielvincent
a validé
end
end
photo = current_user.posts.where(:id => params[:id]).first
Raphael Sofaer
a validé
current_user.retract(photo)
respond_to do |format|
format.json{ render :nothing => true, :status => 204 }
format.html do
flash[:notice] = I18n.t 'photos.destroy.notice'
if photo.status_message_guid
respond_with photo, :location => photo.status_message
else
respond_with photo, :location => person_photos_path(current_user.person)
end
end
respond_with photo, :location => person_photos_path(current_user.person)
maxwell
a validé
else
begin
redirect_to :back
rescue
redirect_to aspects_path
end
def edit
if @photo = current_user.posts.where(:id => params[:id]).first
maxwell
a validé
redirect_to person_photos_path(current_user.person)
end
def update
photo = current_user.posts.where(:id => params[:id]).first
if photo
if current_user.update_post( photo, params[:photo] )
danielvincent
a validé
flash.now[:notice] = I18n.t 'photos.update.notice'
respond_to do |format|
format.js{ render :json => photo, :status => 200 }
end
danielvincent
a validé
flash.now[:error] = I18n.t 'photos.update.error'
respond_to do |format|
format.html{ redirect_to [:edit, photo] }
format.js{ render :status => 403 }
end
maxwell
a validé
redirect_to person_photos_path(current_user.person)
# helpers
# used on the show page to show which aspects are selected
def object_aspect_ids
if params[:action] == 'show' && parent_aspects = parent.aspects.where(:user_id => current_user.id).all
@object_aspect_ids ||= parent_aspects.map{|a| a.id}
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
else
super
end
end
def ownership
@ownership ||= current_user.owns? photo
end
def parent
@parent ||= StatusMessage.where(:guid => photo.status_message_guid).includes(:photos).first if photo.status_message_guid
@parent ||= photo
end
def photo
@photo ||= current_user.find_visible_post_by_id(params[:id], :type => 'Photo')
@photo
end
def additional_photos
if photo.status_message_guid?
@additional_photos ||= photo.status_message.photos
end
end
def next_photo
@next_photo ||= additional_photos[additional_photos.index(photo)+1]
@next_photo ||= additional_photos.first
end
def previous_photo
@previous_photo ||= additional_photos[additional_photos.index(photo)-1]
end
Michael Sofaer
a validé
private
def file_handler(params)
######################## dealing with local files #############
# get file name
file_name = params[:qqfile]
# get file content type
att_content_type = (request.content_type.to_s == "") ? "application/octet-stream" : request.content_type.to_s
# create tempora##l file
begin
file = Tempfile.new(file_name, {:encoding => 'BINARY'})
file.print request.raw_post.force_encoding('BINARY')
rescue RuntimeError => e
raise e unless e.message.include?('cannot generate tempfile')
file = Tempfile.new(file_name) # Ruby 1.8 compatibility
file.print request.raw_post
end
# put data into this file from raw post request
# create several required methods for this temporal file
Tempfile.send(:define_method, "content_type") {return att_content_type}
Tempfile.send(:define_method, "original_filename") {return file_name}
file
end