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 @ 437:102056ec2de9

History | View | Annotate | Download (10.7 KB)

1
# Redmine - project management software
2
# Copyright (C) 2006-2009  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, :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
  before_filter :require_admin, :only => [ :copy, :archive, :unarchive, :destroy ]
27
  accept_key_auth :index
28

    
29
  after_filter :only => [:create, :edit, :update, :archive, :unarchive, :destroy] do |controller|
30
    if controller.request.post?
31
      controller.send :expire_action, :controller => 'welcome', :action => 'robots.txt'
32
    end
33
  end
34

    
35
  # TODO: convert to PUT only
36
  verify :method => [:post, :put], :only => :update, :render => {:nothing => true, :status => :method_not_allowed }
37

    
38
  helper :sort
39
  include SortHelper
40
  helper :custom_fields
41
  include CustomFieldsHelper   
42
  helper :issues
43
  helper :queries
44
  include QueriesHelper
45
  helper :repositories
46
  include RepositoriesHelper
47
  include ProjectsHelper
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.rhtml', :layout => !request.xhr?
67
      }
68
      format.xml  {
69
        @projects = Project.visible.find(:all, :order => 'lft')
70
      }
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
  def new
80
    @issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position")
81
    @trackers = Tracker.all
82
    @project = Project.new(params[:project])
83

    
84
    @project.identifier = Project.next_identifier if Setting.sequential_project_identifiers?
85
    @project.trackers = Tracker.all
86
    @project.is_public = Setting.default_projects_public?
87
    @project.enabled_module_names = Setting.default_projects_modules
88
  end
89

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

    
95
    @project.enabled_module_names = params[:enabled_modules]
96
    if validate_parent_id && @project.save
97
      @project.set_allowed_parent!(params[:project]['parent_id']) if params[:project].has_key?('parent_id')
98
      # Add current user as a project member if he is not admin
99
      unless User.current.admin?
100
        r = Role.givable.find_by_id(Setting.new_project_user_role_id.to_i) || Role.givable.first
101
        m = Member.new(:user => User.current, :roles => [r])
102
        @project.members << m
103
      end
104
      respond_to do |format|
105
        format.html { 
106
          flash[:notice] = l(:notice_successful_create)
107
          redirect_to :controller => 'projects', :action => 'settings', :id => @project
108
        }
109
        format.xml  { render :action => 'show', :status => :created, :location => url_for(:controller => 'projects', :action => 'show', :id => @project.id) }
110
      end
111
    else
112
      respond_to do |format|
113
        format.html { render :action => 'new' }
114
        format.xml  { render :xml => @project.errors, :status => :unprocessable_entity }
115
      end
116
    end
117
    
118
  end
119
  
120
  def copy
121
    @issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position")
122
    @trackers = Tracker.all
123
    @root_projects = Project.find(:all,
124
                                  :conditions => "parent_id IS NULL AND status = #{Project::STATUS_ACTIVE}",
125
                                  :order => 'name')
126
    @source_project = Project.find(params[:id])
127
    if request.get?
128
      @project = Project.copy_from(@source_project)
129
      if @project
130
        @project.identifier = Project.next_identifier if Setting.sequential_project_identifiers?
131
      else
132
        redirect_to :controller => 'admin', :action => 'projects'
133
      end  
134
    else
135
      Mailer.with_deliveries(params[:notifications] == '1') do
136
        @project = Project.new(params[:project])
137
        @project.enabled_module_names = params[:enabled_modules]
138
        if validate_parent_id && @project.copy(@source_project, :only => params[:only])
139
          @project.set_allowed_parent!(params[:project]['parent_id']) if params[:project].has_key?('parent_id')
140
          flash[:notice] = l(:notice_successful_create)
141
          redirect_to :controller => 'projects', :action => 'settings'
142
        elsif !@project.new_record?
143
          # Project was created
144
          # But some objects were not copied due to validation failures
145
          # (eg. issues from disabled trackers)
146
          # TODO: inform about that
147
          redirect_to :controller => 'projects', :action => 'settings'
148
        end
149
      end
150
    end
151
  rescue ActiveRecord::RecordNotFound
152
    redirect_to :controller => 'admin', :action => 'projects'
153
  end
154
        
155
  # Show @project
156
  def show
157
    if params[:jump]
158
      # try to redirect to the requested menu item
159
      redirect_to_project_menu_item(@project, params[:jump]) && return
160
    end
161
    
162
    @users_by_role = @project.users_by_role
163
    @subprojects = @project.children.visible
164
    @news = @project.news.find(:all, :limit => 5, :include => [ :author, :project ], :order => "#{News.table_name}.created_on DESC")
165
    @trackers = @project.rolled_up_trackers
166
    
167
    cond = @project.project_condition(Setting.display_subprojects_issues?)
168
    
169
    @open_issues_by_tracker = Issue.visible.count(:group => :tracker,
170
                                            :include => [:project, :status, :tracker],
171
                                            :conditions => ["(#{cond}) AND #{IssueStatus.table_name}.is_closed=?", false])
172
    @total_issues_by_tracker = Issue.visible.count(:group => :tracker,
173
                                            :include => [:project, :status, :tracker],
174
                                            :conditions => cond)
175
    
176
    TimeEntry.visible_by(User.current) do
177
      @total_hours = TimeEntry.sum(:hours, 
178
                                   :include => :project,
179
                                   :conditions => cond).to_f
180
    end
181
    @key = User.current.rss_key
182
    
183
    respond_to do |format|
184
      format.html
185
      format.xml
186
    end
187
  end
188

    
189
  def settings
190
    @issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position")
191
    @issue_category ||= IssueCategory.new
192
    @member ||= @project.members.new
193
    @trackers = Tracker.all
194
    @repository ||= @project.repository
195
    @wiki ||= @project.wiki
196
  end
197
  
198
  def edit
199
  end
200

    
201
  def update
202
    @project.attributes = params[:project]
203
    if validate_parent_id && @project.save
204
      @project.set_allowed_parent!(params[:project]['parent_id']) if params[:project].has_key?('parent_id')
205
      respond_to do |format|
206
        format.html { 
207
          flash[:notice] = l(:notice_successful_update)
208
          redirect_to :action => 'settings', :id => @project
209
        }
210
        format.xml  { head :ok }
211
      end
212
    else
213
      respond_to do |format|
214
        format.html { 
215
          settings
216
          render :action => 'settings'
217
        }
218
        format.xml  { render :xml => @project.errors, :status => :unprocessable_entity }
219
      end
220
    end
221
  end
222

    
223
  def overview
224
    @project.has_welcome_page = params[:has_welcome_page]
225
    if @project.save
226
      flash[:notice] = l(:notice_successful_update)
227
    end
228
    redirect_to :action => 'settings', :id => @project, :tab => 'overview'
229
  end
230

    
231
  def modules
232
    @project.enabled_module_names = params[:enabled_modules]
233
    flash[:notice] = l(:notice_successful_update)
234
    redirect_to :action => 'settings', :id => @project, :tab => 'modules'
235
  end
236

    
237
  def archive
238
    if request.post?
239
      unless @project.archive
240
        flash[:error] = l(:error_can_not_archive_project)
241
      end
242
    end
243
    redirect_to(url_for(:controller => 'admin', :action => 'projects', :status => params[:status]))
244
  end
245
  
246
  def unarchive
247
    @project.unarchive if request.post? && !@project.active?
248
    redirect_to(url_for(:controller => 'admin', :action => 'projects', :status => params[:status]))
249
  end
250
  
251
  # Delete @project
252
  def destroy
253
    @project_to_destroy = @project
254
    if request.get?
255
      # display confirmation view
256
    else
257
      if params[:format] == 'xml' || params[:confirm]
258
        @project_to_destroy.destroy
259
        respond_to do |format|
260
          format.html { redirect_to :controller => 'admin', :action => 'projects' }
261
          format.xml  { head :ok }
262
        end
263
      end
264
    end
265
    # hide project in layout
266
    @project = nil
267
  end
268

    
269
private
270
  def find_optional_project
271
    return true unless params[:id]
272
    @project = Project.find(params[:id])
273
    authorize
274
  rescue ActiveRecord::RecordNotFound
275
    render_404
276
  end
277

    
278
  # Validates parent_id param according to user's permissions
279
  # TODO: move it to Project model in a validation that depends on User.current
280
  def validate_parent_id
281
    return true if User.current.admin?
282
    parent_id = params[:project] && params[:project][:parent_id]
283
    if parent_id || @project.new_record?
284
      parent = parent_id.blank? ? nil : Project.find_by_id(parent_id.to_i)
285
      unless @project.allowed_parents.include?(parent)
286
        @project.errors.add :parent_id, :invalid
287
        return false
288
      end
289
    end
290
    true
291
  end
292
end