diff --git a/app/views/common/_calendar.html.erb b/app/views/common/_calendar.html.erb index 778a5c2..037f9eb 100644 --- a/app/views/common/_calendar.html.erb +++ b/app/views/common/_calendar.html.erb @@ -1,7 +1,7 @@ <%= javascript_include_tag("calendar", plugin: :redmine_qbo_calendar) %> <% - # Toggles: kept in the session by CalendarsControllerPatch, so month links keep them + # Toggles: each user's own, kept in their preferences by CalendarsControllerPatch (always in params here) only_appointments = params[:only_appointments] == '1' hide_past_weeks = params[:hide_past_weeks] == '1' diff --git a/app/views/customer_appointments/_form.html.erb b/app/views/customer_appointments/_form.html.erb index 48979be..69b5626 100644 --- a/app/views/customer_appointments/_form.html.erb +++ b/app/views/customer_appointments/_form.html.erb @@ -24,8 +24,12 @@

<%= f.text_field :customer, label: :label_customer, class: "customer-name", autocomplete: "off", data: { autocomplete_url: "/customers/autocomplete" } %> <%= f.hidden_field :customer_id, id: "customer_id" %> - - + <%# Creates the customer in a dialog without leaving the appointment (customer-modal.js), for users who + may add customers %> + <% if User.current.allowed_to?(:add_customers, nil, global: true) %> + <%= link_to sprite_icon('add', l(:label_new_customer)), '#', id: 'inline-create-customer-btn', + class: 'icon icon-add', title: l(:label_new_customer) %> + <% end %>

diff --git a/lib/redmine_qbo_calendar/patches/calendars_controller_patch.rb b/lib/redmine_qbo_calendar/patches/calendars_controller_patch.rb index e4f4b14..8845c5d 100644 --- a/lib/redmine_qbo_calendar/patches/calendars_controller_patch.rb +++ b/lib/redmine_qbo_calendar/patches/calendars_controller_patch.rb @@ -24,14 +24,14 @@ module RedmineQboCalendar params[:month] ||= session[:qbo_calendar_month] end - # The "Hide past weeks" and "Show customer appointments only" toggles stick for the session, so - # month links (which don't carry them) keep them without a second page load - %w[only_appointments hide_past_weeks].each do |name| - key = :"qbo_calendar_#{name}" + # The "Hide past weeks" and "Show customer appointments only" toggles are each user's own setting, + # kept in their preferences (the session for anonymous users), so they stick across logins, + # browsers and month links (which don't carry them) + CALENDAR_TOGGLES.each do |name, default| if params.key?(name) - session[key] = params[name] == '1' - elsif session[key] - params[name] = '1' + save_calendar_toggle(name, params[name] == '1') + else + params[name] = calendar_toggle(name, default) ? '1' : '0' end end @@ -84,8 +84,34 @@ module RedmineQboCalendar @calendar.events = events end + # Toggle => its value until the user changes it: customer appointments only, past weeks shown + CALENDAR_TOGGLES = { 'only_appointments' => true, 'hide_past_weeks' => false }.freeze + private + def calendar_toggle_key(name) + :"qbo_calendar_#{name}" + end + + def calendar_toggle(name, default) + key = calendar_toggle_key(name) + stored = User.current.logged? ? User.current.pref[key] : session[key] + stored.nil? ? default : ActiveModel::Type::Boolean.new.cast(stored) + end + + def save_calendar_toggle(name, value) + key = calendar_toggle_key(name) + if User.current.logged? + pref = User.current.pref + return if pref[key] == (value ? '1' : '0') + + pref[key] = value ? '1' : '0' + pref.save + else + session[key] = value + end + end + def log(msg) Rails.logger.info "[CalendarsControllerPatch] #{msg}" end