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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. # encoding: utf-8
  2. #
  3. # Redmine - project management software
  4. # Copyright (C) 2006-2017 Jean-Philippe Lang
  5. #
  6. # This program is free software; you can redistribute it and/or
  7. # modify it under the terms of the GNU General Public License
  8. # as published by the Free Software Foundation; either version 2
  9. # of the License, or (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program; if not, write to the Free Software
  18. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. module IssuesHelper
  20. include ApplicationHelper
  21. include Redmine::Export::PDF::IssuesPdfHelper
  22. def issue_list(issues, &block)
  23. ancestors = []
  24. issues.each do |issue|
  25. while (ancestors.any? && !issue.is_descendant_of?(ancestors.last))
  26. ancestors.pop
  27. end
  28. yield issue, ancestors.size
  29. ancestors << issue unless issue.leaf?
  30. end
  31. end
  32. def grouped_issue_list(issues, query, &block)
  33. ancestors = []
  34. grouped_query_results(issues, query) do |issue, group_name, group_count, group_totals|
  35. while (ancestors.any? && !issue.is_descendant_of?(ancestors.last))
  36. ancestors.pop
  37. end
  38. yield issue, ancestors.size, group_name, group_count, group_totals
  39. ancestors << issue unless issue.leaf?
  40. end
  41. end
  42. # Renders a HTML/CSS tooltip
  43. #
  44. # To use, a trigger div is needed. This is a div with the class of "tooltip"
  45. # that contains this method wrapped in a span with the class of "tip"
  46. #
  47. # <div class="tooltip"><%= link_to_issue(issue) %>
  48. # <span class="tip"><%= render_issue_tooltip(issue) %></span>
  49. # </div>
  50. #
  51. def render_issue_tooltip(issue)
  52. @cached_label_status ||= l(:field_status)
  53. @cached_label_start_date ||= l(:field_start_date)
  54. @cached_label_due_date ||= l(:field_due_date)
  55. @cached_label_assigned_to ||= l(:field_assigned_to)
  56. @cached_label_priority ||= l(:field_priority)
  57. @cached_label_project ||= l(:field_project)
  58. link_to_issue(issue) + "<br /><br />".html_safe +
  59. "<strong>#{@cached_label_project}</strong>: #{link_to_project(issue.project)}<br />".html_safe +
  60. "<strong>#{@cached_label_status}</strong>: #{h(issue.status.name)}<br />".html_safe +
  61. "<strong>#{@cached_label_start_date}</strong>: #{format_date(issue.start_date)}<br />".html_safe +
  62. "<strong>#{@cached_label_due_date}</strong>: #{format_date(issue.due_date)}<br />".html_safe +
  63. "<strong>#{@cached_label_assigned_to}</strong>: #{h(issue.assigned_to)}<br />".html_safe +
  64. "<strong>#{@cached_label_priority}</strong>: #{h(issue.priority.name)}".html_safe
  65. end
  66. def issue_heading(issue)
  67. h("#{issue.tracker} ##{issue.id}")
  68. end
  69. def render_issue_subject_with_tree(issue)
  70. s = ''
  71. ancestors = issue.root? ? [] : issue.ancestors.visible.to_a
  72. ancestors.each do |ancestor|
  73. s << '<div>' + content_tag('p', link_to_issue(ancestor, :project => (issue.project_id != ancestor.project_id)))
  74. end
  75. s << '<div>'
  76. subject = h(issue.subject)
  77. if issue.is_private?
  78. subject = subject + ' ' + content_tag('span', l(:field_is_private), :class => 'private')
  79. end
  80. s << content_tag('h3', subject)
  81. s << '</div>' * (ancestors.size + 1)
  82. s.html_safe
  83. end
  84. def render_descendants_tree(issue)
  85. s = '<table class="list issues odd-even">'
  86. issue_list(issue.descendants.visible.preload(:status, :priority, :tracker, :assigned_to).sort_by(&:lft)) do |child, level|
  87. css = "issue issue-#{child.id} hascontextmenu #{child.css_classes}"
  88. css << " idnt idnt-#{level}" if level > 0
  89. s << content_tag('tr',
  90. content_tag('td', check_box_tag("ids[]", child.id, false, :id => nil), :class => 'checkbox') +
  91. content_tag('td', link_to_issue(child, :project => (issue.project_id != child.project_id)), :class => 'subject', :style => 'width: 50%') +
  92. content_tag('td', h(child.status), :class => 'status') +
  93. content_tag('td', link_to_user(child.assigned_to), :class => 'assigned_to') +
  94. content_tag('td', child.disabled_core_fields.include?('done_ratio') ? '' : progress_bar(child.done_ratio), :class=> 'done_ratio') +
  95. content_tag('td', link_to_context_menu, :class => 'buttons'),
  96. :class => css)
  97. end
  98. s << '</table>'
  99. s.html_safe
  100. end
  101. # Renders the list of related issues on the issue details view
  102. def render_issue_relations(issue, relations)
  103. manage_relations = User.current.allowed_to?(:manage_issue_relations, issue.project)
  104. s = ''.html_safe
  105. relations.each do |relation|
  106. other_issue = relation.other_issue(issue)
  107. css = "issue hascontextmenu #{other_issue.css_classes}"
  108. buttons = manage_relations ? link_to(l(:label_relation_delete),
  109. relation_path(relation),
  110. :remote => true,
  111. :method => :delete,
  112. :data => {:confirm => l(:text_are_you_sure)},
  113. :title => l(:label_relation_delete),
  114. :class => 'icon-only icon-link-break'
  115. ) :"".html_safe
  116. buttons << link_to_context_menu
  117. s << content_tag('tr',
  118. content_tag('td', check_box_tag("ids[]", other_issue.id, false, :id => nil), :class => 'checkbox') +
  119. content_tag('td', relation.to_s(@issue) {|other| link_to_issue(other, :project => Setting.cross_project_issue_relations?)}.html_safe, :class => 'subject', :style => 'width: 50%') +
  120. content_tag('td', other_issue.status, :class => 'status') +
  121. content_tag('td', other_issue.start_date, :class => 'start_date') +
  122. content_tag('td', other_issue.due_date, :class => 'due_date') +
  123. content_tag('td', other_issue.disabled_core_fields.include?('done_ratio') ? '' : progress_bar(other_issue.done_ratio), :class=> 'done_ratio') +
  124. content_tag('td', buttons, :class => 'buttons'),
  125. :id => "relation-#{relation.id}",
  126. :class => css)
  127. end
  128. content_tag('table', s, :class => 'list issues odd-even')
  129. end
  130. def issue_estimated_hours_details(issue)
  131. if issue.total_estimated_hours.present?
  132. if issue.total_estimated_hours == issue.estimated_hours
  133. l_hours_short(issue.estimated_hours)
  134. else
  135. s = issue.estimated_hours.present? ? l_hours_short(issue.estimated_hours) : ""
  136. s << " (#{l(:label_total)}: #{l_hours_short(issue.total_estimated_hours)})"
  137. s.html_safe
  138. end
  139. end
  140. end
  141. def issue_spent_hours_details(issue)
  142. if issue.total_spent_hours > 0
  143. path = project_time_entries_path(issue.project, :issue_id => "~#{issue.id}")
  144. if issue.total_spent_hours == issue.spent_hours
  145. link_to(l_hours_short(issue.spent_hours), path)
  146. else
  147. s = issue.spent_hours > 0 ? l_hours_short(issue.spent_hours) : ""
  148. s << " (#{l(:label_total)}: #{link_to l_hours_short(issue.total_spent_hours), path})"
  149. s.html_safe
  150. end
  151. end
  152. end
  153. # Returns a link for adding a new subtask to the given issue
  154. def link_to_new_subtask(issue)
  155. attrs = {
  156. :parent_issue_id => issue
  157. }
  158. attrs[:tracker_id] = issue.tracker unless issue.tracker.disabled_core_fields.include?('parent_issue_id')
  159. link_to(l(:button_add), new_project_issue_path(issue.project, :issue => attrs, :back_url => issue_path(issue)))
  160. end
  161. def trackers_options_for_select(issue)
  162. trackers = issue.allowed_target_trackers
  163. if issue.new_record? && issue.parent_issue_id.present?
  164. trackers = trackers.reject do |tracker|
  165. issue.tracker_id != tracker.id && tracker.disabled_core_fields.include?('parent_issue_id')
  166. end
  167. end
  168. trackers.collect {|t| [t.name, t.id]}
  169. end
  170. class IssueFieldsRows
  171. include ActionView::Helpers::TagHelper
  172. def initialize
  173. @left = []
  174. @right = []
  175. end
  176. def left(*args)
  177. args.any? ? @left << cells(*args) : @left
  178. end
  179. def right(*args)
  180. args.any? ? @right << cells(*args) : @right
  181. end
  182. def size
  183. @left.size > @right.size ? @left.size : @right.size
  184. end
  185. def to_html
  186. content =
  187. content_tag('div', @left.reduce(&:+), :class => 'splitcontentleft') +
  188. content_tag('div', @right.reduce(&:+), :class => 'splitcontentleft')
  189. content_tag('div', content, :class => 'splitcontent')
  190. end
  191. def cells(label, text, options={})
  192. options[:class] = [options[:class] || "", 'attribute'].join(' ')
  193. content_tag 'div',
  194. content_tag('div', label + ":", :class => 'label') + content_tag('div', text, :class => 'value'),
  195. options
  196. end
  197. end
  198. def issue_fields_rows
  199. r = IssueFieldsRows.new
  200. yield r
  201. r.to_html
  202. end
  203. def render_half_width_custom_fields_rows(issue)
  204. values = issue.visible_custom_field_values.reject {|value| value.custom_field.full_width_layout?}
  205. return if values.empty?
  206. half = (values.size / 2.0).ceil
  207. issue_fields_rows do |rows|
  208. values.each_with_index do |value, i|
  209. css = "cf_#{value.custom_field.id}"
  210. m = (i < half ? :left : :right)
  211. rows.send m, custom_field_name_tag(value.custom_field), show_value(value), :class => css
  212. end
  213. end
  214. end
  215. def render_full_width_custom_fields_rows(issue)
  216. values = issue.visible_custom_field_values.select {|value| value.custom_field.full_width_layout?}
  217. return if values.empty?
  218. s = ''.html_safe
  219. values.each_with_index do |value, i|
  220. attr_value = show_value(value)
  221. next if attr_value.blank?
  222. if value.custom_field.text_formatting == 'full'
  223. attr_value = content_tag('div', attr_value, class: 'wiki')
  224. end
  225. content =
  226. content_tag('hr') +
  227. content_tag('p', content_tag('strong', custom_field_name_tag(value.custom_field) )) +
  228. content_tag('div', attr_value, class: 'value')
  229. s << content_tag('div', content, class: "cf_#{value.custom_field.id} attribute")
  230. end
  231. s
  232. end
  233. # Returns the path for updating the issue form
  234. # with project as the current project
  235. def update_issue_form_path(project, issue)
  236. options = {:format => 'js'}
  237. if issue.new_record?
  238. if project
  239. new_project_issue_path(project, options)
  240. else
  241. new_issue_path(options)
  242. end
  243. else
  244. edit_issue_path(issue, options)
  245. end
  246. end
  247. # Returns the number of descendants for an array of issues
  248. def issues_descendant_count(issues)
  249. ids = issues.reject(&:leaf?).map {|issue| issue.descendants.ids}.flatten.uniq
  250. ids -= issues.map(&:id)
  251. ids.size
  252. end
  253. def issues_destroy_confirmation_message(issues)
  254. issues = [issues] unless issues.is_a?(Array)
  255. message = l(:text_issues_destroy_confirmation)
  256. descendant_count = issues_descendant_count(issues)
  257. if descendant_count > 0
  258. message << "\n" + l(:text_issues_destroy_descendants_confirmation, :count => descendant_count)
  259. end
  260. message
  261. end
  262. # Returns an array of users that are proposed as watchers
  263. # on the new issue form
  264. def users_for_new_issue_watchers(issue)
  265. users = issue.watcher_users.select{|u| u.status == User::STATUS_ACTIVE}
  266. if issue.project.users.count <= 20
  267. users = (users + issue.project.users.sort).uniq
  268. end
  269. users
  270. end
  271. def email_issue_attributes(issue, user, html)
  272. items = []
  273. %w(author status priority assigned_to category fixed_version).each do |attribute|
  274. unless issue.disabled_core_fields.include?(attribute+"_id")
  275. if html
  276. items << content_tag('strong', "#{l("field_#{attribute}")}: ") + (issue.send attribute)
  277. else
  278. items << "#{l("field_#{attribute}")}: #{issue.send attribute}"
  279. end
  280. end
  281. end
  282. issue.visible_custom_field_values(user).each do |value|
  283. if html
  284. items << content_tag('strong', "#{value.custom_field.name}: ") + show_value(value, false)
  285. else
  286. items << "#{value.custom_field.name}: #{show_value(value, false)}"
  287. end
  288. end
  289. items
  290. end
  291. def render_email_issue_attributes(issue, user, html=false)
  292. items = email_issue_attributes(issue, user, html)
  293. if html
  294. content_tag('ul', items.map{|s| content_tag('li', s)}.join("\n").html_safe, :class => "details")
  295. else
  296. items.map{|s| "* #{s}"}.join("\n")
  297. end
  298. end
  299. MultipleValuesDetail = Struct.new(:property, :prop_key, :custom_field, :old_value, :value)
  300. # Returns the textual representation of a journal details
  301. # as an array of strings
  302. def details_to_strings(details, no_html=false, options={})
  303. options[:only_path] = (options[:only_path] == false ? false : true)
  304. strings = []
  305. values_by_field = {}
  306. details.each do |detail|
  307. if detail.property == 'cf'
  308. field = detail.custom_field
  309. if field && field.multiple?
  310. values_by_field[field] ||= {:added => [], :deleted => []}
  311. if detail.old_value
  312. values_by_field[field][:deleted] << detail.old_value
  313. end
  314. if detail.value
  315. values_by_field[field][:added] << detail.value
  316. end
  317. next
  318. end
  319. end
  320. strings << show_detail(detail, no_html, options)
  321. end
  322. if values_by_field.present?
  323. values_by_field.each do |field, changes|
  324. if changes[:added].any?
  325. detail = MultipleValuesDetail.new('cf', field.id.to_s, field)
  326. detail.value = changes[:added]
  327. strings << show_detail(detail, no_html, options)
  328. end
  329. if changes[:deleted].any?
  330. detail = MultipleValuesDetail.new('cf', field.id.to_s, field)
  331. detail.old_value = changes[:deleted]
  332. strings << show_detail(detail, no_html, options)
  333. end
  334. end
  335. end
  336. strings
  337. end
  338. # Returns the textual representation of a single journal detail
  339. def show_detail(detail, no_html=false, options={})
  340. multiple = false
  341. show_diff = false
  342. no_details = false
  343. case detail.property
  344. when 'attr'
  345. field = detail.prop_key.to_s.gsub(/\_id$/, "")
  346. label = l(("field_" + field).to_sym)
  347. case detail.prop_key
  348. when 'due_date', 'start_date'
  349. value = format_date(detail.value.to_date) if detail.value
  350. old_value = format_date(detail.old_value.to_date) if detail.old_value
  351. when 'project_id', 'status_id', 'tracker_id', 'assigned_to_id',
  352. 'priority_id', 'category_id', 'fixed_version_id'
  353. value = find_name_by_reflection(field, detail.value)
  354. old_value = find_name_by_reflection(field, detail.old_value)
  355. when 'estimated_hours'
  356. value = l_hours_short(detail.value.to_f) unless detail.value.blank?
  357. old_value = l_hours_short(detail.old_value.to_f) unless detail.old_value.blank?
  358. when 'parent_id'
  359. label = l(:field_parent_issue)
  360. value = "##{detail.value}" unless detail.value.blank?
  361. old_value = "##{detail.old_value}" unless detail.old_value.blank?
  362. when 'is_private'
  363. value = l(detail.value == "0" ? :general_text_No : :general_text_Yes) unless detail.value.blank?
  364. old_value = l(detail.old_value == "0" ? :general_text_No : :general_text_Yes) unless detail.old_value.blank?
  365. when 'description'
  366. show_diff = true
  367. end
  368. when 'cf'
  369. custom_field = detail.custom_field
  370. if custom_field
  371. label = custom_field.name
  372. if custom_field.format.class.change_no_details
  373. no_details = true
  374. elsif custom_field.format.class.change_as_diff
  375. show_diff = true
  376. else
  377. multiple = custom_field.multiple?
  378. value = format_value(detail.value, custom_field) if detail.value
  379. old_value = format_value(detail.old_value, custom_field) if detail.old_value
  380. end
  381. end
  382. when 'attachment'
  383. label = l(:label_attachment)
  384. when 'relation'
  385. if detail.value && !detail.old_value
  386. rel_issue = Issue.visible.find_by_id(detail.value)
  387. value = rel_issue.nil? ? "#{l(:label_issue)} ##{detail.value}" :
  388. (no_html ? rel_issue : link_to_issue(rel_issue, :only_path => options[:only_path]))
  389. elsif detail.old_value && !detail.value
  390. rel_issue = Issue.visible.find_by_id(detail.old_value)
  391. old_value = rel_issue.nil? ? "#{l(:label_issue)} ##{detail.old_value}" :
  392. (no_html ? rel_issue : link_to_issue(rel_issue, :only_path => options[:only_path]))
  393. end
  394. relation_type = IssueRelation::TYPES[detail.prop_key]
  395. label = l(relation_type[:name]) if relation_type
  396. end
  397. call_hook(:helper_issues_show_detail_after_setting,
  398. {:detail => detail, :label => label, :value => value, :old_value => old_value })
  399. label ||= detail.prop_key
  400. value ||= detail.value
  401. old_value ||= detail.old_value
  402. unless no_html
  403. label = content_tag('strong', label)
  404. old_value = content_tag("i", h(old_value)) if detail.old_value
  405. if detail.old_value && detail.value.blank? && detail.property != 'relation'
  406. old_value = content_tag("del", old_value)
  407. end
  408. if detail.property == 'attachment' && value.present? &&
  409. atta = detail.journal.journalized.attachments.detect {|a| a.id == detail.prop_key.to_i}
  410. # Link to the attachment if it has not been removed
  411. value = link_to_attachment(atta, only_path: options[:only_path])
  412. if options[:only_path] != false
  413. value += ' '
  414. value += link_to_attachment atta, class: 'icon-only icon-download', title: l(:button_download), download: true
  415. end
  416. else
  417. value = content_tag("i", h(value)) if value
  418. end
  419. end
  420. if no_details
  421. s = l(:text_journal_changed_no_detail, :label => label).html_safe
  422. elsif show_diff
  423. s = l(:text_journal_changed_no_detail, :label => label)
  424. unless no_html
  425. diff_link = link_to 'diff',
  426. diff_journal_url(detail.journal_id, :detail_id => detail.id, :only_path => options[:only_path]),
  427. :title => l(:label_view_diff)
  428. s << " (#{ diff_link })"
  429. end
  430. s.html_safe
  431. elsif detail.value.present?
  432. case detail.property
  433. when 'attr', 'cf'
  434. if detail.old_value.present?
  435. l(:text_journal_changed, :label => label, :old => old_value, :new => value).html_safe
  436. elsif multiple
  437. l(:text_journal_added, :label => label, :value => value).html_safe
  438. else
  439. l(:text_journal_set_to, :label => label, :value => value).html_safe
  440. end
  441. when 'attachment', 'relation'
  442. l(:text_journal_added, :label => label, :value => value).html_safe
  443. end
  444. else
  445. l(:text_journal_deleted, :label => label, :old => old_value).html_safe
  446. end
  447. end
  448. # Find the name of an associated record stored in the field attribute
  449. def find_name_by_reflection(field, id)
  450. unless id.present?
  451. return nil
  452. end
  453. @detail_value_name_by_reflection ||= Hash.new do |hash, key|
  454. association = Issue.reflect_on_association(key.first.to_sym)
  455. name = nil
  456. if association
  457. record = association.klass.find_by_id(key.last)
  458. if record
  459. name = record.name.force_encoding('UTF-8')
  460. end
  461. end
  462. hash[key] = name
  463. end
  464. @detail_value_name_by_reflection[[field, id]]
  465. end
  466. # Renders issue children recursively
  467. def render_api_issue_children(issue, api)
  468. return if issue.leaf?
  469. api.array :children do
  470. issue.children.each do |child|
  471. api.issue(:id => child.id) do
  472. api.tracker(:id => child.tracker_id, :name => child.tracker.name) unless child.tracker.nil?
  473. api.subject child.subject
  474. render_api_issue_children(child, api)
  475. end
  476. end
  477. end
  478. end
  479. end