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 @ 765:7fd52a786954

History | View | Annotate | Download (10.8 KB)

1 0:513646585e45 Chris
# Redmine - project management software
2 441:cbce1fd3b1b7 Chris
# Copyright (C) 2006-2011  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
#
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 22:40f7cfd4df19 chris
  before_filter :find_project, :except => [ :index, :list, :new, :create, :copy ]
24
  before_filter :authorize, :except => [ :index, :list, :new, :create, :copy, :archive, :unarchive, :destroy]
25
  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
      controller.send :expire_action, :controller => 'welcome', :action => 'robots.txt'
33
    end
34
  end
35 22:40f7cfd4df19 chris
36 0:513646585e45 Chris
  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 131:513e61d1b4da chris
47 205:05f9a2a9c753 chris
  # Lists visible projects. Paginator is for top-level projects only
48
  # (subprojects belong to them)
49 0:513646585e45 Chris
  def index
50
    respond_to do |format|
51
      format.html {
52 205:05f9a2a9c753 chris
        sort_init 'name'
53
        sort_update %w(name lft created_on updated_on)
54 131:513e61d1b4da chris
        @limit = per_page_option
55 205:05f9a2a9c753 chris
        @project_count = Project.visible_roots.count
56 131:513e61d1b4da chris
        @project_pages = Paginator.new self, @project_count, @limit, params['page']
57
        @offset ||= @project_pages.current.offset
58 760:b2a9e64b8283 luis
        @projects = Project.visible_roots.find(@filtered_projects, :offset => @offset, :limit => @limit, :order => sort_clause)
59 131:513e61d1b4da chris
        if User.current.logged?
60 418:a9921f3e9088 chris
          # seems sort_by gives us case-sensitive ordering, which we don't want
61
#          @user_projects = User.current.projects.sort_by(&:name)
62
          @user_projects = User.current.projects.all(:order => :name)
63 131:513e61d1b4da chris
        end
64
        render :template => 'projects/index.rhtml', :layout => !request.xhr?
65 0:513646585e45 Chris
      }
66 117:af80e5618e9b Chris
      format.api  {
67
        @offset, @limit = api_offset_and_limit
68
        @project_count = Project.visible.count
69
        @projects = Project.visible.all(:offset => @offset, :limit => @limit, :order => 'lft')
70 0:513646585e45 Chris
      }
71
      format.atom {
72
        projects = Project.visible.find(:all, :order => 'created_on DESC',
73
                                              :limit => Setting.feeds_limit.to_i)
74
        render_feed(projects, :title => "#{Setting.app_title}: #{l(:label_project_latest)}")
75
      }
76
    end
77
  end
78
79 22:40f7cfd4df19 chris
  def new
80 0:513646585e45 Chris
    @issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position")
81
    @trackers = Tracker.all
82
    @project = Project.new(params[:project])
83 22:40f7cfd4df19 chris
  end
84
85 117:af80e5618e9b Chris
  verify :method => :post, :only => :create, :render => {:nothing => true, :status => :method_not_allowed }
86 22:40f7cfd4df19 chris
  def create
87
    @issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position")
88
    @trackers = Tracker.all
89 117:af80e5618e9b Chris
    @project = Project.new
90
    @project.safe_attributes = params[:project]
91 22:40f7cfd4df19 chris
92
    if validate_parent_id && @project.save
93
      @project.set_allowed_parent!(params[:project]['parent_id']) if params[:project].has_key?('parent_id')
94
      # Add current user as a project member if he is not admin
95
      unless User.current.admin?
96
        r = Role.givable.find_by_id(Setting.new_project_user_role_id.to_i) || Role.givable.first
97
        m = Member.new(:user => User.current, :roles => [r])
98
        @project.members << m
99
      end
100
      respond_to do |format|
101
        format.html {
102
          flash[:notice] = l(:notice_successful_create)
103
          redirect_to :controller => 'projects', :action => 'settings', :id => @project
104
        }
105 117:af80e5618e9b Chris
        format.api  { render :action => 'show', :status => :created, :location => url_for(:controller => 'projects', :action => 'show', :id => @project.id) }
106 22:40f7cfd4df19 chris
      end
107 0:513646585e45 Chris
    else
108 22:40f7cfd4df19 chris
      respond_to do |format|
109
        format.html { render :action => 'new' }
110 117:af80e5618e9b Chris
        format.api  { render_validation_errors(@project) }
111 0:513646585e45 Chris
      end
112 22:40f7cfd4df19 chris
    end
113
114 0:513646585e45 Chris
  end
115
116
  def copy
117
    @issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position")
118
    @trackers = Tracker.all
119
    @root_projects = Project.find(:all,
120
                                  :conditions => "parent_id IS NULL AND status = #{Project::STATUS_ACTIVE}",
121
                                  :order => 'name')
122
    @source_project = Project.find(params[:id])
123
    if request.get?
124
      @project = Project.copy_from(@source_project)
125
      if @project
126
        @project.identifier = Project.next_identifier if Setting.sequential_project_identifiers?
127
      else
128
        redirect_to :controller => 'admin', :action => 'projects'
129
      end
130
    else
131
      Mailer.with_deliveries(params[:notifications] == '1') do
132 117:af80e5618e9b Chris
        @project = Project.new
133
        @project.safe_attributes = params[:project]
134 0:513646585e45 Chris
        if validate_parent_id && @project.copy(@source_project, :only => params[:only])
135
          @project.set_allowed_parent!(params[:project]['parent_id']) if params[:project].has_key?('parent_id')
136
          flash[:notice] = l(:notice_successful_create)
137 117:af80e5618e9b Chris
          redirect_to :controller => 'projects', :action => 'settings', :id => @project
138 0:513646585e45 Chris
        elsif !@project.new_record?
139
          # Project was created
140
          # But some objects were not copied due to validation failures
141
          # (eg. issues from disabled trackers)
142
          # TODO: inform about that
143 117:af80e5618e9b Chris
          redirect_to :controller => 'projects', :action => 'settings', :id => @project
144 0:513646585e45 Chris
        end
145
      end
146
    end
147
  rescue ActiveRecord::RecordNotFound
148
    redirect_to :controller => 'admin', :action => 'projects'
149
  end
150
151
  # Show @project
152
  def show
153
    if params[:jump]
154
      # try to redirect to the requested menu item
155
      redirect_to_project_menu_item(@project, params[:jump]) && return
156
    end
157
158
    @users_by_role = @project.users_by_role
159 441:cbce1fd3b1b7 Chris
    @subprojects = @project.children.visible.all
160 0:513646585e45 Chris
    @news = @project.news.find(:all, :limit => 5, :include => [ :author, :project ], :order => "#{News.table_name}.created_on DESC")
161
    @trackers = @project.rolled_up_trackers
162
163
    cond = @project.project_condition(Setting.display_subprojects_issues?)
164
165
    @open_issues_by_tracker = Issue.visible.count(:group => :tracker,
166
                                            :include => [:project, :status, :tracker],
167
                                            :conditions => ["(#{cond}) AND #{IssueStatus.table_name}.is_closed=?", false])
168
    @total_issues_by_tracker = Issue.visible.count(:group => :tracker,
169
                                            :include => [:project, :status, :tracker],
170
                                            :conditions => cond)
171
172 441:cbce1fd3b1b7 Chris
    if User.current.allowed_to?(:view_time_entries, @project)
173
      @total_hours = TimeEntry.visible.sum(:hours, :include => :project, :conditions => cond).to_f
174 0:513646585e45 Chris
    end
175 441:cbce1fd3b1b7 Chris
176 0:513646585e45 Chris
    @key = User.current.rss_key
177
178
    respond_to do |format|
179
      format.html
180 117:af80e5618e9b Chris
      format.api
181 0:513646585e45 Chris
    end
182
  end
183
184
  def settings
185
    @issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position")
186
    @issue_category ||= IssueCategory.new
187
    @member ||= @project.members.new
188
    @trackers = Tracker.all
189
    @repository ||= @project.repository
190
    @wiki ||= @project.wiki
191
  end
192
193
  def edit
194 22:40f7cfd4df19 chris
  end
195
196 117:af80e5618e9b Chris
  # TODO: convert to PUT only
197
  verify :method => [:post, :put], :only => :update, :render => {:nothing => true, :status => :method_not_allowed }
198 22:40f7cfd4df19 chris
  def update
199 117:af80e5618e9b Chris
    @project.safe_attributes = params[:project]
200 22:40f7cfd4df19 chris
    if validate_parent_id && @project.save
201
      @project.set_allowed_parent!(params[:project]['parent_id']) if params[:project].has_key?('parent_id')
202
      respond_to do |format|
203
        format.html {
204
          flash[:notice] = l(:notice_successful_update)
205
          redirect_to :action => 'settings', :id => @project
206
        }
207 117:af80e5618e9b Chris
        format.api  { head :ok }
208 22:40f7cfd4df19 chris
      end
209 0:513646585e45 Chris
    else
210 22:40f7cfd4df19 chris
      respond_to do |format|
211
        format.html {
212
          settings
213
          render :action => 'settings'
214
        }
215 117:af80e5618e9b Chris
        format.api  { render_validation_errors(@project) }
216 0:513646585e45 Chris
      end
217
    end
218
  end
219 117:af80e5618e9b Chris
220
  verify :method => :post, :only => :modules, :render => {:nothing => true, :status => :method_not_allowed }
221 443:350acce374a2 Chris
222 351:ebf53b46f3f3 chris
  def overview
223
    @project.has_welcome_page = params[:has_welcome_page]
224
    if @project.save
225
      flash[:notice] = l(:notice_successful_update)
226
    end
227
    redirect_to :action => 'settings', :id => @project, :tab => 'overview'
228
  end
229
230 0:513646585e45 Chris
  def modules
231 117:af80e5618e9b Chris
    @project.enabled_module_names = params[:enabled_module_names]
232 0:513646585e45 Chris
    flash[:notice] = l(:notice_successful_update)
233
    redirect_to :action => 'settings', :id => @project, :tab => 'modules'
234
  end
235
236
  def archive
237
    if request.post?
238
      unless @project.archive
239
        flash[:error] = l(:error_can_not_archive_project)
240
      end
241
    end
242
    redirect_to(url_for(:controller => 'admin', :action => 'projects', :status => params[:status]))
243
  end
244
245
  def unarchive
246
    @project.unarchive if request.post? && !@project.active?
247
    redirect_to(url_for(:controller => 'admin', :action => 'projects', :status => params[:status]))
248
  end
249
250
  # Delete @project
251
  def destroy
252
    @project_to_destroy = @project
253
    if request.get?
254
      # display confirmation view
255
    else
256 117:af80e5618e9b Chris
      if api_request? || params[:confirm]
257 0:513646585e45 Chris
        @project_to_destroy.destroy
258
        respond_to do |format|
259
          format.html { redirect_to :controller => 'admin', :action => 'projects' }
260 117:af80e5618e9b Chris
          format.api  { head :ok }
261 0:513646585e45 Chris
        end
262
      end
263
    end
264
    # hide project in layout
265
    @project = nil
266
  end
267
268
private
269
  def find_optional_project
270
    return true unless params[:id]
271
    @project = Project.find(params[:id])
272
    authorize
273
  rescue ActiveRecord::RecordNotFound
274
    render_404
275
  end
276
277
  # Validates parent_id param according to user's permissions
278
  # TODO: move it to Project model in a validation that depends on User.current
279
  def validate_parent_id
280
    return true if User.current.admin?
281
    parent_id = params[:project] && params[:project][:parent_id]
282
    if parent_id || @project.new_record?
283
      parent = parent_id.blank? ? nil : Project.find_by_id(parent_id.to_i)
284
      unless @project.allowed_parents.include?(parent)
285
        @project.errors.add :parent_id, :invalid
286
        return false
287
      end
288
    end
289
    true
290
  end
291
end