##// END OF EJS Templates
Fixed: projects are referenced by id in search results....
Jean-Philippe Lang -
r3368:46ecc488a997
parent child
Show More
@@ -1,696 +1,696
1 1 # redMine - project management software
2 2 # Copyright (C) 2006 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 # Project statuses
20 20 STATUS_ACTIVE = 1
21 21 STATUS_ARCHIVED = 9
22 22
23 23 # Specific overidden Activities
24 24 has_many :time_entry_activities
25 25 has_many :members, :include => [:user, :roles], :conditions => "#{User.table_name}.type='User' AND #{User.table_name}.status=#{User::STATUS_ACTIVE}"
26 26 has_many :memberships, :class_name => 'Member'
27 27 has_many :member_principals, :class_name => 'Member',
28 28 :include => :principal,
29 29 :conditions => "#{Principal.table_name}.type='Group' OR (#{Principal.table_name}.type='User' AND #{Principal.table_name}.status=#{User::STATUS_ACTIVE})"
30 30 has_many :users, :through => :members
31 31 has_many :principals, :through => :member_principals, :source => :principal
32 32
33 33 has_many :enabled_modules, :dependent => :delete_all
34 34 has_and_belongs_to_many :trackers, :order => "#{Tracker.table_name}.position"
35 35 has_many :issues, :dependent => :destroy, :order => "#{Issue.table_name}.created_on DESC", :include => [:status, :tracker]
36 36 has_many :issue_changes, :through => :issues, :source => :journals
37 37 has_many :versions, :dependent => :destroy, :order => "#{Version.table_name}.effective_date DESC, #{Version.table_name}.name DESC"
38 38 has_many :time_entries, :dependent => :delete_all
39 39 has_many :queries, :dependent => :delete_all
40 40 has_many :documents, :dependent => :destroy
41 41 has_many :news, :dependent => :delete_all, :include => :author
42 42 has_many :issue_categories, :dependent => :delete_all, :order => "#{IssueCategory.table_name}.name"
43 43 has_many :boards, :dependent => :destroy, :order => "position ASC"
44 44 has_one :repository, :dependent => :destroy
45 45 has_many :changesets, :through => :repository
46 46 has_one :wiki, :dependent => :destroy
47 47 # Custom field for the project issues
48 48 has_and_belongs_to_many :issue_custom_fields,
49 49 :class_name => 'IssueCustomField',
50 50 :order => "#{CustomField.table_name}.position",
51 51 :join_table => "#{table_name_prefix}custom_fields_projects#{table_name_suffix}",
52 52 :association_foreign_key => 'custom_field_id'
53 53
54 54 acts_as_nested_set :order => 'name'
55 55 acts_as_attachable :view_permission => :view_files,
56 56 :delete_permission => :manage_files
57 57
58 58 acts_as_customizable
59 59 acts_as_searchable :columns => ['name', 'identifier', 'description'], :project_key => 'id', :permission => nil
60 60 acts_as_event :title => Proc.new {|o| "#{l(:label_project)}: #{o.name}"},
61 :url => Proc.new {|o| {:controller => 'projects', :action => 'show', :id => o.id}},
61 :url => Proc.new {|o| {:controller => 'projects', :action => 'show', :id => o}},
62 62 :author => nil
63 63
64 64 attr_protected :status, :enabled_module_names
65 65
66 66 validates_presence_of :name, :identifier
67 67 validates_uniqueness_of :name, :identifier
68 68 validates_associated :repository, :wiki
69 69 validates_length_of :name, :maximum => 30
70 70 validates_length_of :homepage, :maximum => 255
71 71 validates_length_of :identifier, :in => 1..20
72 72 # donwcase letters, digits, dashes but not digits only
73 73 validates_format_of :identifier, :with => /^(?!\d+$)[a-z0-9\-]*$/, :if => Proc.new { |p| p.identifier_changed? }
74 74 # reserved words
75 75 validates_exclusion_of :identifier, :in => %w( new )
76 76
77 77 before_destroy :delete_all_members, :destroy_children
78 78
79 79 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] } }
80 80 named_scope :active, { :conditions => "#{Project.table_name}.status = #{STATUS_ACTIVE}"}
81 81 named_scope :all_public, { :conditions => { :is_public => true } }
82 82 named_scope :visible, lambda { { :conditions => Project.visible_by(User.current) } }
83 83
84 84 def identifier=(identifier)
85 85 super unless identifier_frozen?
86 86 end
87 87
88 88 def identifier_frozen?
89 89 errors[:identifier].nil? && !(new_record? || identifier.blank?)
90 90 end
91 91
92 92 # returns latest created projects
93 93 # non public projects will be returned only if user is a member of those
94 94 def self.latest(user=nil, count=5)
95 95 find(:all, :limit => count, :conditions => visible_by(user), :order => "created_on DESC")
96 96 end
97 97
98 98 # Returns a SQL :conditions string used to find all active projects for the specified user.
99 99 #
100 100 # Examples:
101 101 # Projects.visible_by(admin) => "projects.status = 1"
102 102 # Projects.visible_by(normal_user) => "projects.status = 1 AND projects.is_public = 1"
103 103 def self.visible_by(user=nil)
104 104 user ||= User.current
105 105 if user && user.admin?
106 106 return "#{Project.table_name}.status=#{Project::STATUS_ACTIVE}"
107 107 elsif user && user.memberships.any?
108 108 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(',')}))"
109 109 else
110 110 return "#{Project.table_name}.status=#{Project::STATUS_ACTIVE} AND #{Project.table_name}.is_public = #{connection.quoted_true}"
111 111 end
112 112 end
113 113
114 114 def self.allowed_to_condition(user, permission, options={})
115 115 statements = []
116 116 base_statement = "#{Project.table_name}.status=#{Project::STATUS_ACTIVE}"
117 117 if perm = Redmine::AccessControl.permission(permission)
118 118 unless perm.project_module.nil?
119 119 # If the permission belongs to a project module, make sure the module is enabled
120 120 base_statement << " AND #{Project.table_name}.id IN (SELECT em.project_id FROM #{EnabledModule.table_name} em WHERE em.name='#{perm.project_module}')"
121 121 end
122 122 end
123 123 if options[:project]
124 124 project_statement = "#{Project.table_name}.id = #{options[:project].id}"
125 125 project_statement << " OR (#{Project.table_name}.lft > #{options[:project].lft} AND #{Project.table_name}.rgt < #{options[:project].rgt})" if options[:with_subprojects]
126 126 base_statement = "(#{project_statement}) AND (#{base_statement})"
127 127 end
128 128 if user.admin?
129 129 # no restriction
130 130 else
131 131 statements << "1=0"
132 132 if user.logged?
133 133 if Role.non_member.allowed_to?(permission) && !options[:member]
134 134 statements << "#{Project.table_name}.is_public = #{connection.quoted_true}"
135 135 end
136 136 allowed_project_ids = user.memberships.select {|m| m.roles.detect {|role| role.allowed_to?(permission)}}.collect {|m| m.project_id}
137 137 statements << "#{Project.table_name}.id IN (#{allowed_project_ids.join(',')})" if allowed_project_ids.any?
138 138 else
139 139 if Role.anonymous.allowed_to?(permission) && !options[:member]
140 140 # anonymous user allowed on public project
141 141 statements << "#{Project.table_name}.is_public = #{connection.quoted_true}"
142 142 end
143 143 end
144 144 end
145 145 statements.empty? ? base_statement : "((#{base_statement}) AND (#{statements.join(' OR ')}))"
146 146 end
147 147
148 148 # Returns the Systemwide and project specific activities
149 149 def activities(include_inactive=false)
150 150 if include_inactive
151 151 return all_activities
152 152 else
153 153 return active_activities
154 154 end
155 155 end
156 156
157 157 # Will create a new Project specific Activity or update an existing one
158 158 #
159 159 # This will raise a ActiveRecord::Rollback if the TimeEntryActivity
160 160 # does not successfully save.
161 161 def update_or_create_time_entry_activity(id, activity_hash)
162 162 if activity_hash.respond_to?(:has_key?) && activity_hash.has_key?('parent_id')
163 163 self.create_time_entry_activity_if_needed(activity_hash)
164 164 else
165 165 activity = project.time_entry_activities.find_by_id(id.to_i)
166 166 activity.update_attributes(activity_hash) if activity
167 167 end
168 168 end
169 169
170 170 # Create a new TimeEntryActivity if it overrides a system TimeEntryActivity
171 171 #
172 172 # This will raise a ActiveRecord::Rollback if the TimeEntryActivity
173 173 # does not successfully save.
174 174 def create_time_entry_activity_if_needed(activity)
175 175 if activity['parent_id']
176 176
177 177 parent_activity = TimeEntryActivity.find(activity['parent_id'])
178 178 activity['name'] = parent_activity.name
179 179 activity['position'] = parent_activity.position
180 180
181 181 if Enumeration.overridding_change?(activity, parent_activity)
182 182 project_activity = self.time_entry_activities.create(activity)
183 183
184 184 if project_activity.new_record?
185 185 raise ActiveRecord::Rollback, "Overridding TimeEntryActivity was not successfully saved"
186 186 else
187 187 self.time_entries.update_all("activity_id = #{project_activity.id}", ["activity_id = ?", parent_activity.id])
188 188 end
189 189 end
190 190 end
191 191 end
192 192
193 193 # Returns a :conditions SQL string that can be used to find the issues associated with this project.
194 194 #
195 195 # Examples:
196 196 # project.project_condition(true) => "(projects.id = 1 OR (projects.lft > 1 AND projects.rgt < 10))"
197 197 # project.project_condition(false) => "projects.id = 1"
198 198 def project_condition(with_subprojects)
199 199 cond = "#{Project.table_name}.id = #{id}"
200 200 cond = "(#{cond} OR (#{Project.table_name}.lft > #{lft} AND #{Project.table_name}.rgt < #{rgt}))" if with_subprojects
201 201 cond
202 202 end
203 203
204 204 def self.find(*args)
205 205 if args.first && args.first.is_a?(String) && !args.first.match(/^\d*$/)
206 206 project = find_by_identifier(*args)
207 207 raise ActiveRecord::RecordNotFound, "Couldn't find Project with identifier=#{args.first}" if project.nil?
208 208 project
209 209 else
210 210 super
211 211 end
212 212 end
213 213
214 214 def to_param
215 215 # id is used for projects with a numeric identifier (compatibility)
216 216 @to_param ||= (identifier.to_s =~ %r{^\d*$} ? id : identifier)
217 217 end
218 218
219 219 def active?
220 220 self.status == STATUS_ACTIVE
221 221 end
222 222
223 223 # Archives the project and its descendants
224 224 def archive
225 225 # Check that there is no issue of a non descendant project that is assigned
226 226 # to one of the project or descendant versions
227 227 v_ids = self_and_descendants.collect {|p| p.version_ids}.flatten
228 228 if v_ids.any? && Issue.find(:first, :include => :project,
229 229 :conditions => ["(#{Project.table_name}.lft < ? OR #{Project.table_name}.rgt > ?)" +
230 230 " AND #{Issue.table_name}.fixed_version_id IN (?)", lft, rgt, v_ids])
231 231 return false
232 232 end
233 233 Project.transaction do
234 234 archive!
235 235 end
236 236 true
237 237 end
238 238
239 239 # Unarchives the project
240 240 # All its ancestors must be active
241 241 def unarchive
242 242 return false if ancestors.detect {|a| !a.active?}
243 243 update_attribute :status, STATUS_ACTIVE
244 244 end
245 245
246 246 # Returns an array of projects the project can be moved to
247 247 # by the current user
248 248 def allowed_parents
249 249 return @allowed_parents if @allowed_parents
250 250 @allowed_parents = Project.find(:all, :conditions => Project.allowed_to_condition(User.current, :add_subprojects))
251 251 @allowed_parents = @allowed_parents - self_and_descendants
252 252 if User.current.allowed_to?(:add_project, nil, :global => true) || (!new_record? && parent.nil?)
253 253 @allowed_parents << nil
254 254 end
255 255 unless parent.nil? || @allowed_parents.empty? || @allowed_parents.include?(parent)
256 256 @allowed_parents << parent
257 257 end
258 258 @allowed_parents
259 259 end
260 260
261 261 # Sets the parent of the project with authorization check
262 262 def set_allowed_parent!(p)
263 263 unless p.nil? || p.is_a?(Project)
264 264 if p.to_s.blank?
265 265 p = nil
266 266 else
267 267 p = Project.find_by_id(p)
268 268 return false unless p
269 269 end
270 270 end
271 271 if p.nil?
272 272 if !new_record? && allowed_parents.empty?
273 273 return false
274 274 end
275 275 elsif !allowed_parents.include?(p)
276 276 return false
277 277 end
278 278 set_parent!(p)
279 279 end
280 280
281 281 # Sets the parent of the project
282 282 # Argument can be either a Project, a String, a Fixnum or nil
283 283 def set_parent!(p)
284 284 unless p.nil? || p.is_a?(Project)
285 285 if p.to_s.blank?
286 286 p = nil
287 287 else
288 288 p = Project.find_by_id(p)
289 289 return false unless p
290 290 end
291 291 end
292 292 if p == parent && !p.nil?
293 293 # Nothing to do
294 294 true
295 295 elsif p.nil? || (p.active? && move_possible?(p))
296 296 # Insert the project so that target's children or root projects stay alphabetically sorted
297 297 sibs = (p.nil? ? self.class.roots : p.children)
298 298 to_be_inserted_before = sibs.detect {|c| c.name.to_s.downcase > name.to_s.downcase }
299 299 if to_be_inserted_before
300 300 move_to_left_of(to_be_inserted_before)
301 301 elsif p.nil?
302 302 if sibs.empty?
303 303 # move_to_root adds the project in first (ie. left) position
304 304 move_to_root
305 305 else
306 306 move_to_right_of(sibs.last) unless self == sibs.last
307 307 end
308 308 else
309 309 # move_to_child_of adds the project in last (ie.right) position
310 310 move_to_child_of(p)
311 311 end
312 312 Issue.update_versions_from_hierarchy_change(self)
313 313 true
314 314 else
315 315 # Can not move to the given target
316 316 false
317 317 end
318 318 end
319 319
320 320 # Returns an array of the trackers used by the project and its active sub projects
321 321 def rolled_up_trackers
322 322 @rolled_up_trackers ||=
323 323 Tracker.find(:all, :include => :projects,
324 324 :select => "DISTINCT #{Tracker.table_name}.*",
325 325 :conditions => ["#{Project.table_name}.lft >= ? AND #{Project.table_name}.rgt <= ? AND #{Project.table_name}.status = #{STATUS_ACTIVE}", lft, rgt],
326 326 :order => "#{Tracker.table_name}.position")
327 327 end
328 328
329 329 # Closes open and locked project versions that are completed
330 330 def close_completed_versions
331 331 Version.transaction do
332 332 versions.find(:all, :conditions => {:status => %w(open locked)}).each do |version|
333 333 if version.completed?
334 334 version.update_attribute(:status, 'closed')
335 335 end
336 336 end
337 337 end
338 338 end
339 339
340 340 # Returns a scope of the Versions used by the project
341 341 def shared_versions
342 342 @shared_versions ||=
343 343 Version.scoped(:include => :project,
344 344 :conditions => "#{Project.table_name}.id = #{id}" +
345 345 " OR (#{Project.table_name}.status = #{Project::STATUS_ACTIVE} AND (" +
346 346 " #{Version.table_name}.sharing = 'system'" +
347 347 " OR (#{Project.table_name}.lft >= #{root.lft} AND #{Project.table_name}.rgt <= #{root.rgt} AND #{Version.table_name}.sharing = 'tree')" +
348 348 " OR (#{Project.table_name}.lft < #{lft} AND #{Project.table_name}.rgt > #{rgt} AND #{Version.table_name}.sharing IN ('hierarchy', 'descendants'))" +
349 349 " OR (#{Project.table_name}.lft > #{lft} AND #{Project.table_name}.rgt < #{rgt} AND #{Version.table_name}.sharing = 'hierarchy')" +
350 350 "))")
351 351 end
352 352
353 353 # Returns a hash of project users grouped by role
354 354 def users_by_role
355 355 members.find(:all, :include => [:user, :roles]).inject({}) do |h, m|
356 356 m.roles.each do |r|
357 357 h[r] ||= []
358 358 h[r] << m.user
359 359 end
360 360 h
361 361 end
362 362 end
363 363
364 364 # Deletes all project's members
365 365 def delete_all_members
366 366 me, mr = Member.table_name, MemberRole.table_name
367 367 connection.delete("DELETE FROM #{mr} WHERE #{mr}.member_id IN (SELECT #{me}.id FROM #{me} WHERE #{me}.project_id = #{id})")
368 368 Member.delete_all(['project_id = ?', id])
369 369 end
370 370
371 371 # Users issues can be assigned to
372 372 def assignable_users
373 373 members.select {|m| m.roles.detect {|role| role.assignable?}}.collect {|m| m.user}.sort
374 374 end
375 375
376 376 # Returns the mail adresses of users that should be always notified on project events
377 377 def recipients
378 378 members.select {|m| m.mail_notification? || m.user.mail_notification?}.collect {|m| m.user.mail}
379 379 end
380 380
381 381 # Returns the users that should be notified on project events
382 382 def notified_users
383 383 members.select {|m| m.mail_notification? || m.user.mail_notification?}.collect {|m| m.user}
384 384 end
385 385
386 386 # Returns an array of all custom fields enabled for project issues
387 387 # (explictly associated custom fields and custom fields enabled for all projects)
388 388 def all_issue_custom_fields
389 389 @all_issue_custom_fields ||= (IssueCustomField.for_all + issue_custom_fields).uniq.sort
390 390 end
391 391
392 392 def project
393 393 self
394 394 end
395 395
396 396 def <=>(project)
397 397 name.downcase <=> project.name.downcase
398 398 end
399 399
400 400 def to_s
401 401 name
402 402 end
403 403
404 404 # Returns a short description of the projects (first lines)
405 405 def short_description(length = 255)
406 406 description.gsub(/^(.{#{length}}[^\n\r]*).*$/m, '\1...').strip if description
407 407 end
408 408
409 409 # Return true if this project is allowed to do the specified action.
410 410 # action can be:
411 411 # * a parameter-like Hash (eg. :controller => 'projects', :action => 'edit')
412 412 # * a permission Symbol (eg. :edit_project)
413 413 def allows_to?(action)
414 414 if action.is_a? Hash
415 415 allowed_actions.include? "#{action[:controller]}/#{action[:action]}"
416 416 else
417 417 allowed_permissions.include? action
418 418 end
419 419 end
420 420
421 421 def module_enabled?(module_name)
422 422 module_name = module_name.to_s
423 423 enabled_modules.detect {|m| m.name == module_name}
424 424 end
425 425
426 426 def enabled_module_names=(module_names)
427 427 if module_names && module_names.is_a?(Array)
428 428 module_names = module_names.collect(&:to_s)
429 429 # remove disabled modules
430 430 enabled_modules.each {|mod| mod.destroy unless module_names.include?(mod.name)}
431 431 # add new modules
432 432 module_names.reject {|name| module_enabled?(name)}.each {|name| enabled_modules << EnabledModule.new(:name => name)}
433 433 else
434 434 enabled_modules.clear
435 435 end
436 436 end
437 437
438 438 # Returns an auto-generated project identifier based on the last identifier used
439 439 def self.next_identifier
440 440 p = Project.find(:first, :order => 'created_on DESC')
441 441 p.nil? ? nil : p.identifier.to_s.succ
442 442 end
443 443
444 444 # Copies and saves the Project instance based on the +project+.
445 445 # Duplicates the source project's:
446 446 # * Wiki
447 447 # * Versions
448 448 # * Categories
449 449 # * Issues
450 450 # * Members
451 451 # * Queries
452 452 #
453 453 # Accepts an +options+ argument to specify what to copy
454 454 #
455 455 # Examples:
456 456 # project.copy(1) # => copies everything
457 457 # project.copy(1, :only => 'members') # => copies members only
458 458 # project.copy(1, :only => ['members', 'versions']) # => copies members and versions
459 459 def copy(project, options={})
460 460 project = project.is_a?(Project) ? project : Project.find(project)
461 461
462 462 to_be_copied = %w(wiki versions issue_categories issues members queries boards)
463 463 to_be_copied = to_be_copied & options[:only].to_a unless options[:only].nil?
464 464
465 465 Project.transaction do
466 466 if save
467 467 reload
468 468 to_be_copied.each do |name|
469 469 send "copy_#{name}", project
470 470 end
471 471 Redmine::Hook.call_hook(:model_project_copy_before_save, :source_project => project, :destination_project => self)
472 472 save
473 473 end
474 474 end
475 475 end
476 476
477 477
478 478 # Copies +project+ and returns the new instance. This will not save
479 479 # the copy
480 480 def self.copy_from(project)
481 481 begin
482 482 project = project.is_a?(Project) ? project : Project.find(project)
483 483 if project
484 484 # clear unique attributes
485 485 attributes = project.attributes.dup.except('id', 'name', 'identifier', 'status', 'parent_id', 'lft', 'rgt')
486 486 copy = Project.new(attributes)
487 487 copy.enabled_modules = project.enabled_modules
488 488 copy.trackers = project.trackers
489 489 copy.custom_values = project.custom_values.collect {|v| v.clone}
490 490 copy.issue_custom_fields = project.issue_custom_fields
491 491 return copy
492 492 else
493 493 return nil
494 494 end
495 495 rescue ActiveRecord::RecordNotFound
496 496 return nil
497 497 end
498 498 end
499 499
500 500 private
501 501
502 502 # Destroys children before destroying self
503 503 def destroy_children
504 504 children.each do |child|
505 505 child.destroy
506 506 end
507 507 end
508 508
509 509 # Copies wiki from +project+
510 510 def copy_wiki(project)
511 511 # Check that the source project has a wiki first
512 512 unless project.wiki.nil?
513 513 self.wiki ||= Wiki.new
514 514 wiki.attributes = project.wiki.attributes.dup.except("id", "project_id")
515 515 wiki_pages_map = {}
516 516 project.wiki.pages.each do |page|
517 517 # Skip pages without content
518 518 next if page.content.nil?
519 519 new_wiki_content = WikiContent.new(page.content.attributes.dup.except("id", "page_id", "updated_on"))
520 520 new_wiki_page = WikiPage.new(page.attributes.dup.except("id", "wiki_id", "created_on", "parent_id"))
521 521 new_wiki_page.content = new_wiki_content
522 522 wiki.pages << new_wiki_page
523 523 wiki_pages_map[page.id] = new_wiki_page
524 524 end
525 525 wiki.save
526 526 # Reproduce page hierarchy
527 527 project.wiki.pages.each do |page|
528 528 if page.parent_id && wiki_pages_map[page.id]
529 529 wiki_pages_map[page.id].parent = wiki_pages_map[page.parent_id]
530 530 wiki_pages_map[page.id].save
531 531 end
532 532 end
533 533 end
534 534 end
535 535
536 536 # Copies versions from +project+
537 537 def copy_versions(project)
538 538 project.versions.each do |version|
539 539 new_version = Version.new
540 540 new_version.attributes = version.attributes.dup.except("id", "project_id", "created_on", "updated_on")
541 541 self.versions << new_version
542 542 end
543 543 end
544 544
545 545 # Copies issue categories from +project+
546 546 def copy_issue_categories(project)
547 547 project.issue_categories.each do |issue_category|
548 548 new_issue_category = IssueCategory.new
549 549 new_issue_category.attributes = issue_category.attributes.dup.except("id", "project_id")
550 550 self.issue_categories << new_issue_category
551 551 end
552 552 end
553 553
554 554 # Copies issues from +project+
555 555 def copy_issues(project)
556 556 # Stores the source issue id as a key and the copied issues as the
557 557 # value. Used to map the two togeather for issue relations.
558 558 issues_map = {}
559 559
560 560 project.issues.each do |issue|
561 561 new_issue = Issue.new
562 562 new_issue.copy_from(issue)
563 563 # Reassign fixed_versions by name, since names are unique per
564 564 # project and the versions for self are not yet saved
565 565 if issue.fixed_version
566 566 new_issue.fixed_version = self.versions.select {|v| v.name == issue.fixed_version.name}.first
567 567 end
568 568 # Reassign the category by name, since names are unique per
569 569 # project and the categories for self are not yet saved
570 570 if issue.category
571 571 new_issue.category = self.issue_categories.select {|c| c.name == issue.category.name}.first
572 572 end
573 573 self.issues << new_issue
574 574 issues_map[issue.id] = new_issue
575 575 end
576 576
577 577 # Relations after in case issues related each other
578 578 project.issues.each do |issue|
579 579 new_issue = issues_map[issue.id]
580 580
581 581 # Relations
582 582 issue.relations_from.each do |source_relation|
583 583 new_issue_relation = IssueRelation.new
584 584 new_issue_relation.attributes = source_relation.attributes.dup.except("id", "issue_from_id", "issue_to_id")
585 585 new_issue_relation.issue_to = issues_map[source_relation.issue_to_id]
586 586 if new_issue_relation.issue_to.nil? && Setting.cross_project_issue_relations?
587 587 new_issue_relation.issue_to = source_relation.issue_to
588 588 end
589 589 new_issue.relations_from << new_issue_relation
590 590 end
591 591
592 592 issue.relations_to.each do |source_relation|
593 593 new_issue_relation = IssueRelation.new
594 594 new_issue_relation.attributes = source_relation.attributes.dup.except("id", "issue_from_id", "issue_to_id")
595 595 new_issue_relation.issue_from = issues_map[source_relation.issue_from_id]
596 596 if new_issue_relation.issue_from.nil? && Setting.cross_project_issue_relations?
597 597 new_issue_relation.issue_from = source_relation.issue_from
598 598 end
599 599 new_issue.relations_to << new_issue_relation
600 600 end
601 601 end
602 602 end
603 603
604 604 # Copies members from +project+
605 605 def copy_members(project)
606 606 project.memberships.each do |member|
607 607 new_member = Member.new
608 608 new_member.attributes = member.attributes.dup.except("id", "project_id", "created_on")
609 609 # only copy non inherited roles
610 610 # inherited roles will be added when copying the group membership
611 611 role_ids = member.member_roles.reject(&:inherited?).collect(&:role_id)
612 612 next if role_ids.empty?
613 613 new_member.role_ids = role_ids
614 614 new_member.project = self
615 615 self.members << new_member
616 616 end
617 617 end
618 618
619 619 # Copies queries from +project+
620 620 def copy_queries(project)
621 621 project.queries.each do |query|
622 622 new_query = Query.new
623 623 new_query.attributes = query.attributes.dup.except("id", "project_id", "sort_criteria")
624 624 new_query.sort_criteria = query.sort_criteria if query.sort_criteria
625 625 new_query.project = self
626 626 self.queries << new_query
627 627 end
628 628 end
629 629
630 630 # Copies boards from +project+
631 631 def copy_boards(project)
632 632 project.boards.each do |board|
633 633 new_board = Board.new
634 634 new_board.attributes = board.attributes.dup.except("id", "project_id", "topics_count", "messages_count", "last_message_id")
635 635 new_board.project = self
636 636 self.boards << new_board
637 637 end
638 638 end
639 639
640 640 def allowed_permissions
641 641 @allowed_permissions ||= begin
642 642 module_names = enabled_modules.collect {|m| m.name}
643 643 Redmine::AccessControl.modules_permissions(module_names).collect {|p| p.name}
644 644 end
645 645 end
646 646
647 647 def allowed_actions
648 648 @actions_allowed ||= allowed_permissions.inject([]) { |actions, permission| actions += Redmine::AccessControl.allowed_actions(permission) }.flatten
649 649 end
650 650
651 651 # Returns all the active Systemwide and project specific activities
652 652 def active_activities
653 653 overridden_activity_ids = self.time_entry_activities.collect(&:parent_id)
654 654
655 655 if overridden_activity_ids.empty?
656 656 return TimeEntryActivity.shared.active
657 657 else
658 658 return system_activities_and_project_overrides
659 659 end
660 660 end
661 661
662 662 # Returns all the Systemwide and project specific activities
663 663 # (inactive and active)
664 664 def all_activities
665 665 overridden_activity_ids = self.time_entry_activities.collect(&:parent_id)
666 666
667 667 if overridden_activity_ids.empty?
668 668 return TimeEntryActivity.shared
669 669 else
670 670 return system_activities_and_project_overrides(true)
671 671 end
672 672 end
673 673
674 674 # Returns the systemwide active activities merged with the project specific overrides
675 675 def system_activities_and_project_overrides(include_inactive=false)
676 676 if include_inactive
677 677 return TimeEntryActivity.shared.
678 678 find(:all,
679 679 :conditions => ["id NOT IN (?)", self.time_entry_activities.collect(&:parent_id)]) +
680 680 self.time_entry_activities
681 681 else
682 682 return TimeEntryActivity.shared.active.
683 683 find(:all,
684 684 :conditions => ["id NOT IN (?)", self.time_entry_activities.collect(&:parent_id)]) +
685 685 self.time_entry_activities.active
686 686 end
687 687 end
688 688
689 689 # Archives subprojects recursively
690 690 def archive!
691 691 children.each do |subproject|
692 692 subproject.send :archive!
693 693 end
694 694 update_attribute :status, STATUS_ARCHIVED
695 695 end
696 696 end
General Comments 0
You need to be logged in to leave comments. Login now