property_hire/app/controllers/admin/property_hires_controller.rb

322 lines
11 KiB
Ruby
Raw Normal View History

2017-01-20 09:02:50 +00:00
class Admin::PropertyHiresController < OrbitAdminController
2020-03-23 17:26:52 +00:00
include Admin::PropertyHiresHelper
2017-01-20 09:02:50 +00:00
def index
@tags = @module_app.tags
@categories = @module_app.categories.enabled
@filter_fields = filter_fields(@categories, @tags)
@table_fields = ["property_hire.title", :category, "property_hire.location", "property_hire.available_for_hire"]
@properties = Property.order_by(sort)
2017-01-20 09:02:50 +00:00
.with_categories(filters("category"))
.with_tags(filters("tag"))
@properties = search_data(@properties,[:title]).page(params[:page]).per(10)
2022-11-24 09:31:30 +00:00
if request.xhr?
render :partial => "index"
end
2017-01-20 09:02:50 +00:00
end
2021-09-19 07:56:01 +00:00
def fields_display_order
uid = params[:id].split("-").last
@property = Property.find_by(:uid=>uid)
end
def update_fields_display_order
uid = params[:id].split("-").last
@property = Property.find_by(:uid=>uid)
prop = params.require(:property).permit!
@property.update_attributes(prop)
redirect_to params[:referer_url]
end
2021-09-06 16:45:14 +00:00
def order
@properties = Property.all.sort_order
end
def updateorder
ids_with_order = params[:order]
ids_with_order.each_with_index do |id,idx|
property = Property.find(id) rescue nil
if !property.nil?
property.order_position = idx
property.save
end
end
render :json => {"success" => true}.to_json
end
def my_bookings
2020-05-28 09:30:03 +00:00
@table_fields = ["property_hire.title","property_hire.hiring_person_name", "property_hire.reason_for_hire", "property_hire.hiring_person_number", "property_hire.period", "property_hire.passed", :actions]
@bookings = PHire.where(:hiring_person_id => current_user.member_profile.id.to_s).desc(:created_at).page(params[:page]).per(10)
end
2024-05-14 00:59:52 +00:00
def copy
@property = Property.where(:uid => params[:id].split("-").last).first.clone_new #rescue nil
create_set (false)
@locations = PropertyLocation.all.desc(:created_at).collect{|loc| [loc.title, loc.id.to_s]}
@locations << ["Other", "other_location"]
end
2017-01-20 09:02:50 +00:00
def new
@property = Property.new
2020-03-23 17:26:52 +00:00
create_set (false)
2017-01-20 09:02:50 +00:00
@locations = PropertyLocation.all.desc(:created_at).collect{|loc| [loc.title, loc.id.to_s]}
@locations << ["Other", "other_location"]
end
def edit_hire
@phire = PHire.find(params[:id])
end
def update_hire
@phire = PHire.find(params[:id])
@phire.update_attributes(phire_params)
redirect_to admin_property_hire_path(:id=>@phire.property)
end
2017-01-20 09:02:50 +00:00
def edit
@property = Property.where(:uid => params[:id].split("-").last).first rescue nil
2020-03-23 17:26:52 +00:00
create_set (true)
2017-01-20 09:02:50 +00:00
@locations = PropertyLocation.all.desc(:created_at).collect{|loc| [loc.title, loc.id.to_s]}
@locations << ["Other", "other_location"]
end
def show
@table_fields = ["property_hire.hiring_person_name", "property_hire.reason_for_hire", "property_hire.hiring_person_number", "property_hire.period", "property_hire.passed", :actions]
@property = Property.where(:uid => params[:id].split("-").last).first rescue nil
@bookings = @property.p_hires.desc(:created_at).page(params[:page]).per(10)
end
def destroy
property = Property.find(params[:id]) rescue nil
2023-12-21 13:06:40 +00:00
email = MemberProfile.where(:id.in=>property.owners).collect{|v| v.email}
2020-05-20 12:14:02 +00:00
Admin::PropertyHiresHelper::HireMethod.send_mail('delete',email,property.id,nil,nil,current_user.id)
2017-01-20 09:02:50 +00:00
property.destroy if !property.nil?
if params[:page]
redirect_to admin_property_hires_path(:page => params[:page])
else
redirect_to admin_property_hires_path
end
end
def update
property = Property.where(:uid => params[:id].split("-").last).first rescue nil
redirect_to admin_property_hires_path and return if property.nil?
2021-09-06 16:45:14 +00:00
@property_params = property_params
if @property_params[:p_hire_fields]
@property_params[:p_hire_fields].each do |a|
@field_name = 'property'
field_status = a.last[:id].present?
@attribute_field = PHireField.add_p_hire_field(property, a.last, a.last[:id], field_status)
@attribute = property
end
if property.need_change_tmp_reason
Thread.new do
p_hire_field_ids = property.p_hire_fields.where(:display_in_reason_for_hire=>true).pluck(:id)
property.p_hires.each do |p_hire|
p_hire.tmp_reason_for_hire = p_hire.p_hire_field_values.where(:p_hire_field_id.in=>p_hire_field_ids).map{|v| v.get_value_by_locale(I18n.locale.to_s)}.join(" ")
p_hire.save
end
property.update(:need_change_tmp_reason => false)
end
end
2021-09-06 16:45:14 +00:00
flash.now[:notice] = "Updated Fields"
property.p_hire_fields.each{|t| t.destroy if t["to_delete"] == true}
2017-01-20 09:02:50 +00:00
else
2021-09-06 16:45:14 +00:00
property.update_attributes(@property_params)
2023-12-21 13:06:40 +00:00
email = MemberProfile.where(:id.in=>property.owners).collect{|v| v.email}
2021-09-06 16:45:14 +00:00
Admin::PropertyHiresHelper::HireMethod.send_mail('edit',email,property.id,nil,nil,current_user.id)
2017-01-20 09:02:50 +00:00
end
2021-09-19 07:56:01 +00:00
redirect_to params[:referer_url]
2017-01-20 09:02:50 +00:00
end
def create
2024-05-14 00:59:52 +00:00
tmp_property_params = property_params
if tmp_property_params["copy_id"] && tmp_property_params["clone_p_hires"].blank?
tmp_property_params["except_clone_relations"] = ["p_hires","p_hire_field_values"]
end
if Property.create(tmp_property_params)
2017-01-20 09:02:50 +00:00
redirect_to admin_property_hires_path
end
end
def show_booking_details
@booking = PHire.find(params[:id]) rescue nil
end
def settings
2023-03-07 13:40:01 +00:00
@settings = PropertyHireSetting.first
if request.request_method == "PATCH"
2021-10-23 02:34:18 +00:00
@settings.update_attributes(settings_params.except("property_day_settings"))
@settings.save
2021-10-23 02:34:18 +00:00
if settings_params["property_day_settings"]
property_day_settings = settings_params["property_day_settings"].values
property_day_settings.each do |setting|
setting["property_id"] = "default_settings"
s = nil
if setting["id"].present?
s = PropertyDaySetting.where(:id=>setting["id"]).first
end
if s
if setting["_destroy"] == "1"
s.destroy
else
s.update_attributes(setting.except("id","_destroy"))
end
else
s = PropertyDaySetting.create(setting.except("id","_destroy"))
end
end
end
@saved = true
else
@saved = false
end
end
2018-01-17 07:53:58 +00:00
def delete_booking_details
booking = PHire.find(params[:id])
property = booking.property
booking.destroy
if request.referrer.split("/").last == "my_bookings"
redirect_to my_bookings_admin_property_hires_path
else
redirect_to admin_property_hire_path(property)
end
2018-01-17 07:53:58 +00:00
end
2017-01-20 09:02:50 +00:00
def pass_booking
phire = PHire.find(params[:id]) rescue nil
case params[:status]
when "accept"
phire.passed = true
when "reject"
phire.passed = false
end
phire.save
2021-10-22 14:11:06 +00:00
email = [phire.hiring_person_email].select{|e| e.present?}
2021-10-22 14:20:20 +00:00
Admin::PropertyHiresHelper::HireMethod.send_mail('p_hire',email,phire.property.id,nil,phire.id,(current_user.id rescue nil))
2017-01-20 09:02:50 +00:00
if params[:ref] == "index"
if params[:page]
redirect_to admin_property_hire_path(phire.property, :page => params[:page])
else
redirect_to admin_property_hire_path(phire.property)
end
else
redirect_to show_booking_details_admin_property_hire_path(phire)
end
end
def manage_locations
@table_fields = ["property_hire.location", "property_hire.property_count"]
@locations = PropertyLocation.all.desc(:created_at).page(params[:page]).per(10)
end
def add_location
@location = PropertyLocation.new
render :layout => false
end
def create_location
PropertyLocation.create(location_params)
redirect_to manage_locations_admin_property_hires_path
end
def edit_location
@location = PropertyLocation.find(params[:id])
render :layout => false
end
def update_location
location = PropertyLocation.find(params[:id]) rescue nil
if location.nil?
redirect_to manage_locations_admin_property_hires_path
else
location.update_attributes(location_params)
if params[:page]
redirect_to manage_locations_admin_property_hires_path(:page => params[:page])
else
redirect_to manage_locations_admin_property_hires_path
end
end
end
def destroy_location
location = PropertyLocation.find(params[:id]) rescue nil
if location.nil?
redirect_to manage_locations_admin_property_hires_path
else
location.destroy
if params[:page]
redirect_to manage_locations_admin_property_hires_path(:page => params[:page])
else
redirect_to manage_locations_admin_property_hires_path
end
end
end
2021-09-06 16:45:14 +00:00
def custom_fields
@field_name = 'property'
uid = params[:id].split("-").last
@attribute = Property.find_by(:uid=>uid)
@attribute_type = 'property'
@class = 'properties'
end
2017-01-20 09:02:50 +00:00
private
def settings_params
params.require(:property_hire_setting).permit!
end
2017-01-20 09:02:50 +00:00
def location_params
params.require(:property_location).permit!
end
def phire_params
2023-04-22 07:39:05 +00:00
p_hire_params = params.require(:p_hire).permit!
property = (@phire ? @phire.property : Property.find(params[:p_hire][:property_id]) rescue nil)
if(property.enable_notes_selector rescue false)
note_texts = ""
property.notes_selector.each do |index,sub_hash|
name = sub_hash["name"][I18n.locale.to_s]
name = sub_hash["name"].values.select{|v| v.present?}.first.to_s if name.blank?
values = sub_hash["value"][I18n.locale.to_s]
values = sub_hash["value"].values.select{|v| v.present?}.first.to_s if values.blank?
value_text = p_hire_params["notes_selector"][index.to_s].to_a.map{|i| values[i.to_i]}.join(",") rescue ""
value_text = I18n.t("property_hire.none") if value_text.blank?
note_texts += (name + ":"+value_text)
note_texts += "<br>".html_safe
end
p_hire_params["note_for_hire"] = note_texts
p_hire_params.delete("notes_selector")
end
return p_hire_params
end
2017-01-20 09:02:50 +00:00
def property_params
prop = params.require(:property).permit!
2021-09-06 16:45:14 +00:00
if prop[:weekdays].nil?
prop[:weekdays] = []
end
2023-08-15 13:52:43 +00:00
if prop[:owners].nil?
prop[:owners] = nil
end
2017-01-20 09:02:50 +00:00
prop.delete(:property_location) if prop[:property_location] == "other"
2020-07-22 10:13:50 +00:00
notes_selector = prop["notes_selector"]
2020-07-26 07:54:19 +00:00
notes_selector = {} if notes_selector.nil?
2020-07-22 10:13:50 +00:00
prop["notes_selector"] = (0 ... notes_selector.keys.count).to_a.map{|k| k.to_s}.zip(notes_selector.values).to_h
return prop
2017-01-20 09:02:50 +00:00
end
2020-03-23 17:26:52 +00:00
def create_set (save_flag)
@email_set = []
['p_hire','edit','delete'].each do |field_name|
email_set = @property.hire_email_sets.select{|v| v.field_name==field_name}
if email_set.length==0
title = Hash.new
content = Hash.new
I18n.available_locales.each do |locale|
I18n.with_locale(locale) do
title[locale] = t("property_hire.email_#{field_name}_success")
content[locale] = t("property_hire.email_#{field_name}_content")
end
end
if save_flag
email_set = @property.hire_email_sets.create(field_name:field_name,title:title,content:content)
else
email_set = @property.hire_email_sets.new(field_name:field_name,title:title,content:content)
end
else
email_set = email_set[0]
end
@email_set << email_set
end
end
2017-01-20 09:02:50 +00:00
end