1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
  def new
    @photo = Photo.new(params[:photo])
  end

  # Create a new Photo. e.g. [POST] /gallery/photos
  #
  # +photo+:: A hash of attributes to set for the new photo object,
  #           e.g. ?photo[title]=Father&photo[body]=Dad&photo[parent_id]=2
  # Returns HTTP status "201 Created" if successful.
  # When params[:diecut_for] is set, redirects to diecut page
  def create
    @photo = Photo.new(params[:photo])
    
    if @photo.send(:filename_state).class == FileColumn::NoUploadedFile
      flash[:warning] = "Nothing uploaded"
      redirect_to :action => "new" and return
    end
    
    @photo.save!
    
    # Add to book, if context exists
    @photo.books << @book if @book
    
    respond_to do |format|
      # format.js   # renders create.rjs
      format.html { redirect_to edit_photo_url(:id => @photo.content_id) }
      format.xml  do
        headers["Location"] = photo_url(:id => @photo.content_id)
        render(:nothing => true, :status => "201 Created")
      end
    end
  end
  
  # Reveal photo data such as caption
  # +id+:: The id of the Photo object to show. e.g. [GET] /gallery/photos/6
  # Returns the photo in HTML or XML.
  def show
    @version = params["version"] || "small"
    respond_to do |format|
      # format.js   # renders show.rjs
      format.html # renders show.rhtml
      format.xml { render :xml => @photo.to_xml }
    end
  end
  
  def edit
    # If we're in a book context, try to add the book's cone(s) to this photo as a default
    if @photo.content_pointer.cones.count == 0
      @photo.content_pointer.copy_cones(@book.content_pointer.cones)
      @photo.content_pointer.save!
    end
    
    respond_to do |format|
      # format.js   # renders edit.rjs
      format.html # renders edit.rhtml
      format.xml { render :xml => @photo.to_xml }
    end
  end
  
  # Change photo attributes
  # +id+:: The id of the Photo object to show. e.g. [PUT] /gallery/photos/6
  # +photo+:: A hash of attributes to update, e.g. ?photo[caption]=Father...
  # Returns the updated Photo in XML or redirects to a page that shows the photo in HTML.
  def update
    @photo.attributes = params[:photo]
    @photo.cone_data = params[:cones]
    @photo.save!

    respond_to do |format|
      # format.js   # renders update.rjs
      format.html do
        flash[:notice] = "Photo updated"
        redirect_to photos_url
      end
      format.xml { render :xml => @photo.to_xml }
    end
  end
  
  # Send a Photo to the trash bin
  # +id+:: The id of the Photo object to trash. e.g. [DELETE] /gallery/photos/6
  def destroy
    if params[:warn]
      @books = @photo.occurrences.map { |o| o.book }.compact
      render :action => "destroy_with_warning"
      return
    end
    @photo.trash
    
    respond_to do |format|
      format.html do
        flash[:notice] = "Photo removed"
        redirect_to photos_url
      end
      format.xml { render :nothing => true }
    end
  end