Chris@1296: # Redmine - project management software Chris@1296: # Copyright (C) 2006-2012 Jean-Philippe Lang Chris@1296: # Chris@1296: # This program is free software; you can redistribute it and/or Chris@1296: # modify it under the terms of the GNU General Public License Chris@1296: # as published by the Free Software Foundation; either version 2 Chris@1296: # of the License, or (at your option) any later version. Chris@1296: # Chris@1296: # This program is distributed in the hope that it will be useful, Chris@1296: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@1296: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@1296: # GNU General Public License for more details. Chris@1296: # Chris@1296: # You should have received a copy of the GNU General Public License Chris@1296: # along with this program; if not, write to the Free Software Chris@1296: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@1296: Chris@1296: class ProjectsController < ApplicationController Chris@1296: menu_item :overview Chris@1296: menu_item :roadmap, :only => :roadmap Chris@1296: menu_item :settings, :only => :settings Chris@1296: Chris@1296: before_filter :find_project, :except => [ :index, :list, :new, :create, :copy ] Chris@1296: before_filter :authorize, :except => [ :index, :list, :new, :create, :copy, :archive, :unarchive, :destroy] Chris@1296: before_filter :authorize_global, :only => [:new, :create] Chris@1296: before_filter :require_admin, :only => [ :copy, :archive, :unarchive, :destroy ] Chris@1296: accept_rss_auth :index Chris@1296: accept_api_auth :index, :show, :create, :update, :destroy Chris@1296: Chris@1296: after_filter :only => [:create, :edit, :update, :archive, :unarchive, :destroy] do |controller| Chris@1296: if controller.request.post? Chris@1296: controller.send :expire_action, :controller => 'welcome', :action => 'robots' Chris@1296: end Chris@1296: end Chris@1296: Chris@1296: helper :sort Chris@1296: include SortHelper Chris@1296: helper :custom_fields Chris@1296: include CustomFieldsHelper Chris@1296: helper :issues Chris@1296: helper :queries Chris@1296: include QueriesHelper Chris@1296: helper :repositories Chris@1296: include RepositoriesHelper Chris@1296: include ProjectsHelper Chris@1296: Chris@1296: # Lists visible projects Chris@1296: def index Chris@1296: respond_to do |format| Chris@1296: format.html { Chris@1296: scope = Project Chris@1296: unless params[:closed] Chris@1296: scope = scope.active Chris@1296: end Chris@1296: @projects = scope.visible.order('lft').all Chris@1296: } Chris@1296: format.api { Chris@1296: @offset, @limit = api_offset_and_limit Chris@1296: @project_count = Project.visible.count Chris@1296: @projects = Project.visible.all(:offset => @offset, :limit => @limit, :order => 'lft') Chris@1296: } Chris@1296: format.atom { Chris@1296: projects = Project.visible.find(:all, :order => 'created_on DESC', Chris@1296: :limit => Setting.feeds_limit.to_i) Chris@1296: render_feed(projects, :title => "#{Setting.app_title}: #{l(:label_project_latest)}") Chris@1296: } Chris@1296: end Chris@1296: end Chris@1296: Chris@1296: def new Chris@1296: @issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position") Chris@1296: @trackers = Tracker.sorted.all Chris@1296: @project = Project.new Chris@1296: @project.safe_attributes = params[:project] Chris@1296: end Chris@1296: Chris@1296: def create Chris@1296: @issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position") Chris@1296: @trackers = Tracker.sorted.all Chris@1296: @project = Project.new Chris@1296: @project.safe_attributes = params[:project] Chris@1296: Chris@1296: if validate_parent_id && @project.save Chris@1296: @project.set_allowed_parent!(params[:project]['parent_id']) if params[:project].has_key?('parent_id') Chris@1296: # Add current user as a project member if he is not admin Chris@1296: unless User.current.admin? Chris@1296: r = Role.givable.find_by_id(Setting.new_project_user_role_id.to_i) || Role.givable.first Chris@1296: m = Member.new(:user => User.current, :roles => [r]) Chris@1296: @project.members << m Chris@1296: end Chris@1296: respond_to do |format| Chris@1296: format.html { Chris@1296: flash[:notice] = l(:notice_successful_create) Chris@1296: redirect_to(params[:continue] ? Chris@1296: {:controller => 'projects', :action => 'new', :project => {:parent_id => @project.parent_id}.reject {|k,v| v.nil?}} : Chris@1296: {:controller => 'projects', :action => 'settings', :id => @project} Chris@1296: ) Chris@1296: } Chris@1296: format.api { render :action => 'show', :status => :created, :location => url_for(:controller => 'projects', :action => 'show', :id => @project.id) } Chris@1296: end Chris@1296: else Chris@1296: respond_to do |format| Chris@1296: format.html { render :action => 'new' } Chris@1296: format.api { render_validation_errors(@project) } Chris@1296: end Chris@1296: end Chris@1296: Chris@1296: end Chris@1296: Chris@1296: def copy Chris@1296: @issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position") Chris@1296: @trackers = Tracker.sorted.all Chris@1296: @root_projects = Project.find(:all, Chris@1296: :conditions => "parent_id IS NULL AND status = #{Project::STATUS_ACTIVE}", Chris@1296: :order => 'name') Chris@1296: @source_project = Project.find(params[:id]) Chris@1296: if request.get? Chris@1296: @project = Project.copy_from(@source_project) Chris@1296: @project.identifier = Project.next_identifier if Setting.sequential_project_identifiers? Chris@1296: else Chris@1296: Mailer.with_deliveries(params[:notifications] == '1') do Chris@1296: @project = Project.new Chris@1296: @project.safe_attributes = params[:project] Chris@1296: if validate_parent_id && @project.copy(@source_project, :only => params[:only]) Chris@1296: @project.set_allowed_parent!(params[:project]['parent_id']) if params[:project].has_key?('parent_id') Chris@1296: flash[:notice] = l(:notice_successful_create) Chris@1296: redirect_to :controller => 'projects', :action => 'settings', :id => @project Chris@1296: elsif !@project.new_record? Chris@1296: # Project was created Chris@1296: # But some objects were not copied due to validation failures Chris@1296: # (eg. issues from disabled trackers) Chris@1296: # TODO: inform about that Chris@1296: redirect_to :controller => 'projects', :action => 'settings', :id => @project Chris@1296: end Chris@1296: end Chris@1296: end Chris@1296: rescue ActiveRecord::RecordNotFound Chris@1296: # source_project not found Chris@1296: render_404 Chris@1296: end Chris@1296: Chris@1296: # Show @project Chris@1296: def show Chris@1296: if params[:jump] Chris@1296: # try to redirect to the requested menu item Chris@1296: redirect_to_project_menu_item(@project, params[:jump]) && return Chris@1296: end Chris@1296: Chris@1296: @users_by_role = @project.users_by_role Chris@1296: @subprojects = @project.children.visible.all Chris@1296: @news = @project.news.find(:all, :limit => 5, :include => [ :author, :project ], :order => "#{News.table_name}.created_on DESC") Chris@1296: @trackers = @project.rolled_up_trackers Chris@1296: Chris@1296: cond = @project.project_condition(Setting.display_subprojects_issues?) Chris@1296: Chris@1296: @open_issues_by_tracker = Issue.visible.open.where(cond).count(:group => :tracker) Chris@1296: @total_issues_by_tracker = Issue.visible.where(cond).count(:group => :tracker) Chris@1296: Chris@1296: if User.current.allowed_to?(:view_time_entries, @project) Chris@1296: @total_hours = TimeEntry.visible.sum(:hours, :include => :project, :conditions => cond).to_f Chris@1296: end Chris@1296: Chris@1296: @key = User.current.rss_key Chris@1296: Chris@1296: respond_to do |format| Chris@1296: format.html Chris@1296: format.api Chris@1296: end Chris@1296: end Chris@1296: Chris@1296: def settings Chris@1296: @issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position") Chris@1296: @issue_category ||= IssueCategory.new Chris@1296: @member ||= @project.members.new Chris@1296: @trackers = Tracker.sorted.all Chris@1296: @wiki ||= @project.wiki Chris@1296: end Chris@1296: Chris@1296: def edit Chris@1296: end Chris@1296: Chris@1296: def update Chris@1296: @project.safe_attributes = params[:project] Chris@1296: if validate_parent_id && @project.save Chris@1296: @project.set_allowed_parent!(params[:project]['parent_id']) if params[:project].has_key?('parent_id') Chris@1296: respond_to do |format| Chris@1296: format.html { Chris@1296: flash[:notice] = l(:notice_successful_update) Chris@1296: redirect_to :action => 'settings', :id => @project Chris@1296: } Chris@1296: format.api { render_api_ok } Chris@1296: end Chris@1296: else Chris@1296: respond_to do |format| Chris@1296: format.html { Chris@1296: settings Chris@1296: render :action => 'settings' Chris@1296: } Chris@1296: format.api { render_validation_errors(@project) } Chris@1296: end Chris@1296: end Chris@1296: end Chris@1296: Chris@1296: def modules Chris@1296: @project.enabled_module_names = params[:enabled_module_names] Chris@1296: flash[:notice] = l(:notice_successful_update) Chris@1296: redirect_to :action => 'settings', :id => @project, :tab => 'modules' Chris@1296: end Chris@1296: Chris@1296: def archive Chris@1296: if request.post? Chris@1296: unless @project.archive Chris@1296: flash[:error] = l(:error_can_not_archive_project) Chris@1296: end Chris@1296: end Chris@1296: redirect_to(url_for(:controller => 'admin', :action => 'projects', :status => params[:status])) Chris@1296: end Chris@1296: Chris@1296: def unarchive Chris@1296: @project.unarchive if request.post? && !@project.active? Chris@1296: redirect_to(url_for(:controller => 'admin', :action => 'projects', :status => params[:status])) Chris@1296: end Chris@1296: Chris@1296: def close Chris@1296: @project.close Chris@1296: redirect_to project_path(@project) Chris@1296: end Chris@1296: Chris@1296: def reopen Chris@1296: @project.reopen Chris@1296: redirect_to project_path(@project) Chris@1296: end Chris@1296: Chris@1296: # Delete @project Chris@1296: def destroy Chris@1296: @project_to_destroy = @project Chris@1296: if api_request? || params[:confirm] Chris@1296: @project_to_destroy.destroy Chris@1296: respond_to do |format| Chris@1296: format.html { redirect_to :controller => 'admin', :action => 'projects' } Chris@1296: format.api { render_api_ok } Chris@1296: end Chris@1296: end Chris@1296: # hide project in layout Chris@1296: @project = nil Chris@1296: end Chris@1296: Chris@1296: private Chris@1296: Chris@1296: # Validates parent_id param according to user's permissions Chris@1296: # TODO: move it to Project model in a validation that depends on User.current Chris@1296: def validate_parent_id Chris@1296: return true if User.current.admin? Chris@1296: parent_id = params[:project] && params[:project][:parent_id] Chris@1296: if parent_id || @project.new_record? Chris@1296: parent = parent_id.blank? ? nil : Project.find_by_id(parent_id.to_i) Chris@1296: unless @project.allowed_parents.include?(parent) Chris@1296: @project.errors.add :parent_id, :invalid Chris@1296: return false Chris@1296: end Chris@1296: end Chris@1296: true Chris@1296: end Chris@1296: end