]> git.openstreetmap.org Git - rails.git/blob - app/controllers/map_bugs_controller.rb
2de11ba755e49578e5c58c9ff2591a6e8d63e3a7
[rails.git] / app / controllers / map_bugs_controller.rb
1 class MapBugsController < ApplicationController
2
3   layout 'site', :only => [:my_bugs]
4
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
12
13   # Help methods for checking boundary sanity and area size
14   include MapBoundary
15
16   def get_bugs
17
18     # Figure out the bbox
19     bbox = params['bbox']
20
21     if bbox and bbox.count(',') == 3
22       bbox = bbox.split(',')
23       @min_lon, @min_lat, @max_lon, @max_lat = sanitise_boundaries(bbox)
24     else
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']
30
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
35     end
36
37     limit = getLimit
38     conditions = closedCondition
39         
40     check_boundaries(@min_lon, @min_lat, @max_lon, @max_lat, :false)
41
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)
43
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'}
47       format.js
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'}
51     end
52   end
53
54   def add_bug
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']
58
59     lon = params['lon'].to_f
60     lat = params['lat'].to_f
61     comment = params['text']
62
63     name = "NoName"
64     name = params['name'] if params['name']
65
66     #Include in a transaction to ensure that there is always a map_bug_comment for every map_bug
67     MapBug.transaction do
68       @bug = MapBug.create_bug(lat, lon)
69
70       #TODO: move this into a helper function
71       begin
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))) 
74                 
75         if result = response.get_text("reversegeocode/result") 
76           @bug.nearby_place = result.to_s 
77         else 
78           @bug.nearby_place = "unknown"
79         end
80       rescue Exception => err
81         @bug.nearby_place = "unknown"
82       end
83           
84       @bug.save
85       add_comment(@bug, comment, name,"opened")
86     end
87  
88     render_ok
89   end
90
91   def edit_bug
92     raise OSM::APIBadUserInput.new("No id was given") unless params['id']
93     raise OSM::APIBadUserInput.new("No text was given") unless params['text']
94
95     name = "NoName"
96     name = params['name'] if params['name']
97         
98     id = params['id'].to_i
99
100     bug = MapBug.find_by_id(id)
101     raise OSM::APINotFoundError unless bug
102     raise OSM::APIAlreadyDeletedError unless bug.visible
103
104     MapBug.transaction do
105       bug_comment = add_comment(bug, params['text'], name,"commented")
106     end
107
108     render_ok
109   end
110
111   def close_bug
112     raise OSM::APIBadUserInput.new("No id was given") unless params['id']
113         
114     id = params['id'].to_i
115     name = "NoName"
116     name = params['name'] if params['name']
117
118     bug = MapBug.find_by_id(id)
119     raise OSM::APINotFoundError unless bug
120     raise OSM::APIAlreadyDeletedError unless bug.visible
121
122     MapBug.transaction do
123       bug.close_bug
124       add_comment(bug,:nil,name,"closed")
125     end
126
127     render_ok
128   end 
129
130
131   def rss
132     # Figure out the bbox
133     bbox = params['bbox']
134
135     if bbox and bbox.count(',') == 3
136       bbox = bbox.split(',')
137       @min_lon, @min_lat, @max_lon, @max_lat = sanitise_boundaries(bbox)
138     else
139       @min_lon = -180.0
140       @min_lat = -90.0
141       @max_lon = 180.0
142       @max_lat = 90.0
143     end
144
145     limit = getLimit
146     conditions = closedCondition
147     conditions = cond_merge conditions, [OSM.sql_for_area_no_quadtile(@min_lat, @min_lon, @max_lat, @max_lon)]
148         
149     check_boundaries(@min_lon, @min_lat, @max_lon, @max_lat, :false)
150
151     @comments = MapBugComment.find(:all, :limit => limit, :order => "date_created DESC", :joins => :map_bug, :include => :map_bug, :conditions => conditions)
152     render :template => 'map_bugs/rss.rss'
153   end
154
155   def gpx_bugs
156     request.format = :xml
157     get_bugs
158   end
159   
160   def read
161     @bug = MapBug.find(params['id'])
162     raise OSM::APINotFoundError unless @bug
163     raise OSM::APIAlreadyDeletedError unless @bug.visible
164     
165     respond_to do |format|
166       format.rss
167       format.xml
168       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]}}) }   
169       format.gpx
170     end
171   end
172
173   def delete
174     bug = MapBug.find(params['id'])
175     raise OSM::APINotFoundError unless @bug
176     raise OSM::APIAlreadyDeletedError unless @bug.visible
177
178     MapBug.transaction do
179       bug.status = "hidden"
180       bug.save
181       add_comment(bug,:nil,name,"hidden")
182     end
183
184     render :text => "ok\n", :content_type => "text/html" 
185   end
186
187   def search
188     raise OSM::APIBadUserInput.new("No query string was given") unless params['q']
189     limit = getLimit
190     conditions = closedCondition
191     conditions = cond_merge conditions, ['map_bug_comment.comment ~ ?', params['q']]
192         
193     #TODO: There should be a better way to do this.   CloseConditions are ignored at the moment
194
195     bugs2 = MapBug.find(:all, :limit => limit, :order => "last_changed DESC", :joins => :map_bug_comment, :include => :map_bug_comment,
196                         :conditions => conditions)
197     @bugs = bugs2.uniq
198     respond_to do |format|
199       format.html {render :template => 'map_bugs/get_bugs.js', :content_type => "text/javascript"}
200       format.rss {render :template => 'map_bugs/get_bugs.rss'}
201       format.js
202       format.xml {render :template => 'map_bugs/get_bugs.xml'}
203       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]}}) }
204       format.gpx {render :template => 'map_bugs/get_bugs.gpx'}
205     end
206   end
207
208   def my_bugs
209     if params[:display_name] 
210       @user2 = User.find_by_display_name(params[:display_name], :conditions => { :status => ["active", "confirmed"] }) 
211  
212       if @user2  
213         if @user2.data_public? or @user2 == @user 
214           conditions = ['map_bug_comment.commenter_id = ?', @user2.id] 
215         else 
216           conditions = ['false'] 
217         end 
218       else #if request.format == :html 
219         @title = t 'user.no_such_user.title' 
220         @not_found_user = params[:display_name] 
221         render :template => 'user/no_such_user', :status => :not_found 
222         return
223       end 
224     end
225
226     if @user2 
227       user_link = render_to_string :partial => "user", :object => @user2 
228     end 
229     
230     @title =  t 'bugs.user.title_user', :user => @user2.display_name 
231     @heading =  t 'bugs.user.heading_user', :user => @user2.display_name 
232     @description = t 'bugs.user.description_user', :user => user_link
233     
234     @page = (params[:page] || 1).to_i 
235     @page_size = 10
236
237     @bugs = MapBug.find(:all, 
238                         :include => [:map_bug_comment, {:map_bug_comment => :user}],
239                         :joins => :map_bug_comment,
240                         :order => "last_changed DESC",
241                         :conditions => conditions,
242                         :offset => (@page - 1) * @page_size, 
243                         :limit => @page_size).uniq
244   end
245
246 private 
247   #------------------------------------------------------------ 
248   # utility functions below. 
249   #------------------------------------------------------------   
250  
251   ## 
252   # merge two conditions 
253   # TODO: this is a copy from changeset_controler.rb and should be factored out to share
254   def cond_merge(a, b) 
255     if a and b 
256       a_str = a.shift 
257       b_str = b.shift 
258       return [ a_str + " AND " + b_str ] + a + b 
259     elsif a  
260       return a 
261     else b 
262       return b 
263     end 
264   end 
265
266   def render_ok
267     output_js = :false
268     output_js = :true if params['format'] == "js"
269
270     if output_js == :true
271       render :text => "osbResponse();", :content_type => "text/javascript" 
272     else
273       render :text => "ok " + @bug.id.to_s + "\n", :content_type => "text/html" if @bug
274       render :text => "ok\n", :content_type => "text/html" unless @bug
275     end
276   end
277
278   def getLimit
279     limit = 100
280     limit = params['limit'] if ((params['limit']) && (params['limit'].to_i < 10000) && (params['limit'].to_i > 0))
281     return limit
282   end
283
284   def closedCondition
285     closed_since = 7 unless params['closed']
286     closed_since = params['closed'].to_i if params['closed']
287         
288     if closed_since < 0
289       conditions = ["status != 'hidden'"]
290     elsif closed_since > 0
291       conditions = ["((status = 'open') OR ((status = 'closed' ) AND (date_closed > '" + (Time.now - closed_since.days).to_s + "')))"]
292     else
293       conditions = ["status = 'open'"]
294     end
295
296     return conditions
297   end
298
299   def add_comment(bug, comment, name,event) 
300     t = Time.now.getutc 
301     bug_comment = bug.map_bug_comment.create(:date_created => t, :visible => true, :event => event)
302     bug_comment.comment = comment unless comment == :nil
303     if @user  
304       bug_comment.commenter_id = @user.id
305       bug_comment.commenter_name = @user.display_name
306     else  
307       bug_comment.commenter_ip = request.remote_ip
308       bug_comment.commenter_name = name + " (a)"
309     end
310     bug_comment.save 
311     bug.last_changed = t 
312     bug.save
313
314     sent_to = Set.new
315     bug.map_bug_comment.each do | cmt |
316       if cmt.user
317         unless sent_to.include?(cmt.user)
318           Notifier.deliver_bug_comment_notification(bug_comment, cmt.user) unless cmt.user == @user
319           sent_to.add(cmt.user)
320         end
321       end
322     end
323   end
324 end