1 class MapBugsController < ApplicationController
3 before_filter :check_api_readable
4 before_filter :authorize_web, :only => [:add_bug, :close_bug, :edit_bug, :delete]
5 before_filter :check_api_writable, :only => [:add_bug, :close_bug, :edit_bug, :delete]
6 before_filter :require_moderator, :only => [:delete]
7 after_filter :compress_output
8 around_filter :api_call_handle_error, :api_call_timeout
10 # Help methods for checking boundary sanity and area size
18 if bbox and bbox.count(',') == 3
19 bbox = bbox.split(',')
20 min_lon, min_lat, max_lon, max_lat = sanitise_boundaries(bbox)
22 #Fallback to old style, this is deprecated and should not be used
23 raise OSM::APIBadUserInput.new("No l was given") unless params['l']
24 raise OSM::APIBadUserInput.new("No r was given") unless params['r']
25 raise OSM::APIBadUserInput.new("No b was given") unless params['b']
26 raise OSM::APIBadUserInput.new("No t was given") unless params['t']
28 min_lon = params['l'].to_f
29 max_lon = params['r'].to_f
30 min_lat = params['b'].to_f
31 max_lat = params['t'].to_f
34 conditions = closedCondition
36 # check boundary is sane and area within defined
37 # see /config/application.yml
39 check_boundaries(min_lon, min_lat, max_lon, max_lat)
40 rescue Exception => err
41 report_error(err.message)
47 @bugs = MapBug.find_by_area(min_lat, min_lon, max_lat, max_lon, :order => "last_changed DESC", :limit => limit, :conditions => conditions)
49 respond_to do |format|
50 format.html {render :template => 'map_bugs/get_bugs.js', :content_type => "text/javascript"}
51 format.rss {render :template => 'map_bugs/get_bugs.rss'}
53 format.xml {render :template => 'map_bugs/get_bugs.xml'}
54 format.json { render :json => @bugs.to_json(:methods => [:lat, :lon], :only => [:id, :status, :date_created], :include => { :map_bug_comment => { :only => [:commenter_name, :date_created, :comment]}}) }
55 # format.gpx {render :template => 'map_bugs/get_bugs.gpx'}
60 raise OSM::APIBadUserInput.new("No lat was given") unless params['lat']
61 raise OSM::APIBadUserInput.new("No lon was given") unless params['lon']
62 raise OSM::APIBadUserInput.new("No text was given") unless params['text']
64 lon = params['lon'].to_f
65 lat = params['lat'].to_f
66 comment = params['text']
69 name = params['name'] if params['name'];
71 @bug = MapBug.create_bug(lat, lon)
74 #TODO: move this into a helper function
75 url = "http://nominatim.openstreetmap.org/reverse?lat=" + lat.to_s + "&lon=" + lon.to_s + "&zoom=16"
76 response = REXML::Document.new(Net::HTTP.get(URI.parse(url)))
78 if result = response.get_text("reversegeocode/result")
79 @bug.nearby_place = result.to_s
81 @bug.nearby_place = "unknown"
85 add_comment(@bug, comment, name);
91 raise OSM::APIBadUserInput.new("No id was given") unless params['id']
92 raise OSM::APIBadUserInput.new("No text was given") unless params['text']
95 name = params['name'] if params['name'];
97 id = params['id'].to_i
99 bug = MapBug.find_by_id(id);
101 bug_comment = add_comment(bug, params['text'], name);
107 raise OSM::APIBadUserInput.new("No id was given") unless params['id']
109 id = params['id'].to_i
111 bug = MapBug.find_by_id(id);
119 request.format = :rss
124 request.format = :xml
129 @bug = MapBug.find(params['id'])
130 render :text => "", :status => :gone unless @bug.visible
131 respond_to do |format|
134 format.json { render :json => @bug.to_json(:methods => [:lat, :lon], :only => [:id, :status, :date_created], :include => { :map_bug_comment => { :only => [:commenter_name, :date_created, :comment]}}) }
139 bug = MapBug.find(params['id'])
140 bug.status = "hidden"
142 render :text => "ok\n", :content_type => "text/html"
146 raise OSM::APIBadUserInput.new("No query string was given") unless params['q']
148 conditions = closedCondition
150 #TODO: There should be a better way to do this. CloseConditions are ignored at the moment
152 bugs2 = MapBug.find(:all, :limit => limit, :order => "last_changed DESC", :joins => :map_bug_comment,
153 :conditions => ['map_bug_comment.comment ~ ?', params['q']])
155 respond_to do |format|
156 format.html {render :template => 'map_bugs/get_bugs.js', :content_type => "text/javascript"}
157 format.rss {render :template => 'map_bugs/get_bugs.rss'}
159 format.xml {render :template => 'map_bugs/get_bugs.xml'}
160 format.json { render :json => @bugs.to_json(:methods => [:lat, :lon], :only => [:id, :status, :date_created], :include => { :map_bug_comment => { :only => [:commenter_name, :date_created, :comment]}}) }
161 # format.gpx {render :template => 'map_bugs/get_bugs.gpx'}
168 output_js = :true if params['format'] == "js"
170 if output_js == :true
171 render :text => "osbResponse();", :content_type => "text/javascript"
173 render :text => "ok " + @bug.id.to_s + "\n", :content_type => "text/html" if @bug
174 render :text => "ok\n", :content_type => "text/html" unless @bug
180 limit = params['limit'] if ((params['limit']) && (params['limit'].to_i < 10000) && (params['limit'].to_i > 0))
185 closed_since = 7 unless params['closed']
186 closed_since = params['closed'].to_i if params['closed']
189 conditions = "status != 'hidden'"
190 elsif closed_since > 0
191 conditions = "((status = 'open') OR ((status = 'closed' ) AND (date_closed > '" + (Time.now - 7.days).to_s + "')))"
193 conditions = "status = 'open'"
199 def add_comment(bug, comment, name)
201 bug_comment = bug.map_bug_comment.create(:date_created => t, :visible => true, :comment => comment);
203 bug_comment.commenter_id = @user.id
204 bug_comment.commenter_name = @user.display_name
206 bug_comment.commenter_ip = request.remote_ip
207 bug_comment.commenter_name = name + " (a)"