@@ -1,729 +1,732 | |||
|
1 | 1 | # redMine - project management software |
|
2 | 2 | # Copyright (C) 2006-2007 Jean-Philippe Lang |
|
3 | 3 | # |
|
4 | 4 | # This program is free software; you can redistribute it and/or |
|
5 | 5 | # modify it under the terms of the GNU General Public License |
|
6 | 6 | # as published by the Free Software Foundation; either version 2 |
|
7 | 7 | # of the License, or (at your option) any later version. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU General Public License |
|
15 | 15 | # along with this program; if not, write to the Free Software |
|
16 | 16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
17 | 17 | |
|
18 | 18 | require 'coderay' |
|
19 | 19 | require 'coderay/helpers/file_type' |
|
20 | 20 | require 'forwardable' |
|
21 | 21 | require 'cgi' |
|
22 | 22 | |
|
23 | 23 | module ApplicationHelper |
|
24 | 24 | include Redmine::WikiFormatting::Macros::Definitions |
|
25 | 25 | include Redmine::I18n |
|
26 | 26 | include GravatarHelper::PublicMethods |
|
27 | 27 | |
|
28 | 28 | extend Forwardable |
|
29 | 29 | def_delegators :wiki_helper, :wikitoolbar_for, :heads_for_wiki_formatter |
|
30 | 30 | |
|
31 | 31 | # Return true if user is authorized for controller/action, otherwise false |
|
32 | 32 | def authorize_for(controller, action) |
|
33 | 33 | User.current.allowed_to?({:controller => controller, :action => action}, @project) |
|
34 | 34 | end |
|
35 | 35 | |
|
36 | 36 | # Display a link if user is authorized |
|
37 | 37 | def link_to_if_authorized(name, options = {}, html_options = nil, *parameters_for_method_reference) |
|
38 | 38 | link_to(name, options, html_options, *parameters_for_method_reference) if authorize_for(options[:controller] || params[:controller], options[:action]) |
|
39 | 39 | end |
|
40 | 40 | |
|
41 | 41 | # Display a link to remote if user is authorized |
|
42 | 42 | def link_to_remote_if_authorized(name, options = {}, html_options = nil) |
|
43 | 43 | url = options[:url] || {} |
|
44 | 44 | link_to_remote(name, options, html_options) if authorize_for(url[:controller] || params[:controller], url[:action]) |
|
45 | 45 | end |
|
46 | 46 | |
|
47 | 47 | # Displays a link to user's account page if active |
|
48 | 48 | def link_to_user(user, options={}) |
|
49 | 49 | if user.is_a?(User) |
|
50 | 50 | name = h(user.name(options[:format])) |
|
51 | 51 | if user.active? |
|
52 | 52 | link_to name, :controller => 'users', :action => 'show', :id => user |
|
53 | 53 | else |
|
54 | 54 | name |
|
55 | 55 | end |
|
56 | 56 | else |
|
57 | 57 | h(user.to_s) |
|
58 | 58 | end |
|
59 | 59 | end |
|
60 | 60 | |
|
61 | 61 | # Displays a link to +issue+ with its subject. |
|
62 | 62 | # Examples: |
|
63 | 63 | # |
|
64 | 64 | # link_to_issue(issue) # => Defect #6: This is the subject |
|
65 | 65 | # link_to_issue(issue, :truncate => 6) # => Defect #6: This i... |
|
66 | 66 | # link_to_issue(issue, :subject => false) # => Defect #6 |
|
67 | 67 | # link_to_issue(issue, :project => true) # => Foo - Defect #6 |
|
68 | 68 | # |
|
69 | 69 | def link_to_issue(issue, options={}) |
|
70 | 70 | title = nil |
|
71 | 71 | subject = nil |
|
72 | 72 | if options[:subject] == false |
|
73 | 73 | title = truncate(issue.subject, :length => 60) |
|
74 | 74 | else |
|
75 | 75 | subject = issue.subject |
|
76 | 76 | if options[:truncate] |
|
77 | 77 | subject = truncate(subject, :length => options[:truncate]) |
|
78 | 78 | end |
|
79 | 79 | end |
|
80 | 80 | s = link_to "#{issue.tracker} ##{issue.id}", {:controller => "issues", :action => "show", :id => issue}, |
|
81 | 81 | :class => issue.css_classes, |
|
82 | 82 | :title => title |
|
83 | 83 | s << ": #{h subject}" if subject |
|
84 | 84 | s = "#{h issue.project} - " + s if options[:project] |
|
85 | 85 | s |
|
86 | 86 | end |
|
87 | 87 | |
|
88 | 88 | # Generates a link to an attachment. |
|
89 | 89 | # Options: |
|
90 | 90 | # * :text - Link text (default to attachment filename) |
|
91 | 91 | # * :download - Force download (default: false) |
|
92 | 92 | def link_to_attachment(attachment, options={}) |
|
93 | 93 | text = options.delete(:text) || attachment.filename |
|
94 | 94 | action = options.delete(:download) ? 'download' : 'show' |
|
95 | 95 | |
|
96 | 96 | link_to(h(text), {:controller => 'attachments', :action => action, :id => attachment, :filename => attachment.filename }, options) |
|
97 | 97 | end |
|
98 | 98 | |
|
99 | 99 | # Generates a link to a SCM revision |
|
100 | 100 | # Options: |
|
101 | 101 | # * :text - Link text (default to the formatted revision) |
|
102 | 102 | def link_to_revision(revision, project, options={}) |
|
103 | 103 | text = options.delete(:text) || format_revision(revision) |
|
104 | 104 | |
|
105 | 105 | link_to(text, {:controller => 'repositories', :action => 'revision', :id => project, :rev => revision}, :title => l(:label_revision_id, revision)) |
|
106 | 106 | end |
|
107 | 107 | |
|
108 | 108 | def toggle_link(name, id, options={}) |
|
109 | 109 | onclick = "Element.toggle('#{id}'); " |
|
110 | 110 | onclick << (options[:focus] ? "Form.Element.focus('#{options[:focus]}'); " : "this.blur(); ") |
|
111 | 111 | onclick << "return false;" |
|
112 | 112 | link_to(name, "#", :onclick => onclick) |
|
113 | 113 | end |
|
114 | 114 | |
|
115 | 115 | def image_to_function(name, function, html_options = {}) |
|
116 | 116 | html_options.symbolize_keys! |
|
117 | 117 | tag(:input, html_options.merge({ |
|
118 | 118 | :type => "image", :src => image_path(name), |
|
119 | 119 | :onclick => (html_options[:onclick] ? "#{html_options[:onclick]}; " : "") + "#{function};" |
|
120 | 120 | })) |
|
121 | 121 | end |
|
122 | 122 | |
|
123 | 123 | def prompt_to_remote(name, text, param, url, html_options = {}) |
|
124 | 124 | html_options[:onclick] = "promptToRemote('#{text}', '#{param}', '#{url_for(url)}'); return false;" |
|
125 | 125 | link_to name, {}, html_options |
|
126 | 126 | end |
|
127 | 127 | |
|
128 | 128 | def format_activity_title(text) |
|
129 | 129 | h(truncate_single_line(text, :length => 100)) |
|
130 | 130 | end |
|
131 | 131 | |
|
132 | 132 | def format_activity_day(date) |
|
133 | 133 | date == Date.today ? l(:label_today).titleize : format_date(date) |
|
134 | 134 | end |
|
135 | 135 | |
|
136 | 136 | def format_activity_description(text) |
|
137 | 137 | h(truncate(text.to_s, :length => 120).gsub(%r{[\r\n]*<(pre|code)>.*$}m, '...')).gsub(/[\r\n]+/, "<br />") |
|
138 | 138 | end |
|
139 | 139 | |
|
140 | 140 | def format_version_name(version) |
|
141 | 141 | if version.project == @project |
|
142 | 142 | h(version) |
|
143 | 143 | else |
|
144 | 144 | h("#{version.project} - #{version}") |
|
145 | 145 | end |
|
146 | 146 | end |
|
147 | 147 | |
|
148 | 148 | def due_date_distance_in_words(date) |
|
149 | 149 | if date |
|
150 | 150 | l((date < Date.today ? :label_roadmap_overdue : :label_roadmap_due_in), distance_of_date_in_words(Date.today, date)) |
|
151 | 151 | end |
|
152 | 152 | end |
|
153 | 153 | |
|
154 | 154 | def render_page_hierarchy(pages, node=nil) |
|
155 | 155 | content = '' |
|
156 | 156 | if pages[node] |
|
157 | 157 | content << "<ul class=\"pages-hierarchy\">\n" |
|
158 | 158 | pages[node].each do |page| |
|
159 | 159 | content << "<li>" |
|
160 | 160 | content << link_to(h(page.pretty_title), {:controller => 'wiki', :action => 'index', :id => page.project, :page => page.title}, |
|
161 | 161 | :title => (page.respond_to?(:updated_on) ? l(:label_updated_time, distance_of_time_in_words(Time.now, page.updated_on)) : nil)) |
|
162 | 162 | content << "\n" + render_page_hierarchy(pages, page.id) if pages[page.id] |
|
163 | 163 | content << "</li>\n" |
|
164 | 164 | end |
|
165 | 165 | content << "</ul>\n" |
|
166 | 166 | end |
|
167 | 167 | content |
|
168 | 168 | end |
|
169 | 169 | |
|
170 | 170 | # Renders flash messages |
|
171 | 171 | def render_flash_messages |
|
172 | 172 | s = '' |
|
173 | 173 | flash.each do |k,v| |
|
174 | 174 | s << content_tag('div', v, :class => "flash #{k}") |
|
175 | 175 | end |
|
176 | 176 | s |
|
177 | 177 | end |
|
178 | 178 | |
|
179 | 179 | # Renders tabs and their content |
|
180 | 180 | def render_tabs(tabs) |
|
181 | 181 | if tabs.any? |
|
182 | 182 | render :partial => 'common/tabs', :locals => {:tabs => tabs} |
|
183 | 183 | else |
|
184 | 184 | content_tag 'p', l(:label_no_data), :class => "nodata" |
|
185 | 185 | end |
|
186 | 186 | end |
|
187 | 187 | |
|
188 | 188 | # Renders the project quick-jump box |
|
189 | 189 | def render_project_jump_box |
|
190 | 190 | # Retrieve them now to avoid a COUNT query |
|
191 | 191 | projects = User.current.projects.all |
|
192 | 192 | if projects.any? |
|
193 | 193 | s = '<select onchange="if (this.value != \'\') { window.location = this.value; }">' + |
|
194 | 194 | "<option value=''>#{ l(:label_jump_to_a_project) }</option>" + |
|
195 | 195 | '<option value="" disabled="disabled">---</option>' |
|
196 | 196 | s << project_tree_options_for_select(projects, :selected => @project) do |p| |
|
197 | 197 | { :value => url_for(:controller => 'projects', :action => 'show', :id => p, :jump => current_menu_item) } |
|
198 | 198 | end |
|
199 | 199 | s << '</select>' |
|
200 | 200 | s |
|
201 | 201 | end |
|
202 | 202 | end |
|
203 | 203 | |
|
204 | 204 | def project_tree_options_for_select(projects, options = {}) |
|
205 | 205 | s = '' |
|
206 | 206 | project_tree(projects) do |project, level| |
|
207 | 207 | name_prefix = (level > 0 ? (' ' * 2 * level + '» ') : '') |
|
208 | 208 | tag_options = {:value => project.id, :selected => ((project == options[:selected]) ? 'selected' : nil)} |
|
209 | 209 | tag_options.merge!(yield(project)) if block_given? |
|
210 | 210 | s << content_tag('option', name_prefix + h(project), tag_options) |
|
211 | 211 | end |
|
212 | 212 | s |
|
213 | 213 | end |
|
214 | 214 | |
|
215 | 215 | # Yields the given block for each project with its level in the tree |
|
216 | 216 | def project_tree(projects, &block) |
|
217 | 217 | ancestors = [] |
|
218 | 218 | projects.sort_by(&:lft).each do |project| |
|
219 | 219 | while (ancestors.any? && !project.is_descendant_of?(ancestors.last)) |
|
220 | 220 | ancestors.pop |
|
221 | 221 | end |
|
222 | 222 | yield project, ancestors.size |
|
223 | 223 | ancestors << project |
|
224 | 224 | end |
|
225 | 225 | end |
|
226 | 226 | |
|
227 | 227 | def project_nested_ul(projects, &block) |
|
228 | 228 | s = '' |
|
229 | 229 | if projects.any? |
|
230 | 230 | ancestors = [] |
|
231 | 231 | projects.sort_by(&:lft).each do |project| |
|
232 | 232 | if (ancestors.empty? || project.is_descendant_of?(ancestors.last)) |
|
233 | 233 | s << "<ul>\n" |
|
234 | 234 | else |
|
235 | 235 | ancestors.pop |
|
236 | 236 | s << "</li>" |
|
237 | 237 | while (ancestors.any? && !project.is_descendant_of?(ancestors.last)) |
|
238 | 238 | ancestors.pop |
|
239 | 239 | s << "</ul></li>\n" |
|
240 | 240 | end |
|
241 | 241 | end |
|
242 | 242 | s << "<li>" |
|
243 | 243 | s << yield(project).to_s |
|
244 | 244 | ancestors << project |
|
245 | 245 | end |
|
246 | 246 | s << ("</li></ul>\n" * ancestors.size) |
|
247 | 247 | end |
|
248 | 248 | s |
|
249 | 249 | end |
|
250 | 250 | |
|
251 | 251 | def principals_check_box_tags(name, principals) |
|
252 | 252 | s = '' |
|
253 | 253 | principals.sort.each do |principal| |
|
254 | 254 | s << "<label>#{ check_box_tag name, principal.id, false } #{h principal}</label>\n" |
|
255 | 255 | end |
|
256 | 256 | s |
|
257 | 257 | end |
|
258 | 258 | |
|
259 | 259 | # Truncates and returns the string as a single line |
|
260 | 260 | def truncate_single_line(string, *args) |
|
261 | 261 | truncate(string.to_s, *args).gsub(%r{[\r\n]+}m, ' ') |
|
262 | 262 | end |
|
263 | 263 | |
|
264 | 264 | def html_hours(text) |
|
265 | 265 | text.gsub(%r{(\d+)\.(\d+)}, '<span class="hours hours-int">\1</span><span class="hours hours-dec">.\2</span>') |
|
266 | 266 | end |
|
267 | 267 | |
|
268 | 268 | def authoring(created, author, options={}) |
|
269 | 269 | l(options[:label] || :label_added_time_by, :author => link_to_user(author), :age => time_tag(created)) |
|
270 | 270 | end |
|
271 | 271 | |
|
272 | 272 | def time_tag(time) |
|
273 | 273 | text = distance_of_time_in_words(Time.now, time) |
|
274 | 274 | if @project |
|
275 | 275 | link_to(text, {:controller => 'projects', :action => 'activity', :id => @project, :from => time.to_date}, :title => format_time(time)) |
|
276 | 276 | else |
|
277 | 277 | content_tag('acronym', text, :title => format_time(time)) |
|
278 | 278 | end |
|
279 | 279 | end |
|
280 | 280 | |
|
281 | 281 | def syntax_highlight(name, content) |
|
282 | 282 | type = CodeRay::FileType[name] |
|
283 | 283 | type ? CodeRay.scan(content, type).html : h(content) |
|
284 | 284 | end |
|
285 | 285 | |
|
286 | 286 | def to_path_param(path) |
|
287 | 287 | path.to_s.split(%r{[/\\]}).select {|p| !p.blank?} |
|
288 | 288 | end |
|
289 | 289 | |
|
290 | 290 | def pagination_links_full(paginator, count=nil, options={}) |
|
291 | 291 | page_param = options.delete(:page_param) || :page |
|
292 | 292 | url_param = params.dup |
|
293 | 293 | # don't reuse query params if filters are present |
|
294 | 294 | url_param.merge!(:fields => nil, :values => nil, :operators => nil) if url_param.delete(:set_filter) |
|
295 | 295 | |
|
296 | 296 | html = '' |
|
297 | 297 | if paginator.current.previous |
|
298 | 298 | html << link_to_remote_content_update('« ' + l(:label_previous), url_param.merge(page_param => paginator.current.previous)) + ' ' |
|
299 | 299 | end |
|
300 | 300 | |
|
301 | 301 | html << (pagination_links_each(paginator, options) do |n| |
|
302 | 302 | link_to_remote_content_update(n.to_s, url_param.merge(page_param => n)) |
|
303 | 303 | end || '') |
|
304 | 304 | |
|
305 | 305 | if paginator.current.next |
|
306 | 306 | html << ' ' + link_to_remote_content_update((l(:label_next) + ' »'), url_param.merge(page_param => paginator.current.next)) |
|
307 | 307 | end |
|
308 | 308 | |
|
309 | 309 | unless count.nil? |
|
310 | 310 | html << [ |
|
311 | 311 | " (#{paginator.current.first_item}-#{paginator.current.last_item}/#{count})", |
|
312 | 312 | per_page_links(paginator.items_per_page) |
|
313 | 313 | ].compact.join(' | ') |
|
314 | 314 | end |
|
315 | 315 | |
|
316 | 316 | html |
|
317 | 317 | end |
|
318 | 318 | |
|
319 | 319 | def per_page_links(selected=nil) |
|
320 | 320 | url_param = params.dup |
|
321 | 321 | url_param.clear if url_param.has_key?(:set_filter) |
|
322 | 322 | |
|
323 | 323 | links = Setting.per_page_options_array.collect do |n| |
|
324 | 324 | n == selected ? n : link_to_remote(n, {:update => "content", |
|
325 | 325 | :url => params.dup.merge(:per_page => n), |
|
326 | 326 | :method => :get}, |
|
327 | 327 | {:href => url_for(url_param.merge(:per_page => n))}) |
|
328 | 328 | end |
|
329 | 329 | links.size > 1 ? l(:label_display_per_page, links.join(', ')) : nil |
|
330 | 330 | end |
|
331 | 331 | |
|
332 | 332 | def reorder_links(name, url) |
|
333 | 333 | link_to(image_tag('2uparrow.png', :alt => l(:label_sort_highest)), url.merge({"#{name}[move_to]" => 'highest'}), :method => :post, :title => l(:label_sort_highest)) + |
|
334 | 334 | link_to(image_tag('1uparrow.png', :alt => l(:label_sort_higher)), url.merge({"#{name}[move_to]" => 'higher'}), :method => :post, :title => l(:label_sort_higher)) + |
|
335 | 335 | link_to(image_tag('1downarrow.png', :alt => l(:label_sort_lower)), url.merge({"#{name}[move_to]" => 'lower'}), :method => :post, :title => l(:label_sort_lower)) + |
|
336 | 336 | link_to(image_tag('2downarrow.png', :alt => l(:label_sort_lowest)), url.merge({"#{name}[move_to]" => 'lowest'}), :method => :post, :title => l(:label_sort_lowest)) |
|
337 | 337 | end |
|
338 | 338 | |
|
339 | 339 | def breadcrumb(*args) |
|
340 | 340 | elements = args.flatten |
|
341 | 341 | elements.any? ? content_tag('p', args.join(' » ') + ' » ', :class => 'breadcrumb') : nil |
|
342 | 342 | end |
|
343 | 343 | |
|
344 | 344 | def other_formats_links(&block) |
|
345 | 345 | concat('<p class="other-formats">' + l(:label_export_to)) |
|
346 | 346 | yield Redmine::Views::OtherFormatsBuilder.new(self) |
|
347 | 347 | concat('</p>') |
|
348 | 348 | end |
|
349 | 349 | |
|
350 | 350 | def page_header_title |
|
351 | 351 | if @project.nil? || @project.new_record? |
|
352 | 352 | h(Setting.app_title) |
|
353 | 353 | else |
|
354 | 354 | b = [] |
|
355 | 355 | ancestors = (@project.root? ? [] : @project.ancestors.visible) |
|
356 | 356 | if ancestors.any? |
|
357 | 357 | root = ancestors.shift |
|
358 | 358 | b << link_to(h(root), {:controller => 'projects', :action => 'show', :id => root, :jump => current_menu_item}, :class => 'root') |
|
359 | 359 | if ancestors.size > 2 |
|
360 | 360 | b << '…' |
|
361 | 361 | ancestors = ancestors[-2, 2] |
|
362 | 362 | end |
|
363 | 363 | b += ancestors.collect {|p| link_to(h(p), {:controller => 'projects', :action => 'show', :id => p, :jump => current_menu_item}, :class => 'ancestor') } |
|
364 | 364 | end |
|
365 | 365 | b << h(@project) |
|
366 | 366 | b.join(' » ') |
|
367 | 367 | end |
|
368 | 368 | end |
|
369 | 369 | |
|
370 | 370 | def html_title(*args) |
|
371 | 371 | if args.empty? |
|
372 | 372 | title = [] |
|
373 | 373 | title << @project.name if @project |
|
374 | 374 | title += @html_title if @html_title |
|
375 | 375 | title << Setting.app_title |
|
376 | 376 | title.select {|t| !t.blank? }.join(' - ') |
|
377 | 377 | else |
|
378 | 378 | @html_title ||= [] |
|
379 | 379 | @html_title += args |
|
380 | 380 | end |
|
381 | 381 | end |
|
382 | 382 | |
|
383 | 383 | def accesskey(s) |
|
384 | 384 | Redmine::AccessKeys.key_for s |
|
385 | 385 | end |
|
386 | 386 | |
|
387 | 387 | # Formats text according to system settings. |
|
388 | 388 | # 2 ways to call this method: |
|
389 | 389 | # * with a String: textilizable(text, options) |
|
390 | 390 | # * with an object and one of its attribute: textilizable(issue, :description, options) |
|
391 | 391 | def textilizable(*args) |
|
392 | 392 | options = args.last.is_a?(Hash) ? args.pop : {} |
|
393 | 393 | case args.size |
|
394 | 394 | when 1 |
|
395 | 395 | obj = options[:object] |
|
396 | 396 | text = args.shift |
|
397 | 397 | when 2 |
|
398 | 398 | obj = args.shift |
|
399 | 399 | text = obj.send(args.shift).to_s |
|
400 | 400 | else |
|
401 | 401 | raise ArgumentError, 'invalid arguments to textilizable' |
|
402 | 402 | end |
|
403 | 403 | return '' if text.blank? |
|
404 | 404 | |
|
405 | text = Redmine::WikiFormatting.to_html(Setting.text_formatting, text) { |macro, args| exec_macro(macro, obj, args) } | |
|
406 | ||
|
405 | 407 | only_path = options.delete(:only_path) == false ? false : true |
|
406 | 408 | |
|
407 | 409 | # when using an image link, try to use an attachment, if possible |
|
408 | 410 | attachments = options[:attachments] || (obj && obj.respond_to?(:attachments) ? obj.attachments : nil) |
|
409 | 411 | |
|
410 | 412 | if attachments |
|
411 | 413 | attachments = attachments.sort_by(&:created_on).reverse |
|
412 | text = text.gsub(/!((\<|\=|\>)?(\([^\)]+\))?(\[[^\]]+\])?(\{[^\}]+\})?)(\S+\.(bmp|gif|jpg|jpeg|png))!/i) do |m| | |
|
413 | style = $1 | |
|
414 | filename = $6.downcase | |
|
414 | text.gsub!(/src="([^\/"]+\.(bmp|gif|jpg|jpeg|png))"(\s+alt="([^"]*)")?/i) do |m| | |
|
415 | filename, ext, alt, alttext = $1.downcase, $2, $3, $4 | |
|
416 | ||
|
415 | 417 | # search for the picture in attachments |
|
416 | 418 | if found = attachments.detect { |att| att.filename.downcase == filename } |
|
417 | 419 | image_url = url_for :only_path => only_path, :controller => 'attachments', :action => 'download', :id => found |
|
418 |
desc = found.description.to_s.gsub( |
|
|
419 | alt = desc.blank? ? nil : "(#{desc})" | |
|
420 | "!#{style}#{image_url}#{alt}!" | |
|
420 | desc = found.description.to_s.gsub('"', '') | |
|
421 | if !desc.blank? && alttext.blank? | |
|
422 | alt = " title=\"#{desc}\" alt=\"#{desc}\"" | |
|
423 | end | |
|
424 | "src=\"#{image_url}\"#{alt}" | |
|
421 | 425 | else |
|
422 | 426 | m |
|
423 | 427 | end |
|
424 | 428 | end |
|
425 | 429 | end |
|
426 | 430 | |
|
427 | text = Redmine::WikiFormatting.to_html(Setting.text_formatting, text) { |macro, args| exec_macro(macro, obj, args) } | |
|
428 | 431 | |
|
429 | 432 | # different methods for formatting wiki links |
|
430 | 433 | case options[:wiki_links] |
|
431 | 434 | when :local |
|
432 | 435 | # used for local links to html files |
|
433 | 436 | format_wiki_link = Proc.new {|project, title, anchor| "#{title}.html" } |
|
434 | 437 | when :anchor |
|
435 | 438 | # used for single-file wiki export |
|
436 | 439 | format_wiki_link = Proc.new {|project, title, anchor| "##{title}" } |
|
437 | 440 | else |
|
438 | 441 | format_wiki_link = Proc.new {|project, title, anchor| url_for(:only_path => only_path, :controller => 'wiki', :action => 'index', :id => project, :page => title, :anchor => anchor) } |
|
439 | 442 | end |
|
440 | 443 | |
|
441 | 444 | project = options[:project] || @project || (obj && obj.respond_to?(:project) ? obj.project : nil) |
|
442 | 445 | |
|
443 | 446 | # Wiki links |
|
444 | 447 | # |
|
445 | 448 | # Examples: |
|
446 | 449 | # [[mypage]] |
|
447 | 450 | # [[mypage|mytext]] |
|
448 | 451 | # wiki links can refer other project wikis, using project name or identifier: |
|
449 | 452 | # [[project:]] -> wiki starting page |
|
450 | 453 | # [[project:|mytext]] |
|
451 | 454 | # [[project:mypage]] |
|
452 | 455 | # [[project:mypage|mytext]] |
|
453 | 456 | text = text.gsub(/(!)?(\[\[([^\]\n\|]+)(\|([^\]\n\|]+))?\]\])/) do |m| |
|
454 | 457 | link_project = project |
|
455 | 458 | esc, all, page, title = $1, $2, $3, $5 |
|
456 | 459 | if esc.nil? |
|
457 | 460 | if page =~ /^([^\:]+)\:(.*)$/ |
|
458 | 461 | link_project = Project.find_by_name($1) || Project.find_by_identifier($1) |
|
459 | 462 | page = $2 |
|
460 | 463 | title ||= $1 if page.blank? |
|
461 | 464 | end |
|
462 | 465 | |
|
463 | 466 | if link_project && link_project.wiki |
|
464 | 467 | # extract anchor |
|
465 | 468 | anchor = nil |
|
466 | 469 | if page =~ /^(.+?)\#(.+)$/ |
|
467 | 470 | page, anchor = $1, $2 |
|
468 | 471 | end |
|
469 | 472 | # check if page exists |
|
470 | 473 | wiki_page = link_project.wiki.find_page(page) |
|
471 | 474 | link_to((title || page), format_wiki_link.call(link_project, Wiki.titleize(page), anchor), |
|
472 | 475 | :class => ('wiki-page' + (wiki_page ? '' : ' new'))) |
|
473 | 476 | else |
|
474 | 477 | # project or wiki doesn't exist |
|
475 | 478 | all |
|
476 | 479 | end |
|
477 | 480 | else |
|
478 | 481 | all |
|
479 | 482 | end |
|
480 | 483 | end |
|
481 | 484 | |
|
482 | 485 | # Redmine links |
|
483 | 486 | # |
|
484 | 487 | # Examples: |
|
485 | 488 | # Issues: |
|
486 | 489 | # #52 -> Link to issue #52 |
|
487 | 490 | # Changesets: |
|
488 | 491 | # r52 -> Link to revision 52 |
|
489 | 492 | # commit:a85130f -> Link to scmid starting with a85130f |
|
490 | 493 | # Documents: |
|
491 | 494 | # document#17 -> Link to document with id 17 |
|
492 | 495 | # document:Greetings -> Link to the document with title "Greetings" |
|
493 | 496 | # document:"Some document" -> Link to the document with title "Some document" |
|
494 | 497 | # Versions: |
|
495 | 498 | # version#3 -> Link to version with id 3 |
|
496 | 499 | # version:1.0.0 -> Link to version named "1.0.0" |
|
497 | 500 | # version:"1.0 beta 2" -> Link to version named "1.0 beta 2" |
|
498 | 501 | # Attachments: |
|
499 | 502 | # attachment:file.zip -> Link to the attachment of the current object named file.zip |
|
500 | 503 | # Source files: |
|
501 | 504 | # source:some/file -> Link to the file located at /some/file in the project's repository |
|
502 | 505 | # source:some/file@52 -> Link to the file's revision 52 |
|
503 | 506 | # source:some/file#L120 -> Link to line 120 of the file |
|
504 | 507 | # source:some/file@52#L120 -> Link to line 120 of the file's revision 52 |
|
505 | 508 | # export:some/file -> Force the download of the file |
|
506 | 509 | # Forum messages: |
|
507 | 510 | # message#1218 -> Link to message with id 1218 |
|
508 | 511 | text = text.gsub(%r{([\s\(,\-\>]|^)(!)?(attachment|document|version|commit|source|export|message)?((#|r)(\d+)|(:)([^"\s<>][^\s<>]*?|"[^"]+?"))(?=(?=[[:punct:]]\W)|,|\s|<|$)}) do |m| |
|
509 | 512 | leading, esc, prefix, sep, oid = $1, $2, $3, $5 || $7, $6 || $8 |
|
510 | 513 | link = nil |
|
511 | 514 | if esc.nil? |
|
512 | 515 | if prefix.nil? && sep == 'r' |
|
513 | 516 | if project && (changeset = project.changesets.find_by_revision(oid)) |
|
514 | 517 | link = link_to("r#{oid}", {:only_path => only_path, :controller => 'repositories', :action => 'revision', :id => project, :rev => oid}, |
|
515 | 518 | :class => 'changeset', |
|
516 | 519 | :title => truncate_single_line(changeset.comments, :length => 100)) |
|
517 | 520 | end |
|
518 | 521 | elsif sep == '#' |
|
519 | 522 | oid = oid.to_i |
|
520 | 523 | case prefix |
|
521 | 524 | when nil |
|
522 | 525 | if issue = Issue.visible.find_by_id(oid, :include => :status) |
|
523 | 526 | link = link_to("##{oid}", {:only_path => only_path, :controller => 'issues', :action => 'show', :id => oid}, |
|
524 | 527 | :class => issue.css_classes, |
|
525 | 528 | :title => "#{truncate(issue.subject, :length => 100)} (#{issue.status.name})") |
|
526 | 529 | end |
|
527 | 530 | when 'document' |
|
528 | 531 | if document = Document.find_by_id(oid, :include => [:project], :conditions => Project.visible_by(User.current)) |
|
529 | 532 | link = link_to h(document.title), {:only_path => only_path, :controller => 'documents', :action => 'show', :id => document}, |
|
530 | 533 | :class => 'document' |
|
531 | 534 | end |
|
532 | 535 | when 'version' |
|
533 | 536 | if version = Version.find_by_id(oid, :include => [:project], :conditions => Project.visible_by(User.current)) |
|
534 | 537 | link = link_to h(version.name), {:only_path => only_path, :controller => 'versions', :action => 'show', :id => version}, |
|
535 | 538 | :class => 'version' |
|
536 | 539 | end |
|
537 | 540 | when 'message' |
|
538 | 541 | if message = Message.find_by_id(oid, :include => [:parent, {:board => :project}], :conditions => Project.visible_by(User.current)) |
|
539 | 542 | link = link_to h(truncate(message.subject, :length => 60)), {:only_path => only_path, |
|
540 | 543 | :controller => 'messages', |
|
541 | 544 | :action => 'show', |
|
542 | 545 | :board_id => message.board, |
|
543 | 546 | :id => message.root, |
|
544 | 547 | :anchor => (message.parent ? "message-#{message.id}" : nil)}, |
|
545 | 548 | :class => 'message' |
|
546 | 549 | end |
|
547 | 550 | end |
|
548 | 551 | elsif sep == ':' |
|
549 | 552 | # removes the double quotes if any |
|
550 | 553 | name = oid.gsub(%r{^"(.*)"$}, "\\1") |
|
551 | 554 | case prefix |
|
552 | 555 | when 'document' |
|
553 | 556 | if project && document = project.documents.find_by_title(name) |
|
554 | 557 | link = link_to h(document.title), {:only_path => only_path, :controller => 'documents', :action => 'show', :id => document}, |
|
555 | 558 | :class => 'document' |
|
556 | 559 | end |
|
557 | 560 | when 'version' |
|
558 | 561 | if project && version = project.versions.find_by_name(name) |
|
559 | 562 | link = link_to h(version.name), {:only_path => only_path, :controller => 'versions', :action => 'show', :id => version}, |
|
560 | 563 | :class => 'version' |
|
561 | 564 | end |
|
562 | 565 | when 'commit' |
|
563 | 566 | if project && (changeset = project.changesets.find(:first, :conditions => ["scmid LIKE ?", "#{name}%"])) |
|
564 | 567 | link = link_to h("#{name}"), {:only_path => only_path, :controller => 'repositories', :action => 'revision', :id => project, :rev => changeset.revision}, |
|
565 | 568 | :class => 'changeset', |
|
566 | 569 | :title => truncate_single_line(changeset.comments, :length => 100) |
|
567 | 570 | end |
|
568 | 571 | when 'source', 'export' |
|
569 | 572 | if project && project.repository |
|
570 | 573 | name =~ %r{^[/\\]*(.*?)(@([0-9a-f]+))?(#(L\d+))?$} |
|
571 | 574 | path, rev, anchor = $1, $3, $5 |
|
572 | 575 | link = link_to h("#{prefix}:#{name}"), {:controller => 'repositories', :action => 'entry', :id => project, |
|
573 | 576 | :path => to_path_param(path), |
|
574 | 577 | :rev => rev, |
|
575 | 578 | :anchor => anchor, |
|
576 | 579 | :format => (prefix == 'export' ? 'raw' : nil)}, |
|
577 | 580 | :class => (prefix == 'export' ? 'source download' : 'source') |
|
578 | 581 | end |
|
579 | 582 | when 'attachment' |
|
580 | 583 | if attachments && attachment = attachments.detect {|a| a.filename == name } |
|
581 | 584 | link = link_to h(attachment.filename), {:only_path => only_path, :controller => 'attachments', :action => 'download', :id => attachment}, |
|
582 | 585 | :class => 'attachment' |
|
583 | 586 | end |
|
584 | 587 | end |
|
585 | 588 | end |
|
586 | 589 | end |
|
587 | 590 | leading + (link || "#{prefix}#{sep}#{oid}") |
|
588 | 591 | end |
|
589 | 592 | |
|
590 | 593 | text |
|
591 | 594 | end |
|
592 | 595 | |
|
593 | 596 | # Same as Rails' simple_format helper without using paragraphs |
|
594 | 597 | def simple_format_without_paragraph(text) |
|
595 | 598 | text.to_s. |
|
596 | 599 | gsub(/\r\n?/, "\n"). # \r\n and \r -> \n |
|
597 | 600 | gsub(/\n\n+/, "<br /><br />"). # 2+ newline -> 2 br |
|
598 | 601 | gsub(/([^\n]\n)(?=[^\n])/, '\1<br />') # 1 newline -> br |
|
599 | 602 | end |
|
600 | 603 | |
|
601 | 604 | def lang_options_for_select(blank=true) |
|
602 | 605 | (blank ? [["(auto)", ""]] : []) + |
|
603 | 606 | valid_languages.collect{|lang| [ ll(lang.to_s, :general_lang_name), lang.to_s]}.sort{|x,y| x.last <=> y.last } |
|
604 | 607 | end |
|
605 | 608 | |
|
606 | 609 | def label_tag_for(name, option_tags = nil, options = {}) |
|
607 | 610 | label_text = l(("field_"+field.to_s.gsub(/\_id$/, "")).to_sym) + (options.delete(:required) ? @template.content_tag("span", " *", :class => "required"): "") |
|
608 | 611 | content_tag("label", label_text) |
|
609 | 612 | end |
|
610 | 613 | |
|
611 | 614 | def labelled_tabular_form_for(name, object, options, &proc) |
|
612 | 615 | options[:html] ||= {} |
|
613 | 616 | options[:html][:class] = 'tabular' unless options[:html].has_key?(:class) |
|
614 | 617 | form_for(name, object, options.merge({ :builder => TabularFormBuilder, :lang => current_language}), &proc) |
|
615 | 618 | end |
|
616 | 619 | |
|
617 | 620 | def back_url_hidden_field_tag |
|
618 | 621 | back_url = params[:back_url] || request.env['HTTP_REFERER'] |
|
619 | 622 | back_url = CGI.unescape(back_url.to_s) |
|
620 | 623 | hidden_field_tag('back_url', CGI.escape(back_url)) unless back_url.blank? |
|
621 | 624 | end |
|
622 | 625 | |
|
623 | 626 | def check_all_links(form_name) |
|
624 | 627 | link_to_function(l(:button_check_all), "checkAll('#{form_name}', true)") + |
|
625 | 628 | " | " + |
|
626 | 629 | link_to_function(l(:button_uncheck_all), "checkAll('#{form_name}', false)") |
|
627 | 630 | end |
|
628 | 631 | |
|
629 | 632 | def progress_bar(pcts, options={}) |
|
630 | 633 | pcts = [pcts, pcts] unless pcts.is_a?(Array) |
|
631 | 634 | pcts = pcts.collect(&:round) |
|
632 | 635 | pcts[1] = pcts[1] - pcts[0] |
|
633 | 636 | pcts << (100 - pcts[1] - pcts[0]) |
|
634 | 637 | width = options[:width] || '100px;' |
|
635 | 638 | legend = options[:legend] || '' |
|
636 | 639 | content_tag('table', |
|
637 | 640 | content_tag('tr', |
|
638 | 641 | (pcts[0] > 0 ? content_tag('td', '', :style => "width: #{pcts[0]}%;", :class => 'closed') : '') + |
|
639 | 642 | (pcts[1] > 0 ? content_tag('td', '', :style => "width: #{pcts[1]}%;", :class => 'done') : '') + |
|
640 | 643 | (pcts[2] > 0 ? content_tag('td', '', :style => "width: #{pcts[2]}%;", :class => 'todo') : '') |
|
641 | 644 | ), :class => 'progress', :style => "width: #{width};") + |
|
642 | 645 | content_tag('p', legend, :class => 'pourcent') |
|
643 | 646 | end |
|
644 | 647 | |
|
645 | 648 | def context_menu_link(name, url, options={}) |
|
646 | 649 | options[:class] ||= '' |
|
647 | 650 | if options.delete(:selected) |
|
648 | 651 | options[:class] << ' icon-checked disabled' |
|
649 | 652 | options[:disabled] = true |
|
650 | 653 | end |
|
651 | 654 | if options.delete(:disabled) |
|
652 | 655 | options.delete(:method) |
|
653 | 656 | options.delete(:confirm) |
|
654 | 657 | options.delete(:onclick) |
|
655 | 658 | options[:class] << ' disabled' |
|
656 | 659 | url = '#' |
|
657 | 660 | end |
|
658 | 661 | link_to name, url, options |
|
659 | 662 | end |
|
660 | 663 | |
|
661 | 664 | def calendar_for(field_id) |
|
662 | 665 | include_calendar_headers_tags |
|
663 | 666 | image_tag("calendar.png", {:id => "#{field_id}_trigger",:class => "calendar-trigger"}) + |
|
664 | 667 | javascript_tag("Calendar.setup({inputField : '#{field_id}', ifFormat : '%Y-%m-%d', button : '#{field_id}_trigger' });") |
|
665 | 668 | end |
|
666 | 669 | |
|
667 | 670 | def include_calendar_headers_tags |
|
668 | 671 | unless @calendar_headers_tags_included |
|
669 | 672 | @calendar_headers_tags_included = true |
|
670 | 673 | content_for :header_tags do |
|
671 | 674 | start_of_week = case Setting.start_of_week.to_i |
|
672 | 675 | when 1 |
|
673 | 676 | 'Calendar._FD = 1;' # Monday |
|
674 | 677 | when 7 |
|
675 | 678 | 'Calendar._FD = 0;' # Sunday |
|
676 | 679 | else |
|
677 | 680 | '' # use language |
|
678 | 681 | end |
|
679 | 682 | |
|
680 | 683 | javascript_include_tag('calendar/calendar') + |
|
681 | 684 | javascript_include_tag("calendar/lang/calendar-#{current_language.to_s.downcase}.js") + |
|
682 | 685 | javascript_tag(start_of_week) + |
|
683 | 686 | javascript_include_tag('calendar/calendar-setup') + |
|
684 | 687 | stylesheet_link_tag('calendar') |
|
685 | 688 | end |
|
686 | 689 | end |
|
687 | 690 | end |
|
688 | 691 | |
|
689 | 692 | def content_for(name, content = nil, &block) |
|
690 | 693 | @has_content ||= {} |
|
691 | 694 | @has_content[name] = true |
|
692 | 695 | super(name, content, &block) |
|
693 | 696 | end |
|
694 | 697 | |
|
695 | 698 | def has_content?(name) |
|
696 | 699 | (@has_content && @has_content[name]) || false |
|
697 | 700 | end |
|
698 | 701 | |
|
699 | 702 | # Returns the avatar image tag for the given +user+ if avatars are enabled |
|
700 | 703 | # +user+ can be a User or a string that will be scanned for an email address (eg. 'joe <joe@foo.bar>') |
|
701 | 704 | def avatar(user, options = { }) |
|
702 | 705 | if Setting.gravatar_enabled? |
|
703 | 706 | options.merge!({:ssl => Setting.protocol == 'https', :default => Setting.gravatar_default}) |
|
704 | 707 | email = nil |
|
705 | 708 | if user.respond_to?(:mail) |
|
706 | 709 | email = user.mail |
|
707 | 710 | elsif user.to_s =~ %r{<(.+?)>} |
|
708 | 711 | email = $1 |
|
709 | 712 | end |
|
710 | 713 | return gravatar(email.to_s.downcase, options) unless email.blank? rescue nil |
|
711 | 714 | end |
|
712 | 715 | end |
|
713 | 716 | |
|
714 | 717 | private |
|
715 | 718 | |
|
716 | 719 | def wiki_helper |
|
717 | 720 | helper = Redmine::WikiFormatting.helper_for(Setting.text_formatting) |
|
718 | 721 | extend helper |
|
719 | 722 | return self |
|
720 | 723 | end |
|
721 | 724 | |
|
722 | 725 | def link_to_remote_content_update(text, url_params) |
|
723 | 726 | link_to_remote(text, |
|
724 | 727 | {:url => url_params, :method => :get, :update => 'content', :complete => 'window.scrollTo(0,0)'}, |
|
725 | 728 | {:href => url_for(:params => url_params)} |
|
726 | 729 | ) |
|
727 | 730 | end |
|
728 | 731 | |
|
729 | 732 | end |
General Comments 0
You need to be logged in to leave comments.
Login now