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