space/app/controllers/admin/spaces_controller.rb

190 lines
4.8 KiB
Ruby

class Admin::SpacesController < OrbitAdminController
def index
@buildings = Building.all
end
def new
@building = Building.new
end
def create
building = Building.new(building_params)
building.save
redirect_to admin_spaces_path
end
def floors
uid = params[:space_id].split("-").last
@building = Building.find_by(:uid => uid) rescue nil
@table_fields = [:title, :action]
end
def add_floor
building = Building.find(params[:building_id])
floor = Floor.new(floor_params)
floor.building = building
floor.save
redirect_to "/admin/spaces/#{building.to_param}/floors"
end
def update_floor
floor = Floor.find(params[:floor_id]) rescue nil
floor.update_attributes(floor_params)
floor.save
redirect_to "/admin/spaces/#{floor.building.to_param}/floors"
end
def floor_layout
@floor = Floor.find_by(:uid => params[:floor_id].split("-").last)
end
def delete_floor
floor = Floor.find_by(:uid => params[:floor_id].split("-").last) rescue nil
floor.destroy if !floor.nil?
render :json => {"success" => true}.to_json
end
def add_floor_unit
@floor_unit = FloorUnit.new
@floor = Floor.find_by(:uid => params[:floor_id].split("-").last)
end
def create_floor_unit
floor_unit = FloorUnit.new(floor_unit_params)
floor_unit.save
respond_to do |format|
format.html {redirect_to "/admin/spaces/#{floor_unit.floor.building.to_param}/#{floor_unit.floor.to_param}/units"}
format.js {render :json => {"success" => true, "unit" => {"id" => floor_unit.id.to_s, "title" => floor_unit.title}}.to_json}
end
end
def edit_floor_unit
@floor_unit = FloorUnit.find_by(:uid => params[:unit_id].split("-").last) rescue nil
@floor = @floor_unit.floor
end
def update_floor_unit
floor_unit = FloorUnit.find(params[:unit_id]) rescue nil
if !floor_unit.nil?
floor_unit.update_attributes(floor_unit_params)
floor_unit.save
end
redirect_to "/admin/spaces/#{floor_unit.floor.building.to_param}/#{floor_unit.floor.to_param}/units"
end
def save_floor_layout
floor = Floor.find(params[:floor_id]) rescue nil
if !floor.nil?
floor.layout = params[:layout_html]
floor.save
end
render :json => {"success" => true}.to_json
end
def delete_floor_unit
floor_unit = FloorUnit.find_by(:uid => params[:unit_id].split("-").last) rescue nil
floor = floor_unit.floor
if !floor_unit.nil?
floor_unit.destroy
end
redirect_to "/admin/spaces/#{floor.building.to_param}/#{floor.to_param}/units"
end
def units
@floor = Floor.find_by(:uid => params[:floor_id].split("-").last)
end
def sub_units
@floor_unit = FloorUnit.find_by(:uid => params[:unit_id].split("-").last)
end
def add_sub_unit
@floor_unit = FloorUnit.find_by(:uid => params[:unit_id].split("-").last) rescue nil
@sub_unit = FloorSubUnit.new
end
def edit_sub_unit
@sub_unit = FloorSubUnit.find_by(:uid => params[:sub_unit_id].split("-").last) rescue nil
end
def update_sub_unit
sub_unit = FloorSubUnit.find(params[:floor_sub_unit_id]) rescue nil
if !sub_unit.nil?
p = sub_unit_params
p[:floor_sub_unit_images].concat(sub_unit.floor_sub_unit_images) if p[:floor_sub_unit_images].present?
sub_unit.update_attributes(p)
sub_unit.save
end
if params[:images_to_destroy].present?
params[:images_to_destroy].each do |id|
image = FloorSubUnitImage.find(id) rescue nil
image.destroy if !image.nil?
end
end
redirect_to "/admin/spaces/#{sub_unit.floor_unit.floor.to_param}/#{sub_unit.floor_unit.to_param}/sub_units"
end
def upload_sub_unit_image
image = FloorSubUnitImage.new(image_params)
image.save
render :json => {"success" => true,"id" => image.id.to_s}.to_json
end
def create_sub_unit
sub_unit = FloorSubUnit.new(sub_unit_params)
sub_unit.save
respond_to do |format|
format.html {redirect_to "/admin/spaces/#{sub_unit.floor_unit.floor.to_param}/#{sub_unit.floor_unit.to_param}/sub_units"}
format.js {render :json => {"success" => true, "unit" => {"id" => sub_unit.id.to_s, "title" => sub_unit.title}}.to_json}
end
end
def unit_layout
@floor_unit = FloorUnit.find_by(:uid => params[:unit_id].split("-").last) rescue nil
end
def save_unit_layout
floor_unit = FloorUnit.find(params[:unit_id]) rescue nil
if !floor_unit.nil?
floor_unit.layout = params[:layout_html]
floor_unit.save
end
render :json => {"success" => true}.to_json
end
private
def sub_unit_params
p = params.require(:floor_sub_unit).permit!
if p[:floor_sub_unit_images].present?
images = []
p[:floor_sub_unit_images].each do |id|
image = FloorSubUnitImage.find(id) rescue nil
images << image if !image.nil?
end
p[:floor_sub_unit_images] = images
end
p
end
def image_params
params.require(:floor_sub_unit_image).permit!
end
def floor_params
params.require(:floor).permit!
end
def building_params
params.require(:building).permit!
end
def floor_unit_params
params.require(:floor_unit).permit!
end
end