More flexible mail notifications settings at user level. A user has now 3 options:
[gitredmine.git] / app / models / project.rb
blob8f7e03a7c470fc1fe16a8bd0aa7c5a3eeecb930b
1 # redMine - project management software
2 # Copyright (C) 2006  Jean-Philippe Lang
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
8
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU General Public License for more details.
13
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18 class Project < ActiveRecord::Base
19   # Project statuses
20   STATUS_ACTIVE     = 1
21   STATUS_ARCHIVED   = 9
22   
23   has_many :members, :dependent => :delete_all, :include => :user, :conditions => "#{User.table_name}.status=#{User::STATUS_ACTIVE}"
24   has_many :users, :through => :members
25   has_many :custom_values, :dependent => :delete_all, :as => :customized
26   has_many :enabled_modules, :dependent => :delete_all
27   has_many :issues, :dependent => :destroy, :order => "#{Issue.table_name}.created_on DESC", :include => [:status, :tracker]
28   has_many :issue_changes, :through => :issues, :source => :journals
29   has_many :versions, :dependent => :destroy, :order => "#{Version.table_name}.effective_date DESC, #{Version.table_name}.name DESC"
30   has_many :time_entries, :dependent => :delete_all
31   has_many :queries, :dependent => :delete_all
32   has_many :documents, :dependent => :destroy
33   has_many :news, :dependent => :delete_all, :include => :author
34   has_many :issue_categories, :dependent => :delete_all, :order => "#{IssueCategory.table_name}.name"
35   has_many :boards, :order => "position ASC"
36   has_one :repository, :dependent => :destroy
37   has_many :changesets, :through => :repository
38   has_one :wiki, :dependent => :destroy
39   has_and_belongs_to_many :custom_fields, :class_name => 'IssueCustomField', :join_table => "#{table_name_prefix}custom_fields_projects#{table_name_suffix}", :association_foreign_key => 'custom_field_id'
40   acts_as_tree :order => "name", :counter_cache => true
42   acts_as_searchable :columns => ['name', 'description'], :project_key => 'id'
43   acts_as_event :title => Proc.new {|o| "#{l(:label_project)}: #{o.name}"},
44                 :url => Proc.new {|o| {:controller => 'projects', :action => 'show', :id => o.id}}
46   attr_protected :status, :enabled_module_names
47   
48   validates_presence_of :name, :description, :identifier
49   validates_uniqueness_of :name, :identifier
50   validates_associated :custom_values, :on => :update
51   validates_associated :repository, :wiki
52   validates_length_of :name, :maximum => 30
53   validates_format_of :name, :with => /^[\w\s\'\-]*$/i
54   validates_length_of :description, :maximum => 255
55   validates_length_of :homepage, :maximum => 60
56   validates_length_of :identifier, :in => 3..12
57   validates_format_of :identifier, :with => /^[a-z0-9\-]*$/
58   
59   def identifier=(identifier)
60     super unless identifier_frozen?
61   end
62   
63   def identifier_frozen?
64     errors[:identifier].nil? && !(new_record? || identifier.blank?)
65   end
66   
67   def issues_with_subprojects(include_subprojects=false)
68     conditions = nil
69     if include_subprojects && !active_children.empty?
70       ids = [id] + active_children.collect {|c| c.id}
71       conditions = ["#{Issue.table_name}.project_id IN (#{ids.join(',')})"]
72     end
73     conditions ||= ["#{Issue.table_name}.project_id = ?", id]
74     Issue.with_scope :find => { :conditions => conditions } do 
75       yield
76     end 
77   end
78   
79   # returns latest created projects
80   # non public projects will be returned only if user is a member of those
81   def self.latest(user=nil, count=5)
82     find(:all, :limit => count, :conditions => visible_by(user), :order => "created_on DESC")   
83   end   
85   def self.visible_by(user=nil)
86     if user && user.admin?
87       return "#{Project.table_name}.status=#{Project::STATUS_ACTIVE}"
88     elsif user && user.memberships.any?
89       return "#{Project.table_name}.status=#{Project::STATUS_ACTIVE} AND (#{Project.table_name}.is_public = #{connection.quoted_true} or #{Project.table_name}.id IN (#{user.memberships.collect{|m| m.project_id}.join(',')}))"
90     else
91       return "#{Project.table_name}.status=#{Project::STATUS_ACTIVE} AND #{Project.table_name}.is_public = #{connection.quoted_true}"
92     end
93   end
94   
95   def active?
96     self.status == STATUS_ACTIVE
97   end
98   
99   def archive
100     # Archive subprojects if any
101     children.each do |subproject|
102       subproject.archive
103     end
104     update_attribute :status, STATUS_ARCHIVED
105   end
106   
107   def unarchive
108     return false if parent && !parent.active?
109     update_attribute :status, STATUS_ACTIVE
110   end
111   
112   def active_children
113     children.select {|child| child.active?}
114   end
115   
116   # Users issues can be assigned to
117   def assignable_users
118     members.select {|m| m.role.assignable?}.collect {|m| m.user}
119   end
120   
121   # Returns the mail adresses of users that should be always notified on project events
122   def recipients
123     members.select {|m| m.mail_notification? || m.user.mail_notification?}.collect {|m| m.user.mail}
124   end
125   
126   # Returns an array of all custom fields enabled for project issues
127   # (explictly associated custom fields and custom fields enabled for all projects)
128   def custom_fields_for_issues(tracker)
129     all_custom_fields.select {|c| tracker.custom_fields.include? c }
130   end
131   
132   def all_custom_fields
133     @all_custom_fields ||= (IssueCustomField.for_all + custom_fields).uniq
134   end
135   
136   def <=>(project)
137     name <=> project.name
138   end
139   
140   def allows_to?(action)
141     if action.is_a? Hash
142       allowed_actions.include? "#{action[:controller]}/#{action[:action]}"
143     else
144       allowed_permissions.include? action
145     end
146   end
147   
148   def module_enabled?(module_name)
149     module_name = module_name.to_s
150     enabled_modules.detect {|m| m.name == module_name}
151   end
152   
153   def enabled_module_names=(module_names)
154     enabled_modules.clear
155     module_names = [] unless module_names && module_names.is_a?(Array)
156     module_names.each do |name|
157       enabled_modules << EnabledModule.new(:name => name.to_s)
158     end
159   end
161 protected
162   def validate
163     errors.add(parent_id, " must be a root project") if parent and parent.parent
164     errors.add_to_base("A project with subprojects can't be a subproject") if parent and children.size > 0
165   end
166   
167 private
168   def allowed_permissions
169     @allowed_permissions ||= begin
170       module_names = enabled_modules.collect {|m| m.name}
171       Redmine::AccessControl.modules_permissions(module_names).collect {|p| p.name}
172     end
173   end
175   def allowed_actions
176     @actions_allowed ||= allowed_permissions.inject([]) { |actions, permission| actions += Redmine::AccessControl.allowed_actions(permission) }.flatten
177   end