To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Tag: | Revision:

root / app / controllers / projects_controller.rb @ 1273:052ea7c838f6

History | View | Annotate | Download (10.8 KB)

1
# Redmine - project management software
2
# Copyright (C) 2006-2012  Jean-Philippe Lang
3
#
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.
8
#
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.
13
#
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.
17

    
18
class ProjectsController < ApplicationController
19
  menu_item :overview
20
  menu_item :roadmap, :only => :roadmap
21
  menu_item :settings, :only => :settings
22

    
23
  before_filter :find_project, :except => [ :index, :list, :explore, :new, :create, :copy ]
24
  before_filter :authorize, :except => [ :index, :list, :explore, :new, :create, :copy, :archive, :unarchive, :destroy]
25
  before_filter :authorize_global, :only => [:new, :create]
26
  before_filter :require_admin, :only => [ :copy, :archive, :unarchive, :destroy ]
27
  accept_rss_auth :index
28
  accept_api_auth :index, :show, :create, :update, :destroy
29

    
30
  after_filter :only => [:create, :edit, :update, :archive, :unarchive, :destroy] do |controller|
31
    if controller.request.post?
32
      controller.send :expire_action, :controller => 'welcome', :action => 'robots'
33
    end
34
  end
35

    
36
  helper :sort
37
  include SortHelper
38
  helper :custom_fields
39
  include CustomFieldsHelper
40
  helper :issues
41
  helper :queries
42
  include QueriesHelper
43
  helper :repositories
44
  include RepositoriesHelper
45
  include ProjectsHelper
46
  include ActivitiesHelper
47
  helper :activities
48

    
49
  # Lists visible projects. Paginator is for top-level projects only
50
  # (subprojects belong to them)
51
  def index
52
    respond_to do |format|
53
      format.html {
54
        sort_init 'name'
55
        sort_update %w(name lft created_on updated_on)
56
        @limit = per_page_option
57
        @project_count = Project.visible_roots.count
58
        @project_pages = Paginator.new self, @project_count, @limit, params['page']
59
        @offset ||= @project_pages.current.offset
60
        @projects = Project.visible_roots.all(:offset => @offset, :limit => @limit, :order => sort_clause)
61
        render :template => 'projects/index.html.erb', :layout => !request.xhr?
62

    
63
## Redmine 2.2:
64
#        scope = Project
65
#        unless params[:closed]
66
#          scope = scope.active
67
#        end
68
#        @projects = scope.visible.order('lft').all
69
      }
70
      format.api  {
71
        @offset, @limit = api_offset_and_limit
72
        @project_count = Project.visible.count
73
        @projects = Project.visible.all(:offset => @offset, :limit => @limit, :order => 'lft')
74
      }
75
      format.atom {
76
        projects = Project.visible.find(:all, :order => 'created_on DESC',
77
                                              :limit => Setting.feeds_limit.to_i)
78
        render_feed(projects, :title => "#{Setting.app_title}: #{l(:label_project_latest)}")
79
      }
80
    end
81
  end
82

    
83
  # A different view of projects using explore boxes
84
  def explore
85
    respond_to do |format|
86
      format.html {
87
        @projects = Project.visible
88
        render :template => 'projects/explore.html.erb', :layout => !request.xhr?
89
      }
90
    end
91
  end
92

    
93
  def new
94
    @issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position")
95
    @trackers = Tracker.sorted.all
96
    @project = Project.new
97
    @project.safe_attributes = params[:project]
98
  end
99

    
100
  def create
101
    @issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position")
102
    @trackers = Tracker.sorted.all
103
    @project = Project.new
104
    @project.safe_attributes = params[:project]
105

    
106
    if validate_is_public_key && validate_parent_id && @project.save
107
      @project.set_allowed_parent!(params[:project]['parent_id']) if params[:project].has_key?('parent_id')
108
      # Add current user as a project member if he is not admin
109
      unless User.current.admin?
110
        r = Role.givable.find_by_id(Setting.new_project_user_role_id.to_i) || Role.givable.first
111
        m = Member.new(:user => User.current, :roles => [r])
112
        @project.members << m
113
      end
114
      respond_to do |format|
115
        format.html {
116
          flash[:notice] = l(:notice_successful_create)
117
          redirect_to(params[:continue] ?
118
            {:controller => 'projects', :action => 'new', :project => {:parent_id => @project.parent_id}.reject {|k,v| v.nil?}} :
119
            {:controller => 'projects', :action => 'settings', :id => @project}
120
          )
121
        }
122
        format.api  { render :action => 'show', :status => :created, :location => url_for(:controller => 'projects', :action => 'show', :id => @project.id) }
123
      end
124
    else
125
      respond_to do |format|
126
        format.html { render :action => 'new' }
127
        format.api  { render_validation_errors(@project) }
128
      end
129
    end
130

    
131
  end
132

    
133
  def copy
134
    @issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position")
135
    @trackers = Tracker.sorted.all
136
    @root_projects = Project.find(:all,
137
                                  :conditions => "parent_id IS NULL AND status = #{Project::STATUS_ACTIVE}",
138
                                  :order => 'name')
139
    @source_project = Project.find(params[:id])
140
    if request.get?
141
      @project = Project.copy_from(@source_project)
142
      @project.identifier = Project.next_identifier if Setting.sequential_project_identifiers?
143
    else
144
      Mailer.with_deliveries(params[:notifications] == '1') do
145
        @project = Project.new
146
        @project.safe_attributes = params[:project]
147
        if validate_parent_id && @project.copy(@source_project, :only => params[:only])
148
          @project.set_allowed_parent!(params[:project]['parent_id']) if params[:project].has_key?('parent_id')
149
          flash[:notice] = l(:notice_successful_create)
150
          redirect_to :controller => 'projects', :action => 'settings', :id => @project
151
        elsif !@project.new_record?
152
          # Project was created
153
          # But some objects were not copied due to validation failures
154
          # (eg. issues from disabled trackers)
155
          # TODO: inform about that
156
          redirect_to :controller => 'projects', :action => 'settings', :id => @project
157
        end
158
      end
159
    end
160
  rescue ActiveRecord::RecordNotFound
161
    # source_project not found
162
    render_404
163
  end
164

    
165
  # Show @project
166
  def show
167
    if params[:jump]
168
      # try to redirect to the requested menu item
169
      redirect_to_project_menu_item(@project, params[:jump]) && return
170
    end
171

    
172
    @users_by_role = @project.users_by_role
173
    @subprojects = @project.children.visible.all
174
    @news = @project.news.find(:all, :limit => 5, :include => [ :author, :project ], :order => "#{News.table_name}.created_on DESC")
175
    @trackers = @project.rolled_up_trackers
176

    
177
    cond = @project.project_condition(Setting.display_subprojects_issues?)
178

    
179
    @open_issues_by_tracker = Issue.visible.open.where(cond).count(:group => :tracker)
180
    @total_issues_by_tracker = Issue.visible.where(cond).count(:group => :tracker)
181

    
182
    if User.current.allowed_to?(:view_time_entries, @project)
183
      @total_hours = TimeEntry.visible.sum(:hours, :include => :project, :conditions => cond).to_f
184
    end
185

    
186
    @key = User.current.rss_key
187

    
188
    respond_to do |format|
189
      format.html
190
      format.api
191
    end
192
  end
193

    
194
  def settings
195
    @issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position")
196
    @issue_category ||= IssueCategory.new
197
    @member ||= @project.members.new
198
    @trackers = Tracker.sorted.all
199
    @wiki ||= @project.wiki
200
  end
201

    
202
  def edit
203
  end
204

    
205
  def update
206
    @project.safe_attributes = params[:project]
207
    if validate_parent_id && @project.save
208
      @project.set_allowed_parent!(params[:project]['parent_id']) if params[:project].has_key?('parent_id')
209
      respond_to do |format|
210
        format.html {
211
          flash[:notice] = l(:notice_successful_update)
212
          redirect_to :action => 'settings', :id => @project
213
        }
214
        format.api  { render_api_ok }
215
      end
216
    else
217
      respond_to do |format|
218
        format.html {
219
          settings
220
          render :action => 'settings'
221
        }
222
        format.api  { render_validation_errors(@project) }
223
      end
224
    end
225
  end
226

    
227
  def overview
228
    @project.has_welcome_page = params[:has_welcome_page]
229
    if @project.save
230
      flash[:notice] = l(:notice_successful_update)
231
    end
232
    redirect_to :action => 'settings', :id => @project, :tab => 'overview'
233
  end
234

    
235
  def modules
236
    @project.enabled_module_names = params[:enabled_module_names]
237
    flash[:notice] = l(:notice_successful_update)
238
    redirect_to :action => 'settings', :id => @project, :tab => 'modules'
239
  end
240

    
241
  def archive
242
    if request.post?
243
      unless @project.archive
244
        flash[:error] = l(:error_can_not_archive_project)
245
      end
246
    end
247
    redirect_to(url_for(:controller => 'admin', :action => 'projects', :status => params[:status]))
248
  end
249

    
250
  def unarchive
251
    @project.unarchive if request.post? && !@project.active?
252
    redirect_to(url_for(:controller => 'admin', :action => 'projects', :status => params[:status]))
253
  end
254

    
255
  def close
256
    @project.close
257
    redirect_to project_path(@project)
258
  end
259

    
260
  def reopen
261
    @project.reopen
262
    redirect_to project_path(@project)
263
  end
264

    
265
  # Delete @project
266
  def destroy
267
    @project_to_destroy = @project
268
    if api_request? || params[:confirm]
269
      @project_to_destroy.destroy
270
      respond_to do |format|
271
        format.html { redirect_to :controller => 'admin', :action => 'projects' }
272
        format.api  { render_api_ok }
273
      end
274
    end
275
    # hide project in layout
276
    @project = nil
277
  end
278

    
279
  private
280

    
281
  def validate_is_public_key
282
    # Although is_public isn't mandatory in the project model (it gets
283
    # defaulted), it must be present in params -- it can be true or
284
    # false, but it must be there. This permits us to make forms in
285
    # which the user _has_ to select public or private (rather than
286
    # defaulting it) if we want to
287
    if params.nil? || params[:project].nil? || !params[:project].has_key?(:is_public)
288
      @project.errors.add :is_public, :public_or_private
289
      return false
290
    end
291
    true
292
  end
293

    
294
  # Validates parent_id param according to user's permissions
295
  # TODO: move it to Project model in a validation that depends on User.current
296
  def validate_parent_id
297
    return true if User.current.admin?
298
    parent_id = params[:project] && params[:project][:parent_id]
299
    if parent_id || @project.new_record?
300
      parent = parent_id.blank? ? nil : Project.find_by_id(parent_id.to_i)
301
      unless @project.allowed_parents.include?(parent)
302
        @project.errors.add :parent_id, :invalid
303
        return false
304
      end
305
    end
306
    true
307
  end
308
end