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 @ 1298:4f746d8966dd

History | View | Annotate | Download (10.1 KB)

1 0:513646585e45 Chris
# Redmine - project management software
2 1295:622f24f53b42 Chris
# Copyright (C) 2006-2013  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 1295:622f24f53b42 Chris
  helper :members
47 1011:f44860e089c5 chris
  include ActivitiesHelper
48
  helper :activities
49 909:cbb26bc654de Chris
50 205:05f9a2a9c753 chris
  # Lists visible projects. Paginator is for top-level projects only
51
  # (subprojects belong to them)
52 0:513646585e45 Chris
  def index
53
    respond_to do |format|
54 909:cbb26bc654de Chris
      format.html {
55 205:05f9a2a9c753 chris
        sort_init 'name'
56
        sort_update %w(name lft created_on updated_on)
57 131:513e61d1b4da chris
        @limit = per_page_option
58 205:05f9a2a9c753 chris
        @project_count = Project.visible_roots.count
59 131:513e61d1b4da chris
        @project_pages = Paginator.new self, @project_count, @limit, params['page']
60
        @offset ||= @project_pages.current.offset
61 1128:cd2db8872493 luis
        @projects = Project.visible_roots.all(:offset => @offset, :limit => @limit, :order => sort_clause)
62 919:b03f28dd9026 Chris
        render :template => 'projects/index.html.erb', :layout => !request.xhr?
63 1116:bb32da3bea34 Chris
64
## Redmine 2.2:
65
#        scope = Project
66
#        unless params[:closed]
67
#          scope = scope.active
68
#        end
69
#        @projects = scope.visible.order('lft').all
70 0:513646585e45 Chris
      }
71 117:af80e5618e9b Chris
      format.api  {
72
        @offset, @limit = api_offset_and_limit
73
        @project_count = Project.visible.count
74 1295:622f24f53b42 Chris
        @projects = Project.visible.offset(@offset).limit(@limit).order('lft').all
75 0:513646585e45 Chris
      }
76
      format.atom {
77 1295:622f24f53b42 Chris
        projects = Project.visible.order('created_on DESC').limit(Setting.feeds_limit.to_i).all
78 0:513646585e45 Chris
        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 1295:622f24f53b42 Chris
    @issue_custom_fields = IssueCustomField.sorted.all
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 1295:622f24f53b42 Chris
    @issue_custom_fields = IssueCustomField.sorted.all
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 1295:622f24f53b42 Chris
          if params[:continue]
118
            attrs = {:parent_id => @project.parent_id}.reject {|k,v| v.nil?}
119
            redirect_to new_project_path(attrs)
120
          else
121
            redirect_to settings_project_path(@project)
122
          end
123 22:40f7cfd4df19 chris
        }
124 117:af80e5618e9b Chris
        format.api  { render :action => 'show', :status => :created, :location => url_for(:controller => 'projects', :action => 'show', :id => @project.id) }
125 22:40f7cfd4df19 chris
      end
126 0:513646585e45 Chris
    else
127 22:40f7cfd4df19 chris
      respond_to do |format|
128
        format.html { render :action => 'new' }
129 117:af80e5618e9b Chris
        format.api  { render_validation_errors(@project) }
130 0:513646585e45 Chris
      end
131 22:40f7cfd4df19 chris
    end
132 0:513646585e45 Chris
  end
133 909:cbb26bc654de Chris
134 0:513646585e45 Chris
  def copy
135 1295:622f24f53b42 Chris
    @issue_custom_fields = IssueCustomField.sorted.all
136 1115:433d4f72a19b Chris
    @trackers = Tracker.sorted.all
137 0:513646585e45 Chris
    @source_project = Project.find(params[:id])
138
    if request.get?
139
      @project = Project.copy_from(@source_project)
140 1115:433d4f72a19b Chris
      @project.identifier = Project.next_identifier if Setting.sequential_project_identifiers?
141 0:513646585e45 Chris
    else
142
      Mailer.with_deliveries(params[:notifications] == '1') do
143 117:af80e5618e9b Chris
        @project = Project.new
144
        @project.safe_attributes = params[:project]
145 0:513646585e45 Chris
        if validate_parent_id && @project.copy(@source_project, :only => params[:only])
146
          @project.set_allowed_parent!(params[:project]['parent_id']) if params[:project].has_key?('parent_id')
147
          flash[:notice] = l(:notice_successful_create)
148 1295:622f24f53b42 Chris
          redirect_to settings_project_path(@project)
149 0:513646585e45 Chris
        elsif !@project.new_record?
150
          # Project was created
151
          # But some objects were not copied due to validation failures
152
          # (eg. issues from disabled trackers)
153
          # TODO: inform about that
154 1295:622f24f53b42 Chris
          redirect_to settings_project_path(@project)
155 0:513646585e45 Chris
        end
156
      end
157
    end
158
  rescue ActiveRecord::RecordNotFound
159 1115:433d4f72a19b Chris
    # source_project not found
160
    render_404
161 0:513646585e45 Chris
  end
162 1295:622f24f53b42 Chris
163 0:513646585e45 Chris
  # Show @project
164
  def show
165 1295:622f24f53b42 Chris
    # try to redirect to the requested menu item
166
    if params[:jump] && redirect_to_project_menu_item(@project, params[:jump])
167
      return
168 0:513646585e45 Chris
    end
169 909:cbb26bc654de Chris
170 0:513646585e45 Chris
    @users_by_role = @project.users_by_role
171 441:cbce1fd3b1b7 Chris
    @subprojects = @project.children.visible.all
172 1295:622f24f53b42 Chris
    @news = @project.news.limit(5).includes(:author, :project).reorder("#{News.table_name}.created_on DESC").all
173 0:513646585e45 Chris
    @trackers = @project.rolled_up_trackers
174 909:cbb26bc654de Chris
175 0:513646585e45 Chris
    cond = @project.project_condition(Setting.display_subprojects_issues?)
176 909:cbb26bc654de Chris
177 1115:433d4f72a19b Chris
    @open_issues_by_tracker = Issue.visible.open.where(cond).count(:group => :tracker)
178
    @total_issues_by_tracker = Issue.visible.where(cond).count(:group => :tracker)
179 909:cbb26bc654de Chris
180 441:cbce1fd3b1b7 Chris
    if User.current.allowed_to?(:view_time_entries, @project)
181
      @total_hours = TimeEntry.visible.sum(:hours, :include => :project, :conditions => cond).to_f
182 0:513646585e45 Chris
    end
183 909:cbb26bc654de Chris
184 0:513646585e45 Chris
    @key = User.current.rss_key
185 909:cbb26bc654de Chris
186 0:513646585e45 Chris
    respond_to do |format|
187
      format.html
188 117:af80e5618e9b Chris
      format.api
189 0:513646585e45 Chris
    end
190
  end
191
192
  def settings
193 1295:622f24f53b42 Chris
    @issue_custom_fields = IssueCustomField.sorted.all
194 0:513646585e45 Chris
    @issue_category ||= IssueCategory.new
195
    @member ||= @project.members.new
196 1115:433d4f72a19b Chris
    @trackers = Tracker.sorted.all
197 0:513646585e45 Chris
    @wiki ||= @project.wiki
198
  end
199 909:cbb26bc654de Chris
200 0:513646585e45 Chris
  def edit
201 22:40f7cfd4df19 chris
  end
202
203
  def update
204 117:af80e5618e9b Chris
    @project.safe_attributes = params[:project]
205 22:40f7cfd4df19 chris
    if validate_parent_id && @project.save
206
      @project.set_allowed_parent!(params[:project]['parent_id']) if params[:project].has_key?('parent_id')
207
      respond_to do |format|
208 909:cbb26bc654de Chris
        format.html {
209 22:40f7cfd4df19 chris
          flash[:notice] = l(:notice_successful_update)
210 1295:622f24f53b42 Chris
          redirect_to settings_project_path(@project)
211 22:40f7cfd4df19 chris
        }
212 1115:433d4f72a19b Chris
        format.api  { render_api_ok }
213 22:40f7cfd4df19 chris
      end
214 0:513646585e45 Chris
    else
215 22:40f7cfd4df19 chris
      respond_to do |format|
216 909:cbb26bc654de Chris
        format.html {
217 22:40f7cfd4df19 chris
          settings
218
          render :action => 'settings'
219
        }
220 117:af80e5618e9b Chris
        format.api  { render_validation_errors(@project) }
221 0:513646585e45 Chris
      end
222
    end
223
  end
224 117:af80e5618e9b Chris
225 351:ebf53b46f3f3 chris
  def overview
226
    @project.has_welcome_page = params[:has_welcome_page]
227
    if @project.save
228
      flash[:notice] = l(:notice_successful_update)
229
    end
230
    redirect_to :action => 'settings', :id => @project, :tab => 'overview'
231
  end
232
233 0:513646585e45 Chris
  def modules
234 117:af80e5618e9b Chris
    @project.enabled_module_names = params[:enabled_module_names]
235 0:513646585e45 Chris
    flash[:notice] = l(:notice_successful_update)
236 1295:622f24f53b42 Chris
    redirect_to settings_project_path(@project, :tab => 'modules')
237 0:513646585e45 Chris
  end
238
239
  def archive
240
    if request.post?
241
      unless @project.archive
242
        flash[:error] = l(:error_can_not_archive_project)
243
      end
244
    end
245 1295:622f24f53b42 Chris
    redirect_to admin_projects_path(:status => params[:status])
246 0:513646585e45 Chris
  end
247 909:cbb26bc654de Chris
248 0:513646585e45 Chris
  def unarchive
249
    @project.unarchive if request.post? && !@project.active?
250 1295:622f24f53b42 Chris
    redirect_to admin_projects_path(:status => params[:status])
251 0:513646585e45 Chris
  end
252 909:cbb26bc654de Chris
253 1115:433d4f72a19b Chris
  def close
254
    @project.close
255
    redirect_to project_path(@project)
256
  end
257
258
  def reopen
259
    @project.reopen
260
    redirect_to project_path(@project)
261
  end
262
263 0:513646585e45 Chris
  # Delete @project
264
  def destroy
265
    @project_to_destroy = @project
266 1115:433d4f72a19b Chris
    if api_request? || params[:confirm]
267
      @project_to_destroy.destroy
268
      respond_to do |format|
269 1295:622f24f53b42 Chris
        format.html { redirect_to admin_projects_path }
270 1115:433d4f72a19b Chris
        format.api  { render_api_ok }
271 0:513646585e45 Chris
      end
272
    end
273
    # hide project in layout
274
    @project = nil
275
  end
276
277 1115:433d4f72a19b Chris
  private
278 0:513646585e45 Chris
279 1034:ea5d9652c6f6 chris
  def validate_is_public_key
280
    # Although is_public isn't mandatory in the project model (it gets
281
    # defaulted), it must be present in params -- it can be true or
282
    # false, but it must be there. This permits us to make forms in
283
    # which the user _has_ to select public or private (rather than
284
    # defaulting it) if we want to
285
    if params.nil? || params[:project].nil? || !params[:project].has_key?(:is_public)
286
      @project.errors.add :is_public, :public_or_private
287
      return false
288
    end
289
    true
290
  end
291
292 0:513646585e45 Chris
  # Validates parent_id param according to user's permissions
293
  # TODO: move it to Project model in a validation that depends on User.current
294
  def validate_parent_id
295
    return true if User.current.admin?
296
    parent_id = params[:project] && params[:project][:parent_id]
297
    if parent_id || @project.new_record?
298
      parent = parent_id.blank? ? nil : Project.find_by_id(parent_id.to_i)
299
      unless @project.allowed_parents.include?(parent)
300
        @project.errors.add :parent_id, :invalid
301
        return false
302
      end
303
    end
304
    true
305
  end
306
end