1 class MapBugsController < ApplicationController
3 layout 'site', :only => [:my_bugs]
5 before_filter :check_api_readable
6 before_filter :authorize_web, :only => [:add_bug, :close_bug, :edit_bug, :delete, :my_bugs]
7 before_filter :check_api_writable, :only => [:add_bug, :close_bug, :edit_bug, :delete]
8 before_filter :require_moderator, :only => [:delete]
9 before_filter :set_locale, :only => [:my_bugs]
10 after_filter :compress_output
11 around_filter :api_call_handle_error, :api_call_timeout
13 # Help methods for checking boundary sanity and area size
21 if bbox and bbox.count(',') == 3
22 bbox = bbox.split(',')
23 @min_lon, @min_lat, @max_lon, @max_lat = sanitise_boundaries(bbox)
25 #Fallback to old style, this is deprecated and should not be used
26 raise OSM::APIBadUserInput.new("No l was given") unless params['l']
27 raise OSM::APIBadUserInput.new("No r was given") unless params['r']
28 raise OSM::APIBadUserInput.new("No b was given") unless params['b']
29 raise OSM::APIBadUserInput.new("No t was given") unless params['t']
31 @min_lon = params['l'].to_f
32 @max_lon = params['r'].to_f
33 @min_lat = params['b'].to_f
34 @max_lat = params['t'].to_f
38 conditions = closedCondition
40 check_boundaries(@min_lon, @min_lat, @max_lon, @max_lat, :false)
42 @bugs = MapBug.find_by_area_no_quadtile(@min_lat, @min_lon, @max_lat, @max_lon, :include => :map_bug_comment, :order => "last_changed DESC", :limit => limit, :conditions => conditions)
44 respond_to do |format|
45 format.html {render :template => 'map_bugs/get_bugs.js', :content_type => "text/javascript"}
46 format.rss {render :template => 'map_bugs/get_bugs.rss'}
48 format.xml {render :template => 'map_bugs/get_bugs.xml'}
49 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]}}) }
50 format.gpx {render :template => 'map_bugs/get_bugs.gpx'}
55 raise OSM::APIBadUserInput.new("No lat was given") unless params['lat']
56 raise OSM::APIBadUserInput.new("No lon was given") unless params['lon']
57 raise OSM::APIBadUserInput.new("No text was given") unless params['text']
59 lon = params['lon'].to_f
60 lat = params['lat'].to_f
61 comment = params['text']
64 name = params['name'] if params['name']
66 #Include in a transaction to ensure that there is always a map_bug_comment for every map_bug
68 @bug = MapBug.create_bug(lat, lon)
70 #TODO: move this into a helper function
72 url = "http://nominatim.openstreetmap.org/reverse?lat=" + lat.to_s + "&lon=" + lon.to_s + "&zoom=16"
73 response = REXML::Document.new(Net::HTTP.get(URI.parse(url)))
75 if result = response.get_text("reversegeocode/result")
76 @bug.nearby_place = result.to_s
78 @bug.nearby_place = "unknown"
80 rescue Exception => err
81 @bug.nearby_place = "unknown"
85 add_comment(@bug, comment, name,"opened")
92 raise OSM::APIBadUserInput.new("No id was given") unless params['id']
93 raise OSM::APIBadUserInput.new("No text was given") unless params['text']
96 name = params['name'] if params['name']
98 id = params['id'].to_i
100 bug = MapBug.find_by_id(id)
101 raise OSM::APINotFoundError unless bug
102 raise OSM::APIAlreadyDeletedError unless bug.visible
104 MapBug.transaction do
105 bug_comment = add_comment(bug, params['text'], name,"commented")
112 raise OSM::APIBadUserInput.new("No id was given") unless params['id']
114 id = params['id'].to_i
116 name = params['name'] if params['name']
118 bug = MapBug.find_by_id(id)
119 raise OSM::APINotFoundError unless bug
120 raise OSM::APIAlreadyDeletedError unless bug.visible
122 MapBug.transaction do
124 add_comment(bug,:nil,name,"closed")
131 # Figure out the bbox
132 bbox = params['bbox']
134 if bbox and bbox.count(',') == 3
135 bbox = bbox.split(',')
136 @min_lon, @min_lat, @max_lon, @max_lat = sanitise_boundaries(bbox)
145 conditions = closedCondition
146 conditions = cond_merge conditions, [OSM.sql_for_area_no_quadtile(@min_lat, @min_lon, @max_lat, @max_lon)]
148 check_boundaries(@min_lon, @min_lat, @max_lon, @max_lat, :false)
150 @comments = MapBugComment.find(:all, :limit => limit, :order => "date_created DESC", :joins => :map_bug, :include => :map_bug, :conditions => conditions)
151 render :template => 'map_bugs/rss.rss'
155 @bug = MapBug.find(params['id'])
156 raise OSM::APINotFoundError unless @bug
157 raise OSM::APIAlreadyDeletedError unless @bug.visible
159 respond_to do |format|
162 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]}}) }
168 bug = MapBug.find(params['id'])
169 raise OSM::APINotFoundError unless @bug
170 raise OSM::APIAlreadyDeletedError unless @bug.visible
172 MapBug.transaction do
173 bug.status = "hidden"
175 add_comment(bug,:nil,name,"hidden")
178 render :text => "ok\n", :content_type => "text/html"
182 raise OSM::APIBadUserInput.new("No query string was given") unless params['q']
184 conditions = closedCondition
185 conditions = cond_merge conditions, ['map_bug_comment.comment ~ ?', params['q']]
187 #TODO: There should be a better way to do this. CloseConditions are ignored at the moment
189 bugs2 = MapBug.find(:all, :limit => limit, :order => "last_changed DESC", :joins => :map_bug_comment, :include => :map_bug_comment,
190 :conditions => conditions)
192 respond_to do |format|
193 format.html {render :template => 'map_bugs/get_bugs.js', :content_type => "text/javascript"}
194 format.rss {render :template => 'map_bugs/get_bugs.rss'}
196 format.xml {render :template => 'map_bugs/get_bugs.xml'}
197 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]}}) }
198 format.gpx {render :template => 'map_bugs/get_bugs.gpx'}
203 if params[:display_name]
204 @user2 = User.find_by_display_name(params[:display_name], :conditions => { :status => ["active", "confirmed"] })
207 if @user2.data_public? or @user2 == @user
208 conditions = ['map_bug_comment.commenter_id = ?', @user2.id]
210 conditions = ['false']
212 else #if request.format == :html
213 @title = t 'user.no_such_user.title'
214 @not_found_user = params[:display_name]
215 render :template => 'user/no_such_user', :status => :not_found
221 user_link = render_to_string :partial => "user", :object => @user2
224 @title = t 'bugs.user.title_user', :user => @user2.display_name
225 @heading = t 'bugs.user.heading_user', :user => @user2.display_name
226 @description = t 'bugs.user.description_user', :user => user_link
228 @page = (params[:page] || 1).to_i
231 @bugs = MapBug.find(:all,
232 :include => [:map_bug_comment, {:map_bug_comment => :user}],
233 :joins => :map_bug_comment,
234 :order => "last_changed DESC",
235 :conditions => conditions,
236 :offset => (@page - 1) * @page_size,
237 :limit => @page_size).uniq
241 #------------------------------------------------------------
242 # utility functions below.
243 #------------------------------------------------------------
246 # merge two conditions
247 # TODO: this is a copy from changeset_controler.rb and should be factored out to share
252 return [ a_str + " AND " + b_str ] + a + b
262 output_js = :true if params['format'] == "js"
264 if output_js == :true
265 render :text => "osbResponse();", :content_type => "text/javascript"
267 render :text => "ok " + @bug.id.to_s + "\n", :content_type => "text/html" if @bug
268 render :text => "ok\n", :content_type => "text/html" unless @bug
274 limit = params['limit'] if ((params['limit']) && (params['limit'].to_i < 10000) && (params['limit'].to_i > 0))
279 closed_since = 7 unless params['closed']
280 closed_since = params['closed'].to_i if params['closed']
283 conditions = ["status != 'hidden'"]
284 elsif closed_since > 0
285 conditions = ["((status = 'open') OR ((status = 'closed' ) AND (date_closed > '" + (Time.now - closed_since.days).to_s + "')))"]
287 conditions = ["status = 'open'"]
293 def add_comment(bug, comment, name,event)
295 bug_comment = bug.map_bug_comment.create(:date_created => t, :visible => true, :event => event)
296 bug_comment.comment = comment unless comment == :nil
298 bug_comment.commenter_id = @user.id
299 bug_comment.commenter_name = @user.display_name
301 bug_comment.commenter_ip = request.remote_ip
302 bug_comment.commenter_name = name + " (a)"
309 bug.map_bug_comment.each do | cmt |
311 unless sent_to.include?(cmt.user)
312 Notifier.deliver_bug_comment_notification(bug_comment, cmt.user) unless cmt.user == @user
313 sent_to.add(cmt.user)