1 class MapBugsController < ApplicationController
3 layout 'site', :only => [:mine]
5 before_filter :check_api_readable
6 before_filter :authorize_web, :only => [:create, :close, :update, :delete, :mine]
7 before_filter :check_api_writable, :only => [:create, :close, :update, :delete]
8 before_filter :require_moderator, :only => [:delete]
9 before_filter :set_locale, :only => [:mine]
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
20 if bbox and bbox.count(',') == 3
21 bbox = bbox.split(',')
22 @min_lon, @min_lat, @max_lon, @max_lat = sanitise_boundaries(bbox)
24 #Fallback to old style, this is deprecated and should not be used
25 raise OSM::APIBadUserInput.new("No l was given") unless params['l']
26 raise OSM::APIBadUserInput.new("No r was given") unless params['r']
27 raise OSM::APIBadUserInput.new("No b was given") unless params['b']
28 raise OSM::APIBadUserInput.new("No t was given") unless params['t']
30 @min_lon = params['l'].to_f
31 @max_lon = params['r'].to_f
32 @min_lat = params['b'].to_f
33 @max_lat = params['t'].to_f
37 conditions = closedCondition
39 check_boundaries(@min_lon, @min_lat, @max_lon, @max_lat, MAX_BUG_REQUEST_AREA)
41 @bugs = MapBug.find_by_area(@min_lat, @min_lon, @max_lat, @max_lon, :include => :comments, :order => "updated_at DESC", :limit => limit, :conditions => conditions)
43 respond_to do |format|
44 format.html {render :template => 'map_bugs/list.rjs', :content_type => "text/javascript"}
45 format.rss {render :template => 'map_bugs/list.rss'}
47 format.xml {render :template => 'map_bugs/list.xml'}
48 format.json { render :json => @bugs.to_json(:methods => [:lat, :lon], :only => [:id, :status, :created_at], :include => { :comments => { :only => [:author_name, :created_at, :body]}}) }
49 format.gpx {render :template => 'map_bugs/list.gpx'}
54 raise OSM::APIBadUserInput.new("No lat was given") unless params['lat']
55 raise OSM::APIBadUserInput.new("No lon was given") unless params['lon']
56 raise OSM::APIBadUserInput.new("No text was given") unless params['text']
58 lon = params['lon'].to_f
59 lat = params['lat'].to_f
60 comment = params['text']
63 name = params['name'] if params['name']
65 #Include in a transaction to ensure that there is always a map_bug_comment for every map_bug
67 @bug = MapBug.create_bug(lat, lon)
69 #TODO: move this into a helper function
71 url = "http://nominatim.openstreetmap.org/reverse?lat=" + lat.to_s + "&lon=" + lon.to_s + "&zoom=16"
72 response = REXML::Document.new(Net::HTTP.get(URI.parse(url)))
74 if result = response.get_text("reversegeocode/result")
75 @bug.nearby_place = result.to_s
77 @bug.nearby_place = "unknown"
79 rescue Exception => err
80 @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")
132 conditions = closedCondition
134 # Figure out the bbox
135 bbox = params['bbox']
137 if bbox and bbox.count(',') == 3
138 bbox = bbox.split(',')
139 @min_lon, @min_lat, @max_lon, @max_lat = sanitise_boundaries(bbox)
141 check_boundaries(@min_lon, @min_lat, @max_lon, @max_lat, MAX_BUG_REQUEST_AREA)
143 conditions = cond_merge conditions, [OSM.sql_for_area(@min_lat, @min_lon, @max_lat, @max_lon)]
146 @comments = MapBugComment.find(:all, :limit => limit, :order => "created_at DESC", :joins => :map_bug, :include => :map_bug, :conditions => conditions)
147 render :template => 'map_bugs/rss.rss'
151 @bug = MapBug.find(params['id'])
152 raise OSM::APINotFoundError unless @bug
153 raise OSM::APIAlreadyDeletedError unless @bug.visible
155 respond_to do |format|
158 format.json { render :json => @bug.to_json(:methods => [:lat, :lon], :only => [:id, :status, :created_at], :include => { :comments => { :only => [:author_name, :created_at, :body]}}) }
164 bug = MapBug.find(params['id'])
165 raise OSM::APINotFoundError unless @bug
166 raise OSM::APIAlreadyDeletedError unless @bug.visible
168 MapBug.transaction do
169 bug.status = "hidden"
171 add_comment(bug,:nil,name,"hidden")
174 render :text => "ok\n", :content_type => "text/html"
178 raise OSM::APIBadUserInput.new("No query string was given") unless params['q']
180 conditions = closedCondition
181 conditions = cond_merge conditions, ['map_bug_comment.body ~ ?', params['q']]
183 #TODO: There should be a better way to do this. CloseConditions are ignored at the moment
185 bugs2 = MapBug.find(:all, :limit => limit, :order => "updated_at DESC", :joins => :comments, :include => :comments,
186 :conditions => conditions)
188 respond_to do |format|
189 format.html {render :template => 'map_bugs/list.rjs', :content_type => "text/javascript"}
190 format.rss {render :template => 'map_bugs/list.rss'}
192 format.xml {render :template => 'map_bugs/list.xml'}
193 format.json { render :json => @bugs.to_json(:methods => [:lat, :lon], :only => [:id, :status, :created_at], :include => { :comments => { :only => [:author_name, :created_at, :body]}}) }
194 format.gpx {render :template => 'map_bugs/list.gpx'}
199 if params[:display_name]
200 @user2 = User.find_by_display_name(params[:display_name], :conditions => { :status => ["active", "confirmed"] })
203 if @user2.data_public? or @user2 == @user
204 conditions = ['map_bug_comment.author_id = ?', @user2.id]
206 conditions = ['false']
208 else #if request.format == :html
209 @title = t 'user.no_such_user.title'
210 @not_found_user = params[:display_name]
211 render :template => 'user/no_such_user', :status => :not_found
217 user_link = render_to_string :partial => "user", :object => @user2
220 @title = t 'bugs.user.title_user', :user => @user2.display_name
221 @heading = t 'bugs.user.heading_user', :user => @user2.display_name
222 @description = t 'bugs.user.description_user', :user => user_link
224 @page = (params[:page] || 1).to_i
227 @bugs = MapBug.find(:all,
228 :include => [:comments, {:comments => :author}],
230 :order => "updated_at DESC",
231 :conditions => conditions,
232 :offset => (@page - 1) * @page_size,
233 :limit => @page_size).uniq
237 #------------------------------------------------------------
238 # utility functions below.
239 #------------------------------------------------------------
242 # merge two conditions
243 # TODO: this is a copy from changeset_controler.rb and should be factored out to share
248 return [ a_str + " AND " + b_str ] + a + b
258 output_js = :true if params['format'] == "js"
260 if output_js == :true
261 render :text => "osbResponse();", :content_type => "text/javascript"
263 render :text => "ok " + @bug.id.to_s + "\n", :content_type => "text/html" if @bug
264 render :text => "ok\n", :content_type => "text/html" unless @bug
270 limit = params['limit'] if ((params['limit']) && (params['limit'].to_i < 10000) && (params['limit'].to_i > 0))
275 closed_since = 7 unless params['closed']
276 closed_since = params['closed'].to_i if params['closed']
279 conditions = ["status != 'hidden'"]
280 elsif closed_since > 0
281 conditions = ["((status = 'open') OR ((status = 'closed' ) AND (closed_at > '" + (Time.now - closed_since.days).to_s + "')))"]
283 conditions = ["status = 'open'"]
289 def add_comment(bug, comment, name,event)
290 bug_comment = bug.comments.create(:visible => true, :event => event)
291 bug_comment.body = comment unless comment == :nil
293 bug_comment.author_id = @user.id
294 bug_comment.author_name = @user.display_name
296 bug_comment.author_ip = request.remote_ip
297 bug_comment.author_name = name + " (a)"
303 bug.comments.each do | cmt |
305 unless sent_to.include?(cmt.author)
306 Notifier.deliver_bug_comment_notification(bug_comment, cmt.author) unless cmt.author == @user
307 sent_to.add(cmt.author)