space/app/controllers/admin/spaces_controller.rb

224 lines
5.9 KiB
Ruby

class Admin::SpacesController < OrbitAdminController
def index
@buildings = Building.all
end
def new
@building = Building.new
end
def edit
uid = params[:id].split("-").last
@building = Building.find_by(:uid => uid) rescue nil
end
def create
building = Building.new(building_params)
building.save
redirect_to admin_spaces_path
end
def destroy
uid = params[:id].split("-").last
building = Building.find_by(:uid => uid) rescue nil
building.destroy if !building.nil?
redirect_to admin_spaces_path
end
def update
uid = params[:id].split("-").last
building = Building.find_by(:uid => uid) rescue nil
if !building.nil?
building.update_attributes(building_params)
building.save
end
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
old_img = floor.layout_image.url
floor.update_attributes(floor_params)
img = floor.layout_image.url
floor.layout = floor.layout.sub(old_img ,img) if !floor.layout.nil?
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?
old_img = floor_unit.layout_image.url
floor_unit.update_attributes(floor_unit_params)
img = floor_unit.layout_image.url
floor_unit.layout = floor_unit.layout.sub(old_img ,img) if !floor_unit.layout.nil?
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 destroy_sub_unit
sub_unit = FloorSubUnit.find_by(:uid => params[:sub_unit_id].split("-").last) rescue nil
sub_unit.destroy if !sub_unit.nil?
redirect_to "/admin/spaces/#{sub_unit.floor_unit.floor.to_param}/#{sub_unit.floor_unit.to_param}/sub_units"
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