]> git.openstreetmap.org Git - rails.git/blob - app/controllers/way_controller.rb
rails_port_0.5: Merge rails_port r4643
[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.nds = new_way.nds
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         if RelationMember.find(:first, :joins => "INNER JOIN current_relations ON current_relations.id=current_relation_members.id", :conditions => [ "visible = 1 AND member_type='way' and member_id=?", params[:id]])
81           render :nothing => true, :status => :precondition_failed
82         else
83           way.user_id = @user.id
84           way.tags = []
85           way.nds = []
86           way.visible = false
87           way.save_with_history!
88
89           render :nothing => true
90         end
91       else
92         render :nothing => true, :status => :gone
93       end
94     rescue ActiveRecord::RecordNotFound
95       render :nothing => true, :status => :not_found
96     rescue => ex
97       puts ex
98     end
99   end
100
101   def full
102     begin
103       way = Way.find(params[:id])
104
105       if way.visible
106         nd_ids = way.nds + [-1]
107         nodes = Node.find(:all, :conditions => "visible = 1 AND id IN (#{nd_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         doc.root << way.to_xml_node()
115
116         render :text => doc.to_s, :content_type => "text/xml"
117       else
118         render :nothing => true, :status => :gone
119       end
120     rescue ActiveRecord::RecordNotFound
121       render :nothing => true, :status => :not_found
122     end
123   end
124
125   def ways
126     begin
127       ids = params['ways'].split(',').collect { |w| w.to_i }
128     rescue
129       ids = []
130     end
131
132     if ids.length > 0
133       doc = OSM::API.new.get_xml_doc
134
135       Way.find(ids).each do |way|
136         doc.root << way.to_xml_node
137       end
138
139       render :text => doc.to_s, :content_type => "text/xml"
140     else
141       render :nothing => true, :status => :bad_request
142     end
143   end
144
145   def ways_for_node
146     wayids = WayNode.find(:all, :conditions => ['node_id = ?', params[:id]]).collect { |ws| ws.id }.uniq
147
148     if wayids.length > 0
149       doc = OSM::API.new.get_xml_doc
150
151       Way.find(wayids).each do |way|
152         doc.root << way.to_xml_node
153       end
154
155       render :text => doc.to_s, :content_type => "text/xml"
156     else
157       render :nothing => true, :status => :bad_request
158     end
159   end
160 end