2019-03-16 09:37:35 +00:00
|
|
|
# frozen_string_literal: true
|
2019-03-15 01:32:57 +00:00
|
|
|
|
2011-08-21 01:58:55 +00:00
|
|
|
# Redmine - project management software
|
2017-06-25 08:40:31 +00:00
|
|
|
# Copyright (C) 2006-2017 Jean-Philippe Lang
|
2006-12-03 19:55:45 +00:00
|
|
|
#
|
|
|
|
|
# This program is free software; you can redistribute it and/or
|
|
|
|
|
# modify it under the terms of the GNU General Public License
|
|
|
|
|
# as published by the Free Software Foundation; either version 2
|
|
|
|
|
# of the License, or (at your option) any later version.
|
2011-08-21 01:58:55 +00:00
|
|
|
#
|
2006-12-03 19:55:45 +00:00
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
# GNU General Public License for more details.
|
2011-08-21 01:58:55 +00:00
|
|
|
#
|
2006-12-03 19:55:45 +00:00
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
|
2017-07-25 15:38:56 +00:00
|
|
|
require 'redmine/my_page'
|
|
|
|
|
|
2006-12-03 19:55:45 +00:00
|
|
|
class UserPreference < ActiveRecord::Base
|
2016-07-17 06:43:12 +00:00
|
|
|
include Redmine::SafeAttributes
|
|
|
|
|
|
2006-12-03 19:55:45 +00:00
|
|
|
belongs_to :user
|
2007-01-25 18:25:57 +00:00
|
|
|
serialize :others
|
2011-08-21 01:58:55 +00:00
|
|
|
|
2017-03-15 17:52:41 +00:00
|
|
|
before_save :set_others_hash, :clear_unused_block_settings
|
2013-03-20 08:48:03 +00:00
|
|
|
|
2016-07-17 06:43:12 +00:00
|
|
|
safe_attributes 'hide_mail',
|
|
|
|
|
'time_zone',
|
|
|
|
|
'comments_sorting',
|
|
|
|
|
'warn_on_leaving_unsaved',
|
2016-08-30 19:59:29 +00:00
|
|
|
'no_self_notified',
|
2019-05-20 22:26:30 +00:00
|
|
|
'textarea_font',
|
|
|
|
|
'recently_used_projects'
|
2016-08-30 19:59:29 +00:00
|
|
|
|
|
|
|
|
TEXTAREA_FONT_OPTIONS = ['monospace', 'proportional']
|
2016-07-17 06:43:12 +00:00
|
|
|
|
2011-12-18 13:26:20 +00:00
|
|
|
def initialize(attributes=nil, *args)
|
2006-12-03 19:55:45 +00:00
|
|
|
super
|
2017-01-15 12:58:07 +00:00
|
|
|
if new_record?
|
|
|
|
|
unless attributes && attributes.key?(:hide_mail)
|
|
|
|
|
self.hide_mail = Setting.default_users_hide_mail?
|
|
|
|
|
end
|
|
|
|
|
unless attributes && attributes.key?(:time_zone)
|
|
|
|
|
self.time_zone = Setting.default_users_time_zone
|
|
|
|
|
end
|
|
|
|
|
unless attributes && attributes.key?(:no_self_notified)
|
|
|
|
|
self.no_self_notified = true
|
|
|
|
|
end
|
2016-04-03 11:33:28 +00:00
|
|
|
end
|
2006-12-03 19:55:45 +00:00
|
|
|
self.others ||= {}
|
|
|
|
|
end
|
2011-08-21 01:58:55 +00:00
|
|
|
|
2011-12-04 06:44:39 +00:00
|
|
|
def set_others_hash
|
2007-04-25 15:06:20 +00:00
|
|
|
self.others ||= {}
|
|
|
|
|
end
|
2011-08-21 01:58:55 +00:00
|
|
|
|
2006-12-03 19:55:45 +00:00
|
|
|
def [](attr_name)
|
2013-09-10 17:39:49 +00:00
|
|
|
if has_attribute? attr_name
|
2006-12-03 19:55:45 +00:00
|
|
|
super
|
|
|
|
|
else
|
2007-04-25 15:06:20 +00:00
|
|
|
others ? others[attr_name] : nil
|
2006-12-03 19:55:45 +00:00
|
|
|
end
|
|
|
|
|
end
|
2011-08-21 01:58:55 +00:00
|
|
|
|
2006-12-03 19:55:45 +00:00
|
|
|
def []=(attr_name, value)
|
2013-09-10 17:39:49 +00:00
|
|
|
if has_attribute? attr_name
|
2006-12-03 19:55:45 +00:00
|
|
|
super
|
|
|
|
|
else
|
2012-09-22 06:16:53 +00:00
|
|
|
h = (read_attribute(:others) || {}).dup
|
2008-07-04 17:58:14 +00:00
|
|
|
h.update(attr_name => value)
|
|
|
|
|
write_attribute(:others, h)
|
|
|
|
|
value
|
2006-12-03 19:55:45 +00:00
|
|
|
end
|
|
|
|
|
end
|
2011-08-21 01:58:55 +00:00
|
|
|
|
2008-03-05 15:41:54 +00:00
|
|
|
def comments_sorting; self[:comments_sorting] end
|
|
|
|
|
def comments_sorting=(order); self[:comments_sorting]=order end
|
2011-08-21 01:58:55 +00:00
|
|
|
|
2011-02-21 09:53:29 +00:00
|
|
|
def warn_on_leaving_unsaved; self[:warn_on_leaving_unsaved] || '1'; end
|
|
|
|
|
def warn_on_leaving_unsaved=(value); self[:warn_on_leaving_unsaved]=value; end
|
2013-05-13 16:38:08 +00:00
|
|
|
|
|
|
|
|
def no_self_notified; (self[:no_self_notified] == true || self[:no_self_notified] == '1'); end
|
|
|
|
|
def no_self_notified=(value); self[:no_self_notified]=value; end
|
2015-10-15 19:06:22 +00:00
|
|
|
|
|
|
|
|
def activity_scope; Array(self[:activity_scope]) ; end
|
|
|
|
|
def activity_scope=(value); self[:activity_scope]=value ; end
|
2016-08-30 19:59:29 +00:00
|
|
|
|
|
|
|
|
def textarea_font; self[:textarea_font] end
|
|
|
|
|
def textarea_font=(value); self[:textarea_font]=value; end
|
2016-10-29 10:10:31 +00:00
|
|
|
|
2019-05-20 22:26:30 +00:00
|
|
|
def recently_used_projects; (self[:recently_used_projects] || 3).to_i; end
|
|
|
|
|
def recently_used_projects=(value); self[:recently_used_projects] = value.to_i; end
|
|
|
|
|
|
2017-04-04 17:17:47 +00:00
|
|
|
# Returns the names of groups that are displayed on user's page
|
|
|
|
|
# Example:
|
|
|
|
|
# preferences.my_page_groups
|
|
|
|
|
# # => ['top', 'left, 'right']
|
|
|
|
|
def my_page_groups
|
|
|
|
|
Redmine::MyPage.groups
|
|
|
|
|
end
|
|
|
|
|
|
2016-10-29 10:10:31 +00:00
|
|
|
def my_page_layout
|
|
|
|
|
self[:my_page_layout] ||= Redmine::MyPage.default_layout.deep_dup
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def my_page_layout=(arg)
|
|
|
|
|
self[:my_page_layout] = arg
|
|
|
|
|
end
|
2016-10-29 10:25:55 +00:00
|
|
|
|
2016-10-30 08:32:31 +00:00
|
|
|
def my_page_settings(block=nil)
|
|
|
|
|
s = self[:my_page_settings] ||= {}
|
|
|
|
|
if block
|
2016-12-14 12:28:47 +00:00
|
|
|
s[block] ||= {}
|
2016-10-30 08:32:31 +00:00
|
|
|
else
|
|
|
|
|
s
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def my_page_settings=(arg)
|
|
|
|
|
self[:my_page_settings] = arg
|
|
|
|
|
end
|
|
|
|
|
|
2017-03-16 18:02:43 +00:00
|
|
|
# Removes block from the user page layout
|
2017-04-04 17:17:47 +00:00
|
|
|
# Example:
|
|
|
|
|
# preferences.remove_block('news')
|
2016-10-29 10:25:55 +00:00
|
|
|
def remove_block(block)
|
|
|
|
|
block = block.to_s.underscore
|
2018-08-01 14:28:39 +00:00
|
|
|
my_page_layout.each_key do |group|
|
2017-04-04 17:17:47 +00:00
|
|
|
my_page_layout[group].delete(block)
|
2016-10-29 10:25:55 +00:00
|
|
|
end
|
|
|
|
|
my_page_layout
|
|
|
|
|
end
|
|
|
|
|
|
2017-03-16 18:02:43 +00:00
|
|
|
# Adds block to the user page layout
|
|
|
|
|
# Returns nil if block is not valid or if it's already
|
|
|
|
|
# present in the user page layout
|
2016-10-29 10:25:55 +00:00
|
|
|
def add_block(block)
|
|
|
|
|
block = block.to_s.underscore
|
2017-03-16 18:02:43 +00:00
|
|
|
return unless Redmine::MyPage.valid_block?(block, my_page_layout.values.flatten)
|
2016-10-29 10:25:55 +00:00
|
|
|
|
|
|
|
|
remove_block(block)
|
2017-04-04 17:17:47 +00:00
|
|
|
# add it to the first group
|
|
|
|
|
group = my_page_groups.first
|
|
|
|
|
my_page_layout[group] ||= []
|
|
|
|
|
my_page_layout[group].unshift(block)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# Sets the block order for the given group.
|
|
|
|
|
# Example:
|
|
|
|
|
# preferences.order_blocks('left', ['issueswatched', 'news'])
|
|
|
|
|
def order_blocks(group, blocks)
|
|
|
|
|
group = group.to_s
|
|
|
|
|
if Redmine::MyPage.groups.include?(group) && blocks.present?
|
|
|
|
|
blocks = blocks.map(&:underscore) & my_page_layout.values.flatten
|
|
|
|
|
blocks.each {|block| remove_block(block)}
|
|
|
|
|
my_page_layout[group] = blocks
|
|
|
|
|
end
|
2016-10-29 10:25:55 +00:00
|
|
|
end
|
2016-10-30 08:32:31 +00:00
|
|
|
|
|
|
|
|
def update_block_settings(block, settings)
|
2017-03-14 18:18:19 +00:00
|
|
|
block = block.to_s
|
2016-10-30 08:32:31 +00:00
|
|
|
block_settings = my_page_settings(block).merge(settings.symbolize_keys)
|
|
|
|
|
my_page_settings[block] = block_settings
|
|
|
|
|
end
|
2017-03-15 17:52:41 +00:00
|
|
|
|
|
|
|
|
def clear_unused_block_settings
|
|
|
|
|
blocks = my_page_layout.values.flatten
|
|
|
|
|
my_page_settings.keep_if {|block, settings| blocks.include?(block)}
|
|
|
|
|
end
|
|
|
|
|
private :clear_unused_block_settings
|
2006-12-03 19:55:45 +00:00
|
|
|
end
|