##// END OF EJS Templates
Fixes project wiki copy....
Jean-Philippe Lang -
r2855:a9fb11c0f570
parent child
Show More
@@ -1,38 +1,39
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2009 Jean-Philippe Lang
2 # Copyright (C) 2006-2009 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
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
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.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 class EnabledModule < ActiveRecord::Base
18 class EnabledModule < ActiveRecord::Base
19 belongs_to :project
19 belongs_to :project
20
20
21 validates_presence_of :name
21 validates_presence_of :name
22 validates_uniqueness_of :name, :scope => :project_id
22 validates_uniqueness_of :name, :scope => :project_id
23
23
24 after_create :module_enabled
24 after_create :module_enabled
25
25
26 private
26 private
27
27
28 # after_create callback used to do things when a module is enabled
28 # after_create callback used to do things when a module is enabled
29 def module_enabled
29 def module_enabled
30 case name
30 case name
31 when 'wiki'
31 when 'wiki'
32 # Create a wiki with a default start page
32 # Create a wiki with a default start page
33 if project && project.wiki.nil?
33 if project && project.wiki.nil?
34 #Wiki.create(:project => project, :start_page => 'Wiki')
34 logger.debug("after_create module_enabled")
35 Wiki.create(:project => project, :start_page => 'Wiki')
35 end
36 end
36 end
37 end
37 end
38 end
38 end
39 end
@@ -1,566 +1,573
1 # redMine - project management software
1 # redMine - project management software
2 # Copyright (C) 2006 Jean-Philippe Lang
2 # Copyright (C) 2006 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
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
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.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 class Project < ActiveRecord::Base
18 class Project < ActiveRecord::Base
19 # Project statuses
19 # Project statuses
20 STATUS_ACTIVE = 1
20 STATUS_ACTIVE = 1
21 STATUS_ARCHIVED = 9
21 STATUS_ARCHIVED = 9
22
22
23 # Specific overidden Activities
23 # Specific overidden Activities
24 has_many :time_entry_activities do
24 has_many :time_entry_activities do
25 def active
25 def active
26 find(:all, :conditions => {:active => true})
26 find(:all, :conditions => {:active => true})
27 end
27 end
28 end
28 end
29 has_many :members, :include => :user, :conditions => "#{User.table_name}.type='User' AND #{User.table_name}.status=#{User::STATUS_ACTIVE}"
29 has_many :members, :include => :user, :conditions => "#{User.table_name}.type='User' AND #{User.table_name}.status=#{User::STATUS_ACTIVE}"
30 has_many :member_principals, :class_name => 'Member',
30 has_many :member_principals, :class_name => 'Member',
31 :include => :principal,
31 :include => :principal,
32 :conditions => "#{Principal.table_name}.type='Group' OR (#{Principal.table_name}.type='User' AND #{Principal.table_name}.status=#{User::STATUS_ACTIVE})"
32 :conditions => "#{Principal.table_name}.type='Group' OR (#{Principal.table_name}.type='User' AND #{Principal.table_name}.status=#{User::STATUS_ACTIVE})"
33 has_many :users, :through => :members
33 has_many :users, :through => :members
34 has_many :principals, :through => :member_principals, :source => :principal
34 has_many :principals, :through => :member_principals, :source => :principal
35
35
36 has_many :enabled_modules, :dependent => :delete_all
36 has_many :enabled_modules, :dependent => :delete_all
37 has_and_belongs_to_many :trackers, :order => "#{Tracker.table_name}.position"
37 has_and_belongs_to_many :trackers, :order => "#{Tracker.table_name}.position"
38 has_many :issues, :dependent => :destroy, :order => "#{Issue.table_name}.created_on DESC", :include => [:status, :tracker]
38 has_many :issues, :dependent => :destroy, :order => "#{Issue.table_name}.created_on DESC", :include => [:status, :tracker]
39 has_many :issue_changes, :through => :issues, :source => :journals
39 has_many :issue_changes, :through => :issues, :source => :journals
40 has_many :versions, :dependent => :destroy, :order => "#{Version.table_name}.effective_date DESC, #{Version.table_name}.name DESC"
40 has_many :versions, :dependent => :destroy, :order => "#{Version.table_name}.effective_date DESC, #{Version.table_name}.name DESC"
41 has_many :time_entries, :dependent => :delete_all
41 has_many :time_entries, :dependent => :delete_all
42 has_many :queries, :dependent => :delete_all
42 has_many :queries, :dependent => :delete_all
43 has_many :documents, :dependent => :destroy
43 has_many :documents, :dependent => :destroy
44 has_many :news, :dependent => :delete_all, :include => :author
44 has_many :news, :dependent => :delete_all, :include => :author
45 has_many :issue_categories, :dependent => :delete_all, :order => "#{IssueCategory.table_name}.name"
45 has_many :issue_categories, :dependent => :delete_all, :order => "#{IssueCategory.table_name}.name"
46 has_many :boards, :dependent => :destroy, :order => "position ASC"
46 has_many :boards, :dependent => :destroy, :order => "position ASC"
47 has_one :repository, :dependent => :destroy
47 has_one :repository, :dependent => :destroy
48 has_many :changesets, :through => :repository
48 has_many :changesets, :through => :repository
49 has_one :wiki, :dependent => :destroy
49 has_one :wiki, :dependent => :destroy
50 # Custom field for the project issues
50 # Custom field for the project issues
51 has_and_belongs_to_many :issue_custom_fields,
51 has_and_belongs_to_many :issue_custom_fields,
52 :class_name => 'IssueCustomField',
52 :class_name => 'IssueCustomField',
53 :order => "#{CustomField.table_name}.position",
53 :order => "#{CustomField.table_name}.position",
54 :join_table => "#{table_name_prefix}custom_fields_projects#{table_name_suffix}",
54 :join_table => "#{table_name_prefix}custom_fields_projects#{table_name_suffix}",
55 :association_foreign_key => 'custom_field_id'
55 :association_foreign_key => 'custom_field_id'
56
56
57 acts_as_nested_set :order => 'name', :dependent => :destroy
57 acts_as_nested_set :order => 'name', :dependent => :destroy
58 acts_as_attachable :view_permission => :view_files,
58 acts_as_attachable :view_permission => :view_files,
59 :delete_permission => :manage_files
59 :delete_permission => :manage_files
60
60
61 acts_as_customizable
61 acts_as_customizable
62 acts_as_searchable :columns => ['name', 'description'], :project_key => 'id', :permission => nil
62 acts_as_searchable :columns => ['name', 'description'], :project_key => 'id', :permission => nil
63 acts_as_event :title => Proc.new {|o| "#{l(:label_project)}: #{o.name}"},
63 acts_as_event :title => Proc.new {|o| "#{l(:label_project)}: #{o.name}"},
64 :url => Proc.new {|o| {:controller => 'projects', :action => 'show', :id => o.id}},
64 :url => Proc.new {|o| {:controller => 'projects', :action => 'show', :id => o.id}},
65 :author => nil
65 :author => nil
66
66
67 attr_protected :status, :enabled_module_names
67 attr_protected :status, :enabled_module_names
68
68
69 validates_presence_of :name, :identifier
69 validates_presence_of :name, :identifier
70 validates_uniqueness_of :name, :identifier
70 validates_uniqueness_of :name, :identifier
71 validates_associated :repository, :wiki
71 validates_associated :repository, :wiki
72 validates_length_of :name, :maximum => 30
72 validates_length_of :name, :maximum => 30
73 validates_length_of :homepage, :maximum => 255
73 validates_length_of :homepage, :maximum => 255
74 validates_length_of :identifier, :in => 1..20
74 validates_length_of :identifier, :in => 1..20
75 # donwcase letters, digits, dashes but not digits only
75 # donwcase letters, digits, dashes but not digits only
76 validates_format_of :identifier, :with => /^(?!\d+$)[a-z0-9\-]*$/, :if => Proc.new { |p| p.identifier_changed? }
76 validates_format_of :identifier, :with => /^(?!\d+$)[a-z0-9\-]*$/, :if => Proc.new { |p| p.identifier_changed? }
77 # reserved words
77 # reserved words
78 validates_exclusion_of :identifier, :in => %w( new )
78 validates_exclusion_of :identifier, :in => %w( new )
79
79
80 before_destroy :delete_all_members
80 before_destroy :delete_all_members
81
81
82 named_scope :has_module, lambda { |mod| { :conditions => ["#{Project.table_name}.id IN (SELECT em.project_id FROM #{EnabledModule.table_name} em WHERE em.name=?)", mod.to_s] } }
82 named_scope :has_module, lambda { |mod| { :conditions => ["#{Project.table_name}.id IN (SELECT em.project_id FROM #{EnabledModule.table_name} em WHERE em.name=?)", mod.to_s] } }
83 named_scope :active, { :conditions => "#{Project.table_name}.status = #{STATUS_ACTIVE}"}
83 named_scope :active, { :conditions => "#{Project.table_name}.status = #{STATUS_ACTIVE}"}
84 named_scope :all_public, { :conditions => { :is_public => true } }
84 named_scope :all_public, { :conditions => { :is_public => true } }
85 named_scope :visible, lambda { { :conditions => Project.visible_by(User.current) } }
85 named_scope :visible, lambda { { :conditions => Project.visible_by(User.current) } }
86
86
87 def identifier=(identifier)
87 def identifier=(identifier)
88 super unless identifier_frozen?
88 super unless identifier_frozen?
89 end
89 end
90
90
91 def identifier_frozen?
91 def identifier_frozen?
92 errors[:identifier].nil? && !(new_record? || identifier.blank?)
92 errors[:identifier].nil? && !(new_record? || identifier.blank?)
93 end
93 end
94
94
95 def issues_with_subprojects(include_subprojects=false)
95 def issues_with_subprojects(include_subprojects=false)
96 conditions = nil
96 conditions = nil
97 if include_subprojects
97 if include_subprojects
98 ids = [id] + descendants.collect(&:id)
98 ids = [id] + descendants.collect(&:id)
99 conditions = ["#{Project.table_name}.id IN (#{ids.join(',')}) AND #{Project.visible_by}"]
99 conditions = ["#{Project.table_name}.id IN (#{ids.join(',')}) AND #{Project.visible_by}"]
100 end
100 end
101 conditions ||= ["#{Project.table_name}.id = ?", id]
101 conditions ||= ["#{Project.table_name}.id = ?", id]
102 # Quick and dirty fix for Rails 2 compatibility
102 # Quick and dirty fix for Rails 2 compatibility
103 Issue.send(:with_scope, :find => { :conditions => conditions }) do
103 Issue.send(:with_scope, :find => { :conditions => conditions }) do
104 Version.send(:with_scope, :find => { :conditions => conditions }) do
104 Version.send(:with_scope, :find => { :conditions => conditions }) do
105 yield
105 yield
106 end
106 end
107 end
107 end
108 end
108 end
109
109
110 # returns latest created projects
110 # returns latest created projects
111 # non public projects will be returned only if user is a member of those
111 # non public projects will be returned only if user is a member of those
112 def self.latest(user=nil, count=5)
112 def self.latest(user=nil, count=5)
113 find(:all, :limit => count, :conditions => visible_by(user), :order => "created_on DESC")
113 find(:all, :limit => count, :conditions => visible_by(user), :order => "created_on DESC")
114 end
114 end
115
115
116 # Returns a SQL :conditions string used to find all active projects for the specified user.
116 # Returns a SQL :conditions string used to find all active projects for the specified user.
117 #
117 #
118 # Examples:
118 # Examples:
119 # Projects.visible_by(admin) => "projects.status = 1"
119 # Projects.visible_by(admin) => "projects.status = 1"
120 # Projects.visible_by(normal_user) => "projects.status = 1 AND projects.is_public = 1"
120 # Projects.visible_by(normal_user) => "projects.status = 1 AND projects.is_public = 1"
121 def self.visible_by(user=nil)
121 def self.visible_by(user=nil)
122 user ||= User.current
122 user ||= User.current
123 if user && user.admin?
123 if user && user.admin?
124 return "#{Project.table_name}.status=#{Project::STATUS_ACTIVE}"
124 return "#{Project.table_name}.status=#{Project::STATUS_ACTIVE}"
125 elsif user && user.memberships.any?
125 elsif user && user.memberships.any?
126 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(',')}))"
126 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(',')}))"
127 else
127 else
128 return "#{Project.table_name}.status=#{Project::STATUS_ACTIVE} AND #{Project.table_name}.is_public = #{connection.quoted_true}"
128 return "#{Project.table_name}.status=#{Project::STATUS_ACTIVE} AND #{Project.table_name}.is_public = #{connection.quoted_true}"
129 end
129 end
130 end
130 end
131
131
132 def self.allowed_to_condition(user, permission, options={})
132 def self.allowed_to_condition(user, permission, options={})
133 statements = []
133 statements = []
134 base_statement = "#{Project.table_name}.status=#{Project::STATUS_ACTIVE}"
134 base_statement = "#{Project.table_name}.status=#{Project::STATUS_ACTIVE}"
135 if perm = Redmine::AccessControl.permission(permission)
135 if perm = Redmine::AccessControl.permission(permission)
136 unless perm.project_module.nil?
136 unless perm.project_module.nil?
137 # If the permission belongs to a project module, make sure the module is enabled
137 # If the permission belongs to a project module, make sure the module is enabled
138 base_statement << " AND #{Project.table_name}.id IN (SELECT em.project_id FROM #{EnabledModule.table_name} em WHERE em.name='#{perm.project_module}')"
138 base_statement << " AND #{Project.table_name}.id IN (SELECT em.project_id FROM #{EnabledModule.table_name} em WHERE em.name='#{perm.project_module}')"
139 end
139 end
140 end
140 end
141 if options[:project]
141 if options[:project]
142 project_statement = "#{Project.table_name}.id = #{options[:project].id}"
142 project_statement = "#{Project.table_name}.id = #{options[:project].id}"
143 project_statement << " OR (#{Project.table_name}.lft > #{options[:project].lft} AND #{Project.table_name}.rgt < #{options[:project].rgt})" if options[:with_subprojects]
143 project_statement << " OR (#{Project.table_name}.lft > #{options[:project].lft} AND #{Project.table_name}.rgt < #{options[:project].rgt})" if options[:with_subprojects]
144 base_statement = "(#{project_statement}) AND (#{base_statement})"
144 base_statement = "(#{project_statement}) AND (#{base_statement})"
145 end
145 end
146 if user.admin?
146 if user.admin?
147 # no restriction
147 # no restriction
148 else
148 else
149 statements << "1=0"
149 statements << "1=0"
150 if user.logged?
150 if user.logged?
151 statements << "#{Project.table_name}.is_public = #{connection.quoted_true}" if Role.non_member.allowed_to?(permission)
151 statements << "#{Project.table_name}.is_public = #{connection.quoted_true}" if Role.non_member.allowed_to?(permission)
152 allowed_project_ids = user.memberships.select {|m| m.roles.detect {|role| role.allowed_to?(permission)}}.collect {|m| m.project_id}
152 allowed_project_ids = user.memberships.select {|m| m.roles.detect {|role| role.allowed_to?(permission)}}.collect {|m| m.project_id}
153 statements << "#{Project.table_name}.id IN (#{allowed_project_ids.join(',')})" if allowed_project_ids.any?
153 statements << "#{Project.table_name}.id IN (#{allowed_project_ids.join(',')})" if allowed_project_ids.any?
154 elsif Role.anonymous.allowed_to?(permission)
154 elsif Role.anonymous.allowed_to?(permission)
155 # anonymous user allowed on public project
155 # anonymous user allowed on public project
156 statements << "#{Project.table_name}.is_public = #{connection.quoted_true}"
156 statements << "#{Project.table_name}.is_public = #{connection.quoted_true}"
157 else
157 else
158 # anonymous user is not authorized
158 # anonymous user is not authorized
159 end
159 end
160 end
160 end
161 statements.empty? ? base_statement : "((#{base_statement}) AND (#{statements.join(' OR ')}))"
161 statements.empty? ? base_statement : "((#{base_statement}) AND (#{statements.join(' OR ')}))"
162 end
162 end
163
163
164 # Returns the Systemwide and project specific activities
164 # Returns the Systemwide and project specific activities
165 def activities(include_inactive=false)
165 def activities(include_inactive=false)
166 if include_inactive
166 if include_inactive
167 return all_activities
167 return all_activities
168 else
168 else
169 return active_activities
169 return active_activities
170 end
170 end
171 end
171 end
172
172
173 # Will create a new Project specific Activity or update an existing one
173 # Will create a new Project specific Activity or update an existing one
174 #
174 #
175 # This will raise a ActiveRecord::Rollback if the TimeEntryActivity
175 # This will raise a ActiveRecord::Rollback if the TimeEntryActivity
176 # does not successfully save.
176 # does not successfully save.
177 def update_or_create_time_entry_activity(id, activity_hash)
177 def update_or_create_time_entry_activity(id, activity_hash)
178 if activity_hash.respond_to?(:has_key?) && activity_hash.has_key?('parent_id')
178 if activity_hash.respond_to?(:has_key?) && activity_hash.has_key?('parent_id')
179 self.create_time_entry_activity_if_needed(activity_hash)
179 self.create_time_entry_activity_if_needed(activity_hash)
180 else
180 else
181 activity = project.time_entry_activities.find_by_id(id.to_i)
181 activity = project.time_entry_activities.find_by_id(id.to_i)
182 activity.update_attributes(activity_hash) if activity
182 activity.update_attributes(activity_hash) if activity
183 end
183 end
184 end
184 end
185
185
186 # Create a new TimeEntryActivity if it overrides a system TimeEntryActivity
186 # Create a new TimeEntryActivity if it overrides a system TimeEntryActivity
187 #
187 #
188 # This will raise a ActiveRecord::Rollback if the TimeEntryActivity
188 # This will raise a ActiveRecord::Rollback if the TimeEntryActivity
189 # does not successfully save.
189 # does not successfully save.
190 def create_time_entry_activity_if_needed(activity)
190 def create_time_entry_activity_if_needed(activity)
191 if activity['parent_id']
191 if activity['parent_id']
192
192
193 parent_activity = TimeEntryActivity.find(activity['parent_id'])
193 parent_activity = TimeEntryActivity.find(activity['parent_id'])
194 activity['name'] = parent_activity.name
194 activity['name'] = parent_activity.name
195 activity['position'] = parent_activity.position
195 activity['position'] = parent_activity.position
196
196
197 if Enumeration.overridding_change?(activity, parent_activity)
197 if Enumeration.overridding_change?(activity, parent_activity)
198 project_activity = self.time_entry_activities.create(activity)
198 project_activity = self.time_entry_activities.create(activity)
199
199
200 if project_activity.new_record?
200 if project_activity.new_record?
201 raise ActiveRecord::Rollback, "Overridding TimeEntryActivity was not successfully saved"
201 raise ActiveRecord::Rollback, "Overridding TimeEntryActivity was not successfully saved"
202 else
202 else
203 self.time_entries.update_all("activity_id = #{project_activity.id}", ["activity_id = ?", parent_activity.id])
203 self.time_entries.update_all("activity_id = #{project_activity.id}", ["activity_id = ?", parent_activity.id])
204 end
204 end
205 end
205 end
206 end
206 end
207 end
207 end
208
208
209 # Returns a :conditions SQL string that can be used to find the issues associated with this project.
209 # Returns a :conditions SQL string that can be used to find the issues associated with this project.
210 #
210 #
211 # Examples:
211 # Examples:
212 # project.project_condition(true) => "(projects.id = 1 OR (projects.lft > 1 AND projects.rgt < 10))"
212 # project.project_condition(true) => "(projects.id = 1 OR (projects.lft > 1 AND projects.rgt < 10))"
213 # project.project_condition(false) => "projects.id = 1"
213 # project.project_condition(false) => "projects.id = 1"
214 def project_condition(with_subprojects)
214 def project_condition(with_subprojects)
215 cond = "#{Project.table_name}.id = #{id}"
215 cond = "#{Project.table_name}.id = #{id}"
216 cond = "(#{cond} OR (#{Project.table_name}.lft > #{lft} AND #{Project.table_name}.rgt < #{rgt}))" if with_subprojects
216 cond = "(#{cond} OR (#{Project.table_name}.lft > #{lft} AND #{Project.table_name}.rgt < #{rgt}))" if with_subprojects
217 cond
217 cond
218 end
218 end
219
219
220 def self.find(*args)
220 def self.find(*args)
221 if args.first && args.first.is_a?(String) && !args.first.match(/^\d*$/)
221 if args.first && args.first.is_a?(String) && !args.first.match(/^\d*$/)
222 project = find_by_identifier(*args)
222 project = find_by_identifier(*args)
223 raise ActiveRecord::RecordNotFound, "Couldn't find Project with identifier=#{args.first}" if project.nil?
223 raise ActiveRecord::RecordNotFound, "Couldn't find Project with identifier=#{args.first}" if project.nil?
224 project
224 project
225 else
225 else
226 super
226 super
227 end
227 end
228 end
228 end
229
229
230 def to_param
230 def to_param
231 # id is used for projects with a numeric identifier (compatibility)
231 # id is used for projects with a numeric identifier (compatibility)
232 @to_param ||= (identifier.to_s =~ %r{^\d*$} ? id : identifier)
232 @to_param ||= (identifier.to_s =~ %r{^\d*$} ? id : identifier)
233 end
233 end
234
234
235 def active?
235 def active?
236 self.status == STATUS_ACTIVE
236 self.status == STATUS_ACTIVE
237 end
237 end
238
238
239 # Archives the project and its descendants recursively
239 # Archives the project and its descendants recursively
240 def archive
240 def archive
241 # Archive subprojects if any
241 # Archive subprojects if any
242 children.each do |subproject|
242 children.each do |subproject|
243 subproject.archive
243 subproject.archive
244 end
244 end
245 update_attribute :status, STATUS_ARCHIVED
245 update_attribute :status, STATUS_ARCHIVED
246 end
246 end
247
247
248 # Unarchives the project
248 # Unarchives the project
249 # All its ancestors must be active
249 # All its ancestors must be active
250 def unarchive
250 def unarchive
251 return false if ancestors.detect {|a| !a.active?}
251 return false if ancestors.detect {|a| !a.active?}
252 update_attribute :status, STATUS_ACTIVE
252 update_attribute :status, STATUS_ACTIVE
253 end
253 end
254
254
255 # Returns an array of projects the project can be moved to
255 # Returns an array of projects the project can be moved to
256 def possible_parents
256 def possible_parents
257 @possible_parents ||= (Project.active.find(:all) - self_and_descendants)
257 @possible_parents ||= (Project.active.find(:all) - self_and_descendants)
258 end
258 end
259
259
260 # Sets the parent of the project
260 # Sets the parent of the project
261 # Argument can be either a Project, a String, a Fixnum or nil
261 # Argument can be either a Project, a String, a Fixnum or nil
262 def set_parent!(p)
262 def set_parent!(p)
263 unless p.nil? || p.is_a?(Project)
263 unless p.nil? || p.is_a?(Project)
264 if p.to_s.blank?
264 if p.to_s.blank?
265 p = nil
265 p = nil
266 else
266 else
267 p = Project.find_by_id(p)
267 p = Project.find_by_id(p)
268 return false unless p
268 return false unless p
269 end
269 end
270 end
270 end
271 if p == parent && !p.nil?
271 if p == parent && !p.nil?
272 # Nothing to do
272 # Nothing to do
273 true
273 true
274 elsif p.nil? || (p.active? && move_possible?(p))
274 elsif p.nil? || (p.active? && move_possible?(p))
275 # Insert the project so that target's children or root projects stay alphabetically sorted
275 # Insert the project so that target's children or root projects stay alphabetically sorted
276 sibs = (p.nil? ? self.class.roots : p.children)
276 sibs = (p.nil? ? self.class.roots : p.children)
277 to_be_inserted_before = sibs.detect {|c| c.name.to_s.downcase > name.to_s.downcase }
277 to_be_inserted_before = sibs.detect {|c| c.name.to_s.downcase > name.to_s.downcase }
278 if to_be_inserted_before
278 if to_be_inserted_before
279 move_to_left_of(to_be_inserted_before)
279 move_to_left_of(to_be_inserted_before)
280 elsif p.nil?
280 elsif p.nil?
281 if sibs.empty?
281 if sibs.empty?
282 # move_to_root adds the project in first (ie. left) position
282 # move_to_root adds the project in first (ie. left) position
283 move_to_root
283 move_to_root
284 else
284 else
285 move_to_right_of(sibs.last) unless self == sibs.last
285 move_to_right_of(sibs.last) unless self == sibs.last
286 end
286 end
287 else
287 else
288 # move_to_child_of adds the project in last (ie.right) position
288 # move_to_child_of adds the project in last (ie.right) position
289 move_to_child_of(p)
289 move_to_child_of(p)
290 end
290 end
291 true
291 true
292 else
292 else
293 # Can not move to the given target
293 # Can not move to the given target
294 false
294 false
295 end
295 end
296 end
296 end
297
297
298 # Returns an array of the trackers used by the project and its active sub projects
298 # Returns an array of the trackers used by the project and its active sub projects
299 def rolled_up_trackers
299 def rolled_up_trackers
300 @rolled_up_trackers ||=
300 @rolled_up_trackers ||=
301 Tracker.find(:all, :include => :projects,
301 Tracker.find(:all, :include => :projects,
302 :select => "DISTINCT #{Tracker.table_name}.*",
302 :select => "DISTINCT #{Tracker.table_name}.*",
303 :conditions => ["#{Project.table_name}.lft >= ? AND #{Project.table_name}.rgt <= ? AND #{Project.table_name}.status = #{STATUS_ACTIVE}", lft, rgt],
303 :conditions => ["#{Project.table_name}.lft >= ? AND #{Project.table_name}.rgt <= ? AND #{Project.table_name}.status = #{STATUS_ACTIVE}", lft, rgt],
304 :order => "#{Tracker.table_name}.position")
304 :order => "#{Tracker.table_name}.position")
305 end
305 end
306
306
307 # Returns a hash of project users grouped by role
307 # Returns a hash of project users grouped by role
308 def users_by_role
308 def users_by_role
309 members.find(:all, :include => [:user, :roles]).inject({}) do |h, m|
309 members.find(:all, :include => [:user, :roles]).inject({}) do |h, m|
310 m.roles.each do |r|
310 m.roles.each do |r|
311 h[r] ||= []
311 h[r] ||= []
312 h[r] << m.user
312 h[r] << m.user
313 end
313 end
314 h
314 h
315 end
315 end
316 end
316 end
317
317
318 # Deletes all project's members
318 # Deletes all project's members
319 def delete_all_members
319 def delete_all_members
320 me, mr = Member.table_name, MemberRole.table_name
320 me, mr = Member.table_name, MemberRole.table_name
321 connection.delete("DELETE FROM #{mr} WHERE #{mr}.member_id IN (SELECT #{me}.id FROM #{me} WHERE #{me}.project_id = #{id})")
321 connection.delete("DELETE FROM #{mr} WHERE #{mr}.member_id IN (SELECT #{me}.id FROM #{me} WHERE #{me}.project_id = #{id})")
322 Member.delete_all(['project_id = ?', id])
322 Member.delete_all(['project_id = ?', id])
323 end
323 end
324
324
325 # Users issues can be assigned to
325 # Users issues can be assigned to
326 def assignable_users
326 def assignable_users
327 members.select {|m| m.roles.detect {|role| role.assignable?}}.collect {|m| m.user}.sort
327 members.select {|m| m.roles.detect {|role| role.assignable?}}.collect {|m| m.user}.sort
328 end
328 end
329
329
330 # Returns the mail adresses of users that should be always notified on project events
330 # Returns the mail adresses of users that should be always notified on project events
331 def recipients
331 def recipients
332 members.select {|m| m.mail_notification? || m.user.mail_notification?}.collect {|m| m.user.mail}
332 members.select {|m| m.mail_notification? || m.user.mail_notification?}.collect {|m| m.user.mail}
333 end
333 end
334
334
335 # Returns an array of all custom fields enabled for project issues
335 # Returns an array of all custom fields enabled for project issues
336 # (explictly associated custom fields and custom fields enabled for all projects)
336 # (explictly associated custom fields and custom fields enabled for all projects)
337 def all_issue_custom_fields
337 def all_issue_custom_fields
338 @all_issue_custom_fields ||= (IssueCustomField.for_all + issue_custom_fields).uniq.sort
338 @all_issue_custom_fields ||= (IssueCustomField.for_all + issue_custom_fields).uniq.sort
339 end
339 end
340
340
341 def project
341 def project
342 self
342 self
343 end
343 end
344
344
345 def <=>(project)
345 def <=>(project)
346 name.downcase <=> project.name.downcase
346 name.downcase <=> project.name.downcase
347 end
347 end
348
348
349 def to_s
349 def to_s
350 name
350 name
351 end
351 end
352
352
353 # Returns a short description of the projects (first lines)
353 # Returns a short description of the projects (first lines)
354 def short_description(length = 255)
354 def short_description(length = 255)
355 description.gsub(/^(.{#{length}}[^\n\r]*).*$/m, '\1...').strip if description
355 description.gsub(/^(.{#{length}}[^\n\r]*).*$/m, '\1...').strip if description
356 end
356 end
357
357
358 # Return true if this project is allowed to do the specified action.
358 # Return true if this project is allowed to do the specified action.
359 # action can be:
359 # action can be:
360 # * a parameter-like Hash (eg. :controller => 'projects', :action => 'edit')
360 # * a parameter-like Hash (eg. :controller => 'projects', :action => 'edit')
361 # * a permission Symbol (eg. :edit_project)
361 # * a permission Symbol (eg. :edit_project)
362 def allows_to?(action)
362 def allows_to?(action)
363 if action.is_a? Hash
363 if action.is_a? Hash
364 allowed_actions.include? "#{action[:controller]}/#{action[:action]}"
364 allowed_actions.include? "#{action[:controller]}/#{action[:action]}"
365 else
365 else
366 allowed_permissions.include? action
366 allowed_permissions.include? action
367 end
367 end
368 end
368 end
369
369
370 def module_enabled?(module_name)
370 def module_enabled?(module_name)
371 module_name = module_name.to_s
371 module_name = module_name.to_s
372 enabled_modules.detect {|m| m.name == module_name}
372 enabled_modules.detect {|m| m.name == module_name}
373 end
373 end
374
374
375 def enabled_module_names=(module_names)
375 def enabled_module_names=(module_names)
376 if module_names && module_names.is_a?(Array)
376 if module_names && module_names.is_a?(Array)
377 module_names = module_names.collect(&:to_s)
377 module_names = module_names.collect(&:to_s)
378 # remove disabled modules
378 # remove disabled modules
379 enabled_modules.each {|mod| mod.destroy unless module_names.include?(mod.name)}
379 enabled_modules.each {|mod| mod.destroy unless module_names.include?(mod.name)}
380 # add new modules
380 # add new modules
381 module_names.each {|name| enabled_modules << EnabledModule.new(:name => name)}
381 module_names.each {|name| enabled_modules << EnabledModule.new(:name => name)}
382 else
382 else
383 enabled_modules.clear
383 enabled_modules.clear
384 end
384 end
385 end
385 end
386
386
387 # Returns an auto-generated project identifier based on the last identifier used
387 # Returns an auto-generated project identifier based on the last identifier used
388 def self.next_identifier
388 def self.next_identifier
389 p = Project.find(:first, :order => 'created_on DESC')
389 p = Project.find(:first, :order => 'created_on DESC')
390 p.nil? ? nil : p.identifier.to_s.succ
390 p.nil? ? nil : p.identifier.to_s.succ
391 end
391 end
392
392
393 # Copies and saves the Project instance based on the +project+.
393 # Copies and saves the Project instance based on the +project+.
394 # Duplicates the source project's:
394 # Duplicates the source project's:
395 # * Wiki
395 # * Wiki
396 # * Versions
396 # * Versions
397 # * Categories
397 # * Categories
398 # * Issues
398 # * Issues
399 # * Members
399 # * Members
400 # * Queries
400 # * Queries
401 #
401 #
402 # Accepts an +options+ argument to specify what to copy
402 # Accepts an +options+ argument to specify what to copy
403 #
403 #
404 # Examples:
404 # Examples:
405 # project.copy(1) # => copies everything
405 # project.copy(1) # => copies everything
406 # project.copy(1, :only => 'members') # => copies members only
406 # project.copy(1, :only => 'members') # => copies members only
407 # project.copy(1, :only => ['members', 'versions']) # => copies members and versions
407 # project.copy(1, :only => ['members', 'versions']) # => copies members and versions
408 def copy(project, options={})
408 def copy(project, options={})
409 project = project.is_a?(Project) ? project : Project.find(project)
409 project = project.is_a?(Project) ? project : Project.find(project)
410
410
411 to_be_copied = %w(wiki versions issue_categories issues members queries)
411 to_be_copied = %w(wiki versions issue_categories issues members queries)
412 to_be_copied = to_be_copied & options[:only].to_a unless options[:only].nil?
412 to_be_copied = to_be_copied & options[:only].to_a unless options[:only].nil?
413
413
414 Project.transaction do
414 Project.transaction do
415 to_be_copied.each do |name|
415 if save
416 send "copy_#{name}", project
416 reload
417 to_be_copied.each do |name|
418 send "copy_#{name}", project
419 end
420 Redmine::Hook.call_hook(:model_project_copy_before_save, :source_project => project, :destination_project => self)
421 save
417 end
422 end
418 Redmine::Hook.call_hook(:model_project_copy_before_save, :source_project => project, :destination_project => self)
419 self.save
420 end
423 end
421 end
424 end
422
425
423
426
424 # Copies +project+ and returns the new instance. This will not save
427 # Copies +project+ and returns the new instance. This will not save
425 # the copy
428 # the copy
426 def self.copy_from(project)
429 def self.copy_from(project)
427 begin
430 begin
428 project = project.is_a?(Project) ? project : Project.find(project)
431 project = project.is_a?(Project) ? project : Project.find(project)
429 if project
432 if project
430 # clear unique attributes
433 # clear unique attributes
431 attributes = project.attributes.dup.except('name', 'identifier', 'id', 'status')
434 attributes = project.attributes.dup.except('name', 'identifier', 'id', 'status')
432 copy = Project.new(attributes)
435 copy = Project.new(attributes)
433 copy.enabled_modules = project.enabled_modules
436 copy.enabled_modules = project.enabled_modules
434 copy.trackers = project.trackers
437 copy.trackers = project.trackers
435 copy.custom_values = project.custom_values.collect {|v| v.clone}
438 copy.custom_values = project.custom_values.collect {|v| v.clone}
436 copy.issue_custom_fields = project.issue_custom_fields
439 copy.issue_custom_fields = project.issue_custom_fields
437 return copy
440 return copy
438 else
441 else
439 return nil
442 return nil
440 end
443 end
441 rescue ActiveRecord::RecordNotFound
444 rescue ActiveRecord::RecordNotFound
442 return nil
445 return nil
443 end
446 end
444 end
447 end
445
448
446 private
449 private
447
450
448 # Copies wiki from +project+
451 # Copies wiki from +project+
449 def copy_wiki(project)
452 def copy_wiki(project)
450 self.wiki = Wiki.new(project.wiki.attributes.dup.except("project_id"))
453 # Check that the source project has a wiki first
451 project.wiki.pages.each do |page|
454 unless project.wiki.nil?
452 new_wiki_content = WikiContent.new(page.content.attributes.dup.except("page_id"))
455 self.wiki ||= Wiki.new
453 new_wiki_page = WikiPage.new(page.attributes.dup.except("wiki_id"))
456 wiki.attributes = project.wiki.attributes.dup.except("project_id")
454 new_wiki_page.content = new_wiki_content
457 project.wiki.pages.each do |page|
455 self.wiki.pages << new_wiki_page
458 new_wiki_content = WikiContent.new(page.content.attributes.dup.except("page_id"))
459 new_wiki_page = WikiPage.new(page.attributes.dup.except("wiki_id"))
460 new_wiki_page.content = new_wiki_content
461 wiki.pages << new_wiki_page
462 end
456 end
463 end
457 end
464 end
458
465
459 # Copies versions from +project+
466 # Copies versions from +project+
460 def copy_versions(project)
467 def copy_versions(project)
461 project.versions.each do |version|
468 project.versions.each do |version|
462 new_version = Version.new
469 new_version = Version.new
463 new_version.attributes = version.attributes.dup.except("project_id")
470 new_version.attributes = version.attributes.dup.except("project_id")
464 self.versions << new_version
471 self.versions << new_version
465 end
472 end
466 end
473 end
467
474
468 # Copies issue categories from +project+
475 # Copies issue categories from +project+
469 def copy_issue_categories(project)
476 def copy_issue_categories(project)
470 project.issue_categories.each do |issue_category|
477 project.issue_categories.each do |issue_category|
471 new_issue_category = IssueCategory.new
478 new_issue_category = IssueCategory.new
472 new_issue_category.attributes = issue_category.attributes.dup.except("project_id")
479 new_issue_category.attributes = issue_category.attributes.dup.except("project_id")
473 self.issue_categories << new_issue_category
480 self.issue_categories << new_issue_category
474 end
481 end
475 end
482 end
476
483
477 # Copies issues from +project+
484 # Copies issues from +project+
478 def copy_issues(project)
485 def copy_issues(project)
479 project.issues.each do |issue|
486 project.issues.each do |issue|
480 new_issue = Issue.new
487 new_issue = Issue.new
481 new_issue.copy_from(issue)
488 new_issue.copy_from(issue)
482 # Reassign fixed_versions by name, since names are unique per
489 # Reassign fixed_versions by name, since names are unique per
483 # project and the versions for self are not yet saved
490 # project and the versions for self are not yet saved
484 if issue.fixed_version
491 if issue.fixed_version
485 new_issue.fixed_version = self.versions.select {|v| v.name == issue.fixed_version.name}.first
492 new_issue.fixed_version = self.versions.select {|v| v.name == issue.fixed_version.name}.first
486 end
493 end
487 # Reassign the category by name, since names are unique per
494 # Reassign the category by name, since names are unique per
488 # project and the categories for self are not yet saved
495 # project and the categories for self are not yet saved
489 if issue.category
496 if issue.category
490 new_issue.category = self.issue_categories.select {|c| c.name == issue.category.name}.first
497 new_issue.category = self.issue_categories.select {|c| c.name == issue.category.name}.first
491 end
498 end
492 self.issues << new_issue
499 self.issues << new_issue
493 end
500 end
494 end
501 end
495
502
496 # Copies members from +project+
503 # Copies members from +project+
497 def copy_members(project)
504 def copy_members(project)
498 project.members.each do |member|
505 project.members.each do |member|
499 new_member = Member.new
506 new_member = Member.new
500 new_member.attributes = member.attributes.dup.except("project_id")
507 new_member.attributes = member.attributes.dup.except("project_id")
501 new_member.role_ids = member.role_ids.dup
508 new_member.role_ids = member.role_ids.dup
502 new_member.project = self
509 new_member.project = self
503 self.members << new_member
510 self.members << new_member
504 end
511 end
505 end
512 end
506
513
507 # Copies queries from +project+
514 # Copies queries from +project+
508 def copy_queries(project)
515 def copy_queries(project)
509 project.queries.each do |query|
516 project.queries.each do |query|
510 new_query = Query.new
517 new_query = Query.new
511 new_query.attributes = query.attributes.dup.except("project_id", "sort_criteria")
518 new_query.attributes = query.attributes.dup.except("project_id", "sort_criteria")
512 new_query.sort_criteria = query.sort_criteria if query.sort_criteria
519 new_query.sort_criteria = query.sort_criteria if query.sort_criteria
513 new_query.project = self
520 new_query.project = self
514 self.queries << new_query
521 self.queries << new_query
515 end
522 end
516 end
523 end
517
524
518 def allowed_permissions
525 def allowed_permissions
519 @allowed_permissions ||= begin
526 @allowed_permissions ||= begin
520 module_names = enabled_modules.collect {|m| m.name}
527 module_names = enabled_modules.collect {|m| m.name}
521 Redmine::AccessControl.modules_permissions(module_names).collect {|p| p.name}
528 Redmine::AccessControl.modules_permissions(module_names).collect {|p| p.name}
522 end
529 end
523 end
530 end
524
531
525 def allowed_actions
532 def allowed_actions
526 @actions_allowed ||= allowed_permissions.inject([]) { |actions, permission| actions += Redmine::AccessControl.allowed_actions(permission) }.flatten
533 @actions_allowed ||= allowed_permissions.inject([]) { |actions, permission| actions += Redmine::AccessControl.allowed_actions(permission) }.flatten
527 end
534 end
528
535
529 # Returns all the active Systemwide and project specific activities
536 # Returns all the active Systemwide and project specific activities
530 def active_activities
537 def active_activities
531 overridden_activity_ids = self.time_entry_activities.active.collect(&:parent_id)
538 overridden_activity_ids = self.time_entry_activities.active.collect(&:parent_id)
532
539
533 if overridden_activity_ids.empty?
540 if overridden_activity_ids.empty?
534 return TimeEntryActivity.active
541 return TimeEntryActivity.active
535 else
542 else
536 return system_activities_and_project_overrides
543 return system_activities_and_project_overrides
537 end
544 end
538 end
545 end
539
546
540 # Returns all the Systemwide and project specific activities
547 # Returns all the Systemwide and project specific activities
541 # (inactive and active)
548 # (inactive and active)
542 def all_activities
549 def all_activities
543 overridden_activity_ids = self.time_entry_activities.collect(&:parent_id)
550 overridden_activity_ids = self.time_entry_activities.collect(&:parent_id)
544
551
545 if overridden_activity_ids.empty?
552 if overridden_activity_ids.empty?
546 return TimeEntryActivity.all
553 return TimeEntryActivity.all
547 else
554 else
548 return system_activities_and_project_overrides(true)
555 return system_activities_and_project_overrides(true)
549 end
556 end
550 end
557 end
551
558
552 # Returns the systemwide active activities merged with the project specific overrides
559 # Returns the systemwide active activities merged with the project specific overrides
553 def system_activities_and_project_overrides(include_inactive=false)
560 def system_activities_and_project_overrides(include_inactive=false)
554 if include_inactive
561 if include_inactive
555 return TimeEntryActivity.all.
562 return TimeEntryActivity.all.
556 find(:all,
563 find(:all,
557 :conditions => ["id NOT IN (?)", self.time_entry_activities.collect(&:parent_id)]) +
564 :conditions => ["id NOT IN (?)", self.time_entry_activities.collect(&:parent_id)]) +
558 self.time_entry_activities
565 self.time_entry_activities
559 else
566 else
560 return TimeEntryActivity.active.
567 return TimeEntryActivity.active.
561 find(:all,
568 find(:all,
562 :conditions => ["id NOT IN (?)", self.time_entry_activities.active.collect(&:parent_id)]) +
569 :conditions => ["id NOT IN (?)", self.time_entry_activities.active.collect(&:parent_id)]) +
563 self.time_entry_activities.active
570 self.time_entry_activities.active
564 end
571 end
565 end
572 end
566 end
573 end
General Comments 0
You need to be logged in to leave comments. Login now