@@ -1,262 +1,262 | |||
|
1 | 1 | # Redmine - project management software |
|
2 | 2 | # Copyright (C) 2006-2013 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 | helper :members |
|
47 | 47 | |
|
48 | 48 | # Lists visible projects |
|
49 | 49 | def index |
|
50 | 50 | respond_to do |format| |
|
51 | 51 | format.html { |
|
52 | 52 | scope = Project |
|
53 | 53 | unless params[:closed] |
|
54 | 54 | scope = scope.active |
|
55 | 55 | end |
|
56 | 56 | @projects = scope.visible.order('lft').all |
|
57 | 57 | } |
|
58 | 58 | format.api { |
|
59 | 59 | @offset, @limit = api_offset_and_limit |
|
60 | 60 | @project_count = Project.visible.count |
|
61 | 61 | @projects = Project.visible.offset(@offset).limit(@limit).order('lft').all |
|
62 | 62 | } |
|
63 | 63 | format.atom { |
|
64 | 64 | projects = Project.visible.order('created_on DESC').limit(Setting.feeds_limit.to_i).all |
|
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.sorted.all |
|
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.sorted.all |
|
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 |
# Add current user as a project member if |
|
|
85 | # Add current user as a project member if current user 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 | if params[:continue] |
|
95 | 95 | attrs = {:parent_id => @project.parent_id}.reject {|k,v| v.nil?} |
|
96 | 96 | redirect_to new_project_path(attrs) |
|
97 | 97 | else |
|
98 | 98 | redirect_to settings_project_path(@project) |
|
99 | 99 | end |
|
100 | 100 | } |
|
101 | 101 | format.api { render :action => 'show', :status => :created, :location => url_for(:controller => 'projects', :action => 'show', :id => @project.id) } |
|
102 | 102 | end |
|
103 | 103 | else |
|
104 | 104 | respond_to do |format| |
|
105 | 105 | format.html { render :action => 'new' } |
|
106 | 106 | format.api { render_validation_errors(@project) } |
|
107 | 107 | end |
|
108 | 108 | end |
|
109 | 109 | end |
|
110 | 110 | |
|
111 | 111 | def copy |
|
112 | 112 | @issue_custom_fields = IssueCustomField.sorted.all |
|
113 | 113 | @trackers = Tracker.sorted.all |
|
114 | 114 | @source_project = Project.find(params[:id]) |
|
115 | 115 | if request.get? |
|
116 | 116 | @project = Project.copy_from(@source_project) |
|
117 | 117 | @project.identifier = Project.next_identifier if Setting.sequential_project_identifiers? |
|
118 | 118 | else |
|
119 | 119 | Mailer.with_deliveries(params[:notifications] == '1') do |
|
120 | 120 | @project = Project.new |
|
121 | 121 | @project.safe_attributes = params[:project] |
|
122 | 122 | if validate_parent_id && @project.copy(@source_project, :only => params[:only]) |
|
123 | 123 | @project.set_allowed_parent!(params[:project]['parent_id']) if params[:project].has_key?('parent_id') |
|
124 | 124 | flash[:notice] = l(:notice_successful_create) |
|
125 | 125 | redirect_to settings_project_path(@project) |
|
126 | 126 | elsif !@project.new_record? |
|
127 | 127 | # Project was created |
|
128 | 128 | # But some objects were not copied due to validation failures |
|
129 | 129 | # (eg. issues from disabled trackers) |
|
130 | 130 | # TODO: inform about that |
|
131 | 131 | redirect_to settings_project_path(@project) |
|
132 | 132 | end |
|
133 | 133 | end |
|
134 | 134 | end |
|
135 | 135 | rescue ActiveRecord::RecordNotFound |
|
136 | 136 | # source_project not found |
|
137 | 137 | render_404 |
|
138 | 138 | end |
|
139 | 139 | |
|
140 | 140 | # Show @project |
|
141 | 141 | def show |
|
142 | 142 | # try to redirect to the requested menu item |
|
143 | 143 | if params[:jump] && redirect_to_project_menu_item(@project, params[:jump]) |
|
144 | 144 | return |
|
145 | 145 | end |
|
146 | 146 | |
|
147 | 147 | @users_by_role = @project.users_by_role |
|
148 | 148 | @subprojects = @project.children.visible.all |
|
149 | 149 | @news = @project.news.limit(5).includes(:author, :project).reorder("#{News.table_name}.created_on DESC").all |
|
150 | 150 | @trackers = @project.rolled_up_trackers |
|
151 | 151 | |
|
152 | 152 | cond = @project.project_condition(Setting.display_subprojects_issues?) |
|
153 | 153 | |
|
154 | 154 | @open_issues_by_tracker = Issue.visible.open.where(cond).count(:group => :tracker) |
|
155 | 155 | @total_issues_by_tracker = Issue.visible.where(cond).count(:group => :tracker) |
|
156 | 156 | |
|
157 | 157 | if User.current.allowed_to?(:view_time_entries, @project) |
|
158 | 158 | @total_hours = TimeEntry.visible.where(cond).sum(:hours).to_f |
|
159 | 159 | end |
|
160 | 160 | |
|
161 | 161 | @key = User.current.rss_key |
|
162 | 162 | |
|
163 | 163 | respond_to do |format| |
|
164 | 164 | format.html |
|
165 | 165 | format.api |
|
166 | 166 | end |
|
167 | 167 | end |
|
168 | 168 | |
|
169 | 169 | def settings |
|
170 | 170 | @issue_custom_fields = IssueCustomField.sorted.all |
|
171 | 171 | @issue_category ||= IssueCategory.new |
|
172 | 172 | @member ||= @project.members.new |
|
173 | 173 | @trackers = Tracker.sorted.all |
|
174 | 174 | @wiki ||= @project.wiki |
|
175 | 175 | end |
|
176 | 176 | |
|
177 | 177 | def edit |
|
178 | 178 | end |
|
179 | 179 | |
|
180 | 180 | def update |
|
181 | 181 | @project.safe_attributes = params[:project] |
|
182 | 182 | if validate_parent_id && @project.save |
|
183 | 183 | @project.set_allowed_parent!(params[:project]['parent_id']) if params[:project].has_key?('parent_id') |
|
184 | 184 | respond_to do |format| |
|
185 | 185 | format.html { |
|
186 | 186 | flash[:notice] = l(:notice_successful_update) |
|
187 | 187 | redirect_to settings_project_path(@project) |
|
188 | 188 | } |
|
189 | 189 | format.api { render_api_ok } |
|
190 | 190 | end |
|
191 | 191 | else |
|
192 | 192 | respond_to do |format| |
|
193 | 193 | format.html { |
|
194 | 194 | settings |
|
195 | 195 | render :action => 'settings' |
|
196 | 196 | } |
|
197 | 197 | format.api { render_validation_errors(@project) } |
|
198 | 198 | end |
|
199 | 199 | end |
|
200 | 200 | end |
|
201 | 201 | |
|
202 | 202 | def modules |
|
203 | 203 | @project.enabled_module_names = params[:enabled_module_names] |
|
204 | 204 | flash[:notice] = l(:notice_successful_update) |
|
205 | 205 | redirect_to settings_project_path(@project, :tab => 'modules') |
|
206 | 206 | end |
|
207 | 207 | |
|
208 | 208 | def archive |
|
209 | 209 | if request.post? |
|
210 | 210 | unless @project.archive |
|
211 | 211 | flash[:error] = l(:error_can_not_archive_project) |
|
212 | 212 | end |
|
213 | 213 | end |
|
214 | 214 | redirect_to admin_projects_path(:status => params[:status]) |
|
215 | 215 | end |
|
216 | 216 | |
|
217 | 217 | def unarchive |
|
218 | 218 | @project.unarchive if request.post? && !@project.active? |
|
219 | 219 | redirect_to admin_projects_path(:status => params[:status]) |
|
220 | 220 | end |
|
221 | 221 | |
|
222 | 222 | def close |
|
223 | 223 | @project.close |
|
224 | 224 | redirect_to project_path(@project) |
|
225 | 225 | end |
|
226 | 226 | |
|
227 | 227 | def reopen |
|
228 | 228 | @project.reopen |
|
229 | 229 | redirect_to project_path(@project) |
|
230 | 230 | end |
|
231 | 231 | |
|
232 | 232 | # Delete @project |
|
233 | 233 | def destroy |
|
234 | 234 | @project_to_destroy = @project |
|
235 | 235 | if api_request? || params[:confirm] |
|
236 | 236 | @project_to_destroy.destroy |
|
237 | 237 | respond_to do |format| |
|
238 | 238 | format.html { redirect_to admin_projects_path } |
|
239 | 239 | format.api { render_api_ok } |
|
240 | 240 | end |
|
241 | 241 | end |
|
242 | 242 | # hide project in layout |
|
243 | 243 | @project = nil |
|
244 | 244 | end |
|
245 | 245 | |
|
246 | 246 | private |
|
247 | 247 | |
|
248 | 248 | # Validates parent_id param according to user's permissions |
|
249 | 249 | # TODO: move it to Project model in a validation that depends on User.current |
|
250 | 250 | def validate_parent_id |
|
251 | 251 | return true if User.current.admin? |
|
252 | 252 | parent_id = params[:project] && params[:project][:parent_id] |
|
253 | 253 | if parent_id || @project.new_record? |
|
254 | 254 | parent = parent_id.blank? ? nil : Project.find_by_id(parent_id.to_i) |
|
255 | 255 | unless @project.allowed_parents.include?(parent) |
|
256 | 256 | @project.errors.add :parent_id, :invalid |
|
257 | 257 | return false |
|
258 | 258 | end |
|
259 | 259 | end |
|
260 | 260 | true |
|
261 | 261 | end |
|
262 | 262 | end |
General Comments 0
You need to be logged in to leave comments.
Login now