]> git.openstreetmap.org Git - rails.git/blobdiff - app/controllers/amf_controller.rb
changeset comments and a couple of other small fixes
[rails.git] / app / controllers / amf_controller.rb
index 4034ce10ddb3769e69e1abcdcc290a5777a707e1..6e1b364d4794dbfef7ebeca2166e354078f1c604 100644 (file)
 # from the AMF message), each method generally takes arguments in the order 
 # they were sent by the Potlatch SWF. Do not assume typing has been preserved. 
 # Methods all return an array to the SWF.
+#
+# == API 0.6
+#
+# Note that this requires a patched version of composite_primary_keys 1.1.0
+# (see http://groups.google.com/group/compositekeys/t/a00e7562b677e193) 
+# if you are to run with POTLATCH_USE_SQL=false .
 # 
 # == Debugging
 # 
@@ -72,6 +78,7 @@ class AmfController < ApplicationController
                when 'getway_old';                      results[index]=AMF.putdata(index,getway_old(args[0].to_i,args[1].to_i))
                when 'getway_history';          results[index]=AMF.putdata(index,getway_history(args[0].to_i))
                when 'getnode_history';         results[index]=AMF.putdata(index,getnode_history(args[0].to_i))
+               when 'findgpx';                         results[index]=AMF.putdata(index,findgpx(*args))
                when 'findrelations';           results[index]=AMF.putdata(index,findrelations(*args))
                when 'getpoi';                          results[index]=AMF.putdata(index,getpoi(*args))
          end
@@ -128,6 +135,14 @@ class AmfController < ApplicationController
 
        # close previous changeset and add comment
        if closeid
+         cs = Changeset.find(closeid)
+         cs.open = false
+         if closecomment.empty?
+               cs.save!
+         else
+           cs.tags['comment']=closecomment
+           cs.save_with_tags!
+         end
        end
        
        # open a new changeset
@@ -159,19 +174,20 @@ class AmfController < ApplicationController
     begin
       check_boundaries(xmin, ymin, xmax, ymax)
     rescue Exception => err
-      # FIXME: report an error rather than just return an empty result
-      return [[],[],[]]
+      return [-2,"Sorry - I can't get the map for that area."]
     end
 
        if POTLATCH_USE_SQL then
          ways = sql_find_ways_in_area(xmin, ymin, xmax, ymax)
          points = sql_find_pois_in_area(xmin, ymin, xmax, ymax)
-         relation_ids = sql_find_relations_in_area_and_ways(xmin, ymin, xmax, ymax, ways.collect {|x| x[0]})
+         relations = sql_find_relations_in_area_and_ways(xmin, ymin, xmax, ymax, ways.collect {|x| x[0]})
        else
          # find the way ids in an area
-         nodes_in_area = Node.find_by_area(ymin, xmin, ymax, xmax, :conditions => ["current_nodes.visible = ?", true], :include => :ways)  # ** include causes problems
-         way_ids = nodes_in_area.collect { |node| node.way_ids }.flatten.uniq
-         # ** get versions
+         nodes_in_area = Node.find_by_area(ymin, xmin, ymax, xmax, :conditions => ["current_nodes.visible = ?", true], :include => :ways)
+         ways = nodes_in_area.collect { |node| 
+               node.ways.collect { |w| [w.id,w.version] }.flatten
+         }.uniq
+         ways.delete([])
 
          # find the node ids in an area that aren't part of ways
          nodes_not_used_in_area = nodes_in_area.select { |node| node.ways.empty? }
@@ -179,11 +195,11 @@ class AmfController < ApplicationController
 
          # find the relations used by those nodes and ways
          relations = Relation.find_for_nodes(nodes_in_area.collect { |n| n.id }, :conditions => {:visible => true}) +
-                  Relation.find_for_ways(way_ids, :conditions => {:visible => true})
-         relation_ids = relations.collect { |relation| relation.id }.uniq
+                  Relation.find_for_ways(ways.collect { |w| w[0] }, :conditions => {:visible => true})
+         relations = relations.collect { |relation| [relation.id,relation.version] }.uniq
        end
 
-       [ways, points, relation_ids]
+       [0,ways, points, relations]
   end
 
   # Find deleted ways in current bounding box (similar to whichways, but ways
@@ -198,14 +214,13 @@ class AmfController < ApplicationController
     begin
       check_boundaries(xmin, ymin, xmax, ymax)
     rescue Exception => err
-      # FIXME: report an error rather than just return an empty result
-      return [[]]
+      return [-2,"Sorry - I can't get the map for that area."]
     end
 
        nodes_in_area = Node.find_by_area(ymin, xmin, ymax, xmax, :conditions => ["current_ways.visible = ?", false], :include => :ways_via_history)
        way_ids = nodes_in_area.collect { |node| node.ways_via_history_ids }.flatten.uniq
 
-       [way_ids]
+       [0,way_ids]
   end
 
   # Get a way including nodes and tags.
@@ -310,6 +325,27 @@ class AmfController < ApplicationController
     end
   end
 
+  # Find GPS traces with specified name/id.
+  # Returns array listing GPXs, each one comprising id, name and description.
+  
+  def findgpx(searchterm, usertoken)
+       uid = getuserid(usertoken)
+       if !uid then return -1,"You must be logged in to search for GPX traces." end
+
+       gpxs = []
+       if searchterm.to_i>0 then
+         gpx = Trace.find(searchterm.to_i, :conditions => ["visible=? AND (public=? OR user_id=?)",true,true,uid] )
+         if gpx then
+           gpxs.push([gpx.id, gpx.name, gpx.description])
+         end
+       else
+         Trace.find(:all, :limit => 21, :conditions => ["visible=? AND (public=? OR user_id=?) AND MATCH(name) AGAINST (?)",true,true,uid,searchterm] ).each do |gpx|
+               gpxs.push([gpx.id, gpx.name, gpx.description])
+         end
+       end
+       gpxs
+  end
+
   # Get a relation with all tags and members.
   # Returns:
   # 0. relation id,
@@ -613,10 +649,10 @@ class AmfController < ApplicationController
   end
 
   # Authenticate token
-  # (could be removed if no-one uses the username+password form)
+  # (can also be of form user:pass)
 
   def getuserid(token) #:doc:
-       if (token =~ /^(.+)\+(.+)$/) then
+       if (token =~ /^(.+)\:(.+)$/) then
          user = User.authenticate(:username => $1, :password => $2)
        else
          user = User.authenticate(:token => token)