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(',')
|
|
|
|
@all.each do |item|
|
|
|
|
item = item.split('/')
|
|
|
|
@mapping = Mapping.new()
|
|
|
|
@mapping.category = "Item"
|
|
|
|
@mapping.user = @user
|
|
|
|
@mapping.map = @map
|
|
|
|
@mapping.item = Item.find(item[0])
|
|
|
|
@mapping.xloc = item[1]
|
|
|
|
@mapping.yloc = item[2]
|
|
|
|
@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
|
|
|
|
2012-12-21 23:07:13 +00:00
|
|
|
@outitems = @map.items.order("name ASC").delete_if{|item| not item.authorize_to_view(@current)}
|
2012-10-26 10:04:52 +00:00
|
|
|
|
2012-12-21 23:07:13 +00:00
|
|
|
respond_with(@user, @map, @outitems)
|
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
|
|
|
if params[:outitems]
|
|
|
|
@outitems = params[:outitems]
|
|
|
|
@outitems.each do |item|
|
|
|
|
@mapping = Mapping.where("map_id = ? AND item_id = ?", @map.id, item).first
|
|
|
|
@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(',')
|
|
|
|
@all.each do |item|
|
|
|
|
item = item.split('/')
|
|
|
|
@mapping = Mapping.find(item[0])
|
|
|
|
@mapping.xloc = item[1]
|
|
|
|
@mapping.yloc = item[2]
|
|
|
|
@mapping.save
|
|
|
|
end
|
|
|
|
@map.arranged = true
|
|
|
|
@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
|
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
|