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 @ 1034:ea5d9652c6f6

History | View | Annotate | Download (12.1 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 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
      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 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 767:dd33798e514d luis
        @projects = Project.visible_roots.all(:offset => @offset, :limit => @limit, :order => sort_clause)
61 131:513e61d1b4da chris
        if User.current.logged?
62 418:a9921f3e9088 chris
          # seems sort_by gives us case-sensitive ordering, which we don't want
63
#          @user_projects = User.current.projects.sort_by(&:name)
64
          @user_projects = User.current.projects.all(:order => :name)
65 131:513e61d1b4da chris
        end
66 919:b03f28dd9026 Chris
        render :template => 'projects/index.html.erb', :layout => !request.xhr?
67 0:513646585e45 Chris
      }
68 117:af80e5618e9b Chris
      format.api  {
69
        @offset, @limit = api_offset_and_limit
70
        @project_count = Project.visible.count
71
        @projects = Project.visible.all(:offset => @offset, :limit => @limit, :order => 'lft')
72 0:513646585e45 Chris
      }
73
      format.atom {
74
        projects = Project.visible.find(:all, :order => 'created_on DESC',
75
                                              :limit => Setting.feeds_limit.to_i)
76
        render_feed(projects, :title => "#{Setting.app_title}: #{l(:label_project_latest)}")
77
      }
78
    end
79
  end
80 909:cbb26bc654de Chris
81 1009:066b55d7c053 chris
  # A different view of projects using explore boxes
82
  def explore
83 1007:3e52bc15dd67 chris
    respond_to do |format|
84
      format.html {
85
        @projects = Project.visible
86 1009:066b55d7c053 chris
        render :template => 'projects/explore.html.erb', :layout => !request.xhr?
87 1007:3e52bc15dd67 chris
      }
88
    end
89
  end
90
91 22:40f7cfd4df19 chris
  def new
92 0:513646585e45 Chris
    @issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position")
93
    @trackers = Tracker.all
94 929:5f33065ddc4b Chris
    @project = Project.new
95
    @project.safe_attributes = params[:project]
96 22:40f7cfd4df19 chris
  end
97
98 117:af80e5618e9b Chris
  verify :method => :post, :only => :create, :render => {:nothing => true, :status => :method_not_allowed }
99 22:40f7cfd4df19 chris
  def create
100
    @issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position")
101
    @trackers = Tracker.all
102 117:af80e5618e9b Chris
    @project = Project.new
103
    @project.safe_attributes = params[:project]
104 22:40f7cfd4df19 chris
105 831:7614169e14ed luis
106
    # todo: luisf: this should be removed from here...
107
    if params && params[:project] && !params[:project][:tag_list].nil?
108
      new_tags = params[:project][:tag_list].to_s.downcase
109
110
      @project.tag_list = ActionController::Base.helpers.strip_tags(new_tags)
111
    end
112
    # end of code to be removed
113
114 1034:ea5d9652c6f6 chris
    if validate_is_public_key && validate_parent_id && @project.save
115 22:40f7cfd4df19 chris
      @project.set_allowed_parent!(params[:project]['parent_id']) if params[:project].has_key?('parent_id')
116
      # Add current user as a project member if he is not admin
117
      unless User.current.admin?
118
        r = Role.givable.find_by_id(Setting.new_project_user_role_id.to_i) || Role.givable.first
119
        m = Member.new(:user => User.current, :roles => [r])
120
        @project.members << m
121
      end
122
      respond_to do |format|
123 909:cbb26bc654de Chris
        format.html {
124 22:40f7cfd4df19 chris
          flash[:notice] = l(:notice_successful_create)
125 909:cbb26bc654de Chris
          redirect_to(params[:continue] ?
126
            {:controller => 'projects', :action => 'new', :project => {:parent_id => @project.parent_id}.reject {|k,v| v.nil?}} :
127
            {:controller => 'projects', :action => 'settings', :id => @project}
128
          )
129 22:40f7cfd4df19 chris
        }
130 117:af80e5618e9b Chris
        format.api  { render :action => 'show', :status => :created, :location => url_for(:controller => 'projects', :action => 'show', :id => @project.id) }
131 22:40f7cfd4df19 chris
      end
132 0:513646585e45 Chris
    else
133 22:40f7cfd4df19 chris
      respond_to do |format|
134
        format.html { render :action => 'new' }
135 117:af80e5618e9b Chris
        format.api  { render_validation_errors(@project) }
136 0:513646585e45 Chris
      end
137 22:40f7cfd4df19 chris
    end
138 909:cbb26bc654de Chris
139 0:513646585e45 Chris
  end
140 909:cbb26bc654de Chris
141 0:513646585e45 Chris
  def copy
142
    @issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position")
143
    @trackers = Tracker.all
144
    @root_projects = Project.find(:all,
145
                                  :conditions => "parent_id IS NULL AND status = #{Project::STATUS_ACTIVE}",
146
                                  :order => 'name')
147
    @source_project = Project.find(params[:id])
148
    if request.get?
149
      @project = Project.copy_from(@source_project)
150
      if @project
151
        @project.identifier = Project.next_identifier if Setting.sequential_project_identifiers?
152
      else
153
        redirect_to :controller => 'admin', :action => 'projects'
154 909:cbb26bc654de Chris
      end
155 0:513646585e45 Chris
    else
156
      Mailer.with_deliveries(params[:notifications] == '1') do
157 117:af80e5618e9b Chris
        @project = Project.new
158
        @project.safe_attributes = params[:project]
159 0:513646585e45 Chris
        if validate_parent_id && @project.copy(@source_project, :only => params[:only])
160
          @project.set_allowed_parent!(params[:project]['parent_id']) if params[:project].has_key?('parent_id')
161
          flash[:notice] = l(:notice_successful_create)
162 117:af80e5618e9b Chris
          redirect_to :controller => 'projects', :action => 'settings', :id => @project
163 0:513646585e45 Chris
        elsif !@project.new_record?
164
          # Project was created
165
          # But some objects were not copied due to validation failures
166
          # (eg. issues from disabled trackers)
167
          # TODO: inform about that
168 117:af80e5618e9b Chris
          redirect_to :controller => 'projects', :action => 'settings', :id => @project
169 0:513646585e45 Chris
        end
170
      end
171
    end
172
  rescue ActiveRecord::RecordNotFound
173
    redirect_to :controller => 'admin', :action => 'projects'
174
  end
175
176
  # Show @project
177
  def show
178
    if params[:jump]
179
      # try to redirect to the requested menu item
180
      redirect_to_project_menu_item(@project, params[:jump]) && return
181
    end
182 909:cbb26bc654de Chris
183 0:513646585e45 Chris
    @users_by_role = @project.users_by_role
184 441:cbce1fd3b1b7 Chris
    @subprojects = @project.children.visible.all
185 0:513646585e45 Chris
    @news = @project.news.find(:all, :limit => 5, :include => [ :author, :project ], :order => "#{News.table_name}.created_on DESC")
186
    @trackers = @project.rolled_up_trackers
187 909:cbb26bc654de Chris
188 0:513646585e45 Chris
    cond = @project.project_condition(Setting.display_subprojects_issues?)
189 909:cbb26bc654de Chris
190 0:513646585e45 Chris
    @open_issues_by_tracker = Issue.visible.count(:group => :tracker,
191
                                            :include => [:project, :status, :tracker],
192
                                            :conditions => ["(#{cond}) AND #{IssueStatus.table_name}.is_closed=?", false])
193
    @total_issues_by_tracker = Issue.visible.count(:group => :tracker,
194
                                            :include => [:project, :status, :tracker],
195
                                            :conditions => cond)
196 909:cbb26bc654de Chris
197 441:cbce1fd3b1b7 Chris
    if User.current.allowed_to?(:view_time_entries, @project)
198
      @total_hours = TimeEntry.visible.sum(:hours, :include => :project, :conditions => cond).to_f
199 0:513646585e45 Chris
    end
200 909:cbb26bc654de Chris
201 0:513646585e45 Chris
    @key = User.current.rss_key
202 909:cbb26bc654de Chris
203 0:513646585e45 Chris
    respond_to do |format|
204
      format.html
205 117:af80e5618e9b Chris
      format.api
206 0:513646585e45 Chris
    end
207
  end
208
209
  def settings
210
    @issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position")
211
    @issue_category ||= IssueCategory.new
212
    @member ||= @project.members.new
213
    @trackers = Tracker.all
214
    @repository ||= @project.repository
215
    @wiki ||= @project.wiki
216
  end
217 909:cbb26bc654de Chris
218 0:513646585e45 Chris
  def edit
219 22:40f7cfd4df19 chris
  end
220
221 117:af80e5618e9b Chris
  # TODO: convert to PUT only
222
  verify :method => [:post, :put], :only => :update, :render => {:nothing => true, :status => :method_not_allowed }
223 22:40f7cfd4df19 chris
  def update
224 117:af80e5618e9b Chris
    @project.safe_attributes = params[:project]
225 22:40f7cfd4df19 chris
    if validate_parent_id && @project.save
226
      @project.set_allowed_parent!(params[:project]['parent_id']) if params[:project].has_key?('parent_id')
227
      respond_to do |format|
228 909:cbb26bc654de Chris
        format.html {
229 22:40f7cfd4df19 chris
          flash[:notice] = l(:notice_successful_update)
230
          redirect_to :action => 'settings', :id => @project
231
        }
232 117:af80e5618e9b Chris
        format.api  { head :ok }
233 22:40f7cfd4df19 chris
      end
234 0:513646585e45 Chris
    else
235 22:40f7cfd4df19 chris
      respond_to do |format|
236 909:cbb26bc654de Chris
        format.html {
237 22:40f7cfd4df19 chris
          settings
238
          render :action => 'settings'
239
        }
240 117:af80e5618e9b Chris
        format.api  { render_validation_errors(@project) }
241 0:513646585e45 Chris
      end
242
    end
243
  end
244 117:af80e5618e9b Chris
245
  verify :method => :post, :only => :modules, :render => {:nothing => true, :status => :method_not_allowed }
246 443:350acce374a2 Chris
247 351:ebf53b46f3f3 chris
  def overview
248
    @project.has_welcome_page = params[:has_welcome_page]
249
    if @project.save
250
      flash[:notice] = l(:notice_successful_update)
251
    end
252
    redirect_to :action => 'settings', :id => @project, :tab => 'overview'
253
  end
254
255 0:513646585e45 Chris
  def modules
256 117:af80e5618e9b Chris
    @project.enabled_module_names = params[:enabled_module_names]
257 0:513646585e45 Chris
    flash[:notice] = l(:notice_successful_update)
258
    redirect_to :action => 'settings', :id => @project, :tab => 'modules'
259
  end
260
261
  def archive
262
    if request.post?
263
      unless @project.archive
264
        flash[:error] = l(:error_can_not_archive_project)
265
      end
266
    end
267
    redirect_to(url_for(:controller => 'admin', :action => 'projects', :status => params[:status]))
268
  end
269 909:cbb26bc654de Chris
270 0:513646585e45 Chris
  def unarchive
271
    @project.unarchive if request.post? && !@project.active?
272
    redirect_to(url_for(:controller => 'admin', :action => 'projects', :status => params[:status]))
273
  end
274 909:cbb26bc654de Chris
275 0:513646585e45 Chris
  # Delete @project
276
  def destroy
277
    @project_to_destroy = @project
278
    if request.get?
279
      # display confirmation view
280
    else
281 117:af80e5618e9b Chris
      if api_request? || params[:confirm]
282 0:513646585e45 Chris
        @project_to_destroy.destroy
283
        respond_to do |format|
284
          format.html { redirect_to :controller => 'admin', :action => 'projects' }
285 117:af80e5618e9b Chris
          format.api  { head :ok }
286 0:513646585e45 Chris
        end
287
      end
288
    end
289
    # hide project in layout
290
    @project = nil
291
  end
292
293
private
294
  def find_optional_project
295
    return true unless params[:id]
296
    @project = Project.find(params[:id])
297
    authorize
298
  rescue ActiveRecord::RecordNotFound
299
    render_404
300
  end
301
302 1034:ea5d9652c6f6 chris
  def validate_is_public_key
303
    # Although is_public isn't mandatory in the project model (it gets
304
    # defaulted), it must be present in params -- it can be true or
305
    # false, but it must be there. This permits us to make forms in
306
    # which the user _has_ to select public or private (rather than
307
    # defaulting it) if we want to
308
    if params.nil? || params[:project].nil? || !params[:project].has_key?(:is_public)
309
      @project.errors.add :is_public, :public_or_private
310
      return false
311
    end
312
    true
313
  end
314
315 0:513646585e45 Chris
  # Validates parent_id param according to user's permissions
316
  # TODO: move it to Project model in a validation that depends on User.current
317
  def validate_parent_id
318
    return true if User.current.admin?
319
    parent_id = params[:project] && params[:project][:parent_id]
320
    if parent_id || @project.new_record?
321
      parent = parent_id.blank? ? nil : Project.find_by_id(parent_id.to_i)
322
      unless @project.allowed_parents.include?(parent)
323
        @project.errors.add :parent_id, :invalid
324
        return false
325
      end
326
    end
327
    true
328
  end
329
end