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
|
2012-12-25 23:29:20 +00:00
|
|
|
@map = Map.new()
|
|
|
|
@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]
|
|
|
|
@all = params[:map][:topicsToMap]
|
|
|
|
@all = @all.split(',')
|
2013-01-01 22:45:35 +00:00
|
|
|
@all.each do |topic|
|
|
|
|
topic = topic.split('/')
|
2012-12-25 23:29:20 +00:00
|
|
|
@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
|
|
|
|
end
|
|
|
|
@map.arranged = true
|
|
|
|
@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
|
|
|
|
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
|
|
|
|
2013-01-01 22:45:35 +00:00
|
|
|
if params[:outtopics]
|
|
|
|
@outtopics = params[:outtopics]
|
|
|
|
@outtopics.each do |topic|
|
|
|
|
@mapping = Mapping.where("map_id = ? AND topic_id = ?", @map.id, topic).first
|
2012-12-21 23:07:13 +00:00
|
|
|
@mapping.delete
|
|
|
|
end
|
|
|
|
end
|
2012-10-30 03:40:38 +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])
|
|
|
|
@mapping.xloc = topic[1]
|
|
|
|
@mapping.yloc = topic[2]
|
2012-12-21 23:07:13 +00:00
|
|
|
@mapping.save
|
|
|
|
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])
|
|
|
|
|
|
|
|
@mapjson = @map.self_as_json(@current).html_safe
|
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
format.js { respond_with(@mapjson) }
|
|
|
|
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
|