2012-10-26 10:04:52 +00:00
|
|
|
class MapsController < ApplicationController
|
2012-10-27 08:30:56 +00:00
|
|
|
|
2012-12-01 23:05:17 +00:00
|
|
|
before_filter :require_user, only: [:new, :create, :edit, :update, :savelayout]
|
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]
|
|
|
|
|
2012-12-21 23:07:13 +00:00
|
|
|
# GET /maps
|
|
|
|
# or GET /users/:user_id/maps
|
2012-10-26 10:04:52 +00:00
|
|
|
def index
|
2012-12-21 23:07:13 +00:00
|
|
|
|
|
|
|
@current = current_user
|
|
|
|
|
|
|
|
if params[:user_id]
|
|
|
|
@user = User.find(params[:user_id])
|
2012-12-25 23:29:20 +00:00
|
|
|
@maps = Map.order("name ASC").visibleToUser(@current, @user)
|
2012-12-21 23:07:13 +00:00
|
|
|
elsif
|
2012-12-25 23:29:20 +00:00
|
|
|
@maps = Map.order("name ASC").visibleToUser(@current, nil)
|
2012-12-21 23:07:13 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
respond_with(@maps,@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
|
|
|
|
|
2012-12-21 23:07:13 +00:00
|
|
|
@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
|
|
|
|
|
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
|
|
|
|
@map.save
|
|
|
|
|
|
|
|
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(',')
|
|
|
|
@synAll.each do |synapse|
|
|
|
|
@mapping = Mapping.new()
|
|
|
|
@mapping.category = "Synapse"
|
|
|
|
@mapping.user = @user
|
|
|
|
@mapping.map = @map
|
|
|
|
@mapping.synapse = Synapse.find(synapse[0])
|
|
|
|
@mapping.save
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@map.arranged = true
|
2012-12-25 23:29:20 +00:00
|
|
|
@map.save
|
|
|
|
respond_to do |format|
|
|
|
|
format.js { respond_with(@map) }
|
|
|
|
end
|
|
|
|
else
|
|
|
|
respond_to do |format|
|
|
|
|
format.html { respond_with(@user, location: map_path(@map)) }
|
|
|
|
end
|
2013-01-08 03:04:53 +00:00
|
|
|
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
|
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
|
|
|
@map.attributes = params[:map]
|
|
|
|
@map.save
|
2012-10-26 10:04:52 +00:00
|
|
|
|
2012-12-21 23:07:13 +00:00
|
|
|
respond_with(@user, location: map_path(@map)) do |format|
|
2012-10-26 10:04:52 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-12-21 23:07:13 +00:00
|
|
|
# PUT maps/:id/savelayout
|
2012-12-01 23:05:17 +00:00
|
|
|
def savelayout
|
2012-12-21 23:07:13 +00:00
|
|
|
@map = Map.find(params[:id])
|
2012-12-01 23:05:17 +00:00
|
|
|
|
2012-12-21 23:07:13 +00:00
|
|
|
if params[:map][:coordinates]
|
|
|
|
@all = params[:map][:coordinates]
|
|
|
|
@all = @all.split(',')
|
2013-01-01 22:45:35 +00:00
|
|
|
@all.each do |topic|
|
|
|
|
topic = topic.split('/')
|
|
|
|
@mapping = Mapping.find(topic[0])
|
2013-01-04 21:51:37 +00:00
|
|
|
if @mapping
|
|
|
|
@mapping.xloc = topic[1]
|
|
|
|
@mapping.yloc = topic[2]
|
|
|
|
@mapping.save
|
|
|
|
end
|
2012-12-21 23:07:13 +00:00
|
|
|
end
|
|
|
|
@map.arranged = true
|
|
|
|
@map.save
|
|
|
|
end
|
2012-12-01 23:05:17 +00:00
|
|
|
end
|
|
|
|
|
2013-01-04 05:20:43 +00:00
|
|
|
# GET maps/:id/realtime
|
|
|
|
def realtime
|
|
|
|
@current = current_user
|
|
|
|
@map = Map.find(params[:id])
|
|
|
|
|
2013-01-04 21:51:37 +00:00
|
|
|
@time = params[:map][:time]
|
2013-01-05 22:50:40 +00:00
|
|
|
@time = @time.to_i - 10
|
2013-01-04 21:51:37 +00:00
|
|
|
|
|
|
|
@topics = Array.new()
|
|
|
|
@synapses = Array.new()
|
|
|
|
@mappings = Array.new()
|
|
|
|
# add code for finding deleted topics and sending the ids of those back to the client here
|
|
|
|
@topics = @map.topics.select{|t| t.updated_at.to_i > @time}
|
|
|
|
@synapses = @map.synapses.select{|t| t.updated_at.to_i > @time}
|
|
|
|
@mappings = @map.mappings.select{|t| t.updated_at.to_i > @time && t.category == "Topic"}
|
2013-01-04 05:20:43 +00:00
|
|
|
|
|
|
|
respond_to do |format|
|
2013-01-04 21:51:37 +00:00
|
|
|
format.js { respond_with(@map,@topics,@synapses,@mappings) }
|
2013-01-04 05:20:43 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-12-25 23:29:20 +00:00
|
|
|
# DELETE maps/:id
|
2012-10-26 10:04:52 +00:00
|
|
|
def destroy
|
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|
|
|
|
|
format.js
|
2012-10-26 10:04:52 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|