aboutsummaryrefslogtreecommitdiffstats
path: root/app/controllers/admin_public_body_controller.rb
blob: 74a3a86c6eceaa5f65137d929de719986544eec2 (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
# app/controllers/admin_public_body_controller.rb:
# Controller for editing public bodies from the admin interface.
#
# Copyright (c) 2007 UK Citizens Online Democracy. All rights reserved.
# Email: francis@mysociety.org; WWW: http://www.mysociety.org/
#
# $Id: admin_public_body_controller.rb,v 1.23 2009-08-26 00:58:29 francis Exp $

class AdminPublicBodyController < AdminController
    def index
        list
        render :action => 'list'
    end

    def _lookup_query_internal
        @query = params[:query]
        if @query == ""
            @query = nil
        end
        @page = params[:page]
        if @page == ""
            @page = nil
        end
        @public_bodies = PublicBody.paginate :order => "name", :page => @page, :per_page => 100,
            :conditions =>  @query.nil? ? nil : ["lower(name) like lower('%'||?||'%') or 
                             lower(short_name) like lower('%'||?||'%') or 
                             lower(request_email) like lower('%'||?||'%')", @query, @query, @query]
        @public_bodies_by_tag = PublicBody.find_by_tag(@query) 
    end

    def list
        self._lookup_query_internal
    end

    def mass_tag_add
        self._lookup_query_internal

        if params[:new_tag] and params[:new_tag] != ""
            if params[:table_name] == 'exact':
                bodies = @public_bodies_by_tag
            elsif params[:table_name] == 'substring':
                bodies = @public_bodies
            else
                raise "Unknown table_name " + params[:table_name]
            end
            for body in bodies
                body.add_tag_if_not_already_present(params[:new_tag])
            end
            flash[:notice] = "Added tag to table of bodies."
        end

        redirect_to admin_url('body/list') + "?query=" + @query + (@page.nil? ? "" : "&page=" + @page) # XXX construct this URL properly
    end

    def missing_scheme
        # There might be a way to do this in ActiveRecord, but I can't find it
        @public_bodies = PublicBody.find_by_sql("
            SELECT a.id, a.name, a.url_name, COUNT(*) AS howmany 
              FROM public_bodies a JOIN info_requests r ON a.id = r.public_body_id 
             WHERE a.publication_scheme = '' 
             GROUP BY a.id, a.name, a.url_name 
             ORDER BY howmany DESC 
             LIMIT 20
        ")
        @stats = {
          "total" => PublicBody.count,
          "entered" => PublicBody.count(:conditions => "publication_scheme != ''")
        }
    end

    def show
        @public_body = PublicBody.find(params[:id])
    end

    def new
        @public_body = PublicBody.new
    end

    def create
        params[:public_body][:last_edit_editor] = admin_http_auth_user()
        @public_body = PublicBody.new(params[:public_body])
        if @public_body.save
            flash[:notice] = 'PublicBody was successfully created.'
            redirect_to admin_url('body/show/' + @public_body.id.to_s)
        else
            render :action => 'new'
        end
    end

    def edit
        @public_body = PublicBody.find(params[:id])
        @public_body.last_edit_comment = ""
    end

    def update
        params[:public_body][:last_edit_editor] = admin_http_auth_user()
        @public_body = PublicBody.find(params[:id])
        if @public_body.update_attributes(params[:public_body])
            flash[:notice] = 'PublicBody was successfully updated.'
            redirect_to admin_url('body/show/' + @public_body.id.to_s)
        else
            render :action => 'edit'
        end
    end

    def destroy
        public_body = PublicBody.find(params[:id])

        if public_body.info_requests.size > 0
            flash[:notice] = "There are requests associated with the authority, so can't destroy it"
            redirect_to admin_url('body/show/' + public_body.id.to_s)
            return
        end

        public_body.tag_string = ""
        public_body.destroy
        flash[:notice] = "PublicBody was successfully destroyed."
        redirect_to admin_url('body/list')
    end

    def import_csv
        if params[:csv_file]
            if not params[:tag].empty?
                # Try with dry run first
                csv_contents = params[:csv_file].read
                en = PublicBody.import_csv(csv_contents, params[:tag], true, admin_http_auth_user())
                errors = en[0]
                notes = en[1]

                if errors.size == 0
                    # And if OK, with real run
                    en = PublicBody.import_csv(csv_contents, params[:tag], false, admin_http_auth_user())
                    errors = en[0]
                    notes = en[1]
                    if errors.size != 0
                        raise "dry run mismatched real run"
                    end
                    notes.push("Import was successful.")
                end
                @errors = errors.join("\n")
                @notes = notes.join("\n")
            else
                @errors = "Please enter a tag, use a singular e.g. sea_fishery_committee"
                @notes = ""
            end
        else
            @errors = ""
            @notes = ""
        end
        
    end

    private

end