+
+ # Counts and selects pages of GPX traces for various criteria (by user, tags, public etc.).
+ # target_user - if set, specifies the user to fetch traces for. if not set will fetch all traces
+ def list(target_user = nil, action = "list")
+ # from display name, pick up user id if one user's traces only
+ display_name = params[:display_name]
+ if target_user.nil? and !display_name.blank?
+ target_user = User.find(:first, :conditions => [ "display_name = ?", display_name])
+ end
+
+ # set title
+ if target_user.nil?
+ @title = "Public GPS traces"
+ elsif @user and @user.id == target_user.id
+ @title = "Your GPS traces"
+ else
+ @title = "Public GPS traces from #{target_user.display_name}"
+ end
+
+ @title += " tagged with #{params[:tag]}" if params[:tag]
+
+ # four main cases:
+ # 1 - all traces, logged in = all public traces + all user's (i.e + all mine)
+ # 2 - all traces, not logged in = all public traces
+ # 3 - user's traces, logged in as same user = all user's traces
+ # 4 - user's traces, not logged in as that user = all user's public traces
+ if target_user.nil? # all traces
+ if @user
+ conditions = ["(gpx_files.public = 1 OR gpx_files.user_id = ?)", @user.id] #1
+ else
+ conditions = ["gpx_files.public = 1"] #2
+ end
+ else
+ if @user and @user.id == target_user.id
+ conditions = ["gpx_files.user_id = ?", @user.id] #3 (check vs user id, so no join + can't pick up non-public traces by changing name)
+ else
+ conditions = ["gpx_files.public = 1 AND gpx_files.user_id = ?", target_user.id] #4
+ end
+ end
+
+ if params[:tag]
+ @tag = params[:tag]
+ conditions[0] += " AND EXISTS (SELECT * FROM gpx_file_tags AS gft WHERE gft.gpx_id = gpx_files.id AND gft.tag = ?)"
+ conditions << @tag
+ end
+
+ conditions[0] += " AND gpx_files.visible = 1"
+
+ @trace_pages, @traces = paginate(:traces,
+ :include => [:user, :tags],
+ :conditions => conditions,
+ :order => "gpx_files.timestamp DESC",
+ :per_page => 20)