]> git.openstreetmap.org Git - rails.git/blob - app/helpers/application_helper.rb
try and make the OpenStreetBugs API a bit more consistent with the rest of the API
[rails.git] / app / helpers / application_helper.rb
1 module ApplicationHelper
2   require 'rexml/document'
3
4   def htmlize(text)
5     return linkify(sanitize(simple_format(text)))
6   end
7
8   def linkify(text)
9     return auto_link(text, :link => :urls, :html => { :rel => "nofollow" })
10   end
11
12   def html_escape_unicode(text)
13     chars = ActiveSupport::Multibyte::Chars.u_unpack(text).map do |c|
14       c < 127 ? c.chr : "&##{c.to_s};"
15     end
16
17     return chars.join("")
18   end
19
20   def rss_link_to(*args)
21     return link_to(image_tag("RSS.gif", :size => "16x16", :border => 0), Hash[*args], { :class => "rsssmall" });
22   end
23
24   def atom_link_to(*args)
25     return link_to(image_tag("RSS.gif", :size => "16x16", :border => 0), Hash[*args], { :class => "rsssmall" });
26   end
27
28   def javascript_strings
29     js = ""
30
31     js << "<script type='text/javascript'>\n"
32     js << "i18n_strings = new Array();\n"
33     js << javascript_strings_for_key("javascripts")
34     js << "</script>\n"
35
36     return js
37   end
38
39   def describe_location(lat, lon, zoom = nil, language = nil)
40     zoom = zoom || 14
41     language = language || request.user_preferred_languages.join(',')
42     url = "http://nominatim.openstreetmap.org/reverse?lat=#{lat}&lon=#{lon}&zoom=#{zoom}&accept-language=#{language}"
43     response = REXML::Document.new(Net::HTTP.get(URI.parse(url)))
44
45     if result = response.get_text("reversegeocode/result")
46       result.to_s
47     else
48       "#{number_with_precision(lat, :precision => 3)}, #{number_with_precision(lon, :precision => 3)}"
49     end
50   end
51
52   def user_image(user, options = {})
53     options[:class] ||= "user_image"
54
55     if user.image
56       image_tag url_for_file_column(user, "image"), options
57     else
58       image_tag "anon_large.png", options
59     end
60   end
61
62   def user_thumbnail(user, options = {})
63     options[:class] ||= "user_thumbnail"
64
65     if user.image
66       image_tag url_for_file_column(user, "image"), options
67     else
68       image_tag "anon_small.png", options
69     end
70   end
71
72 private
73
74   def javascript_strings_for_key(key)
75     js = ""
76     value = t(key, :locale => "en")
77
78     if value.is_a?(String)
79       js << "i18n_strings['#{key}'] = '" << escape_javascript(t(key)) << "';\n"
80     else
81       value.each_key do |k|
82         js << javascript_strings_for_key("#{key}.#{k}")
83       end
84     end
85
86     return js
87   end
88 end