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 / .svn / text-base / projects_controller.rb.svn-base @ 441:cbce1fd3b1b7

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