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