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 @ 967:19884e9d5eff

History | View | Annotate | Download (11.2 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, :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_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

    
47
  # Lists visible projects. Paginator is for top-level projects only
48
  # (subprojects belong to them)
49
  def index
50
    respond_to do |format|
51
      format.html {
52
        sort_init 'name'
53
        sort_update %w(name lft created_on updated_on)
54
        @limit = per_page_option
55
        @project_count = Project.visible_roots.count
56
        @project_pages = Paginator.new self, @project_count, @limit, params['page']
57
        @offset ||= @project_pages.current.offset
58
        @projects = Project.visible_roots.all(:offset => @offset, :limit => @limit, :order => sort_clause) 
59
        if User.current.logged?
60
          # 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
        end
64
        render :template => 'projects/index.html.erb', :layout => !request.xhr?
65
      }
66
      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
      }
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
83
    @project.safe_attributes = params[:project]
84
  end
85

    
86
  verify :method => :post, :only => :create, :render => {:nothing => true, :status => :method_not_allowed }
87
  def create
88
    @issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position")
89
    @trackers = Tracker.all
90
    @project = Project.new
91
    @project.safe_attributes = params[:project]
92

    
93

    
94
    # todo: luisf: this should be removed from here...
95
    if params && params[:project] && !params[:project][:tag_list].nil?
96
      new_tags = params[:project][:tag_list].to_s.downcase
97

    
98
      @project.tag_list = ActionController::Base.helpers.strip_tags(new_tags)
99
    end
100
    # end of code to be removed
101

    
102

    
103
    if validate_parent_id && @project.save
104
      @project.set_allowed_parent!(params[:project]['parent_id']) if params[:project].has_key?('parent_id')
105
      # Add current user as a project member if he is not admin
106
      unless User.current.admin?
107
        r = Role.givable.find_by_id(Setting.new_project_user_role_id.to_i) || Role.givable.first
108
        m = Member.new(:user => User.current, :roles => [r])
109
        @project.members << m
110
      end
111
      respond_to do |format|
112
        format.html {
113
          flash[:notice] = l(:notice_successful_create)
114
          redirect_to(params[:continue] ?
115
            {:controller => 'projects', :action => 'new', :project => {:parent_id => @project.parent_id}.reject {|k,v| v.nil?}} :
116
            {:controller => 'projects', :action => 'settings', :id => @project}
117
          )
118
        }
119
        format.api  { render :action => 'show', :status => :created, :location => url_for(:controller => 'projects', :action => 'show', :id => @project.id) }
120
      end
121
    else
122
      respond_to do |format|
123
        format.html { render :action => 'new' }
124
        format.api  { render_validation_errors(@project) }
125
      end
126
    end
127

    
128
  end
129

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

    
172
    @users_by_role = @project.users_by_role
173
    @subprojects = @project.children.visible.all
174
    @news = @project.news.find(:all, :limit => 5, :include => [ :author, :project ], :order => "#{News.table_name}.created_on DESC")
175
    @trackers = @project.rolled_up_trackers
176

    
177
    cond = @project.project_condition(Setting.display_subprojects_issues?)
178

    
179
    @open_issues_by_tracker = Issue.visible.count(:group => :tracker,
180
                                            :include => [:project, :status, :tracker],
181
                                            :conditions => ["(#{cond}) AND #{IssueStatus.table_name}.is_closed=?", false])
182
    @total_issues_by_tracker = Issue.visible.count(:group => :tracker,
183
                                            :include => [:project, :status, :tracker],
184
                                            :conditions => cond)
185

    
186
    if User.current.allowed_to?(:view_time_entries, @project)
187
      @total_hours = TimeEntry.visible.sum(:hours, :include => :project, :conditions => cond).to_f
188
    end
189

    
190
    @key = User.current.rss_key
191

    
192
    respond_to do |format|
193
      format.html
194
      format.api
195
    end
196
  end
197

    
198
  def settings
199
    @issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position")
200
    @issue_category ||= IssueCategory.new
201
    @member ||= @project.members.new
202
    @trackers = Tracker.all
203
    @repository ||= @project.repository
204
    @wiki ||= @project.wiki
205
  end
206

    
207
  def edit
208
  end
209

    
210
  # TODO: convert to PUT only
211
  verify :method => [:post, :put], :only => :update, :render => {:nothing => true, :status => :method_not_allowed }
212
  def update
213
    @project.safe_attributes = params[:project]
214
    if validate_parent_id && @project.save
215
      @project.set_allowed_parent!(params[:project]['parent_id']) if params[:project].has_key?('parent_id')
216
      respond_to do |format|
217
        format.html {
218
          flash[:notice] = l(:notice_successful_update)
219
          redirect_to :action => 'settings', :id => @project
220
        }
221
        format.api  { head :ok }
222
      end
223
    else
224
      respond_to do |format|
225
        format.html {
226
          settings
227
          render :action => 'settings'
228
        }
229
        format.api  { render_validation_errors(@project) }
230
      end
231
    end
232
  end
233

    
234
  verify :method => :post, :only => :modules, :render => {:nothing => true, :status => :method_not_allowed }
235
  
236
  def overview
237
    @project.has_welcome_page = params[:has_welcome_page]
238
    if @project.save
239
      flash[:notice] = l(:notice_successful_update)
240
    end
241
    redirect_to :action => 'settings', :id => @project, :tab => 'overview'
242
  end
243

    
244
  def modules
245
    @project.enabled_module_names = params[:enabled_module_names]
246
    flash[:notice] = l(:notice_successful_update)
247
    redirect_to :action => 'settings', :id => @project, :tab => 'modules'
248
  end
249

    
250
  def archive
251
    if request.post?
252
      unless @project.archive
253
        flash[:error] = l(:error_can_not_archive_project)
254
      end
255
    end
256
    redirect_to(url_for(:controller => 'admin', :action => 'projects', :status => params[:status]))
257
  end
258

    
259
  def unarchive
260
    @project.unarchive if request.post? && !@project.active?
261
    redirect_to(url_for(:controller => 'admin', :action => 'projects', :status => params[:status]))
262
  end
263

    
264
  # Delete @project
265
  def destroy
266
    @project_to_destroy = @project
267
    if request.get?
268
      # display confirmation view
269
    else
270
      if api_request? || params[:confirm]
271
        @project_to_destroy.destroy
272
        respond_to do |format|
273
          format.html { redirect_to :controller => 'admin', :action => 'projects' }
274
          format.api  { head :ok }
275
        end
276
      end
277
    end
278
    # hide project in layout
279
    @project = nil
280
  end
281

    
282
private
283
  def find_optional_project
284
    return true unless params[:id]
285
    @project = Project.find(params[:id])
286
    authorize
287
  rescue ActiveRecord::RecordNotFound
288
    render_404
289
  end
290

    
291
  # Validates parent_id param according to user's permissions
292
  # TODO: move it to Project model in a validation that depends on User.current
293
  def validate_parent_id
294
    return true if User.current.admin?
295
    parent_id = params[:project] && params[:project][:parent_id]
296
    if parent_id || @project.new_record?
297
      parent = parent_id.blank? ? nil : Project.find_by_id(parent_id.to_i)
298
      unless @project.allowed_parents.include?(parent)
299
        @project.errors.add :parent_id, :invalid
300
        return false
301
      end
302
    end
303
    true
304
  end
305
end