1 class MapBugsController < ApplicationController
 
   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
 
  10   # Help methods for checking boundary sanity and area size
 
  18     if bbox and bbox.count(',') == 3
 
  19       bbox = bbox.split(',')
 
  20           min_lon, min_lat, max_lon, max_lat = sanitise_boundaries(bbox)
 
  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']
 
  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
 
  34         conditions = closedCondition
 
  36         # check boundary is sane and area within defined
 
  37     # see /config/application.yml
 
  39       check_boundaries(min_lon, min_lat, max_lon, max_lat)
 
  40     rescue Exception => err
 
  41       report_error(err.message)
 
  47         @bugs = MapBug.find_by_area(min_lat, min_lon, max_lat, max_lon, :order => "last_changed DESC", :limit => limit, :conditions => conditions)
 
  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'}
 
  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'}
 
  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']
 
  64         lon = params['lon'].to_f
 
  65         lat = params['lat'].to_f
 
  66         comment = params['text']
 
  69         name = params['name'] if params['name'];
 
  71     @bug = MapBug.create_bug(lat, lon)
 
  73         add_comment(@bug, comment, name);
 
  79         raise OSM::APIBadUserInput.new("No id was given") unless params['id']
 
  80         raise OSM::APIBadUserInput.new("No text was given") unless params['text']
 
  83         name = params['name'] if params['name'];
 
  85         id = params['id'].to_i
 
  87         bug = MapBug.find_by_id(id);
 
  89         bug_comment = add_comment(bug, params['text'], name);
 
  95         raise OSM::APIBadUserInput.new("No id was given") unless params['id']
 
  97         id = params['id'].to_i
 
  99         bug = MapBug.find_by_id(id);
 
 107         request.format = :rss
 
 112         request.format = :xml
 
 117         @bug = MapBug.find(params['id'])
 
 118     render :text => "", :status => :gone unless @bug.visible
 
 119         respond_to do |format|
 
 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]}}) }       
 
 127         bug = MapBug.find(params['id'])
 
 128         bug.status = "hidden"
 
 130         render :text => "ok\n", :content_type => "text/html" 
 
 134         raise OSM::APIBadUserInput.new("No query string was given") unless params['q']
 
 136         conditions = closedCondition
 
 138         #TODO: There should be a better way to do this.   CloseConditions are ignored at the moment
 
 140         bugs2 = MapBug.find(:all, :limit => limit, :order => "last_changed DESC", :joins => :map_bug_comment,
 
 141                                                 :conditions => ['map_bug_comment.comment ~ ?', params['q']])
 
 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'}
 
 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'}
 
 156         output_js = :true if params['format'] == "js"
 
 158         if output_js == :true
 
 159           render :text => "osbResponse();", :content_type => "text/javascript" 
 
 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
 
 168         limit = params['limit'] if ((params['limit']) && (params['limit'].to_i < 10000) && (params['limit'].to_i > 0))
 
 173         closed_since = 7 unless params['closed']
 
 174         closed_since = params['closed'].to_i if params['closed']
 
 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 + "')))"
 
 181           conditions = "status = 'open'"
 
 187   def add_comment(bug, comment, name) 
 
 189     bug_comment = bug.map_bug_comment.create(:date_created => t, :visible => true, :comment => comment);  
 
 191       bug_comment.commenter_id = @user.id
 
 192           bug_comment.commenter_name = @user.display_name
 
 194       bug_comment.commenter_ip = request.remote_ip
 
 195           bug_comment.commenter_name = name + " (a)"