You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

issues_helper.rb 26KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2021 Jean-Philippe Lang
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; either version 2
  8. # of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. module IssuesHelper
  19. include ApplicationHelper
  20. include Redmine::Export::PDF::IssuesPdfHelper
  21. include IssueStatusesHelper
  22. def issue_list(issues, &block)
  23. ancestors = []
  24. issues.each do |issue|
  25. while ancestors.any? &&
  26. !issue.is_descendant_of?(ancestors.last)
  27. ancestors.pop
  28. end
  29. yield issue, ancestors.size
  30. ancestors << issue unless issue.leaf?
  31. end
  32. end
  33. def grouped_issue_list(issues, query, &block)
  34. ancestors = []
  35. grouped_query_results(issues, query) do |issue, group_name, group_count, group_totals|
  36. while ancestors.any? &&
  37. !issue.is_descendant_of?(ancestors.last)
  38. ancestors.pop
  39. end
  40. yield issue, ancestors.size, group_name, group_count, group_totals
  41. ancestors << issue unless issue.leaf?
  42. end
  43. end
  44. # Renders a HTML/CSS tooltip
  45. #
  46. # To use, a trigger div is needed. This is a div with the class of "tooltip"
  47. # that contains this method wrapped in a span with the class of "tip"
  48. #
  49. # <div class="tooltip"><%= link_to_issue(issue) %>
  50. # <span class="tip"><%= render_issue_tooltip(issue) %></span>
  51. # </div>
  52. #
  53. def render_issue_tooltip(issue)
  54. @cached_label_status ||= l(:field_status)
  55. @cached_label_start_date ||= l(:field_start_date)
  56. @cached_label_due_date ||= l(:field_due_date)
  57. @cached_label_assigned_to ||= l(:field_assigned_to)
  58. @cached_label_priority ||= l(:field_priority)
  59. @cached_label_project ||= l(:field_project)
  60. link_to_issue(issue) + "<br /><br />".html_safe +
  61. "<strong>#{@cached_label_project}</strong>: #{link_to_project(issue.project)}<br />".html_safe +
  62. "<strong>#{@cached_label_status}</strong>: #{h(issue.status.name) + (" (#{format_date(issue.closed_on)})" if issue.closed?)}<br />".html_safe +
  63. "<strong>#{@cached_label_start_date}</strong>: #{format_date(issue.start_date)}<br />".html_safe +
  64. "<strong>#{@cached_label_due_date}</strong>: #{format_date(issue.due_date)}<br />".html_safe +
  65. "<strong>#{@cached_label_assigned_to}</strong>: #{avatar(issue.assigned_to, :size => '13', :title => l(:field_assigned_to)) if issue.assigned_to} #{h(issue.assigned_to)}<br />".html_safe +
  66. "<strong>#{@cached_label_priority}</strong>: #{h(issue.priority.name)}".html_safe
  67. end
  68. def issue_heading(issue)
  69. h("#{issue.tracker} ##{issue.id}")
  70. end
  71. def render_issue_subject_with_tree(issue)
  72. s = +''
  73. ancestors = issue.root? ? [] : issue.ancestors.visible.to_a
  74. ancestors.each do |ancestor|
  75. s << '<div>' + content_tag('p', link_to_issue(ancestor, :project => (issue.project_id != ancestor.project_id)))
  76. end
  77. s << '<div>'
  78. subject = h(issue.subject)
  79. s << content_tag('h3', subject)
  80. s << '</div>' * (ancestors.size + 1)
  81. s.html_safe
  82. end
  83. def render_descendants_tree(issue)
  84. manage_relations = User.current.allowed_to?(:manage_subtasks, issue.project)
  85. s = +'<table class="list issues odd-even">'
  86. issue_list(
  87. issue.descendants.visible.
  88. preload(:status, :priority, :tracker,
  89. :assigned_to).sort_by(&:lft)) do |child, level|
  90. css = +"issue issue-#{child.id} hascontextmenu #{child.css_classes}"
  91. css << " idnt idnt-#{level}" if level > 0
  92. buttons =
  93. if manage_relations
  94. link_to(
  95. l(:label_delete_link_to_subtask),
  96. issue_path(
  97. {:id => child.id, :issue => {:parent_issue_id => ''},
  98. :back_url => issue_path(issue.id), :no_flash => '1'}
  99. ),
  100. :method => :put,
  101. :data => {:confirm => l(:text_are_you_sure)},
  102. :title => l(:label_delete_link_to_subtask),
  103. :class => 'icon-only icon-link-break'
  104. )
  105. else
  106. "".html_safe
  107. end
  108. buttons << link_to_context_menu
  109. s <<
  110. content_tag(
  111. 'tr',
  112. content_tag('td', check_box_tag("ids[]", child.id, false, :id => nil),
  113. :class => 'checkbox') +
  114. content_tag('td',
  115. link_to_issue(
  116. child,
  117. :project => (issue.project_id != child.project_id)),
  118. :class => 'subject') +
  119. content_tag('td', h(child.status), :class => 'status') +
  120. content_tag('td', link_to_user(child.assigned_to), :class => 'assigned_to') +
  121. content_tag('td', format_date(child.start_date), :class => 'start_date') +
  122. content_tag('td', format_date(child.due_date), :class => 'due_date') +
  123. content_tag('td',
  124. (if child.disabled_core_fields.include?('done_ratio')
  125. ''
  126. else
  127. progress_bar(child.done_ratio)
  128. end),
  129. :class=> 'done_ratio') +
  130. content_tag('td', buttons, :class => 'buttons'),
  131. :class => css)
  132. end
  133. s << '</table>'
  134. s.html_safe
  135. end
  136. # Renders descendants stats (total descendants (open - closed)) with query links
  137. def render_descendants_stats(issue)
  138. # Get issue descendants grouped by status type (open/closed) using a single query
  139. subtasks_grouped = issue.descendants.visible.joins(:status).select(:is_closed, :id).group(:is_closed).reorder(:is_closed).count(:id)
  140. # Cast keys to boolean in order to have consistent results between database types
  141. subtasks_grouped.transform_keys! {|k| ActiveModel::Type::Boolean.new.cast(k)}
  142. open_subtasks = subtasks_grouped[false].to_i
  143. closed_subtasks = subtasks_grouped[true].to_i
  144. all_subtasks = open_subtasks + closed_subtasks
  145. return if all_subtasks == 0
  146. all_block = content_tag(
  147. 'span',
  148. link_to(all_subtasks, issues_path(parent_id: "~#{issue.id}", set_filter: true, status_id: '*')),
  149. class: 'badge badge-issues-count'
  150. )
  151. closed_block = content_tag(
  152. 'span',
  153. link_to_if(
  154. closed_subtasks > 0,
  155. l(:label_x_closed_issues_abbr, count: closed_subtasks),
  156. issues_path(parent_id: "~#{issue.id}", set_filter: true, status_id: 'c')
  157. ),
  158. class: 'closed'
  159. )
  160. open_block = content_tag(
  161. 'span',
  162. link_to_if(
  163. open_subtasks > 0,
  164. l(:label_x_open_issues_abbr, :count => open_subtasks),
  165. issues_path(:parent_id => "~#{issue.id}", :set_filter => true, :status_id => 'o')
  166. ),
  167. class: 'open'
  168. )
  169. content_tag(
  170. 'span',
  171. "#{all_block} (#{open_block} &#8212; #{closed_block})".html_safe,
  172. :class => 'issues-stat'
  173. )
  174. end
  175. # Renders the list of related issues on the issue details view
  176. def render_issue_relations(issue, relations)
  177. manage_relations = User.current.allowed_to?(:manage_issue_relations, issue.project)
  178. s = ''.html_safe
  179. relations.each do |relation|
  180. other_issue = relation.other_issue(issue)
  181. css = "issue hascontextmenu #{other_issue.css_classes}"
  182. buttons =
  183. if manage_relations
  184. link_to(
  185. l(:label_relation_delete),
  186. relation_path(relation),
  187. :remote => true,
  188. :method => :delete,
  189. :data => {:confirm => l(:text_are_you_sure)},
  190. :title => l(:label_relation_delete),
  191. :class => 'icon-only icon-link-break'
  192. )
  193. else
  194. "".html_safe
  195. end
  196. buttons << link_to_context_menu
  197. s <<
  198. content_tag(
  199. 'tr',
  200. content_tag('td',
  201. check_box_tag(
  202. "ids[]", other_issue.id,
  203. false, :id => nil),
  204. :class => 'checkbox') +
  205. content_tag('td',
  206. relation.to_s(@issue) do |other|
  207. link_to_issue(
  208. other,
  209. :project => Setting.cross_project_issue_relations?
  210. )
  211. end.html_safe,
  212. :class => 'subject') +
  213. content_tag('td', other_issue.status, :class => 'status') +
  214. content_tag('td', link_to_user(other_issue.assigned_to), :class => 'assigned_to') +
  215. content_tag('td', format_date(other_issue.start_date), :class => 'start_date') +
  216. content_tag('td', format_date(other_issue.due_date), :class => 'due_date') +
  217. content_tag('td',
  218. (if other_issue.disabled_core_fields.include?('done_ratio')
  219. ''
  220. else
  221. progress_bar(other_issue.done_ratio)
  222. end),
  223. :class=> 'done_ratio') +
  224. content_tag('td', buttons, :class => 'buttons'),
  225. :id => "relation-#{relation.id}",
  226. :class => css)
  227. end
  228. content_tag('table', s, :class => 'list issues odd-even')
  229. end
  230. def issue_estimated_hours_details(issue)
  231. if issue.total_estimated_hours.present?
  232. if issue.total_estimated_hours == issue.estimated_hours
  233. l_hours_short(issue.estimated_hours)
  234. else
  235. s = issue.estimated_hours.present? ? l_hours_short(issue.estimated_hours) : ""
  236. s += " (#{l(:label_total)}: #{l_hours_short(issue.total_estimated_hours)})"
  237. s.html_safe
  238. end
  239. end
  240. end
  241. def issue_spent_hours_details(issue)
  242. if issue.total_spent_hours > 0
  243. path = project_time_entries_path(issue.project, :issue_id => "~#{issue.id}")
  244. if issue.total_spent_hours == issue.spent_hours
  245. link_to(l_hours_short(issue.spent_hours), path)
  246. else
  247. s = issue.spent_hours > 0 ? l_hours_short(issue.spent_hours) : ""
  248. s += " (#{l(:label_total)}: #{link_to l_hours_short(issue.total_spent_hours), path})"
  249. s.html_safe
  250. end
  251. end
  252. end
  253. def issue_due_date_details(issue)
  254. return if issue&.due_date.nil?
  255. s = format_date(issue.due_date)
  256. s += " (#{due_date_distance_in_words(issue.due_date)})" unless issue.closed?
  257. s
  258. end
  259. # Returns a link for adding a new subtask to the given issue
  260. def link_to_new_subtask(issue)
  261. link_to(l(:button_add), url_for_new_subtask(issue))
  262. end
  263. def url_for_new_subtask(issue)
  264. attrs = {
  265. :parent_issue_id => issue
  266. }
  267. attrs[:tracker_id] = issue.tracker unless issue.tracker.disabled_core_fields.include?('parent_issue_id')
  268. params = {:issue => attrs}
  269. params[:back_url] = issue_path(issue) if controller_name == 'issues' && action_name == 'show'
  270. new_project_issue_path(issue.project, params)
  271. end
  272. def trackers_options_for_select(issue)
  273. trackers = trackers_for_select(issue)
  274. trackers.collect {|t| [t.name, t.id]}
  275. end
  276. def trackers_for_select(issue)
  277. trackers = issue.allowed_target_trackers
  278. if issue.new_record? && issue.parent_issue_id.present?
  279. trackers = trackers.reject do |tracker|
  280. issue.tracker_id != tracker.id && tracker.disabled_core_fields.include?('parent_issue_id')
  281. end
  282. end
  283. trackers
  284. end
  285. class IssueFieldsRows
  286. include ActionView::Helpers::TagHelper
  287. def initialize
  288. @left = []
  289. @right = []
  290. end
  291. def left(*args)
  292. args.any? ? @left << cells(*args) : @left
  293. end
  294. def right(*args)
  295. args.any? ? @right << cells(*args) : @right
  296. end
  297. def size
  298. @left.size > @right.size ? @left.size : @right.size
  299. end
  300. def to_html
  301. content =
  302. content_tag('div', @left.reduce(&:+), :class => 'splitcontentleft') +
  303. content_tag('div', @right.reduce(&:+), :class => 'splitcontentleft')
  304. content_tag('div', content, :class => 'splitcontent')
  305. end
  306. def cells(label, text, options={})
  307. options[:class] = [options[:class] || "", 'attribute'].join(' ')
  308. content_tag(
  309. 'div',
  310. content_tag('div', label + ":", :class => 'label') +
  311. content_tag('div', text, :class => 'value'),
  312. options)
  313. end
  314. end
  315. def issue_fields_rows
  316. r = IssueFieldsRows.new
  317. yield r
  318. r.to_html
  319. end
  320. def render_half_width_custom_fields_rows(issue)
  321. values = issue.visible_custom_field_values.reject {|value| value.custom_field.full_width_layout?}
  322. return if values.empty?
  323. half = (values.size / 2.0).ceil
  324. issue_fields_rows do |rows|
  325. values.each_with_index do |value, i|
  326. m = (i < half ? :left : :right)
  327. rows.send m, custom_field_name_tag(value.custom_field), custom_field_value_tag(value), :class => value.custom_field.css_classes
  328. end
  329. end
  330. end
  331. def render_full_width_custom_fields_rows(issue)
  332. values = issue.visible_custom_field_values.select {|value| value.custom_field.full_width_layout?}
  333. return if values.empty?
  334. s = ''.html_safe
  335. values.each_with_index do |value, i|
  336. attr_value_tag = custom_field_value_tag(value)
  337. next if attr_value_tag.blank?
  338. content =
  339. content_tag('hr') +
  340. content_tag('p', content_tag('strong', custom_field_name_tag(value.custom_field) )) +
  341. content_tag('div', attr_value_tag, class: 'value')
  342. s << content_tag('div', content, class: "#{value.custom_field.css_classes} attribute")
  343. end
  344. s
  345. end
  346. # Returns the path for updating the issue form
  347. # with project as the current project
  348. def update_issue_form_path(project, issue)
  349. options = {:format => 'js'}
  350. if issue.new_record?
  351. if project
  352. new_project_issue_path(project, options)
  353. else
  354. new_issue_path(options)
  355. end
  356. else
  357. edit_issue_path(issue, options)
  358. end
  359. end
  360. # Returns the number of descendants for an array of issues
  361. def issues_descendant_count(issues)
  362. ids = issues.reject(&:leaf?).map {|issue| issue.descendants.ids}.flatten.uniq
  363. ids -= issues.map(&:id)
  364. ids.size
  365. end
  366. def issues_destroy_confirmation_message(issues)
  367. issues = [issues] unless issues.is_a?(Array)
  368. message = l(:text_issues_destroy_confirmation)
  369. descendant_count = issues_descendant_count(issues)
  370. if descendant_count > 0
  371. message << "\n" + l(:text_issues_destroy_descendants_confirmation, :count => descendant_count)
  372. end
  373. message
  374. end
  375. # Returns an array of users that are proposed as watchers
  376. # on the new issue form
  377. def users_for_new_issue_watchers(issue)
  378. users = issue.watcher_users.select{|u| u.status == User::STATUS_ACTIVE}
  379. assignable_watchers = issue.project.principals.assignable_watchers.limit(21)
  380. if assignable_watchers.size <= 20
  381. users += assignable_watchers.sort
  382. end
  383. users.uniq
  384. end
  385. def email_issue_attributes(issue, user, html)
  386. items = []
  387. %w(author status priority assigned_to category fixed_version start_date due_date).each do |attribute|
  388. if issue.disabled_core_fields.grep(/^#{attribute}(_id)?$/).empty?
  389. attr_value = (issue.send attribute).to_s
  390. next if attr_value.blank?
  391. if html
  392. items << content_tag('strong', "#{l("field_#{attribute}")}: ") + attr_value
  393. else
  394. items << "#{l("field_#{attribute}")}: #{attr_value}"
  395. end
  396. end
  397. end
  398. issue.visible_custom_field_values(user).each do |value|
  399. cf_value = show_value(value, false)
  400. next if cf_value.blank?
  401. if html
  402. items << content_tag('strong', "#{value.custom_field.name}: ") + cf_value
  403. else
  404. items << "#{value.custom_field.name}: #{cf_value}"
  405. end
  406. end
  407. items
  408. end
  409. def render_email_issue_attributes(issue, user, html=false)
  410. items = email_issue_attributes(issue, user, html)
  411. if html
  412. content_tag('ul', items.map{|s| content_tag('li', s)}.join("\n").html_safe, :class => "details")
  413. else
  414. items.map{|s| "* #{s}"}.join("\n")
  415. end
  416. end
  417. MultipleValuesDetail = Struct.new(:property, :prop_key, :custom_field, :old_value, :value)
  418. # Returns the textual representation of a journal details
  419. # as an array of strings
  420. def details_to_strings(details, no_html=false, options={})
  421. options[:only_path] = !(options[:only_path] == false)
  422. strings = []
  423. values_by_field = {}
  424. details.each do |detail|
  425. if detail.property == 'cf'
  426. field = detail.custom_field
  427. if field && field.multiple?
  428. values_by_field[field] ||= {:added => [], :deleted => []}
  429. if detail.old_value
  430. values_by_field[field][:deleted] << detail.old_value
  431. end
  432. if detail.value
  433. values_by_field[field][:added] << detail.value
  434. end
  435. next
  436. end
  437. end
  438. strings << show_detail(detail, no_html, options)
  439. end
  440. if values_by_field.present?
  441. values_by_field.each do |field, changes|
  442. if changes[:added].any?
  443. detail = MultipleValuesDetail.new('cf', field.id.to_s, field)
  444. detail.value = changes[:added]
  445. strings << show_detail(detail, no_html, options)
  446. end
  447. if changes[:deleted].any?
  448. detail = MultipleValuesDetail.new('cf', field.id.to_s, field)
  449. detail.old_value = changes[:deleted]
  450. strings << show_detail(detail, no_html, options)
  451. end
  452. end
  453. end
  454. strings
  455. end
  456. # Returns the textual representation of a single journal detail
  457. def show_detail(detail, no_html=false, options={})
  458. multiple = false
  459. show_diff = false
  460. no_details = false
  461. case detail.property
  462. when 'attr'
  463. field = detail.prop_key.to_s.gsub(/\_id$/, "")
  464. label = l(("field_" + field).to_sym)
  465. case detail.prop_key
  466. when 'due_date', 'start_date'
  467. value = format_date(detail.value.to_date) if detail.value
  468. old_value = format_date(detail.old_value.to_date) if detail.old_value
  469. when 'project_id', 'status_id', 'tracker_id', 'assigned_to_id',
  470. 'priority_id', 'category_id', 'fixed_version_id'
  471. value = find_name_by_reflection(field, detail.value)
  472. old_value = find_name_by_reflection(field, detail.old_value)
  473. when 'estimated_hours'
  474. value = l_hours_short(detail.value.to_f) unless detail.value.blank?
  475. old_value = l_hours_short(detail.old_value.to_f) unless detail.old_value.blank?
  476. when 'parent_id'
  477. label = l(:field_parent_issue)
  478. value = "##{detail.value}" unless detail.value.blank?
  479. old_value = "##{detail.old_value}" unless detail.old_value.blank?
  480. when 'is_private'
  481. value = l(detail.value == "0" ? :general_text_No : :general_text_Yes) unless detail.value.blank?
  482. old_value = l(detail.old_value == "0" ? :general_text_No : :general_text_Yes) unless detail.old_value.blank?
  483. when 'description'
  484. show_diff = true
  485. end
  486. when 'cf'
  487. custom_field = detail.custom_field
  488. if custom_field
  489. label = custom_field.name
  490. if custom_field.format.class.change_no_details
  491. no_details = true
  492. elsif custom_field.format.class.change_as_diff
  493. show_diff = true
  494. else
  495. multiple = custom_field.multiple?
  496. value = format_value(detail.value, custom_field) if detail.value
  497. old_value = format_value(detail.old_value, custom_field) if detail.old_value
  498. end
  499. end
  500. when 'attachment'
  501. label = l(:label_attachment)
  502. when 'relation'
  503. if detail.value && !detail.old_value
  504. rel_issue = Issue.visible.find_by_id(detail.value)
  505. value =
  506. if rel_issue.nil?
  507. "#{l(:label_issue)} ##{detail.value}"
  508. else
  509. (no_html ? rel_issue : link_to_issue(rel_issue, :only_path => options[:only_path]))
  510. end
  511. elsif detail.old_value && !detail.value
  512. rel_issue = Issue.visible.find_by_id(detail.old_value)
  513. old_value =
  514. if rel_issue.nil?
  515. "#{l(:label_issue)} ##{detail.old_value}"
  516. else
  517. (no_html ? rel_issue : link_to_issue(rel_issue, :only_path => options[:only_path]))
  518. end
  519. end
  520. relation_type = IssueRelation::TYPES[detail.prop_key]
  521. label = l(relation_type[:name]) if relation_type
  522. end
  523. call_hook(:helper_issues_show_detail_after_setting,
  524. {:detail => detail, :label => label, :value => value, :old_value => old_value})
  525. label ||= detail.prop_key
  526. value ||= detail.value
  527. old_value ||= detail.old_value
  528. unless no_html
  529. label = content_tag('strong', label)
  530. old_value = content_tag("i", h(old_value)) if detail.old_value
  531. if detail.old_value && detail.value.blank? && detail.property != 'relation'
  532. old_value = content_tag("del", old_value)
  533. end
  534. if detail.property == 'attachment' && value.present? &&
  535. atta = detail.journal.journalized.attachments.detect {|a| a.id == detail.prop_key.to_i}
  536. # Link to the attachment if it has not been removed
  537. value = link_to_attachment(atta, only_path: options[:only_path])
  538. if options[:only_path] != false
  539. value += ' '
  540. value += link_to_attachment atta, class: 'icon-only icon-download', title: l(:button_download), download: true
  541. end
  542. else
  543. value = content_tag("i", h(value)) if value
  544. end
  545. end
  546. if no_details
  547. s = l(:text_journal_changed_no_detail, :label => label).html_safe
  548. elsif show_diff
  549. s = l(:text_journal_changed_no_detail, :label => label)
  550. unless no_html
  551. diff_link =
  552. link_to(
  553. l(:label_diff),
  554. diff_journal_url(detail.journal_id, :detail_id => detail.id,
  555. :only_path => options[:only_path]),
  556. :title => l(:label_view_diff))
  557. s << " (#{diff_link})"
  558. end
  559. s.html_safe
  560. elsif detail.value.present?
  561. case detail.property
  562. when 'attr', 'cf'
  563. if detail.old_value.present?
  564. l(:text_journal_changed, :label => label, :old => old_value, :new => value).html_safe
  565. elsif multiple
  566. l(:text_journal_added, :label => label, :value => value).html_safe
  567. else
  568. l(:text_journal_set_to, :label => label, :value => value).html_safe
  569. end
  570. when 'attachment', 'relation'
  571. l(:text_journal_added, :label => label, :value => value).html_safe
  572. end
  573. else
  574. l(:text_journal_deleted, :label => label, :old => old_value).html_safe
  575. end
  576. end
  577. # Find the name of an associated record stored in the field attribute
  578. # For project, return the associated record only if is visible for the current User
  579. def find_name_by_reflection(field, id)
  580. return nil if id.blank?
  581. @detail_value_name_by_reflection ||= Hash.new do |hash, key|
  582. association = Issue.reflect_on_association(key.first.to_sym)
  583. name = nil
  584. if association
  585. record = association.klass.find_by_id(key.last)
  586. if (record && !record.is_a?(Project)) || (record.is_a?(Project) && record.visible?)
  587. name = record.name.force_encoding('UTF-8')
  588. end
  589. end
  590. hash[key] = name
  591. end
  592. @detail_value_name_by_reflection[[field, id]]
  593. end
  594. # Renders issue children recursively
  595. def render_api_issue_children(issue, api)
  596. return if issue.leaf?
  597. api.array :children do
  598. issue.children.each do |child|
  599. api.issue(:id => child.id) do
  600. api.tracker(:id => child.tracker_id, :name => child.tracker.name) unless child.tracker.nil?
  601. api.subject child.subject
  602. render_api_issue_children(child, api)
  603. end
  604. end
  605. end
  606. end
  607. # Issue history tabs
  608. def issue_history_tabs
  609. tabs = []
  610. if @journals.present?
  611. journals_without_notes = @journals.select{|value| value.notes.blank?}
  612. journals_with_notes = @journals.reject{|value| value.notes.blank?}
  613. tabs <<
  614. {
  615. :name => 'history',
  616. :label => :label_history,
  617. :onclick => 'showIssueHistory("history", this.href)',
  618. :partial => 'issues/tabs/history',
  619. :locals => {:issue => @issue, :journals => @journals}
  620. }
  621. if journals_with_notes.any?
  622. tabs <<
  623. {
  624. :name => 'notes',
  625. :label => :label_issue_history_notes,
  626. :onclick => 'showIssueHistory("notes", this.href)'
  627. }
  628. end
  629. if journals_without_notes.any?
  630. tabs <<
  631. {
  632. :name => 'properties',
  633. :label => :label_issue_history_properties,
  634. :onclick => 'showIssueHistory("properties", this.href)'
  635. }
  636. end
  637. end
  638. if User.current.allowed_to?(:view_time_entries, @project) && @issue.spent_hours > 0
  639. tabs <<
  640. {
  641. :name => 'time_entries',
  642. :label => :label_time_entry_plural,
  643. :remote => true,
  644. :onclick =>
  645. "getRemoteTab('time_entries', " \
  646. "'#{tab_issue_path(@issue, :name => 'time_entries')}', " \
  647. "'#{issue_path(@issue, :tab => 'time_entries')}')"
  648. }
  649. end
  650. if @has_changesets
  651. tabs <<
  652. {
  653. :name => 'changesets',
  654. :label => :label_associated_revisions,
  655. :remote => true,
  656. :onclick =>
  657. "getRemoteTab('changesets', " \
  658. "'#{tab_issue_path(@issue, :name => 'changesets')}', " \
  659. "'#{issue_path(@issue, :tab => 'changesets')}')"
  660. }
  661. end
  662. tabs
  663. end
  664. def issue_history_default_tab
  665. # tab params overrides user default tab preference
  666. return params[:tab] if params[:tab].present?
  667. user_default_tab = User.current.pref.history_default_tab
  668. case user_default_tab
  669. when 'last_tab_visited'
  670. cookies['history_last_tab'].presence || 'notes'
  671. when ''
  672. 'notes'
  673. else
  674. user_default_tab
  675. end
  676. end
  677. def projects_for_select(issue)
  678. if issue.parent_issue_id.present?
  679. issue.allowed_target_projects_for_subtask(User.current)
  680. elsif @project && issue.new_record? && !issue.copy?
  681. issue.allowed_target_projects(User.current, 'tree')
  682. else
  683. issue.allowed_target_projects(User.current)
  684. end
  685. end
  686. end