]> git.openstreetmap.org Git - rails.git/blob - app/controllers/map_bugs_controller.rb
16d127641493236b78dd8dc7e183f38f29cbd1f8
[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         limit = getLimit
37         conditions = closedCondition
38         
39         # check boundary is sane and area within defined
40     # see /config/application.yml
41     begin
42       check_boundaries(min_lon, min_lat, max_lon, max_lat)
43     rescue Exception => err
44       report_error(err.message)
45       return
46     end
47
48
49
50         @bugs = MapBug.find_by_area(min_lat, min_lon, max_lat, max_lon, :order => "last_changed DESC", :limit => limit, :conditions => conditions)
51
52         respond_to do |format|
53           format.html {render :template => 'map_bugs/get_bugs.js', :content_type => "text/javascript"}
54           format.rss {render :template => 'map_bugs/get_bugs.rss'}
55           format.js
56           format.xml {render :template => 'map_bugs/get_bugs.xml'}
57           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]}}) }      
58 #         format.gpx {render :template => 'map_bugs/get_bugs.gpx'}
59         end
60   end
61
62   def add_bug
63         raise OSM::APIBadUserInput.new("No lat was given") unless params['lat']
64         raise OSM::APIBadUserInput.new("No lon was given") unless params['lon']
65         raise OSM::APIBadUserInput.new("No text was given") unless params['text']
66
67         lon = params['lon'].to_f
68         lat = params['lat'].to_f
69         comment = params['text']
70
71         name = "NoName";
72         name = params['name'] if params['name'];
73
74     @bug = MapBug.create_bug(lat, lon)
75
76
77         #TODO: move this into a helper function
78         url = "http://nominatim.openstreetmap.org/reverse?lat=" + lat.to_s + "&lon=" + lon.to_s + "&zoom=16" 
79     response = REXML::Document.new(Net::HTTP.get(URI.parse(url))) 
80  
81     if result = response.get_text("reversegeocode/result") 
82       @bug.nearby_place = result.to_s 
83     else 
84       @bug.nearby_place = "unknown"
85     end 
86         
87         @bug.save;
88         add_comment(@bug, comment, name);
89  
90         render_ok
91   end
92
93   def edit_bug
94         raise OSM::APIBadUserInput.new("No id was given") unless params['id']
95         raise OSM::APIBadUserInput.new("No text was given") unless params['text']
96
97         name = "NoName";
98         name = params['name'] if params['name'];
99         
100         id = params['id'].to_i
101
102         bug = MapBug.find_by_id(id);
103         raise OSM::APINotFoundError unless bug
104         raise OSM::APIAlreadyDeletedError unless bug.visible
105
106         bug_comment = add_comment(bug, params['text'], name);
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
116         bug = MapBug.find_by_id(id);
117         raise OSM::APINotFoundError unless bug
118         raise OSM::APIAlreadyDeletedError unless bug.visible
119
120         bug.close_bug;
121
122         render_ok
123   end 
124
125
126   def rss
127         request.format = :rss
128         get_bugs
129   end
130
131   def gpx_bugs
132         request.format = :xml
133         get_bugs
134   end
135
136   def read
137         @bug = MapBug.find(params['id'])
138         raise OSM::APINotFoundError unless @bug
139         raise OSM::APIAlreadyDeletedError unless @bug.visible
140
141         respond_to do |format|
142           format.rss
143           format.xml
144           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]}}) }       
145         end
146   end
147
148   def delete
149         bug = MapBug.find(params['id'])
150         raise OSM::APINotFoundError unless @bug
151         raise OSM::APIAlreadyDeletedError unless @bug.visible
152         bug.status = "hidden"
153         bug.save
154         render :text => "ok\n", :content_type => "text/html" 
155   end
156
157   def search
158         raise OSM::APIBadUserInput.new("No query string was given") unless params['q']
159         limit = getLimit
160         conditions = closedCondition
161         conditions = cond_merge conditions, ['map_bug_comment.comment ~ ?', params['q']]
162         
163         #TODO: There should be a better way to do this.   CloseConditions are ignored at the moment
164
165         bugs2 = MapBug.find(:all, :limit => limit, :order => "last_changed DESC", :joins => :map_bug_comment,
166                                                 :conditions => conditions)
167         @bugs = bugs2.uniq
168         respond_to do |format|
169           format.html {render :template => 'map_bugs/get_bugs.js', :content_type => "text/javascript"}
170           format.rss {render :template => 'map_bugs/get_bugs.rss'}
171           format.js
172           format.xml {render :template => 'map_bugs/get_bugs.xml'}
173           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]}}) }
174 #         format.gpx {render :template => 'map_bugs/get_bugs.gpx'}
175         end
176   end
177
178   def my_bugs
179  
180     if params[:display_name] 
181       @user2 = User.find_by_display_name(params[:display_name], :conditions => { :visible => true }) 
182  
183       if @user2  
184         if @user2.data_public? or @user2 == @user 
185           conditions = ['map_bug_comment.commenter_id = ?', @user2.id] 
186         else 
187           conditions = ['false'] 
188         end 
189       elsif request.format == :html 
190         @title = t 'user.no_such_user.title' 
191         @not_found_user = params[:display_name] 
192         render :template => 'user/no_such_user', :status => :not_found 
193       end 
194     end
195
196         if @user2 
197       user_link = render_to_string :partial => "user", :object => @user2 
198     end 
199
200         @title =  t 'bugs.user.title_user', :user => @user2.display_name 
201     @heading =  t 'bugs.user.heading_user', :user => @user2.display_name 
202     @description = t 'bugs.user.description_user', :user => user_link
203
204         @page = (params[:page] || 1).to_i 
205     @page_size = 10
206
207         @bugs = MapBug.find(:all, 
208                                                 :include => [:map_bug_comment, {:map_bug_comment => :user}],
209                                                 :joins => :map_bug_comment,
210                                                 :order => "last_changed DESC",
211                                                 :conditions => conditions,
212                                                 :offset => (@page - 1) * @page_size, 
213                                                 :limit => @page_size).uniq
214         
215   end
216
217 private 
218   #------------------------------------------------------------ 
219   # utility functions below. 
220   #------------------------------------------------------------   
221  
222   ## 
223   # merge two conditions 
224   # TODO: this is a copy from changeset_controler.rb and should be factored out to share
225   def cond_merge(a, b) 
226     if a and b 
227       a_str = a.shift 
228       b_str = b.shift 
229       return [ a_str + " AND " + b_str ] + a + b 
230     elsif a  
231       return a 
232     else b 
233       return b 
234     end 
235   end 
236
237
238
239   def render_ok
240         output_js = :false
241         output_js = :true if params['format'] == "js"
242
243         if output_js == :true
244           render :text => "osbResponse();", :content_type => "text/javascript" 
245         else
246           render :text => "ok " + @bug.id.to_s + "\n", :content_type => "text/html" if @bug
247           render :text => "ok\n", :content_type => "text/html" unless @bug
248         end
249   end
250
251   def getLimit
252         limit = 100;
253         limit = params['limit'] if ((params['limit']) && (params['limit'].to_i < 10000) && (params['limit'].to_i > 0))
254         return limit
255   end
256
257   def closedCondition
258         closed_since = 7 unless params['closed']
259         closed_since = params['closed'].to_i if params['closed']
260         
261         if closed_since < 0
262           conditions = ["status != 'hidden'"]
263         elsif closed_since > 0
264           conditions = ["((status = 'open') OR ((status = 'closed' ) AND (date_closed > '" + (Time.now - closed_since.days).to_s + "')))"]
265         else
266           conditions = ["status = 'open'"]
267         end
268
269         return conditions
270   end
271
272   def add_comment(bug, comment, name) 
273     t = Time.now.getutc 
274     bug_comment = bug.map_bug_comment.create(:date_created => t, :visible => true, :comment => comment);  
275     if @user  
276       bug_comment.commenter_id = @user.id
277           bug_comment.commenter_name = @user.display_name
278     else  
279       bug_comment.commenter_ip = request.remote_ip
280           bug_comment.commenter_name = name + " (a)"
281     end
282     bug_comment.save; 
283     bug.last_changed = t 
284     bug.save 
285   end
286
287 end