aboutsummaryrefslogtreecommitdiffstats
path: root/app/controllers/body_controller.rb
blob: 7a07daf39203508a250d1907763c4aa0e763a3ab (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
# app/controllers/body_controller.rb:
# Show information about a public body.
#
# Copyright (c) 2007 UK Citizens Online Democracy. All rights reserved.
# Email: francis@mysociety.org; WWW: http://www.mysociety.org/
#
# $Id: body_controller.rb,v 1.19 2008-10-17 11:37:59 francis Exp $

class BodyController < ApplicationController
    # XXX tidy this up with better error messages, and a more standard infrastructure for the redirect to canonical URL
    def show
        if MySociety::Format.simplify_url_part(params[:url_name]) != params[:url_name]
            redirect_to :url_name =>  MySociety::Format.simplify_url_part(params[:url_name])
            return
        end

        @public_bodies = PublicBody.find(:all, :conditions => [ "url_name = ?", params[:url_name] ])
        if @public_bodies.size > 1
            raise "Two bodies with the same URL name: " . params[:url_name]
        end
        # If none found, then search the history of short names, and do a redirect
        if @public_bodies.size == 0
            @public_bodies = PublicBody.find(:all,
                :conditions => [ "id in (select public_body_id from public_body_versions where url_name = ?)", params[:url_name] ])
            if @public_bodies.size > 1
                raise "Two bodies with the same historical URL name: " . params[:url_name]
            end
            if @public_bodies.size == 0
                raise "None found" # XXX proper 404
            end
            redirect_to show_public_body_url(:url_name => @public_bodies[0].url_name)
        end
        @public_body = @public_bodies[0]
        set_last_body(@public_body)

        @track_thing = TrackThing.create_track_for_public_body(@public_body)
        @feed_autodetect = [ { :url => do_track_url(@track_thing, 'feed'), :title => @track_thing.params[:title_in_rss] } ]
    end

    def view_email
        @public_bodies = PublicBody.find(:all, :conditions => [ "url_name = ?", params[:url_name] ])
        @public_body = @public_bodies[0]

        if params[:submitted_view_email]
            if verify_recaptcha
                flash[:error] = nil
                render :template => "body/view_email"
                return
            end
            flash[:error] = "There was an error with the words you entered, please try again."
        end
        render :template => "body/view_email_captcha"
    end

    def list
        @tag = params[:tag]
        if @tag.nil?
            @tag = "all"
            conditions = []
        elsif @tag == 'other'
            category_list = PublicBody.categories.map{|c| "'"+c+"'"}.join(",")
            conditions = ['(select count(*) from public_body_tags where public_body_tags.public_body_id = public_bodies.id
                and public_body_tags.name in (' + category_list + ')) = 0']
        elsif @tag.size == 1
            @tag.upcase!
            conditions = ['first_letter = ?', @tag]
        else
            conditions = ['(select count(*) from public_body_tags where public_body_tags.public_body_id = public_bodies.id
                and public_body_tags.name = ?) > 0', @tag]
        end
        @public_bodies = PublicBody.paginate(
            :order => "public_bodies.name", :page => params[:page], :per_page => 1000, # fit all councils on one page
            :conditions => conditions
            )
        if @tag.size == 1
            @description = "beginning with '" + @tag + "'"
        else
            @description = PublicBody.categories_by_tag[@tag]
            if @description.nil?
                @description = @tag
            end
        end
    end
end