]> git.openstreetmap.org Git - rails.git/blob - app/controllers/map_bugs_controller.rb
c0fc97a5f63aaf50947db6aafceb684f58a9e070
[rails.git] / app / controllers / map_bugs_controller.rb
1 class MapBugsController < ApplicationController
2
3   before_filter :check_api_readable
4   before_filter :authorize_web, :only => [:add_bug, :close_bug, :edit_bug, :delete]
5   before_filter :check_api_writable, :only => [:add_bug, :close_bug, :edit_bug, :delete]
6   before_filter :require_moderator, :only => [:delete]
7   after_filter :compress_output
8   around_filter :api_call_handle_error, :api_call_timeout
9
10   # Help methods for checking boundary sanity and area size
11   include MapBoundary
12
13   def get_bugs
14
15         # Figure out the bbox
16     bbox = params['bbox']
17
18     if bbox and bbox.count(',') == 3
19       bbox = bbox.split(',')
20           min_lon, min_lat, max_lon, max_lat = sanitise_boundaries(bbox)
21         else
22           #Fallback to old style, this is deprecated and should not be used
23           raise OSM::APIBadUserInput.new("No l was given") unless params['l']
24           raise OSM::APIBadUserInput.new("No r was given") unless params['r']
25           raise OSM::APIBadUserInput.new("No b was given") unless params['b']
26           raise OSM::APIBadUserInput.new("No t was given") unless params['t']
27
28           min_lon = params['l'].to_f
29           max_lon = params['r'].to_f
30           min_lat = params['b'].to_f
31           max_lat = params['t'].to_f
32     end
33         limit = getLimit
34         conditions = closedCondition
35         
36         # check boundary is sane and area within defined
37     # see /config/application.yml
38     begin
39       check_boundaries(min_lon, min_lat, max_lon, max_lat)
40     rescue Exception => err
41       report_error(err.message)
42       return
43     end
44
45
46
47         @bugs = MapBug.find_by_area(min_lat, min_lon, max_lat, max_lon, :order => "last_changed DESC", :limit => limit, :conditions => conditions)
48
49         respond_to do |format|
50           format.html {render :template => 'map_bugs/get_bugs.js', :content_type => "text/javascript"}
51           format.rss {render :template => 'map_bugs/get_bugs.rss'}
52           format.js
53           format.xml {render :template => 'map_bugs/get_bugs.xml'}
54           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]}}) }      
55           format.gpx {render :template => 'map_bugs/get_bugs.gpx'}
56         end
57   end
58
59   def add_bug
60         raise OSM::APIBadUserInput.new("No lat was given") unless params['lat']
61         raise OSM::APIBadUserInput.new("No lon was given") unless params['lon']
62         raise OSM::APIBadUserInput.new("No text was given") unless params['text']
63
64         lon = params['lon'].to_f
65         lat = params['lat'].to_f
66         comment = params['text']
67
68         name = "NoName";
69         name = params['name'] if params['name'];
70
71     @bug = MapBug.create_bug(lat, lon)
72         @bug.save;
73         add_comment(@bug, comment, name);
74  
75         render_ok
76   end
77
78   def edit_bug
79         raise OSM::APIBadUserInput.new("No id was given") unless params['id']
80         raise OSM::APIBadUserInput.new("No text was given") unless params['text']
81
82         name = "NoName";
83         name = params['name'] if params['name'];
84         
85         id = params['id'].to_i
86
87         bug = MapBug.find_by_id(id);
88
89         bug_comment = add_comment(bug, params['text'], name);
90
91         render_ok
92   end
93
94   def close_bug
95         raise OSM::APIBadUserInput.new("No id was given") unless params['id']
96         
97         id = params['id'].to_i
98
99         bug = MapBug.find_by_id(id);
100         bug.close_bug;
101
102         render_ok
103   end 
104
105
106   def rss
107         request.format = :rss
108         get_bugs
109   end
110
111   def gpx_bugs
112         request.format = :xml
113         get_bugs
114   end
115
116   def read
117         @bug = MapBug.find(params['id'])
118     render :text => "", :status => :gone unless @bug.visible
119         respond_to do |format|
120           format.rss
121           format.xml
122           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]}}) }       
123         end
124   end
125
126   def delete
127         bug = MapBug.find(params['id'])
128         bug.status = "hidden"
129         bug.save
130         render :text => "ok\n", :content_type => "text/html" 
131   end
132
133   def search
134         raise OSM::APIBadUserInput.new("No query string was given") unless params['q']
135         limit = getLimit
136         conditions = closedCondition
137         
138         #TODO: There should be a better way to do this.   CloseConditions are ignored at the moment
139
140         bugs2 = MapBug.find(:all, :limit => limit, :order => "last_changed DESC", :joins => :map_bug_comment,
141                                                 :conditions => ['map_bug_comment.comment ~ ?', params['q']])
142         @bugs = bugs2.uniq
143         respond_to do |format|
144           format.html {render :template => 'map_bugs/get_bugs.js', :content_type => "text/javascript"}
145           format.rss {render :template => 'map_bugs/get_bugs.rss'}
146           format.js
147           format.xml {render :template => 'map_bugs/get_bugs.xml'}
148           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]}}) }
149           format.gpx {render :template => 'map_bugs/get_bugs.gpx'}
150         end
151   end
152
153
154   def render_ok
155         output_js = :false
156         output_js = :true if params['format'] == "js"
157
158         if output_js == :true
159           render :text => "osbResponse();", :content_type => "text/javascript" 
160         else
161           render :text => "ok " + @bug.id.to_s + "\n", :content_type => "text/html" if @bug
162           render :text => "ok\n", :content_type => "text/html" unless @bug
163         end
164   end
165
166   def getLimit
167         limit = 100;
168         limit = params['limit'] if ((params['limit']) && (params['limit'].to_i < 10000) && (params['limit'].to_i > 0))
169         return limit
170   end
171
172   def closedCondition
173         closed_since = 7 unless params['closed']
174         closed_since = params['closed'].to_i if params['closed']
175         
176         if closed_since < 0
177           conditions = "status != 'hidden'"
178         elsif closed_since > 0
179           conditions = "((status = 'open') OR ((status = 'closed' ) AND (date_closed > '" + (Time.now - 7.days).to_s + "')))"
180         else
181           conditions = "status = 'open'"
182         end
183
184         return conditions
185   end
186
187   def add_comment(bug, comment, name) 
188     t = Time.now.getutc 
189     bug_comment = bug.map_bug_comment.create(:date_created => t, :visible => true, :comment => comment);  
190     if @user  
191       bug_comment.commenter_id = @user.id
192           bug_comment.commenter_name = @user.display_name
193     else  
194       bug_comment.commenter_ip = request.remote_ip
195           bug_comment.commenter_name = name + " (a)"
196     end
197     bug_comment.save; 
198     bug.last_changed = t 
199     bug.save 
200   end
201
202 end