]> git.openstreetmap.org Git - rails.git/blob - app/controllers/map_bugs_controller.rb
6224a8727cbc02928478b8466488051cb586938e
[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]
5   before_filter :check_api_writable, :only => [:add_bug, :close_bug, :edit_bug]
6   after_filter :compress_output
7   around_filter :api_call_handle_error, :api_call_timeout
8
9   # Help methods for checking boundary sanity and area size
10   include MapBoundary
11
12   def get_bugs
13
14         raise OSM::APIBadUserInput.new("No l was given") unless params['l']
15         raise OSM::APIBadUserInput.new("No r was given") unless params['r']
16         raise OSM::APIBadUserInput.new("No b was given") unless params['b']
17         raise OSM::APIBadUserInput.new("No t was given") unless params['t']
18
19         min_lon = params['l'].to_f
20         max_lon = params['r'].to_f
21         min_lat = params['b'].to_f
22         max_lat = params['t'].to_f
23
24         limit = 100;
25         limit = params['limit'] if ((params['limit']) && (params['limit'].to_i < 10000) && (params['limit'].to_i > 0))
26
27         
28         # check boundary is sane and area within defined
29     # see /config/application.yml
30     begin
31       check_boundaries(min_lon, min_lat, max_lon, max_lat)
32     rescue Exception => err
33       report_error(err.message)
34       return
35     end
36
37         @bugs = MapBug.find_by_area(min_lat, min_lon, max_lat, max_lon, :order => "last_changed DESC", :limit => limit, :conditions => "status != 'hidden'")
38
39         respond_to do |format|
40           format.html {render :template => 'map_bugs/get_bugs.js', :content_type => "text/javascript"}
41           format.rss {render :template => 'map_bugs/get_bugs.rss'}
42           format.js
43           format.xml {render :template => 'map_bugs/get_bugs.xml'}
44           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]}}) }      
45           #format.gpx {render :text => "Rendering GPX"}
46         end
47   end
48
49   def add_bug
50         raise OSM::APIBadUserInput.new("No lat was given") unless params['lat']
51         raise OSM::APIBadUserInput.new("No lon was given") unless params['lon']
52         raise OSM::APIBadUserInput.new("No text was given") unless params['text']
53
54         lon = params['lon'].to_f
55         lat = params['lat'].to_f
56         comment = params['text']
57
58         name = "NoName";
59         name = params['name'] if params['name'];
60
61     bug = MapBug.create_bug(lat, lon)
62         bug.save;
63         add_comment(bug, comment, name);
64  
65         render_ok
66   end
67
68   def edit_bug
69         raise OSM::APIBadUserInput.new("No id was given") unless params['id']
70         raise OSM::APIBadUserInput.new("No text was given") unless params['text']
71
72         name = "NoName";
73         name = params['name'] if params['name'];
74         
75         id = params['id'].to_i
76
77         bug = MapBug.find_by_id(id);
78
79         bug_comment = add_comment(bug, params['text'], name);
80
81         render_ok
82   end
83
84   def close_bug
85         raise OSM::APIBadUserInput.new("No id was given") unless params['id']
86         
87         id = params['id'].to_i
88
89         bug = MapBug.find_by_id(id);
90         bug.close_bug;
91
92         render_ok
93   end 
94
95   def render_ok
96         output_js = :false
97         output_js = :true if params['format'] == "js"
98
99         if output_js == :true
100           render :text => "osbResponse();", :content_type => "text/javascript" 
101         else
102           render :text => "ok\n", :content_type => "text/html" 
103         end
104         
105   end
106
107   def rss
108         request.format = :rss
109         get_bugs
110   end
111
112   def gpx_bugs
113         request.format = :xml
114         get_bugs
115   end
116
117   def add_comment(bug, comment, name) 
118     t = Time.now.getutc 
119     bug_comment = bug.map_bug_comment.create(:date_created => t, :visible => true, :comment => comment);  
120     if @user  
121       bug_comment.commenter_id = @user.id
122           bug_comment.commenter_name = @user.display_name
123     else  
124       bug_comment.commenter_ip = request.remote_ip
125           bug_comment.commenter_name = name + " (a)"
126     end
127     bug_comment.save; 
128     bug.last_changed = t 
129     bug.save 
130   end
131
132 end