]> git.openstreetmap.org Git - rails.git/blob - app/controllers/node_controller.rb
Add recent nodes to start of tab, and return false from onclick actions
[rails.git] / app / controllers / node_controller.rb
1 # The NodeController is the RESTful interface to Node objects
2
3 class NodeController < ApplicationController
4   require 'xml/libxml'
5
6   session :off
7   before_filter :authorize, :only => [:create, :update, :delete]
8   before_filter :check_write_availability, :only => [:create, :update, :delete]
9   before_filter :check_read_availability, :except => [:create, :update, :delete]
10   after_filter :compress_output
11
12   # Create a node from XML.
13   def create
14     if request.put?
15       node = Node.from_xml(request.raw_post, true)
16
17       if node
18         node.user_id = @user.id
19         node.visible = true
20         node.save_with_history!
21
22         render :text => node.id.to_s, :content_type => "text/plain"
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   # Dump the details on a node given in params[:id]
32   def read
33     begin
34       node = Node.find(params[:id])
35       if node.visible
36         response.headers['Last-Modified'] = node.timestamp.rfc822
37         render :text => node.to_xml.to_s, :content_type => "text/xml"
38        else
39         render :text => "", :status => :gone
40       end
41     rescue ActiveRecord::RecordNotFound
42       render :nothing => true, :status => :not_found
43     end
44   end
45
46   # Update a node from given XML
47   def update
48     begin
49       node = Node.find(params[:id])
50       new_node = Node.from_xml(request.raw_post)
51
52       if new_node and new_node.id == node.id
53         node.user_id = @user.id
54         node.latitude = new_node.latitude 
55         node.longitude = new_node.longitude
56         node.tags = new_node.tags
57         node.visible = true
58         node.save_with_history!
59
60         render :nothing => true
61       else
62         render :nothing => true, :status => :bad_request
63       end
64     rescue ActiveRecord::RecordNotFound
65       render :nothing => true, :status => :not_found
66     end
67   end
68
69   # Delete a node. Doesn't actually delete it, but retains its history in a wiki-like way.
70   # FIXME remove all the fricking SQL
71   def delete
72     begin
73       node = Node.find(params[:id])
74
75       if node.visible
76         if WayNode.find(:first, :joins => "INNER JOIN current_ways ON current_ways.id = current_way_nodes.id", :conditions => [ "current_ways.visible = 1 AND current_way_nodes.node_id = ?", node.id ])
77           render :text => "", :status => :precondition_failed
78         elsif RelationMember.find(:first, :joins => "INNER JOIN current_relations ON current_relations.id=current_relation_members.id", :conditions => [ "visible = 1 AND member_type='node' and member_id=?", params[:id]])
79           render :text => "", :status => :precondition_failed
80         else
81           node.user_id = @user.id
82           node.visible = 0
83           node.save_with_history!
84
85           render :nothing => true
86         end
87       else
88         render :text => "", :status => :gone
89       end
90     rescue ActiveRecord::RecordNotFound
91       render :nothing => true, :status => :not_found
92     end
93   end
94
95   # WTF does this do?
96   def nodes
97     ids = params['nodes'].split(',').collect { |n| n.to_i }
98
99     if ids.length > 0
100       doc = OSM::API.new.get_xml_doc
101
102       Node.find(ids).each do |node|
103         doc.root << node.to_xml_node
104       end 
105
106       render :text => doc.to_s, :content_type => "text/xml"
107     else
108       render :nothing => true, :status => :bad_request
109     end
110   end
111 end