]> git.openstreetmap.org Git - rails.git/blob - app/controllers/map_bugs_controller.rb
Split comment field out of map bugs table
[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         # check boundary is sane and area within defined
25     # see /config/application.yml
26     begin
27       check_boundaries(min_lon, min_lat, max_lon, max_lat)
28     rescue Exception => err
29       report_error(err.message)
30       return
31     end
32
33         bugs = MapBug.find_by_area(min_lat, min_lon, max_lat, max_lon, :order => "last_changed DESC", :limit => 100, :conditions => "status != 'hidden'")
34
35         resp = ""
36         
37         bugs.each do |bug|
38           resp += "putAJAXMarker(" + bug.id.to_s + ", " + bug.lon.to_s + ", " + bug.lat.to_s;
39           comment_no = 1
40           bug.map_bug_comment.each do |comment|
41         resp += (comment_no == 1 ? ", '" : "<hr />")
42                 resp += comment.comment if comment.comment
43                 resp += " [ " 
44                 resp += comment.commenter_name if comment.commenter_name
45                 resp += " " + comment.date_created.to_s + " ]"
46                 comment_no += 1
47           end
48           resp += (comment_no == 1 ? "," : "', ") + (bug.status=="open"?"0":"1") + ");\n"
49         end
50
51         render :text => resp, :content_type => "text/javascript"
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     bug = MapBug.create_bug(lat, lon)
64         bug.save;
65         add_comment(bug, comment);
66  
67         render_ok
68   end
69
70   def edit_bug
71         raise OSM::APIBadUserInput.new("No id was given") unless params['id']
72         raise OSM::APIBadUserInput.new("No text was given") unless params['text']
73
74         id = params['id'].to_i
75
76         bug = MapBug.find_by_id(id);
77
78         bug_comment = add_comment(bug, params['text']);
79
80         render_ok
81   end
82
83   def close_bug
84         raise OSM::APIBadUserInput.new("No id was given") unless params['id']
85         
86         id = params['id'].to_i
87
88         bug = MapBug.find_by_id(id);
89         bug.close_bug;
90
91         render_ok
92   end 
93
94   def render_ok
95         output_js = :false
96         output_js = :true if params['format'] == "js"
97
98         if output_js == :true
99           render :text => "osbResponse();", :content_type => "text/javascript" 
100         else
101           render :text => "ok\n", :content_type => "text/html" 
102         end
103         
104   end
105
106   def rss
107         ##TODO: needs to be implemented
108   end
109
110   def gpx_bugs
111         ##TODO: needs to be implemented
112   end
113
114   def add_comment(bug, comment) 
115     t = Time.now.getutc 
116     bug_comment = bug.map_bug_comment.create(:date_created => t, :visible => true, :comment => comment);  
117     if @user  
118       bug_comment.commenter_id = @user.id
119           bug_comment.commenter_name = @user.display_name
120     else  
121       bug_comment.commenter_ip = request.remote_ip
122           bug_comment.commenter_name = "anonymous (a)"
123     end
124     bug_comment.save; 
125     bug.last_changed = t 
126     bug.save 
127   end
128
129 end