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