aboutsummaryrefslogtreecommitdiffstats
path: root/app/helpers/application_helper.rb
blob: 33525cb3d5ad2e0c3704809d7494d2d2e65f6026 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# app/helpers/application_helper.rb:
# Methods added to this helper will be available to all views (i.e. templates)
# in the application.
#
# Copyright (c) 2007 UK Citizens Online Democracy. All rights reserved.
# Email: hello@mysociety.org; WWW: http://www.mysociety.org/

require 'languages'

module ApplicationHelper
    # URL generating functions are needed by all controllers (for redirects),
    # views (for links) and mailers (for use in emails), so include them into
    # all of all.
    include LinkToHelper

    # Site-wide access to configuration settings
    include ConfigHelper

    # Useful for sending emails
    include MailerHelper

    # Copied from error_messages_for in active_record_helper.rb
    def foi_error_messages_for(*params)
        options = params.last.is_a?(Hash) ? params.pop.symbolize_keys : {}
        objects = params.collect {|object_name| instance_variable_get("@#{object_name}") }.compact
        count   = objects.inject(0) {|sum, object| sum + object.errors.count }
        unless count.zero?
            html = {}
            [:id, :class].each do |key|
              if options.include?(key)
                  value = options[key]
                  html[key] = value unless value.blank?
              else
                  html[key] = 'errorExplanation'
              end
          end

          error_messages = "".html_safe
          for object in objects
              object.errors.each do |attr, message|
                  error_messages << content_tag(:li, h(message))
              end
          end

          content_tag(:div,
              content_tag(:ul, error_messages),
            html
          )
        else
            ''
        end
    end

    # Highlight words, also escapes HTML (other than spans that we add)
    def highlight_words(t, words, html = true)
        if html
            highlight(h(t), words, :highlighter => '<span class="highlight">\1</span>').html_safe
        else
            highlight(t, words, :highlighter => '*\1*')
        end
    end

    def highlight_and_excerpt(t, words, excount, html = true)
        newt = excerpt(t, words[0], :radius => excount)
        if not newt
            newt = excerpt(t, '', :radius => excount)
        end
        t = newt
        t = highlight_words(t, words, html)
        return t
    end

    def locale_name(locale)
        return LanguageNames::get_language_name(locale)
    end

    # (unfortunately) ugly way of getting id of generated form element
    # ids
    # see http://chrisblunt.com/2009/10/12/rails-getting-the-id-of-form-fields-inside-a-fields_for-block/
    def sanitized_object_name(object_name)
        object_name.gsub(/\]\[|[^-a-zA-Z0-9:.]/,"_").sub(/_$/,"")
    end

    def sanitized_method_name(method_name)
        method_name.sub(/\?$/, "")
    end

    def form_tag_id(object_name, method_name, locale=nil)
    if locale.nil?
            return "#{sanitized_object_name(object_name.to_s)}_#{sanitized_method_name(method_name.to_s)}"
        else
            return "#{sanitized_object_name(object_name.to_s)}_#{sanitized_method_name(method_name.to_s)}__#{locale.to_s}"
        end
    end

    def admin_value(v)
      if v.nil?
        nil
      elsif v.instance_of?(Time)
        admin_date(v)
      else
        h(v)
      end
    end

    def admin_date(date)
        ago_text = _('{{length_of_time}} ago', :length_of_time => time_ago_in_words(date))
        exact_date = I18n.l(date, :format => "%e %B %Y %H:%M:%S")
        return "#{exact_date} (#{ago_text})"
    end

    # Note that if the admin interface is proxied via another server, we can't
    # rely on a sesssion being shared between the front end and admin interface,
    # so need to check the status of the user.
    def is_admin?
      return !session[:using_admin].nil? || (!@user.nil? && @user.super?)
    end

    def cache_if_caching_fragments(*args, &block)
        if AlaveteliConfiguration::cache_fragments
            cache(*args) { yield }
        else
            yield
        end
    end

    # We only want to cache request lists that have a reasonable chance of not expiring
    # before they're requested again. Don't cache lists returned from specific searches
    # or anything except the first page of results, just the first page of the default
    # views
    def request_list_cache_key
        cacheable_param_list = ['controller', 'action', 'locale', 'view']
        if params.keys.all?{ |key| cacheable_param_list.include?(key) }
            "request-list-#{@view}-#{@locale}"
        else
            nil
        end
    end

    def event_description(event)
        body_link = public_body_link_absolute(event.info_request.public_body)
        user_link = request_user_link_absolute(event.info_request)
        date = simple_date(event.created_at)
        case event.event_type
        when 'sent'
            _('Request sent to {{public_body_name}} by {{info_request_user}} on {{date}}.',
             :public_body_name => body_link,
             :info_request_user => user_link,
             :date => date)
        when 'followup_sent'
            case event.calculated_state
            when 'internal_review'
                _('Internal review request sent to {{public_body_name}} by {{info_request_user}} on {{date}}.',
                 :public_body_name => body_link,
                 :info_request_user => user_link,
                 :date => date)
            when 'waiting_response'
                _('Clarification sent to {{public_body_name}} by {{info_request_user}} on {{date}}.',
                 :public_body_name => body_link,
                 :info_request_user => user_link,
                 :date => date)
            else
                _('Follow up sent to {{public_body_name}} by {{info_request_user}} on {{date}}.',
                 :public_body_name => body_link,
                 :info_request_user => user_link,
                 :date => date)
            end
        when 'response'
            _('Response by {{public_body_name}} to {{info_request_user}} on {{date}}.',
             :public_body_name => body_link,
             :info_request_user => user_link,
             :date => date)
        when 'comment'
            _('Request to {{public_body_name}} by {{info_request_user}}. Annotated by {{event_comment_user}} on {{date}}.',
             :public_body_name => body_link,
             :info_request_user => user_link,
             :event_comment_user => user_link_absolute(event.comment.user),
             :date => date)
        end
    end
end