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 27KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2022 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. render_issues_stats(open_subtasks, closed_subtasks, {:parent_id => "~#{issue.id}"})
  145. end
  146. # Renders relations stats (total relations (open - closed)) with query links
  147. def render_relations_stats(issue, relations)
  148. open_relations = relations.count{|r| (r.other_issue(issue).closed?)==false}
  149. closed_relations = relations.count{|r| r.other_issue(issue).closed?}
  150. render_issues_stats(open_relations, closed_relations, {:issue_id => relations.map{|r| r.other_issue(issue).id}.join(',')})
  151. end
  152. # Renders issues stats (total relations (open - closed)) with query links
  153. def render_issues_stats(open_issues=0, closed_issues=0, issues_path_attr={})
  154. total_issues = open_issues + closed_issues
  155. return if total_issues == 0
  156. all_block = content_tag(
  157. 'span',
  158. link_to(total_issues, issues_path(issues_path_attr.merge({:set_filter => true, :status_id => '*'}))),
  159. class: 'badge badge-issues-count'
  160. )
  161. closed_block = content_tag(
  162. 'span',
  163. link_to_if(
  164. closed_issues > 0,
  165. l(:label_x_closed_issues_abbr, count: closed_issues),
  166. issues_path(issues_path_attr.merge({:set_filter => true, :status_id => 'c'}))
  167. ),
  168. class: 'closed'
  169. )
  170. open_block = content_tag(
  171. 'span',
  172. link_to_if(
  173. open_issues > 0,
  174. l(:label_x_open_issues_abbr, :count => open_issues),
  175. issues_path(issues_path_attr.merge({:set_filter => true, :status_id => 'o'}))
  176. ),
  177. class: 'open'
  178. )
  179. content_tag(
  180. 'span',
  181. "#{all_block} (#{open_block} &#8212; #{closed_block})".html_safe,
  182. :class => 'issues-stat'
  183. )
  184. end
  185. # Renders the list of related issues on the issue details view
  186. def render_issue_relations(issue, relations)
  187. manage_relations = User.current.allowed_to?(:manage_issue_relations, issue.project)
  188. s = ''.html_safe
  189. relations.each do |relation|
  190. other_issue = relation.other_issue(issue)
  191. css = "issue hascontextmenu #{other_issue.css_classes}"
  192. buttons =
  193. if manage_relations
  194. link_to(
  195. l(:label_relation_delete),
  196. relation_path(relation),
  197. :remote => true,
  198. :method => :delete,
  199. :data => {:confirm => l(:text_are_you_sure)},
  200. :title => l(:label_relation_delete),
  201. :class => 'icon-only icon-link-break'
  202. )
  203. else
  204. "".html_safe
  205. end
  206. buttons << link_to_context_menu
  207. s <<
  208. content_tag(
  209. 'tr',
  210. content_tag('td',
  211. check_box_tag(
  212. "ids[]", other_issue.id,
  213. false, :id => nil),
  214. :class => 'checkbox') +
  215. content_tag('td',
  216. relation.to_s(@issue) do |other|
  217. link_to_issue(
  218. other,
  219. :project => Setting.cross_project_issue_relations?
  220. )
  221. end.html_safe,
  222. :class => 'subject') +
  223. content_tag('td', other_issue.status, :class => 'status') +
  224. content_tag('td', link_to_user(other_issue.assigned_to), :class => 'assigned_to') +
  225. content_tag('td', format_date(other_issue.start_date), :class => 'start_date') +
  226. content_tag('td', format_date(other_issue.due_date), :class => 'due_date') +
  227. content_tag('td',
  228. (if other_issue.disabled_core_fields.include?('done_ratio')
  229. ''
  230. else
  231. progress_bar(other_issue.done_ratio)
  232. end),
  233. :class=> 'done_ratio') +
  234. content_tag('td', buttons, :class => 'buttons'),
  235. :id => "relation-#{relation.id}",
  236. :class => css)
  237. end
  238. content_tag('table', s, :class => 'list issues odd-even')
  239. end
  240. def issue_estimated_hours_details(issue)
  241. if issue.total_estimated_hours.present?
  242. if issue.total_estimated_hours == issue.estimated_hours
  243. l_hours_short(issue.estimated_hours)
  244. else
  245. s = issue.estimated_hours.present? ? l_hours_short(issue.estimated_hours) : ""
  246. s += " (#{l(:label_total)}: #{l_hours_short(issue.total_estimated_hours)})"
  247. s.html_safe
  248. end
  249. end
  250. end
  251. def issue_spent_hours_details(issue)
  252. if issue.total_spent_hours > 0
  253. path = project_time_entries_path(issue.project, :issue_id => "~#{issue.id}")
  254. if issue.total_spent_hours == issue.spent_hours
  255. link_to(l_hours_short(issue.spent_hours), path)
  256. else
  257. # link to global time entries if cross-project subtasks are allowed
  258. # in order to show time entries from not descendents projects
  259. if %w(system tree hierarchy).include?(Setting.cross_project_subtasks)
  260. path = time_entries_path(:issue_id => "~#{issue.id}")
  261. end
  262. s = issue.spent_hours > 0 ? l_hours_short(issue.spent_hours) : ""
  263. s += " (#{l(:label_total)}: #{link_to l_hours_short(issue.total_spent_hours), path})"
  264. s.html_safe
  265. end
  266. end
  267. end
  268. def issue_due_date_details(issue)
  269. return if issue&.due_date.nil?
  270. s = format_date(issue.due_date)
  271. s += " (#{due_date_distance_in_words(issue.due_date)})" unless issue.closed?
  272. s
  273. end
  274. # Returns a link for adding a new subtask to the given issue
  275. def link_to_new_subtask(issue)
  276. link_to(l(:button_add), url_for_new_subtask(issue))
  277. end
  278. def url_for_new_subtask(issue)
  279. attrs = {
  280. :parent_issue_id => issue
  281. }
  282. attrs[:tracker_id] = issue.tracker unless issue.tracker.disabled_core_fields.include?('parent_issue_id')
  283. params = {:issue => attrs}
  284. params[:back_url] = issue_path(issue) if controller_name == 'issues' && action_name == 'show'
  285. new_project_issue_path(issue.project, params)
  286. end
  287. def trackers_options_for_select(issue)
  288. trackers = trackers_for_select(issue)
  289. trackers.collect {|t| [t.name, t.id]}
  290. end
  291. def trackers_for_select(issue)
  292. trackers = issue.allowed_target_trackers
  293. if issue.new_record? && issue.parent_issue_id.present?
  294. trackers = trackers.reject do |tracker|
  295. issue.tracker_id != tracker.id && tracker.disabled_core_fields.include?('parent_issue_id')
  296. end
  297. end
  298. trackers
  299. end
  300. class IssueFieldsRows
  301. include ActionView::Helpers::TagHelper
  302. def initialize
  303. @left = []
  304. @right = []
  305. end
  306. def left(*args)
  307. args.any? ? @left << cells(*args) : @left
  308. end
  309. def right(*args)
  310. args.any? ? @right << cells(*args) : @right
  311. end
  312. def size
  313. @left.size > @right.size ? @left.size : @right.size
  314. end
  315. def to_html
  316. content =
  317. content_tag('div', @left.reduce(&:+), :class => 'splitcontentleft') +
  318. content_tag('div', @right.reduce(&:+), :class => 'splitcontentleft')
  319. content_tag('div', content, :class => 'splitcontent')
  320. end
  321. def cells(label, text, options={})
  322. options[:class] = [options[:class] || "", 'attribute'].join(' ')
  323. content_tag(
  324. 'div',
  325. content_tag('div', label + ":", :class => 'label') +
  326. content_tag('div', text, :class => 'value'),
  327. options)
  328. end
  329. end
  330. def issue_fields_rows
  331. r = IssueFieldsRows.new
  332. yield r
  333. r.to_html
  334. end
  335. def render_half_width_custom_fields_rows(issue)
  336. values = issue.visible_custom_field_values.reject {|value| value.custom_field.full_width_layout?}
  337. return if values.empty?
  338. half = (values.size / 2.0).ceil
  339. issue_fields_rows do |rows|
  340. values.each_with_index do |value, i|
  341. m = (i < half ? :left : :right)
  342. rows.send m, custom_field_name_tag(value.custom_field), custom_field_value_tag(value), :class => value.custom_field.css_classes
  343. end
  344. end
  345. end
  346. def render_full_width_custom_fields_rows(issue)
  347. values = issue.visible_custom_field_values.select {|value| value.custom_field.full_width_layout?}
  348. return if values.empty?
  349. s = ''.html_safe
  350. values.each_with_index do |value, i|
  351. attr_value_tag = custom_field_value_tag(value)
  352. next if attr_value_tag.blank?
  353. content =
  354. content_tag('hr') +
  355. content_tag('p', content_tag('strong', custom_field_name_tag(value.custom_field) )) +
  356. content_tag('div', attr_value_tag, class: 'value')
  357. s << content_tag('div', content, class: "#{value.custom_field.css_classes} attribute")
  358. end
  359. s
  360. end
  361. # Returns the path for updating the issue form
  362. # with project as the current project
  363. def update_issue_form_path(project, issue)
  364. options = {:format => 'js'}
  365. if issue.new_record?
  366. if project
  367. new_project_issue_path(project, options)
  368. else
  369. new_issue_path(options)
  370. end
  371. else
  372. edit_issue_path(issue, options)
  373. end
  374. end
  375. # Returns the number of descendants for an array of issues
  376. def issues_descendant_count(issues)
  377. ids = issues.reject(&:leaf?).map {|issue| issue.descendants.ids}.flatten.uniq
  378. ids -= issues.map(&:id)
  379. ids.size
  380. end
  381. def issues_destroy_confirmation_message(issues)
  382. issues = [issues] unless issues.is_a?(Array)
  383. message = l(:text_issues_destroy_confirmation)
  384. descendant_count = issues_descendant_count(issues)
  385. if descendant_count > 0
  386. message << "\n" + l(:text_issues_destroy_descendants_confirmation, :count => descendant_count)
  387. end
  388. message
  389. end
  390. # Returns an array of users that are proposed as watchers
  391. # on the new issue form
  392. def users_for_new_issue_watchers(issue)
  393. users = issue.watcher_users.select{|u| u.status == User::STATUS_ACTIVE}
  394. assignable_watchers = issue.project.principals.assignable_watchers.limit(21)
  395. if assignable_watchers.size <= 20
  396. users += assignable_watchers.sort
  397. end
  398. users.uniq
  399. end
  400. def email_issue_attributes(issue, user, html)
  401. items = []
  402. %w(author status priority assigned_to category fixed_version start_date due_date).each do |attribute|
  403. if issue.disabled_core_fields.grep(/^#{attribute}(_id)?$/).empty?
  404. attr_value = (issue.send attribute).to_s
  405. next if attr_value.blank?
  406. if html
  407. items << content_tag('strong', "#{l("field_#{attribute}")}: ") + attr_value
  408. else
  409. items << "#{l("field_#{attribute}")}: #{attr_value}"
  410. end
  411. end
  412. end
  413. issue.visible_custom_field_values(user).each do |value|
  414. cf_value = show_value(value, false)
  415. next if cf_value.blank?
  416. if html
  417. items << content_tag('strong', "#{value.custom_field.name}: ") + cf_value
  418. else
  419. items << "#{value.custom_field.name}: #{cf_value}"
  420. end
  421. end
  422. items
  423. end
  424. def render_email_issue_attributes(issue, user, html=false)
  425. items = email_issue_attributes(issue, user, html)
  426. if html
  427. content_tag('ul', items.map{|s| content_tag('li', s)}.join("\n").html_safe, :class => "details")
  428. else
  429. items.map{|s| "* #{s}"}.join("\n")
  430. end
  431. end
  432. MultipleValuesDetail = Struct.new(:property, :prop_key, :custom_field, :old_value, :value)
  433. # Returns the textual representation of a journal details
  434. # as an array of strings
  435. def details_to_strings(details, no_html=false, options={})
  436. options[:only_path] = !(options[:only_path] == false)
  437. strings = []
  438. values_by_field = {}
  439. details.each do |detail|
  440. if detail.property == 'cf'
  441. field = detail.custom_field
  442. if field && field.multiple?
  443. values_by_field[field] ||= {:added => [], :deleted => []}
  444. if detail.old_value
  445. values_by_field[field][:deleted] << detail.old_value
  446. end
  447. if detail.value
  448. values_by_field[field][:added] << detail.value
  449. end
  450. next
  451. end
  452. end
  453. strings << show_detail(detail, no_html, options)
  454. end
  455. if values_by_field.present?
  456. values_by_field.each do |field, changes|
  457. if changes[:added].any?
  458. detail = MultipleValuesDetail.new('cf', field.id.to_s, field)
  459. detail.value = changes[:added]
  460. strings << show_detail(detail, no_html, options)
  461. end
  462. if changes[:deleted].any?
  463. detail = MultipleValuesDetail.new('cf', field.id.to_s, field)
  464. detail.old_value = changes[:deleted]
  465. strings << show_detail(detail, no_html, options)
  466. end
  467. end
  468. end
  469. strings
  470. end
  471. # Returns the textual representation of a single journal detail
  472. def show_detail(detail, no_html=false, options={})
  473. multiple = false
  474. show_diff = false
  475. no_details = false
  476. case detail.property
  477. when 'attr'
  478. field = detail.prop_key.to_s.gsub(/\_id$/, "")
  479. label = l(("field_" + field).to_sym)
  480. case detail.prop_key
  481. when 'due_date', 'start_date'
  482. value = format_date(detail.value.to_date) if detail.value
  483. old_value = format_date(detail.old_value.to_date) if detail.old_value
  484. when 'project_id', 'status_id', 'tracker_id', 'assigned_to_id',
  485. 'priority_id', 'category_id', 'fixed_version_id'
  486. value = find_name_by_reflection(field, detail.value)
  487. old_value = find_name_by_reflection(field, detail.old_value)
  488. when 'estimated_hours'
  489. value = l_hours_short(detail.value.to_f) unless detail.value.blank?
  490. old_value = l_hours_short(detail.old_value.to_f) unless detail.old_value.blank?
  491. when 'parent_id'
  492. label = l(:field_parent_issue)
  493. value = "##{detail.value}" unless detail.value.blank?
  494. old_value = "##{detail.old_value}" unless detail.old_value.blank?
  495. when 'child_id'
  496. label = l(:label_subtask)
  497. value = "##{detail.value}" unless detail.value.blank?
  498. old_value = "##{detail.old_value}" unless detail.old_value.blank?
  499. multiple = true
  500. when 'is_private'
  501. value = l(detail.value == "0" ? :general_text_No : :general_text_Yes) unless detail.value.blank?
  502. old_value = l(detail.old_value == "0" ? :general_text_No : :general_text_Yes) unless detail.old_value.blank?
  503. when 'description'
  504. show_diff = true
  505. end
  506. when 'cf'
  507. custom_field = detail.custom_field
  508. if custom_field
  509. label = custom_field.name
  510. if custom_field.format.class.change_no_details
  511. no_details = true
  512. elsif custom_field.format.class.change_as_diff
  513. show_diff = true
  514. else
  515. multiple = custom_field.multiple?
  516. value = format_value(detail.value, custom_field) if detail.value
  517. old_value = format_value(detail.old_value, custom_field) if detail.old_value
  518. end
  519. end
  520. when 'attachment'
  521. label = l(:label_attachment)
  522. when 'relation'
  523. if detail.value && !detail.old_value
  524. rel_issue = Issue.visible.find_by_id(detail.value)
  525. value =
  526. if rel_issue.nil?
  527. "#{l(:label_issue)} ##{detail.value}"
  528. else
  529. (no_html ? rel_issue : link_to_issue(rel_issue, :only_path => options[:only_path]))
  530. end
  531. elsif detail.old_value && !detail.value
  532. rel_issue = Issue.visible.find_by_id(detail.old_value)
  533. old_value =
  534. if rel_issue.nil?
  535. "#{l(:label_issue)} ##{detail.old_value}"
  536. else
  537. (no_html ? rel_issue : link_to_issue(rel_issue, :only_path => options[:only_path]))
  538. end
  539. end
  540. relation_type = IssueRelation::TYPES[detail.prop_key]
  541. label = l(relation_type[:name]) if relation_type
  542. end
  543. call_hook(:helper_issues_show_detail_after_setting,
  544. {:detail => detail, :label => label, :value => value, :old_value => old_value})
  545. label ||= detail.prop_key
  546. value ||= detail.value
  547. old_value ||= detail.old_value
  548. unless no_html
  549. label = content_tag('strong', label)
  550. old_value = content_tag("i", h(old_value)) if detail.old_value
  551. if detail.old_value && detail.value.blank? && detail.property != 'relation'
  552. old_value = content_tag("del", old_value)
  553. end
  554. if detail.property == 'attachment' && value.present? &&
  555. atta = detail.journal.journalized.attachments.detect {|a| a.id == detail.prop_key.to_i}
  556. # Link to the attachment if it has not been removed
  557. value = link_to_attachment(atta, only_path: options[:only_path])
  558. if options[:only_path] != false
  559. value += ' '
  560. value += link_to_attachment atta, class: 'icon-only icon-download', title: l(:button_download), download: true
  561. end
  562. else
  563. value = content_tag("i", h(value)) if value
  564. end
  565. end
  566. if no_details
  567. s = l(:text_journal_changed_no_detail, :label => label).html_safe
  568. elsif show_diff
  569. s = l(:text_journal_changed_no_detail, :label => label)
  570. unless no_html
  571. diff_link =
  572. link_to(
  573. l(:label_diff),
  574. diff_journal_url(detail.journal_id, :detail_id => detail.id,
  575. :only_path => options[:only_path]),
  576. :title => l(:label_view_diff))
  577. s << " (#{diff_link})"
  578. end
  579. s.html_safe
  580. elsif detail.value.present?
  581. case detail.property
  582. when 'attr', 'cf'
  583. if detail.old_value.present?
  584. l(:text_journal_changed, :label => label, :old => old_value, :new => value).html_safe
  585. elsif multiple
  586. l(:text_journal_added, :label => label, :value => value).html_safe
  587. else
  588. l(:text_journal_set_to, :label => label, :value => value).html_safe
  589. end
  590. when 'attachment', 'relation'
  591. l(:text_journal_added, :label => label, :value => value).html_safe
  592. end
  593. else
  594. l(:text_journal_deleted, :label => label, :old => old_value).html_safe
  595. end
  596. end
  597. # Find the name of an associated record stored in the field attribute
  598. # For project, return the associated record only if is visible for the current User
  599. def find_name_by_reflection(field, id)
  600. return nil if id.blank?
  601. @detail_value_name_by_reflection ||= Hash.new do |hash, key|
  602. association = Issue.reflect_on_association(key.first.to_sym)
  603. name = nil
  604. if association
  605. record = association.klass.find_by_id(key.last)
  606. if (record && !record.is_a?(Project)) || (record.is_a?(Project) && record.visible?)
  607. name = record.name.force_encoding('UTF-8')
  608. end
  609. end
  610. hash[key] = name
  611. end
  612. @detail_value_name_by_reflection[[field, id]]
  613. end
  614. # Renders issue children recursively
  615. def render_api_issue_children(issue, api)
  616. return if issue.leaf?
  617. api.array :children do
  618. issue.children.each do |child|
  619. api.issue(:id => child.id) do
  620. api.tracker(:id => child.tracker_id, :name => child.tracker.name) unless child.tracker.nil?
  621. api.subject child.subject
  622. render_api_issue_children(child, api)
  623. end
  624. end
  625. end
  626. end
  627. # Issue history tabs
  628. def issue_history_tabs
  629. tabs = []
  630. if @journals.present?
  631. journals_without_notes = @journals.select{|value| value.notes.blank?}
  632. journals_with_notes = @journals.reject{|value| value.notes.blank?}
  633. tabs <<
  634. {
  635. :name => 'history',
  636. :label => :label_history,
  637. :onclick => 'showIssueHistory("history", this.href)',
  638. :partial => 'issues/tabs/history',
  639. :locals => {:issue => @issue, :journals => @journals}
  640. }
  641. if journals_with_notes.any?
  642. tabs <<
  643. {
  644. :name => 'notes',
  645. :label => :label_issue_history_notes,
  646. :onclick => 'showIssueHistory("notes", this.href)'
  647. }
  648. end
  649. if journals_without_notes.any?
  650. tabs <<
  651. {
  652. :name => 'properties',
  653. :label => :label_issue_history_properties,
  654. :onclick => 'showIssueHistory("properties", this.href)'
  655. }
  656. end
  657. end
  658. if User.current.allowed_to?(:view_time_entries, @project) && @issue.spent_hours > 0
  659. tabs <<
  660. {
  661. :name => 'time_entries',
  662. :label => :label_time_entry_plural,
  663. :remote => true,
  664. :onclick =>
  665. "getRemoteTab('time_entries', " \
  666. "'#{tab_issue_path(@issue, :name => 'time_entries')}', " \
  667. "'#{issue_path(@issue, :tab => 'time_entries')}')"
  668. }
  669. end
  670. if @has_changesets
  671. tabs <<
  672. {
  673. :name => 'changesets',
  674. :label => :label_associated_revisions,
  675. :remote => true,
  676. :onclick =>
  677. "getRemoteTab('changesets', " \
  678. "'#{tab_issue_path(@issue, :name => 'changesets')}', " \
  679. "'#{issue_path(@issue, :tab => 'changesets')}')"
  680. }
  681. end
  682. tabs
  683. end
  684. def issue_history_default_tab
  685. # tab params overrides user default tab preference
  686. return params[:tab] if params[:tab].present?
  687. user_default_tab = User.current.pref.history_default_tab
  688. case user_default_tab
  689. when 'last_tab_visited'
  690. cookies['history_last_tab'].presence || 'notes'
  691. when ''
  692. 'notes'
  693. else
  694. user_default_tab
  695. end
  696. end
  697. def projects_for_select(issue)
  698. if issue.parent_issue_id.present?
  699. issue.allowed_target_projects_for_subtask(User.current)
  700. elsif @project && issue.new_record? && !issue.copy?
  701. issue.allowed_target_projects(User.current, 'tree')
  702. else
  703. issue.allowed_target_projects(User.current)
  704. end
  705. end
  706. end