GitLab steht wegen Wartungsarbeiten am Montag, den 10. Mai, zwischen 17:00 und 19:00 Uhr nicht zur Verfügung.

projects_controller.rb 9.51 KB
Newer Older
1 2
# Redmine - project management software
# Copyright (C) 2006-2009  Jean-Philippe Lang
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

class ProjectsController < ApplicationController
19 20 21 22
  menu_item :overview
  menu_item :roadmap, :only => :roadmap
  menu_item :settings, :only => :settings
  
23 24
  before_filter :find_project, :except => [ :index, :list, :add, :copy ]
  before_filter :authorize, :except => [ :index, :list, :add, :copy, :archive, :unarchive, :destroy]
25 26
  before_filter :authorize_global, :only => :add
  before_filter :require_admin, :only => [ :copy, :archive, :unarchive, :destroy ]
27
  accept_key_auth :index
28
  
29 30 31 32 33 34
  after_filter :only => [:add, :edit, :archive, :unarchive, :destroy] do |controller|
    if controller.request.post?
      controller.send :expire_action, :controller => 'welcome', :action => 'robots.txt'
    end
  end
  
35 36 37 38
  helper :sort
  include SortHelper
  helper :custom_fields
  include CustomFieldsHelper   
39
  helper :issues
40 41
  helper :queries
  include QueriesHelper
42 43
  helper :repositories
  include RepositoriesHelper
44
  include ProjectsHelper
45
  
46
  # Lists visible projects
47
  def index
jplang's avatar
jplang committed
48 49
    respond_to do |format|
      format.html { 
50
        @projects = Project.visible.find(:all, :order => 'lft') 
jplang's avatar
jplang committed
51
      }
jplang's avatar
jplang committed
52 53 54
      format.xml  {
        @projects = Project.visible.find(:all, :order => 'lft')
      }
jplang's avatar
jplang committed
55
      format.atom {
56 57 58
        projects = Project.visible.find(:all, :order => 'created_on DESC',
                                              :limit => Setting.feeds_limit.to_i)
        render_feed(projects, :title => "#{Setting.app_title}: #{l(:label_project_latest)}")
jplang's avatar
jplang committed
59 60
      }
    end
61
  end
62
  
jplang's avatar
jplang committed
63
  # Add a new project
64
  def add
65
    @issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position")
66
    @trackers = Tracker.all
67 68
    @project = Project.new(params[:project])
    if request.get?
69
      @project.identifier = Project.next_identifier if Setting.sequential_project_identifiers?
70
      @project.trackers = Tracker.all
71
      @project.is_public = Setting.default_projects_public?
72
      @project.enabled_module_names = Setting.default_projects_modules
73
    else
74
      @project.enabled_module_names = params[:enabled_modules]
jplang's avatar
jplang committed
75
      if validate_parent_id && @project.save
76
        @project.set_allowed_parent!(params[:project]['parent_id']) if params[:project].has_key?('parent_id')
77 78
        # Add current user as a project member if he is not admin
        unless User.current.admin?
79 80
          r = Role.givable.find_by_id(Setting.new_project_user_role_id.to_i) || Role.givable.first
          m = Member.new(:user => User.current, :roles => [r])
81 82
          @project.members << m
        end
jplang's avatar
jplang committed
83 84 85 86 87 88 89 90 91 92 93 94
        respond_to do |format|
          format.html { 
            flash[:notice] = l(:notice_successful_create)
            redirect_to :controller => 'projects', :action => 'settings', :id => @project
          }
          format.xml  { head :created, :location => url_for(:controller => 'projects', :action => 'show', :id => @project.id) }
        end
      else
        respond_to do |format|
          format.html
          format.xml  { render :xml => @project.errors, :status => :unprocessable_entity }
        end
95
      end
96 97
    end	
  end
98 99 100 101 102 103 104
  
  def copy
    @issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position")
    @trackers = Tracker.all
    @root_projects = Project.find(:all,
                                  :conditions => "parent_id IS NULL AND status = #{Project::STATUS_ACTIVE}",
                                  :order => 'name')
105
    @source_project = Project.find(params[:id])
106
    if request.get?
107
      @project = Project.copy_from(@source_project)
108 109 110 111 112 113
      if @project
        @project.identifier = Project.next_identifier if Setting.sequential_project_identifiers?
      else
        redirect_to :controller => 'admin', :action => 'projects'
      end  
    else
114 115 116 117 118 119 120 121 122 123 124 125 126 127
      Mailer.with_deliveries(params[:notifications] == '1') do
        @project = Project.new(params[:project])
        @project.enabled_module_names = params[:enabled_modules]
        if validate_parent_id && @project.copy(@source_project, :only => params[:only])
          @project.set_allowed_parent!(params[:project]['parent_id']) if params[:project].has_key?('parent_id')
          flash[:notice] = l(:notice_successful_create)
          redirect_to :controller => 'admin', :action => 'projects'
        elsif !@project.new_record?
          # Project was created
          # But some objects were not copied due to validation failures
          # (eg. issues from disabled trackers)
          # TODO: inform about that
          redirect_to :controller => 'admin', :action => 'projects'
        end
128
      end
jplang's avatar
jplang committed
129 130 131
    end
  rescue ActiveRecord::RecordNotFound
    redirect_to :controller => 'admin', :action => 'projects'
132
  end
133 134 135
	
  # Show @project
  def show
136 137 138 139 140
    if params[:jump]
      # try to redirect to the requested menu item
      redirect_to_project_menu_item(@project, params[:jump]) && return
    end
    
141
    @users_by_role = @project.users_by_role
142
    @subprojects = @project.children.visible
143
    @news = @project.news.find(:all, :limit => 5, :include => [ :author, :project ], :order => "#{News.table_name}.created_on DESC")
144
    @trackers = @project.rolled_up_trackers
145 146
    
    cond = @project.project_condition(Setting.display_subprojects_issues?)
jplang's avatar
jplang committed
147 148
    
    @open_issues_by_tracker = Issue.visible.count(:group => :tracker,
149
                                            :include => [:project, :status, :tracker],
150
                                            :conditions => ["(#{cond}) AND #{IssueStatus.table_name}.is_closed=?", false])
jplang's avatar
jplang committed
151
    @total_issues_by_tracker = Issue.visible.count(:group => :tracker,
152
                                            :include => [:project, :status, :tracker],
153
                                            :conditions => cond)
jplang's avatar
jplang committed
154
    
155 156 157
    TimeEntry.visible_by(User.current) do
      @total_hours = TimeEntry.sum(:hours, 
                                   :include => :project,
158
                                   :conditions => cond).to_f
159
    end
jplang's avatar
jplang committed
160
    @key = User.current.rss_key
jplang's avatar
jplang committed
161 162 163 164 165
    
    respond_to do |format|
      format.html
      format.xml
    end
jplang's avatar
v0.2.0  
jplang committed
166
  end
167 168

  def settings
169
    @issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position")
170 171
    @issue_category ||= IssueCategory.new
    @member ||= @project.members.new
172
    @trackers = Tracker.all
jplang's avatar
jplang committed
173 174
    @repository ||= @project.repository
    @wiki ||= @project.wiki
175 176 177 178
  end
  
  # Edit @project
  def edit
jplang's avatar
jplang committed
179 180
    if request.get?
    else
181
      @project.attributes = params[:project]
jplang's avatar
jplang committed
182
      if validate_parent_id && @project.save
183
        @project.set_allowed_parent!(params[:project]['parent_id']) if params[:project].has_key?('parent_id')
jplang's avatar
jplang committed
184 185 186 187 188 189 190
        respond_to do |format|
          format.html { 
            flash[:notice] = l(:notice_successful_update)
            redirect_to :action => 'settings', :id => @project
          }
          format.xml  { head :ok }
        end
191
      else
jplang's avatar
jplang committed
192 193 194 195 196 197 198
        respond_to do |format|
          format.html { 
            settings
            render :action => 'settings'
          }
          format.xml  { render :xml => @project.errors, :status => :unprocessable_entity }
        end
199
      end
jplang's avatar
v0.2.0  
jplang committed
200
    end
201
  end
jplang's avatar
jplang committed
202 203 204
  
  def modules
    @project.enabled_module_names = params[:enabled_modules]
205
    flash[:notice] = l(:notice_successful_update)
jplang's avatar
jplang committed
206 207
    redirect_to :action => 'settings', :id => @project, :tab => 'modules'
  end
208

209
  def archive
210 211 212 213 214
    if request.post?
      unless @project.archive
        flash[:error] = l(:error_can_not_archive_project)
      end
    end
215
    redirect_to(url_for(:controller => 'admin', :action => 'projects', :status => params[:status]))
216 217 218 219
  end
  
  def unarchive
    @project.unarchive if request.post? && !@project.active?
220
    redirect_to(url_for(:controller => 'admin', :action => 'projects', :status => params[:status]))
221 222
  end
  
jplang's avatar
jplang committed
223
  # Delete @project
224
  def destroy
225
    @project_to_destroy = @project
jplang's avatar
jplang committed
226 227 228 229 230 231 232 233 234 235
    if request.get?
      # display confirmation view
    else
      if params[:format] == 'xml' || params[:confirm]
        @project_to_destroy.destroy
        respond_to do |format|
          format.html { redirect_to :controller => 'admin', :action => 'projects' }
          format.xml  { head :ok }
        end
      end
236
    end
237 238
    # hide project in layout
    @project = nil
239 240 241
  end

private
jplang's avatar
jplang committed
242 243 244 245 246 247 248 249
  def find_optional_project
    return true unless params[:id]
    @project = Project.find(params[:id])
    authorize
  rescue ActiveRecord::RecordNotFound
    render_404
  end

jplang's avatar
jplang committed
250 251 252 253 254 255 256 257 258 259 260 261 262 263
  # Validates parent_id param according to user's permissions
  # TODO: move it to Project model in a validation that depends on User.current
  def validate_parent_id
    return true if User.current.admin?
    parent_id = params[:project] && params[:project][:parent_id]
    if parent_id || @project.new_record?
      parent = parent_id.blank? ? nil : Project.find_by_id(parent_id.to_i)
      unless @project.allowed_parents.include?(parent)
        @project.errors.add :parent_id, :invalid
        return false
      end
    end
    true
  end
jplang's avatar
jplang committed
264
end