Bug 36084: svc - config/systempreferences

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
This commit is contained in:
Jonathan Druart 2024-02-16 14:56:09 +01:00
parent 52d0005ae0
commit d322494ef6
No known key found for this signature in database
GPG Key ID: A085E712BEF0E0F0
5 changed files with 76 additions and 50 deletions

View File

@ -117,16 +117,15 @@
function yesimeant() {
var OPACdidyoumean = serialize_plugins('opac');
const csrf_token = "[% Koha.GenerateCSRF | $raw %]";
let data = "pref_OPACdidyoumean=%s&csrf_token=%s".format(encodeURIComponent(OPACdidyoumean), csrf_token);
$.ajax({
data: data,
type: 'POST',
url: '/cgi-bin/koha/svc/config/systempreferences/',
success: function () { alert(_("Successfully saved configuration")); },
});
const client = APIClient.syspref;
client.sysprefs.update('OPACdidyoumean', OPACdidyoumean).then(
success => {
alert(_("Successfully saved configuration"));
},
error => {
console.warn("Something wrong happened: %s".format(error));
}
);
return false;
}

View File

@ -1,7 +1,9 @@
import ArticleRequestAPIClient from "./article-request-api-client.js";
import AVAPIClient from "./authorised-value-api-client.js";
import SysprefAPIClient from "./system-preferences-api-client.js";
export const APIClient = {
article_request: new ArticleRequestAPIClient(),
authorised_value: new AVAPIClient(),
syspref: new SysprefAPIClient(),
};

View File

@ -0,0 +1,50 @@
import HttpClient from "./http-client.js";
export class SysprefAPIClient extends HttpClient {
constructor() {
super({
baseURL: "/cgi-bin/koha/svc/config/systempreferences",
});
}
get sysprefs() {
return {
get: variable =>
this.get({
endpoint: "/?pref=" + variable,
}),
update: (variable, value) =>
this.post({
endpoint: "",
body: "pref_%s=%s".format(
encodeURIComponent(variable),
encodeURIComponent(value)
),
headers: {
"Content-Type":
"application/x-www-form-urlencoded;charset=utf-8",
},
}),
update_all: sysprefs =>
this.post({
endpoint: "",
body: Object.keys(sysprefs)
.map(variable =>
sysprefs[variable].length
? sysprefs[variable].map(value =>
"%s=%s".format(variable, value)
)
: "%s=".format(variable)
)
.flat(Infinity)
.join("&"),
headers: {
"Content-Type":
"application/x-www-form-urlencoded;charset=utf-8",
},
}),
};
}
}
export default SysprefAPIClient;

View File

@ -8,41 +8,28 @@ KOHA.Preferences = {
return;
}
modified_prefs = $( form ).find( '.modified' );
// $.serialize removes empty value, we need to keep them.
// If a multiple select has all its entries unselected
var unserialized = new Array();
$(modified_prefs).each(function(){
if ( $(this).attr('multiple') && $(this).val().length == 0 ) {
unserialized.push($(this));
}
});
data = modified_prefs.serialize();
$(unserialized).each(function(){
data += '&' + $(this).attr('name') + '=';
});
if ( !data ) {
let sysprefs = $(form).find('.modified').toArray().reduce((map, e) => ({ ...map, [$(e).attr('name')]: [$(e).val()].flat()}), {});
if ( !Object.keys(sysprefs).length ) {
humanMsg.displayAlert( __("Nothing to save") );
return;
}
let csrf_token_el = $( form ).find('input[name="csrf_token"]');
if (csrf_token_el.length > 0){
let csrf_token = csrf_token_el.val();
if (csrf_token){
data += '&' + 'csrf_token=' + csrf_token;
}
}
KOHA.AJAX.MarkRunning($(form).find('.save-all'), __("Saving...") );
KOHA.AJAX.Submit( {
data: data,
url: '/cgi-bin/koha/svc/config/systempreferences/',
success: function ( data ) { KOHA.Preferences.Success( form ) },
complete: function () { KOHA.AJAX.MarkDone( $( form ).find( '.save-all' ) ) }
} );
const client = APIClient.syspref;
client.sysprefs.update_all(sysprefs).then(
success => {
KOHA.Preferences.Success( form );
},
error => {
console.warn("Something wrong happened: %s".format(error));
}
).then(() => {
KOHA.AJAX.MarkDone( $( form ).find( '.save-all' ) );
});
},
Success: function ( form ) {
var msg = "";
modified_prefs.each(function(){
$(form).find('.modified').each(function(){
var modified_pref = $(this).attr("id");
modified_pref = modified_pref.replace("pref_","");
msg += "<strong>" + __("Saved preference %s").format(modified_pref) + "</strong>\n";

View File

@ -64,12 +64,6 @@ Used to set a single system preference.
sub set_preference {
my ( $preference ) = @_;
die "wrong_csrf_token\n" unless Koha::Token->new->check_csrf(
{
session_id => scalar $query->cookie('CGISESSID'),
token => scalar $query->param('csrf_token'),
}
);
my $value = join( ',', $query->param( 'value' ) );
C4::Context->set_preference( $preference, $value );
@ -129,12 +123,6 @@ pref_virtualshelves=0
=cut
sub set_preferences {
die "wrong_csrf_token\n" unless Koha::Token->new->check_csrf(
{
session_id => scalar $query->cookie('CGISESSID'),
token => scalar $query->param('csrf_token'),
}
);
foreach my $param ( $query->param() ) {
my ( $pref ) = ( $param =~ /pref_(.*)/ );