Compare commits
5 Commits
| Author | SHA1 | Date |
|---|---|---|
|
|
d727991fe2 | |
|
|
ac3b22ef5b | |
|
|
97e0766168 | |
|
|
3bdba0772f | |
|
|
9cc4585c44 |
|
|
@ -1,20 +1,38 @@
|
||||||
class Admin::PropertyHiresController < OrbitAdminController
|
class Admin::PropertyHiresController < OrbitAdminController
|
||||||
include Admin::PropertyHiresHelper
|
include Admin::PropertyHiresHelper
|
||||||
|
|
||||||
def index
|
def index
|
||||||
|
if PropertyHireSortOrder.count == 0
|
||||||
|
PropertyHireSortOrder.create('sort_order' => false)
|
||||||
|
end
|
||||||
|
|
||||||
|
if !(params['order_asc'].nil?)
|
||||||
|
if params['order_asc'] == 'true'
|
||||||
|
PropertyHireSortOrder.first.update_attributes('sort_order' => true)
|
||||||
|
elsif params['order_asc'] == 'false'
|
||||||
|
PropertyHireSortOrder.first.update_attributes('sort_order' => false)
|
||||||
|
end
|
||||||
|
|
||||||
|
sort_direction = PropertyHireSortOrder.first['sort_order'] ? 1 : -1
|
||||||
|
Property.recalc_sort_number(sort_direction)
|
||||||
|
end
|
||||||
|
|
||||||
|
@choose = PropertyHireSortOrder.first['sort_order']
|
||||||
@tags = @module_app.tags
|
@tags = @module_app.tags
|
||||||
@categories = @module_app.categories.enabled
|
@categories = @module_app.categories.enabled
|
||||||
@filter_fields = filter_fields(@categories, @tags)
|
@filter_fields = filter_fields(@categories, @tags)
|
||||||
@table_fields = ["property_hire.title", :category, "property_hire.location", "property_hire.available_for_hire"]
|
@table_fields = ["property_hire.title", :category, "property_hire.location", "property_hire.available_for_hire"]
|
||||||
|
|
||||||
@properties = Property.order_by(order_position: :asc)
|
@properties = Property.order_by(tmp_sort_number: :asc)
|
||||||
.with_categories(filters("category"))
|
.with_categories(filters("category"))
|
||||||
.with_tags(filters("tag"))
|
.with_tags(filters("tag"))
|
||||||
|
|
||||||
@properties = search_data(@properties, [:title]).page(params[:page]).per(10)
|
@properties = search_data(@properties, [:title]).page(params[:page]).per(10)
|
||||||
|
|
||||||
if request.xhr?
|
if request.xhr?
|
||||||
render :partial => "index"
|
render :partial => "index"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def fields_display_order
|
def fields_display_order
|
||||||
uid = params[:id].split("-").last
|
uid = params[:id].split("-").last
|
||||||
@property = Property.find_by(:uid=>uid)
|
@property = Property.find_by(:uid=>uid)
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@ class PropertyHiresController < ApplicationController
|
||||||
include Admin::PropertyHiresHelper
|
include Admin::PropertyHiresHelper
|
||||||
FrontendMethods = ["hire", "view_calendar", "hire_success"]
|
FrontendMethods = ["hire", "view_calendar", "hire_success"]
|
||||||
def index
|
def index
|
||||||
properties = Property.can_display.filter_by_categories.order_by(order_position: :asc)
|
properties = Property.can_display.filter_by_categories.order_by(tmp_sort_number: :asc)
|
||||||
url = "/#{I18n.locale}#{OrbitHelper.page.get_url}"
|
url = "/#{I18n.locale}#{OrbitHelper.page.get_url}"
|
||||||
data = index_data(properties, url)
|
data = index_data(properties, url)
|
||||||
filterprops = data.dup
|
filterprops = data.dup
|
||||||
|
|
@ -275,14 +275,14 @@ class PropertyHiresController < ApplicationController
|
||||||
phire_id = booking_p[:id]
|
phire_id = booking_p[:id]
|
||||||
end
|
end
|
||||||
data = check_for_availability(start_time,end_time,booking_p[:property_id], booking_p[:recurring_interval], booking_p[:recurring_end_date], time_setting_id)
|
data = check_for_availability(start_time,end_time,booking_p[:property_id], booking_p[:recurring_interval], booking_p[:recurring_end_date], time_setting_id)
|
||||||
if data["success"] == true
|
if data["success"] == true || is_admin_page
|
||||||
error_messages = property.check_require_fields(booking_p)
|
error_messages = property.check_require_fields(booking_p)
|
||||||
if error_messages.present?
|
if error_messages.present?
|
||||||
data["msg"] = error_messages
|
data["msg"] = error_messages
|
||||||
data["success"] = false
|
data["success"] = false
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
if data["success"] == true
|
if data["success"] == true || is_admin_page
|
||||||
hire = nil
|
hire = nil
|
||||||
if phire_id
|
if phire_id
|
||||||
hire = PHire.find(phire_id) rescue nil
|
hire = PHire.find(phire_id) rescue nil
|
||||||
|
|
@ -364,6 +364,22 @@ class PropertyHiresController < ApplicationController
|
||||||
end
|
end
|
||||||
|
|
||||||
def check_availability
|
def check_availability
|
||||||
|
property = Property.find(params[:property_id])
|
||||||
|
|
||||||
|
if !check_if_user_is_manager?
|
||||||
|
# 1. 取得預約日期並歸零時間 (設為該日 00:00:00)
|
||||||
|
book_date = Time.zone.parse(params[:stime]).beginning_of_day
|
||||||
|
|
||||||
|
# 2. 取得「今天」的凌晨 00:00:00,再加 7 天
|
||||||
|
# 這樣判定會變成:只要是 7 天後的「那一天」就准許
|
||||||
|
limit_day = Time.zone.now.beginning_of_day + property.need_hire_before.send(property.need_hire_before_unit)
|
||||||
|
|
||||||
|
if book_date < limit_day
|
||||||
|
msg = "預約失敗:此項目最快僅能預約 #{limit_day.strftime('%Y-%m-%d')} 之後的時段。"
|
||||||
|
render json: { success: false, msg: msg }
|
||||||
|
return
|
||||||
|
end
|
||||||
|
end
|
||||||
time_setting_id = params[:time_setting_id]
|
time_setting_id = params[:time_setting_id]
|
||||||
data = check_for_availability params[:stime], params[:etime], params[:property_id], params[:interval], params[:recurring_end_date], time_setting_id
|
data = check_for_availability params[:stime], params[:etime], params[:property_id], params[:interval], params[:recurring_end_date], time_setting_id
|
||||||
render :json => data.to_json
|
render :json => data.to_json
|
||||||
|
|
@ -482,7 +498,11 @@ class PropertyHiresController < ApplicationController
|
||||||
start_wday = startt.wday
|
start_wday = startt.wday
|
||||||
start_date = startt.to_date
|
start_date = startt.to_date
|
||||||
@start_date = start_date
|
@start_date = start_date
|
||||||
|
if endt > endt.beginning_of_day
|
||||||
|
end_date = endt.to_date + 1.day
|
||||||
|
else
|
||||||
end_date = endt.to_date
|
end_date = endt.to_date
|
||||||
|
end
|
||||||
@end_date = end_date
|
@end_date = end_date
|
||||||
@first_check_time = startt.strftime("%H:%M")
|
@first_check_time = startt.strftime("%H:%M")
|
||||||
@all_day_settings = all_day_settings.sort_by { |k, _| k.to_i }.to_h
|
@all_day_settings = all_day_settings.sort_by { |k, _| k.to_i }.to_h
|
||||||
|
|
@ -513,7 +533,7 @@ class PropertyHiresController < ApplicationController
|
||||||
if i != -1
|
if i != -1
|
||||||
check_events = @need_check_events[0..i]
|
check_events = @need_check_events[0..i]
|
||||||
end
|
end
|
||||||
@need_check_events = @need_check_events[(i+1)..-1]
|
#@need_check_events = @need_check_events[(i+1)..-1]
|
||||||
settings = @all_day_settings[wday_str]
|
settings = @all_day_settings[wday_str]
|
||||||
|
|
||||||
if check_events.count != 0
|
if check_events.count != 0
|
||||||
|
|
|
||||||
|
|
@ -110,7 +110,7 @@ module Admin::PropertyHiresHelper
|
||||||
when "datetime"
|
when "datetime"
|
||||||
print_format = print_format.gsub("{#{field_name}}", phire.send(field_name).strftime("%Y/%m/%d %H:%M")) rescue print_format.gsub("{#{field_name}}", "")
|
print_format = print_format.gsub("{#{field_name}}", phire.send(field_name).strftime("%Y/%m/%d %H:%M")) rescue print_format.gsub("{#{field_name}}", "")
|
||||||
else
|
else
|
||||||
print_format = print_format.gsub("{#{field_name}}", phire.send(field_name).to_s) rescue print_format.gsub("{#{field_name}}", "")
|
print_format = print_format.gsub("{#{field_name}}", phire.send(field_name).to_s).gsub(/\r\n|\r|\n/, "<br />") rescue print_format.gsub("{#{field_name}}", "")
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
field = PHireField.where(:key => field_name, :property_id => phire.property_id).first
|
field = PHireField.where(:key => field_name, :property_id => phire.property_id).first
|
||||||
|
|
|
||||||
|
|
@ -895,4 +895,42 @@ class Property
|
||||||
end
|
end
|
||||||
emails.select{|email| email.present?}
|
emails.select{|email| email.present?}
|
||||||
end
|
end
|
||||||
|
def self.check_sort_number
|
||||||
|
ph_sort_order = PropertyHireSortOrder.first
|
||||||
|
if ph_sort_order && ph_sort_order.need_update_sort
|
||||||
|
self.recalc_sort_number(ph_sort_order.sort_order)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.recalc_sort_number(sort_order=nil)
|
||||||
|
if sort_order.nil?
|
||||||
|
sort_order = (PropertyHireSortOrder.first['sort_order'] rescue false) ? 1 : -1
|
||||||
|
end
|
||||||
|
sorted_hires = self.all.to_a.sort_by{|k| [
|
||||||
|
(k["is_top"] ? 0 : 1),
|
||||||
|
(k["order_position"].nil? ? Float::INFINITY : sort_order * k["order_position"].to_i),
|
||||||
|
-k["created_at"].to_i
|
||||||
|
]}
|
||||||
|
|
||||||
|
sorted_hires.each_with_index do |a, i|
|
||||||
|
a.instance_variable_set(:@skip_callback, true)
|
||||||
|
self.where(:id => a.id).update_all(:tmp_sort_number => i)
|
||||||
|
end
|
||||||
|
|
||||||
|
min_pos, max_pos = (sorted_hires.count != 0) ? (sorted_hires.map{|a| a.order_position.to_i}.minmax) : [0, 0]
|
||||||
|
PropertyHireSortOrder.update_all(
|
||||||
|
:min_sort_number => min_pos,
|
||||||
|
:max_sort_number => max_pos,
|
||||||
|
:need_update_sort => false
|
||||||
|
)
|
||||||
|
|
||||||
|
self.create_indexes
|
||||||
|
return max_pos
|
||||||
|
end
|
||||||
|
after_create :trigger_recalc
|
||||||
|
|
||||||
|
def trigger_recalc
|
||||||
|
order_status = PropertyHireSortOrder.first.try(:sort_order)
|
||||||
|
self.class.recalc_sort_number(order_status ? 1 : -1)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,53 @@
|
||||||
|
class PropertyHireSortOrder
|
||||||
|
include Mongoid::Document
|
||||||
|
field :sort_order, :type => Boolean
|
||||||
|
field :need_update_sort, :type => Boolean, :default => true
|
||||||
|
field :min_sort_number, :type => Integer, :default => 0
|
||||||
|
field :max_sort_number, :type => Integer, :default => 0
|
||||||
|
|
||||||
|
after_initialize do |record|
|
||||||
|
if(!record.new_record? && record.sort_order.nil?)
|
||||||
|
record.sort_order = true
|
||||||
|
record.save
|
||||||
|
elsif(!record.new_record?)
|
||||||
|
@module_app = ModuleApp.where(:key=>'property_hire').first
|
||||||
|
if !@module_app.nil?
|
||||||
|
@asc = @module_app.asc rescue nil
|
||||||
|
if !@asc.nil?
|
||||||
|
record.sort_order = @asc
|
||||||
|
record.save
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
after_save do |record|
|
||||||
|
unless record.instance_variable_get(:@skip_callback)
|
||||||
|
if record.sort_order_changed?
|
||||||
|
@module_app = ModuleApp.where(:key=>'property_hire').first
|
||||||
|
if !@module_app.nil?
|
||||||
|
@sort_order = record.sort_order rescue nil
|
||||||
|
if !@sort_order.nil?
|
||||||
|
if !@module_app.attributes[:asc].nil?
|
||||||
|
@module_app.asc = @sort_order
|
||||||
|
@module_app.save
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
record.instance_variable_set(:@skip_callback, true)
|
||||||
|
record.update(:need_update_sort=>true)
|
||||||
|
if record.need_update_sort
|
||||||
|
Thread.new do
|
||||||
|
sort_order = record.sort_order ? 1 : -1
|
||||||
|
PHire.recalc_sort_number(sort_order)
|
||||||
|
record.instance_variable_set(:@skip_callback, false)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def get_default_order
|
||||||
|
self.sort_order ? self.min_sort_number : self.max_sort_number
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
@ -4,7 +4,7 @@
|
||||||
%>
|
%>
|
||||||
<div class="attributes-header clearfix">
|
<div class="attributes-header clearfix">
|
||||||
<div class="toggle-control" style="float: right;">
|
<div class="toggle-control" style="float: right;">
|
||||||
<div class="togglebox <%= attribute_field.disabled ? 'disabled' : ''%>">
|
<div class="togglebox <%= attribute_field.disabled ? 'disable' : ''%>">
|
||||||
<%= hidden_field "#{@field_name}[p_hire_fields][#{@af_counter}]","disabled",:value=>attribute_field.disabled,:class=>"toggle-check", :data=>{:deploy=>"right"} %>
|
<%= hidden_field "#{@field_name}[p_hire_fields][#{@af_counter}]","disabled",:value=>attribute_field.disabled,:class=>"toggle-check", :data=>{:deploy=>"right"} %>
|
||||||
<label><b></b></label>
|
<label><b></b></label>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
||||||
|
|
@ -691,6 +691,7 @@
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
</div>
|
||||||
<!-- print settings -->
|
<!-- print settings -->
|
||||||
<div class="tab-pane fade" id="printsettings">
|
<div class="tab-pane fade" id="printsettings">
|
||||||
<table>
|
<table>
|
||||||
|
|
@ -706,7 +707,6 @@
|
||||||
</tbody>
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
|
||||||
<!-- Language Tabs -->
|
<!-- Language Tabs -->
|
||||||
<div class="nav-name"><strong><%= t(:language) %></strong></div>
|
<div class="nav-name"><strong><%= t(:language) %></strong></div>
|
||||||
<ul class="nav nav-pills language-nav">
|
<ul class="nav nav-pills language-nav">
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,18 @@
|
||||||
|
<script>
|
||||||
|
function change_order(){
|
||||||
|
var value = $("form[name='order_form']").find("input[type='checkbox']")[0].checked
|
||||||
|
$("input[name='order_asc']").val(String(value))
|
||||||
|
document.order_form.submit()
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
<form method="get" action='?' name='order_form'>
|
||||||
|
<input type='hidden' name='order_asc' value=''>
|
||||||
|
<nobr>
|
||||||
|
<%= t('archive.choose_order')+':' %>
|
||||||
|
<% if @choose %>
|
||||||
|
<input type="checkbox" onchange='change_order()' checked><span><%= t('archive.yes') %></span>
|
||||||
|
<% else %>
|
||||||
|
<input type="checkbox" onchange='change_order()'><span><%= t('archive.no') %></span>
|
||||||
|
<% end %>
|
||||||
|
</nobr>
|
||||||
|
</form>
|
||||||
|
|
@ -23,7 +23,7 @@
|
||||||
<fieldset>
|
<fieldset>
|
||||||
|
|
||||||
<div id="attributes-area" class="input-area">
|
<div id="attributes-area" class="input-area">
|
||||||
<%= render partial: "attribute_field",collection: @attribute.p_hire_fields_enabled.asc(:_id)%>
|
<%= render partial: "attribute_field",collection: @attribute.p_hire_fields.asc(:_id)%>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="form-actions">
|
<div class="form-actions">
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
<%= render_filter @filter_fields, "index_table" %>
|
<%= render_filter @filter_fields, "index_table" %>
|
||||||
|
<%= render 'sort_order_choose' %>
|
||||||
<span id="index_table">
|
<span id="index_table">
|
||||||
<%= render 'index'%>
|
<%= render 'index'%>
|
||||||
</span>
|
</span>
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue