]> git.openstreetmap.org Git - rails.git/blob - app/controllers/way_controller.rb
Fix typo in HTML version of account confirmation message.
[rails.git] / app / controllers / way_controller.rb
1 class WayController < ApplicationController
2   require 'xml/libxml'
3
4   session :off
5   before_filter :authorize, :only => [:create, :update, :delete]
6   before_filter :check_write_availability, :only => [:create, :update, :delete]
7   before_filter :check_read_availability, :except => [:create, :update, :delete]
8   after_filter :compress_output
9
10   def create
11     if request.put?
12       way = Way.from_xml(request.raw_post, true)
13
14       if way
15         if !way.preconditions_ok?
16           render :nothing => true, :status => :precondition_failed
17         else
18           way.user_id = @user.id
19           way.save_with_history!
20
21           render :text => way.id.to_s, :content_type => "text/plain"
22         end
23       else
24         render :nothing => true, :status => :bad_request
25       end
26     else
27       render :nothing => true, :status => :method_not_allowed
28     end
29   end
30
31   def read
32     begin
33       way = Way.find(params[:id])
34
35       if way.visible
36         render :text => way.to_xml.to_s, :content_type => "text/xml"
37       else
38         render :nothing => true, :status => :gone
39       end
40     rescue ActiveRecord::RecordNotFound
41       render :nothing => true, :status => :not_found
42     end
43   end
44
45   def update
46     begin
47       way = Way.find(params[:id])
48
49       if way.visible
50         new_way = Way.from_xml(request.raw_post)
51
52         if new_way and new_way.id == way.id
53           if !new_way.preconditions_ok?
54             render :nothing => true, :status => :precondition_failed
55           else
56             way.user_id = @user.id
57             way.tags = new_way.tags
58             way.segs = new_way.segs
59             way.visible = true
60             way.save_with_history!
61
62             render :nothing => true
63           end
64         else
65           render :nothing => true, :status => :bad_request
66         end
67       else
68         render :nothing => true, :status => :gone
69       end
70     rescue ActiveRecord::RecordNotFound
71       render :nothing => true, :status => :not_found
72     end
73   end
74
75   def delete
76     begin
77       way = Way.find(params[:id])
78
79       if way.visible
80         way.user_id = @user.id
81         way.tags = []
82         way.segs = []
83         way.visible = false
84         way.save_with_history!
85
86         render :nothing => true
87       else
88         render :nothing => true, :status => :gone
89       end
90     rescue ActiveRecord::RecordNotFound
91       render :nothing => true, :status => :not_found
92     end
93   end
94
95   def full
96     begin
97       way = Way.find(params[:id])
98
99       if way.visible
100         # In future, we might want to do all the data fetch in one step
101         seg_ids = way.segs + [-1]
102         segments = Segment.find_by_sql "select * from current_segments where visible = 1 and id IN (#{seg_ids.join(',')})"
103
104         node_ids = segments.collect {|segment| segment.node_a }
105         node_ids += segments.collect {|segment| segment.node_b }
106         node_ids += [-1]
107         nodes = Node.find(:all, :conditions => "visible = 1 AND id IN (#{node_ids.join(',')})")
108
109         # Render
110         doc = OSM::API.new.get_xml_doc
111         nodes.each do |node|
112           doc.root << node.to_xml_node()
113         end
114         segments.each do |segment|
115           doc.root << segment.to_xml_node()
116         end
117         doc.root << way.to_xml_node()
118
119         render :text => doc.to_s, :content_type => "text/xml"
120       else
121         render :nothing => true, :status => :gone
122       end
123     rescue ActiveRecord::RecordNotFound
124       render :nothing => true, :status => :not_found
125     end
126   end
127
128   def ways
129     begin
130       ids = params['ways'].split(',').collect { |w| w.to_i }
131     rescue
132       ids = []
133     end
134
135     if ids.length > 0
136       doc = OSM::API.new.get_xml_doc
137
138       Way.find(ids).each do |way|
139         doc.root << way.to_xml_node
140       end
141
142       render :text => doc.to_s, :content_type => "text/xml"
143     else
144       render :nothing => true, :status => :bad_request
145     end
146   end
147
148   def ways_for_segment
149     wayids = WaySegment.find(:all, :conditions => ['segment_id = ?', params[:id]]).collect { |ws| ws.id }.uniq
150
151     if wayids.length > 0
152       doc = OSM::API.new.get_xml_doc
153
154       Way.find(wayids).each do |way|
155         doc.root << way.to_xml_node
156       end
157
158       render :text => doc.to_s, :content_type => "text/xml"
159     else
160       render :nothing => true, :status => :bad_request
161     end
162   end
163 end