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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2011 Jean-Philippe Lang
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License
  6. # as published by the Free Software Foundation; either version 2
  7. # of the License, or (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. module IssuesHelper
  18. include ApplicationHelper
  19. def issue_list(issues, &block)
  20. ancestors = []
  21. issues.each do |issue|
  22. while (ancestors.any? && !issue.is_descendant_of?(ancestors.last))
  23. ancestors.pop
  24. end
  25. yield issue, ancestors.size
  26. ancestors << issue unless issue.leaf?
  27. end
  28. end
  29. # Renders a HTML/CSS tooltip
  30. #
  31. # To use, a trigger div is needed. This is a div with the class of "tooltip"
  32. # that contains this method wrapped in a span with the class of "tip"
  33. #
  34. # <div class="tooltip"><%= link_to_issue(issue) %>
  35. # <span class="tip"><%= render_issue_tooltip(issue) %></span>
  36. # </div>
  37. #
  38. def render_issue_tooltip(issue)
  39. @cached_label_status ||= l(:field_status)
  40. @cached_label_start_date ||= l(:field_start_date)
  41. @cached_label_due_date ||= l(:field_due_date)
  42. @cached_label_assigned_to ||= l(:field_assigned_to)
  43. @cached_label_priority ||= l(:field_priority)
  44. @cached_label_project ||= l(:field_project)
  45. (link_to_issue(issue) + "<br /><br />" +
  46. "<strong>#{@cached_label_project}</strong>: #{link_to_project(issue.project)}<br />" +
  47. "<strong>#{@cached_label_status}</strong>: #{h(issue.status.name)}<br />" +
  48. "<strong>#{@cached_label_start_date}</strong>: #{format_date(issue.start_date)}<br />" +
  49. "<strong>#{@cached_label_due_date}</strong>: #{format_date(issue.due_date)}<br />" +
  50. "<strong>#{@cached_label_assigned_to}</strong>: #{h(issue.assigned_to)}<br />" +
  51. "<strong>#{@cached_label_priority}</strong>: #{h(issue.priority.name)}").html_safe
  52. end
  53. def issue_heading(issue)
  54. h("#{issue.tracker} ##{issue.id}")
  55. end
  56. def render_issue_subject_with_tree(issue)
  57. s = ''
  58. ancestors = issue.root? ? [] : issue.ancestors.visible.all
  59. ancestors.each do |ancestor|
  60. s << '<div>' + content_tag('p', link_to_issue(ancestor))
  61. end
  62. s << '<div>'
  63. subject = h(issue.subject)
  64. if issue.is_private?
  65. subject = content_tag('span', l(:field_is_private), :class => 'private') + ' ' + subject
  66. end
  67. s << content_tag('h3', subject)
  68. s << '</div>' * (ancestors.size + 1)
  69. s.html_safe
  70. end
  71. def render_descendants_tree(issue)
  72. s = '<form><table class="list issues">'
  73. issue_list(issue.descendants.visible.sort_by(&:lft)) do |child, level|
  74. s << content_tag('tr',
  75. content_tag('td', check_box_tag("ids[]", child.id, false, :id => nil), :class => 'checkbox') +
  76. content_tag('td', link_to_issue(child, :truncate => 60), :class => 'subject') +
  77. content_tag('td', h(child.status)) +
  78. content_tag('td', link_to_user(child.assigned_to)) +
  79. content_tag('td', progress_bar(child.done_ratio, :width => '80px')),
  80. :class => "issue issue-#{child.id} hascontextmenu #{level > 0 ? "idnt idnt-#{level}" : nil}")
  81. end
  82. s << '</form></table>'
  83. s.html_safe
  84. end
  85. def render_custom_fields_rows(issue)
  86. return if issue.custom_field_values.empty?
  87. ordered_values = []
  88. half = (issue.custom_field_values.size / 2.0).ceil
  89. half.times do |i|
  90. ordered_values << issue.custom_field_values[i]
  91. ordered_values << issue.custom_field_values[i + half]
  92. end
  93. s = "<tr>\n"
  94. n = 0
  95. ordered_values.compact.each do |value|
  96. s << "</tr>\n<tr>\n" if n > 0 && (n % 2) == 0
  97. s << "\t<th>#{ h(value.custom_field.name) }:</th><td>#{ simple_format_without_paragraph(h(show_value(value))) }</td>\n"
  98. n += 1
  99. end
  100. s << "</tr>\n"
  101. s.html_safe
  102. end
  103. def issues_destroy_confirmation_message(issues)
  104. issues = [issues] unless issues.is_a?(Array)
  105. message = l(:text_issues_destroy_confirmation)
  106. descendant_count = issues.inject(0) {|memo, i| memo += (i.right - i.left - 1)/2}
  107. if descendant_count > 0
  108. issues.each do |issue|
  109. next if issue.root?
  110. issues.each do |other_issue|
  111. descendant_count -= 1 if issue.is_descendant_of?(other_issue)
  112. end
  113. end
  114. if descendant_count > 0
  115. message << "\n" + l(:text_issues_destroy_descendants_confirmation, :count => descendant_count)
  116. end
  117. end
  118. message
  119. end
  120. def sidebar_queries
  121. unless @sidebar_queries
  122. # User can see public queries and his own queries
  123. visible = ARCondition.new(["is_public = ? OR user_id = ?", true, (User.current.logged? ? User.current.id : 0)])
  124. # Project specific queries and global queries
  125. visible << (@project.nil? ? ["project_id IS NULL"] : ["project_id IS NULL OR project_id = ?", @project.id])
  126. @sidebar_queries = Query.find(:all,
  127. :select => 'id, name, is_public',
  128. :order => "name ASC",
  129. :conditions => visible.conditions)
  130. end
  131. @sidebar_queries
  132. end
  133. def query_links(title, queries)
  134. # links to #index on issues/show
  135. url_params = controller_name == 'issues' ? {:controller => 'issues', :action => 'index', :project_id => @project} : params
  136. content_tag('h3', h(title)) +
  137. queries.collect {|query|
  138. link_to(h(query.name), url_params.merge(:query_id => query))
  139. }.join('<br />')
  140. end
  141. def render_sidebar_queries
  142. out = ''
  143. queries = sidebar_queries.select {|q| !q.is_public?}
  144. out << query_links(l(:label_my_queries), queries) if queries.any?
  145. queries = sidebar_queries.select {|q| q.is_public?}
  146. out << query_links(l(:label_query_plural), queries) if queries.any?
  147. out
  148. end
  149. def show_detail(detail, no_html=false)
  150. case detail.property
  151. when 'attr'
  152. field = detail.prop_key.to_s.gsub(/\_id$/, "")
  153. label = l(("field_" + field).to_sym)
  154. case
  155. when ['due_date', 'start_date'].include?(detail.prop_key)
  156. value = format_date(detail.value.to_date) if detail.value
  157. old_value = format_date(detail.old_value.to_date) if detail.old_value
  158. when ['project_id', 'status_id', 'tracker_id', 'assigned_to_id', 'priority_id', 'category_id', 'fixed_version_id'].include?(detail.prop_key)
  159. value = find_name_by_reflection(field, detail.value)
  160. old_value = find_name_by_reflection(field, detail.old_value)
  161. when detail.prop_key == 'estimated_hours'
  162. value = "%0.02f" % detail.value.to_f unless detail.value.blank?
  163. old_value = "%0.02f" % detail.old_value.to_f unless detail.old_value.blank?
  164. when detail.prop_key == 'parent_id'
  165. label = l(:field_parent_issue)
  166. value = "##{detail.value}" unless detail.value.blank?
  167. old_value = "##{detail.old_value}" unless detail.old_value.blank?
  168. when detail.prop_key == 'is_private'
  169. value = l(detail.value == "0" ? :general_text_No : :general_text_Yes) unless detail.value.blank?
  170. old_value = l(detail.old_value == "0" ? :general_text_No : :general_text_Yes) unless detail.old_value.blank?
  171. end
  172. when 'cf'
  173. custom_field = CustomField.find_by_id(detail.prop_key)
  174. if custom_field
  175. label = custom_field.name
  176. value = format_value(detail.value, custom_field.field_format) if detail.value
  177. old_value = format_value(detail.old_value, custom_field.field_format) if detail.old_value
  178. end
  179. when 'attachment'
  180. label = l(:label_attachment)
  181. end
  182. call_hook(:helper_issues_show_detail_after_setting, {:detail => detail, :label => label, :value => value, :old_value => old_value })
  183. label ||= detail.prop_key
  184. value ||= detail.value
  185. old_value ||= detail.old_value
  186. unless no_html
  187. label = content_tag('strong', label)
  188. old_value = content_tag("i", h(old_value)) if detail.old_value
  189. old_value = content_tag("strike", old_value) if detail.old_value and detail.value.blank?
  190. if detail.property == 'attachment' && !value.blank? && a = Attachment.find_by_id(detail.prop_key)
  191. # Link to the attachment if it has not been removed
  192. value = link_to_attachment(a)
  193. else
  194. value = content_tag("i", h(value)) if value
  195. end
  196. end
  197. if detail.property == 'attr' && detail.prop_key == 'description'
  198. s = l(:text_journal_changed_no_detail, :label => label)
  199. unless no_html
  200. diff_link = link_to 'diff',
  201. {:controller => 'journals', :action => 'diff', :id => detail.journal_id, :detail_id => detail.id},
  202. :title => l(:label_view_diff)
  203. s << " (#{ diff_link })"
  204. end
  205. s
  206. elsif !detail.value.blank?
  207. case detail.property
  208. when 'attr', 'cf'
  209. if !detail.old_value.blank?
  210. l(:text_journal_changed, :label => label, :old => old_value, :new => value)
  211. else
  212. l(:text_journal_set_to, :label => label, :value => value)
  213. end
  214. when 'attachment'
  215. l(:text_journal_added, :label => label, :value => value)
  216. end
  217. else
  218. l(:text_journal_deleted, :label => label, :old => old_value)
  219. end
  220. end
  221. # Find the name of an associated record stored in the field attribute
  222. def find_name_by_reflection(field, id)
  223. association = Issue.reflect_on_association(field.to_sym)
  224. if association
  225. record = association.class_name.constantize.find_by_id(id)
  226. return record.name if record
  227. end
  228. end
  229. # Renders issue children recursively
  230. def render_api_issue_children(issue, api)
  231. return if issue.leaf?
  232. api.array :children do
  233. issue.children.each do |child|
  234. api.issue(:id => child.id) do
  235. api.tracker(:id => child.tracker_id, :name => child.tracker.name) unless child.tracker.nil?
  236. api.subject child.subject
  237. render_api_issue_children(child, api)
  238. end
  239. end
  240. end
  241. end
  242. def issues_to_csv(issues, project = nil)
  243. ic = Iconv.new(l(:general_csv_encoding), 'UTF-8')
  244. decimal_separator = l(:general_csv_decimal_separator)
  245. export = FCSV.generate(:col_sep => l(:general_csv_separator)) do |csv|
  246. # csv header fields
  247. headers = [ "#",
  248. l(:field_status),
  249. l(:field_project),
  250. l(:field_tracker),
  251. l(:field_priority),
  252. l(:field_subject),
  253. l(:field_assigned_to),
  254. l(:field_category),
  255. l(:field_fixed_version),
  256. l(:field_author),
  257. l(:field_start_date),
  258. l(:field_due_date),
  259. l(:field_done_ratio),
  260. l(:field_estimated_hours),
  261. l(:field_parent_issue),
  262. l(:field_created_on),
  263. l(:field_updated_on)
  264. ]
  265. # Export project custom fields if project is given
  266. # otherwise export custom fields marked as "For all projects"
  267. custom_fields = project.nil? ? IssueCustomField.for_all : project.all_issue_custom_fields
  268. custom_fields.each {|f| headers << f.name}
  269. # Description in the last column
  270. headers << l(:field_description)
  271. csv << headers.collect {|c| begin; ic.iconv(c.to_s); rescue; c.to_s; end }
  272. # csv lines
  273. issues.each do |issue|
  274. fields = [issue.id,
  275. issue.status.name,
  276. issue.project.name,
  277. issue.tracker.name,
  278. issue.priority.name,
  279. issue.subject,
  280. issue.assigned_to,
  281. issue.category,
  282. issue.fixed_version,
  283. issue.author.name,
  284. format_date(issue.start_date),
  285. format_date(issue.due_date),
  286. issue.done_ratio,
  287. issue.estimated_hours.to_s.gsub('.', decimal_separator),
  288. issue.parent_id,
  289. format_time(issue.created_on),
  290. format_time(issue.updated_on)
  291. ]
  292. custom_fields.each {|f| fields << show_value(issue.custom_value_for(f)) }
  293. fields << issue.description
  294. csv << fields.collect {|c| begin; ic.iconv(c.to_s); rescue; c.to_s; end }
  295. end
  296. end
  297. export
  298. end
  299. end