Bug 8836 - Resurrect Rotating Collections
[koha.git] / rotating_collections / editCollections.pl
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 2 of the License, or (at your option) any later
8 # version.
9 #
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License along with
15 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
16 # Suite 330, Boston, MA  02111-1307 USA
17 #
18
19 use Modern::Perl;
20
21 use CGI;
22
23 use C4::Output;
24 use C4::Auth;
25 use C4::Context;
26
27 use C4::RotatingCollections;
28
29 my $query = new CGI;
30
31 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
32     {
33         template_name   => "rotating_collections/editCollections.tmpl",
34         query           => $query,
35         type            => "intranet",
36         authnotrequired => 0,
37         flagsrequired   => { tools => 'rotating_collections' },
38         debug           => 1,
39     }
40 );
41
42 # Create new Collection
43 if ( $query->param('action') eq 'create' ) {
44     my $title       = $query->param('title');
45     my $description = $query->param('description');
46
47     my ( $createdSuccessfully, $errorCode, $errorMessage ) =
48       CreateCollection( $title, $description );
49
50     $template->param(
51         previousActionCreate => 1,
52         createdTitle         => $title,
53     );
54
55     if ($createdSuccessfully) {
56         $template->param( createSuccess => 1 );
57     }
58     else {
59         $template->param( createFailure  => 1 );
60         $template->param( failureMessage => $errorMessage );
61     }
62 }
63
64 ## Delete a club or service
65 elsif ( $query->param('action') eq 'delete' ) {
66     my $colId = $query->param('colId');
67     my ( $success, $errorCode, $errorMessage ) = DeleteCollection($colId);
68
69     $template->param( previousActionDelete => 1 );
70     if ($success) {
71         $template->param( deleteSuccess => 1 );
72     }
73     else {
74         $template->param( deleteFailure  => 1 );
75         $template->param( failureMessage => $errorMessage );
76     }
77 }
78
79 ## Edit a club or service: grab data, put in form.
80 elsif ( $query->param('action') eq 'edit' ) {
81     my $colId = $query->param('colId');
82     my ( $colId, $colTitle, $colDesc, $colBranchcode ) = GetCollection($colId);
83
84     $template->param(
85         previousActionEdit => 1,
86         editColId          => $colId,
87         editColTitle       => $colTitle,
88         editColDescription => $colDesc,
89     );
90 }
91
92 # Update a Club or Service
93 elsif ( $query->param('action') eq 'update' ) {
94     my $colId       = $query->param('colId');
95     my $title       = $query->param('title');
96     my $description = $query->param('description');
97
98     my ( $createdSuccessfully, $errorCode, $errorMessage ) =
99       UpdateCollection( $colId, $title, $description );
100
101     $template->param(
102         previousActionUpdate => 1,
103         updatedTitle         => $title,
104     );
105
106     if ($createdSuccessfully) {
107         $template->param( updateSuccess => 1 );
108     }
109     else {
110         $template->param( updateFailure  => 1 );
111         $template->param( failureMessage => $errorMessage );
112     }
113 }
114
115 my $collections = GetCollections();
116
117 $template->param(
118     intranetcolorstylesheet =>
119       C4::Context->preference("intranetcolorstylesheet"),
120     intranetstylesheet => C4::Context->preference("intranetstylesheet"),
121     IntranetNav        => C4::Context->preference("IntranetNav"),
122
123     collectionsLoop => $collections,
124 );
125
126 output_html_with_http_headers $query, $cookie, $template->output;