Chris@0: # redMine - project management software Chris@0: # Copyright (C) 2006 Jean-Philippe Lang Chris@0: # Chris@0: # This program is free software; you can redistribute it and/or Chris@0: # modify it under the terms of the GNU General Public License Chris@0: # as published by the Free Software Foundation; either version 2 Chris@0: # of the License, or (at your option) any later version. Chris@0: # Chris@0: # This program is distributed in the hope that it will be useful, Chris@0: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@0: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@0: # GNU General Public License for more details. Chris@0: # Chris@0: # You should have received a copy of the GNU General Public License Chris@0: # along with this program; if not, write to the Free Software Chris@0: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@0: Chris@0: module IssuesHelper Chris@0: include ApplicationHelper Chris@0: Chris@0: def issue_list(issues, &block) Chris@0: ancestors = [] Chris@0: issues.each do |issue| Chris@0: while (ancestors.any? && !issue.is_descendant_of?(ancestors.last)) Chris@0: ancestors.pop Chris@0: end Chris@0: yield issue, ancestors.size Chris@0: ancestors << issue unless issue.leaf? Chris@0: end Chris@0: end chris@22: chris@22: # Renders a HTML/CSS tooltip chris@22: # chris@22: # To use, a trigger div is needed. This is a div with the class of "tooltip" chris@22: # that contains this method wrapped in a span with the class of "tip" chris@22: # chris@22: #
<%= link_to_issue(issue) %> chris@22: # <%= render_issue_tooltip(issue) %> chris@22: #
chris@22: # Chris@0: def render_issue_tooltip(issue) Chris@14: @cached_label_status ||= l(:field_status) Chris@0: @cached_label_start_date ||= l(:field_start_date) Chris@0: @cached_label_due_date ||= l(:field_due_date) Chris@0: @cached_label_assigned_to ||= l(:field_assigned_to) Chris@0: @cached_label_priority ||= l(:field_priority) chris@22: @cached_label_project ||= l(:field_project) chris@22: Chris@0: link_to_issue(issue) + "

" + chris@22: "#{@cached_label_project}: #{link_to_project(issue.project)}
" + Chris@14: "#{@cached_label_status}: #{issue.status.name}
" + Chris@0: "#{@cached_label_start_date}: #{format_date(issue.start_date)}
" + Chris@0: "#{@cached_label_due_date}: #{format_date(issue.due_date)}
" + Chris@0: "#{@cached_label_assigned_to}: #{issue.assigned_to}
" + Chris@0: "#{@cached_label_priority}: #{issue.priority.name}" Chris@0: end Chris@0: Chris@0: def render_issue_subject_with_tree(issue) Chris@0: s = '' Chris@0: issue.ancestors.each do |ancestor| Chris@0: s << '
' + content_tag('p', link_to_issue(ancestor)) Chris@0: end Chris@0: s << '
' + content_tag('h3', h(issue.subject)) Chris@0: s << '
' * (issue.ancestors.size + 1) Chris@0: s Chris@0: end Chris@0: Chris@0: def render_descendants_tree(issue) Chris@0: s = '
' Chris@0: issue_list(issue.descendants.sort_by(&:lft)) do |child, level| Chris@0: s << content_tag('tr', Chris@0: content_tag('td', check_box_tag("ids[]", child.id, false, :id => nil), :class => 'checkbox') + Chris@0: content_tag('td', link_to_issue(child, :truncate => 60), :class => 'subject') + Chris@0: content_tag('td', h(child.status)) + Chris@0: content_tag('td', link_to_user(child.assigned_to)) + Chris@0: content_tag('td', progress_bar(child.done_ratio, :width => '80px')), Chris@0: :class => "issue issue-#{child.id} hascontextmenu #{level > 0 ? "idnt idnt-#{level}" : nil}") Chris@0: end Chris@0: s << '
' Chris@0: s Chris@0: end Chris@0: Chris@0: def render_custom_fields_rows(issue) Chris@0: return if issue.custom_field_values.empty? Chris@0: ordered_values = [] Chris@0: half = (issue.custom_field_values.size / 2.0).ceil Chris@0: half.times do |i| Chris@0: ordered_values << issue.custom_field_values[i] Chris@0: ordered_values << issue.custom_field_values[i + half] Chris@0: end Chris@0: s = "\n" Chris@0: n = 0 Chris@0: ordered_values.compact.each do |value| Chris@0: s << "\n\n" if n > 0 && (n % 2) == 0 Chris@0: s << "\t#{ h(value.custom_field.name) }:#{ simple_format_without_paragraph(h(show_value(value))) }\n" Chris@0: n += 1 Chris@0: end Chris@0: s << "\n" Chris@0: s Chris@0: end Chris@0: Chris@0: def sidebar_queries Chris@0: unless @sidebar_queries Chris@0: # User can see public queries and his own queries Chris@0: visible = ARCondition.new(["is_public = ? OR user_id = ?", true, (User.current.logged? ? User.current.id : 0)]) Chris@0: # Project specific queries and global queries Chris@0: visible << (@project.nil? ? ["project_id IS NULL"] : ["project_id IS NULL OR project_id = ?", @project.id]) Chris@0: @sidebar_queries = Query.find(:all, Chris@0: :select => 'id, name', Chris@0: :order => "name ASC", Chris@0: :conditions => visible.conditions) Chris@0: end Chris@0: @sidebar_queries Chris@0: end Chris@0: Chris@0: def show_detail(detail, no_html=false) Chris@0: case detail.property Chris@0: when 'attr' Chris@0: field = detail.prop_key.to_s.gsub(/\_id$/, "") Chris@0: label = l(("field_" + field).to_sym) Chris@0: case Chris@0: when ['due_date', 'start_date'].include?(detail.prop_key) Chris@0: value = format_date(detail.value.to_date) if detail.value Chris@0: old_value = format_date(detail.old_value.to_date) if detail.old_value Chris@0: Chris@0: when ['project_id', 'status_id', 'tracker_id', 'assigned_to_id', 'priority_id', 'category_id', 'fixed_version_id'].include?(detail.prop_key) Chris@0: value = find_name_by_reflection(field, detail.value) Chris@0: old_value = find_name_by_reflection(field, detail.old_value) Chris@0: Chris@0: when detail.prop_key == 'estimated_hours' Chris@0: value = "%0.02f" % detail.value.to_f unless detail.value.blank? Chris@0: old_value = "%0.02f" % detail.old_value.to_f unless detail.old_value.blank? Chris@0: Chris@0: when detail.prop_key == 'parent_id' Chris@0: label = l(:field_parent_issue) Chris@0: value = "##{detail.value}" unless detail.value.blank? Chris@0: old_value = "##{detail.old_value}" unless detail.old_value.blank? Chris@0: end Chris@0: when 'cf' Chris@0: custom_field = CustomField.find_by_id(detail.prop_key) Chris@0: if custom_field Chris@0: label = custom_field.name Chris@0: value = format_value(detail.value, custom_field.field_format) if detail.value Chris@0: old_value = format_value(detail.old_value, custom_field.field_format) if detail.old_value Chris@0: end Chris@0: when 'attachment' Chris@0: label = l(:label_attachment) Chris@0: end Chris@0: call_hook(:helper_issues_show_detail_after_setting, {:detail => detail, :label => label, :value => value, :old_value => old_value }) Chris@0: Chris@0: label ||= detail.prop_key Chris@0: value ||= detail.value Chris@0: old_value ||= detail.old_value Chris@0: Chris@0: unless no_html Chris@0: label = content_tag('strong', label) Chris@0: old_value = content_tag("i", h(old_value)) if detail.old_value Chris@0: old_value = content_tag("strike", old_value) if detail.old_value and (!detail.value or detail.value.empty?) Chris@0: if detail.property == 'attachment' && !value.blank? && a = Attachment.find_by_id(detail.prop_key) Chris@0: # Link to the attachment if it has not been removed Chris@0: value = link_to_attachment(a) Chris@0: else Chris@0: value = content_tag("i", h(value)) if value Chris@0: end Chris@0: end Chris@0: Chris@0: if !detail.value.blank? Chris@0: case detail.property Chris@0: when 'attr', 'cf' Chris@0: if !detail.old_value.blank? Chris@0: l(:text_journal_changed, :label => label, :old => old_value, :new => value) Chris@0: else Chris@0: l(:text_journal_set_to, :label => label, :value => value) Chris@0: end Chris@0: when 'attachment' Chris@0: l(:text_journal_added, :label => label, :value => value) Chris@0: end Chris@0: else Chris@0: l(:text_journal_deleted, :label => label, :old => old_value) Chris@0: end Chris@0: end Chris@0: Chris@0: # Find the name of an associated record stored in the field attribute Chris@0: def find_name_by_reflection(field, id) Chris@0: association = Issue.reflect_on_association(field.to_sym) Chris@0: if association Chris@0: record = association.class_name.constantize.find_by_id(id) Chris@0: return record.name if record Chris@0: end Chris@0: end Chris@0: Chris@117: # Renders issue children recursively Chris@117: def render_api_issue_children(issue, api) Chris@117: return if issue.leaf? Chris@117: api.array :children do Chris@117: issue.children.each do |child| Chris@117: api.issue(:id => child.id) do Chris@117: api.tracker(:id => child.tracker_id, :name => child.tracker.name) unless child.tracker.nil? Chris@117: api.subject child.subject Chris@117: render_api_issue_children(child, api) Chris@117: end Chris@117: end Chris@117: end Chris@117: end Chris@117: Chris@0: def issues_to_csv(issues, project = nil) Chris@0: ic = Iconv.new(l(:general_csv_encoding), 'UTF-8') Chris@0: decimal_separator = l(:general_csv_decimal_separator) Chris@0: export = FCSV.generate(:col_sep => l(:general_csv_separator)) do |csv| Chris@0: # csv header fields Chris@0: headers = [ "#", Chris@0: l(:field_status), Chris@0: l(:field_project), Chris@0: l(:field_tracker), Chris@0: l(:field_priority), Chris@0: l(:field_subject), Chris@0: l(:field_assigned_to), Chris@0: l(:field_category), Chris@0: l(:field_fixed_version), Chris@0: l(:field_author), Chris@0: l(:field_start_date), Chris@0: l(:field_due_date), Chris@0: l(:field_done_ratio), Chris@0: l(:field_estimated_hours), Chris@0: l(:field_parent_issue), Chris@0: l(:field_created_on), Chris@0: l(:field_updated_on) Chris@0: ] Chris@0: # Export project custom fields if project is given Chris@0: # otherwise export custom fields marked as "For all projects" Chris@0: custom_fields = project.nil? ? IssueCustomField.for_all : project.all_issue_custom_fields Chris@0: custom_fields.each {|f| headers << f.name} Chris@0: # Description in the last column Chris@0: headers << l(:field_description) Chris@0: csv << headers.collect {|c| begin; ic.iconv(c.to_s); rescue; c.to_s; end } Chris@0: # csv lines Chris@0: issues.each do |issue| Chris@0: fields = [issue.id, Chris@0: issue.status.name, Chris@0: issue.project.name, Chris@0: issue.tracker.name, Chris@0: issue.priority.name, Chris@0: issue.subject, Chris@0: issue.assigned_to, Chris@0: issue.category, Chris@0: issue.fixed_version, Chris@0: issue.author.name, Chris@0: format_date(issue.start_date), Chris@0: format_date(issue.due_date), Chris@0: issue.done_ratio, Chris@0: issue.estimated_hours.to_s.gsub('.', decimal_separator), Chris@0: issue.parent_id, Chris@0: format_time(issue.created_on), Chris@0: format_time(issue.updated_on) Chris@0: ] Chris@0: custom_fields.each {|f| fields << show_value(issue.custom_value_for(f)) } Chris@0: fields << issue.description Chris@0: csv << fields.collect {|c| begin; ic.iconv(c.to_s); rescue; c.to_s; end } Chris@0: end Chris@0: end Chris@0: export Chris@0: end Chris@0: end