@@ -1,271 +1,267 | |||
|
1 | 1 | # Redmine - project management software |
|
2 | 2 | # Copyright (C) 2006-2012 Jean-Philippe Lang |
|
3 | 3 | # |
|
4 | 4 | # This program is free software; you can redistribute it and/or |
|
5 | 5 | # modify it under the terms of the GNU General Public License |
|
6 | 6 | # as published by the Free Software Foundation; either version 2 |
|
7 | 7 | # of the License, or (at your option) any later version. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU General Public License |
|
15 | 15 | # along with this program; if not, write to the Free Software |
|
16 | 16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
17 | 17 | |
|
18 | 18 | class ProjectsController < ApplicationController |
|
19 | 19 | menu_item :overview |
|
20 | 20 | menu_item :roadmap, :only => :roadmap |
|
21 | 21 | menu_item :settings, :only => :settings |
|
22 | 22 | |
|
23 | 23 | before_filter :find_project, :except => [ :index, :list, :new, :create, :copy ] |
|
24 | 24 | before_filter :authorize, :except => [ :index, :list, :new, :create, :copy, :archive, :unarchive, :destroy] |
|
25 | 25 | before_filter :authorize_global, :only => [:new, :create] |
|
26 | 26 | before_filter :require_admin, :only => [ :copy, :archive, :unarchive, :destroy ] |
|
27 | 27 | accept_rss_auth :index |
|
28 | 28 | accept_api_auth :index, :show, :create, :update, :destroy |
|
29 | 29 | |
|
30 | 30 | after_filter :only => [:create, :edit, :update, :archive, :unarchive, :destroy] do |controller| |
|
31 | 31 | if controller.request.post? |
|
32 | 32 | controller.send :expire_action, :controller => 'welcome', :action => 'robots' |
|
33 | 33 | end |
|
34 | 34 | end |
|
35 | 35 | |
|
36 | 36 | helper :sort |
|
37 | 37 | include SortHelper |
|
38 | 38 | helper :custom_fields |
|
39 | 39 | include CustomFieldsHelper |
|
40 | 40 | helper :issues |
|
41 | 41 | helper :queries |
|
42 | 42 | include QueriesHelper |
|
43 | 43 | helper :repositories |
|
44 | 44 | include RepositoriesHelper |
|
45 | 45 | include ProjectsHelper |
|
46 | 46 | |
|
47 | 47 | # Lists visible projects |
|
48 | 48 | def index |
|
49 | 49 | respond_to do |format| |
|
50 | 50 | format.html { |
|
51 | 51 | scope = Project |
|
52 | 52 | unless params[:closed] |
|
53 | 53 | scope = scope.active |
|
54 | 54 | end |
|
55 | 55 | @projects = scope.visible.order('lft').all |
|
56 | 56 | } |
|
57 | 57 | format.api { |
|
58 | 58 | @offset, @limit = api_offset_and_limit |
|
59 | 59 | @project_count = Project.visible.count |
|
60 | 60 | @projects = Project.visible.all(:offset => @offset, :limit => @limit, :order => 'lft') |
|
61 | 61 | } |
|
62 | 62 | format.atom { |
|
63 | 63 | projects = Project.visible.find(:all, :order => 'created_on DESC', |
|
64 | 64 | :limit => Setting.feeds_limit.to_i) |
|
65 | 65 | render_feed(projects, :title => "#{Setting.app_title}: #{l(:label_project_latest)}") |
|
66 | 66 | } |
|
67 | 67 | end |
|
68 | 68 | end |
|
69 | 69 | |
|
70 | 70 | def new |
|
71 | 71 | @issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position") |
|
72 | 72 | @trackers = Tracker.sorted.all |
|
73 | 73 | @project = Project.new |
|
74 | 74 | @project.safe_attributes = params[:project] |
|
75 | 75 | end |
|
76 | 76 | |
|
77 | 77 | def create |
|
78 | 78 | @issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position") |
|
79 | 79 | @trackers = Tracker.sorted.all |
|
80 | 80 | @project = Project.new |
|
81 | 81 | @project.safe_attributes = params[:project] |
|
82 | 82 | |
|
83 | 83 | if validate_parent_id && @project.save |
|
84 | 84 | @project.set_allowed_parent!(params[:project]['parent_id']) if params[:project].has_key?('parent_id') |
|
85 | 85 | # Add current user as a project member if he is not admin |
|
86 | 86 | unless User.current.admin? |
|
87 | 87 | r = Role.givable.find_by_id(Setting.new_project_user_role_id.to_i) || Role.givable.first |
|
88 | 88 | m = Member.new(:user => User.current, :roles => [r]) |
|
89 | 89 | @project.members << m |
|
90 | 90 | end |
|
91 | 91 | respond_to do |format| |
|
92 | 92 | format.html { |
|
93 | 93 | flash[:notice] = l(:notice_successful_create) |
|
94 | 94 | redirect_to(params[:continue] ? |
|
95 | 95 | {:controller => 'projects', :action => 'new', :project => {:parent_id => @project.parent_id}.reject {|k,v| v.nil?}} : |
|
96 | 96 | {:controller => 'projects', :action => 'settings', :id => @project} |
|
97 | 97 | ) |
|
98 | 98 | } |
|
99 | 99 | format.api { render :action => 'show', :status => :created, :location => url_for(:controller => 'projects', :action => 'show', :id => @project.id) } |
|
100 | 100 | end |
|
101 | 101 | else |
|
102 | 102 | respond_to do |format| |
|
103 | 103 | format.html { render :action => 'new' } |
|
104 | 104 | format.api { render_validation_errors(@project) } |
|
105 | 105 | end |
|
106 | 106 | end |
|
107 | 107 | |
|
108 | 108 | end |
|
109 | 109 | |
|
110 | 110 | def copy |
|
111 | 111 | @issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position") |
|
112 | 112 | @trackers = Tracker.sorted.all |
|
113 | 113 | @root_projects = Project.find(:all, |
|
114 | 114 | :conditions => "parent_id IS NULL AND status = #{Project::STATUS_ACTIVE}", |
|
115 | 115 | :order => 'name') |
|
116 | 116 | @source_project = Project.find(params[:id]) |
|
117 | 117 | if request.get? |
|
118 | 118 | @project = Project.copy_from(@source_project) |
|
119 | 119 | if @project |
|
120 | 120 | @project.identifier = Project.next_identifier if Setting.sequential_project_identifiers? |
|
121 | 121 | else |
|
122 | 122 | redirect_to :controller => 'admin', :action => 'projects' |
|
123 | 123 | end |
|
124 | 124 | else |
|
125 | 125 | Mailer.with_deliveries(params[:notifications] == '1') do |
|
126 | 126 | @project = Project.new |
|
127 | 127 | @project.safe_attributes = params[:project] |
|
128 | 128 | if validate_parent_id && @project.copy(@source_project, :only => params[:only]) |
|
129 | 129 | @project.set_allowed_parent!(params[:project]['parent_id']) if params[:project].has_key?('parent_id') |
|
130 | 130 | flash[:notice] = l(:notice_successful_create) |
|
131 | 131 | redirect_to :controller => 'projects', :action => 'settings', :id => @project |
|
132 | 132 | elsif !@project.new_record? |
|
133 | 133 | # Project was created |
|
134 | 134 | # But some objects were not copied due to validation failures |
|
135 | 135 | # (eg. issues from disabled trackers) |
|
136 | 136 | # TODO: inform about that |
|
137 | 137 | redirect_to :controller => 'projects', :action => 'settings', :id => @project |
|
138 | 138 | end |
|
139 | 139 | end |
|
140 | 140 | end |
|
141 | 141 | rescue ActiveRecord::RecordNotFound |
|
142 | 142 | redirect_to :controller => 'admin', :action => 'projects' |
|
143 | 143 | end |
|
144 | 144 | |
|
145 | 145 | # Show @project |
|
146 | 146 | def show |
|
147 | 147 | if params[:jump] |
|
148 | 148 | # try to redirect to the requested menu item |
|
149 | 149 | redirect_to_project_menu_item(@project, params[:jump]) && return |
|
150 | 150 | end |
|
151 | 151 | |
|
152 | 152 | @users_by_role = @project.users_by_role |
|
153 | 153 | @subprojects = @project.children.visible.all |
|
154 | 154 | @news = @project.news.find(:all, :limit => 5, :include => [ :author, :project ], :order => "#{News.table_name}.created_on DESC") |
|
155 | 155 | @trackers = @project.rolled_up_trackers |
|
156 | 156 | |
|
157 | 157 | cond = @project.project_condition(Setting.display_subprojects_issues?) |
|
158 | 158 | |
|
159 |
@open_issues_by_tracker = Issue.visible.count(:group => :tracker |
|
|
160 | :include => [:project, :status, :tracker], | |
|
161 | :conditions => ["(#{cond}) AND #{IssueStatus.table_name}.is_closed=?", false]) | |
|
162 | @total_issues_by_tracker = Issue.visible.count(:group => :tracker, | |
|
163 | :include => [:project, :status, :tracker], | |
|
164 | :conditions => cond) | |
|
159 | @open_issues_by_tracker = Issue.visible.open.where(cond).count(:group => :tracker) | |
|
160 | @total_issues_by_tracker = Issue.visible.where(cond).count(:group => :tracker) | |
|
165 | 161 | |
|
166 | 162 | if User.current.allowed_to?(:view_time_entries, @project) |
|
167 | 163 | @total_hours = TimeEntry.visible.sum(:hours, :include => :project, :conditions => cond).to_f |
|
168 | 164 | end |
|
169 | 165 | |
|
170 | 166 | @key = User.current.rss_key |
|
171 | 167 | |
|
172 | 168 | respond_to do |format| |
|
173 | 169 | format.html |
|
174 | 170 | format.api |
|
175 | 171 | end |
|
176 | 172 | end |
|
177 | 173 | |
|
178 | 174 | def settings |
|
179 | 175 | @issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position") |
|
180 | 176 | @issue_category ||= IssueCategory.new |
|
181 | 177 | @member ||= @project.members.new |
|
182 | 178 | @trackers = Tracker.sorted.all |
|
183 | 179 | @wiki ||= @project.wiki |
|
184 | 180 | end |
|
185 | 181 | |
|
186 | 182 | def edit |
|
187 | 183 | end |
|
188 | 184 | |
|
189 | 185 | def update |
|
190 | 186 | @project.safe_attributes = params[:project] |
|
191 | 187 | if validate_parent_id && @project.save |
|
192 | 188 | @project.set_allowed_parent!(params[:project]['parent_id']) if params[:project].has_key?('parent_id') |
|
193 | 189 | respond_to do |format| |
|
194 | 190 | format.html { |
|
195 | 191 | flash[:notice] = l(:notice_successful_update) |
|
196 | 192 | redirect_to :action => 'settings', :id => @project |
|
197 | 193 | } |
|
198 | 194 | format.api { head :ok } |
|
199 | 195 | end |
|
200 | 196 | else |
|
201 | 197 | respond_to do |format| |
|
202 | 198 | format.html { |
|
203 | 199 | settings |
|
204 | 200 | render :action => 'settings' |
|
205 | 201 | } |
|
206 | 202 | format.api { render_validation_errors(@project) } |
|
207 | 203 | end |
|
208 | 204 | end |
|
209 | 205 | end |
|
210 | 206 | |
|
211 | 207 | def modules |
|
212 | 208 | @project.enabled_module_names = params[:enabled_module_names] |
|
213 | 209 | flash[:notice] = l(:notice_successful_update) |
|
214 | 210 | redirect_to :action => 'settings', :id => @project, :tab => 'modules' |
|
215 | 211 | end |
|
216 | 212 | |
|
217 | 213 | def archive |
|
218 | 214 | if request.post? |
|
219 | 215 | unless @project.archive |
|
220 | 216 | flash[:error] = l(:error_can_not_archive_project) |
|
221 | 217 | end |
|
222 | 218 | end |
|
223 | 219 | redirect_to(url_for(:controller => 'admin', :action => 'projects', :status => params[:status])) |
|
224 | 220 | end |
|
225 | 221 | |
|
226 | 222 | def unarchive |
|
227 | 223 | @project.unarchive if request.post? && !@project.active? |
|
228 | 224 | redirect_to(url_for(:controller => 'admin', :action => 'projects', :status => params[:status])) |
|
229 | 225 | end |
|
230 | 226 | |
|
231 | 227 | def close |
|
232 | 228 | @project.close |
|
233 | 229 | redirect_to project_path(@project) |
|
234 | 230 | end |
|
235 | 231 | |
|
236 | 232 | def reopen |
|
237 | 233 | @project.reopen |
|
238 | 234 | redirect_to project_path(@project) |
|
239 | 235 | end |
|
240 | 236 | |
|
241 | 237 | # Delete @project |
|
242 | 238 | def destroy |
|
243 | 239 | @project_to_destroy = @project |
|
244 | 240 | if api_request? || params[:confirm] |
|
245 | 241 | @project_to_destroy.destroy |
|
246 | 242 | respond_to do |format| |
|
247 | 243 | format.html { redirect_to :controller => 'admin', :action => 'projects' } |
|
248 | 244 | format.api { head :ok } |
|
249 | 245 | end |
|
250 | 246 | end |
|
251 | 247 | # hide project in layout |
|
252 | 248 | @project = nil |
|
253 | 249 | end |
|
254 | 250 | |
|
255 | 251 | private |
|
256 | 252 | |
|
257 | 253 | # Validates parent_id param according to user's permissions |
|
258 | 254 | # TODO: move it to Project model in a validation that depends on User.current |
|
259 | 255 | def validate_parent_id |
|
260 | 256 | return true if User.current.admin? |
|
261 | 257 | parent_id = params[:project] && params[:project][:parent_id] |
|
262 | 258 | if parent_id || @project.new_record? |
|
263 | 259 | parent = parent_id.blank? ? nil : Project.find_by_id(parent_id.to_i) |
|
264 | 260 | unless @project.allowed_parents.include?(parent) |
|
265 | 261 | @project.errors.add :parent_id, :invalid |
|
266 | 262 | return false |
|
267 | 263 | end |
|
268 | 264 | end |
|
269 | 265 | true |
|
270 | 266 | end |
|
271 | 267 | end |
General Comments 0
You need to be logged in to leave comments.
Login now