aboutsummaryrefslogtreecommitdiffstats
path: root/app/helpers/link_to_helper.rb
blob: 238a36ce41f92abd2e60d8c6409a0b45c06689f1 (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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# app/helpers/link_to_helper.rb:
# This module is included into all controllers via controllers/application.rb
# -
#
# Copyright (c) 2007 UK Citizens Online Democracy. All rights reserved.
# Email: hello@mysociety.org; WWW: http://www.mysociety.org/

module LinkToHelper

    # Links to various models

    # Requests
    def request_url(info_request, options = {})
        show_request_url({:url_title => info_request.url_title}.merge(options))
    end

    def request_path(info_request, options = {})
        request_url(info_request, {:only_path => true}.merge(options))
    end

    def request_link(info_request, cls=nil )
        link_to h(info_request.title), request_path(info_request), :class => cls
    end

    def request_details_path(info_request)
        details_request_path(:url_title => info_request.url_title)
    end

    # Incoming / outgoing messages
    def incoming_message_url(incoming_message, options = {})
        return request_url(incoming_message.info_request, options.merge(:anchor => "incoming-#{incoming_message.id}"))
    end

    def incoming_message_path(incoming_message)
        incoming_message_url(incoming_message, :only_path => true)
    end

    def outgoing_message_url(outgoing_message, options = {})
        request_url(outgoing_message.info_request, options.merge(:anchor => "outgoing-#{outgoing_message.id}"))
    end

    def outgoing_message_path(outgoing_message)
        outgoing_message_url(outgoing_message, :only_path => true)
    end

    def comment_url(comment, options = {})
        request_url(comment.info_request, options.merge(:anchor => "comment-#{comment.id}"))
    end

    def comment_path(comment)
        comment_url(comment, :only_path => true)
    end

    # Respond to request
    def respond_to_last_url(info_request, options = {})
        last_response = info_request.get_last_response
        if last_response.nil?
            show_response_no_followup_url(options.merge(:id => info_request.id))
        else
            show_response_url(options.merge(:id => info_request.id, :incoming_message_id => last_response.id))
        end
    end

    def respond_to_last_path(info_request)
        respond_to_last_url(info_request, :only_path => true)
    end

    # Public bodies
    def public_body_url(public_body, options = {})
        public_body.url_name.nil? ? '' : show_public_body_url(options.merge(:url_name => public_body.url_name))
    end

    def public_body_path(public_body)
        public_body_url(public_body, :only_path => true)
    end

    def public_body_link_short(public_body)
        link_to h(public_body.short_or_long_name), public_body_path(public_body)
    end

    def public_body_link(public_body, cls=nil)
        link_to h(public_body.name), public_body_path(public_body), :class => cls
    end

    def public_body_link_absolute(public_body) # e.g. for in RSS
        link_to h(public_body.name), public_body_url(public_body)
    end

    # Users
    def user_url(user, options = {})
        show_user_url(options.merge(:url_name => user.url_name))
    end

    def user_path(user)
        user_url(user, :only_path => true)
    end

    def user_link(user, cls=nil)
        link_to h(user.name), user_path(user), :class => cls
    end

    def user_link_for_request(request, cls=nil)
        if request.is_external?
            user_name = request.external_user_name || _("Anonymous user")
            if !request.external_url.nil?
                link_to h(user_name), request.external_url
            else
                user_name
            end
        else
            link_to h(request.user.name), user_path(request.user), :class => cls
        end
    end

    def user_admin_link_for_request(request, external_text=nil, internal_text=nil)
        if request.is_external?
            external_text || (request.external_user_name || _("Anonymous user")) + " (external)"
        else
            link_to(h(internal_text || request.user.name), admin_user_show_url(request.user))
        end
    end

    def user_link_absolute(user)
        link_to h(user.name), user_url(user)
    end

    def user_link(user)
        link_to h(user.name), user_path(user)
    end

    def request_user_link_absolute(request)
        if request.is_external?
            request.external_user_name || _("Anonymous user")
        else
            user_link_absolute(request.user)
        end
    end

    def request_user_link(request)
        if request.is_external?
            request.external_user_name || _("Anonymous user")
        else
            user_link(request.user)
        end
    end

    def user_or_you_link(user)
        if @user && user == @user
            link_to h("you"), user_path(user)
        else
            link_to h(user.name), user_path(user)
        end
    end

    def user_or_you_capital(user)
        if @user && user == @user
            return h("You")
        else
            return h(user.name)
        end
    end

    def user_or_you_capital_link(user)
        link_to user_or_you_capital(user), user_path(user)
    end

    def user_admin_link(user, name="admin", cls=nil)
      link_to name, admin_user_show_url(user), :class => cls
    end

    # Tracks. feed can be 'track' or 'feed'
    def do_track_url(track_thing, feed = 'track', options = {})
        if track_thing.track_type == 'request_updates'
            track_request_url(options.merge(:url_title => track_thing.info_request.url_title, :feed => feed))
        elsif track_thing.track_type == 'all_new_requests'
            track_list_url(options.merge(:view => 'recent', :feed => feed))
        elsif track_thing.track_type == 'all_successful_requests'
            track_list_url(options.merge(:view => 'successful', :feed => feed))
        elsif track_thing.track_type == 'public_body_updates'
            track_public_body_url(options.merge(:url_name => track_thing.public_body.url_name, :feed => feed))
        elsif track_thing.track_type == 'user_updates'
            track_user_url(options.merge(:url_name => track_thing.tracked_user.url_name, :feed => feed))
        elsif track_thing.track_type == 'search_query'
            track_search_url(options.merge(:query_array => track_thing.track_query, :feed => feed))
        else
            raise "unknown tracking type " + track_thing.track_type
        end
    end

    def do_track_path(track_thing, feed = 'track')
        do_track_url(track_thing, feed, :only_path => true)
    end

    # General pages.
    def search_url(query, params = nil)
        if query.kind_of?(Array)
            query = query - ["", nil]
            query = query.join("/")
        end
        routing_info = {:controller => 'general',
                        :action => 'search',
                        :combined => query,
                        :view => nil}
        if !params.nil?
            routing_info = params.merge(routing_info)
        end
        url = url_for(routing_info)
        # Here we can't escape the slashes, as RFC 2396 doesn't allow slashes
        # within a path component. Rails is assuming when generating URLs that
        # either there aren't slashes, or we are in a query part where you can
        # have escaped slashes. Apache complains if you do include slashes
        # within a path component.
        # See http://www.webmasterworld.com/apache/3279075.htm
        # and also 3.3 of http://www.ietf.org/rfc/rfc2396.txt
        # It turns out this is a regression in Rails 2.1, caused by this bug fix:
        #   http://rails.lighthouseapp.com/projects/8994/tickets/144-patch-bug-in-rails-route-globbing
        url = url.gsub("%2F", "/")

        return url
    end
    def search_link(query, variety_postfix = nil, sort_postfix = nil, advanced = nil)
        link_to h(query), search_url(query)
    end

    def search_path(query, options = {})
        search_url(query, options.merge(:only_path => true))
    end

    def search_link(query)
        link_to h(query), search_url(query)
    end

    # Deprecated helper
    # TODO: Remove in next release
    def admin_url(relative_path)
        warn "[DEPRECATION] admin_url is deprecated. Please remove it from your theme."
        relative_path
    end

    # Deprecated helper
    # TODO: Remove in next release
    def main_url(relative_path, append = nil)
        warn "[DEPRECATION] main_url is deprecated. Please remove it from your theme."
        url_prefix = "http://" + AlaveteliConfiguration::domain
        url = url_prefix + relative_path
        if !append.nil?
            begin
                env = Rack::MockRequest.env_for(url)
                req = Rack::Request.new(env)
                req.path_info += append
                url = req.url
            rescue URI::InvalidURIError
                # don't append to it
            end
        end
        return url
    end

    # About page URLs
    def about_url
        return help_general_url(:action => 'about')
    end

    def unhappy_url(info_request = nil)
        if info_request.nil?
            return help_general_url(:action => 'unhappy')
        else
            return help_unhappy_url(:url_title => info_request.url_title)
        end
    end

    # Basic date format
    def simple_date(date)
        date = date.in_time_zone.to_date unless date.is_a? Date

        date_format = _("simple_date_format")
        date_format = :long if date_format == "simple_date_format"
        return I18n.l(date, :format => date_format)
    end

    def simple_time(date)
        return date.strftime("%H:%M:%S").strip
    end

    def year_from_date(date)
        return date.strftime("%Y").strip
    end

    #I18n locale switcher

    def locale_switcher(locale, params)
        params['locale'] = locale
        return url_for(params)
    end

end