Koha/tools/additional-contents.pl
Owen Leonard f5fcd2a55e Bug 29019: Unable to delete HTML customization
This patch makes some corrections to additional contents to allow
content to be deleted. The wrong parameter was being passed to the
script. The script was also not handling multiple deletions correctly.

The patch also adds a "category" parameter to the delete operation so
that the page is redirected correctly.

The patch also changes some strings which referred to "news"
referencing operations which might be performed on news or on HTML
customizations, e.g.:

"Are you sure you want to delete the selected content?" instead of "Are
you sure you want to delete the selected news?"

To test, apply the patch and go to Tools -> News.

- Create multiple news items if necessary.
- Test the "Delete" button corresponding to a single news item:
  - Clicking the button should ask you to confirm.
  - Check that the wording of the message is correct.
  - After confirming the news item should be deleted.
  - Ideally, test on news items which are on the second page of the
    DataTable of news items.
- Test the process of deleting multiple news items at once:
  - Check the checkbox next to multiple items.
  - Click the "Delete selected" button at the bottom of the page.
  - Check that the wording of the confirmation message is correct.
  - After you confirm, the items should be deleted.
- Repeat these tests under Tools -> HTML customizations to confirm that
  redirects work correctly. After deleting an HTML customization you
  should be redirected back to the list of HTML customizations.

Signed-off-by: Caroline Cyr La Rose <caroline.cyr-la-rose@inlibro.com>

Signed-off-by: Katrin Fischer <katrin.fischer.83@web.de>

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
2021-09-28 15:12:45 +02:00

229 lines
7.5 KiB
Perl
Executable file
Raw Blame History

#!/usr/bin/perl
# This file is part of Koha.
#
# Script to manage the opac news.
# written 11/04
# Casta<74>eda, Carlos Sebastian - seba3c@yahoo.com.ar - Physics Library UNLP Argentina
# Modified to include news to KOHA intranet - tgarip@neu.edu.tr NEU library -Cyprus
# Copyright 2000-2002 Katipo Communications
# Copyright (C) 2013 Mark Tompsett
#
# Koha is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# Koha is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Koha; if not, see <http://www.gnu.org/licenses>.
use Modern::Perl;
use CGI qw ( -utf8 );
use C4::Auth qw(get_template_and_user);
use C4::Koha;
use C4::Context;
use C4::Output qw(output_html_with_http_headers);
use C4::Languages qw(getTranslatedLanguages);
use Koha::DateUtils;
use Koha::AdditionalContents;
my $cgi = CGI->new;
my $op = $cgi->param('op') || 'list';
my $id = $cgi->param('id');
my $category = $cgi->param('category') || 'news';
my $wysiwyg;
if( $cgi->param('editmode') ){
$wysiwyg = $cgi->param('editmode') eq "wysiwyg" ? 1 : 0;
} else {
$wysiwyg = C4::Context->preference("AdditionalContentsEditor") eq "tinymce" ? 1 : 0;
}
my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
{
template_name => "tools/additional-contents.tt",
query => $cgi,
type => "intranet",
flagsrequired => { tools => 'edit_additional_contents' },
}
);
my @messages;
if ( $op eq 'add_form' ) {
my $additional_content = Koha::AdditionalContents->find($id);
my $translated_contents;
if ( $additional_content ) {
$translated_contents = {
map { $_->lang => $_ } Koha::AdditionalContents->search(
{
category => $additional_content->category,
code => $additional_content->code,
location => $additional_content->location,
branchcode => $additional_content->branchcode,
}
)
};
$category = $additional_content->category;
}
$template->param(
additional_content => $additional_content,
translated_contents => $translated_contents,
);
}
elsif ( $op eq 'add_validate' ) {
my $location = $cgi->param('location');
my $code = $cgi->param('code');
my $branchcode = $cgi->param('branchcode') || undef;
my @title = $cgi->multi_param('title');
my @content = $cgi->multi_param('content');
my @lang = $cgi->multi_param('lang');
my $expirationdate;
if ( $cgi->param('expirationdate') ) {
$expirationdate = output_pref(
{
dt => dt_from_string( scalar $cgi->param('expirationdate') ),
dateformat => 'iso',
dateonly => 1
}
);
}
my $published_on = output_pref(
{
dt => dt_from_string( scalar $cgi->param('published_on') ),
dateformat => 'iso',
dateonly => 1
}
);
my $number = $cgi->param('number');
my $success = 1;
for my $lang ( @lang ) {
my $title = shift @title;
my $content = shift @content;
my $additional_content = Koha::AdditionalContents->find(
{
category => $category,
code => $code,
branchcode => $branchcode,
lang => $lang,
}
);
# Delete if title or content is empty
unless ( $title and $content ) {
$additional_content->delete if $additional_content;
next;
} elsif ( $additional_content ) {
eval {
$additional_content->update(
{
category => $category,
code => $code,
location => $location,
branchcode => $branchcode,
title => $title,
content => $content,
lang => $lang,
expirationdate => $expirationdate,
published_on => $published_on,
number => $number,
borrowernumber => $borrowernumber,
}
);
};
if ($@) {
$success = 0;
push @messages, { type => 'error', code => 'error_on_update' };
last;
}
}
else {
my $additional_content = Koha::AdditionalContent->new(
{
category => $category,
code => $code,
location => $location,
branchcode => $branchcode,
title => $title,
content => $content,
lang => $lang,
expirationdate => $expirationdate,
published_on => $published_on,
number => $number,
borrowernumber => $borrowernumber,
}
)->store;
eval { $additional_content->store; };
if ($@) {
$success = 0;
push @messages, { type => 'error', code => 'error_on_insert' };
last;
}
}
}
$op = 'list';
}
elsif ( $op eq 'delete_confirmed' ) {
my @ids = $cgi->multi_param('ids');
my $deleted =
map { Koha::AdditionalContents->find($_)->delete() } @ids;
if ( $@ or not $deleted ) {
push @messages, { type => 'error', code => 'error_on_delete' };
}
else {
push @messages, { type => 'message', code => 'success_on_delete' };
}
$op = 'list';
}
if ( $op eq 'list' ) {
my $additional_contents = Koha::AdditionalContents->search(
{ category => $category, lang => 'default' },
{ order_by => { -desc => 'published_on' } }
);
$template->param( additional_contents => $additional_contents );
}
my $translated_languages = C4::Languages::getTranslatedLanguages;
my @languages;
for my $language (@$translated_languages) {
for my $sublanguage ( @{ $language->{sublanguages_loop} } ) {
if ( $language->{plural} ) {
push @languages,
{
lang => $sublanguage->{rfc4646_subtag},
description => $sublanguage->{native_description} . ' '
. $sublanguage->{region_description} . ' ('
. $sublanguage->{rfc4646_subtag} . ')',
};
}
else {
push @languages,
{
lang => $sublanguage->{rfc4646_subtag},
description => $sublanguage->{native_description} . ' ('
. $sublanguage->{rfc4646_subtag} . ')',
};
}
}
}
unshift @languages, {lang => 'default'} if @languages;
$template->param(
op => $op,
category => $category,
wysiwyg => $wysiwyg,
languages => \@languages,
);
output_html_with_http_headers $cgi, $cookie, $template->output;