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 @ 1361:7c0909052511

History | View | Annotate | Download (10.8 KB)

1 0:513646585e45 Chris
# Redmine - project management software
2 1115:433d4f72a19b Chris
# Copyright (C) 2006-2012  Jean-Philippe Lang
3 0:513646585e45 Chris
#
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 909:cbb26bc654de Chris
#
9 0:513646585e45 Chris
# 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 909:cbb26bc654de Chris
#
14 0:513646585e45 Chris
# 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 909:cbb26bc654de Chris
23 1009:066b55d7c053 chris
  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 22:40f7cfd4df19 chris
  before_filter :authorize_global, :only => [:new, :create]
26 0:513646585e45 Chris
  before_filter :require_admin, :only => [ :copy, :archive, :unarchive, :destroy ]
27 507:0c939c159af4 Chris
  accept_rss_auth :index
28
  accept_api_auth :index, :show, :create, :update, :destroy
29 22:40f7cfd4df19 chris
30
  after_filter :only => [:create, :edit, :update, :archive, :unarchive, :destroy] do |controller|
31 0:513646585e45 Chris
    if controller.request.post?
32 1115:433d4f72a19b Chris
      controller.send :expire_action, :controller => 'welcome', :action => 'robots'
33 0:513646585e45 Chris
    end
34
  end
35 22:40f7cfd4df19 chris
36 0:513646585e45 Chris
  helper :sort
37
  include SortHelper
38
  helper :custom_fields
39 909:cbb26bc654de Chris
  include CustomFieldsHelper
40 0:513646585e45 Chris
  helper :issues
41
  helper :queries
42
  include QueriesHelper
43
  helper :repositories
44
  include RepositoriesHelper
45
  include ProjectsHelper
46 1011:f44860e089c5 chris
  include ActivitiesHelper
47
  helper :activities
48 909:cbb26bc654de Chris
49 205:05f9a2a9c753 chris
  # Lists visible projects. Paginator is for top-level projects only
50
  # (subprojects belong to them)
51 0:513646585e45 Chris
  def index
52
    respond_to do |format|
53 909:cbb26bc654de Chris
      format.html {
54 205:05f9a2a9c753 chris
        sort_init 'name'
55
        sort_update %w(name lft created_on updated_on)
56 131:513e61d1b4da chris
        @limit = per_page_option
57 205:05f9a2a9c753 chris
        @project_count = Project.visible_roots.count
58 131:513e61d1b4da chris
        @project_pages = Paginator.new self, @project_count, @limit, params['page']
59
        @offset ||= @project_pages.current.offset
60 1128:cd2db8872493 luis
        @projects = Project.visible_roots.all(:offset => @offset, :limit => @limit, :order => sort_clause)
61 919:b03f28dd9026 Chris
        render :template => 'projects/index.html.erb', :layout => !request.xhr?
62 1116:bb32da3bea34 Chris
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 0:513646585e45 Chris
      }
70 117:af80e5618e9b Chris
      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 0:513646585e45 Chris
      }
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 909:cbb26bc654de Chris
83 1009:066b55d7c053 chris
  # A different view of projects using explore boxes
84
  def explore
85 1007:3e52bc15dd67 chris
    respond_to do |format|
86
      format.html {
87
        @projects = Project.visible
88 1009:066b55d7c053 chris
        render :template => 'projects/explore.html.erb', :layout => !request.xhr?
89 1007:3e52bc15dd67 chris
      }
90
    end
91
  end
92
93 22:40f7cfd4df19 chris
  def new
94 0:513646585e45 Chris
    @issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position")
95 1115:433d4f72a19b Chris
    @trackers = Tracker.sorted.all
96 929:5f33065ddc4b Chris
    @project = Project.new
97
    @project.safe_attributes = params[:project]
98 22:40f7cfd4df19 chris
  end
99
100
  def create
101
    @issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position")
102 1115:433d4f72a19b Chris
    @trackers = Tracker.sorted.all
103 117:af80e5618e9b Chris
    @project = Project.new
104
    @project.safe_attributes = params[:project]
105 22:40f7cfd4df19 chris
106 1034:ea5d9652c6f6 chris
    if validate_is_public_key && validate_parent_id && @project.save
107 22:40f7cfd4df19 chris
      @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 909:cbb26bc654de Chris
        format.html {
116 22:40f7cfd4df19 chris
          flash[:notice] = l(:notice_successful_create)
117 909:cbb26bc654de Chris
          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 22:40f7cfd4df19 chris
        }
122 117:af80e5618e9b Chris
        format.api  { render :action => 'show', :status => :created, :location => url_for(:controller => 'projects', :action => 'show', :id => @project.id) }
123 22:40f7cfd4df19 chris
      end
124 0:513646585e45 Chris
    else
125 22:40f7cfd4df19 chris
      respond_to do |format|
126
        format.html { render :action => 'new' }
127 117:af80e5618e9b Chris
        format.api  { render_validation_errors(@project) }
128 0:513646585e45 Chris
      end
129 22:40f7cfd4df19 chris
    end
130 909:cbb26bc654de Chris
131 0:513646585e45 Chris
  end
132 909:cbb26bc654de Chris
133 0:513646585e45 Chris
  def copy
134
    @issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position")
135 1115:433d4f72a19b Chris
    @trackers = Tracker.sorted.all
136 0:513646585e45 Chris
    @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 1115:433d4f72a19b Chris
      @project.identifier = Project.next_identifier if Setting.sequential_project_identifiers?
143 0:513646585e45 Chris
    else
144
      Mailer.with_deliveries(params[:notifications] == '1') do
145 117:af80e5618e9b Chris
        @project = Project.new
146
        @project.safe_attributes = params[:project]
147 0:513646585e45 Chris
        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 117:af80e5618e9b Chris
          redirect_to :controller => 'projects', :action => 'settings', :id => @project
151 0:513646585e45 Chris
        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 117:af80e5618e9b Chris
          redirect_to :controller => 'projects', :action => 'settings', :id => @project
157 0:513646585e45 Chris
        end
158
      end
159
    end
160
  rescue ActiveRecord::RecordNotFound
161 1115:433d4f72a19b Chris
    # source_project not found
162
    render_404
163 0:513646585e45 Chris
  end
164 1128:cd2db8872493 luis
165 0:513646585e45 Chris
  # 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 909:cbb26bc654de Chris
172 0:513646585e45 Chris
    @users_by_role = @project.users_by_role
173 441:cbce1fd3b1b7 Chris
    @subprojects = @project.children.visible.all
174 0:513646585e45 Chris
    @news = @project.news.find(:all, :limit => 5, :include => [ :author, :project ], :order => "#{News.table_name}.created_on DESC")
175
    @trackers = @project.rolled_up_trackers
176 909:cbb26bc654de Chris
177 0:513646585e45 Chris
    cond = @project.project_condition(Setting.display_subprojects_issues?)
178 909:cbb26bc654de Chris
179 1115:433d4f72a19b Chris
    @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 909:cbb26bc654de Chris
182 441:cbce1fd3b1b7 Chris
    if User.current.allowed_to?(:view_time_entries, @project)
183
      @total_hours = TimeEntry.visible.sum(:hours, :include => :project, :conditions => cond).to_f
184 0:513646585e45 Chris
    end
185 909:cbb26bc654de Chris
186 0:513646585e45 Chris
    @key = User.current.rss_key
187 909:cbb26bc654de Chris
188 0:513646585e45 Chris
    respond_to do |format|
189
      format.html
190 117:af80e5618e9b Chris
      format.api
191 0:513646585e45 Chris
    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 1115:433d4f72a19b Chris
    @trackers = Tracker.sorted.all
199 1354:fc0fecf09eb9 chris
    @repository ||= @project.repository
200 0:513646585e45 Chris
    @wiki ||= @project.wiki
201
  end
202 909:cbb26bc654de Chris
203 0:513646585e45 Chris
  def edit
204 22:40f7cfd4df19 chris
  end
205
206
  def update
207 117:af80e5618e9b Chris
    @project.safe_attributes = params[:project]
208 22:40f7cfd4df19 chris
    if validate_parent_id && @project.save
209
      @project.set_allowed_parent!(params[:project]['parent_id']) if params[:project].has_key?('parent_id')
210
      respond_to do |format|
211 909:cbb26bc654de Chris
        format.html {
212 22:40f7cfd4df19 chris
          flash[:notice] = l(:notice_successful_update)
213
          redirect_to :action => 'settings', :id => @project
214
        }
215 1115:433d4f72a19b Chris
        format.api  { render_api_ok }
216 22:40f7cfd4df19 chris
      end
217 0:513646585e45 Chris
    else
218 22:40f7cfd4df19 chris
      respond_to do |format|
219 909:cbb26bc654de Chris
        format.html {
220 22:40f7cfd4df19 chris
          settings
221
          render :action => 'settings'
222
        }
223 117:af80e5618e9b Chris
        format.api  { render_validation_errors(@project) }
224 0:513646585e45 Chris
      end
225
    end
226
  end
227 117:af80e5618e9b Chris
228 351:ebf53b46f3f3 chris
  def overview
229
    @project.has_welcome_page = params[:has_welcome_page]
230
    if @project.save
231
      flash[:notice] = l(:notice_successful_update)
232
    end
233
    redirect_to :action => 'settings', :id => @project, :tab => 'overview'
234
  end
235
236 0:513646585e45 Chris
  def modules
237 117:af80e5618e9b Chris
    @project.enabled_module_names = params[:enabled_module_names]
238 0:513646585e45 Chris
    flash[:notice] = l(:notice_successful_update)
239
    redirect_to :action => 'settings', :id => @project, :tab => 'modules'
240
  end
241
242
  def archive
243
    if request.post?
244
      unless @project.archive
245
        flash[:error] = l(:error_can_not_archive_project)
246
      end
247
    end
248
    redirect_to(url_for(:controller => 'admin', :action => 'projects', :status => params[:status]))
249
  end
250 909:cbb26bc654de Chris
251 0:513646585e45 Chris
  def unarchive
252
    @project.unarchive if request.post? && !@project.active?
253
    redirect_to(url_for(:controller => 'admin', :action => 'projects', :status => params[:status]))
254
  end
255 909:cbb26bc654de Chris
256 1115:433d4f72a19b Chris
  def close
257
    @project.close
258
    redirect_to project_path(@project)
259
  end
260
261
  def reopen
262
    @project.reopen
263
    redirect_to project_path(@project)
264
  end
265
266 0:513646585e45 Chris
  # Delete @project
267
  def destroy
268
    @project_to_destroy = @project
269 1115:433d4f72a19b Chris
    if api_request? || params[:confirm]
270
      @project_to_destroy.destroy
271
      respond_to do |format|
272
        format.html { redirect_to :controller => 'admin', :action => 'projects' }
273
        format.api  { render_api_ok }
274 0:513646585e45 Chris
      end
275
    end
276
    # hide project in layout
277
    @project = nil
278
  end
279
280 1115:433d4f72a19b Chris
  private
281 0:513646585e45 Chris
282 1034:ea5d9652c6f6 chris
  def validate_is_public_key
283
    # Although is_public isn't mandatory in the project model (it gets
284
    # defaulted), it must be present in params -- it can be true or
285
    # false, but it must be there. This permits us to make forms in
286
    # which the user _has_ to select public or private (rather than
287
    # defaulting it) if we want to
288
    if params.nil? || params[:project].nil? || !params[:project].has_key?(:is_public)
289
      @project.errors.add :is_public, :public_or_private
290
      return false
291
    end
292
    true
293
  end
294
295 0:513646585e45 Chris
  # Validates parent_id param according to user's permissions
296
  # TODO: move it to Project model in a validation that depends on User.current
297
  def validate_parent_id
298
    return true if User.current.admin?
299
    parent_id = params[:project] && params[:project][:parent_id]
300
    if parent_id || @project.new_record?
301
      parent = parent_id.blank? ? nil : Project.find_by_id(parent_id.to_i)
302
      unless @project.allowed_parents.include?(parent)
303
        @project.errors.add :parent_id, :invalid
304
        return false
305
      end
306
    end
307
    true
308
  end
309
end