]> git.openstreetmap.org Git - rails.git/blob - app/controllers/map_bugs_controller.rb
Convert openstreetbugs javascript to new style api
[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_boundaries(min_lon, min_lat, max_lon, max_lat, :false)
40
41         @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)
42
43         respond_to do |format|
44           format.html {render :template => 'map_bugs/get_bugs.js', :content_type => "text/javascript"}
45           format.rss {render :template => 'map_bugs/get_bugs.rss'}
46           format.js
47           format.xml {render :template => 'map_bugs/get_bugs.xml'}
48           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]}}) }      
49 #         format.gpx {render :template => 'map_bugs/get_bugs.gpx'}
50         end
51   end
52
53   def add_bug
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']
57
58         lon = params['lon'].to_f
59         lat = params['lat'].to_f
60         comment = params['text']
61
62         name = "NoName";
63         name = params['name'] if params['name'];
64
65         #Include in a transaction to ensure that there is always a map_bug_comment for every map_bug
66         MapBug.transaction do
67           @bug = MapBug.create_bug(lat, lon)
68
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);
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         bug_comment = add_comment(bug, params['text'], name);
105
106         render_ok
107   end
108
109   def close_bug
110         raise OSM::APIBadUserInput.new("No id was given") unless params['id']
111         
112         id = params['id'].to_i
113
114         bug = MapBug.find_by_id(id);
115         raise OSM::APINotFoundError unless bug
116         raise OSM::APIAlreadyDeletedError unless bug.visible
117
118         bug.close_bug;
119
120         render_ok
121   end 
122
123
124   def rss
125         request.format = :rss
126         get_bugs
127   end
128
129   def gpx_bugs
130         request.format = :xml
131         get_bugs
132   end
133
134   def read
135         @bug = MapBug.find(params['id'])
136         raise OSM::APINotFoundError unless @bug
137         raise OSM::APIAlreadyDeletedError unless @bug.visible
138
139         respond_to do |format|
140           format.rss
141           format.xml
142           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]}}) }       
143         end
144   end
145
146   def delete
147         bug = MapBug.find(params['id'])
148         raise OSM::APINotFoundError unless @bug
149         raise OSM::APIAlreadyDeletedError unless @bug.visible
150         bug.status = "hidden"
151         bug.save
152         render :text => "ok\n", :content_type => "text/html" 
153   end
154
155   def search
156         raise OSM::APIBadUserInput.new("No query string was given") unless params['q']
157         limit = getLimit
158         conditions = closedCondition
159         conditions = cond_merge conditions, ['map_bug_comment.comment ~ ?', params['q']]
160         
161         #TODO: There should be a better way to do this.   CloseConditions are ignored at the moment
162
163         bugs2 = MapBug.find(:all, :limit => limit, :order => "last_changed DESC", :joins => :map_bug_comment, :include => :map_bug_comment,
164                                                 :conditions => conditions)
165         @bugs = bugs2.uniq
166         respond_to do |format|
167           format.html {render :template => 'map_bugs/get_bugs.js', :content_type => "text/javascript"}
168           format.rss {render :template => 'map_bugs/get_bugs.rss'}
169           format.js
170           format.xml {render :template => 'map_bugs/get_bugs.xml'}
171           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]}}) }
172 #         format.gpx {render :template => 'map_bugs/get_bugs.gpx'}
173         end
174   end
175
176   def my_bugs
177  
178     if params[:display_name] 
179       @user2 = User.find_by_display_name(params[:display_name], :conditions => { :visible => true }) 
180  
181       if @user2  
182         if @user2.data_public? or @user2 == @user 
183           conditions = ['map_bug_comment.commenter_id = ?', @user2.id] 
184         else 
185           conditions = ['false'] 
186         end 
187       elsif request.format == :html 
188         @title = t 'user.no_such_user.title' 
189         @not_found_user = params[:display_name] 
190         render :template => 'user/no_such_user', :status => :not_found 
191       end 
192     end
193
194         if @user2 
195       user_link = render_to_string :partial => "user", :object => @user2 
196     end 
197
198         @title =  t 'bugs.user.title_user', :user => @user2.display_name 
199     @heading =  t 'bugs.user.heading_user', :user => @user2.display_name 
200     @description = t 'bugs.user.description_user', :user => user_link
201
202         @page = (params[:page] || 1).to_i 
203     @page_size = 10
204
205         @bugs = MapBug.find(:all, 
206                                                 :include => [:map_bug_comment, {:map_bug_comment => :user}],
207                                                 :joins => :map_bug_comment,
208                                                 :order => "last_changed DESC",
209                                                 :conditions => conditions,
210                                                 :offset => (@page - 1) * @page_size, 
211                                                 :limit => @page_size).uniq
212         
213   end
214
215 private 
216   #------------------------------------------------------------ 
217   # utility functions below. 
218   #------------------------------------------------------------   
219  
220   ## 
221   # merge two conditions 
222   # TODO: this is a copy from changeset_controler.rb and should be factored out to share
223   def cond_merge(a, b) 
224     if a and b 
225       a_str = a.shift 
226       b_str = b.shift 
227       return [ a_str + " AND " + b_str ] + a + b 
228     elsif a  
229       return a 
230     else b 
231       return b 
232     end 
233   end 
234
235
236
237   def render_ok
238         output_js = :false
239         output_js = :true if params['format'] == "js"
240
241         if output_js == :true
242           render :text => "osbResponse();", :content_type => "text/javascript" 
243         else
244           render :text => "ok " + @bug.id.to_s + "\n", :content_type => "text/html" if @bug
245           render :text => "ok\n", :content_type => "text/html" unless @bug
246         end
247   end
248
249   def getLimit
250         limit = 100;
251         limit = params['limit'] if ((params['limit']) && (params['limit'].to_i < 10000) && (params['limit'].to_i > 0))
252         return limit
253   end
254
255   def closedCondition
256         closed_since = 7 unless params['closed']
257         closed_since = params['closed'].to_i if params['closed']
258         
259         if closed_since < 0
260           conditions = ["status != 'hidden'"]
261         elsif closed_since > 0
262           conditions = ["((status = 'open') OR ((status = 'closed' ) AND (date_closed > '" + (Time.now - closed_since.days).to_s + "')))"]
263         else
264           conditions = ["status = 'open'"]
265         end
266
267         return conditions
268   end
269
270   def add_comment(bug, comment, name) 
271     t = Time.now.getutc 
272     bug_comment = bug.map_bug_comment.create(:date_created => t, :visible => true, :comment => comment);  
273     if @user  
274       bug_comment.commenter_id = @user.id
275           bug_comment.commenter_name = @user.display_name
276     else  
277       bug_comment.commenter_ip = request.remote_ip
278           bug_comment.commenter_name = name + " (a)"
279     end
280     bug_comment.save; 
281     bug.last_changed = t 
282     bug.save 
283   end
284
285 end