1 # redMine - project management software
2 # Copyright (C) 2006-2007 Jean-Philippe Lang
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 class ProjectsController < ApplicationController
20 before_filter :find_project, :except => [ :index, :list, :add ]
21 before_filter :authorize, :except => [ :index, :list, :add, :archive, :unarchive, :destroy ]
22 before_filter :require_admin, :only => [ :add, :archive, :unarchive, :destroy ]
23 accept_key_auth :activity, :calendar
25 cache_sweeper :project_sweeper, :only => [ :add, :edit, :archive, :unarchive, :destroy ]
26 cache_sweeper :issue_sweeper, :only => [ :add_issue ]
27 cache_sweeper :version_sweeper, :only => [ :add_version ]
32 include CustomFieldsHelper
40 include RepositoriesHelper
41 include ProjectsHelper
45 render :action => 'list' unless request.xhr?
48 # Lists visible projects
50 projects = Project.find :all,
51 :conditions => Project.visible_by(logged_in_user),
53 @project_tree = projects.group_by {|p| p.parent || p}
54 @project_tree.each_key {|p| @project_tree[p] -= [p]}
59 @custom_fields = IssueCustomField.find(:all)
60 @root_projects = Project.find(:all, :conditions => "parent_id IS NULL AND status = #{Project::STATUS_ACTIVE}")
61 @project = Project.new(params[:project])
62 @project.enabled_module_names = Redmine::AccessControl.available_project_modules
64 @custom_values = ProjectCustomField.find(:all).collect { |x| CustomValue.new(:custom_field => x, :customized => @project) }
66 @project.custom_fields = CustomField.find(params[:custom_field_ids]) if params[:custom_field_ids]
67 @custom_values = ProjectCustomField.find(:all).collect { |x| CustomValue.new(:custom_field => x, :customized => @project, :value => (params[:custom_fields] ? params["custom_fields"][x.id.to_s] : nil)) }
68 @project.custom_values = @custom_values
70 @project.enabled_module_names = params[:enabled_modules]
71 flash[:notice] = l(:notice_successful_create)
72 redirect_to :controller => 'admin', :action => 'projects'
79 @custom_values = @project.custom_values.find(:all, :include => :custom_field)
80 @members_by_role = @project.members.find(:all, :include => [:user, :role], :order => 'position').group_by {|m| m.role}
81 @subprojects = @project.active_children
82 @news = @project.news.find(:all, :limit => 5, :include => [ :author, :project ], :order => "#{News.table_name}.created_on DESC")
83 @trackers = Tracker.find(:all, :order => 'position')
84 @open_issues_by_tracker = Issue.count(:group => :tracker, :joins => "INNER JOIN #{IssueStatus.table_name} ON #{IssueStatus.table_name}.id = #{Issue.table_name}.status_id", :conditions => ["project_id=? and #{IssueStatus.table_name}.is_closed=?", @project.id, false])
85 @total_issues_by_tracker = Issue.count(:group => :tracker, :conditions => ["project_id=?", @project.id])
86 @total_hours = @project.time_entries.sum(:hours)
87 @key = User.current.rss_key
91 @root_projects = Project::find(:all, :conditions => ["parent_id IS NULL AND status = #{Project::STATUS_ACTIVE} AND id <> ?", @project.id])
92 @custom_fields = IssueCustomField.find(:all)
93 @issue_category ||= IssueCategory.new
94 @member ||= @project.members.new
95 @custom_values ||= ProjectCustomField.find(:all).collect { |x| @project.custom_values.find_by_custom_field_id(x.id) || CustomValue.new(:custom_field => x) }
96 @repository ||= @project.repository
97 @wiki ||= @project.wiki
103 @project.custom_fields = IssueCustomField.find(params[:custom_field_ids]) if params[:custom_field_ids]
104 if params[:custom_fields]
105 @custom_values = ProjectCustomField.find(:all).collect { |x| CustomValue.new(:custom_field => x, :customized => @project, :value => params["custom_fields"][x.id.to_s]) }
106 @project.custom_values = @custom_values
108 @project.attributes = params[:project]
110 flash[:notice] = l(:notice_successful_update)
111 redirect_to :action => 'settings', :id => @project
114 render :action => 'settings'
120 @project.enabled_module_names = params[:enabled_modules]
121 redirect_to :action => 'settings', :id => @project, :tab => 'modules'
125 @project.archive if request.post? && @project.active?
126 redirect_to :controller => 'admin', :action => 'projects'
130 @project.unarchive if request.post? && !@project.active?
131 redirect_to :controller => 'admin', :action => 'projects'
136 @project_to_destroy = @project
137 if request.post? and params[:confirm]
138 @project_to_destroy.destroy
139 redirect_to :controller => 'admin', :action => 'projects'
141 # hide project in layout
145 # Add a new issue category to @project
146 def add_issue_category
147 @category = @project.issue_categories.build(params[:category])
148 if request.post? and @category.save
149 respond_to do |format|
151 flash[:notice] = l(:notice_successful_create)
152 redirect_to :action => 'settings', :tab => 'categories', :id => @project
155 # IE doesn't support the replace_html rjs method for select box options
156 render(:update) {|page| page.replace "issue_category_id",
157 content_tag('select', '<option></option>' + options_from_collection_for_select(@project.issue_categories, 'id', 'name', @category.id), :id => 'issue_category_id', :name => 'issue[category_id]')
164 # Add a new version to @project
166 @version = @project.versions.build(params[:version])
167 if request.post? and @version.save
168 flash[:notice] = l(:notice_successful_create)
169 redirect_to :action => 'settings', :tab => 'versions', :id => @project
173 # Add a new document to @project
175 @document = @project.documents.build(params[:document])
176 if request.post? and @document.save
177 # Save the attachments
178 params[:attachments].each { |a|
179 Attachment.create(:container => @document, :file => a, :author => logged_in_user) unless a.size == 0
180 } if params[:attachments] and params[:attachments].is_a? Array
181 flash[:notice] = l(:notice_successful_create)
182 Mailer.deliver_document_added(@document) if Setting.notified_events.include?('document_added')
183 redirect_to :action => 'list_documents', :id => @project
187 # Show documents list of @project
189 @sort_by = %w(category date title author).include?(params[:sort_by]) ? params[:sort_by] : 'category'
190 documents = @project.documents.find :all, :include => [:attachments, :category]
193 @grouped = documents.group_by {|d| d.created_on.to_date }
195 @grouped = documents.group_by {|d| d.title.first.upcase}
197 @grouped = documents.select{|d| d.attachments.any?}.group_by {|d| d.attachments.last.author}
199 @grouped = documents.group_by(&:category)
201 render :layout => false if request.xhr?
204 # Add a new issue to @project
205 # The new issue will be created from an existing one if copy_from parameter is given
207 @issue = params[:copy_from] ? Issue.new.copy_from(params[:copy_from]) : Issue.new(params[:issue])
208 @issue.project = @project
209 @issue.author = User.current
210 @issue.tracker ||= Tracker.find(params[:tracker_id])
212 default_status = IssueStatus.default
213 unless default_status
214 flash.now[:error] = 'No default issue status is defined. Please check your configuration (Go to "Administration -> Issue statuses").'
215 render :nothing => true, :layout => true
218 @issue.status = default_status
219 @allowed_statuses = ([default_status] + default_status.find_new_statuses_allowed_to(logged_in_user.role_for_project(@project), @issue.tracker))if logged_in_user
222 @issue.start_date ||= Date.today
223 @custom_values = @issue.custom_values.empty? ?
224 @project.custom_fields_for_issues(@issue.tracker).collect { |x| CustomValue.new(:custom_field => x, :customized => @issue) } :
227 requested_status = IssueStatus.find_by_id(params[:issue][:status_id])
228 # Check that the user is allowed to apply the requested status
229 @issue.status = (@allowed_statuses.include? requested_status) ? requested_status : default_status
230 @custom_values = @project.custom_fields_for_issues(@issue.tracker).collect { |x| CustomValue.new(:custom_field => x, :customized => @issue, :value => params["custom_fields"][x.id.to_s]) }
231 @issue.custom_values = @custom_values
233 if params[:attachments] && params[:attachments].is_a?(Array)
235 params[:attachments].each {|a| Attachment.create(:container => @issue, :file => a, :author => User.current) unless a.size == 0}
237 flash[:notice] = l(:notice_successful_create)
238 Mailer.deliver_issue_add(@issue) if Setting.notified_events.include?('issue_added')
239 redirect_to :controller => 'issues', :action => 'index', :project_id => @project
243 @priorities = Enumeration::get_values('IPRI')
249 status = params[:status_id].blank? ? nil : IssueStatus.find_by_id(params[:status_id])
250 priority = params[:priority_id].blank? ? nil : Enumeration.find_by_id(params[:priority_id])
251 assigned_to = params[:assigned_to_id].blank? ? nil : User.find_by_id(params[:assigned_to_id])
252 category = params[:category_id].blank? ? nil : @project.issue_categories.find_by_id(params[:category_id])
253 fixed_version = params[:fixed_version_id].blank? ? nil : @project.versions.find_by_id(params[:fixed_version_id])
254 issues = @project.issues.find_all_by_id(params[:issue_ids])
255 unsaved_issue_ids = []
256 issues.each do |issue|
257 journal = issue.init_journal(User.current, params[:notes])
258 issue.priority = priority if priority
259 issue.assigned_to = assigned_to if assigned_to || params[:assigned_to_id] == 'none'
260 issue.category = category if category
261 issue.fixed_version = fixed_version if fixed_version
262 issue.start_date = params[:start_date] unless params[:start_date].blank?
263 issue.due_date = params[:due_date] unless params[:due_date].blank?
264 issue.done_ratio = params[:done_ratio] unless params[:done_ratio].blank?
265 # Don't save any change to the issue if the user is not authorized to apply the requested status
266 if (status.nil? || (issue.status.new_status_allowed_to?(status, current_role, issue.tracker) && issue.status = status)) && issue.save
267 # Send notification for each issue (if changed)
268 Mailer.deliver_issue_edit(journal) if journal.details.any? && Setting.notified_events.include?('issue_updated')
270 # Keep unsaved issue ids to display them in flash error
271 unsaved_issue_ids << issue.id
274 if unsaved_issue_ids.empty?
275 flash[:notice] = l(:notice_successful_update) unless issues.empty?
277 flash[:error] = l(:notice_failed_to_save_issues, unsaved_issue_ids.size, issues.size, '#' + unsaved_issue_ids.join(', #'))
279 redirect_to :controller => 'issues', :action => 'index', :project_id => @project
282 if current_role && User.current.allowed_to?(:change_issue_status, @project)
283 # Find potential statuses the user could be allowed to switch issues to
284 @available_statuses = Workflow.find(:all, :include => :new_status,
285 :conditions => {:role_id => current_role.id}).collect(&:new_status).compact.uniq
287 render :update do |page|
288 page.hide 'query_form'
289 page.replace_html 'bulk-edit', :partial => 'issues/bulk_edit_form'
294 @issues = @project.issues.find(params[:issue_ids]) if params[:issue_ids]
295 redirect_to :controller => 'issues', :action => 'index', :project_id => @project and return unless @issues
297 # find projects to which the user is allowed to move the issue
298 User.current.memberships.each {|m| @projects << m.project if m.role.allowed_to?(:controller => 'projects', :action => 'move_issues')}
299 # issue can be moved to any tracker
300 @trackers = Tracker.find(:all)
301 if request.post? and params[:new_project_id] and params[:new_tracker_id]
302 new_project = Project.find_by_id(params[:new_project_id])
303 new_tracker = Tracker.find_by_id(params[:new_tracker_id])
305 if new_project && i.project_id != new_project.id
306 # issue is moved to another project
308 i.fixed_version = nil
309 # delete issue relations
310 i.relations_from.clear
312 i.project = new_project
315 i.tracker = new_tracker
319 flash[:notice] = l(:notice_successful_update)
320 redirect_to :controller => 'issues', :action => 'index', :project_id => @project
324 # Add a news to @project
326 @news = News.new(:project => @project)
328 @news.attributes = params[:news]
329 @news.author_id = self.logged_in_user.id if self.logged_in_user
331 flash[:notice] = l(:notice_successful_create)
332 Mailer.deliver_news_added(@news) if Setting.notified_events.include?('news_added')
333 redirect_to :controller => 'news', :action => 'index', :project_id => @project
340 @version = @project.versions.find_by_id(params[:version_id])
341 # Save the attachments
343 params[:attachments].each { |file|
344 next unless file.size > 0
345 a = Attachment.create(:container => @version, :file => file, :author => logged_in_user)
346 @attachments << a unless a.new_record?
347 } if params[:attachments] and params[:attachments].is_a? Array
348 Mailer.deliver_attachments_added(@attachments) if !@attachments.empty? && Setting.notified_events.include?('file_added')
349 redirect_to :controller => 'projects', :action => 'list_files', :id => @project
351 @versions = @project.versions.sort
355 @versions = @project.versions.sort
358 # Show changelog for @project
360 @trackers = Tracker.find(:all, :conditions => ["is_in_chlog=?", true], :order => 'position')
361 retrieve_selected_tracker_ids(@trackers)
362 @versions = @project.versions.sort
366 @trackers = Tracker.find(:all, :conditions => ["is_in_roadmap=?", true], :order => 'position')
367 retrieve_selected_tracker_ids(@trackers)
368 @versions = @project.versions.sort
369 @versions = @versions.select {|v| !v.completed? } unless params[:completed]
373 if params[:year] and params[:year].to_i > 1900
374 @year = params[:year].to_i
375 if params[:month] and params[:month].to_i > 0 and params[:month].to_i < 13
376 @month = params[:month].to_i
379 @year ||= Date.today.year
380 @month ||= Date.today.month
385 @date_from = Date.today - 30
386 @date_to = Date.today + 1
389 @date_from = Date.civil(@year, @month, 1)
390 @date_to = @date_from >> 1
393 @event_types = %w(issues news files documents wiki_pages changesets)
394 @event_types.delete('wiki_pages') unless @project.wiki
395 @event_types.delete('changesets') unless @project.repository
396 # only show what the user is allowed to view
397 @event_types = @event_types.select {|o| User.current.allowed_to?("view_#{o}".to_sym, @project)}
399 @scope = @event_types.select {|t| params["show_#{t}"]}
400 # default events if none is specified in parameters
401 @scope = (@event_types - %w(wiki_pages))if @scope.empty?
405 if @scope.include?('issues')
406 @events += @project.issues.find(:all, :include => [:author, :tracker], :conditions => ["#{Issue.table_name}.created_on>=? and #{Issue.table_name}.created_on<=?", @date_from, @date_to] )
409 if @scope.include?('news')
410 @events += @project.news.find(:all, :conditions => ["#{News.table_name}.created_on>=? and #{News.table_name}.created_on<=?", @date_from, @date_to], :include => :author )
413 if @scope.include?('files')
414 @events += Attachment.find(:all, :select => "#{Attachment.table_name}.*", :joins => "LEFT JOIN #{Version.table_name} ON #{Version.table_name}.id = #{Attachment.table_name}.container_id", :conditions => ["#{Attachment.table_name}.container_type='Version' and #{Version.table_name}.project_id=? and #{Attachment.table_name}.created_on>=? and #{Attachment.table_name}.created_on<=?", @project.id, @date_from, @date_to], :include => :author )
417 if @scope.include?('documents')
418 @events += @project.documents.find(:all, :conditions => ["#{Document.table_name}.created_on>=? and #{Document.table_name}.created_on<=?", @date_from, @date_to] )
419 @events += Attachment.find(:all, :select => "attachments.*", :joins => "LEFT JOIN #{Document.table_name} ON #{Document.table_name}.id = #{Attachment.table_name}.container_id", :conditions => ["#{Attachment.table_name}.container_type='Document' and #{Document.table_name}.project_id=? and #{Attachment.table_name}.created_on>=? and #{Attachment.table_name}.created_on<=?", @project.id, @date_from, @date_to], :include => :author )
422 if @scope.include?('wiki_pages')
423 select = "#{WikiContent.versioned_table_name}.updated_on, #{WikiContent.versioned_table_name}.comments, " +
424 "#{WikiContent.versioned_table_name}.#{WikiContent.version_column}, #{WikiPage.table_name}.title, " +
425 "#{WikiContent.versioned_table_name}.page_id, #{WikiContent.versioned_table_name}.author_id, " +
426 "#{WikiContent.versioned_table_name}.id"
427 joins = "LEFT JOIN #{WikiPage.table_name} ON #{WikiPage.table_name}.id = #{WikiContent.versioned_table_name}.page_id " +
428 "LEFT JOIN #{Wiki.table_name} ON #{Wiki.table_name}.id = #{WikiPage.table_name}.wiki_id "
429 conditions = ["#{Wiki.table_name}.project_id = ? AND #{WikiContent.versioned_table_name}.updated_on BETWEEN ? AND ?",
430 @project.id, @date_from, @date_to]
432 @events += WikiContent.versioned_class.find(:all, :select => select, :joins => joins, :conditions => conditions)
435 if @scope.include?('changesets')
436 @events += @project.repository.changesets.find(:all, :conditions => ["#{Changeset.table_name}.committed_on BETWEEN ? AND ?", @date_from, @date_to])
439 @events_by_day = @events.group_by(&:event_date)
441 respond_to do |format|
442 format.html { render :layout => false if request.xhr? }
443 format.atom { render_feed(@events, :title => "#{@project.name}: #{l(:label_activity)}") }
448 @trackers = Tracker.find(:all, :order => 'position')
449 retrieve_selected_tracker_ids(@trackers)
451 if params[:year] and params[:year].to_i > 1900
452 @year = params[:year].to_i
453 if params[:month] and params[:month].to_i > 0 and params[:month].to_i < 13
454 @month = params[:month].to_i
457 @year ||= Date.today.year
458 @month ||= Date.today.month
459 @calendar = Redmine::Helpers::Calendar.new(Date.civil(@year, @month, 1), current_language, :month)
462 @project.issues_with_subprojects(params[:with_subprojects]) do
463 events += Issue.find(:all,
464 :include => [:tracker, :status, :assigned_to, :priority, :project],
465 :conditions => ["((start_date BETWEEN ? AND ?) OR (due_date BETWEEN ? AND ?)) AND #{Issue.table_name}.tracker_id IN (#{@selected_tracker_ids.join(',')})", @calendar.startdt, @calendar.enddt, @calendar.startdt, @calendar.enddt]
466 ) unless @selected_tracker_ids.empty?
468 events += @project.versions.find(:all, :conditions => ["effective_date BETWEEN ? AND ?", @calendar.startdt, @calendar.enddt])
469 @calendar.events = events
471 render :layout => false if request.xhr?
475 @trackers = Tracker.find(:all, :order => 'position')
476 retrieve_selected_tracker_ids(@trackers)
478 if params[:year] and params[:year].to_i >0
479 @year_from = params[:year].to_i
480 if params[:month] and params[:month].to_i >=1 and params[:month].to_i <= 12
481 @month_from = params[:month].to_i
486 @month_from ||= Date.today.month
487 @year_from ||= Date.today.year
490 zoom = (params[:zoom] || User.current.pref[:gantt_zoom]).to_i
491 @zoom = (zoom > 0 && zoom < 5) ? zoom : 2
492 months = (params[:months] || User.current.pref[:gantt_months]).to_i
493 @months = (months > 0 && months < 25) ? months : 6
495 # Save gantt paramters as user preference (zoom and months count)
496 if (User.current.logged? && (@zoom != User.current.pref[:gantt_zoom] || @months != User.current.pref[:gantt_months]))
497 User.current.pref[:gantt_zoom], User.current.pref[:gantt_months] = @zoom, @months
498 User.current.preference.save
501 @date_from = Date.civil(@year_from, @month_from, 1)
502 @date_to = (@date_from >> @months) - 1
505 @project.issues_with_subprojects(params[:with_subprojects]) do
506 @events += Issue.find(:all,
507 :order => "start_date, due_date",
508 :include => [:tracker, :status, :assigned_to, :priority, :project],
509 :conditions => ["(((start_date>=? and start_date<=?) or (due_date>=? and due_date<=?) or (start_date<? and due_date>?)) and start_date is not null and due_date is not null and #{Issue.table_name}.tracker_id in (#{@selected_tracker_ids.join(',')}))", @date_from, @date_to, @date_from, @date_to, @date_from, @date_to]
510 ) unless @selected_tracker_ids.empty?
512 @events += @project.versions.find(:all, :conditions => ["effective_date BETWEEN ? AND ?", @date_from, @date_to])
513 @events.sort! {|x,y| x.start_date <=> y.start_date }
515 if params[:format]=='pdf'
516 @options_for_rfpdf ||= {}
517 @options_for_rfpdf[:file_name] = "#{@project.identifier}-gantt.pdf"
518 render :template => "projects/gantt.rfpdf", :layout => false
519 elsif params[:format]=='png' && respond_to?('gantt_image')
520 image = gantt_image(@events, @date_from, @months, @zoom)
522 send_data(image.to_blob, :disposition => 'inline', :type => 'image/png', :filename => "#{@project.identifier}-gantt.png")
524 render :template => "projects/gantt.rhtml"
529 # Find project of id params[:id]
530 # if not found, redirect to project list
531 # Used as a before_filter
533 @project = Project.find(params[:id])
534 rescue ActiveRecord::RecordNotFound
538 def retrieve_selected_tracker_ids(selectable_trackers)
539 if ids = params[:tracker_ids]
540 @selected_tracker_ids = (ids.is_a? Array) ? ids.collect { |id| id.to_i.to_s } : ids.split('/').collect { |id| id.to_i.to_s }
542 @selected_tracker_ids = selectable_trackers.collect {|t| t.id.to_s }