Bug 7567: convert news add/update routines to take hashref; fix bugs
[koha.git] / tools / koha-news.pl
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Script to manage the opac news.
6 # written 11/04
7 # Casta�eda, Carlos Sebastian - seba3c@yahoo.com.ar - Physics Library UNLP Argentina
8 # Modified to include news to KOHA intranet - tgarip@neu.edu.tr NEU library -Cyprus
9 # Copyright 2000-2002 Katipo Communications
10 # Copyright (C) 2013    Mark Tompsett
11 #
12 # Koha is free software; you can redistribute it and/or modify it
13 # under the terms of the GNU General Public License as published by
14 # the Free Software Foundation; either version 3 of the License, or
15 # (at your option) any later version.
16 #
17 # Koha is distributed in the hope that it will be useful, but
18 # WITHOUT ANY WARRANTY; without even the implied warranty of
19 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 # GNU General Public License for more details.
21 #
22 # You should have received a copy of the GNU General Public License
23 # along with Koha; if not, see <http://www.gnu.org/licenses>.
24
25 use strict;
26 # use warnings; FIXME - Bug 2505
27 use CGI;
28 use C4::Auth;
29 use C4::Koha;
30 use C4::Context;
31 use C4::Dates qw(format_date_in_iso);
32 use C4::Output;
33 use C4::NewsChannels;
34 use C4::Languages qw(getTranslatedLanguages);
35 use Date::Calc qw/Date_to_Days Today/;
36
37 my $cgi = new CGI;
38
39 my $id             = $cgi->param('id');
40 my $title          = $cgi->param('title');
41 my $new            = $cgi->param('new');
42 my $expirationdate = format_date_in_iso($cgi->param('expirationdate'));
43 my $timestamp      = format_date_in_iso($cgi->param('timestamp'));
44 my $number         = $cgi->param('number');
45 my $lang           = $cgi->param('lang');
46
47 my $new_detail = get_opac_new($id);
48
49 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
50     {
51         template_name   => "tools/koha-news.tmpl",
52         query           => $cgi,
53         type            => "intranet",
54         authnotrequired => 0,
55         flagsrequired   => { tools => 'edit_news' },
56         debug           => 1,
57     }
58 );
59
60 # get lang list
61 my @lang_list;
62 my $tlangs = getTranslatedLanguages() ;
63 foreach my $language ( @$tlangs ) {
64     push @lang_list,
65       {
66         language => $language->{'rfc4646_subtag'},
67         selected => ( $new_detail->{lang} eq $language->{'rfc4646_subtag'} ? 1 : 0 ),
68       };
69 }
70
71 $template->param( lang_list => \@lang_list );
72
73 my $op = $cgi->param('op');
74
75 if ( $op eq 'add_form' ) {
76     $template->param( add_form => 1 );
77     if ($id) {
78         if($new_detail->{lang} eq "slip"){ $template->param( slip => 1); }
79         $template->param( 
80             op => 'edit',
81             id => $new_detail->{'idnew'}
82         );
83         $template->{VARS}->{'new_detail'} = $new_detail;
84     }
85     else {
86         $template->param( op => 'add' );
87     }
88 }
89 elsif ( $op eq 'add' ) {
90     my $parameters = {
91                       title          => $title,
92                       new            => $new,
93                       lang           => $lang,
94                       expirationdate => $expirationdate,
95                       timestamp      => $timestamp,
96                       number         => $number,
97                   };
98     add_opac_new( $parameters );
99     print $cgi->redirect("/cgi-bin/koha/tools/koha-news.pl");
100 }
101 elsif ( $op eq 'edit' ) {
102     my $parameters = {
103                       idnew          => $id,
104                       title          => $title,
105                       new            => $new,
106                       lang           => $lang,
107                       expirationdate => $expirationdate,
108                       timestamp      => $timestamp,
109                       number         => $number,
110                   };
111     upd_opac_new( $parameters );
112     print $cgi->redirect("/cgi-bin/koha/tools/koha-news.pl");
113 }
114 elsif ( $op eq 'del' ) {
115     my @ids = $cgi->param('ids');
116     del_opac_new( join ",", @ids );
117     print $cgi->redirect("/cgi-bin/koha/tools/koha-news.pl");
118 }
119
120 else {
121
122     my ( $opac_news_count, $opac_news ) = &get_opac_news( undef, $lang );
123     
124     foreach my $new ( @$opac_news ) {
125         next unless $new->{'expirationdate'};
126         #$new->{'expirationdate'}=format_date_in_iso($new->{'expirationdate'});
127         my @date = split (/-/,$new->{'expirationdate'});
128         if ($date[0]*$date[1]*$date[2]>0 && Date_to_Days( @date ) < Date_to_Days(&Today) ){
129                         $new->{'expired'} = 1;
130         }
131     }
132     
133     $template->param(
134         opac_news       => $opac_news,
135         opac_news_count => $opac_news_count,
136                 );
137 }
138 $template->param( lang => $lang );
139 output_html_with_http_headers $cgi, $cookie, $template->output;