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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. # encoding: utf-8
  2. #
  3. # Redmine - project management software
  4. # Copyright (C) 2006-2013 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. def issue_list(issues, &block)
  22. ancestors = []
  23. issues.each do |issue|
  24. while (ancestors.any? && !issue.is_descendant_of?(ancestors.last))
  25. ancestors.pop
  26. end
  27. yield issue, ancestors.size
  28. ancestors << issue unless issue.leaf?
  29. end
  30. end
  31. # Renders a HTML/CSS tooltip
  32. #
  33. # To use, a trigger div is needed. This is a div with the class of "tooltip"
  34. # that contains this method wrapped in a span with the class of "tip"
  35. #
  36. # <div class="tooltip"><%= link_to_issue(issue) %>
  37. # <span class="tip"><%= render_issue_tooltip(issue) %></span>
  38. # </div>
  39. #
  40. def render_issue_tooltip(issue)
  41. @cached_label_status ||= l(:field_status)
  42. @cached_label_start_date ||= l(:field_start_date)
  43. @cached_label_due_date ||= l(:field_due_date)
  44. @cached_label_assigned_to ||= l(:field_assigned_to)
  45. @cached_label_priority ||= l(:field_priority)
  46. @cached_label_project ||= l(:field_project)
  47. link_to_issue(issue) + "<br /><br />".html_safe +
  48. "<strong>#{@cached_label_project}</strong>: #{link_to_project(issue.project)}<br />".html_safe +
  49. "<strong>#{@cached_label_status}</strong>: #{h(issue.status.name)}<br />".html_safe +
  50. "<strong>#{@cached_label_start_date}</strong>: #{format_date(issue.start_date)}<br />".html_safe +
  51. "<strong>#{@cached_label_due_date}</strong>: #{format_date(issue.due_date)}<br />".html_safe +
  52. "<strong>#{@cached_label_assigned_to}</strong>: #{h(issue.assigned_to)}<br />".html_safe +
  53. "<strong>#{@cached_label_priority}</strong>: #{h(issue.priority.name)}".html_safe
  54. end
  55. def issue_heading(issue)
  56. h("#{issue.tracker} ##{issue.id}")
  57. end
  58. def render_issue_subject_with_tree(issue)
  59. s = ''
  60. ancestors = issue.root? ? [] : issue.ancestors.visible.all
  61. ancestors.each do |ancestor|
  62. s << '<div>' + content_tag('p', link_to_issue(ancestor, :project => (issue.project_id != ancestor.project_id)))
  63. end
  64. s << '<div>'
  65. subject = h(issue.subject)
  66. if issue.is_private?
  67. subject = content_tag('span', l(:field_is_private), :class => 'private') + ' ' + subject
  68. end
  69. s << content_tag('h3', subject)
  70. s << '</div>' * (ancestors.size + 1)
  71. s.html_safe
  72. end
  73. def render_descendants_tree(issue)
  74. s = '<form><table class="list issues">'
  75. issue_list(issue.descendants.visible.sort_by(&:lft)) do |child, level|
  76. css = "issue issue-#{child.id} hascontextmenu"
  77. css << " idnt idnt-#{level}" if level > 0
  78. s << content_tag('tr',
  79. content_tag('td', check_box_tag("ids[]", child.id, false, :id => nil), :class => 'checkbox') +
  80. content_tag('td', link_to_issue(child, :truncate => 60, :project => (issue.project_id != child.project_id)), :class => 'subject') +
  81. content_tag('td', h(child.status)) +
  82. content_tag('td', link_to_user(child.assigned_to)) +
  83. content_tag('td', progress_bar(child.done_ratio, :width => '80px')),
  84. :class => css)
  85. end
  86. s << '</table></form>'
  87. s.html_safe
  88. end
  89. # Returns a link for adding a new subtask to the given issue
  90. def link_to_new_subtask(issue)
  91. attrs = {
  92. :tracker_id => issue.tracker,
  93. :parent_issue_id => issue
  94. }
  95. link_to(l(:button_add), new_project_issue_path(issue.project, :issue => attrs))
  96. end
  97. class IssueFieldsRows
  98. include ActionView::Helpers::TagHelper
  99. def initialize
  100. @left = []
  101. @right = []
  102. end
  103. def left(*args)
  104. args.any? ? @left << cells(*args) : @left
  105. end
  106. def right(*args)
  107. args.any? ? @right << cells(*args) : @right
  108. end
  109. def size
  110. @left.size > @right.size ? @left.size : @right.size
  111. end
  112. def to_html
  113. html = ''.html_safe
  114. blank = content_tag('th', '') + content_tag('td', '')
  115. size.times do |i|
  116. left = @left[i] || blank
  117. right = @right[i] || blank
  118. html << content_tag('tr', left + right)
  119. end
  120. html
  121. end
  122. def cells(label, text, options={})
  123. content_tag('th', "#{label}:", options) + content_tag('td', text, options)
  124. end
  125. end
  126. def issue_fields_rows
  127. r = IssueFieldsRows.new
  128. yield r
  129. r.to_html
  130. end
  131. def render_custom_fields_rows(issue)
  132. return if issue.custom_field_values.empty?
  133. ordered_values = []
  134. half = (issue.custom_field_values.size / 2.0).ceil
  135. half.times do |i|
  136. ordered_values << issue.custom_field_values[i]
  137. ordered_values << issue.custom_field_values[i + half]
  138. end
  139. s = "<tr>\n"
  140. n = 0
  141. ordered_values.compact.each do |value|
  142. s << "</tr>\n<tr>\n" if n > 0 && (n % 2) == 0
  143. s << "\t<th>#{ h(value.custom_field.name) }:</th><td>#{ simple_format_without_paragraph(h(show_value(value))) }</td>\n"
  144. n += 1
  145. end
  146. s << "</tr>\n"
  147. s.html_safe
  148. end
  149. def issues_destroy_confirmation_message(issues)
  150. issues = [issues] unless issues.is_a?(Array)
  151. message = l(:text_issues_destroy_confirmation)
  152. descendant_count = issues.inject(0) {|memo, i| memo += (i.right - i.left - 1)/2}
  153. if descendant_count > 0
  154. issues.each do |issue|
  155. next if issue.root?
  156. issues.each do |other_issue|
  157. descendant_count -= 1 if issue.is_descendant_of?(other_issue)
  158. end
  159. end
  160. if descendant_count > 0
  161. message << "\n" + l(:text_issues_destroy_descendants_confirmation, :count => descendant_count)
  162. end
  163. end
  164. message
  165. end
  166. def sidebar_queries
  167. unless @sidebar_queries
  168. @sidebar_queries = IssueQuery.visible.all(
  169. :order => "#{Query.table_name}.name ASC",
  170. # Project specific queries and global queries
  171. :conditions => (@project.nil? ? ["project_id IS NULL"] : ["project_id IS NULL OR project_id = ?", @project.id])
  172. )
  173. end
  174. @sidebar_queries
  175. end
  176. def query_links(title, queries)
  177. # links to #index on issues/show
  178. url_params = controller_name == 'issues' ? {:controller => 'issues', :action => 'index', :project_id => @project} : params
  179. content_tag('h3', h(title)) +
  180. queries.collect {|query|
  181. css = 'query'
  182. css << ' selected' if query == @query
  183. link_to(h(query.name), url_params.merge(:query_id => query), :class => css)
  184. }.join('<br />').html_safe
  185. end
  186. def render_sidebar_queries
  187. out = ''.html_safe
  188. queries = sidebar_queries.select {|q| !q.is_public?}
  189. out << query_links(l(:label_my_queries), queries) if queries.any?
  190. queries = sidebar_queries.select {|q| q.is_public?}
  191. out << query_links(l(:label_query_plural), queries) if queries.any?
  192. out
  193. end
  194. # Returns the textual representation of a journal details
  195. # as an array of strings
  196. def details_to_strings(details, no_html=false, options={})
  197. options[:only_path] = (options[:only_path] == false ? false : true)
  198. strings = []
  199. values_by_field = {}
  200. details.each do |detail|
  201. if detail.property == 'cf'
  202. field_id = detail.prop_key
  203. field = CustomField.find_by_id(field_id)
  204. if field && field.multiple?
  205. values_by_field[field_id] ||= {:added => [], :deleted => []}
  206. if detail.old_value
  207. values_by_field[field_id][:deleted] << detail.old_value
  208. end
  209. if detail.value
  210. values_by_field[field_id][:added] << detail.value
  211. end
  212. next
  213. end
  214. end
  215. strings << show_detail(detail, no_html, options)
  216. end
  217. values_by_field.each do |field_id, changes|
  218. detail = JournalDetail.new(:property => 'cf', :prop_key => field_id)
  219. if changes[:added].any?
  220. detail.value = changes[:added]
  221. strings << show_detail(detail, no_html, options)
  222. elsif changes[:deleted].any?
  223. detail.old_value = changes[:deleted]
  224. strings << show_detail(detail, no_html, options)
  225. end
  226. end
  227. strings
  228. end
  229. # Returns the textual representation of a single journal detail
  230. def show_detail(detail, no_html=false, options={})
  231. multiple = false
  232. case detail.property
  233. when 'attr'
  234. field = detail.prop_key.to_s.gsub(/\_id$/, "")
  235. label = l(("field_" + field).to_sym)
  236. case detail.prop_key
  237. when 'due_date', 'start_date'
  238. value = format_date(detail.value.to_date) if detail.value
  239. old_value = format_date(detail.old_value.to_date) if detail.old_value
  240. when 'project_id', 'status_id', 'tracker_id', 'assigned_to_id',
  241. 'priority_id', 'category_id', 'fixed_version_id'
  242. value = find_name_by_reflection(field, detail.value)
  243. old_value = find_name_by_reflection(field, detail.old_value)
  244. when 'estimated_hours'
  245. value = "%0.02f" % detail.value.to_f unless detail.value.blank?
  246. old_value = "%0.02f" % detail.old_value.to_f unless detail.old_value.blank?
  247. when 'parent_id'
  248. label = l(:field_parent_issue)
  249. value = "##{detail.value}" unless detail.value.blank?
  250. old_value = "##{detail.old_value}" unless detail.old_value.blank?
  251. when 'is_private'
  252. value = l(detail.value == "0" ? :general_text_No : :general_text_Yes) unless detail.value.blank?
  253. old_value = l(detail.old_value == "0" ? :general_text_No : :general_text_Yes) unless detail.old_value.blank?
  254. end
  255. when 'cf'
  256. custom_field = CustomField.find_by_id(detail.prop_key)
  257. if custom_field
  258. multiple = custom_field.multiple?
  259. label = custom_field.name
  260. value = format_value(detail.value, custom_field.field_format) if detail.value
  261. old_value = format_value(detail.old_value, custom_field.field_format) if detail.old_value
  262. end
  263. when 'attachment'
  264. label = l(:label_attachment)
  265. end
  266. call_hook(:helper_issues_show_detail_after_setting,
  267. {:detail => detail, :label => label, :value => value, :old_value => old_value })
  268. label ||= detail.prop_key
  269. value ||= detail.value
  270. old_value ||= detail.old_value
  271. unless no_html
  272. label = content_tag('strong', label)
  273. old_value = content_tag("i", h(old_value)) if detail.old_value
  274. old_value = content_tag("del", old_value) if detail.old_value and detail.value.blank?
  275. if detail.property == 'attachment' && !value.blank? && atta = Attachment.find_by_id(detail.prop_key)
  276. # Link to the attachment if it has not been removed
  277. value = link_to_attachment(atta, :download => true, :only_path => options[:only_path])
  278. if options[:only_path] != false && atta.is_text?
  279. value += link_to(
  280. image_tag('magnifier.png'),
  281. :controller => 'attachments', :action => 'show',
  282. :id => atta, :filename => atta.filename
  283. )
  284. end
  285. else
  286. value = content_tag("i", h(value)) if value
  287. end
  288. end
  289. if detail.property == 'attr' && detail.prop_key == 'description'
  290. s = l(:text_journal_changed_no_detail, :label => label)
  291. unless no_html
  292. diff_link = link_to 'diff',
  293. {:controller => 'journals', :action => 'diff', :id => detail.journal_id,
  294. :detail_id => detail.id, :only_path => options[:only_path]},
  295. :title => l(:label_view_diff)
  296. s << " (#{ diff_link })"
  297. end
  298. s.html_safe
  299. elsif detail.value.present?
  300. case detail.property
  301. when 'attr', 'cf'
  302. if detail.old_value.present?
  303. l(:text_journal_changed, :label => label, :old => old_value, :new => value).html_safe
  304. elsif multiple
  305. l(:text_journal_added, :label => label, :value => value).html_safe
  306. else
  307. l(:text_journal_set_to, :label => label, :value => value).html_safe
  308. end
  309. when 'attachment'
  310. l(:text_journal_added, :label => label, :value => value).html_safe
  311. end
  312. else
  313. l(:text_journal_deleted, :label => label, :old => old_value).html_safe
  314. end
  315. end
  316. # Find the name of an associated record stored in the field attribute
  317. def find_name_by_reflection(field, id)
  318. unless id.present?
  319. return nil
  320. end
  321. association = Issue.reflect_on_association(field.to_sym)
  322. if association
  323. record = association.class_name.constantize.find_by_id(id)
  324. return record.name if record
  325. end
  326. end
  327. # Renders issue children recursively
  328. def render_api_issue_children(issue, api)
  329. return if issue.leaf?
  330. api.array :children do
  331. issue.children.each do |child|
  332. api.issue(:id => child.id) do
  333. api.tracker(:id => child.tracker_id, :name => child.tracker.name) unless child.tracker.nil?
  334. api.subject child.subject
  335. render_api_issue_children(child, api)
  336. end
  337. end
  338. end
  339. end
  340. def issues_to_csv(issues, project, query, options={})
  341. decimal_separator = l(:general_csv_decimal_separator)
  342. encoding = l(:general_csv_encoding)
  343. columns = (options[:columns] == 'all' ? query.available_inline_columns : query.inline_columns)
  344. if options[:description]
  345. if description = query.available_columns.detect {|q| q.name == :description}
  346. columns << description
  347. end
  348. end
  349. export = FCSV.generate(:col_sep => l(:general_csv_separator)) do |csv|
  350. # csv header fields
  351. csv << [ "#" ] + columns.collect {|c| Redmine::CodesetUtil.from_utf8(c.caption.to_s, encoding) }
  352. # csv lines
  353. issues.each do |issue|
  354. col_values = columns.collect do |column|
  355. s = if column.is_a?(QueryCustomFieldColumn)
  356. cv = issue.custom_field_values.detect {|v| v.custom_field_id == column.custom_field.id}
  357. show_value(cv)
  358. else
  359. value = column.value(issue)
  360. if value.is_a?(Date)
  361. format_date(value)
  362. elsif value.is_a?(Time)
  363. format_time(value)
  364. elsif value.is_a?(Float)
  365. ("%.2f" % value).gsub('.', decimal_separator)
  366. else
  367. value
  368. end
  369. end
  370. s.to_s
  371. end
  372. csv << [ issue.id.to_s ] + col_values.collect {|c| Redmine::CodesetUtil.from_utf8(c.to_s, encoding) }
  373. end
  374. end
  375. export
  376. end
  377. end