2012-10-26 10:04:52 +00:00
|
|
|
class MapsController < ApplicationController
|
2012-10-27 08:30:56 +00:00
|
|
|
|
2013-01-18 22:08:06 +00:00
|
|
|
before_filter :require_user, only: [:new, :create, :edit, :update, :savelayout, :destroy]
|
2012-10-26 10:04:52 +00:00
|
|
|
|
|
|
|
respond_to :html, :js, :json
|
|
|
|
|
2012-12-16 20:00:43 +00:00
|
|
|
autocomplete :map, :name, :full => true, :extra_data => [:user_id]
|
|
|
|
|
2014-02-10 04:36:23 +00:00
|
|
|
# GET /maps/recent
|
|
|
|
# GET /maps/featured
|
|
|
|
# GET /maps/new
|
|
|
|
# GET /maps/mappers/:id
|
2012-10-26 10:04:52 +00:00
|
|
|
def index
|
2012-12-21 23:07:13 +00:00
|
|
|
|
2014-02-10 04:36:23 +00:00
|
|
|
if request.path == "/maps"
|
|
|
|
redirect_to activemaps_url and return
|
|
|
|
end
|
|
|
|
|
2012-12-21 23:07:13 +00:00
|
|
|
@current = current_user
|
2014-02-10 04:36:23 +00:00
|
|
|
@user = nil
|
2012-12-21 23:07:13 +00:00
|
|
|
|
2014-02-10 04:36:23 +00:00
|
|
|
if request.path =="/maps/active"
|
2014-02-21 01:22:13 +00:00
|
|
|
@maps = Map.order("updated_at DESC").limit(20)
|
2014-02-10 04:36:23 +00:00
|
|
|
@request = "active"
|
|
|
|
|
|
|
|
elsif request.path =="/maps/featured"
|
|
|
|
@maps = Map.order("name ASC").find_all_by_featured(true)
|
|
|
|
@request = "featured"
|
|
|
|
|
2014-02-21 01:22:13 +00:00
|
|
|
elsif request.path == "/maps/new"
|
|
|
|
@maps = Map.order("created_at DESC").limit(20)
|
2014-02-10 04:36:23 +00:00
|
|
|
@request = "new"
|
|
|
|
|
2014-02-21 01:22:13 +00:00
|
|
|
elsif request.path.index('/maps/mappers/') != nil # looking for maps by a mapper
|
2014-02-10 04:36:23 +00:00
|
|
|
@user = User.find(params[:id])
|
2014-02-21 01:22:13 +00:00
|
|
|
@maps = Map.order("name ASC").find_all_by_user_id(@user.id)
|
2014-02-10 04:36:23 +00:00
|
|
|
@request = "you" if authenticated? && @user == @current
|
|
|
|
@request = "other" if authenticated? && @user != @current
|
|
|
|
|
2014-02-21 01:22:13 +00:00
|
|
|
elsif request.path.index('/maps/topics/') != nil # looking for maps by a certain topic they include
|
|
|
|
@topic = Topic.find(params[:id]).authorize_to_show(@current)
|
|
|
|
if !@topic
|
|
|
|
redirect_to featuredmaps_url, notice: "Access denied." and return
|
|
|
|
end
|
|
|
|
@maps = @topic.maps
|
|
|
|
@request = "topic"
|
2012-12-21 23:07:13 +00:00
|
|
|
end
|
|
|
|
|
2014-02-21 01:22:13 +00:00
|
|
|
#read this next line as 'delete a map if its private and you're either 1. logged out or 2. logged in but not the map creator
|
|
|
|
@maps.delete_if {|m| m.permission == "private" && (!authenticated? || (authenticated? && @current.id != m.user_id)) }
|
|
|
|
|
2014-02-10 04:36:23 +00:00
|
|
|
respond_with(@maps, @request, @user)
|
2012-10-26 10:04:52 +00:00
|
|
|
end
|
|
|
|
|
2012-12-21 23:07:13 +00:00
|
|
|
# GET maps/new
|
2012-10-26 10:04:52 +00:00
|
|
|
def new
|
|
|
|
@map = Map.new
|
|
|
|
@user = current_user
|
|
|
|
|
|
|
|
respond_with(@map)
|
|
|
|
end
|
|
|
|
|
2012-12-21 23:07:13 +00:00
|
|
|
# GET maps/:id
|
2012-10-26 10:04:52 +00:00
|
|
|
def show
|
2014-07-08 01:02:43 +00:00
|
|
|
|
|
|
|
@current = current_user
|
2012-12-21 23:07:13 +00:00
|
|
|
@map = Map.find(params[:id]).authorize_to_show(@current)
|
2013-02-19 14:44:43 +00:00
|
|
|
|
|
|
|
if not @map
|
|
|
|
redirect_to root_url and return
|
|
|
|
end
|
2014-07-27 19:57:35 +00:00
|
|
|
|
2013-02-19 14:44:43 +00:00
|
|
|
@mapjson = @map.self_as_json(@current).html_safe
|
2014-07-27 19:57:35 +00:00
|
|
|
|
|
|
|
@alltopics = @map.topics # should limit to topics visible to user
|
|
|
|
@allsynapses = @map.synapses # should also be limited
|
|
|
|
@allmappings = @map.mappings
|
|
|
|
@allmetacodes = Metacode.all
|
|
|
|
|
2013-02-19 14:44:43 +00:00
|
|
|
respond_to do |format|
|
2014-07-27 19:57:35 +00:00
|
|
|
format.html { respond_with(@allmetacodes, @allmappings, @allsynapses, @alltopics, @map, @user) }
|
|
|
|
#format.json { respond_with(@mapjson) }
|
|
|
|
format.json { render json: @topics }
|
2013-02-19 14:44:43 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# GET maps/:id/embed
|
|
|
|
def embed
|
|
|
|
|
|
|
|
@current = current_user
|
|
|
|
@map = Map.find(params[:id]).authorize_to_show(@current)
|
2012-10-26 10:04:52 +00:00
|
|
|
|
2012-12-21 23:07:13 +00:00
|
|
|
if not @map
|
|
|
|
redirect_to root_url and return
|
|
|
|
end
|
2012-10-27 08:30:56 +00:00
|
|
|
|
2012-12-21 23:07:13 +00:00
|
|
|
@mapjson = @map.self_as_json(@current).html_safe
|
2012-10-26 10:04:52 +00:00
|
|
|
|
2012-12-21 23:07:13 +00:00
|
|
|
respond_to do |format|
|
2012-10-26 10:04:52 +00:00
|
|
|
format.html { respond_with(@map, @user) }
|
|
|
|
format.json { respond_with(@mapjson) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-01-18 22:08:06 +00:00
|
|
|
# GET maps/:id/json
|
|
|
|
def json
|
|
|
|
|
|
|
|
@current = current_user
|
|
|
|
@map = Map.find(params[:id]).authorize_to_show(@current)
|
|
|
|
|
|
|
|
if not @map
|
|
|
|
redirect_to root_url and return
|
|
|
|
end
|
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
format.json { render :json => @map.self_as_json(@current) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-12-21 23:07:13 +00:00
|
|
|
# POST maps
|
2012-10-26 10:04:52 +00:00
|
|
|
def create
|
|
|
|
|
|
|
|
@user = current_user
|
2013-01-08 03:04:53 +00:00
|
|
|
@map = Map.new()
|
2012-12-25 23:29:20 +00:00
|
|
|
@map.name = params[:map][:name]
|
|
|
|
@map.desc = params[:map][:desc]
|
|
|
|
@map.permission = params[:map][:permission]
|
|
|
|
@map.user = @user
|
|
|
|
@map.arranged = false
|
2014-01-31 00:32:15 +00:00
|
|
|
@map.save
|
|
|
|
|
|
|
|
#this variable specifies to the js file whether it's a brand new map or a forked one
|
|
|
|
@forked = false
|
2012-12-25 23:29:20 +00:00
|
|
|
|
|
|
|
if params[:map][:topicsToMap]
|
2013-01-08 03:04:53 +00:00
|
|
|
@all = params[:map][:topicsToMap]
|
|
|
|
@all = @all.split(',')
|
|
|
|
@all.each do |topic|
|
|
|
|
topic = topic.split('/')
|
|
|
|
@mapping = Mapping.new()
|
2013-01-01 22:45:35 +00:00
|
|
|
@mapping.category = "Topic"
|
2012-12-25 23:29:20 +00:00
|
|
|
@mapping.user = @user
|
|
|
|
@mapping.map = @map
|
2013-01-01 22:45:35 +00:00
|
|
|
@mapping.topic = Topic.find(topic[0])
|
|
|
|
@mapping.xloc = topic[1]
|
|
|
|
@mapping.yloc = topic[2]
|
2012-12-25 23:29:20 +00:00
|
|
|
@mapping.save
|
2013-01-08 03:04:53 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
if params[:map][:synapsesToMap]
|
|
|
|
@synAll = params[:map][:synapsesToMap]
|
|
|
|
@synAll = @synAll.split(',')
|
2013-01-08 04:03:29 +00:00
|
|
|
@synAll.each do |synapse_id|
|
2013-01-08 03:04:53 +00:00
|
|
|
@mapping = Mapping.new()
|
|
|
|
@mapping.category = "Synapse"
|
|
|
|
@mapping.user = @user
|
|
|
|
@mapping.map = @map
|
2013-01-08 04:03:29 +00:00
|
|
|
@mapping.synapse = Synapse.find(synapse_id)
|
2013-01-08 03:04:53 +00:00
|
|
|
@mapping.save
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@map.arranged = true
|
2012-12-25 23:29:20 +00:00
|
|
|
@map.save
|
2014-01-31 00:32:15 +00:00
|
|
|
|
|
|
|
@forked = true
|
|
|
|
end
|
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
format.js { respond_with(@map, @forked) }
|
|
|
|
end
|
2012-10-26 10:04:52 +00:00
|
|
|
end
|
|
|
|
|
2012-12-21 23:07:13 +00:00
|
|
|
# GET maps/:id/edit
|
2012-10-26 10:04:52 +00:00
|
|
|
def edit
|
2012-12-21 23:07:13 +00:00
|
|
|
@current = current_user
|
|
|
|
@map = Map.find(params[:id]).authorize_to_edit(@current)
|
2012-10-27 08:30:56 +00:00
|
|
|
|
2012-12-21 23:07:13 +00:00
|
|
|
if not @map
|
|
|
|
redirect_to root_url and return
|
|
|
|
end
|
2012-10-30 03:40:38 +00:00
|
|
|
|
2013-01-01 22:45:35 +00:00
|
|
|
@outtopics = @map.topics.order("name ASC").delete_if{|topic| not topic.authorize_to_view(@current)}
|
2012-10-26 10:04:52 +00:00
|
|
|
|
2013-01-01 22:45:35 +00:00
|
|
|
respond_with(@user, @map, @outtopics)
|
2012-10-26 10:04:52 +00:00
|
|
|
end
|
|
|
|
|
2012-12-21 23:07:13 +00:00
|
|
|
# PUT maps/:id
|
2012-10-26 10:04:52 +00:00
|
|
|
def update
|
2013-01-18 22:08:06 +00:00
|
|
|
@current = current_user
|
|
|
|
@map = Map.find(params[:id]).authorize_to_edit(@current)
|
2012-10-26 10:04:52 +00:00
|
|
|
|
2013-01-18 22:08:06 +00:00
|
|
|
if @map
|
|
|
|
if params[:map]
|
|
|
|
@map.name = params[:map][:name] if params[:map][:name]
|
|
|
|
@map.desc = params[:map][:desc] if params[:map][:desc]
|
|
|
|
@map.permission = params[:map][:permission] if params[:map][:permission]
|
|
|
|
end
|
|
|
|
@map.save
|
2012-10-26 10:04:52 +00:00
|
|
|
end
|
2013-01-18 22:08:06 +00:00
|
|
|
|
|
|
|
respond_with @map
|
2012-10-26 10:04:52 +00:00
|
|
|
end
|
|
|
|
|
2012-12-21 23:07:13 +00:00
|
|
|
# PUT maps/:id/savelayout
|
2012-12-01 23:05:17 +00:00
|
|
|
def savelayout
|
2013-04-26 04:07:29 +00:00
|
|
|
@user = current_user
|
2013-02-15 03:36:08 +00:00
|
|
|
@map = Map.find(params[:id])
|
|
|
|
|
|
|
|
if params[:map][:coordinates]
|
|
|
|
@all = params[:map][:coordinates]
|
|
|
|
@all = @all.split(',')
|
|
|
|
@all.each do |topic|
|
|
|
|
topic = topic.split('/')
|
|
|
|
@mapping = Mapping.find(topic[0])
|
|
|
|
if @mapping
|
2013-01-04 21:51:37 +00:00
|
|
|
@mapping.xloc = topic[1]
|
2013-02-15 03:36:08 +00:00
|
|
|
@mapping.yloc = topic[2]
|
|
|
|
@mapping.save
|
2013-04-26 04:07:29 +00:00
|
|
|
|
|
|
|
#push realtime update for location on map
|
2014-02-04 17:08:09 +00:00
|
|
|
@mapping.message 'update',@user.id
|
2013-01-04 21:51:37 +00:00
|
|
|
end
|
2013-02-15 03:36:08 +00:00
|
|
|
end
|
|
|
|
@map.arranged = true
|
2014-02-26 20:23:29 +00:00
|
|
|
@map.touch(:updated_at)
|
2013-02-15 03:36:08 +00:00
|
|
|
@map.save
|
|
|
|
end
|
2012-12-01 23:05:17 +00:00
|
|
|
end
|
|
|
|
|
2012-12-25 23:29:20 +00:00
|
|
|
# DELETE maps/:id
|
2012-10-26 10:04:52 +00:00
|
|
|
def destroy
|
2014-02-21 01:22:13 +00:00
|
|
|
@current = current_user
|
|
|
|
|
2012-12-21 23:07:13 +00:00
|
|
|
@map = Map.find(params[:id])
|
2012-10-26 10:04:52 +00:00
|
|
|
|
2012-12-21 23:07:13 +00:00
|
|
|
@mappings = @map.mappings
|
2012-10-26 10:04:52 +00:00
|
|
|
|
2012-12-21 23:07:13 +00:00
|
|
|
@mappings.each do |mapping|
|
|
|
|
mapping.delete
|
|
|
|
end
|
2012-10-26 10:04:52 +00:00
|
|
|
|
2012-12-21 23:07:13 +00:00
|
|
|
@map.delete
|
2012-10-26 10:04:52 +00:00
|
|
|
|
2012-12-21 23:07:13 +00:00
|
|
|
respond_to do |format|
|
2014-02-26 20:00:18 +00:00
|
|
|
format.html { redirect_to "/maps/mappers/" + @current.id.to_s, notice: "Map deleted." }
|
2012-10-26 10:04:52 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|