From 6b8c0042451f9598cd44e5d59c96edf7460eb618 Mon Sep 17 00:00:00 2001 From: Lucas Gass Date: Tue, 29 Jul 2025 20:29:20 +0000 Subject: [PATCH] Bug 37054: Store and retrieve values in JSON --- Koha/Template/Plugin/Branches.pm | 23 +++++++++ .../prog/en/includes/doc-head-open.inc | 13 ++--- .../prog/en/modules/admin/branches.tt | 50 ++++++++++++++++--- 3 files changed, 73 insertions(+), 13 deletions(-) diff --git a/Koha/Template/Plugin/Branches.pm b/Koha/Template/Plugin/Branches.pm index 3329a9c859b..e91cb046e02 100644 --- a/Koha/Template/Plugin/Branches.pm +++ b/Koha/Template/Plugin/Branches.pm @@ -27,6 +27,7 @@ use C4::Koha; use C4::Context; use Koha::Cache::Memory::Lite; use Koha::Libraries; +use JSON qw( decode_json ); sub GetName { my ( $self, $branchcode ) = @_; @@ -194,4 +195,26 @@ sub GetBranchSpecificCSS { return $opacusercss; } +sub GetBranchCustomization { + my ( $self, $branchcode, $key ) = @_; + + return q{} unless defined $branchcode; + + my $library = Koha::Libraries->find($branchcode); + my $customizations_json = $library ? $library->ui_staff_customization : q{}; + my $customizations = decode_json($customizations_json); + + return q{} unless defined $key; + + my @keys = split /\./, $key; + my $value = $customizations; + + for my $k (@keys) { + return q{} unless ref $value eq 'HASH' && exists $value->{$k}; + $value = $value->{$k}; + } + + return defined $value ? $value : q{}; +} + 1; diff --git a/koha-tmpl/intranet-tmpl/prog/en/includes/doc-head-open.inc b/koha-tmpl/intranet-tmpl/prog/en/includes/doc-head-open.inc index 18eb86ca410..1082b191e7a 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/includes/doc-head-open.inc +++ b/koha-tmpl/intranet-tmpl/prog/en/includes/doc-head-open.inc @@ -34,11 +34,12 @@ [% IF ( bidi ) %][% ELSE %][% END %] -[% SET ui_staff_customization = Branches.GetLoggedInBranch.ui_staff_customization %] -[% IF ui_staff_customization %] - -[% END %] + [% END %] + diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/branches.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/branches.tt index 295f4ed77e7..af06a754a5f 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/branches.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/branches.tt @@ -2,6 +2,7 @@ [% USE raw %] [% USE Asset %] [% USE TablesSettings %] +[% USE Branches %] [%- USE KohaSpan -%] [% USE KohaTimes %] [% PROCESS 'i18n.inc' %] @@ -9,6 +10,8 @@ [% INCLUDE 'doc-head-open.inc' %] [% IF library %][% SET OpacLibraryInfo = library.opac_info( lang => lang ) %][% END %] +[% SET ui_staff_color_background = Branches.GetBranchCustomization( Branches.GetLoggedInBranchcode, 'colors.background') %] + [% FILTER collapse %] [% IF op == 'add_form' %] @@ -512,11 +515,12 @@ <label for="colorpicker">Primary branch color: </label> </div> <div class="fg-input"> - <input type="color" name="colorpicker" id="colorpicker" value="[% library.ui_staff_customization | html %]" /> - <input type="text" name="ui_staff_customization" id="ui_staff_customization" readonly="readonly" size="10" maxlength="16" value="[% library.ui_staff_customization | html %]" /> + <input type="color" id="colorpicker" value="[% ui_staff_color_background | html %]" /> + <input type="text" id="ui_staff_customization_background" readonly="readonly" size="10" maxlength="16" value="[% ui_staff_color_background | html %]" /> <a href="#" class="clear_color"><i class="fa fa-fw fa-trash-can"></i> Clear</a> </div> </div> + <input type="hidden" name="ui_staff_customization" id="ui_staff_customization" value="[% library.ui_staff_customization | html %]" /> </fieldset> [% IF additional_fields.size %] [% INCLUDE 'additional-fields-entry.inc' available=additional_fields values=additional_field_values wrap_fieldset=1 %] @@ -817,6 +821,26 @@ } </style> <script> + function updateCustomizations() { + let customizations = { colors: {} }; + try { + var currentJson = $hiddenField.val(); + if (currentJson) { + customizations = JSON.parse(currentJson); + } + } catch(e) { + customizations = { colors: {} }; + } + if (!customizations.colors) customizations.colors = {}; + + // Get value from colorpicker, fallback to input + let background = $('#colorpicker').val() || $('#ui_staff_customization_background').val(); + if ( background ) { + customizations.colors.background = background; + } + $('#ui_staff_customization').val(JSON.stringify(customizations)); + } + var table_settings = [% TablesSettings.GetTableSettings( 'admin', 'libraries', 'libraries', 'json' ) | $raw %]; var calendarFirstDayOfWeek = '[% Koha.Preference('CalendarFirstDayOfWeek') | html %]'; @@ -1032,14 +1056,26 @@ } $( "#" + target ).hide(); }); - $("#colorpicker").on("change", function(){ - let ui_staff_customization = this.value; - $("#ui_staff_customization").val( ui_staff_customization ); + + $('#colorpicker').on('change input', function() { + var color = $(this).val(); + $('#ui_staff_customization_background').val(color); + updateCustomizations(); + }); + + $('#ui_staff_customization_background').on('change blur', function() { + var color = $(this).val(); + if (color.match(/^#[0-9A-Fa-f]{6}$/)) { + $('#colorpicker').val(color); + } + updateCustomizations(); }); - $(".clear_color").on("click", function(e){ + $('.clear_color').on('click', function(e) { e.preventDefault(); - $("#ui_staff_customization, #colorpicker").val( "" ); + $('#colorpicker').val(""); + $('#ui_staff_customization_background').val(''); + updateCustomizations(); }); </script> [% END %] -- 2.39.5