]> git.openstreetmap.org Git - rails.git/blob - app/controllers/trace_controller.rb
c5552f39d07f44b2359338852049a3743727102a
[rails.git] / app / controllers / trace_controller.rb
1 class TraceController < ApplicationController
2   before_filter :authorize_web  
3   before_filter :authorize, :only => [:api_details, :api_data, :api_create]
4   layout 'site'
5  
6   # Counts and selects pages of GPX traces for various criteria (by user, tags, public etc.).
7   #  target_user - if set, specifies the user to fetch traces for.  if not set will fetch all traces
8   #  paging_action - the action that will be linked back to from view
9   def list (target_user = nil, paging_action = 'list')
10     @title = 'public GPS traces'
11     @title += " tagged with #{params[:tag]}" if params[:tag]
12     @traces_per_page = 20
13     page_index = params[:page] ? params[:page].to_i - 1 : 0 # nice 1-based page -> 0-based page index
14
15     # from display name, pick up user id if one user's traces only
16     display_name = params[:display_name]
17     if target_user.nil? and display_name and display_name != ''
18       @paging_action = 'view'
19       @display_name = display_name
20       @title += " from #{@display_name}"
21       target_user = User.find(:first, :conditions => [ "display_name = ?", display_name])
22     end
23
24     opt = Hash.new
25     opt[:include] = [:user, :tags] # load users and tags from db at same time as traces
26
27     # four main cases:
28     # 1 - all traces, logged in = all public traces + all user's (i.e + all mine)
29     # 2 - all traces, not logged in = all public traces
30     # 3 - user's traces, logged in as same user = all user's traces 
31     # 4 - user's traces, not logged in as that user = all user's public traces
32     if target_user.nil? # all traces
33       if @user
34         conditions = ["(public = 1 OR user_id = ?)", @user.id] #1
35       else
36         conditions  = ["public = 1"] #2
37       end
38     else
39       if @user and @user.id == target_user.id
40         conditions = ["user_id = ?", @user.id] #3 (check vs user id, so no join + can't pick up non-public traces by changing name)
41       else
42         conditions = ["public = 1 AND user_id = ?", target_user.id] #4
43       end
44     end
45     conditions[0] += " AND users.display_name != ''" # users need to set display name before traces will be exposed
46     
47     opt[:order] = 'timestamp DESC'
48     if params[:tag]
49       @tag = params[:tag]
50       conditions[0] += " AND gpx_file_tags.tag = ?"
51       conditions << @tag;
52     end
53     
54     opt[:conditions] = conditions
55
56     # count traces using all options except limit
57     @max_trace = Trace.count(opt)
58     @max_page = Integer((@max_trace + 1) / @traces_per_page) 
59     
60     # last step before fetch - add paging options
61     opt[:limit] = @traces_per_page
62     if page_index > 0
63       opt[:offset] = @traces_per_page * page_index
64     end
65
66     @traces = Trace.find(:all , opt)
67     
68     # put together SET of tags across traces, for related links
69     tagset = Hash.new
70     if @traces
71       @traces.each do |trace|
72         trace.tags.reload if params[:tag] # if searched by tag, ActiveRecord won't bring back other tags, so do explicitly here
73         trace.tags.each do |tag|
74           tagset[tag.tag] = tag.tag
75         end
76       end
77     end
78     
79     # final helper vars for view
80     @display_name = display_name
81     @all_tags = tagset.values
82     @paging_action = paging_action # the action that paging requests should route back to, e.g. 'list' or 'mine'
83     @page = page_index + 1 # nice 1-based external page numbers
84   end
85
86   def mine
87     if @user
88       list(@user, 'mine') unless @user.nil?
89     else
90       redirect_to :controller => 'user', :action => 'login', :referer => request.request_uri
91     end
92   end
93
94   def view
95     @trace = Trace.find(params[:id])
96     unless @trace.public
97       if @user
98         render :nothing, :status => 401 if @trace.user.id != @user.id
99       end
100     end
101   end
102
103   def create
104     filename = "/tmp/#{rand}"
105
106     File.open(filename, "w") { |f| f.write(params[:trace][:gpx_file].read) }
107     params[:trace][:name] = params[:trace][:gpx_file].original_filename.gsub(/[^a-zA-Z0-9.]/, '_') # This makes sure filenames are sane
108     params[:trace].delete('gpx_file') # remove the field from the hash, because there's no such field in the DB
109     @trace = Trace.new(params[:trace])
110     @trace.inserted = false
111     @trace.user = @user
112     @trace.timestamp = Time.now
113
114     if @trace.save
115       saved_filename = "/home/osm/gpx/#{@trace.id}.gpx"
116       File.rename(filename, saved_filename)
117
118       logger.info("id is #{@trace.id}")
119       flash[:notice] = "Your GPX file has been uploaded and is awaiting insertion in to the database. This will usually happen within half an hour, and an email will be sent to you on completion."
120       redirect_to :action => 'mine'
121     end
122   end
123
124   def data
125     trace = Trace.find(params[:id])
126     if trace and (trace.public? or (@user and @user == trace.user))
127       send_file(trace.trace_name, :filename => "#{trace.id}.gpx", :type => trace.mime_type, :disposition => 'attachment')
128     else
129       render :nothing, :status => 404
130     end
131   end
132
133   def make_public
134     trace = Trace.find(params[:id])
135     if @user and trace.user == @user and !trace.public
136       trace.public = true
137       trace.save
138       flash[:notice] = 'Track made public'
139       redirect_to :controller => 'trace', :action => 'view', :id => params[:id]
140     end
141   end
142
143   def georss
144     traces = Trace.find(:all, :conditions => ['public = true'], :order => 'timestamp DESC', :limit => 20)
145
146     rss = OSM::GeoRSS.new
147
148     #def add(latitude=0, longitude=0, title_text='dummy title', url='http://www.example.com/', description_text='dummy description', timestamp=Time.now)
149     traces.each do |trace|
150       rss.add(trace.latitude, trace.longitude, trace.name, url_for({:controller => 'trace', :action => 'view', :id => trace.id, :display_name => trace.user.display_name}), "<img src='#{url_for({:controller => 'trace', :action => 'icon', :id => trace.id, :user_login => trace.user.display_name})}'> GPX file with #{trace.size} points from #{trace.user.display_name}", trace.timestamp)
151     end
152
153     response.headers["Content-Type"] = 'application/rss+xml'
154
155     render :text => rss.to_s
156   end
157
158   def picture
159     trace = Trace.find(params[:id])
160     if trace and (trace.public? or (@user and @user == trace.user))
161       send_file(trace.large_picture_name, :filename => "#{trace.id}.gif", :type => 'image/gif', :disposition => 'inline')
162     else
163       render :nothing, :status => 404
164     end
165   end
166
167   def icon
168     trace = Trace.find(params[:id])
169     if trace and (trace.public? or (@user and @user == trace.user))
170       send_file(trace.icon_picture_name, :filename => "#{trace.id}_icon.gif", :type => 'image/gif', :disposition => 'inline')
171     else
172       render :nothing, :status => 404
173     end
174   end
175
176   def api_details
177     trace = Trace.find(params[:id])
178     doc = OSM::API.new.get_xml_doc
179     doc.root << trace.to_xml_node() if trace.public? or trace.user == @user
180     render :text => doc.to_s
181   end
182
183   def api_data
184     render :action => 'data'
185   end
186
187   def api_create
188     #FIXME merge this code with create as they're pretty similar?
189     
190     filename = "/tmp/#{rand}"
191     File.open(filename, "w") { |f| f.write(request.raw_post) }
192     params[:trace] = {}
193     params[:trace][:name] = params[:filename]
194     params[:trace][:tagstring] = params[:tags]
195     params[:trace][:description] = params[:description]
196     @trace = Trace.new(params[:trace])
197     @trace.inserted = false
198     @trace.user = @user
199     @trace.timestamp = Time.now
200
201     if @trace.save
202       saved_filename = "/home/osm/gpx/#{@trace.id}.gpx"
203       File.rename(filename, saved_filename)
204       logger.info("id is #{@trace.id}")
205       flash[:notice] = "Your GPX file has been uploaded and is awaiting insertion in to the database. This will usually happen within half an hour, and an email will be sent to you on completion."
206       render :nothing => true
207     else
208       render :nothing => true, :status => 400 # er FIXME what fricking code to return?
209     end
210
211   end
212 end