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 @ 1014:e3f78fa30a36

History | View | Annotate | Download (11.5 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
        if User.current.logged?
62
          # 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
        end
66
        render :template => 'projects/index.html.erb', :layout => !request.xhr?
67
      }
68
      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
      }
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

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

    
91
  def new
92
    @issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position")
93
    @trackers = Tracker.all
94
    @project = Project.new
95
    @project.safe_attributes = params[:project]
96
  end
97

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

    
105

    
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

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

    
140
  end
141

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

    
184
    @users_by_role = @project.users_by_role
185
    @subprojects = @project.children.visible.all
186
    @news = @project.news.find(:all, :limit => 5, :include => [ :author, :project ], :order => "#{News.table_name}.created_on DESC")
187
    @trackers = @project.rolled_up_trackers
188

    
189
    cond = @project.project_condition(Setting.display_subprojects_issues?)
190

    
191
    @open_issues_by_tracker = Issue.visible.count(:group => :tracker,
192
                                            :include => [:project, :status, :tracker],
193
                                            :conditions => ["(#{cond}) AND #{IssueStatus.table_name}.is_closed=?", false])
194
    @total_issues_by_tracker = Issue.visible.count(:group => :tracker,
195
                                            :include => [:project, :status, :tracker],
196
                                            :conditions => cond)
197

    
198
    if User.current.allowed_to?(:view_time_entries, @project)
199
      @total_hours = TimeEntry.visible.sum(:hours, :include => :project, :conditions => cond).to_f
200
    end
201

    
202
    @key = User.current.rss_key
203

    
204
    respond_to do |format|
205
      format.html
206
      format.api
207
    end
208
  end
209

    
210
  def settings
211
    @issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position")
212
    @issue_category ||= IssueCategory.new
213
    @member ||= @project.members.new
214
    @trackers = Tracker.all
215
    @repository ||= @project.repository
216
    @wiki ||= @project.wiki
217
  end
218

    
219
  def edit
220
  end
221

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

    
246
  verify :method => :post, :only => :modules, :render => {:nothing => true, :status => :method_not_allowed }
247
  
248
  def overview
249
    @project.has_welcome_page = params[:has_welcome_page]
250
    if @project.save
251
      flash[:notice] = l(:notice_successful_update)
252
    end
253
    redirect_to :action => 'settings', :id => @project, :tab => 'overview'
254
  end
255

    
256
  def modules
257
    @project.enabled_module_names = params[:enabled_module_names]
258
    flash[:notice] = l(:notice_successful_update)
259
    redirect_to :action => 'settings', :id => @project, :tab => 'modules'
260
  end
261

    
262
  def archive
263
    if request.post?
264
      unless @project.archive
265
        flash[:error] = l(:error_can_not_archive_project)
266
      end
267
    end
268
    redirect_to(url_for(:controller => 'admin', :action => 'projects', :status => params[:status]))
269
  end
270

    
271
  def unarchive
272
    @project.unarchive if request.post? && !@project.active?
273
    redirect_to(url_for(:controller => 'admin', :action => 'projects', :status => params[:status]))
274
  end
275

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

    
294
private
295
  def find_optional_project
296
    return true unless params[:id]
297
    @project = Project.find(params[:id])
298
    authorize
299
  rescue ActiveRecord::RecordNotFound
300
    render_404
301
  end
302

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