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