@@ -1,441 +1,442 | |||
|
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 IssuesController < ApplicationController |
|
19 | 19 | menu_item :new_issue, :only => [:new, :create] |
|
20 | 20 | default_search_scope :issues |
|
21 | 21 | |
|
22 | 22 | before_filter :find_issue, :only => [:show, :edit, :update] |
|
23 | 23 | before_filter :find_issues, :only => [:bulk_edit, :bulk_update, :destroy] |
|
24 | 24 | before_filter :find_project, :only => [:new, :create, :update_form] |
|
25 | 25 | before_filter :authorize, :except => [:index] |
|
26 | 26 | before_filter :find_optional_project, :only => [:index] |
|
27 | 27 | before_filter :check_for_default_issue_status, :only => [:new, :create] |
|
28 | 28 | before_filter :build_new_issue_from_params, :only => [:new, :create, :update_form] |
|
29 | 29 | accept_rss_auth :index, :show |
|
30 | 30 | accept_api_auth :index, :show, :create, :update, :destroy |
|
31 | 31 | |
|
32 | 32 | rescue_from Query::StatementInvalid, :with => :query_statement_invalid |
|
33 | 33 | |
|
34 | 34 | helper :journals |
|
35 | 35 | helper :projects |
|
36 | 36 | include ProjectsHelper |
|
37 | 37 | helper :custom_fields |
|
38 | 38 | include CustomFieldsHelper |
|
39 | 39 | helper :issue_relations |
|
40 | 40 | include IssueRelationsHelper |
|
41 | 41 | helper :watchers |
|
42 | 42 | include WatchersHelper |
|
43 | 43 | helper :attachments |
|
44 | 44 | include AttachmentsHelper |
|
45 | 45 | helper :queries |
|
46 | 46 | include QueriesHelper |
|
47 | 47 | helper :repositories |
|
48 | 48 | include RepositoriesHelper |
|
49 | 49 | helper :sort |
|
50 | 50 | include SortHelper |
|
51 | 51 | include IssuesHelper |
|
52 | 52 | helper :timelog |
|
53 | 53 | include Redmine::Export::PDF |
|
54 | 54 | |
|
55 | 55 | def index |
|
56 | 56 | retrieve_query |
|
57 | 57 | sort_init(@query.sort_criteria.empty? ? [['id', 'desc']] : @query.sort_criteria) |
|
58 | 58 | sort_update(@query.sortable_columns) |
|
59 | 59 | @query.sort_criteria = sort_criteria.to_a |
|
60 | 60 | |
|
61 | 61 | if @query.valid? |
|
62 | 62 | case params[:format] |
|
63 | 63 | when 'csv', 'pdf' |
|
64 | 64 | @limit = Setting.issues_export_limit.to_i |
|
65 | 65 | when 'atom' |
|
66 | 66 | @limit = Setting.feeds_limit.to_i |
|
67 | 67 | when 'xml', 'json' |
|
68 | 68 | @offset, @limit = api_offset_and_limit |
|
69 | 69 | else |
|
70 | 70 | @limit = per_page_option |
|
71 | 71 | end |
|
72 | 72 | |
|
73 | 73 | @issue_count = @query.issue_count |
|
74 | 74 | @issue_pages = Paginator.new @issue_count, @limit, params['page'] |
|
75 | 75 | @offset ||= @issue_pages.offset |
|
76 | 76 | @issues = @query.issues(:include => [:assigned_to, :tracker, :priority, :category, :fixed_version], |
|
77 | 77 | :order => sort_clause, |
|
78 | 78 | :offset => @offset, |
|
79 | 79 | :limit => @limit) |
|
80 | 80 | @issue_count_by_group = @query.issue_count_by_group |
|
81 | 81 | |
|
82 | 82 | respond_to do |format| |
|
83 | 83 | format.html { render :template => 'issues/index', :layout => !request.xhr? } |
|
84 | 84 | format.api { |
|
85 | 85 | Issue.load_visible_relations(@issues) if include_in_api_response?('relations') |
|
86 | 86 | } |
|
87 | 87 | format.atom { render_feed(@issues, :title => "#{@project || Setting.app_title}: #{l(:label_issue_plural)}") } |
|
88 | 88 | format.csv { send_data(query_to_csv(@issues, @query, params), :type => 'text/csv; header=present', :filename => 'issues.csv') } |
|
89 | 89 | format.pdf { send_data(issues_to_pdf(@issues, @project, @query), :type => 'application/pdf', :filename => 'issues.pdf') } |
|
90 | 90 | end |
|
91 | 91 | else |
|
92 | 92 | respond_to do |format| |
|
93 | 93 | format.html { render(:template => 'issues/index', :layout => !request.xhr?) } |
|
94 | 94 | format.any(:atom, :csv, :pdf) { render(:nothing => true) } |
|
95 | 95 | format.api { render_validation_errors(@query) } |
|
96 | 96 | end |
|
97 | 97 | end |
|
98 | 98 | rescue ActiveRecord::RecordNotFound |
|
99 | 99 | render_404 |
|
100 | 100 | end |
|
101 | 101 | |
|
102 | 102 | def show |
|
103 | 103 | @journals = @issue.journals.includes(:user, :details).reorder("#{Journal.table_name}.id ASC").all |
|
104 | 104 | @journals.each_with_index {|j,i| j.indice = i+1} |
|
105 | 105 | @journals.reject!(&:private_notes?) unless User.current.allowed_to?(:view_private_notes, @issue.project) |
|
106 | 106 | @journals.reverse! if User.current.wants_comments_in_reverse_order? |
|
107 | 107 | |
|
108 | 108 | @changesets = @issue.changesets.visible.all |
|
109 | 109 | @changesets.reverse! if User.current.wants_comments_in_reverse_order? |
|
110 | 110 | |
|
111 | 111 | @relations = @issue.relations.select {|r| r.other_issue(@issue) && r.other_issue(@issue).visible? } |
|
112 | 112 | @allowed_statuses = @issue.new_statuses_allowed_to(User.current) |
|
113 | 113 | @edit_allowed = User.current.allowed_to?(:edit_issues, @project) |
|
114 | 114 | @priorities = IssuePriority.active |
|
115 | 115 | @time_entry = TimeEntry.new(:issue => @issue, :project => @issue.project) |
|
116 | 116 | @relation = IssueRelation.new |
|
117 | 117 | |
|
118 | 118 | respond_to do |format| |
|
119 | 119 | format.html { |
|
120 | 120 | retrieve_previous_and_next_issue_ids |
|
121 | 121 | render :template => 'issues/show' |
|
122 | 122 | } |
|
123 | 123 | format.api |
|
124 | 124 | format.atom { render :template => 'journals/index', :layout => false, :content_type => 'application/atom+xml' } |
|
125 | 125 | format.pdf { |
|
126 | 126 | pdf = issue_to_pdf(@issue, :journals => @journals) |
|
127 | 127 | send_data(pdf, :type => 'application/pdf', :filename => "#{@project.identifier}-#{@issue.id}.pdf") |
|
128 | 128 | } |
|
129 | 129 | end |
|
130 | 130 | end |
|
131 | 131 | |
|
132 | 132 | # Add a new issue |
|
133 | 133 | # The new issue will be created from an existing one if copy_from parameter is given |
|
134 | 134 | def new |
|
135 | 135 | respond_to do |format| |
|
136 | 136 | format.html { render :action => 'new', :layout => !request.xhr? } |
|
137 | 137 | end |
|
138 | 138 | end |
|
139 | 139 | |
|
140 | 140 | def create |
|
141 | 141 | call_hook(:controller_issues_new_before_save, { :params => params, :issue => @issue }) |
|
142 | 142 | @issue.save_attachments(params[:attachments] || (params[:issue] && params[:issue][:uploads])) |
|
143 | 143 | if @issue.save |
|
144 | 144 | call_hook(:controller_issues_new_after_save, { :params => params, :issue => @issue}) |
|
145 | 145 | respond_to do |format| |
|
146 | 146 | format.html { |
|
147 | 147 | render_attachment_warning_if_needed(@issue) |
|
148 | 148 | flash[:notice] = l(:notice_issue_successful_create, :id => view_context.link_to("##{@issue.id}", issue_path(@issue), :title => @issue.subject)) |
|
149 | 149 | if params[:continue] |
|
150 | 150 | attrs = {:tracker_id => @issue.tracker, :parent_issue_id => @issue.parent_issue_id}.reject {|k,v| v.nil?} |
|
151 | 151 | redirect_to new_project_issue_path(@issue.project, :issue => attrs) |
|
152 | 152 | else |
|
153 | 153 | redirect_to issue_path(@issue) |
|
154 | 154 | end |
|
155 | 155 | } |
|
156 | 156 | format.api { render :action => 'show', :status => :created, :location => issue_url(@issue) } |
|
157 | 157 | end |
|
158 | 158 | return |
|
159 | 159 | else |
|
160 | 160 | respond_to do |format| |
|
161 | 161 | format.html { render :action => 'new' } |
|
162 | 162 | format.api { render_validation_errors(@issue) } |
|
163 | 163 | end |
|
164 | 164 | end |
|
165 | 165 | end |
|
166 | 166 | |
|
167 | 167 | def edit |
|
168 | 168 | return unless update_issue_from_params |
|
169 | 169 | |
|
170 | 170 | respond_to do |format| |
|
171 | 171 | format.html { } |
|
172 | 172 | format.xml { } |
|
173 | 173 | end |
|
174 | 174 | end |
|
175 | 175 | |
|
176 | 176 | def update |
|
177 | 177 | return unless update_issue_from_params |
|
178 | 178 | @issue.save_attachments(params[:attachments] || (params[:issue] && params[:issue][:uploads])) |
|
179 | 179 | saved = false |
|
180 | 180 | begin |
|
181 | 181 | saved = @issue.save_issue_with_child_records(params, @time_entry) |
|
182 | 182 | rescue ActiveRecord::StaleObjectError |
|
183 | 183 | @conflict = true |
|
184 | 184 | if params[:last_journal_id] |
|
185 | 185 | @conflict_journals = @issue.journals_after(params[:last_journal_id]).all |
|
186 | 186 | @conflict_journals.reject!(&:private_notes?) unless User.current.allowed_to?(:view_private_notes, @issue.project) |
|
187 | 187 | end |
|
188 | 188 | end |
|
189 | 189 | |
|
190 | 190 | if saved |
|
191 | 191 | render_attachment_warning_if_needed(@issue) |
|
192 | 192 | flash[:notice] = l(:notice_successful_update) unless @issue.current_journal.new_record? |
|
193 | 193 | |
|
194 | 194 | respond_to do |format| |
|
195 | 195 | format.html { redirect_back_or_default issue_path(@issue) } |
|
196 | 196 | format.api { render_api_ok } |
|
197 | 197 | end |
|
198 | 198 | else |
|
199 | 199 | respond_to do |format| |
|
200 | 200 | format.html { render :action => 'edit' } |
|
201 | 201 | format.api { render_validation_errors(@issue) } |
|
202 | 202 | end |
|
203 | 203 | end |
|
204 | 204 | end |
|
205 | 205 | |
|
206 | 206 | # Updates the issue form when changing the project, status or tracker |
|
207 | 207 | # on issue creation/update |
|
208 | 208 | def update_form |
|
209 | 209 | end |
|
210 | 210 | |
|
211 | 211 | # Bulk edit/copy a set of issues |
|
212 | 212 | def bulk_edit |
|
213 | 213 | @issues.sort! |
|
214 | 214 | @copy = params[:copy].present? |
|
215 | 215 | @notes = params[:notes] |
|
216 | 216 | |
|
217 | 217 | if User.current.allowed_to?(:move_issues, @projects) |
|
218 | 218 | @allowed_projects = Issue.allowed_target_projects_on_move |
|
219 | 219 | if params[:issue] |
|
220 | 220 | @target_project = @allowed_projects.detect {|p| p.id.to_s == params[:issue][:project_id].to_s} |
|
221 | 221 | if @target_project |
|
222 | 222 | target_projects = [@target_project] |
|
223 | 223 | end |
|
224 | 224 | end |
|
225 | 225 | end |
|
226 | 226 | target_projects ||= @projects |
|
227 | 227 | |
|
228 | 228 | if @copy |
|
229 | 229 | @available_statuses = [IssueStatus.default] |
|
230 | 230 | else |
|
231 | 231 | @available_statuses = @issues.map(&:new_statuses_allowed_to).reduce(:&) |
|
232 | 232 | end |
|
233 | 233 | @custom_fields = target_projects.map{|p|p.all_issue_custom_fields}.reduce(:&) |
|
234 | 234 | @assignables = target_projects.map(&:assignable_users).reduce(:&) |
|
235 | 235 | @trackers = target_projects.map(&:trackers).reduce(:&) |
|
236 | 236 | @versions = target_projects.map {|p| p.shared_versions.open}.reduce(:&) |
|
237 | 237 | @categories = target_projects.map {|p| p.issue_categories}.reduce(:&) |
|
238 | 238 | if @copy |
|
239 | 239 | @attachments_present = @issues.detect {|i| i.attachments.any?}.present? |
|
240 | 240 | @subtasks_present = @issues.detect {|i| !i.leaf?}.present? |
|
241 | 241 | end |
|
242 | 242 | |
|
243 | 243 | @safe_attributes = @issues.map(&:safe_attribute_names).reduce(:&) |
|
244 | 244 | render :layout => false if request.xhr? |
|
245 | 245 | end |
|
246 | 246 | |
|
247 | 247 | def bulk_update |
|
248 | 248 | @issues.sort! |
|
249 | 249 | @copy = params[:copy].present? |
|
250 | 250 | attributes = parse_params_for_bulk_issue_attributes(params) |
|
251 | 251 | |
|
252 | 252 | unsaved_issue_ids = [] |
|
253 | 253 | moved_issues = [] |
|
254 | 254 | |
|
255 | 255 | if @copy && params[:copy_subtasks].present? |
|
256 | 256 | # Descendant issues will be copied with the parent task |
|
257 | 257 | # Don't copy them twice |
|
258 | 258 | @issues.reject! {|issue| @issues.detect {|other| issue.is_descendant_of?(other)}} |
|
259 | 259 | end |
|
260 | 260 | |
|
261 | 261 | @issues.each do |issue| |
|
262 | 262 | issue.reload |
|
263 | 263 | if @copy |
|
264 | 264 | issue = issue.copy({}, |
|
265 | 265 | :attachments => params[:copy_attachments].present?, |
|
266 | 266 | :subtasks => params[:copy_subtasks].present? |
|
267 | 267 | ) |
|
268 | 268 | end |
|
269 | 269 | journal = issue.init_journal(User.current, params[:notes]) |
|
270 | 270 | issue.safe_attributes = attributes |
|
271 | 271 | call_hook(:controller_issues_bulk_edit_before_save, { :params => params, :issue => issue }) |
|
272 | 272 | if issue.save |
|
273 | 273 | moved_issues << issue |
|
274 | 274 | else |
|
275 | logger.info "issue could not be updated or copied: #{issue.errors.full_messages}" if logger && logger.info | |
|
275 | 276 | # Keep unsaved issue ids to display them in flash error |
|
276 | 277 | unsaved_issue_ids << issue.id |
|
277 | 278 | end |
|
278 | 279 | end |
|
279 | 280 | set_flash_from_bulk_issue_save(@issues, unsaved_issue_ids) |
|
280 | 281 | |
|
281 | 282 | if params[:follow] |
|
282 | 283 | if @issues.size == 1 && moved_issues.size == 1 |
|
283 | 284 | redirect_to issue_path(moved_issues.first) |
|
284 | 285 | elsif moved_issues.map(&:project).uniq.size == 1 |
|
285 | 286 | redirect_to project_issues_path(moved_issues.map(&:project).first) |
|
286 | 287 | end |
|
287 | 288 | else |
|
288 | 289 | redirect_back_or_default _project_issues_path(@project) |
|
289 | 290 | end |
|
290 | 291 | end |
|
291 | 292 | |
|
292 | 293 | def destroy |
|
293 | 294 | @hours = TimeEntry.sum(:hours, :conditions => ['issue_id IN (?)', @issues]).to_f |
|
294 | 295 | if @hours > 0 |
|
295 | 296 | case params[:todo] |
|
296 | 297 | when 'destroy' |
|
297 | 298 | # nothing to do |
|
298 | 299 | when 'nullify' |
|
299 | 300 | TimeEntry.update_all('issue_id = NULL', ['issue_id IN (?)', @issues]) |
|
300 | 301 | when 'reassign' |
|
301 | 302 | reassign_to = @project.issues.find_by_id(params[:reassign_to_id]) |
|
302 | 303 | if reassign_to.nil? |
|
303 | 304 | flash.now[:error] = l(:error_issue_not_found_in_project) |
|
304 | 305 | return |
|
305 | 306 | else |
|
306 | 307 | TimeEntry.update_all("issue_id = #{reassign_to.id}", ['issue_id IN (?)', @issues]) |
|
307 | 308 | end |
|
308 | 309 | else |
|
309 | 310 | # display the destroy form if it's a user request |
|
310 | 311 | return unless api_request? |
|
311 | 312 | end |
|
312 | 313 | end |
|
313 | 314 | @issues.each do |issue| |
|
314 | 315 | begin |
|
315 | 316 | issue.reload.destroy |
|
316 | 317 | rescue ::ActiveRecord::RecordNotFound # raised by #reload if issue no longer exists |
|
317 | 318 | # nothing to do, issue was already deleted (eg. by a parent) |
|
318 | 319 | end |
|
319 | 320 | end |
|
320 | 321 | respond_to do |format| |
|
321 | 322 | format.html { redirect_back_or_default _project_issues_path(@project) } |
|
322 | 323 | format.api { render_api_ok } |
|
323 | 324 | end |
|
324 | 325 | end |
|
325 | 326 | |
|
326 | 327 | private |
|
327 | 328 | |
|
328 | 329 | def find_project |
|
329 | 330 | project_id = params[:project_id] || (params[:issue] && params[:issue][:project_id]) |
|
330 | 331 | @project = Project.find(project_id) |
|
331 | 332 | rescue ActiveRecord::RecordNotFound |
|
332 | 333 | render_404 |
|
333 | 334 | end |
|
334 | 335 | |
|
335 | 336 | def retrieve_previous_and_next_issue_ids |
|
336 | 337 | retrieve_query_from_session |
|
337 | 338 | if @query |
|
338 | 339 | sort_init(@query.sort_criteria.empty? ? [['id', 'desc']] : @query.sort_criteria) |
|
339 | 340 | sort_update(@query.sortable_columns, 'issues_index_sort') |
|
340 | 341 | limit = 500 |
|
341 | 342 | issue_ids = @query.issue_ids(:order => sort_clause, :limit => (limit + 1), :include => [:assigned_to, :tracker, :priority, :category, :fixed_version]) |
|
342 | 343 | if (idx = issue_ids.index(@issue.id)) && idx < limit |
|
343 | 344 | if issue_ids.size < 500 |
|
344 | 345 | @issue_position = idx + 1 |
|
345 | 346 | @issue_count = issue_ids.size |
|
346 | 347 | end |
|
347 | 348 | @prev_issue_id = issue_ids[idx - 1] if idx > 0 |
|
348 | 349 | @next_issue_id = issue_ids[idx + 1] if idx < (issue_ids.size - 1) |
|
349 | 350 | end |
|
350 | 351 | end |
|
351 | 352 | end |
|
352 | 353 | |
|
353 | 354 | # Used by #edit and #update to set some common instance variables |
|
354 | 355 | # from the params |
|
355 | 356 | # TODO: Refactor, not everything in here is needed by #edit |
|
356 | 357 | def update_issue_from_params |
|
357 | 358 | @edit_allowed = User.current.allowed_to?(:edit_issues, @project) |
|
358 | 359 | @time_entry = TimeEntry.new(:issue => @issue, :project => @issue.project) |
|
359 | 360 | @time_entry.attributes = params[:time_entry] |
|
360 | 361 | |
|
361 | 362 | @issue.init_journal(User.current) |
|
362 | 363 | |
|
363 | 364 | issue_attributes = params[:issue] |
|
364 | 365 | if issue_attributes && params[:conflict_resolution] |
|
365 | 366 | case params[:conflict_resolution] |
|
366 | 367 | when 'overwrite' |
|
367 | 368 | issue_attributes = issue_attributes.dup |
|
368 | 369 | issue_attributes.delete(:lock_version) |
|
369 | 370 | when 'add_notes' |
|
370 | 371 | issue_attributes = issue_attributes.slice(:notes) |
|
371 | 372 | when 'cancel' |
|
372 | 373 | redirect_to issue_path(@issue) |
|
373 | 374 | return false |
|
374 | 375 | end |
|
375 | 376 | end |
|
376 | 377 | @issue.safe_attributes = issue_attributes |
|
377 | 378 | @priorities = IssuePriority.active |
|
378 | 379 | @allowed_statuses = @issue.new_statuses_allowed_to(User.current) |
|
379 | 380 | true |
|
380 | 381 | end |
|
381 | 382 | |
|
382 | 383 | # TODO: Refactor, lots of extra code in here |
|
383 | 384 | # TODO: Changing tracker on an existing issue should not trigger this |
|
384 | 385 | def build_new_issue_from_params |
|
385 | 386 | if params[:id].blank? |
|
386 | 387 | @issue = Issue.new |
|
387 | 388 | if params[:copy_from] |
|
388 | 389 | begin |
|
389 | 390 | @copy_from = Issue.visible.find(params[:copy_from]) |
|
390 | 391 | @copy_attachments = params[:copy_attachments].present? || request.get? |
|
391 | 392 | @copy_subtasks = params[:copy_subtasks].present? || request.get? |
|
392 | 393 | @issue.copy_from(@copy_from, :attachments => @copy_attachments, :subtasks => @copy_subtasks) |
|
393 | 394 | rescue ActiveRecord::RecordNotFound |
|
394 | 395 | render_404 |
|
395 | 396 | return |
|
396 | 397 | end |
|
397 | 398 | end |
|
398 | 399 | @issue.project = @project |
|
399 | 400 | else |
|
400 | 401 | @issue = @project.issues.visible.find(params[:id]) |
|
401 | 402 | end |
|
402 | 403 | |
|
403 | 404 | @issue.project = @project |
|
404 | 405 | @issue.author ||= User.current |
|
405 | 406 | # Tracker must be set before custom field values |
|
406 | 407 | @issue.tracker ||= @project.trackers.find((params[:issue] && params[:issue][:tracker_id]) || params[:tracker_id] || :first) |
|
407 | 408 | if @issue.tracker.nil? |
|
408 | 409 | render_error l(:error_no_tracker_in_project) |
|
409 | 410 | return false |
|
410 | 411 | end |
|
411 | 412 | @issue.start_date ||= Date.today if Setting.default_issue_start_date_to_creation_date? |
|
412 | 413 | @issue.safe_attributes = params[:issue] |
|
413 | 414 | |
|
414 | 415 | @priorities = IssuePriority.active |
|
415 | 416 | @allowed_statuses = @issue.new_statuses_allowed_to(User.current, true) |
|
416 | 417 | @available_watchers = (@issue.project.users.sort + @issue.watcher_users).uniq |
|
417 | 418 | end |
|
418 | 419 | |
|
419 | 420 | def check_for_default_issue_status |
|
420 | 421 | if IssueStatus.default.nil? |
|
421 | 422 | render_error l(:error_no_default_issue_status) |
|
422 | 423 | return false |
|
423 | 424 | end |
|
424 | 425 | end |
|
425 | 426 | |
|
426 | 427 | def parse_params_for_bulk_issue_attributes(params) |
|
427 | 428 | attributes = (params[:issue] || {}).reject {|k,v| v.blank?} |
|
428 | 429 | attributes.keys.each {|k| attributes[k] = '' if attributes[k] == 'none'} |
|
429 | 430 | if custom = attributes[:custom_field_values] |
|
430 | 431 | custom.reject! {|k,v| v.blank?} |
|
431 | 432 | custom.keys.each do |k| |
|
432 | 433 | if custom[k].is_a?(Array) |
|
433 | 434 | custom[k] << '' if custom[k].delete('__none__') |
|
434 | 435 | else |
|
435 | 436 | custom[k] = '' if custom[k] == '__none__' |
|
436 | 437 | end |
|
437 | 438 | end |
|
438 | 439 | end |
|
439 | 440 | attributes |
|
440 | 441 | end |
|
441 | 442 | end |
@@ -1,314 +1,315 | |||
|
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 TimelogController < ApplicationController |
|
19 | 19 | menu_item :issues |
|
20 | 20 | |
|
21 | 21 | before_filter :find_project_for_new_time_entry, :only => [:create] |
|
22 | 22 | before_filter :find_time_entry, :only => [:show, :edit, :update] |
|
23 | 23 | before_filter :find_time_entries, :only => [:bulk_edit, :bulk_update, :destroy] |
|
24 | 24 | before_filter :authorize, :except => [:new, :index, :report] |
|
25 | 25 | |
|
26 | 26 | before_filter :find_optional_project, :only => [:index, :report] |
|
27 | 27 | before_filter :find_optional_project_for_new_time_entry, :only => [:new] |
|
28 | 28 | before_filter :authorize_global, :only => [:new, :index, :report] |
|
29 | 29 | |
|
30 | 30 | accept_rss_auth :index |
|
31 | 31 | accept_api_auth :index, :show, :create, :update, :destroy |
|
32 | 32 | |
|
33 | 33 | rescue_from Query::StatementInvalid, :with => :query_statement_invalid |
|
34 | 34 | |
|
35 | 35 | helper :sort |
|
36 | 36 | include SortHelper |
|
37 | 37 | helper :issues |
|
38 | 38 | include TimelogHelper |
|
39 | 39 | helper :custom_fields |
|
40 | 40 | include CustomFieldsHelper |
|
41 | 41 | helper :queries |
|
42 | 42 | include QueriesHelper |
|
43 | 43 | |
|
44 | 44 | def index |
|
45 | 45 | @query = TimeEntryQuery.build_from_params(params, :project => @project, :name => '_') |
|
46 | 46 | scope = time_entry_scope |
|
47 | 47 | |
|
48 | 48 | sort_init(@query.sort_criteria.empty? ? [['spent_on', 'desc']] : @query.sort_criteria) |
|
49 | 49 | sort_update(@query.sortable_columns) |
|
50 | 50 | |
|
51 | 51 | respond_to do |format| |
|
52 | 52 | format.html { |
|
53 | 53 | # Paginate results |
|
54 | 54 | @entry_count = scope.count |
|
55 | 55 | @entry_pages = Paginator.new @entry_count, per_page_option, params['page'] |
|
56 | 56 | @entries = scope.all( |
|
57 | 57 | :include => [:project, :activity, :user, {:issue => :tracker}], |
|
58 | 58 | :order => sort_clause, |
|
59 | 59 | :limit => @entry_pages.per_page, |
|
60 | 60 | :offset => @entry_pages.offset |
|
61 | 61 | ) |
|
62 | 62 | @total_hours = scope.sum(:hours).to_f |
|
63 | 63 | |
|
64 | 64 | render :layout => !request.xhr? |
|
65 | 65 | } |
|
66 | 66 | format.api { |
|
67 | 67 | @entry_count = scope.count |
|
68 | 68 | @offset, @limit = api_offset_and_limit |
|
69 | 69 | @entries = scope.all( |
|
70 | 70 | :include => [:project, :activity, :user, {:issue => :tracker}], |
|
71 | 71 | :order => sort_clause, |
|
72 | 72 | :limit => @limit, |
|
73 | 73 | :offset => @offset |
|
74 | 74 | ) |
|
75 | 75 | } |
|
76 | 76 | format.atom { |
|
77 | 77 | entries = scope.all( |
|
78 | 78 | :include => [:project, :activity, :user, {:issue => :tracker}], |
|
79 | 79 | :order => "#{TimeEntry.table_name}.created_on DESC", |
|
80 | 80 | :limit => Setting.feeds_limit.to_i |
|
81 | 81 | ) |
|
82 | 82 | render_feed(entries, :title => l(:label_spent_time)) |
|
83 | 83 | } |
|
84 | 84 | format.csv { |
|
85 | 85 | # Export all entries |
|
86 | 86 | @entries = scope.all( |
|
87 | 87 | :include => [:project, :activity, :user, {:issue => [:tracker, :assigned_to, :priority]}], |
|
88 | 88 | :order => sort_clause |
|
89 | 89 | ) |
|
90 | 90 | send_data(query_to_csv(@entries, @query, params), :type => 'text/csv; header=present', :filename => 'timelog.csv') |
|
91 | 91 | } |
|
92 | 92 | end |
|
93 | 93 | end |
|
94 | 94 | |
|
95 | 95 | def report |
|
96 | 96 | @query = TimeEntryQuery.build_from_params(params, :project => @project, :name => '_') |
|
97 | 97 | scope = time_entry_scope |
|
98 | 98 | |
|
99 | 99 | @report = Redmine::Helpers::TimeReport.new(@project, @issue, params[:criteria], params[:columns], scope) |
|
100 | 100 | |
|
101 | 101 | respond_to do |format| |
|
102 | 102 | format.html { render :layout => !request.xhr? } |
|
103 | 103 | format.csv { send_data(report_to_csv(@report), :type => 'text/csv; header=present', :filename => 'timelog.csv') } |
|
104 | 104 | end |
|
105 | 105 | end |
|
106 | 106 | |
|
107 | 107 | def show |
|
108 | 108 | respond_to do |format| |
|
109 | 109 | # TODO: Implement html response |
|
110 | 110 | format.html { render :nothing => true, :status => 406 } |
|
111 | 111 | format.api |
|
112 | 112 | end |
|
113 | 113 | end |
|
114 | 114 | |
|
115 | 115 | def new |
|
116 | 116 | @time_entry ||= TimeEntry.new(:project => @project, :issue => @issue, :user => User.current, :spent_on => User.current.today) |
|
117 | 117 | @time_entry.safe_attributes = params[:time_entry] |
|
118 | 118 | end |
|
119 | 119 | |
|
120 | 120 | def create |
|
121 | 121 | @time_entry ||= TimeEntry.new(:project => @project, :issue => @issue, :user => User.current, :spent_on => User.current.today) |
|
122 | 122 | @time_entry.safe_attributes = params[:time_entry] |
|
123 | 123 | |
|
124 | 124 | call_hook(:controller_timelog_edit_before_save, { :params => params, :time_entry => @time_entry }) |
|
125 | 125 | |
|
126 | 126 | if @time_entry.save |
|
127 | 127 | respond_to do |format| |
|
128 | 128 | format.html { |
|
129 | 129 | flash[:notice] = l(:notice_successful_create) |
|
130 | 130 | if params[:continue] |
|
131 | 131 | if params[:project_id] |
|
132 | 132 | options = { |
|
133 | 133 | :time_entry => {:issue_id => @time_entry.issue_id, :activity_id => @time_entry.activity_id}, |
|
134 | 134 | :back_url => params[:back_url] |
|
135 | 135 | } |
|
136 | 136 | if @time_entry.issue |
|
137 | 137 | redirect_to new_project_issue_time_entry_path(@time_entry.project, @time_entry.issue, options) |
|
138 | 138 | else |
|
139 | 139 | redirect_to new_project_time_entry_path(@time_entry.project, options) |
|
140 | 140 | end |
|
141 | 141 | else |
|
142 | 142 | options = { |
|
143 | 143 | :time_entry => {:project_id => @time_entry.project_id, :issue_id => @time_entry.issue_id, :activity_id => @time_entry.activity_id}, |
|
144 | 144 | :back_url => params[:back_url] |
|
145 | 145 | } |
|
146 | 146 | redirect_to new_time_entry_path(options) |
|
147 | 147 | end |
|
148 | 148 | else |
|
149 | 149 | redirect_back_or_default project_time_entries_path(@time_entry.project) |
|
150 | 150 | end |
|
151 | 151 | } |
|
152 | 152 | format.api { render :action => 'show', :status => :created, :location => time_entry_url(@time_entry) } |
|
153 | 153 | end |
|
154 | 154 | else |
|
155 | 155 | respond_to do |format| |
|
156 | 156 | format.html { render :action => 'new' } |
|
157 | 157 | format.api { render_validation_errors(@time_entry) } |
|
158 | 158 | end |
|
159 | 159 | end |
|
160 | 160 | end |
|
161 | 161 | |
|
162 | 162 | def edit |
|
163 | 163 | @time_entry.safe_attributes = params[:time_entry] |
|
164 | 164 | end |
|
165 | 165 | |
|
166 | 166 | def update |
|
167 | 167 | @time_entry.safe_attributes = params[:time_entry] |
|
168 | 168 | |
|
169 | 169 | call_hook(:controller_timelog_edit_before_save, { :params => params, :time_entry => @time_entry }) |
|
170 | 170 | |
|
171 | 171 | if @time_entry.save |
|
172 | 172 | respond_to do |format| |
|
173 | 173 | format.html { |
|
174 | 174 | flash[:notice] = l(:notice_successful_update) |
|
175 | 175 | redirect_back_or_default project_time_entries_path(@time_entry.project) |
|
176 | 176 | } |
|
177 | 177 | format.api { render_api_ok } |
|
178 | 178 | end |
|
179 | 179 | else |
|
180 | 180 | respond_to do |format| |
|
181 | 181 | format.html { render :action => 'edit' } |
|
182 | 182 | format.api { render_validation_errors(@time_entry) } |
|
183 | 183 | end |
|
184 | 184 | end |
|
185 | 185 | end |
|
186 | 186 | |
|
187 | 187 | def bulk_edit |
|
188 | 188 | @available_activities = TimeEntryActivity.shared.active |
|
189 | 189 | @custom_fields = TimeEntry.first.available_custom_fields |
|
190 | 190 | end |
|
191 | 191 | |
|
192 | 192 | def bulk_update |
|
193 | 193 | attributes = parse_params_for_bulk_time_entry_attributes(params) |
|
194 | 194 | |
|
195 | 195 | unsaved_time_entry_ids = [] |
|
196 | 196 | @time_entries.each do |time_entry| |
|
197 | 197 | time_entry.reload |
|
198 | 198 | time_entry.safe_attributes = attributes |
|
199 | 199 | call_hook(:controller_time_entries_bulk_edit_before_save, { :params => params, :time_entry => time_entry }) |
|
200 | 200 | unless time_entry.save |
|
201 | logger.info "time entry could not be updated: #{time_entry.errors.full_messages}" if logger && logger.info | |
|
201 | 202 | # Keep unsaved time_entry ids to display them in flash error |
|
202 | 203 | unsaved_time_entry_ids << time_entry.id |
|
203 | 204 | end |
|
204 | 205 | end |
|
205 | 206 | set_flash_from_bulk_time_entry_save(@time_entries, unsaved_time_entry_ids) |
|
206 | 207 | redirect_back_or_default project_time_entries_path(@projects.first) |
|
207 | 208 | end |
|
208 | 209 | |
|
209 | 210 | def destroy |
|
210 | 211 | destroyed = TimeEntry.transaction do |
|
211 | 212 | @time_entries.each do |t| |
|
212 | 213 | unless t.destroy && t.destroyed? |
|
213 | 214 | raise ActiveRecord::Rollback |
|
214 | 215 | end |
|
215 | 216 | end |
|
216 | 217 | end |
|
217 | 218 | |
|
218 | 219 | respond_to do |format| |
|
219 | 220 | format.html { |
|
220 | 221 | if destroyed |
|
221 | 222 | flash[:notice] = l(:notice_successful_delete) |
|
222 | 223 | else |
|
223 | 224 | flash[:error] = l(:notice_unable_delete_time_entry) |
|
224 | 225 | end |
|
225 | 226 | redirect_back_or_default project_time_entries_path(@projects.first) |
|
226 | 227 | } |
|
227 | 228 | format.api { |
|
228 | 229 | if destroyed |
|
229 | 230 | render_api_ok |
|
230 | 231 | else |
|
231 | 232 | render_validation_errors(@time_entries) |
|
232 | 233 | end |
|
233 | 234 | } |
|
234 | 235 | end |
|
235 | 236 | end |
|
236 | 237 | |
|
237 | 238 | private |
|
238 | 239 | def find_time_entry |
|
239 | 240 | @time_entry = TimeEntry.find(params[:id]) |
|
240 | 241 | unless @time_entry.editable_by?(User.current) |
|
241 | 242 | render_403 |
|
242 | 243 | return false |
|
243 | 244 | end |
|
244 | 245 | @project = @time_entry.project |
|
245 | 246 | rescue ActiveRecord::RecordNotFound |
|
246 | 247 | render_404 |
|
247 | 248 | end |
|
248 | 249 | |
|
249 | 250 | def find_time_entries |
|
250 | 251 | @time_entries = TimeEntry.find_all_by_id(params[:id] || params[:ids]) |
|
251 | 252 | raise ActiveRecord::RecordNotFound if @time_entries.empty? |
|
252 | 253 | @projects = @time_entries.collect(&:project).compact.uniq |
|
253 | 254 | @project = @projects.first if @projects.size == 1 |
|
254 | 255 | rescue ActiveRecord::RecordNotFound |
|
255 | 256 | render_404 |
|
256 | 257 | end |
|
257 | 258 | |
|
258 | 259 | def set_flash_from_bulk_time_entry_save(time_entries, unsaved_time_entry_ids) |
|
259 | 260 | if unsaved_time_entry_ids.empty? |
|
260 | 261 | flash[:notice] = l(:notice_successful_update) unless time_entries.empty? |
|
261 | 262 | else |
|
262 | 263 | flash[:error] = l(:notice_failed_to_save_time_entries, |
|
263 | 264 | :count => unsaved_time_entry_ids.size, |
|
264 | 265 | :total => time_entries.size, |
|
265 | 266 | :ids => '#' + unsaved_time_entry_ids.join(', #')) |
|
266 | 267 | end |
|
267 | 268 | end |
|
268 | 269 | |
|
269 | 270 | def find_optional_project_for_new_time_entry |
|
270 | 271 | if (project_id = (params[:project_id] || params[:time_entry] && params[:time_entry][:project_id])).present? |
|
271 | 272 | @project = Project.find(project_id) |
|
272 | 273 | end |
|
273 | 274 | if (issue_id = (params[:issue_id] || params[:time_entry] && params[:time_entry][:issue_id])).present? |
|
274 | 275 | @issue = Issue.find(issue_id) |
|
275 | 276 | @project ||= @issue.project |
|
276 | 277 | end |
|
277 | 278 | rescue ActiveRecord::RecordNotFound |
|
278 | 279 | render_404 |
|
279 | 280 | end |
|
280 | 281 | |
|
281 | 282 | def find_project_for_new_time_entry |
|
282 | 283 | find_optional_project_for_new_time_entry |
|
283 | 284 | if @project.nil? |
|
284 | 285 | render_404 |
|
285 | 286 | end |
|
286 | 287 | end |
|
287 | 288 | |
|
288 | 289 | def find_optional_project |
|
289 | 290 | if !params[:issue_id].blank? |
|
290 | 291 | @issue = Issue.find(params[:issue_id]) |
|
291 | 292 | @project = @issue.project |
|
292 | 293 | elsif !params[:project_id].blank? |
|
293 | 294 | @project = Project.find(params[:project_id]) |
|
294 | 295 | end |
|
295 | 296 | end |
|
296 | 297 | |
|
297 | 298 | # Returns the TimeEntry scope for index and report actions |
|
298 | 299 | def time_entry_scope |
|
299 | 300 | scope = TimeEntry.visible.where(@query.statement) |
|
300 | 301 | if @issue |
|
301 | 302 | scope = scope.on_issue(@issue) |
|
302 | 303 | elsif @project |
|
303 | 304 | scope = scope.on_project(@project, Setting.display_subprojects_issues?) |
|
304 | 305 | end |
|
305 | 306 | scope |
|
306 | 307 | end |
|
307 | 308 | |
|
308 | 309 | def parse_params_for_bulk_time_entry_attributes(params) |
|
309 | 310 | attributes = (params[:time_entry] || {}).reject {|k,v| v.blank?} |
|
310 | 311 | attributes.keys.each {|k| attributes[k] = '' if attributes[k] == 'none'} |
|
311 | 312 | attributes[:custom_field_values].reject! {|k,v| v.blank?} if attributes[:custom_field_values] |
|
312 | 313 | attributes |
|
313 | 314 | end |
|
314 | 315 | end |
General Comments 0
You need to be logged in to leave comments.
Login now