]> git.openstreetmap.org Git - rails.git/blob - app/controllers/map_bugs_controller.rb
Allow for different formats to the getBugs call
[rails.git] / app / controllers / map_bugs_controller.rb
1 class MapBugsController < ApplicationController
2
3   before_filter :check_api_readable
4   before_filter :authorize_web, :only => [:add_bug, :close_bug, :edit_bug]
5   before_filter :check_api_writable, :only => [:add_bug, :close_bug, :edit_bug]
6   after_filter :compress_output
7   around_filter :api_call_handle_error, :api_call_timeout
8
9   # Help methods for checking boundary sanity and area size
10   include MapBoundary
11
12   def get_bugs
13
14         raise OSM::APIBadUserInput.new("No l was given") unless params['l']
15         raise OSM::APIBadUserInput.new("No r was given") unless params['r']
16         raise OSM::APIBadUserInput.new("No b was given") unless params['b']
17         raise OSM::APIBadUserInput.new("No t was given") unless params['t']
18
19         min_lon = params['l'].to_f
20         max_lon = params['r'].to_f
21         min_lat = params['b'].to_f
22         max_lat = params['t'].to_f
23         
24         # check boundary is sane and area within defined
25     # see /config/application.yml
26     begin
27       check_boundaries(min_lon, min_lat, max_lon, max_lat)
28     rescue Exception => err
29       report_error(err.message)
30       return
31     end
32
33         @bugs = MapBug.find_by_area(min_lat, min_lon, max_lat, max_lon, :order => "last_changed DESC", :limit => 100, :conditions => "status != 'hidden'")
34
35         respond_to do |format|
36           format.html {render :template => 'map_bugs/get_bugs.js', :content_type => "text/javascript"}
37           format.rss {render :template => 'map_bugs/get_bugs.rss'}
38           format.js
39           format.xml {render :template => 'map_bugs/get_bugs.xml'}
40           #format.gpx {render :text => "Rendering GPX"}
41         end
42   end
43
44   def add_bug
45         raise OSM::APIBadUserInput.new("No lat was given") unless params['lat']
46         raise OSM::APIBadUserInput.new("No lon was given") unless params['lon']
47         raise OSM::APIBadUserInput.new("No text was given") unless params['text']
48
49         lon = params['lon'].to_f
50         lat = params['lat'].to_f
51         comment = params['text']
52
53     bug = MapBug.create_bug(lat, lon)
54         bug.save;
55         add_comment(bug, comment);
56  
57         render_ok
58   end
59
60   def edit_bug
61         raise OSM::APIBadUserInput.new("No id was given") unless params['id']
62         raise OSM::APIBadUserInput.new("No text was given") unless params['text']
63
64         id = params['id'].to_i
65
66         bug = MapBug.find_by_id(id);
67
68         bug_comment = add_comment(bug, params['text']);
69
70         render_ok
71   end
72
73   def close_bug
74         raise OSM::APIBadUserInput.new("No id was given") unless params['id']
75         
76         id = params['id'].to_i
77
78         bug = MapBug.find_by_id(id);
79         bug.close_bug;
80
81         render_ok
82   end 
83
84   def render_ok
85         output_js = :false
86         output_js = :true if params['format'] == "js"
87
88         if output_js == :true
89           render :text => "osbResponse();", :content_type => "text/javascript" 
90         else
91           render :text => "ok\n", :content_type => "text/html" 
92         end
93         
94   end
95
96   def rss
97         request.format = :rss
98         get_bugs
99   end
100
101   def gpx_bugs
102         request.format = :xml
103         get_bugs
104   end
105
106   def add_comment(bug, comment) 
107     t = Time.now.getutc 
108     bug_comment = bug.map_bug_comment.create(:date_created => t, :visible => true, :comment => comment);  
109     if @user  
110       bug_comment.commenter_id = @user.id
111           bug_comment.commenter_name = @user.display_name
112     else  
113       bug_comment.commenter_ip = request.remote_ip
114           bug_comment.commenter_name = "anonymous (a)"
115     end
116     bug_comment.save; 
117     bug.last_changed = t 
118     bug.save 
119   end
120
121 end