The requested changes are too big and content was truncated. Show full diff
@@ -1,439 +1,439 | |||
|
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 |
format.csv { send_data( |
|
|
88 | format.csv { send_data(query_to_csv(@issues, @query, params), :type => 'text/csv; header=present', :filename => 'export.csv') } | |
|
89 | 89 | format.pdf { send_data(issues_to_pdf(@issues, @project, @query), :type => 'application/pdf', :filename => 'export.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 | respond_to do |format| |
|
117 | 117 | format.html { |
|
118 | 118 | retrieve_previous_and_next_issue_ids |
|
119 | 119 | render :template => 'issues/show' |
|
120 | 120 | } |
|
121 | 121 | format.api |
|
122 | 122 | format.atom { render :template => 'journals/index', :layout => false, :content_type => 'application/atom+xml' } |
|
123 | 123 | format.pdf { |
|
124 | 124 | pdf = issue_to_pdf(@issue, :journals => @journals) |
|
125 | 125 | send_data(pdf, :type => 'application/pdf', :filename => "#{@project.identifier}-#{@issue.id}.pdf") |
|
126 | 126 | } |
|
127 | 127 | end |
|
128 | 128 | end |
|
129 | 129 | |
|
130 | 130 | # Add a new issue |
|
131 | 131 | # The new issue will be created from an existing one if copy_from parameter is given |
|
132 | 132 | def new |
|
133 | 133 | respond_to do |format| |
|
134 | 134 | format.html { render :action => 'new', :layout => !request.xhr? } |
|
135 | 135 | end |
|
136 | 136 | end |
|
137 | 137 | |
|
138 | 138 | def create |
|
139 | 139 | call_hook(:controller_issues_new_before_save, { :params => params, :issue => @issue }) |
|
140 | 140 | @issue.save_attachments(params[:attachments] || (params[:issue] && params[:issue][:uploads])) |
|
141 | 141 | if @issue.save |
|
142 | 142 | call_hook(:controller_issues_new_after_save, { :params => params, :issue => @issue}) |
|
143 | 143 | respond_to do |format| |
|
144 | 144 | format.html { |
|
145 | 145 | render_attachment_warning_if_needed(@issue) |
|
146 | 146 | flash[:notice] = l(:notice_issue_successful_create, :id => view_context.link_to("##{@issue.id}", issue_path(@issue), :title => @issue.subject)) |
|
147 | 147 | if params[:continue] |
|
148 | 148 | attrs = {:tracker_id => @issue.tracker, :parent_issue_id => @issue.parent_issue_id}.reject {|k,v| v.nil?} |
|
149 | 149 | redirect_to new_project_issue_path(@issue.project, :issue => attrs) |
|
150 | 150 | else |
|
151 | 151 | redirect_to issue_path(@issue) |
|
152 | 152 | end |
|
153 | 153 | } |
|
154 | 154 | format.api { render :action => 'show', :status => :created, :location => issue_url(@issue) } |
|
155 | 155 | end |
|
156 | 156 | return |
|
157 | 157 | else |
|
158 | 158 | respond_to do |format| |
|
159 | 159 | format.html { render :action => 'new' } |
|
160 | 160 | format.api { render_validation_errors(@issue) } |
|
161 | 161 | end |
|
162 | 162 | end |
|
163 | 163 | end |
|
164 | 164 | |
|
165 | 165 | def edit |
|
166 | 166 | return unless update_issue_from_params |
|
167 | 167 | |
|
168 | 168 | respond_to do |format| |
|
169 | 169 | format.html { } |
|
170 | 170 | format.xml { } |
|
171 | 171 | end |
|
172 | 172 | end |
|
173 | 173 | |
|
174 | 174 | def update |
|
175 | 175 | return unless update_issue_from_params |
|
176 | 176 | @issue.save_attachments(params[:attachments] || (params[:issue] && params[:issue][:uploads])) |
|
177 | 177 | saved = false |
|
178 | 178 | begin |
|
179 | 179 | saved = @issue.save_issue_with_child_records(params, @time_entry) |
|
180 | 180 | rescue ActiveRecord::StaleObjectError |
|
181 | 181 | @conflict = true |
|
182 | 182 | if params[:last_journal_id] |
|
183 | 183 | @conflict_journals = @issue.journals_after(params[:last_journal_id]).all |
|
184 | 184 | @conflict_journals.reject!(&:private_notes?) unless User.current.allowed_to?(:view_private_notes, @issue.project) |
|
185 | 185 | end |
|
186 | 186 | end |
|
187 | 187 | |
|
188 | 188 | if saved |
|
189 | 189 | render_attachment_warning_if_needed(@issue) |
|
190 | 190 | flash[:notice] = l(:notice_successful_update) unless @issue.current_journal.new_record? |
|
191 | 191 | |
|
192 | 192 | respond_to do |format| |
|
193 | 193 | format.html { redirect_back_or_default issue_path(@issue) } |
|
194 | 194 | format.api { render_api_ok } |
|
195 | 195 | end |
|
196 | 196 | else |
|
197 | 197 | respond_to do |format| |
|
198 | 198 | format.html { render :action => 'edit' } |
|
199 | 199 | format.api { render_validation_errors(@issue) } |
|
200 | 200 | end |
|
201 | 201 | end |
|
202 | 202 | end |
|
203 | 203 | |
|
204 | 204 | # Updates the issue form when changing the project, status or tracker |
|
205 | 205 | # on issue creation/update |
|
206 | 206 | def update_form |
|
207 | 207 | end |
|
208 | 208 | |
|
209 | 209 | # Bulk edit/copy a set of issues |
|
210 | 210 | def bulk_edit |
|
211 | 211 | @issues.sort! |
|
212 | 212 | @copy = params[:copy].present? |
|
213 | 213 | @notes = params[:notes] |
|
214 | 214 | |
|
215 | 215 | if User.current.allowed_to?(:move_issues, @projects) |
|
216 | 216 | @allowed_projects = Issue.allowed_target_projects_on_move |
|
217 | 217 | if params[:issue] |
|
218 | 218 | @target_project = @allowed_projects.detect {|p| p.id.to_s == params[:issue][:project_id].to_s} |
|
219 | 219 | if @target_project |
|
220 | 220 | target_projects = [@target_project] |
|
221 | 221 | end |
|
222 | 222 | end |
|
223 | 223 | end |
|
224 | 224 | target_projects ||= @projects |
|
225 | 225 | |
|
226 | 226 | if @copy |
|
227 | 227 | @available_statuses = [IssueStatus.default] |
|
228 | 228 | else |
|
229 | 229 | @available_statuses = @issues.map(&:new_statuses_allowed_to).reduce(:&) |
|
230 | 230 | end |
|
231 | 231 | @custom_fields = target_projects.map{|p|p.all_issue_custom_fields}.reduce(:&) |
|
232 | 232 | @assignables = target_projects.map(&:assignable_users).reduce(:&) |
|
233 | 233 | @trackers = target_projects.map(&:trackers).reduce(:&) |
|
234 | 234 | @versions = target_projects.map {|p| p.shared_versions.open}.reduce(:&) |
|
235 | 235 | @categories = target_projects.map {|p| p.issue_categories}.reduce(:&) |
|
236 | 236 | if @copy |
|
237 | 237 | @attachments_present = @issues.detect {|i| i.attachments.any?}.present? |
|
238 | 238 | @subtasks_present = @issues.detect {|i| !i.leaf?}.present? |
|
239 | 239 | end |
|
240 | 240 | |
|
241 | 241 | @safe_attributes = @issues.map(&:safe_attribute_names).reduce(:&) |
|
242 | 242 | render :layout => false if request.xhr? |
|
243 | 243 | end |
|
244 | 244 | |
|
245 | 245 | def bulk_update |
|
246 | 246 | @issues.sort! |
|
247 | 247 | @copy = params[:copy].present? |
|
248 | 248 | attributes = parse_params_for_bulk_issue_attributes(params) |
|
249 | 249 | |
|
250 | 250 | unsaved_issue_ids = [] |
|
251 | 251 | moved_issues = [] |
|
252 | 252 | |
|
253 | 253 | if @copy && params[:copy_subtasks].present? |
|
254 | 254 | # Descendant issues will be copied with the parent task |
|
255 | 255 | # Don't copy them twice |
|
256 | 256 | @issues.reject! {|issue| @issues.detect {|other| issue.is_descendant_of?(other)}} |
|
257 | 257 | end |
|
258 | 258 | |
|
259 | 259 | @issues.each do |issue| |
|
260 | 260 | issue.reload |
|
261 | 261 | if @copy |
|
262 | 262 | issue = issue.copy({}, |
|
263 | 263 | :attachments => params[:copy_attachments].present?, |
|
264 | 264 | :subtasks => params[:copy_subtasks].present? |
|
265 | 265 | ) |
|
266 | 266 | end |
|
267 | 267 | journal = issue.init_journal(User.current, params[:notes]) |
|
268 | 268 | issue.safe_attributes = attributes |
|
269 | 269 | call_hook(:controller_issues_bulk_edit_before_save, { :params => params, :issue => issue }) |
|
270 | 270 | if issue.save |
|
271 | 271 | moved_issues << issue |
|
272 | 272 | else |
|
273 | 273 | # Keep unsaved issue ids to display them in flash error |
|
274 | 274 | unsaved_issue_ids << issue.id |
|
275 | 275 | end |
|
276 | 276 | end |
|
277 | 277 | set_flash_from_bulk_issue_save(@issues, unsaved_issue_ids) |
|
278 | 278 | |
|
279 | 279 | if params[:follow] |
|
280 | 280 | if @issues.size == 1 && moved_issues.size == 1 |
|
281 | 281 | redirect_to issue_path(moved_issues.first) |
|
282 | 282 | elsif moved_issues.map(&:project).uniq.size == 1 |
|
283 | 283 | redirect_to project_issues_path(moved_issues.map(&:project).first) |
|
284 | 284 | end |
|
285 | 285 | else |
|
286 | 286 | redirect_back_or_default _project_issues_path(@project) |
|
287 | 287 | end |
|
288 | 288 | end |
|
289 | 289 | |
|
290 | 290 | def destroy |
|
291 | 291 | @hours = TimeEntry.sum(:hours, :conditions => ['issue_id IN (?)', @issues]).to_f |
|
292 | 292 | if @hours > 0 |
|
293 | 293 | case params[:todo] |
|
294 | 294 | when 'destroy' |
|
295 | 295 | # nothing to do |
|
296 | 296 | when 'nullify' |
|
297 | 297 | TimeEntry.update_all('issue_id = NULL', ['issue_id IN (?)', @issues]) |
|
298 | 298 | when 'reassign' |
|
299 | 299 | reassign_to = @project.issues.find_by_id(params[:reassign_to_id]) |
|
300 | 300 | if reassign_to.nil? |
|
301 | 301 | flash.now[:error] = l(:error_issue_not_found_in_project) |
|
302 | 302 | return |
|
303 | 303 | else |
|
304 | 304 | TimeEntry.update_all("issue_id = #{reassign_to.id}", ['issue_id IN (?)', @issues]) |
|
305 | 305 | end |
|
306 | 306 | else |
|
307 | 307 | # display the destroy form if it's a user request |
|
308 | 308 | return unless api_request? |
|
309 | 309 | end |
|
310 | 310 | end |
|
311 | 311 | @issues.each do |issue| |
|
312 | 312 | begin |
|
313 | 313 | issue.reload.destroy |
|
314 | 314 | rescue ::ActiveRecord::RecordNotFound # raised by #reload if issue no longer exists |
|
315 | 315 | # nothing to do, issue was already deleted (eg. by a parent) |
|
316 | 316 | end |
|
317 | 317 | end |
|
318 | 318 | respond_to do |format| |
|
319 | 319 | format.html { redirect_back_or_default _project_issues_path(@project) } |
|
320 | 320 | format.api { render_api_ok } |
|
321 | 321 | end |
|
322 | 322 | end |
|
323 | 323 | |
|
324 | 324 | private |
|
325 | 325 | |
|
326 | 326 | def find_project |
|
327 | 327 | project_id = params[:project_id] || (params[:issue] && params[:issue][:project_id]) |
|
328 | 328 | @project = Project.find(project_id) |
|
329 | 329 | rescue ActiveRecord::RecordNotFound |
|
330 | 330 | render_404 |
|
331 | 331 | end |
|
332 | 332 | |
|
333 | 333 | def retrieve_previous_and_next_issue_ids |
|
334 | 334 | retrieve_query_from_session |
|
335 | 335 | if @query |
|
336 | 336 | sort_init(@query.sort_criteria.empty? ? [['id', 'desc']] : @query.sort_criteria) |
|
337 | 337 | sort_update(@query.sortable_columns, 'issues_index_sort') |
|
338 | 338 | limit = 500 |
|
339 | 339 | issue_ids = @query.issue_ids(:order => sort_clause, :limit => (limit + 1), :include => [:assigned_to, :tracker, :priority, :category, :fixed_version]) |
|
340 | 340 | if (idx = issue_ids.index(@issue.id)) && idx < limit |
|
341 | 341 | if issue_ids.size < 500 |
|
342 | 342 | @issue_position = idx + 1 |
|
343 | 343 | @issue_count = issue_ids.size |
|
344 | 344 | end |
|
345 | 345 | @prev_issue_id = issue_ids[idx - 1] if idx > 0 |
|
346 | 346 | @next_issue_id = issue_ids[idx + 1] if idx < (issue_ids.size - 1) |
|
347 | 347 | end |
|
348 | 348 | end |
|
349 | 349 | end |
|
350 | 350 | |
|
351 | 351 | # Used by #edit and #update to set some common instance variables |
|
352 | 352 | # from the params |
|
353 | 353 | # TODO: Refactor, not everything in here is needed by #edit |
|
354 | 354 | def update_issue_from_params |
|
355 | 355 | @edit_allowed = User.current.allowed_to?(:edit_issues, @project) |
|
356 | 356 | @time_entry = TimeEntry.new(:issue => @issue, :project => @issue.project) |
|
357 | 357 | @time_entry.attributes = params[:time_entry] |
|
358 | 358 | |
|
359 | 359 | @issue.init_journal(User.current) |
|
360 | 360 | |
|
361 | 361 | issue_attributes = params[:issue] |
|
362 | 362 | if issue_attributes && params[:conflict_resolution] |
|
363 | 363 | case params[:conflict_resolution] |
|
364 | 364 | when 'overwrite' |
|
365 | 365 | issue_attributes = issue_attributes.dup |
|
366 | 366 | issue_attributes.delete(:lock_version) |
|
367 | 367 | when 'add_notes' |
|
368 | 368 | issue_attributes = issue_attributes.slice(:notes) |
|
369 | 369 | when 'cancel' |
|
370 | 370 | redirect_to issue_path(@issue) |
|
371 | 371 | return false |
|
372 | 372 | end |
|
373 | 373 | end |
|
374 | 374 | @issue.safe_attributes = issue_attributes |
|
375 | 375 | @priorities = IssuePriority.active |
|
376 | 376 | @allowed_statuses = @issue.new_statuses_allowed_to(User.current) |
|
377 | 377 | true |
|
378 | 378 | end |
|
379 | 379 | |
|
380 | 380 | # TODO: Refactor, lots of extra code in here |
|
381 | 381 | # TODO: Changing tracker on an existing issue should not trigger this |
|
382 | 382 | def build_new_issue_from_params |
|
383 | 383 | if params[:id].blank? |
|
384 | 384 | @issue = Issue.new |
|
385 | 385 | if params[:copy_from] |
|
386 | 386 | begin |
|
387 | 387 | @copy_from = Issue.visible.find(params[:copy_from]) |
|
388 | 388 | @copy_attachments = params[:copy_attachments].present? || request.get? |
|
389 | 389 | @copy_subtasks = params[:copy_subtasks].present? || request.get? |
|
390 | 390 | @issue.copy_from(@copy_from, :attachments => @copy_attachments, :subtasks => @copy_subtasks) |
|
391 | 391 | rescue ActiveRecord::RecordNotFound |
|
392 | 392 | render_404 |
|
393 | 393 | return |
|
394 | 394 | end |
|
395 | 395 | end |
|
396 | 396 | @issue.project = @project |
|
397 | 397 | else |
|
398 | 398 | @issue = @project.issues.visible.find(params[:id]) |
|
399 | 399 | end |
|
400 | 400 | |
|
401 | 401 | @issue.project = @project |
|
402 | 402 | @issue.author ||= User.current |
|
403 | 403 | # Tracker must be set before custom field values |
|
404 | 404 | @issue.tracker ||= @project.trackers.find((params[:issue] && params[:issue][:tracker_id]) || params[:tracker_id] || :first) |
|
405 | 405 | if @issue.tracker.nil? |
|
406 | 406 | render_error l(:error_no_tracker_in_project) |
|
407 | 407 | return false |
|
408 | 408 | end |
|
409 | 409 | @issue.start_date ||= Date.today if Setting.default_issue_start_date_to_creation_date? |
|
410 | 410 | @issue.safe_attributes = params[:issue] |
|
411 | 411 | |
|
412 | 412 | @priorities = IssuePriority.active |
|
413 | 413 | @allowed_statuses = @issue.new_statuses_allowed_to(User.current, true) |
|
414 | 414 | @available_watchers = (@issue.project.users.sort + @issue.watcher_users).uniq |
|
415 | 415 | end |
|
416 | 416 | |
|
417 | 417 | def check_for_default_issue_status |
|
418 | 418 | if IssueStatus.default.nil? |
|
419 | 419 | render_error l(:error_no_default_issue_status) |
|
420 | 420 | return false |
|
421 | 421 | end |
|
422 | 422 | end |
|
423 | 423 | |
|
424 | 424 | def parse_params_for_bulk_issue_attributes(params) |
|
425 | 425 | attributes = (params[:issue] || {}).reject {|k,v| v.blank?} |
|
426 | 426 | attributes.keys.each {|k| attributes[k] = '' if attributes[k] == 'none'} |
|
427 | 427 | if custom = attributes[:custom_field_values] |
|
428 | 428 | custom.reject! {|k,v| v.blank?} |
|
429 | 429 | custom.keys.each do |k| |
|
430 | 430 | if custom[k].is_a?(Array) |
|
431 | 431 | custom[k] << '' if custom[k].delete('__none__') |
|
432 | 432 | else |
|
433 | 433 | custom[k] = '' if custom[k] == '__none__' |
|
434 | 434 | end |
|
435 | 435 | end |
|
436 | 436 | end |
|
437 | 437 | attributes |
|
438 | 438 | end |
|
439 | 439 | end |
@@ -1,314 +1,314 | |||
|
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 |
send_data( |
|
|
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 | 201 | # Keep unsaved time_entry ids to display them in flash error |
|
202 | 202 | unsaved_time_entry_ids << time_entry.id |
|
203 | 203 | end |
|
204 | 204 | end |
|
205 | 205 | set_flash_from_bulk_time_entry_save(@time_entries, unsaved_time_entry_ids) |
|
206 | 206 | redirect_back_or_default project_time_entries_path(@projects.first) |
|
207 | 207 | end |
|
208 | 208 | |
|
209 | 209 | def destroy |
|
210 | 210 | destroyed = TimeEntry.transaction do |
|
211 | 211 | @time_entries.each do |t| |
|
212 | 212 | unless t.destroy && t.destroyed? |
|
213 | 213 | raise ActiveRecord::Rollback |
|
214 | 214 | end |
|
215 | 215 | end |
|
216 | 216 | end |
|
217 | 217 | |
|
218 | 218 | respond_to do |format| |
|
219 | 219 | format.html { |
|
220 | 220 | if destroyed |
|
221 | 221 | flash[:notice] = l(:notice_successful_delete) |
|
222 | 222 | else |
|
223 | 223 | flash[:error] = l(:notice_unable_delete_time_entry) |
|
224 | 224 | end |
|
225 | 225 | redirect_back_or_default project_time_entries_path(@projects.first) |
|
226 | 226 | } |
|
227 | 227 | format.api { |
|
228 | 228 | if destroyed |
|
229 | 229 | render_api_ok |
|
230 | 230 | else |
|
231 | 231 | render_validation_errors(@time_entries) |
|
232 | 232 | end |
|
233 | 233 | } |
|
234 | 234 | end |
|
235 | 235 | end |
|
236 | 236 | |
|
237 | 237 | private |
|
238 | 238 | def find_time_entry |
|
239 | 239 | @time_entry = TimeEntry.find(params[:id]) |
|
240 | 240 | unless @time_entry.editable_by?(User.current) |
|
241 | 241 | render_403 |
|
242 | 242 | return false |
|
243 | 243 | end |
|
244 | 244 | @project = @time_entry.project |
|
245 | 245 | rescue ActiveRecord::RecordNotFound |
|
246 | 246 | render_404 |
|
247 | 247 | end |
|
248 | 248 | |
|
249 | 249 | def find_time_entries |
|
250 | 250 | @time_entries = TimeEntry.find_all_by_id(params[:id] || params[:ids]) |
|
251 | 251 | raise ActiveRecord::RecordNotFound if @time_entries.empty? |
|
252 | 252 | @projects = @time_entries.collect(&:project).compact.uniq |
|
253 | 253 | @project = @projects.first if @projects.size == 1 |
|
254 | 254 | rescue ActiveRecord::RecordNotFound |
|
255 | 255 | render_404 |
|
256 | 256 | end |
|
257 | 257 | |
|
258 | 258 | def set_flash_from_bulk_time_entry_save(time_entries, unsaved_time_entry_ids) |
|
259 | 259 | if unsaved_time_entry_ids.empty? |
|
260 | 260 | flash[:notice] = l(:notice_successful_update) unless time_entries.empty? |
|
261 | 261 | else |
|
262 | 262 | flash[:error] = l(:notice_failed_to_save_time_entries, |
|
263 | 263 | :count => unsaved_time_entry_ids.size, |
|
264 | 264 | :total => time_entries.size, |
|
265 | 265 | :ids => '#' + unsaved_time_entry_ids.join(', #')) |
|
266 | 266 | end |
|
267 | 267 | end |
|
268 | 268 | |
|
269 | 269 | def find_optional_project_for_new_time_entry |
|
270 | 270 | if (project_id = (params[:project_id] || params[:time_entry] && params[:time_entry][:project_id])).present? |
|
271 | 271 | @project = Project.find(project_id) |
|
272 | 272 | end |
|
273 | 273 | if (issue_id = (params[:issue_id] || params[:time_entry] && params[:time_entry][:issue_id])).present? |
|
274 | 274 | @issue = Issue.find(issue_id) |
|
275 | 275 | @project ||= @issue.project |
|
276 | 276 | end |
|
277 | 277 | rescue ActiveRecord::RecordNotFound |
|
278 | 278 | render_404 |
|
279 | 279 | end |
|
280 | 280 | |
|
281 | 281 | def find_project_for_new_time_entry |
|
282 | 282 | find_optional_project_for_new_time_entry |
|
283 | 283 | if @project.nil? |
|
284 | 284 | render_404 |
|
285 | 285 | end |
|
286 | 286 | end |
|
287 | 287 | |
|
288 | 288 | def find_optional_project |
|
289 | 289 | if !params[:issue_id].blank? |
|
290 | 290 | @issue = Issue.find(params[:issue_id]) |
|
291 | 291 | @project = @issue.project |
|
292 | 292 | elsif !params[:project_id].blank? |
|
293 | 293 | @project = Project.find(params[:project_id]) |
|
294 | 294 | end |
|
295 | 295 | end |
|
296 | 296 | |
|
297 | 297 | # Returns the TimeEntry scope for index and report actions |
|
298 | 298 | def time_entry_scope |
|
299 | 299 | scope = TimeEntry.visible.where(@query.statement) |
|
300 | 300 | if @issue |
|
301 | 301 | scope = scope.on_issue(@issue) |
|
302 | 302 | elsif @project |
|
303 | 303 | scope = scope.on_project(@project, Setting.display_subprojects_issues?) |
|
304 | 304 | end |
|
305 | 305 | scope |
|
306 | 306 | end |
|
307 | 307 | |
|
308 | 308 | def parse_params_for_bulk_time_entry_attributes(params) |
|
309 | 309 | attributes = (params[:time_entry] || {}).reject {|k,v| v.blank?} |
|
310 | 310 | attributes.keys.each {|k| attributes[k] = '' if attributes[k] == 'none'} |
|
311 | 311 | attributes[:custom_field_values].reject! {|k,v| v.blank?} if attributes[:custom_field_values] |
|
312 | 312 | attributes |
|
313 | 313 | end |
|
314 | 314 | end |
@@ -1,393 +1,373 | |||
|
1 | 1 | # encoding: utf-8 |
|
2 | 2 | # |
|
3 | 3 | # Redmine - project management software |
|
4 | 4 | # Copyright (C) 2006-2013 Jean-Philippe Lang |
|
5 | 5 | # |
|
6 | 6 | # This program is free software; you can redistribute it and/or |
|
7 | 7 | # modify it under the terms of the GNU General Public License |
|
8 | 8 | # as published by the Free Software Foundation; either version 2 |
|
9 | 9 | # of the License, or (at your option) any later version. |
|
10 | 10 | # |
|
11 | 11 | # This program is distributed in the hope that it will be useful, |
|
12 | 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 | 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14 | 14 | # GNU General Public License for more details. |
|
15 | 15 | # |
|
16 | 16 | # You should have received a copy of the GNU General Public License |
|
17 | 17 | # along with this program; if not, write to the Free Software |
|
18 | 18 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
19 | 19 | |
|
20 | 20 | module IssuesHelper |
|
21 | 21 | include ApplicationHelper |
|
22 | 22 | |
|
23 | 23 | def issue_list(issues, &block) |
|
24 | 24 | ancestors = [] |
|
25 | 25 | issues.each do |issue| |
|
26 | 26 | while (ancestors.any? && !issue.is_descendant_of?(ancestors.last)) |
|
27 | 27 | ancestors.pop |
|
28 | 28 | end |
|
29 | 29 | yield issue, ancestors.size |
|
30 | 30 | ancestors << issue unless issue.leaf? |
|
31 | 31 | end |
|
32 | 32 | end |
|
33 | 33 | |
|
34 | 34 | # Renders a HTML/CSS tooltip |
|
35 | 35 | # |
|
36 | 36 | # To use, a trigger div is needed. This is a div with the class of "tooltip" |
|
37 | 37 | # that contains this method wrapped in a span with the class of "tip" |
|
38 | 38 | # |
|
39 | 39 | # <div class="tooltip"><%= link_to_issue(issue) %> |
|
40 | 40 | # <span class="tip"><%= render_issue_tooltip(issue) %></span> |
|
41 | 41 | # </div> |
|
42 | 42 | # |
|
43 | 43 | def render_issue_tooltip(issue) |
|
44 | 44 | @cached_label_status ||= l(:field_status) |
|
45 | 45 | @cached_label_start_date ||= l(:field_start_date) |
|
46 | 46 | @cached_label_due_date ||= l(:field_due_date) |
|
47 | 47 | @cached_label_assigned_to ||= l(:field_assigned_to) |
|
48 | 48 | @cached_label_priority ||= l(:field_priority) |
|
49 | 49 | @cached_label_project ||= l(:field_project) |
|
50 | 50 | |
|
51 | 51 | link_to_issue(issue) + "<br /><br />".html_safe + |
|
52 | 52 | "<strong>#{@cached_label_project}</strong>: #{link_to_project(issue.project)}<br />".html_safe + |
|
53 | 53 | "<strong>#{@cached_label_status}</strong>: #{h(issue.status.name)}<br />".html_safe + |
|
54 | 54 | "<strong>#{@cached_label_start_date}</strong>: #{format_date(issue.start_date)}<br />".html_safe + |
|
55 | 55 | "<strong>#{@cached_label_due_date}</strong>: #{format_date(issue.due_date)}<br />".html_safe + |
|
56 | 56 | "<strong>#{@cached_label_assigned_to}</strong>: #{h(issue.assigned_to)}<br />".html_safe + |
|
57 | 57 | "<strong>#{@cached_label_priority}</strong>: #{h(issue.priority.name)}".html_safe |
|
58 | 58 | end |
|
59 | 59 | |
|
60 | 60 | def issue_heading(issue) |
|
61 | 61 | h("#{issue.tracker} ##{issue.id}") |
|
62 | 62 | end |
|
63 | 63 | |
|
64 | 64 | def render_issue_subject_with_tree(issue) |
|
65 | 65 | s = '' |
|
66 | 66 | ancestors = issue.root? ? [] : issue.ancestors.visible.all |
|
67 | 67 | ancestors.each do |ancestor| |
|
68 | 68 | s << '<div>' + content_tag('p', link_to_issue(ancestor, :project => (issue.project_id != ancestor.project_id))) |
|
69 | 69 | end |
|
70 | 70 | s << '<div>' |
|
71 | 71 | subject = h(issue.subject) |
|
72 | 72 | if issue.is_private? |
|
73 | 73 | subject = content_tag('span', l(:field_is_private), :class => 'private') + ' ' + subject |
|
74 | 74 | end |
|
75 | 75 | s << content_tag('h3', subject) |
|
76 | 76 | s << '</div>' * (ancestors.size + 1) |
|
77 | 77 | s.html_safe |
|
78 | 78 | end |
|
79 | 79 | |
|
80 | 80 | def render_descendants_tree(issue) |
|
81 | 81 | s = '<form><table class="list issues">' |
|
82 | 82 | issue_list(issue.descendants.visible.sort_by(&:lft)) do |child, level| |
|
83 | 83 | css = "issue issue-#{child.id} hascontextmenu" |
|
84 | 84 | css << " idnt idnt-#{level}" if level > 0 |
|
85 | 85 | s << content_tag('tr', |
|
86 | 86 | content_tag('td', check_box_tag("ids[]", child.id, false, :id => nil), :class => 'checkbox') + |
|
87 | 87 | content_tag('td', link_to_issue(child, :truncate => 60, :project => (issue.project_id != child.project_id)), :class => 'subject') + |
|
88 | 88 | content_tag('td', h(child.status)) + |
|
89 | 89 | content_tag('td', link_to_user(child.assigned_to)) + |
|
90 | 90 | content_tag('td', progress_bar(child.done_ratio, :width => '80px')), |
|
91 | 91 | :class => css) |
|
92 | 92 | end |
|
93 | 93 | s << '</table></form>' |
|
94 | 94 | s.html_safe |
|
95 | 95 | end |
|
96 | 96 | |
|
97 | 97 | # Returns a link for adding a new subtask to the given issue |
|
98 | 98 | def link_to_new_subtask(issue) |
|
99 | 99 | attrs = { |
|
100 | 100 | :tracker_id => issue.tracker, |
|
101 | 101 | :parent_issue_id => issue |
|
102 | 102 | } |
|
103 | 103 | link_to(l(:button_add), new_project_issue_path(issue.project, :issue => attrs)) |
|
104 | 104 | end |
|
105 | 105 | |
|
106 | 106 | class IssueFieldsRows |
|
107 | 107 | include ActionView::Helpers::TagHelper |
|
108 | 108 | |
|
109 | 109 | def initialize |
|
110 | 110 | @left = [] |
|
111 | 111 | @right = [] |
|
112 | 112 | end |
|
113 | 113 | |
|
114 | 114 | def left(*args) |
|
115 | 115 | args.any? ? @left << cells(*args) : @left |
|
116 | 116 | end |
|
117 | 117 | |
|
118 | 118 | def right(*args) |
|
119 | 119 | args.any? ? @right << cells(*args) : @right |
|
120 | 120 | end |
|
121 | 121 | |
|
122 | 122 | def size |
|
123 | 123 | @left.size > @right.size ? @left.size : @right.size |
|
124 | 124 | end |
|
125 | 125 | |
|
126 | 126 | def to_html |
|
127 | 127 | html = ''.html_safe |
|
128 | 128 | blank = content_tag('th', '') + content_tag('td', '') |
|
129 | 129 | size.times do |i| |
|
130 | 130 | left = @left[i] || blank |
|
131 | 131 | right = @right[i] || blank |
|
132 | 132 | html << content_tag('tr', left + right) |
|
133 | 133 | end |
|
134 | 134 | html |
|
135 | 135 | end |
|
136 | 136 | |
|
137 | 137 | def cells(label, text, options={}) |
|
138 | 138 | content_tag('th', "#{label}:", options) + content_tag('td', text, options) |
|
139 | 139 | end |
|
140 | 140 | end |
|
141 | 141 | |
|
142 | 142 | def issue_fields_rows |
|
143 | 143 | r = IssueFieldsRows.new |
|
144 | 144 | yield r |
|
145 | 145 | r.to_html |
|
146 | 146 | end |
|
147 | 147 | |
|
148 | 148 | def render_custom_fields_rows(issue) |
|
149 | 149 | return if issue.custom_field_values.empty? |
|
150 | 150 | ordered_values = [] |
|
151 | 151 | half = (issue.custom_field_values.size / 2.0).ceil |
|
152 | 152 | half.times do |i| |
|
153 | 153 | ordered_values << issue.custom_field_values[i] |
|
154 | 154 | ordered_values << issue.custom_field_values[i + half] |
|
155 | 155 | end |
|
156 | 156 | s = "<tr>\n" |
|
157 | 157 | n = 0 |
|
158 | 158 | ordered_values.compact.each do |value| |
|
159 | 159 | s << "</tr>\n<tr>\n" if n > 0 && (n % 2) == 0 |
|
160 | 160 | s << "\t<th>#{ h(value.custom_field.name) }:</th><td>#{ simple_format_without_paragraph(h(show_value(value))) }</td>\n" |
|
161 | 161 | n += 1 |
|
162 | 162 | end |
|
163 | 163 | s << "</tr>\n" |
|
164 | 164 | s.html_safe |
|
165 | 165 | end |
|
166 | 166 | |
|
167 | 167 | def issues_destroy_confirmation_message(issues) |
|
168 | 168 | issues = [issues] unless issues.is_a?(Array) |
|
169 | 169 | message = l(:text_issues_destroy_confirmation) |
|
170 | 170 | descendant_count = issues.inject(0) {|memo, i| memo += (i.right - i.left - 1)/2} |
|
171 | 171 | if descendant_count > 0 |
|
172 | 172 | issues.each do |issue| |
|
173 | 173 | next if issue.root? |
|
174 | 174 | issues.each do |other_issue| |
|
175 | 175 | descendant_count -= 1 if issue.is_descendant_of?(other_issue) |
|
176 | 176 | end |
|
177 | 177 | end |
|
178 | 178 | if descendant_count > 0 |
|
179 | 179 | message << "\n" + l(:text_issues_destroy_descendants_confirmation, :count => descendant_count) |
|
180 | 180 | end |
|
181 | 181 | end |
|
182 | 182 | message |
|
183 | 183 | end |
|
184 | 184 | |
|
185 | 185 | def sidebar_queries |
|
186 | 186 | unless @sidebar_queries |
|
187 | 187 | @sidebar_queries = IssueQuery.visible.all( |
|
188 | 188 | :order => "#{Query.table_name}.name ASC", |
|
189 | 189 | # Project specific queries and global queries |
|
190 | 190 | :conditions => (@project.nil? ? ["project_id IS NULL"] : ["project_id IS NULL OR project_id = ?", @project.id]) |
|
191 | 191 | ) |
|
192 | 192 | end |
|
193 | 193 | @sidebar_queries |
|
194 | 194 | end |
|
195 | 195 | |
|
196 | 196 | def query_links(title, queries) |
|
197 | 197 | # links to #index on issues/show |
|
198 | 198 | url_params = controller_name == 'issues' ? {:controller => 'issues', :action => 'index', :project_id => @project} : params |
|
199 | 199 | |
|
200 | 200 | content_tag('h3', h(title)) + |
|
201 | 201 | queries.collect {|query| |
|
202 | 202 | css = 'query' |
|
203 | 203 | css << ' selected' if query == @query |
|
204 | 204 | link_to(h(query.name), url_params.merge(:query_id => query), :class => css) |
|
205 | 205 | }.join('<br />').html_safe |
|
206 | 206 | end |
|
207 | 207 | |
|
208 | 208 | def render_sidebar_queries |
|
209 | 209 | out = ''.html_safe |
|
210 | 210 | queries = sidebar_queries.select {|q| !q.is_public?} |
|
211 | 211 | out << query_links(l(:label_my_queries), queries) if queries.any? |
|
212 | 212 | queries = sidebar_queries.select {|q| q.is_public?} |
|
213 | 213 | out << query_links(l(:label_query_plural), queries) if queries.any? |
|
214 | 214 | out |
|
215 | 215 | end |
|
216 | 216 | |
|
217 | 217 | # Returns the textual representation of a journal details |
|
218 | 218 | # as an array of strings |
|
219 | 219 | def details_to_strings(details, no_html=false, options={}) |
|
220 | 220 | options[:only_path] = (options[:only_path] == false ? false : true) |
|
221 | 221 | strings = [] |
|
222 | 222 | values_by_field = {} |
|
223 | 223 | details.each do |detail| |
|
224 | 224 | if detail.property == 'cf' |
|
225 | 225 | field_id = detail.prop_key |
|
226 | 226 | field = CustomField.find_by_id(field_id) |
|
227 | 227 | if field && field.multiple? |
|
228 | 228 | values_by_field[field_id] ||= {:added => [], :deleted => []} |
|
229 | 229 | if detail.old_value |
|
230 | 230 | values_by_field[field_id][:deleted] << detail.old_value |
|
231 | 231 | end |
|
232 | 232 | if detail.value |
|
233 | 233 | values_by_field[field_id][:added] << detail.value |
|
234 | 234 | end |
|
235 | 235 | next |
|
236 | 236 | end |
|
237 | 237 | end |
|
238 | 238 | strings << show_detail(detail, no_html, options) |
|
239 | 239 | end |
|
240 | 240 | values_by_field.each do |field_id, changes| |
|
241 | 241 | detail = JournalDetail.new(:property => 'cf', :prop_key => field_id) |
|
242 | 242 | if changes[:added].any? |
|
243 | 243 | detail.value = changes[:added] |
|
244 | 244 | strings << show_detail(detail, no_html, options) |
|
245 | 245 | elsif changes[:deleted].any? |
|
246 | 246 | detail.old_value = changes[:deleted] |
|
247 | 247 | strings << show_detail(detail, no_html, options) |
|
248 | 248 | end |
|
249 | 249 | end |
|
250 | 250 | strings |
|
251 | 251 | end |
|
252 | 252 | |
|
253 | 253 | # Returns the textual representation of a single journal detail |
|
254 | 254 | def show_detail(detail, no_html=false, options={}) |
|
255 | 255 | multiple = false |
|
256 | 256 | case detail.property |
|
257 | 257 | when 'attr' |
|
258 | 258 | field = detail.prop_key.to_s.gsub(/\_id$/, "") |
|
259 | 259 | label = l(("field_" + field).to_sym) |
|
260 | 260 | case detail.prop_key |
|
261 | 261 | when 'due_date', 'start_date' |
|
262 | 262 | value = format_date(detail.value.to_date) if detail.value |
|
263 | 263 | old_value = format_date(detail.old_value.to_date) if detail.old_value |
|
264 | 264 | |
|
265 | 265 | when 'project_id', 'status_id', 'tracker_id', 'assigned_to_id', |
|
266 | 266 | 'priority_id', 'category_id', 'fixed_version_id' |
|
267 | 267 | value = find_name_by_reflection(field, detail.value) |
|
268 | 268 | old_value = find_name_by_reflection(field, detail.old_value) |
|
269 | 269 | |
|
270 | 270 | when 'estimated_hours' |
|
271 | 271 | value = "%0.02f" % detail.value.to_f unless detail.value.blank? |
|
272 | 272 | old_value = "%0.02f" % detail.old_value.to_f unless detail.old_value.blank? |
|
273 | 273 | |
|
274 | 274 | when 'parent_id' |
|
275 | 275 | label = l(:field_parent_issue) |
|
276 | 276 | value = "##{detail.value}" unless detail.value.blank? |
|
277 | 277 | old_value = "##{detail.old_value}" unless detail.old_value.blank? |
|
278 | 278 | |
|
279 | 279 | when 'is_private' |
|
280 | 280 | value = l(detail.value == "0" ? :general_text_No : :general_text_Yes) unless detail.value.blank? |
|
281 | 281 | old_value = l(detail.old_value == "0" ? :general_text_No : :general_text_Yes) unless detail.old_value.blank? |
|
282 | 282 | end |
|
283 | 283 | when 'cf' |
|
284 | 284 | custom_field = CustomField.find_by_id(detail.prop_key) |
|
285 | 285 | if custom_field |
|
286 | 286 | multiple = custom_field.multiple? |
|
287 | 287 | label = custom_field.name |
|
288 | 288 | value = format_value(detail.value, custom_field.field_format) if detail.value |
|
289 | 289 | old_value = format_value(detail.old_value, custom_field.field_format) if detail.old_value |
|
290 | 290 | end |
|
291 | 291 | when 'attachment' |
|
292 | 292 | label = l(:label_attachment) |
|
293 | 293 | end |
|
294 | 294 | call_hook(:helper_issues_show_detail_after_setting, |
|
295 | 295 | {:detail => detail, :label => label, :value => value, :old_value => old_value }) |
|
296 | 296 | |
|
297 | 297 | label ||= detail.prop_key |
|
298 | 298 | value ||= detail.value |
|
299 | 299 | old_value ||= detail.old_value |
|
300 | 300 | |
|
301 | 301 | unless no_html |
|
302 | 302 | label = content_tag('strong', label) |
|
303 | 303 | old_value = content_tag("i", h(old_value)) if detail.old_value |
|
304 | 304 | old_value = content_tag("del", old_value) if detail.old_value and detail.value.blank? |
|
305 | 305 | if detail.property == 'attachment' && !value.blank? && atta = Attachment.find_by_id(detail.prop_key) |
|
306 | 306 | # Link to the attachment if it has not been removed |
|
307 | 307 | value = link_to_attachment(atta, :download => true, :only_path => options[:only_path]) |
|
308 | 308 | if options[:only_path] != false && atta.is_text? |
|
309 | 309 | value += link_to( |
|
310 | 310 | image_tag('magnifier.png'), |
|
311 | 311 | :controller => 'attachments', :action => 'show', |
|
312 | 312 | :id => atta, :filename => atta.filename |
|
313 | 313 | ) |
|
314 | 314 | end |
|
315 | 315 | else |
|
316 | 316 | value = content_tag("i", h(value)) if value |
|
317 | 317 | end |
|
318 | 318 | end |
|
319 | 319 | |
|
320 | 320 | if detail.property == 'attr' && detail.prop_key == 'description' |
|
321 | 321 | s = l(:text_journal_changed_no_detail, :label => label) |
|
322 | 322 | unless no_html |
|
323 | 323 | diff_link = link_to 'diff', |
|
324 | 324 | {:controller => 'journals', :action => 'diff', :id => detail.journal_id, |
|
325 | 325 | :detail_id => detail.id, :only_path => options[:only_path]}, |
|
326 | 326 | :title => l(:label_view_diff) |
|
327 | 327 | s << " (#{ diff_link })" |
|
328 | 328 | end |
|
329 | 329 | s.html_safe |
|
330 | 330 | elsif detail.value.present? |
|
331 | 331 | case detail.property |
|
332 | 332 | when 'attr', 'cf' |
|
333 | 333 | if detail.old_value.present? |
|
334 | 334 | l(:text_journal_changed, :label => label, :old => old_value, :new => value).html_safe |
|
335 | 335 | elsif multiple |
|
336 | 336 | l(:text_journal_added, :label => label, :value => value).html_safe |
|
337 | 337 | else |
|
338 | 338 | l(:text_journal_set_to, :label => label, :value => value).html_safe |
|
339 | 339 | end |
|
340 | 340 | when 'attachment' |
|
341 | 341 | l(:text_journal_added, :label => label, :value => value).html_safe |
|
342 | 342 | end |
|
343 | 343 | else |
|
344 | 344 | l(:text_journal_deleted, :label => label, :old => old_value).html_safe |
|
345 | 345 | end |
|
346 | 346 | end |
|
347 | 347 | |
|
348 | 348 | # Find the name of an associated record stored in the field attribute |
|
349 | 349 | def find_name_by_reflection(field, id) |
|
350 | 350 | unless id.present? |
|
351 | 351 | return nil |
|
352 | 352 | end |
|
353 | 353 | association = Issue.reflect_on_association(field.to_sym) |
|
354 | 354 | if association |
|
355 | 355 | record = association.class_name.constantize.find_by_id(id) |
|
356 | 356 | return record.name if record |
|
357 | 357 | end |
|
358 | 358 | end |
|
359 | 359 | |
|
360 | 360 | # Renders issue children recursively |
|
361 | 361 | def render_api_issue_children(issue, api) |
|
362 | 362 | return if issue.leaf? |
|
363 | 363 | api.array :children do |
|
364 | 364 | issue.children.each do |child| |
|
365 | 365 | api.issue(:id => child.id) do |
|
366 | 366 | api.tracker(:id => child.tracker_id, :name => child.tracker.name) unless child.tracker.nil? |
|
367 | 367 | api.subject child.subject |
|
368 | 368 | render_api_issue_children(child, api) |
|
369 | 369 | end |
|
370 | 370 | end |
|
371 | 371 | end |
|
372 | 372 | end |
|
373 | ||
|
374 | def issues_to_csv(issues, project, query, options={}) | |
|
375 | encoding = l(:general_csv_encoding) | |
|
376 | columns = (options[:columns] == 'all' ? query.available_inline_columns : query.inline_columns) | |
|
377 | if options[:description] | |
|
378 | if description = query.available_columns.detect {|q| q.name == :description} | |
|
379 | columns << description | |
|
380 | end | |
|
381 | end | |
|
382 | ||
|
383 | export = FCSV.generate(:col_sep => l(:general_csv_separator)) do |csv| | |
|
384 | # csv header fields | |
|
385 | csv << columns.collect {|c| Redmine::CodesetUtil.from_utf8(c.caption.to_s, encoding) } | |
|
386 | # csv lines | |
|
387 | issues.each do |issue| | |
|
388 | csv << columns.collect {|c| Redmine::CodesetUtil.from_utf8(csv_content(c, issue), encoding) } | |
|
389 | end | |
|
390 | end | |
|
391 | export | |
|
392 | end | |
|
393 | 373 | end |
@@ -1,166 +1,186 | |||
|
1 | 1 | # encoding: utf-8 |
|
2 | 2 | # |
|
3 | 3 | # Redmine - project management software |
|
4 | 4 | # Copyright (C) 2006-2013 Jean-Philippe Lang |
|
5 | 5 | # |
|
6 | 6 | # This program is free software; you can redistribute it and/or |
|
7 | 7 | # modify it under the terms of the GNU General Public License |
|
8 | 8 | # as published by the Free Software Foundation; either version 2 |
|
9 | 9 | # of the License, or (at your option) any later version. |
|
10 | 10 | # |
|
11 | 11 | # This program is distributed in the hope that it will be useful, |
|
12 | 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 | 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14 | 14 | # GNU General Public License for more details. |
|
15 | 15 | # |
|
16 | 16 | # You should have received a copy of the GNU General Public License |
|
17 | 17 | # along with this program; if not, write to the Free Software |
|
18 | 18 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
19 | 19 | |
|
20 | 20 | module QueriesHelper |
|
21 | 21 | def filters_options_for_select(query) |
|
22 | 22 | options_for_select(filters_options(query)) |
|
23 | 23 | end |
|
24 | 24 | |
|
25 | 25 | def filters_options(query) |
|
26 | 26 | options = [[]] |
|
27 | 27 | options += query.available_filters.map do |field, field_options| |
|
28 | 28 | [field_options[:name], field] |
|
29 | 29 | end |
|
30 | 30 | end |
|
31 | 31 | |
|
32 | 32 | def available_block_columns_tags(query) |
|
33 | 33 | tags = ''.html_safe |
|
34 | 34 | query.available_block_columns.each do |column| |
|
35 | 35 | tags << content_tag('label', check_box_tag('c[]', column.name.to_s, query.has_column?(column)) + " #{column.caption}", :class => 'inline') |
|
36 | 36 | end |
|
37 | 37 | tags |
|
38 | 38 | end |
|
39 | 39 | |
|
40 | 40 | def column_header(column) |
|
41 | 41 | column.sortable ? sort_header_tag(column.name.to_s, :caption => column.caption, |
|
42 | 42 | :default_order => column.default_order) : |
|
43 | 43 | content_tag('th', h(column.caption)) |
|
44 | 44 | end |
|
45 | 45 | |
|
46 | 46 | def column_content(column, issue) |
|
47 | 47 | value = column.value(issue) |
|
48 | 48 | if value.is_a?(Array) |
|
49 | 49 | value.collect {|v| column_value(column, issue, v)}.compact.join(', ').html_safe |
|
50 | 50 | else |
|
51 | 51 | column_value(column, issue, value) |
|
52 | 52 | end |
|
53 | 53 | end |
|
54 | 54 | |
|
55 | 55 | def column_value(column, issue, value) |
|
56 | 56 | case value.class.name |
|
57 | 57 | when 'String' |
|
58 | 58 | if column.name == :subject |
|
59 | 59 | link_to(h(value), :controller => 'issues', :action => 'show', :id => issue) |
|
60 | 60 | elsif column.name == :description |
|
61 | 61 | issue.description? ? content_tag('div', textilizable(issue, :description), :class => "wiki") : '' |
|
62 | 62 | else |
|
63 | 63 | h(value) |
|
64 | 64 | end |
|
65 | 65 | when 'Time' |
|
66 | 66 | format_time(value) |
|
67 | 67 | when 'Date' |
|
68 | 68 | format_date(value) |
|
69 | 69 | when 'Fixnum' |
|
70 | 70 | if column.name == :id |
|
71 | 71 | link_to value, issue_path(issue) |
|
72 | 72 | elsif column.name == :done_ratio |
|
73 | 73 | progress_bar(value, :width => '80px') |
|
74 | 74 | else |
|
75 | 75 | value.to_s |
|
76 | 76 | end |
|
77 | 77 | when 'Float' |
|
78 | 78 | sprintf "%.2f", value |
|
79 | 79 | when 'User' |
|
80 | 80 | link_to_user value |
|
81 | 81 | when 'Project' |
|
82 | 82 | link_to_project value |
|
83 | 83 | when 'Version' |
|
84 | 84 | link_to(h(value), :controller => 'versions', :action => 'show', :id => value) |
|
85 | 85 | when 'TrueClass' |
|
86 | 86 | l(:general_text_Yes) |
|
87 | 87 | when 'FalseClass' |
|
88 | 88 | l(:general_text_No) |
|
89 | 89 | when 'Issue' |
|
90 | 90 | value.visible? ? link_to_issue(value) : "##{value.id}" |
|
91 | 91 | when 'IssueRelation' |
|
92 | 92 | other = value.other_issue(issue) |
|
93 | 93 | content_tag('span', |
|
94 | 94 | (l(value.label_for(issue)) + " " + link_to_issue(other, :subject => false, :tracker => false)).html_safe, |
|
95 | 95 | :class => value.css_classes_for(issue)) |
|
96 | 96 | else |
|
97 | 97 | h(value) |
|
98 | 98 | end |
|
99 | 99 | end |
|
100 | 100 | |
|
101 | 101 | def csv_content(column, issue) |
|
102 | 102 | value = column.value(issue) |
|
103 | 103 | if value.is_a?(Array) |
|
104 | 104 | value.collect {|v| csv_value(column, issue, v)}.compact.join(', ') |
|
105 | 105 | else |
|
106 | 106 | csv_value(column, issue, value) |
|
107 | 107 | end |
|
108 | 108 | end |
|
109 | 109 | |
|
110 | 110 | def csv_value(column, issue, value) |
|
111 | 111 | case value.class.name |
|
112 | 112 | when 'Time' |
|
113 | 113 | format_time(value) |
|
114 | 114 | when 'Date' |
|
115 | 115 | format_date(value) |
|
116 | 116 | when 'Float' |
|
117 | 117 | sprintf("%.2f", value).gsub('.', l(:general_csv_decimal_separator)) |
|
118 | 118 | when 'IssueRelation' |
|
119 | 119 | other = value.other_issue(issue) |
|
120 | 120 | l(value.label_for(issue)) + " ##{other.id}" |
|
121 | 121 | else |
|
122 | 122 | value.to_s |
|
123 | 123 | end |
|
124 | 124 | end |
|
125 | 125 | |
|
126 | def query_to_csv(items, query, options={}) | |
|
127 | encoding = l(:general_csv_encoding) | |
|
128 | columns = (options[:columns] == 'all' ? query.available_inline_columns : query.inline_columns) | |
|
129 | query.available_block_columns.each do |column| | |
|
130 | if options[column.name].present? | |
|
131 | columns << column | |
|
132 | end | |
|
133 | end | |
|
134 | ||
|
135 | export = FCSV.generate(:col_sep => l(:general_csv_separator)) do |csv| | |
|
136 | # csv header fields | |
|
137 | csv << columns.collect {|c| Redmine::CodesetUtil.from_utf8(c.caption.to_s, encoding) } | |
|
138 | # csv lines | |
|
139 | items.each do |item| | |
|
140 | csv << columns.collect {|c| Redmine::CodesetUtil.from_utf8(csv_content(c, item), encoding) } | |
|
141 | end | |
|
142 | end | |
|
143 | export | |
|
144 | end | |
|
145 | ||
|
126 | 146 | # Retrieve query from session or build a new query |
|
127 | 147 | def retrieve_query |
|
128 | 148 | if !params[:query_id].blank? |
|
129 | 149 | cond = "project_id IS NULL" |
|
130 | 150 | cond << " OR project_id = #{@project.id}" if @project |
|
131 | 151 | @query = IssueQuery.find(params[:query_id], :conditions => cond) |
|
132 | 152 | raise ::Unauthorized unless @query.visible? |
|
133 | 153 | @query.project = @project |
|
134 | 154 | session[:query] = {:id => @query.id, :project_id => @query.project_id} |
|
135 | 155 | sort_clear |
|
136 | 156 | elsif api_request? || params[:set_filter] || session[:query].nil? || session[:query][:project_id] != (@project ? @project.id : nil) |
|
137 | 157 | # Give it a name, required to be valid |
|
138 | 158 | @query = IssueQuery.new(:name => "_") |
|
139 | 159 | @query.project = @project |
|
140 | 160 | @query.build_from_params(params) |
|
141 | 161 | session[:query] = {:project_id => @query.project_id, :filters => @query.filters, :group_by => @query.group_by, :column_names => @query.column_names} |
|
142 | 162 | else |
|
143 | 163 | # retrieve from session |
|
144 | 164 | @query = IssueQuery.find_by_id(session[:query][:id]) if session[:query][:id] |
|
145 | 165 | @query ||= IssueQuery.new(:name => "_", :filters => session[:query][:filters], :group_by => session[:query][:group_by], :column_names => session[:query][:column_names]) |
|
146 | 166 | @query.project = @project |
|
147 | 167 | end |
|
148 | 168 | end |
|
149 | 169 | |
|
150 | 170 | def retrieve_query_from_session |
|
151 | 171 | if session[:query] |
|
152 | 172 | if session[:query][:id] |
|
153 | 173 | @query = IssueQuery.find_by_id(session[:query][:id]) |
|
154 | 174 | return unless @query |
|
155 | 175 | else |
|
156 | 176 | @query = IssueQuery.new(:name => "_", :filters => session[:query][:filters], :group_by => session[:query][:group_by], :column_names => session[:query][:column_names]) |
|
157 | 177 | end |
|
158 | 178 | if session[:query].has_key?(:project_id) |
|
159 | 179 | @query.project_id = session[:query][:project_id] |
|
160 | 180 | else |
|
161 | 181 | @query.project = @project |
|
162 | 182 | end |
|
163 | 183 | @query |
|
164 | 184 | end |
|
165 | 185 | end |
|
166 | 186 | end |
@@ -1,169 +1,154 | |||
|
1 | 1 | # encoding: utf-8 |
|
2 | 2 | # |
|
3 | 3 | # Redmine - project management software |
|
4 | 4 | # Copyright (C) 2006-2013 Jean-Philippe Lang |
|
5 | 5 | # |
|
6 | 6 | # This program is free software; you can redistribute it and/or |
|
7 | 7 | # modify it under the terms of the GNU General Public License |
|
8 | 8 | # as published by the Free Software Foundation; either version 2 |
|
9 | 9 | # of the License, or (at your option) any later version. |
|
10 | 10 | # |
|
11 | 11 | # This program is distributed in the hope that it will be useful, |
|
12 | 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 | 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14 | 14 | # GNU General Public License for more details. |
|
15 | 15 | # |
|
16 | 16 | # You should have received a copy of the GNU General Public License |
|
17 | 17 | # along with this program; if not, write to the Free Software |
|
18 | 18 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
19 | 19 | |
|
20 | 20 | module TimelogHelper |
|
21 | 21 | include ApplicationHelper |
|
22 | 22 | |
|
23 | 23 | def render_timelog_breadcrumb |
|
24 | 24 | links = [] |
|
25 | 25 | links << link_to(l(:label_project_all), {:project_id => nil, :issue_id => nil}) |
|
26 | 26 | links << link_to(h(@project), {:project_id => @project, :issue_id => nil}) if @project |
|
27 | 27 | if @issue |
|
28 | 28 | if @issue.visible? |
|
29 | 29 | links << link_to_issue(@issue, :subject => false) |
|
30 | 30 | else |
|
31 | 31 | links << "##{@issue.id}" |
|
32 | 32 | end |
|
33 | 33 | end |
|
34 | 34 | breadcrumb links |
|
35 | 35 | end |
|
36 | 36 | |
|
37 | 37 | # Returns a collection of activities for a select field. time_entry |
|
38 | 38 | # is optional and will be used to check if the selected TimeEntryActivity |
|
39 | 39 | # is active. |
|
40 | 40 | def activity_collection_for_select_options(time_entry=nil, project=nil) |
|
41 | 41 | project ||= @project |
|
42 | 42 | if project.nil? |
|
43 | 43 | activities = TimeEntryActivity.shared.active |
|
44 | 44 | else |
|
45 | 45 | activities = project.activities |
|
46 | 46 | end |
|
47 | 47 | |
|
48 | 48 | collection = [] |
|
49 | 49 | if time_entry && time_entry.activity && !time_entry.activity.active? |
|
50 | 50 | collection << [ "--- #{l(:actionview_instancetag_blank_option)} ---", '' ] |
|
51 | 51 | else |
|
52 | 52 | collection << [ "--- #{l(:actionview_instancetag_blank_option)} ---", '' ] unless activities.detect(&:is_default) |
|
53 | 53 | end |
|
54 | 54 | activities.each { |a| collection << [a.name, a.id] } |
|
55 | 55 | collection |
|
56 | 56 | end |
|
57 | 57 | |
|
58 | 58 | def select_hours(data, criteria, value) |
|
59 | 59 | if value.to_s.empty? |
|
60 | 60 | data.select {|row| row[criteria].blank? } |
|
61 | 61 | else |
|
62 | 62 | data.select {|row| row[criteria].to_s == value.to_s} |
|
63 | 63 | end |
|
64 | 64 | end |
|
65 | 65 | |
|
66 | 66 | def sum_hours(data) |
|
67 | 67 | sum = 0 |
|
68 | 68 | data.each do |row| |
|
69 | 69 | sum += row['hours'].to_f |
|
70 | 70 | end |
|
71 | 71 | sum |
|
72 | 72 | end |
|
73 | 73 | |
|
74 | 74 | def options_for_period_select(value) |
|
75 | 75 | options_for_select([[l(:label_all_time), 'all'], |
|
76 | 76 | [l(:label_today), 'today'], |
|
77 | 77 | [l(:label_yesterday), 'yesterday'], |
|
78 | 78 | [l(:label_this_week), 'current_week'], |
|
79 | 79 | [l(:label_last_week), 'last_week'], |
|
80 | 80 | [l(:label_last_n_weeks, 2), 'last_2_weeks'], |
|
81 | 81 | [l(:label_last_n_days, 7), '7_days'], |
|
82 | 82 | [l(:label_this_month), 'current_month'], |
|
83 | 83 | [l(:label_last_month), 'last_month'], |
|
84 | 84 | [l(:label_last_n_days, 30), '30_days'], |
|
85 | 85 | [l(:label_this_year), 'current_year']], |
|
86 | 86 | value) |
|
87 | 87 | end |
|
88 | 88 | |
|
89 | def entries_to_csv(entries, query, options={}) | |
|
90 | encoding = l(:general_csv_encoding) | |
|
91 | columns = (options[:columns] == 'all' ? query.available_inline_columns : query.inline_columns) | |
|
92 | ||
|
93 | export = FCSV.generate(:col_sep => l(:general_csv_separator)) do |csv| | |
|
94 | # csv header fields | |
|
95 | csv << columns.collect {|c| Redmine::CodesetUtil.from_utf8(c.caption.to_s, encoding) } | |
|
96 | # csv lines | |
|
97 | entries.each do |entry| | |
|
98 | csv << columns.collect {|c| Redmine::CodesetUtil.from_utf8(csv_content(c, entry), encoding) } | |
|
99 | end | |
|
100 | end | |
|
101 | export | |
|
102 | end | |
|
103 | ||
|
104 | 89 | def format_criteria_value(criteria_options, value) |
|
105 | 90 | if value.blank? |
|
106 | 91 | "[#{l(:label_none)}]" |
|
107 | 92 | elsif k = criteria_options[:klass] |
|
108 | 93 | obj = k.find_by_id(value.to_i) |
|
109 | 94 | if obj.is_a?(Issue) |
|
110 | 95 | obj.visible? ? "#{obj.tracker} ##{obj.id}: #{obj.subject}" : "##{obj.id}" |
|
111 | 96 | else |
|
112 | 97 | obj |
|
113 | 98 | end |
|
114 | 99 | else |
|
115 | 100 | format_value(value, criteria_options[:format]) |
|
116 | 101 | end |
|
117 | 102 | end |
|
118 | 103 | |
|
119 | 104 | def report_to_csv(report) |
|
120 | 105 | decimal_separator = l(:general_csv_decimal_separator) |
|
121 | 106 | export = FCSV.generate(:col_sep => l(:general_csv_separator)) do |csv| |
|
122 | 107 | # Column headers |
|
123 | 108 | headers = report.criteria.collect {|criteria| l(report.available_criteria[criteria][:label]) } |
|
124 | 109 | headers += report.periods |
|
125 | 110 | headers << l(:label_total) |
|
126 | 111 | csv << headers.collect {|c| Redmine::CodesetUtil.from_utf8( |
|
127 | 112 | c.to_s, |
|
128 | 113 | l(:general_csv_encoding) ) } |
|
129 | 114 | # Content |
|
130 | 115 | report_criteria_to_csv(csv, report.available_criteria, report.columns, report.criteria, report.periods, report.hours) |
|
131 | 116 | # Total row |
|
132 | 117 | str_total = Redmine::CodesetUtil.from_utf8(l(:label_total), l(:general_csv_encoding)) |
|
133 | 118 | row = [ str_total ] + [''] * (report.criteria.size - 1) |
|
134 | 119 | total = 0 |
|
135 | 120 | report.periods.each do |period| |
|
136 | 121 | sum = sum_hours(select_hours(report.hours, report.columns, period.to_s)) |
|
137 | 122 | total += sum |
|
138 | 123 | row << (sum > 0 ? ("%.2f" % sum).gsub('.',decimal_separator) : '') |
|
139 | 124 | end |
|
140 | 125 | row << ("%.2f" % total).gsub('.',decimal_separator) |
|
141 | 126 | csv << row |
|
142 | 127 | end |
|
143 | 128 | export |
|
144 | 129 | end |
|
145 | 130 | |
|
146 | 131 | def report_criteria_to_csv(csv, available_criteria, columns, criteria, periods, hours, level=0) |
|
147 | 132 | decimal_separator = l(:general_csv_decimal_separator) |
|
148 | 133 | hours.collect {|h| h[criteria[level]].to_s}.uniq.each do |value| |
|
149 | 134 | hours_for_value = select_hours(hours, criteria[level], value) |
|
150 | 135 | next if hours_for_value.empty? |
|
151 | 136 | row = [''] * level |
|
152 | 137 | row << Redmine::CodesetUtil.from_utf8( |
|
153 | 138 | format_criteria_value(available_criteria[criteria[level]], value).to_s, |
|
154 | 139 | l(:general_csv_encoding) ) |
|
155 | 140 | row += [''] * (criteria.length - level - 1) |
|
156 | 141 | total = 0 |
|
157 | 142 | periods.each do |period| |
|
158 | 143 | sum = sum_hours(select_hours(hours_for_value, columns, period.to_s)) |
|
159 | 144 | total += sum |
|
160 | 145 | row << (sum > 0 ? ("%.2f" % sum).gsub('.',decimal_separator) : '') |
|
161 | 146 | end |
|
162 | 147 | row << ("%.2f" % total).gsub('.',decimal_separator) |
|
163 | 148 | csv << row |
|
164 | 149 | if criteria.length > level + 1 |
|
165 | 150 | report_criteria_to_csv(csv, available_criteria, columns, criteria, periods, hours_for_value, level + 1) |
|
166 | 151 | end |
|
167 | 152 | end |
|
168 | 153 | end |
|
169 | 154 | end |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
General Comments 0
You need to be logged in to leave comments.
Login now