]> git.openstreetmap.org Git - rails.git/blob - app/helpers/application_helper.rb
Interpolation variables should not be translated...
[rails.git] / app / helpers / application_helper.rb
1 module ApplicationHelper
2   require 'rexml/document'
3
4   def sanitize(text)
5     Sanitize.clean(text, Sanitize::Config::OSM)
6   end
7
8   def htmlize(text)
9     return linkify(sanitize(simple_format(text)))
10   end
11
12   def linkify(text)
13     return auto_link(text, :link => :urls, :html => { :rel => "nofollow" })
14   end
15
16   def html_escape_unicode(text)
17     chars = ActiveSupport::Multibyte::Chars.u_unpack(text).map do |c|
18       c < 127 ? c.chr : "&##{c.to_s};"
19     end
20
21     return chars.join("")
22   end
23
24   def rss_link_to(*args)
25     return link_to(image_tag("RSS.gif", :size => "16x16", :border => 0), Hash[*args], { :class => "rsssmall" });
26   end
27
28   def atom_link_to(*args)
29     return link_to(image_tag("RSS.gif", :size => "16x16", :border => 0), Hash[*args], { :class => "rsssmall" });
30   end
31
32   def javascript_strings
33     js = ""
34
35     js << "<script type='text/javascript'>\n"
36     js << "i18n_strings = new Array();\n"
37     js << javascript_strings_for_key("javascripts")
38     js << "</script>\n"
39
40     return js
41   end
42
43   def style_rules
44     css = ""
45
46     css << ".hidden { display: none }";
47     css << ".hide_unless_logged_in { display: none }" unless @user;
48     css << ".hide_if_logged_in { display: none }" if @user;
49     css << ".hide_if_user_#{@user.id} { display: none }" if @user;
50     css << ".show_if_user_#{@user.id} { display: inline }" if @user;
51     css << ".hide_unless_administrator { display: none }" unless @user and @user.administrator?;
52
53     return content_tag(:style, css)
54   end
55
56   def if_logged_in(tag = :div, &block)
57     concat(content_tag(tag, capture(&block), :class => "hide_unless_logged_in"))
58   end
59
60   def if_not_logged_in(tag = :div, &block)
61     concat(content_tag(tag, capture(&block), :class => "hide_if_logged_in"))
62   end
63
64   def if_user(user, tag = :div, &block)
65     if user
66       concat(content_tag(tag, capture(&block), :class => "hidden show_if_user_#{user.id}"))
67     end
68   end
69
70   def unless_user(user, tag = :div, &block)
71     if user
72       concat(content_tag(tag, capture(&block), :class => "hide_if_user_#{user.id}"))
73     else
74       concat(content_tag(tag, capture(&block)))
75     end
76   end
77
78   def if_administrator(tag = :div, &block)
79     concat(content_tag(tag, capture(&block), :class => "hide_unless_administrator"))
80   end
81
82   def describe_location(lat, lon, zoom = nil, language = nil)
83     zoom = zoom || 14
84     language = language || request.user_preferred_languages.join(',')
85     url = "http://nominatim.openstreetmap.org/reverse?lat=#{lat}&lon=#{lon}&zoom=#{zoom}&accept-language=#{language}"
86
87     begin
88       response = Timeout::timeout(4) do
89         REXML::Document.new(Net::HTTP.get(URI.parse(url)))
90       end
91     rescue Exception
92       response = nil
93     end
94
95     if response and result = response.get_text("reversegeocode/result")
96       result.to_s
97     else
98       "#{number_with_precision(lat, :precision => 3)}, #{number_with_precision(lon, :precision => 3)}"
99     end
100   end
101
102   def user_image(user, options = {})
103     options[:class] ||= "user_image"
104
105     if user.image
106       image_tag url_for_file_column(user, "image"), options
107     else
108       image_tag "anon_large.png", options
109     end
110   end
111
112   def user_thumbnail(user, options = {})
113     options[:class] ||= "user_thumbnail"
114
115     if user.image
116       image_tag url_for_file_column(user, "image"), options
117     else
118       image_tag "anon_small.png", options
119     end
120   end
121
122   def preferred_editor
123     if params[:editor]
124       params[:editor]
125     elsif @user and @user.preferred_editor
126       @user.preferred_editor
127     else
128       DEFAULT_EDITOR
129     end
130   end
131
132 private
133
134   def javascript_strings_for_key(key)
135     js = ""
136     value = I18n.t(key, :locale => "en")
137
138     if value.is_a?(String)
139       js << "i18n_strings['#{key}'] = '" << escape_javascript(t(key)) << "';\n"
140     else
141       value.each_key do |k|
142         js << javascript_strings_for_key("#{key}.#{k}")
143       end
144     end
145
146     return js
147   end
148 end