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