PageRenderTime 52ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/app/helpers/issues_helper.rb

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