Chris@1517: # Redmine - project management software Chris@1517: # Copyright (C) 2006-2014 Jean-Philippe Lang Chris@1517: # Chris@1517: # This program is free software; you can redistribute it and/or Chris@1517: # modify it under the terms of the GNU General Public License Chris@1517: # as published by the Free Software Foundation; either version 2 Chris@1517: # of the License, or (at your option) any later version. Chris@1517: # Chris@1517: # This program is distributed in the hope that it will be useful, Chris@1517: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@1517: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@1517: # GNU General Public License for more details. Chris@1517: # Chris@1517: # You should have received a copy of the GNU General Public License Chris@1517: # along with this program; if not, write to the Free Software Chris@1517: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@1517: Chris@1517: class CustomField < ActiveRecord::Base Chris@1517: include Redmine::SubclassFactory Chris@1517: Chris@1517: has_many :custom_values, :dependent => :delete_all Chris@1517: has_and_belongs_to_many :roles, :join_table => "#{table_name_prefix}custom_fields_roles#{table_name_suffix}", :foreign_key => "custom_field_id" Chris@1517: acts_as_list :scope => 'type = \'#{self.class}\'' Chris@1517: serialize :possible_values Chris@1517: store :format_store Chris@1517: Chris@1517: validates_presence_of :name, :field_format Chris@1517: validates_uniqueness_of :name, :scope => :type Chris@1517: validates_length_of :name, :maximum => 30 Chris@1517: validates_inclusion_of :field_format, :in => Proc.new { Redmine::FieldFormat.available_formats } Chris@1517: validate :validate_custom_field Chris@1517: Chris@1517: before_validation :set_searchable Chris@1517: before_save do |field| Chris@1517: field.format.before_custom_field_save(field) Chris@1517: end Chris@1517: after_save :handle_multiplicity_change Chris@1517: after_save do |field| Chris@1517: if field.visible_changed? && field.visible Chris@1517: field.roles.clear Chris@1517: end Chris@1517: end Chris@1517: Chris@1517: scope :sorted, lambda { order("#{table_name}.position ASC") } Chris@1517: scope :visible, lambda {|*args| Chris@1517: user = args.shift || User.current Chris@1517: if user.admin? Chris@1517: # nop Chris@1517: elsif user.memberships.any? Chris@1517: where("#{table_name}.visible = ? OR #{table_name}.id IN (SELECT DISTINCT cfr.custom_field_id FROM #{Member.table_name} m" + Chris@1517: " INNER JOIN #{MemberRole.table_name} mr ON mr.member_id = m.id" + Chris@1517: " INNER JOIN #{table_name_prefix}custom_fields_roles#{table_name_suffix} cfr ON cfr.role_id = mr.role_id" + Chris@1517: " WHERE m.user_id = ?)", Chris@1517: true, user.id) Chris@1517: else Chris@1517: where(:visible => true) Chris@1517: end Chris@1517: } Chris@1517: Chris@1517: def visible_by?(project, user=User.current) Chris@1517: visible? || user.admin? Chris@1517: end Chris@1517: Chris@1517: def format Chris@1517: @format ||= Redmine::FieldFormat.find(field_format) Chris@1517: end Chris@1517: Chris@1517: def field_format=(arg) Chris@1517: # cannot change format of a saved custom field Chris@1517: if new_record? Chris@1517: @format = nil Chris@1517: super Chris@1517: end Chris@1517: end Chris@1517: Chris@1517: def set_searchable Chris@1517: # make sure these fields are not searchable Chris@1517: self.searchable = false unless format.class.searchable_supported Chris@1517: # make sure only these fields can have multiple values Chris@1517: self.multiple = false unless format.class.multiple_supported Chris@1517: true Chris@1517: end Chris@1517: Chris@1517: def validate_custom_field Chris@1517: format.validate_custom_field(self).each do |attribute, message| Chris@1517: errors.add attribute, message Chris@1517: end Chris@1517: Chris@1517: if regexp.present? Chris@1517: begin Chris@1517: Regexp.new(regexp) Chris@1517: rescue Chris@1517: errors.add(:regexp, :invalid) Chris@1517: end Chris@1517: end Chris@1517: Chris@1517: if default_value.present? Chris@1517: validate_field_value(default_value).each do |message| Chris@1517: errors.add :default_value, message Chris@1517: end Chris@1517: end Chris@1517: end Chris@1517: Chris@1517: def possible_custom_value_options(custom_value) Chris@1517: format.possible_custom_value_options(custom_value) Chris@1517: end Chris@1517: Chris@1517: def possible_values_options(object=nil) Chris@1517: if object.is_a?(Array) Chris@1517: object.map {|o| format.possible_values_options(self, o)}.reduce(:&) || [] Chris@1517: else Chris@1517: format.possible_values_options(self, object) || [] Chris@1517: end Chris@1517: end Chris@1517: Chris@1517: def possible_values Chris@1517: values = read_attribute(:possible_values) Chris@1517: if values.is_a?(Array) Chris@1517: values.each do |value| Chris@1517: value.force_encoding('UTF-8') if value.respond_to?(:force_encoding) Chris@1517: end Chris@1517: values Chris@1517: else Chris@1517: [] Chris@1517: end Chris@1517: end Chris@1517: Chris@1517: # Makes possible_values accept a multiline string Chris@1517: def possible_values=(arg) Chris@1517: if arg.is_a?(Array) Chris@1517: values = arg.compact.collect(&:strip).select {|v| !v.blank?} Chris@1517: write_attribute(:possible_values, values) Chris@1517: else Chris@1517: self.possible_values = arg.to_s.split(/[\n\r]+/) Chris@1517: end Chris@1517: end Chris@1517: Chris@1517: def cast_value(value) Chris@1517: format.cast_value(self, value) Chris@1517: end Chris@1517: Chris@1517: def value_from_keyword(keyword, customized) Chris@1517: possible_values_options = possible_values_options(customized) Chris@1517: if possible_values_options.present? Chris@1517: keyword = keyword.to_s.downcase Chris@1517: if v = possible_values_options.detect {|text, id| text.downcase == keyword} Chris@1517: if v.is_a?(Array) Chris@1517: v.last Chris@1517: else Chris@1517: v Chris@1517: end Chris@1517: end Chris@1517: else Chris@1517: keyword Chris@1517: end Chris@1517: end Chris@1517: Chris@1517: # Returns a ORDER BY clause that can used to sort customized Chris@1517: # objects by their value of the custom field. Chris@1517: # Returns nil if the custom field can not be used for sorting. Chris@1517: def order_statement Chris@1517: return nil if multiple? Chris@1517: format.order_statement(self) Chris@1517: end Chris@1517: Chris@1517: # Returns a GROUP BY clause that can used to group by custom value Chris@1517: # Returns nil if the custom field can not be used for grouping. Chris@1517: def group_statement Chris@1517: return nil if multiple? Chris@1517: format.group_statement(self) Chris@1517: end Chris@1517: Chris@1517: def join_for_order_statement Chris@1517: format.join_for_order_statement(self) Chris@1517: end Chris@1517: Chris@1517: def visibility_by_project_condition(project_key=nil, user=User.current, id_column=nil) Chris@1517: if visible? || user.admin? Chris@1517: "1=1" Chris@1517: elsif user.anonymous? Chris@1517: "1=0" Chris@1517: else Chris@1517: project_key ||= "#{self.class.customized_class.table_name}.project_id" Chris@1517: id_column ||= id Chris@1517: "#{project_key} IN (SELECT DISTINCT m.project_id FROM #{Member.table_name} m" + Chris@1517: " INNER JOIN #{MemberRole.table_name} mr ON mr.member_id = m.id" + Chris@1517: " INNER JOIN #{table_name_prefix}custom_fields_roles#{table_name_suffix} cfr ON cfr.role_id = mr.role_id" + Chris@1517: " WHERE m.user_id = #{user.id} AND cfr.custom_field_id = #{id_column})" Chris@1517: end Chris@1517: end Chris@1517: Chris@1517: def self.visibility_condition Chris@1517: if user.admin? Chris@1517: "1=1" Chris@1517: elsif user.anonymous? Chris@1517: "#{table_name}.visible" Chris@1517: else Chris@1517: "#{project_key} IN (SELECT DISTINCT m.project_id FROM #{Member.table_name} m" + Chris@1517: " INNER JOIN #{MemberRole.table_name} mr ON mr.member_id = m.id" + Chris@1517: " INNER JOIN #{table_name_prefix}custom_fields_roles#{table_name_suffix} cfr ON cfr.role_id = mr.role_id" + Chris@1517: " WHERE m.user_id = #{user.id} AND cfr.custom_field_id = #{id})" Chris@1517: end Chris@1517: end Chris@1517: Chris@1517: def <=>(field) Chris@1517: position <=> field.position Chris@1517: end Chris@1517: Chris@1517: # Returns the class that values represent Chris@1517: def value_class Chris@1517: format.target_class if format.respond_to?(:target_class) Chris@1517: end Chris@1517: Chris@1517: def self.customized_class Chris@1517: self.name =~ /^(.+)CustomField$/ Chris@1517: $1.constantize rescue nil Chris@1517: end Chris@1517: Chris@1517: # to move in project_custom_field Chris@1517: def self.for_all Chris@1517: where(:is_for_all => true).order('position').all Chris@1517: end Chris@1517: Chris@1517: def type_name Chris@1517: nil Chris@1517: end Chris@1517: Chris@1517: # Returns the error messages for the given value Chris@1517: # or an empty array if value is a valid value for the custom field Chris@1517: def validate_custom_value(custom_value) Chris@1517: value = custom_value.value Chris@1517: errs = [] Chris@1517: if value.is_a?(Array) Chris@1517: if !multiple? Chris@1517: errs << ::I18n.t('activerecord.errors.messages.invalid') Chris@1517: end Chris@1517: if is_required? && value.detect(&:present?).nil? Chris@1517: errs << ::I18n.t('activerecord.errors.messages.blank') Chris@1517: end Chris@1517: else Chris@1517: if is_required? && value.blank? Chris@1517: errs << ::I18n.t('activerecord.errors.messages.blank') Chris@1517: end Chris@1517: end Chris@1517: errs += format.validate_custom_value(custom_value) Chris@1517: errs Chris@1517: end Chris@1517: Chris@1517: # Returns the error messages for the default custom field value Chris@1517: def validate_field_value(value) Chris@1517: validate_custom_value(CustomValue.new(:custom_field => self, :value => value)) Chris@1517: end Chris@1517: Chris@1517: # Returns true if value is a valid value for the custom field Chris@1517: def valid_field_value?(value) Chris@1517: validate_field_value(value).empty? Chris@1517: end Chris@1517: Chris@1517: def format_in?(*args) Chris@1517: args.include?(field_format) Chris@1517: end Chris@1517: Chris@1517: protected Chris@1517: Chris@1517: # Removes multiple values for the custom field after setting the multiple attribute to false Chris@1517: # We kepp the value with the highest id for each customized object Chris@1517: def handle_multiplicity_change Chris@1517: if !new_record? && multiple_was && !multiple Chris@1517: ids = custom_values. Chris@1517: where("EXISTS(SELECT 1 FROM #{CustomValue.table_name} cve WHERE cve.custom_field_id = #{CustomValue.table_name}.custom_field_id" + Chris@1517: " AND cve.customized_type = #{CustomValue.table_name}.customized_type AND cve.customized_id = #{CustomValue.table_name}.customized_id" + Chris@1517: " AND cve.id > #{CustomValue.table_name}.id)"). Chris@1517: pluck(:id) Chris@1517: Chris@1517: if ids.any? Chris@1517: custom_values.where(:id => ids).delete_all Chris@1517: end Chris@1517: end Chris@1517: end Chris@1517: end Chris@1517: Chris@1517: require_dependency 'redmine/field_format'