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 @ 1304:6137548ba453

History | View | Annotate | Download (11.8 KB)

1
# Redmine - project management software
2
# Copyright (C) 2006-2011  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.txt'
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
      format.api  {
64
        @offset, @limit = api_offset_and_limit
65
        @project_count = Project.visible.count
66
        @projects = Project.visible.all(:offset => @offset, :limit => @limit, :order => 'lft')
67
      }
68
      format.atom {
69
        projects = Project.visible.find(:all, :order => 'created_on DESC',
70
                                              :limit => Setting.feeds_limit.to_i)
71
        render_feed(projects, :title => "#{Setting.app_title}: #{l(:label_project_latest)}")
72
      }
73
    end
74
  end
75

    
76
  # A different view of projects using explore boxes
77
  def explore
78
    respond_to do |format|
79
      format.html {
80
        @projects = Project.visible
81
        render :template => 'projects/explore.html.erb', :layout => !request.xhr?
82
      }
83
    end
84
  end
85

    
86
  def new
87
    @issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position")
88
    @trackers = Tracker.all
89
    @project = Project.new
90
    @project.safe_attributes = params[:project]
91
  end
92

    
93
  verify :method => :post, :only => :create, :render => {:nothing => true, :status => :method_not_allowed }
94
  def create
95
    @issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position")
96
    @trackers = Tracker.all
97
    @project = Project.new
98
    @project.safe_attributes = params[:project]
99

    
100

    
101
    # todo: luisf: this should be removed from here...
102
    if params && params[:project] && !params[:project][:tag_list].nil?
103
      new_tags = params[:project][:tag_list].to_s.downcase
104

    
105
      @project.tag_list = ActionController::Base.helpers.strip_tags(new_tags)
106
    end
107
    # end of code to be removed
108

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

    
134
  end
135

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

    
178
    @users_by_role = @project.users_by_role
179
    @subprojects = @project.children.visible.all
180
    @news = @project.news.find(:all, :limit => 5, :include => [ :author, :project ], :order => "#{News.table_name}.created_on DESC")
181
    @trackers = @project.rolled_up_trackers
182

    
183
    cond = @project.project_condition(Setting.display_subprojects_issues?)
184

    
185
    @open_issues_by_tracker = Issue.visible.count(:group => :tracker,
186
                                            :include => [:project, :status, :tracker],
187
                                            :conditions => ["(#{cond}) AND #{IssueStatus.table_name}.is_closed=?", false])
188
    @total_issues_by_tracker = Issue.visible.count(:group => :tracker,
189
                                            :include => [:project, :status, :tracker],
190
                                            :conditions => cond)
191

    
192
    if User.current.allowed_to?(:view_time_entries, @project)
193
      @total_hours = TimeEntry.visible.sum(:hours, :include => :project, :conditions => cond).to_f
194
    end
195

    
196
    @key = User.current.rss_key
197

    
198
    respond_to do |format|
199
      format.html
200
      format.api
201
    end
202
  end
203

    
204
  def settings
205
    @issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position")
206
    @issue_category ||= IssueCategory.new
207
    @member ||= @project.members.new
208
    @trackers = Tracker.all
209
    @repository ||= @project.repository
210
    @wiki ||= @project.wiki
211
  end
212

    
213
  def edit
214
  end
215

    
216
  # TODO: convert to PUT only
217
  verify :method => [:post, :put], :only => :update, :render => {:nothing => true, :status => :method_not_allowed }
218
  def update
219
    @project.safe_attributes = params[:project]
220
    if validate_parent_id && @project.save
221
      @project.set_allowed_parent!(params[:project]['parent_id']) if params[:project].has_key?('parent_id')
222
      respond_to do |format|
223
        format.html {
224
          flash[:notice] = l(:notice_successful_update)
225
          redirect_to :action => 'settings', :id => @project
226
        }
227
        format.api  { head :ok }
228
      end
229
    else
230
      respond_to do |format|
231
        format.html {
232
          settings
233
          render :action => 'settings'
234
        }
235
        format.api  { render_validation_errors(@project) }
236
      end
237
    end
238
  end
239

    
240
  verify :method => :post, :only => :modules, :render => {:nothing => true, :status => :method_not_allowed }
241
  
242
  def overview
243
    @project.has_welcome_page = params[:has_welcome_page]
244
    if @project.save
245
      flash[:notice] = l(:notice_successful_update)
246
    end
247
    redirect_to :action => 'settings', :id => @project, :tab => 'overview'
248
  end
249

    
250
  def modules
251
    @project.enabled_module_names = params[:enabled_module_names]
252
    flash[:notice] = l(:notice_successful_update)
253
    redirect_to :action => 'settings', :id => @project, :tab => 'modules'
254
  end
255

    
256
  def archive
257
    if request.post?
258
      unless @project.archive
259
        flash[:error] = l(:error_can_not_archive_project)
260
      end
261
    end
262
    redirect_to(url_for(:controller => 'admin', :action => 'projects', :status => params[:status]))
263
  end
264

    
265
  def unarchive
266
    @project.unarchive if request.post? && !@project.active?
267
    redirect_to(url_for(:controller => 'admin', :action => 'projects', :status => params[:status]))
268
  end
269

    
270
  # Delete @project
271
  def destroy
272
    @project_to_destroy = @project
273
    if request.get?
274
      # display confirmation view
275
    else
276
      if api_request? || params[:confirm]
277
        @project_to_destroy.destroy
278
        respond_to do |format|
279
          format.html { redirect_to :controller => 'admin', :action => 'projects' }
280
          format.api  { head :ok }
281
        end
282
      end
283
    end
284
    # hide project in layout
285
    @project = nil
286
  end
287

    
288
private
289
  def find_optional_project
290
    return true unless params[:id]
291
    @project = Project.find(params[:id])
292
    authorize
293
  rescue ActiveRecord::RecordNotFound
294
    render_404
295
  end
296

    
297
  def validate_is_public_key
298
    # Although is_public isn't mandatory in the project model (it gets
299
    # defaulted), it must be present in params -- it can be true or
300
    # false, but it must be there. This permits us to make forms in
301
    # which the user _has_ to select public or private (rather than
302
    # defaulting it) if we want to
303
    if params.nil? || params[:project].nil? || !params[:project].has_key?(:is_public)
304
      @project.errors.add :is_public, :public_or_private
305
      return false
306
    end
307
    true
308
  end
309

    
310
  # Validates parent_id param according to user's permissions
311
  # TODO: move it to Project model in a validation that depends on User.current
312
  def validate_parent_id
313
    return true if User.current.admin?
314
    parent_id = params[:project] && params[:project][:parent_id]
315
    if parent_id || @project.new_record?
316
      parent = parent_id.blank? ? nil : Project.find_by_id(parent_id.to_i)
317
      unless @project.allowed_parents.include?(parent)
318
        @project.errors.add :parent_id, :invalid
319
        return false
320
      end
321
    end
322
    true
323
  end
324
end