##// END OF EJS Templates
Removes calls to TimeZone#adjust (#1584)....
Jean-Philippe Lang -
r1612:1e6b8a482ae1
parent child
Show More
@@ -1,517 +1,513
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
21 21 module ApplicationHelper
22 22 include Redmine::WikiFormatting::Macros::Definitions
23 23
24 24 def current_role
25 25 @current_role ||= User.current.role_for_project(@project)
26 26 end
27 27
28 28 # Return true if user is authorized for controller/action, otherwise false
29 29 def authorize_for(controller, action)
30 30 User.current.allowed_to?({:controller => controller, :action => action}, @project)
31 31 end
32 32
33 33 # Display a link if user is authorized
34 34 def link_to_if_authorized(name, options = {}, html_options = nil, *parameters_for_method_reference)
35 35 link_to(name, options, html_options, *parameters_for_method_reference) if authorize_for(options[:controller] || params[:controller], options[:action])
36 36 end
37 37
38 38 # Display a link to user's account page
39 39 def link_to_user(user)
40 40 user ? link_to(user, :controller => 'account', :action => 'show', :id => user) : 'Anonymous'
41 41 end
42 42
43 43 def link_to_issue(issue, options={})
44 44 link_to "#{issue.tracker.name} ##{issue.id}", {:controller => "issues", :action => "show", :id => issue}, options
45 45 end
46 46
47 47 def toggle_link(name, id, options={})
48 48 onclick = "Element.toggle('#{id}'); "
49 49 onclick << (options[:focus] ? "Form.Element.focus('#{options[:focus]}'); " : "this.blur(); ")
50 50 onclick << "return false;"
51 51 link_to(name, "#", :onclick => onclick)
52 52 end
53 53
54 54 def image_to_function(name, function, html_options = {})
55 55 html_options.symbolize_keys!
56 56 tag(:input, html_options.merge({
57 57 :type => "image", :src => image_path(name),
58 58 :onclick => (html_options[:onclick] ? "#{html_options[:onclick]}; " : "") + "#{function};"
59 59 }))
60 60 end
61 61
62 62 def prompt_to_remote(name, text, param, url, html_options = {})
63 63 html_options[:onclick] = "promptToRemote('#{text}', '#{param}', '#{url_for(url)}'); return false;"
64 64 link_to name, {}, html_options
65 65 end
66 66
67 67 def format_date(date)
68 68 return nil unless date
69 69 # "Setting.date_format.size < 2" is a temporary fix (content of date_format setting changed)
70 70 @date_format ||= (Setting.date_format.blank? || Setting.date_format.size < 2 ? l(:general_fmt_date) : Setting.date_format)
71 71 date.strftime(@date_format)
72 72 end
73 73
74 74 def format_time(time, include_date = true)
75 75 return nil unless time
76 76 time = time.to_time if time.is_a?(String)
77 77 zone = User.current.time_zone
78 if time.utc?
79 local = zone ? zone.adjust(time) : time.getlocal
80 else
81 local = zone ? zone.adjust(time.getutc) : time
82 end
78 local = zone ? time.in_time_zone(zone) : (time.utc? ? time.utc_to_local : time)
83 79 @date_format ||= (Setting.date_format.blank? || Setting.date_format.size < 2 ? l(:general_fmt_date) : Setting.date_format)
84 80 @time_format ||= (Setting.time_format.blank? ? l(:general_fmt_time) : Setting.time_format)
85 81 include_date ? local.strftime("#{@date_format} #{@time_format}") : local.strftime(@time_format)
86 82 end
87 83
88 84 # Truncates and returns the string as a single line
89 85 def truncate_single_line(string, *args)
90 86 truncate(string, *args).gsub(%r{[\r\n]+}m, ' ')
91 87 end
92 88
93 89 def html_hours(text)
94 90 text.gsub(%r{(\d+)\.(\d+)}, '<span class="hours hours-int">\1</span><span class="hours hours-dec">.\2</span>')
95 91 end
96 92
97 93 def authoring(created, author)
98 94 time_tag = content_tag('acronym', distance_of_time_in_words(Time.now, created), :title => format_time(created))
99 95 l(:label_added_time_by, author || 'Anonymous', time_tag)
100 96 end
101 97
102 98 def l_or_humanize(s)
103 99 l_has_string?("label_#{s}".to_sym) ? l("label_#{s}".to_sym) : s.to_s.humanize
104 100 end
105 101
106 102 def day_name(day)
107 103 l(:general_day_names).split(',')[day-1]
108 104 end
109 105
110 106 def month_name(month)
111 107 l(:actionview_datehelper_select_month_names).split(',')[month-1]
112 108 end
113 109
114 110 def syntax_highlight(name, content)
115 111 type = CodeRay::FileType[name]
116 112 type ? CodeRay.scan(content, type).html : h(content)
117 113 end
118 114
119 115 def pagination_links_full(paginator, count=nil, options={})
120 116 page_param = options.delete(:page_param) || :page
121 117 url_param = params.dup
122 118 # don't reuse params if filters are present
123 119 url_param.clear if url_param.has_key?(:set_filter)
124 120
125 121 html = ''
126 122 html << link_to_remote(('&#171; ' + l(:label_previous)),
127 123 {:update => 'content',
128 124 :url => url_param.merge(page_param => paginator.current.previous),
129 125 :complete => 'window.scrollTo(0,0)'},
130 126 {:href => url_for(:params => url_param.merge(page_param => paginator.current.previous))}) + ' ' if paginator.current.previous
131 127
132 128 html << (pagination_links_each(paginator, options) do |n|
133 129 link_to_remote(n.to_s,
134 130 {:url => {:params => url_param.merge(page_param => n)},
135 131 :update => 'content',
136 132 :complete => 'window.scrollTo(0,0)'},
137 133 {:href => url_for(:params => url_param.merge(page_param => n))})
138 134 end || '')
139 135
140 136 html << ' ' + link_to_remote((l(:label_next) + ' &#187;'),
141 137 {:update => 'content',
142 138 :url => url_param.merge(page_param => paginator.current.next),
143 139 :complete => 'window.scrollTo(0,0)'},
144 140 {:href => url_for(:params => url_param.merge(page_param => paginator.current.next))}) if paginator.current.next
145 141
146 142 unless count.nil?
147 143 html << [" (#{paginator.current.first_item}-#{paginator.current.last_item}/#{count})", per_page_links(paginator.items_per_page)].compact.join(' | ')
148 144 end
149 145
150 146 html
151 147 end
152 148
153 149 def per_page_links(selected=nil)
154 150 url_param = params.dup
155 151 url_param.clear if url_param.has_key?(:set_filter)
156 152
157 153 links = Setting.per_page_options_array.collect do |n|
158 154 n == selected ? n : link_to_remote(n, {:update => "content", :url => params.dup.merge(:per_page => n)},
159 155 {:href => url_for(url_param.merge(:per_page => n))})
160 156 end
161 157 links.size > 1 ? l(:label_display_per_page, links.join(', ')) : nil
162 158 end
163 159
164 160 def breadcrumb(*args)
165 161 content_tag('p', args.join(' &#187; ') + ' &#187; ', :class => 'breadcrumb')
166 162 end
167 163
168 164 def html_title(*args)
169 165 if args.empty?
170 166 title = []
171 167 title << @project.name if @project
172 168 title += @html_title if @html_title
173 169 title << Setting.app_title
174 170 title.compact.join(' - ')
175 171 else
176 172 @html_title ||= []
177 173 @html_title += args
178 174 end
179 175 end
180 176
181 177 def accesskey(s)
182 178 Redmine::AccessKeys.key_for s
183 179 end
184 180
185 181 # Formats text according to system settings.
186 182 # 2 ways to call this method:
187 183 # * with a String: textilizable(text, options)
188 184 # * with an object and one of its attribute: textilizable(issue, :description, options)
189 185 def textilizable(*args)
190 186 options = args.last.is_a?(Hash) ? args.pop : {}
191 187 case args.size
192 188 when 1
193 189 obj = nil
194 190 text = args.shift
195 191 when 2
196 192 obj = args.shift
197 193 text = obj.send(args.shift).to_s
198 194 else
199 195 raise ArgumentError, 'invalid arguments to textilizable'
200 196 end
201 197 return '' if text.blank?
202 198
203 199 only_path = options.delete(:only_path) == false ? false : true
204 200
205 201 # when using an image link, try to use an attachment, if possible
206 202 attachments = options[:attachments] || (obj && obj.respond_to?(:attachments) ? obj.attachments : nil)
207 203
208 204 if attachments
209 205 text = text.gsub(/!((\<|\=|\>)?(\([^\)]+\))?(\[[^\]]+\])?(\{[^\}]+\})?)(\S+\.(gif|jpg|jpeg|png))!/) do |m|
210 206 style = $1
211 207 filename = $6
212 208 rf = Regexp.new(filename, Regexp::IGNORECASE)
213 209 # search for the picture in attachments
214 210 if found = attachments.detect { |att| att.filename =~ rf }
215 211 image_url = url_for :only_path => only_path, :controller => 'attachments', :action => 'download', :id => found
216 212 desc = found.description.to_s.gsub(/^([^\(\)]*).*$/, "\\1")
217 213 alt = desc.blank? ? nil : "(#{desc})"
218 214 "!#{style}#{image_url}#{alt}!"
219 215 else
220 216 "!#{style}#{filename}!"
221 217 end
222 218 end
223 219 end
224 220
225 221 text = (Setting.text_formatting == 'textile') ?
226 222 Redmine::WikiFormatting.to_html(text) { |macro, args| exec_macro(macro, obj, args) } :
227 223 simple_format(auto_link(h(text)))
228 224
229 225 # different methods for formatting wiki links
230 226 case options[:wiki_links]
231 227 when :local
232 228 # used for local links to html files
233 229 format_wiki_link = Proc.new {|project, title| "#{title}.html" }
234 230 when :anchor
235 231 # used for single-file wiki export
236 232 format_wiki_link = Proc.new {|project, title| "##{title}" }
237 233 else
238 234 format_wiki_link = Proc.new {|project, title| url_for(:only_path => only_path, :controller => 'wiki', :action => 'index', :id => project, :page => title) }
239 235 end
240 236
241 237 project = options[:project] || @project || (obj && obj.respond_to?(:project) ? obj.project : nil)
242 238
243 239 # Wiki links
244 240 #
245 241 # Examples:
246 242 # [[mypage]]
247 243 # [[mypage|mytext]]
248 244 # wiki links can refer other project wikis, using project name or identifier:
249 245 # [[project:]] -> wiki starting page
250 246 # [[project:|mytext]]
251 247 # [[project:mypage]]
252 248 # [[project:mypage|mytext]]
253 249 text = text.gsub(/(!)?(\[\[([^\]\n\|]+)(\|([^\]\n\|]+))?\]\])/) do |m|
254 250 link_project = project
255 251 esc, all, page, title = $1, $2, $3, $5
256 252 if esc.nil?
257 253 if page =~ /^([^\:]+)\:(.*)$/
258 254 link_project = Project.find_by_name($1) || Project.find_by_identifier($1)
259 255 page = $2
260 256 title ||= $1 if page.blank?
261 257 end
262 258
263 259 if link_project && link_project.wiki
264 260 # check if page exists
265 261 wiki_page = link_project.wiki.find_page(page)
266 262 link_to((title || page), format_wiki_link.call(link_project, Wiki.titleize(page)),
267 263 :class => ('wiki-page' + (wiki_page ? '' : ' new')))
268 264 else
269 265 # project or wiki doesn't exist
270 266 title || page
271 267 end
272 268 else
273 269 all
274 270 end
275 271 end
276 272
277 273 # Redmine links
278 274 #
279 275 # Examples:
280 276 # Issues:
281 277 # #52 -> Link to issue #52
282 278 # Changesets:
283 279 # r52 -> Link to revision 52
284 280 # commit:a85130f -> Link to scmid starting with a85130f
285 281 # Documents:
286 282 # document#17 -> Link to document with id 17
287 283 # document:Greetings -> Link to the document with title "Greetings"
288 284 # document:"Some document" -> Link to the document with title "Some document"
289 285 # Versions:
290 286 # version#3 -> Link to version with id 3
291 287 # version:1.0.0 -> Link to version named "1.0.0"
292 288 # version:"1.0 beta 2" -> Link to version named "1.0 beta 2"
293 289 # Attachments:
294 290 # attachment:file.zip -> Link to the attachment of the current object named file.zip
295 291 # Source files:
296 292 # source:some/file -> Link to the file located at /some/file in the project's repository
297 293 # source:some/file@52 -> Link to the file's revision 52
298 294 # source:some/file#L120 -> Link to line 120 of the file
299 295 # source:some/file@52#L120 -> Link to line 120 of the file's revision 52
300 296 # export:some/file -> Force the download of the file
301 297 text = text.gsub(%r{([\s\(,\-\>]|^)(!)?(attachment|document|version|commit|source|export)?((#|r)(\d+)|(:)([^"\s<>][^\s<>]*|"[^"]+"))(?=[[:punct:]]|\s|<|$)}) do |m|
302 298 leading, esc, prefix, sep, oid = $1, $2, $3, $5 || $7, $6 || $8
303 299 link = nil
304 300 if esc.nil?
305 301 if prefix.nil? && sep == 'r'
306 302 if project && (changeset = project.changesets.find_by_revision(oid))
307 303 link = link_to("r#{oid}", {:only_path => only_path, :controller => 'repositories', :action => 'revision', :id => project, :rev => oid},
308 304 :class => 'changeset',
309 305 :title => truncate_single_line(changeset.comments, 100))
310 306 end
311 307 elsif sep == '#'
312 308 oid = oid.to_i
313 309 case prefix
314 310 when nil
315 311 if issue = Issue.find_by_id(oid, :include => [:project, :status], :conditions => Project.visible_by(User.current))
316 312 link = link_to("##{oid}", {:only_path => only_path, :controller => 'issues', :action => 'show', :id => oid},
317 313 :class => (issue.closed? ? 'issue closed' : 'issue'),
318 314 :title => "#{truncate(issue.subject, 100)} (#{issue.status.name})")
319 315 link = content_tag('del', link) if issue.closed?
320 316 end
321 317 when 'document'
322 318 if document = Document.find_by_id(oid, :include => [:project], :conditions => Project.visible_by(User.current))
323 319 link = link_to h(document.title), {:only_path => only_path, :controller => 'documents', :action => 'show', :id => document},
324 320 :class => 'document'
325 321 end
326 322 when 'version'
327 323 if version = Version.find_by_id(oid, :include => [:project], :conditions => Project.visible_by(User.current))
328 324 link = link_to h(version.name), {:only_path => only_path, :controller => 'versions', :action => 'show', :id => version},
329 325 :class => 'version'
330 326 end
331 327 end
332 328 elsif sep == ':'
333 329 # removes the double quotes if any
334 330 name = oid.gsub(%r{^"(.*)"$}, "\\1")
335 331 case prefix
336 332 when 'document'
337 333 if project && document = project.documents.find_by_title(name)
338 334 link = link_to h(document.title), {:only_path => only_path, :controller => 'documents', :action => 'show', :id => document},
339 335 :class => 'document'
340 336 end
341 337 when 'version'
342 338 if project && version = project.versions.find_by_name(name)
343 339 link = link_to h(version.name), {:only_path => only_path, :controller => 'versions', :action => 'show', :id => version},
344 340 :class => 'version'
345 341 end
346 342 when 'commit'
347 343 if project && (changeset = project.changesets.find(:first, :conditions => ["scmid LIKE ?", "#{name}%"]))
348 344 link = link_to h("#{name}"), {:only_path => only_path, :controller => 'repositories', :action => 'revision', :id => project, :rev => changeset.revision},
349 345 :class => 'changeset',
350 346 :title => truncate_single_line(changeset.comments, 100)
351 347 end
352 348 when 'source', 'export'
353 349 if project && project.repository
354 350 name =~ %r{^[/\\]*(.*?)(@([0-9a-f]+))?(#(L\d+))?$}
355 351 path, rev, anchor = $1, $3, $5
356 352 link = link_to h("#{prefix}:#{name}"), {:controller => 'repositories', :action => 'entry', :id => project, :path => path,
357 353 :rev => rev,
358 354 :anchor => anchor,
359 355 :format => (prefix == 'export' ? 'raw' : nil)},
360 356 :class => (prefix == 'export' ? 'source download' : 'source')
361 357 end
362 358 when 'attachment'
363 359 if attachments && attachment = attachments.detect {|a| a.filename == name }
364 360 link = link_to h(attachment.filename), {:only_path => only_path, :controller => 'attachments', :action => 'download', :id => attachment},
365 361 :class => 'attachment'
366 362 end
367 363 end
368 364 end
369 365 end
370 366 leading + (link || "#{prefix}#{sep}#{oid}")
371 367 end
372 368
373 369 text
374 370 end
375 371
376 372 # Same as Rails' simple_format helper without using paragraphs
377 373 def simple_format_without_paragraph(text)
378 374 text.to_s.
379 375 gsub(/\r\n?/, "\n"). # \r\n and \r -> \n
380 376 gsub(/\n\n+/, "<br /><br />"). # 2+ newline -> 2 br
381 377 gsub(/([^\n]\n)(?=[^\n])/, '\1<br />') # 1 newline -> br
382 378 end
383 379
384 380 def error_messages_for(object_name, options = {})
385 381 options = options.symbolize_keys
386 382 object = instance_variable_get("@#{object_name}")
387 383 if object && !object.errors.empty?
388 384 # build full_messages here with controller current language
389 385 full_messages = []
390 386 object.errors.each do |attr, msg|
391 387 next if msg.nil?
392 388 msg = msg.first if msg.is_a? Array
393 389 if attr == "base"
394 390 full_messages << l(msg)
395 391 else
396 392 full_messages << "&#171; " + (l_has_string?("field_" + attr) ? l("field_" + attr) : object.class.human_attribute_name(attr)) + " &#187; " + l(msg) unless attr == "custom_values"
397 393 end
398 394 end
399 395 # retrieve custom values error messages
400 396 if object.errors[:custom_values]
401 397 object.custom_values.each do |v|
402 398 v.errors.each do |attr, msg|
403 399 next if msg.nil?
404 400 msg = msg.first if msg.is_a? Array
405 401 full_messages << "&#171; " + v.custom_field.name + " &#187; " + l(msg)
406 402 end
407 403 end
408 404 end
409 405 content_tag("div",
410 406 content_tag(
411 407 options[:header_tag] || "span", lwr(:gui_validation_error, full_messages.length) + ":"
412 408 ) +
413 409 content_tag("ul", full_messages.collect { |msg| content_tag("li", msg) }),
414 410 "id" => options[:id] || "errorExplanation", "class" => options[:class] || "errorExplanation"
415 411 )
416 412 else
417 413 ""
418 414 end
419 415 end
420 416
421 417 def lang_options_for_select(blank=true)
422 418 (blank ? [["(auto)", ""]] : []) +
423 419 GLoc.valid_languages.collect{|lang| [ ll(lang.to_s, :general_lang_name), lang.to_s]}.sort{|x,y| x.last <=> y.last }
424 420 end
425 421
426 422 def label_tag_for(name, option_tags = nil, options = {})
427 423 label_text = l(("field_"+field.to_s.gsub(/\_id$/, "")).to_sym) + (options.delete(:required) ? @template.content_tag("span", " *", :class => "required"): "")
428 424 content_tag("label", label_text)
429 425 end
430 426
431 427 def labelled_tabular_form_for(name, object, options, &proc)
432 428 options[:html] ||= {}
433 429 options[:html][:class] = 'tabular' unless options[:html].has_key?(:class)
434 430 form_for(name, object, options.merge({ :builder => TabularFormBuilder, :lang => current_language}), &proc)
435 431 end
436 432
437 433 def back_url_hidden_field_tag
438 434 hidden_field_tag 'back_url', (params[:back_url] || request.env['HTTP_REFERER'])
439 435 end
440 436
441 437 def check_all_links(form_name)
442 438 link_to_function(l(:button_check_all), "checkAll('#{form_name}', true)") +
443 439 " | " +
444 440 link_to_function(l(:button_uncheck_all), "checkAll('#{form_name}', false)")
445 441 end
446 442
447 443 def progress_bar(pcts, options={})
448 444 pcts = [pcts, pcts] unless pcts.is_a?(Array)
449 445 pcts[1] = pcts[1] - pcts[0]
450 446 pcts << (100 - pcts[1] - pcts[0])
451 447 width = options[:width] || '100px;'
452 448 legend = options[:legend] || ''
453 449 content_tag('table',
454 450 content_tag('tr',
455 451 (pcts[0] > 0 ? content_tag('td', '', :width => "#{pcts[0].floor}%;", :class => 'closed') : '') +
456 452 (pcts[1] > 0 ? content_tag('td', '', :width => "#{pcts[1].floor}%;", :class => 'done') : '') +
457 453 (pcts[2] > 0 ? content_tag('td', '', :width => "#{pcts[2].floor}%;", :class => 'todo') : '')
458 454 ), :class => 'progress', :style => "width: #{width};") +
459 455 content_tag('p', legend, :class => 'pourcent')
460 456 end
461 457
462 458 def context_menu_link(name, url, options={})
463 459 options[:class] ||= ''
464 460 if options.delete(:selected)
465 461 options[:class] << ' icon-checked disabled'
466 462 options[:disabled] = true
467 463 end
468 464 if options.delete(:disabled)
469 465 options.delete(:method)
470 466 options.delete(:confirm)
471 467 options.delete(:onclick)
472 468 options[:class] << ' disabled'
473 469 url = '#'
474 470 end
475 471 link_to name, url, options
476 472 end
477 473
478 474 def calendar_for(field_id)
479 475 include_calendar_headers_tags
480 476 image_tag("calendar.png", {:id => "#{field_id}_trigger",:class => "calendar-trigger"}) +
481 477 javascript_tag("Calendar.setup({inputField : '#{field_id}', ifFormat : '%Y-%m-%d', button : '#{field_id}_trigger' });")
482 478 end
483 479
484 480 def include_calendar_headers_tags
485 481 unless @calendar_headers_tags_included
486 482 @calendar_headers_tags_included = true
487 483 content_for :header_tags do
488 484 javascript_include_tag('calendar/calendar') +
489 485 javascript_include_tag("calendar/lang/calendar-#{current_language}.js") +
490 486 javascript_include_tag('calendar/calendar-setup') +
491 487 stylesheet_link_tag('calendar')
492 488 end
493 489 end
494 490 end
495 491
496 492 def wikitoolbar_for(field_id)
497 493 return '' unless Setting.text_formatting == 'textile'
498 494
499 495 help_link = l(:setting_text_formatting) + ': ' +
500 496 link_to(l(:label_help), compute_public_path('wiki_syntax', 'help', 'html'),
501 497 :onclick => "window.open(\"#{ compute_public_path('wiki_syntax', 'help', 'html') }\", \"\", \"resizable=yes, location=no, width=300, height=640, menubar=no, status=no, scrollbars=yes\"); return false;")
502 498
503 499 javascript_include_tag('jstoolbar/jstoolbar') +
504 500 javascript_include_tag("jstoolbar/lang/jstoolbar-#{current_language}") +
505 501 javascript_tag("var toolbar = new jsToolBar($('#{field_id}')); toolbar.setHelpLink('#{help_link}'); toolbar.draw();")
506 502 end
507 503
508 504 def content_for(name, content = nil, &block)
509 505 @has_content ||= {}
510 506 @has_content[name] = true
511 507 super(name, content, &block)
512 508 end
513 509
514 510 def has_content?(name)
515 511 (@has_content && @has_content[name]) || false
516 512 end
517 513 end
General Comments 0
You need to be logged in to leave comments. Login now