;]
[askyou.git] / app / helpers / issues_helper.rb
blob2e175f876928543249503111c446a0e6c0071527
1 # redMine - project management software
2 # Copyright (C) 2006  Jean-Philippe Lang
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.
18 module IssuesHelper
19   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
32   # Renders a HTML/CSS tooltip
33   #
34   # To use, a trigger div is needed.  This is a div with the class of "tooltip"
35   # that contains this method wrapped in a span with the class of "tip"
36   #
37   #    <div class="tooltip"><%= link_to_issue(issue) %>
38   #      <span class="tip"><%= render_issue_tooltip(issue) %></span>
39   #    </div>
40   #
41   def render_issue_tooltip(issue)
42     @cached_label_status ||= l(:field_status)
43     @cached_label_start_date ||= l(:field_start_date)
44     @cached_label_due_date ||= l(:field_due_date)
45     @cached_label_assigned_to ||= l(:field_assigned_to)
46     @cached_label_priority ||= l(:field_priority)
47     
48     link_to_issue(issue) + "<br /><br />" +
49       "<strong>#{@cached_label_status}</strong>: #{issue.status.name}<br />" +
50       "<strong>#{@cached_label_start_date}</strong>: #{format_date(issue.start_date)}<br />" +
51       "<strong>#{@cached_label_due_date}</strong>: #{format_date(issue.due_date)}<br />" +
52       "<strong>#{@cached_label_assigned_to}</strong>: #{issue.assigned_to}<br />" +
53       "<strong>#{@cached_label_priority}</strong>: #{issue.priority.name}"
54   end
55     
56   def render_issue_subject_with_tree(issue)
57     s = ''
58     issue.ancestors.each do |ancestor|
59       s << '<div>' + content_tag('p', link_to_issue(ancestor))
60     end
61     s << '<div>' + content_tag('h3', h(issue.subject))
62     s << '</div>' * (issue.ancestors.size + 1)
63     s
64   end
65   
66   def render_descendants_tree(issue)
67     s = '<form><table class="list issues">'
68     issue_list(issue.descendants.sort_by(&:lft)) do |child, level|
69       s << content_tag('tr',
70              content_tag('td', check_box_tag("ids[]", child.id, false, :id => nil), :class => 'checkbox') +
71              content_tag('td', link_to_issue(child, :truncate => 60), :class => 'subject') +
72              content_tag('td', h(child.status)) +
73              content_tag('td', link_to_user(child.assigned_to)) +
74              content_tag('td', progress_bar(child.done_ratio, :width => '80px')),
75              :class => "issue issue-#{child.id} hascontextmenu #{level > 0 ? "idnt idnt-#{level}" : nil}")
76     end
77     s << '</form></table>'
78     s
79   end
80   
81   def render_custom_fields_rows(issue)
82     return if issue.custom_field_values.empty?
83     ordered_values = []
84     half = (issue.custom_field_values.size / 2.0).ceil
85     half.times do |i|
86       ordered_values << issue.custom_field_values[i]
87       ordered_values << issue.custom_field_values[i + half]
88     end
89     s = "<tr>\n"
90     n = 0
91     ordered_values.compact.each do |value|
92       s << "</tr>\n<tr>\n" if n > 0 && (n % 2) == 0
93       s << "\t<th>#{ h(value.custom_field.name) }:</th><td>#{ simple_format_without_paragraph(h(show_value(value))) }</td>\n"
94       n += 1
95     end
96     s << "</tr>\n"
97     s
98   end
99   
100   def sidebar_queries
101     unless @sidebar_queries
102       # User can see public queries and his own queries
103       visible = ARCondition.new(["is_public = ? OR user_id = ?", true, (User.current.logged? ? User.current.id : 0)])
104       # Project specific queries and global queries
105       visible << (@project.nil? ? ["project_id IS NULL"] : ["project_id IS NULL OR project_id = ?", @project.id])
106       @sidebar_queries = Query.find(:all, 
107                                     :select => 'id, name',
108                                     :order => "name ASC",
109                                     :conditions => visible.conditions)
110     end
111     @sidebar_queries
112   end
114   def show_detail(detail, no_html=false)
115     case detail.property
116     when 'attr'
117       field = detail.prop_key.to_s.gsub(/\_id$/, "")
118       label = l(("field_" + field).to_sym)
119       case
120       when ['due_date', 'start_date'].include?(detail.prop_key)
121         value = format_date(detail.value.to_date) if detail.value
122         old_value = format_date(detail.old_value.to_date) if detail.old_value
124       when ['project_id', 'status_id', 'tracker_id', 'assigned_to_id', 'priority_id', 'category_id', 'fixed_version_id'].include?(detail.prop_key)
125         value = find_name_by_reflection(field, detail.value)
126         old_value = find_name_by_reflection(field, detail.old_value)
128       when detail.prop_key == 'estimated_hours'
129         value = "%0.02f" % detail.value.to_f unless detail.value.blank?
130         old_value = "%0.02f" % detail.old_value.to_f unless detail.old_value.blank?
132       when detail.prop_key == 'parent_id'
133         label = l(:field_parent_issue)
134         value = "##{detail.value}" unless detail.value.blank?
135         old_value = "##{detail.old_value}" unless detail.old_value.blank?
136       end
137     when 'cf'
138       custom_field = CustomField.find_by_id(detail.prop_key)
139       if custom_field
140         label = custom_field.name
141         value = format_value(detail.value, custom_field.field_format) if detail.value
142         old_value = format_value(detail.old_value, custom_field.field_format) if detail.old_value
143       end
144     when 'attachment'
145       label = l(:label_attachment)
146     end
147     call_hook(:helper_issues_show_detail_after_setting, {:detail => detail, :label => label, :value => value, :old_value => old_value })
149     label ||= detail.prop_key
150     value ||= detail.value
151     old_value ||= detail.old_value
152     
153     unless no_html
154       label = content_tag('strong', label)
155       old_value = content_tag("i", h(old_value)) if detail.old_value
156       old_value = content_tag("strike", old_value) if detail.old_value and (!detail.value or detail.value.empty?)
157       if detail.property == 'attachment' && !value.blank? && a = Attachment.find_by_id(detail.prop_key)
158         # Link to the attachment if it has not been removed
159         value = link_to_attachment(a)
160       else
161         value = content_tag("i", h(value)) if value
162       end
163     end
164     
165     if !detail.value.blank?
166       case detail.property
167       when 'attr', 'cf'
168         if !detail.old_value.blank?
169           l(:text_journal_changed, :label => label, :old => old_value, :new => value)
170         else
171           l(:text_journal_set_to, :label => label, :value => value)
172         end
173       when 'attachment'
174         l(:text_journal_added, :label => label, :value => value)
175       end
176     else
177       l(:text_journal_deleted, :label => label, :old => old_value)
178     end
179   end
181   # Find the name of an associated record stored in the field attribute
182   def find_name_by_reflection(field, id)
183     association = Issue.reflect_on_association(field.to_sym)
184     if association
185       record = association.class_name.constantize.find_by_id(id)
186       return record.name if record
187     end
188   end
189   
190   def issues_to_csv(issues, project = nil)
191     ic = Iconv.new(l(:general_csv_encoding), 'UTF-8')    
192     decimal_separator = l(:general_csv_decimal_separator)
193     export = FCSV.generate(:col_sep => l(:general_csv_separator)) do |csv|
194       # csv header fields
195       headers = [ "#",
196                   l(:field_status), 
197                   l(:field_project),
198                   l(:field_tracker),
199                   l(:field_priority),
200                   l(:field_subject),
201                   l(:field_assigned_to),
202                   l(:field_category),
203                   l(:field_fixed_version),
204                   l(:field_author),
205                   l(:field_start_date),
206                   l(:field_due_date),
207                   l(:field_done_ratio),
208                   l(:field_estimated_hours),
209                   l(:field_parent_issue),
210                   l(:field_created_on),
211                   l(:field_updated_on)
212                   ]
213       # Export project custom fields if project is given
214       # otherwise export custom fields marked as "For all projects"
215       custom_fields = project.nil? ? IssueCustomField.for_all : project.all_issue_custom_fields
216       custom_fields.each {|f| headers << f.name}
217       # Description in the last column
218       headers << l(:field_description)
219       csv << headers.collect {|c| begin; ic.iconv(c.to_s); rescue; c.to_s; end }
220       # csv lines
221       issues.each do |issue|
222         fields = [issue.id,
223                   issue.status.name, 
224                   issue.project.name,
225                   issue.tracker.name, 
226                   issue.priority.name,
227                   issue.subject,
228                   issue.assigned_to,
229                   issue.category,
230                   issue.fixed_version,
231                   issue.author.name,
232                   format_date(issue.start_date),
233                   format_date(issue.due_date),
234                   issue.done_ratio,
235                   issue.estimated_hours.to_s.gsub('.', decimal_separator),
236                   issue.parent_id,
237                   format_time(issue.created_on),  
238                   format_time(issue.updated_on)
239                   ]
240         custom_fields.each {|f| fields << show_value(issue.custom_value_for(f)) }
241         fields << issue.description
242         csv << fields.collect {|c| begin; ic.iconv(c.to_s); rescue; c.to_s; end }
243       end
244     end
245     export
246   end
248   def gantt_zoom_link(gantt, in_or_out)
249     img_attributes = {:style => 'height:1.4em; width:1.4em; margin-left: 3px;'} # em for accessibility
251     case in_or_out
252     when :in
253       if gantt.zoom < 4
254         link_to_remote(l(:text_zoom_in) + image_tag('zoom_in.png', img_attributes.merge(:alt => l(:text_zoom_in))),
255                        {:url => gantt.params.merge(:zoom => (gantt.zoom+1)), :update => 'content'},
256                        {:href => url_for(gantt.params.merge(:zoom => (gantt.zoom+1)))})
257       else
258         l(:text_zoom_in) +
259           image_tag('zoom_in_g.png', img_attributes.merge(:alt => l(:text_zoom_in)))
260       end
261       
262     when :out
263       if gantt.zoom > 1
264         link_to_remote(l(:text_zoom_out) + image_tag('zoom_out.png', img_attributes.merge(:alt => l(:text_zoom_out))),
265                        {:url => gantt.params.merge(:zoom => (gantt.zoom-1)), :update => 'content'},
266                        {:href => url_for(gantt.params.merge(:zoom => (gantt.zoom-1)))})
267       else
268         l(:text_zoom_out) +
269           image_tag('zoom_out_g.png', img_attributes.merge(:alt => l(:text_zoom_out)))
270       end
271     end
272   end