##// END OF EJS Templates
Merged r11942 from trunk to 2.3-stable (#14242)...
Toshi MARUYAMA -
r11715:0badc79162bb
parent child
Show More
@@ -1,1029 +1,1029
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2013 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 class Project < ActiveRecord::Base
19 19 include Redmine::SafeAttributes
20 20
21 21 # Project statuses
22 22 STATUS_ACTIVE = 1
23 23 STATUS_CLOSED = 5
24 24 STATUS_ARCHIVED = 9
25 25
26 26 # Maximum length for project identifiers
27 27 IDENTIFIER_MAX_LENGTH = 100
28 28
29 29 # Specific overidden Activities
30 30 has_many :time_entry_activities
31 31 has_many :members, :include => [:principal, :roles], :conditions => "#{Principal.table_name}.type='User' AND #{Principal.table_name}.status=#{Principal::STATUS_ACTIVE}"
32 32 has_many :memberships, :class_name => 'Member'
33 33 has_many :member_principals, :class_name => 'Member',
34 34 :include => :principal,
35 35 :conditions => "#{Principal.table_name}.type='Group' OR (#{Principal.table_name}.type='User' AND #{Principal.table_name}.status=#{Principal::STATUS_ACTIVE})"
36 36 has_many :users, :through => :members
37 37 has_many :principals, :through => :member_principals, :source => :principal
38 38
39 39 has_many :enabled_modules, :dependent => :delete_all
40 40 has_and_belongs_to_many :trackers, :order => "#{Tracker.table_name}.position"
41 41 has_many :issues, :dependent => :destroy, :include => [:status, :tracker]
42 42 has_many :issue_changes, :through => :issues, :source => :journals
43 43 has_many :versions, :dependent => :destroy, :order => "#{Version.table_name}.effective_date DESC, #{Version.table_name}.name DESC"
44 44 has_many :time_entries, :dependent => :delete_all
45 45 has_many :queries, :class_name => 'IssueQuery', :dependent => :delete_all
46 46 has_many :documents, :dependent => :destroy
47 47 has_many :news, :dependent => :destroy, :include => :author
48 48 has_many :issue_categories, :dependent => :delete_all, :order => "#{IssueCategory.table_name}.name"
49 49 has_many :boards, :dependent => :destroy, :order => "position ASC"
50 50 has_one :repository, :conditions => ["is_default = ?", true]
51 51 has_many :repositories, :dependent => :destroy
52 52 has_many :changesets, :through => :repository
53 53 has_one :wiki, :dependent => :destroy
54 54 # Custom field for the project issues
55 55 has_and_belongs_to_many :issue_custom_fields,
56 56 :class_name => 'IssueCustomField',
57 57 :order => "#{CustomField.table_name}.position",
58 58 :join_table => "#{table_name_prefix}custom_fields_projects#{table_name_suffix}",
59 59 :association_foreign_key => 'custom_field_id'
60 60
61 61 acts_as_nested_set :order => 'name', :dependent => :destroy
62 62 acts_as_attachable :view_permission => :view_files,
63 63 :delete_permission => :manage_files
64 64
65 65 acts_as_customizable
66 66 acts_as_searchable :columns => ['name', 'identifier', 'description'], :project_key => 'id', :permission => nil
67 67 acts_as_event :title => Proc.new {|o| "#{l(:label_project)}: #{o.name}"},
68 68 :url => Proc.new {|o| {:controller => 'projects', :action => 'show', :id => o}},
69 69 :author => nil
70 70
71 71 attr_protected :status
72 72
73 73 validates_presence_of :name, :identifier
74 74 validates_uniqueness_of :identifier
75 75 validates_associated :repository, :wiki
76 76 validates_length_of :name, :maximum => 255
77 77 validates_length_of :homepage, :maximum => 255
78 78 validates_length_of :identifier, :in => 1..IDENTIFIER_MAX_LENGTH
79 79 # donwcase letters, digits, dashes but not digits only
80 80 validates_format_of :identifier, :with => /\A(?!\d+$)[a-z0-9\-_]*\z/, :if => Proc.new { |p| p.identifier_changed? }
81 81 # reserved words
82 82 validates_exclusion_of :identifier, :in => %w( new )
83 83
84 84 after_save :update_position_under_parent, :if => Proc.new {|project| project.name_changed?}
85 85 after_save :update_inherited_members, :if => Proc.new {|project| project.inherit_members_changed?}
86 86 before_destroy :delete_all_members
87 87
88 88 scope :has_module, lambda {|mod|
89 89 where("#{Project.table_name}.id IN (SELECT em.project_id FROM #{EnabledModule.table_name} em WHERE em.name=?)", mod.to_s)
90 90 }
91 91 scope :active, lambda { where(:status => STATUS_ACTIVE) }
92 92 scope :status, lambda {|arg| where(arg.blank? ? nil : {:status => arg.to_i}) }
93 93 scope :all_public, lambda { where(:is_public => true) }
94 94 scope :visible, lambda {|*args| where(Project.visible_condition(args.shift || User.current, *args)) }
95 95 scope :allowed_to, lambda {|*args|
96 96 user = User.current
97 97 permission = nil
98 98 if args.first.is_a?(Symbol)
99 99 permission = args.shift
100 100 else
101 101 user = args.shift
102 102 permission = args.shift
103 103 end
104 104 where(Project.allowed_to_condition(user, permission, *args))
105 105 }
106 106 scope :like, lambda {|arg|
107 107 if arg.blank?
108 108 where(nil)
109 109 else
110 110 pattern = "%#{arg.to_s.strip.downcase}%"
111 111 where("LOWER(identifier) LIKE :p OR LOWER(name) LIKE :p", :p => pattern)
112 112 end
113 113 }
114 114
115 115 def initialize(attributes=nil, *args)
116 116 super
117 117
118 118 initialized = (attributes || {}).stringify_keys
119 119 if !initialized.key?('identifier') && Setting.sequential_project_identifiers?
120 120 self.identifier = Project.next_identifier
121 121 end
122 122 if !initialized.key?('is_public')
123 123 self.is_public = Setting.default_projects_public?
124 124 end
125 125 if !initialized.key?('enabled_module_names')
126 126 self.enabled_module_names = Setting.default_projects_modules
127 127 end
128 128 if !initialized.key?('trackers') && !initialized.key?('tracker_ids')
129 129 default = Setting.default_projects_tracker_ids
130 130 if default.is_a?(Array)
131 131 self.trackers = Tracker.where(:id => default.map(&:to_i)).sorted.all
132 132 else
133 133 self.trackers = Tracker.sorted.all
134 134 end
135 135 end
136 136 end
137 137
138 138 def identifier=(identifier)
139 139 super unless identifier_frozen?
140 140 end
141 141
142 142 def identifier_frozen?
143 143 errors[:identifier].blank? && !(new_record? || identifier.blank?)
144 144 end
145 145
146 146 # returns latest created projects
147 147 # non public projects will be returned only if user is a member of those
148 148 def self.latest(user=nil, count=5)
149 149 visible(user).limit(count).order("created_on DESC").all
150 150 end
151 151
152 152 # Returns true if the project is visible to +user+ or to the current user.
153 153 def visible?(user=User.current)
154 154 user.allowed_to?(:view_project, self)
155 155 end
156 156
157 157 # Returns a SQL conditions string used to find all projects visible by the specified user.
158 158 #
159 159 # Examples:
160 160 # Project.visible_condition(admin) => "projects.status = 1"
161 161 # Project.visible_condition(normal_user) => "((projects.status = 1) AND (projects.is_public = 1 OR projects.id IN (1,3,4)))"
162 162 # Project.visible_condition(anonymous) => "((projects.status = 1) AND (projects.is_public = 1))"
163 163 def self.visible_condition(user, options={})
164 164 allowed_to_condition(user, :view_project, options)
165 165 end
166 166
167 167 # Returns a SQL conditions string used to find all projects for which +user+ has the given +permission+
168 168 #
169 169 # Valid options:
170 170 # * :project => limit the condition to project
171 171 # * :with_subprojects => limit the condition to project and its subprojects
172 172 # * :member => limit the condition to the user projects
173 173 def self.allowed_to_condition(user, permission, options={})
174 174 perm = Redmine::AccessControl.permission(permission)
175 175 base_statement = (perm && perm.read? ? "#{Project.table_name}.status <> #{Project::STATUS_ARCHIVED}" : "#{Project.table_name}.status = #{Project::STATUS_ACTIVE}")
176 176 if perm && perm.project_module
177 177 # If the permission belongs to a project module, make sure the module is enabled
178 178 base_statement << " AND #{Project.table_name}.id IN (SELECT em.project_id FROM #{EnabledModule.table_name} em WHERE em.name='#{perm.project_module}')"
179 179 end
180 180 if options[:project]
181 181 project_statement = "#{Project.table_name}.id = #{options[:project].id}"
182 182 project_statement << " OR (#{Project.table_name}.lft > #{options[:project].lft} AND #{Project.table_name}.rgt < #{options[:project].rgt})" if options[:with_subprojects]
183 183 base_statement = "(#{project_statement}) AND (#{base_statement})"
184 184 end
185 185
186 186 if user.admin?
187 187 base_statement
188 188 else
189 189 statement_by_role = {}
190 190 unless options[:member]
191 191 role = user.logged? ? Role.non_member : Role.anonymous
192 192 if role.allowed_to?(permission)
193 193 statement_by_role[role] = "#{Project.table_name}.is_public = #{connection.quoted_true}"
194 194 end
195 195 end
196 196 if user.logged?
197 197 user.projects_by_role.each do |role, projects|
198 198 if role.allowed_to?(permission) && projects.any?
199 199 statement_by_role[role] = "#{Project.table_name}.id IN (#{projects.collect(&:id).join(',')})"
200 200 end
201 201 end
202 202 end
203 203 if statement_by_role.empty?
204 204 "1=0"
205 205 else
206 206 if block_given?
207 207 statement_by_role.each do |role, statement|
208 208 if s = yield(role, user)
209 209 statement_by_role[role] = "(#{statement} AND (#{s}))"
210 210 end
211 211 end
212 212 end
213 213 "((#{base_statement}) AND (#{statement_by_role.values.join(' OR ')}))"
214 214 end
215 215 end
216 216 end
217 217
218 218 # Returns the Systemwide and project specific activities
219 219 def activities(include_inactive=false)
220 220 if include_inactive
221 221 return all_activities
222 222 else
223 223 return active_activities
224 224 end
225 225 end
226 226
227 227 # Will create a new Project specific Activity or update an existing one
228 228 #
229 229 # This will raise a ActiveRecord::Rollback if the TimeEntryActivity
230 230 # does not successfully save.
231 231 def update_or_create_time_entry_activity(id, activity_hash)
232 232 if activity_hash.respond_to?(:has_key?) && activity_hash.has_key?('parent_id')
233 233 self.create_time_entry_activity_if_needed(activity_hash)
234 234 else
235 235 activity = project.time_entry_activities.find_by_id(id.to_i)
236 236 activity.update_attributes(activity_hash) if activity
237 237 end
238 238 end
239 239
240 240 # Create a new TimeEntryActivity if it overrides a system TimeEntryActivity
241 241 #
242 242 # This will raise a ActiveRecord::Rollback if the TimeEntryActivity
243 243 # does not successfully save.
244 244 def create_time_entry_activity_if_needed(activity)
245 245 if activity['parent_id']
246 246
247 247 parent_activity = TimeEntryActivity.find(activity['parent_id'])
248 248 activity['name'] = parent_activity.name
249 249 activity['position'] = parent_activity.position
250 250
251 251 if Enumeration.overridding_change?(activity, parent_activity)
252 252 project_activity = self.time_entry_activities.create(activity)
253 253
254 254 if project_activity.new_record?
255 255 raise ActiveRecord::Rollback, "Overridding TimeEntryActivity was not successfully saved"
256 256 else
257 257 self.time_entries.update_all("activity_id = #{project_activity.id}", ["activity_id = ?", parent_activity.id])
258 258 end
259 259 end
260 260 end
261 261 end
262 262
263 263 # Returns a :conditions SQL string that can be used to find the issues associated with this project.
264 264 #
265 265 # Examples:
266 266 # project.project_condition(true) => "(projects.id = 1 OR (projects.lft > 1 AND projects.rgt < 10))"
267 267 # project.project_condition(false) => "projects.id = 1"
268 268 def project_condition(with_subprojects)
269 269 cond = "#{Project.table_name}.id = #{id}"
270 270 cond = "(#{cond} OR (#{Project.table_name}.lft > #{lft} AND #{Project.table_name}.rgt < #{rgt}))" if with_subprojects
271 271 cond
272 272 end
273 273
274 274 def self.find(*args)
275 275 if args.first && args.first.is_a?(String) && !args.first.match(/^\d*$/)
276 276 project = find_by_identifier(*args)
277 277 raise ActiveRecord::RecordNotFound, "Couldn't find Project with identifier=#{args.first}" if project.nil?
278 278 project
279 279 else
280 280 super
281 281 end
282 282 end
283 283
284 284 def self.find_by_param(*args)
285 285 self.find(*args)
286 286 end
287 287
288 288 alias :base_reload :reload
289 289 def reload(*args)
290 290 @shared_versions = nil
291 291 @rolled_up_versions = nil
292 292 @rolled_up_trackers = nil
293 293 @all_issue_custom_fields = nil
294 294 @all_time_entry_custom_fields = nil
295 295 @to_param = nil
296 296 @allowed_parents = nil
297 297 @allowed_permissions = nil
298 298 @actions_allowed = nil
299 299 @start_date = nil
300 300 @due_date = nil
301 301 base_reload(*args)
302 302 end
303 303
304 304 def to_param
305 305 # id is used for projects with a numeric identifier (compatibility)
306 306 @to_param ||= (identifier.to_s =~ %r{^\d*$} ? id.to_s : identifier)
307 307 end
308 308
309 309 def active?
310 310 self.status == STATUS_ACTIVE
311 311 end
312 312
313 313 def archived?
314 314 self.status == STATUS_ARCHIVED
315 315 end
316 316
317 317 # Archives the project and its descendants
318 318 def archive
319 319 # Check that there is no issue of a non descendant project that is assigned
320 320 # to one of the project or descendant versions
321 321 v_ids = self_and_descendants.collect {|p| p.version_ids}.flatten
322 322 if v_ids.any? &&
323 323 Issue.
324 324 includes(:project).
325 325 where("#{Project.table_name}.lft < ? OR #{Project.table_name}.rgt > ?", lft, rgt).
326 326 where("#{Issue.table_name}.fixed_version_id IN (?)", v_ids).
327 327 exists?
328 328 return false
329 329 end
330 330 Project.transaction do
331 331 archive!
332 332 end
333 333 true
334 334 end
335 335
336 336 # Unarchives the project
337 337 # All its ancestors must be active
338 338 def unarchive
339 339 return false if ancestors.detect {|a| !a.active?}
340 340 update_attribute :status, STATUS_ACTIVE
341 341 end
342 342
343 343 def close
344 344 self_and_descendants.status(STATUS_ACTIVE).update_all :status => STATUS_CLOSED
345 345 end
346 346
347 347 def reopen
348 348 self_and_descendants.status(STATUS_CLOSED).update_all :status => STATUS_ACTIVE
349 349 end
350 350
351 351 # Returns an array of projects the project can be moved to
352 352 # by the current user
353 353 def allowed_parents
354 354 return @allowed_parents if @allowed_parents
355 355 @allowed_parents = Project.where(Project.allowed_to_condition(User.current, :add_subprojects)).all
356 356 @allowed_parents = @allowed_parents - self_and_descendants
357 357 if User.current.allowed_to?(:add_project, nil, :global => true) || (!new_record? && parent.nil?)
358 358 @allowed_parents << nil
359 359 end
360 360 unless parent.nil? || @allowed_parents.empty? || @allowed_parents.include?(parent)
361 361 @allowed_parents << parent
362 362 end
363 363 @allowed_parents
364 364 end
365 365
366 366 # Sets the parent of the project with authorization check
367 367 def set_allowed_parent!(p)
368 368 unless p.nil? || p.is_a?(Project)
369 369 if p.to_s.blank?
370 370 p = nil
371 371 else
372 372 p = Project.find_by_id(p)
373 373 return false unless p
374 374 end
375 375 end
376 376 if p.nil?
377 377 if !new_record? && allowed_parents.empty?
378 378 return false
379 379 end
380 380 elsif !allowed_parents.include?(p)
381 381 return false
382 382 end
383 383 set_parent!(p)
384 384 end
385 385
386 386 # Sets the parent of the project
387 387 # Argument can be either a Project, a String, a Fixnum or nil
388 388 def set_parent!(p)
389 389 unless p.nil? || p.is_a?(Project)
390 390 if p.to_s.blank?
391 391 p = nil
392 392 else
393 393 p = Project.find_by_id(p)
394 394 return false unless p
395 395 end
396 396 end
397 397 if p == parent && !p.nil?
398 398 # Nothing to do
399 399 true
400 400 elsif p.nil? || (p.active? && move_possible?(p))
401 401 set_or_update_position_under(p)
402 402 Issue.update_versions_from_hierarchy_change(self)
403 403 true
404 404 else
405 405 # Can not move to the given target
406 406 false
407 407 end
408 408 end
409 409
410 410 # Recalculates all lft and rgt values based on project names
411 411 # Unlike Project.rebuild!, these values are recalculated even if the tree "looks" valid
412 412 # Used in BuildProjectsTree migration
413 413 def self.rebuild_tree!
414 414 transaction do
415 415 update_all "lft = NULL, rgt = NULL"
416 416 rebuild!(false)
417 417 end
418 418 end
419 419
420 420 # Returns an array of the trackers used by the project and its active sub projects
421 421 def rolled_up_trackers
422 422 @rolled_up_trackers ||=
423 423 Tracker.
424 424 joins(:projects).
425 425 joins("JOIN #{EnabledModule.table_name} ON #{EnabledModule.table_name}.project_id = #{Project.table_name}.id AND #{EnabledModule.table_name}.name = 'issue_tracking'").
426 426 select("DISTINCT #{Tracker.table_name}.*").
427 427 where("#{Project.table_name}.lft >= ? AND #{Project.table_name}.rgt <= ? AND #{Project.table_name}.status <> #{STATUS_ARCHIVED}", lft, rgt).
428 428 sorted.
429 429 all
430 430 end
431 431
432 432 # Closes open and locked project versions that are completed
433 433 def close_completed_versions
434 434 Version.transaction do
435 435 versions.where(:status => %w(open locked)).all.each do |version|
436 436 if version.completed?
437 437 version.update_attribute(:status, 'closed')
438 438 end
439 439 end
440 440 end
441 441 end
442 442
443 443 # Returns a scope of the Versions on subprojects
444 444 def rolled_up_versions
445 445 @rolled_up_versions ||=
446 446 Version.scoped(:include => :project,
447 447 :conditions => ["#{Project.table_name}.lft >= ? AND #{Project.table_name}.rgt <= ? AND #{Project.table_name}.status <> #{STATUS_ARCHIVED}", lft, rgt])
448 448 end
449 449
450 450 # Returns a scope of the Versions used by the project
451 451 def shared_versions
452 452 if new_record?
453 453 Version.scoped(:include => :project,
454 454 :conditions => "#{Project.table_name}.status <> #{Project::STATUS_ARCHIVED} AND #{Version.table_name}.sharing = 'system'")
455 455 else
456 456 @shared_versions ||= begin
457 457 r = root? ? self : root
458 458 Version.scoped(:include => :project,
459 459 :conditions => "#{Project.table_name}.id = #{id}" +
460 460 " OR (#{Project.table_name}.status <> #{Project::STATUS_ARCHIVED} AND (" +
461 461 " #{Version.table_name}.sharing = 'system'" +
462 462 " OR (#{Project.table_name}.lft >= #{r.lft} AND #{Project.table_name}.rgt <= #{r.rgt} AND #{Version.table_name}.sharing = 'tree')" +
463 463 " OR (#{Project.table_name}.lft < #{lft} AND #{Project.table_name}.rgt > #{rgt} AND #{Version.table_name}.sharing IN ('hierarchy', 'descendants'))" +
464 464 " OR (#{Project.table_name}.lft > #{lft} AND #{Project.table_name}.rgt < #{rgt} AND #{Version.table_name}.sharing = 'hierarchy')" +
465 465 "))")
466 466 end
467 467 end
468 468 end
469 469
470 470 # Returns a hash of project users grouped by role
471 471 def users_by_role
472 472 members.includes(:user, :roles).all.inject({}) do |h, m|
473 473 m.roles.each do |r|
474 474 h[r] ||= []
475 475 h[r] << m.user
476 476 end
477 477 h
478 478 end
479 479 end
480 480
481 481 # Deletes all project's members
482 482 def delete_all_members
483 483 me, mr = Member.table_name, MemberRole.table_name
484 484 connection.delete("DELETE FROM #{mr} WHERE #{mr}.member_id IN (SELECT #{me}.id FROM #{me} WHERE #{me}.project_id = #{id})")
485 485 Member.delete_all(['project_id = ?', id])
486 486 end
487 487
488 488 # Users/groups issues can be assigned to
489 489 def assignable_users
490 490 assignable = Setting.issue_group_assignment? ? member_principals : members
491 491 assignable.select {|m| m.roles.detect {|role| role.assignable?}}.collect {|m| m.principal}.sort
492 492 end
493 493
494 494 # Returns the mail adresses of users that should be always notified on project events
495 495 def recipients
496 496 notified_users.collect {|user| user.mail}
497 497 end
498 498
499 499 # Returns the users that should be notified on project events
500 500 def notified_users
501 501 # TODO: User part should be extracted to User#notify_about?
502 502 members.select {|m| m.principal.present? && (m.mail_notification? || m.principal.mail_notification == 'all')}.collect {|m| m.principal}
503 503 end
504 504
505 505 # Returns an array of all custom fields enabled for project issues
506 506 # (explictly associated custom fields and custom fields enabled for all projects)
507 507 def all_issue_custom_fields
508 508 @all_issue_custom_fields ||= (IssueCustomField.for_all + issue_custom_fields).uniq.sort
509 509 end
510 510
511 511 # Returns an array of all custom fields enabled for project time entries
512 512 # (explictly associated custom fields and custom fields enabled for all projects)
513 513 def all_time_entry_custom_fields
514 514 @all_time_entry_custom_fields ||= (TimeEntryCustomField.for_all + time_entry_custom_fields).uniq.sort
515 515 end
516 516
517 517 def project
518 518 self
519 519 end
520 520
521 521 def <=>(project)
522 522 name.downcase <=> project.name.downcase
523 523 end
524 524
525 525 def to_s
526 526 name
527 527 end
528 528
529 529 # Returns a short description of the projects (first lines)
530 530 def short_description(length = 255)
531 531 description.gsub(/^(.{#{length}}[^\n\r]*).*$/m, '\1...').strip if description
532 532 end
533 533
534 534 def css_classes
535 535 s = 'project'
536 536 s << ' root' if root?
537 537 s << ' child' if child?
538 538 s << (leaf? ? ' leaf' : ' parent')
539 539 unless active?
540 540 if archived?
541 541 s << ' archived'
542 542 else
543 543 s << ' closed'
544 544 end
545 545 end
546 546 s
547 547 end
548 548
549 549 # The earliest start date of a project, based on it's issues and versions
550 550 def start_date
551 551 @start_date ||= [
552 552 issues.minimum('start_date'),
553 553 shared_versions.minimum('effective_date'),
554 554 Issue.fixed_version(shared_versions).minimum('start_date')
555 555 ].compact.min
556 556 end
557 557
558 558 # The latest due date of an issue or version
559 559 def due_date
560 560 @due_date ||= [
561 561 issues.maximum('due_date'),
562 562 shared_versions.maximum('effective_date'),
563 563 Issue.fixed_version(shared_versions).maximum('due_date')
564 564 ].compact.max
565 565 end
566 566
567 567 def overdue?
568 568 active? && !due_date.nil? && (due_date < Date.today)
569 569 end
570 570
571 571 # Returns the percent completed for this project, based on the
572 572 # progress on it's versions.
573 573 def completed_percent(options={:include_subprojects => false})
574 574 if options.delete(:include_subprojects)
575 575 total = self_and_descendants.collect(&:completed_percent).sum
576 576
577 577 total / self_and_descendants.count
578 578 else
579 579 if versions.count > 0
580 580 total = versions.collect(&:completed_percent).sum
581 581
582 582 total / versions.count
583 583 else
584 584 100
585 585 end
586 586 end
587 587 end
588 588
589 589 # Return true if this project allows to do the specified action.
590 590 # action can be:
591 591 # * a parameter-like Hash (eg. :controller => 'projects', :action => 'edit')
592 592 # * a permission Symbol (eg. :edit_project)
593 593 def allows_to?(action)
594 594 if archived?
595 595 # No action allowed on archived projects
596 596 return false
597 597 end
598 598 unless active? || Redmine::AccessControl.read_action?(action)
599 599 # No write action allowed on closed projects
600 600 return false
601 601 end
602 602 # No action allowed on disabled modules
603 603 if action.is_a? Hash
604 604 allowed_actions.include? "#{action[:controller]}/#{action[:action]}"
605 605 else
606 606 allowed_permissions.include? action
607 607 end
608 608 end
609 609
610 610 def module_enabled?(module_name)
611 611 module_name = module_name.to_s
612 612 enabled_modules.detect {|m| m.name == module_name}
613 613 end
614 614
615 615 def enabled_module_names=(module_names)
616 616 if module_names && module_names.is_a?(Array)
617 617 module_names = module_names.collect(&:to_s).reject(&:blank?)
618 618 self.enabled_modules = module_names.collect {|name| enabled_modules.detect {|mod| mod.name == name} || EnabledModule.new(:name => name)}
619 619 else
620 620 enabled_modules.clear
621 621 end
622 622 end
623 623
624 624 # Returns an array of the enabled modules names
625 625 def enabled_module_names
626 626 enabled_modules.collect(&:name)
627 627 end
628 628
629 629 # Enable a specific module
630 630 #
631 631 # Examples:
632 632 # project.enable_module!(:issue_tracking)
633 633 # project.enable_module!("issue_tracking")
634 634 def enable_module!(name)
635 635 enabled_modules << EnabledModule.new(:name => name.to_s) unless module_enabled?(name)
636 636 end
637 637
638 638 # Disable a module if it exists
639 639 #
640 640 # Examples:
641 641 # project.disable_module!(:issue_tracking)
642 642 # project.disable_module!("issue_tracking")
643 643 # project.disable_module!(project.enabled_modules.first)
644 644 def disable_module!(target)
645 645 target = enabled_modules.detect{|mod| target.to_s == mod.name} unless enabled_modules.include?(target)
646 646 target.destroy unless target.blank?
647 647 end
648 648
649 649 safe_attributes 'name',
650 650 'description',
651 651 'homepage',
652 652 'is_public',
653 653 'identifier',
654 654 'custom_field_values',
655 655 'custom_fields',
656 656 'tracker_ids',
657 657 'issue_custom_field_ids'
658 658
659 659 safe_attributes 'enabled_module_names',
660 660 :if => lambda {|project, user| project.new_record? || user.allowed_to?(:select_project_modules, project) }
661 661
662 662 safe_attributes 'inherit_members',
663 663 :if => lambda {|project, user| project.parent.nil? || project.parent.visible?(user)}
664 664
665 665 # Returns an array of projects that are in this project's hierarchy
666 666 #
667 667 # Example: parents, children, siblings
668 668 def hierarchy
669 669 parents = project.self_and_ancestors || []
670 670 descendants = project.descendants || []
671 671 project_hierarchy = parents | descendants # Set union
672 672 end
673 673
674 674 # Returns an auto-generated project identifier based on the last identifier used
675 675 def self.next_identifier
676 p = Project.order('created_on DESC').first
676 p = Project.order('id DESC').first
677 677 p.nil? ? nil : p.identifier.to_s.succ
678 678 end
679 679
680 680 # Copies and saves the Project instance based on the +project+.
681 681 # Duplicates the source project's:
682 682 # * Wiki
683 683 # * Versions
684 684 # * Categories
685 685 # * Issues
686 686 # * Members
687 687 # * Queries
688 688 #
689 689 # Accepts an +options+ argument to specify what to copy
690 690 #
691 691 # Examples:
692 692 # project.copy(1) # => copies everything
693 693 # project.copy(1, :only => 'members') # => copies members only
694 694 # project.copy(1, :only => ['members', 'versions']) # => copies members and versions
695 695 def copy(project, options={})
696 696 project = project.is_a?(Project) ? project : Project.find(project)
697 697
698 698 to_be_copied = %w(wiki versions issue_categories issues members queries boards)
699 699 to_be_copied = to_be_copied & options[:only].to_a unless options[:only].nil?
700 700
701 701 Project.transaction do
702 702 if save
703 703 reload
704 704 to_be_copied.each do |name|
705 705 send "copy_#{name}", project
706 706 end
707 707 Redmine::Hook.call_hook(:model_project_copy_before_save, :source_project => project, :destination_project => self)
708 708 save
709 709 end
710 710 end
711 711 end
712 712
713 713 # Returns a new unsaved Project instance with attributes copied from +project+
714 714 def self.copy_from(project)
715 715 project = project.is_a?(Project) ? project : Project.find(project)
716 716 # clear unique attributes
717 717 attributes = project.attributes.dup.except('id', 'name', 'identifier', 'status', 'parent_id', 'lft', 'rgt')
718 718 copy = Project.new(attributes)
719 719 copy.enabled_modules = project.enabled_modules
720 720 copy.trackers = project.trackers
721 721 copy.custom_values = project.custom_values.collect {|v| v.clone}
722 722 copy.issue_custom_fields = project.issue_custom_fields
723 723 copy
724 724 end
725 725
726 726 # Yields the given block for each project with its level in the tree
727 727 def self.project_tree(projects, &block)
728 728 ancestors = []
729 729 projects.sort_by(&:lft).each do |project|
730 730 while (ancestors.any? && !project.is_descendant_of?(ancestors.last))
731 731 ancestors.pop
732 732 end
733 733 yield project, ancestors.size
734 734 ancestors << project
735 735 end
736 736 end
737 737
738 738 private
739 739
740 740 def after_parent_changed(parent_was)
741 741 remove_inherited_member_roles
742 742 add_inherited_member_roles
743 743 end
744 744
745 745 def update_inherited_members
746 746 if parent
747 747 if inherit_members? && !inherit_members_was
748 748 remove_inherited_member_roles
749 749 add_inherited_member_roles
750 750 elsif !inherit_members? && inherit_members_was
751 751 remove_inherited_member_roles
752 752 end
753 753 end
754 754 end
755 755
756 756 def remove_inherited_member_roles
757 757 member_roles = memberships.map(&:member_roles).flatten
758 758 member_role_ids = member_roles.map(&:id)
759 759 member_roles.each do |member_role|
760 760 if member_role.inherited_from && !member_role_ids.include?(member_role.inherited_from)
761 761 member_role.destroy
762 762 end
763 763 end
764 764 end
765 765
766 766 def add_inherited_member_roles
767 767 if inherit_members? && parent
768 768 parent.memberships.each do |parent_member|
769 769 member = Member.find_or_new(self.id, parent_member.user_id)
770 770 parent_member.member_roles.each do |parent_member_role|
771 771 member.member_roles << MemberRole.new(:role => parent_member_role.role, :inherited_from => parent_member_role.id)
772 772 end
773 773 member.save!
774 774 end
775 775 end
776 776 end
777 777
778 778 # Copies wiki from +project+
779 779 def copy_wiki(project)
780 780 # Check that the source project has a wiki first
781 781 unless project.wiki.nil?
782 782 wiki = self.wiki || Wiki.new
783 783 wiki.attributes = project.wiki.attributes.dup.except("id", "project_id")
784 784 wiki_pages_map = {}
785 785 project.wiki.pages.each do |page|
786 786 # Skip pages without content
787 787 next if page.content.nil?
788 788 new_wiki_content = WikiContent.new(page.content.attributes.dup.except("id", "page_id", "updated_on"))
789 789 new_wiki_page = WikiPage.new(page.attributes.dup.except("id", "wiki_id", "created_on", "parent_id"))
790 790 new_wiki_page.content = new_wiki_content
791 791 wiki.pages << new_wiki_page
792 792 wiki_pages_map[page.id] = new_wiki_page
793 793 end
794 794
795 795 self.wiki = wiki
796 796 wiki.save
797 797 # Reproduce page hierarchy
798 798 project.wiki.pages.each do |page|
799 799 if page.parent_id && wiki_pages_map[page.id]
800 800 wiki_pages_map[page.id].parent = wiki_pages_map[page.parent_id]
801 801 wiki_pages_map[page.id].save
802 802 end
803 803 end
804 804 end
805 805 end
806 806
807 807 # Copies versions from +project+
808 808 def copy_versions(project)
809 809 project.versions.each do |version|
810 810 new_version = Version.new
811 811 new_version.attributes = version.attributes.dup.except("id", "project_id", "created_on", "updated_on")
812 812 self.versions << new_version
813 813 end
814 814 end
815 815
816 816 # Copies issue categories from +project+
817 817 def copy_issue_categories(project)
818 818 project.issue_categories.each do |issue_category|
819 819 new_issue_category = IssueCategory.new
820 820 new_issue_category.attributes = issue_category.attributes.dup.except("id", "project_id")
821 821 self.issue_categories << new_issue_category
822 822 end
823 823 end
824 824
825 825 # Copies issues from +project+
826 826 def copy_issues(project)
827 827 # Stores the source issue id as a key and the copied issues as the
828 828 # value. Used to map the two togeather for issue relations.
829 829 issues_map = {}
830 830
831 831 # Store status and reopen locked/closed versions
832 832 version_statuses = versions.reject(&:open?).map {|version| [version, version.status]}
833 833 version_statuses.each do |version, status|
834 834 version.update_attribute :status, 'open'
835 835 end
836 836
837 837 # Get issues sorted by root_id, lft so that parent issues
838 838 # get copied before their children
839 839 project.issues.reorder('root_id, lft').all.each do |issue|
840 840 new_issue = Issue.new
841 841 new_issue.copy_from(issue, :subtasks => false, :link => false)
842 842 new_issue.project = self
843 843 # Changing project resets the custom field values
844 844 # TODO: handle this in Issue#project=
845 845 new_issue.custom_field_values = issue.custom_field_values.inject({}) {|h,v| h[v.custom_field_id] = v.value; h}
846 846 # Reassign fixed_versions by name, since names are unique per project
847 847 if issue.fixed_version && issue.fixed_version.project == project
848 848 new_issue.fixed_version = self.versions.detect {|v| v.name == issue.fixed_version.name}
849 849 end
850 850 # Reassign the category by name, since names are unique per project
851 851 if issue.category
852 852 new_issue.category = self.issue_categories.detect {|c| c.name == issue.category.name}
853 853 end
854 854 # Parent issue
855 855 if issue.parent_id
856 856 if copied_parent = issues_map[issue.parent_id]
857 857 new_issue.parent_issue_id = copied_parent.id
858 858 end
859 859 end
860 860
861 861 self.issues << new_issue
862 862 if new_issue.new_record?
863 863 logger.info "Project#copy_issues: issue ##{issue.id} could not be copied: #{new_issue.errors.full_messages}" if logger && logger.info
864 864 else
865 865 issues_map[issue.id] = new_issue unless new_issue.new_record?
866 866 end
867 867 end
868 868
869 869 # Restore locked/closed version statuses
870 870 version_statuses.each do |version, status|
871 871 version.update_attribute :status, status
872 872 end
873 873
874 874 # Relations after in case issues related each other
875 875 project.issues.each do |issue|
876 876 new_issue = issues_map[issue.id]
877 877 unless new_issue
878 878 # Issue was not copied
879 879 next
880 880 end
881 881
882 882 # Relations
883 883 issue.relations_from.each do |source_relation|
884 884 new_issue_relation = IssueRelation.new
885 885 new_issue_relation.attributes = source_relation.attributes.dup.except("id", "issue_from_id", "issue_to_id")
886 886 new_issue_relation.issue_to = issues_map[source_relation.issue_to_id]
887 887 if new_issue_relation.issue_to.nil? && Setting.cross_project_issue_relations?
888 888 new_issue_relation.issue_to = source_relation.issue_to
889 889 end
890 890 new_issue.relations_from << new_issue_relation
891 891 end
892 892
893 893 issue.relations_to.each do |source_relation|
894 894 new_issue_relation = IssueRelation.new
895 895 new_issue_relation.attributes = source_relation.attributes.dup.except("id", "issue_from_id", "issue_to_id")
896 896 new_issue_relation.issue_from = issues_map[source_relation.issue_from_id]
897 897 if new_issue_relation.issue_from.nil? && Setting.cross_project_issue_relations?
898 898 new_issue_relation.issue_from = source_relation.issue_from
899 899 end
900 900 new_issue.relations_to << new_issue_relation
901 901 end
902 902 end
903 903 end
904 904
905 905 # Copies members from +project+
906 906 def copy_members(project)
907 907 # Copy users first, then groups to handle members with inherited and given roles
908 908 members_to_copy = []
909 909 members_to_copy += project.memberships.select {|m| m.principal.is_a?(User)}
910 910 members_to_copy += project.memberships.select {|m| !m.principal.is_a?(User)}
911 911
912 912 members_to_copy.each do |member|
913 913 new_member = Member.new
914 914 new_member.attributes = member.attributes.dup.except("id", "project_id", "created_on")
915 915 # only copy non inherited roles
916 916 # inherited roles will be added when copying the group membership
917 917 role_ids = member.member_roles.reject(&:inherited?).collect(&:role_id)
918 918 next if role_ids.empty?
919 919 new_member.role_ids = role_ids
920 920 new_member.project = self
921 921 self.members << new_member
922 922 end
923 923 end
924 924
925 925 # Copies queries from +project+
926 926 def copy_queries(project)
927 927 project.queries.each do |query|
928 928 new_query = IssueQuery.new
929 929 new_query.attributes = query.attributes.dup.except("id", "project_id", "sort_criteria")
930 930 new_query.sort_criteria = query.sort_criteria if query.sort_criteria
931 931 new_query.project = self
932 932 new_query.user_id = query.user_id
933 933 self.queries << new_query
934 934 end
935 935 end
936 936
937 937 # Copies boards from +project+
938 938 def copy_boards(project)
939 939 project.boards.each do |board|
940 940 new_board = Board.new
941 941 new_board.attributes = board.attributes.dup.except("id", "project_id", "topics_count", "messages_count", "last_message_id")
942 942 new_board.project = self
943 943 self.boards << new_board
944 944 end
945 945 end
946 946
947 947 def allowed_permissions
948 948 @allowed_permissions ||= begin
949 949 module_names = enabled_modules.all(:select => :name).collect {|m| m.name}
950 950 Redmine::AccessControl.modules_permissions(module_names).collect {|p| p.name}
951 951 end
952 952 end
953 953
954 954 def allowed_actions
955 955 @actions_allowed ||= allowed_permissions.inject([]) { |actions, permission| actions += Redmine::AccessControl.allowed_actions(permission) }.flatten
956 956 end
957 957
958 958 # Returns all the active Systemwide and project specific activities
959 959 def active_activities
960 960 overridden_activity_ids = self.time_entry_activities.collect(&:parent_id)
961 961
962 962 if overridden_activity_ids.empty?
963 963 return TimeEntryActivity.shared.active
964 964 else
965 965 return system_activities_and_project_overrides
966 966 end
967 967 end
968 968
969 969 # Returns all the Systemwide and project specific activities
970 970 # (inactive and active)
971 971 def all_activities
972 972 overridden_activity_ids = self.time_entry_activities.collect(&:parent_id)
973 973
974 974 if overridden_activity_ids.empty?
975 975 return TimeEntryActivity.shared
976 976 else
977 977 return system_activities_and_project_overrides(true)
978 978 end
979 979 end
980 980
981 981 # Returns the systemwide active activities merged with the project specific overrides
982 982 def system_activities_and_project_overrides(include_inactive=false)
983 983 if include_inactive
984 984 return TimeEntryActivity.shared.
985 985 where("id NOT IN (?)", self.time_entry_activities.collect(&:parent_id)).all +
986 986 self.time_entry_activities
987 987 else
988 988 return TimeEntryActivity.shared.active.
989 989 where("id NOT IN (?)", self.time_entry_activities.collect(&:parent_id)).all +
990 990 self.time_entry_activities.active
991 991 end
992 992 end
993 993
994 994 # Archives subprojects recursively
995 995 def archive!
996 996 children.each do |subproject|
997 997 subproject.send :archive!
998 998 end
999 999 update_attribute :status, STATUS_ARCHIVED
1000 1000 end
1001 1001
1002 1002 def update_position_under_parent
1003 1003 set_or_update_position_under(parent)
1004 1004 end
1005 1005
1006 1006 # Inserts/moves the project so that target's children or root projects stay alphabetically sorted
1007 1007 def set_or_update_position_under(target_parent)
1008 1008 parent_was = parent
1009 1009 sibs = (target_parent.nil? ? self.class.roots : target_parent.children)
1010 1010 to_be_inserted_before = sibs.sort_by {|c| c.name.to_s.downcase}.detect {|c| c.name.to_s.downcase > name.to_s.downcase }
1011 1011
1012 1012 if to_be_inserted_before
1013 1013 move_to_left_of(to_be_inserted_before)
1014 1014 elsif target_parent.nil?
1015 1015 if sibs.empty?
1016 1016 # move_to_root adds the project in first (ie. left) position
1017 1017 move_to_root
1018 1018 else
1019 1019 move_to_right_of(sibs.last) unless self == sibs.last
1020 1020 end
1021 1021 else
1022 1022 # move_to_child_of adds the project in last (ie.right) position
1023 1023 move_to_child_of(target_parent)
1024 1024 end
1025 1025 if parent_was != target_parent
1026 1026 after_parent_changed(parent_was)
1027 1027 end
1028 1028 end
1029 1029 end
General Comments 0
You need to be logged in to leave comments. Login now