Bug 12705 - News not possible for one sublanguage if two sublanguages are installed
[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 use C4::Branch qw/GetBranches/;
37
38 my $cgi = new CGI;
39
40 my $id             = $cgi->param('id');
41 my $title          = $cgi->param('title');
42 my $new            = $cgi->param('new');
43 my $expirationdate = format_date_in_iso($cgi->param('expirationdate'));
44 my $timestamp      = format_date_in_iso($cgi->param('timestamp'));
45 my $number         = $cgi->param('number');
46 my $lang           = $cgi->param('lang');
47 my $branchcode     = $cgi->param('branch');
48 # Foreign Key constraints work with NULL, not ''
49 # NULL = All branches.
50 $branchcode = undef if (defined($branchcode) && $branchcode eq '');
51
52 my $new_detail = get_opac_new($id);
53
54 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
55     {
56         template_name   => "tools/koha-news.tt",
57         query           => $cgi,
58         type            => "intranet",
59         authnotrequired => 0,
60         flagsrequired   => { tools => 'edit_news' },
61         debug           => 1,
62     }
63 );
64
65 # get lang list
66 my @lang_list;
67 my $tlangs = getTranslatedLanguages() ;
68
69 foreach my $language ( @$tlangs ) {
70     foreach my $sublanguage ( @{$language->{'sublanguages_loop'}} ) {
71         push @lang_list,
72         {
73             language => $sublanguage->{'rfc4646_subtag'},
74             selected => ( $new_detail->{lang} eq $sublanguage->{'rfc4646_subtag'} ? 1 : 0 ),
75         };
76     }
77 }
78
79 my $branches = GetBranches;
80
81 $template->param( lang_list   => \@lang_list,
82                   branch_list => $branches,
83                   branchcode  => $branchcode );
84
85 my $op = $cgi->param('op') // '';
86
87 if ( $op eq 'add_form' ) {
88     $template->param( add_form => 1 );
89     if ($id) {
90         if($new_detail->{lang} eq "slip"){ $template->param( slip => 1); }
91         $template->param( 
92             op => 'edit',
93             id => $new_detail->{'idnew'}
94         );
95         $template->{VARS}->{'new_detail'} = $new_detail;
96     }
97     else {
98         $template->param( op => 'add' );
99     }
100 }
101 elsif ( $op eq 'add' ) {
102     add_opac_new(
103         {
104             title          => $title,
105             new            => $new,
106             lang           => $lang,
107             expirationdate => $expirationdate,
108             timestamp      => $timestamp,
109             number         => $number,
110             branchcode     => $branchcode,
111         }
112     );
113     print $cgi->redirect("/cgi-bin/koha/tools/koha-news.pl");
114 }
115 elsif ( $op eq 'edit' ) {
116     upd_opac_new(
117         {
118             idnew          => $id,
119             title          => $title,
120             new            => $new,
121             lang           => $lang,
122             expirationdate => $expirationdate,
123             timestamp      => $timestamp,
124             number         => $number,
125             branchcode     => $branchcode,
126         }
127     );
128     print $cgi->redirect("/cgi-bin/koha/tools/koha-news.pl");
129 }
130 elsif ( $op eq 'del' ) {
131     my @ids = $cgi->param('ids');
132     del_opac_new( join ",", @ids );
133     print $cgi->redirect("/cgi-bin/koha/tools/koha-news.pl");
134 }
135
136 else {
137
138     my ( $opac_news_count, $opac_news ) = &get_opac_news( undef, $lang, $branchcode );
139     
140     foreach my $new ( @$opac_news ) {
141         next unless $new->{'expirationdate'};
142         #$new->{'expirationdate'}=format_date_in_iso($new->{'expirationdate'});
143         my @date = split (/-/,$new->{'expirationdate'});
144         if ($date[0]*$date[1]*$date[2]>0 && Date_to_Days( @date ) < Date_to_Days(&Today) ){
145                         $new->{'expired'} = 1;
146         }
147     }
148     
149     $template->param(
150         opac_news       => $opac_news,
151         opac_news_count => $opac_news_count,
152                 );
153 }
154 $template->param( lang => $lang );
155 output_html_with_http_headers $cgi, $cookie, $template->output;