Bug 17498 - Expose system preference values to JavaScript in the OPAC
Summary: Expose system preference values to JavaScript in the OPAC
Status: NEW
Alias: None
Product: Koha
Classification: Unclassified
Component: OPAC (show other bugs)
Version: Main
Hardware: All All
: P5 - low enhancement (vote)
Assignee: Owen Leonard
QA Contact: Testopia
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2016-10-25 17:30 UTC by Owen Leonard
Modified: 2019-11-13 12:20 UTC (History)
4 users (show)

See Also:
Change sponsored?: ---
Patch complexity: ---
Documentation contact:
Documentation submission:
Text to go in the release notes:
Version(s) released in:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Owen Leonard 2016-10-25 17:30:50 UTC
We process a lot of JavaScript in the OPAC templates, usually because there are template variable which need to be processed. Some of these variable, like system preferences, are a global setting which is common to many pages in the OPAC. I propose that these settings be defined in embedded JavaScript so that the scripts themselves can be moved to a separate file.

In my proposal, opac-bottom.inc will contain a section like this:

<script type="text/javascript">
    if (typeof KOHA == "undefined" || !KOHA) {
        var KOHA = {};
    }
    KOHA.paths = {
        "interface_theme":"[% interface %]/[% theme %]"
    }
    KOHA.preferences = {
        "BakerTaylorEnabled" : [% IF ( Koha.Preference( 'BakerTaylorEnabled' ) == 1 ) %]1[% ELSE %]0[% END %],

...and then an separate JS file will use those variables to conditionally load resources:

if( KOHA.preferences.Coce && KOHA.preferences.CoceProviders ){
    KOHA.externalResources.push( KOHA.paths.interface_theme + "/js/coce.js" );
    var NO_COCE_JACKET = _("No cover image available");
}

if( KOHA.externalResources.length > 0 ){
    Modernizr.load({
        load: KOHA.externalResources
    });
}

I'd appreciate comments anyone might have on this system. My goal, if this works well, is to then add a JavaScript concatenation/minification task for OPAC js assets. Moving as much JS as possible out of the templates will help us take advantage of that minification.
Comment 1 Owen Leonard 2016-10-25 17:40:58 UTC
I feel I should clarify: This wouldn't expose any private system preference data like API keys. It would be used primarily to expose "on" or "off" settings for various features.
Comment 2 Chris Cormack 2016-10-25 20:38:05 UTC
As long as it's an opt in thing, IE we have to define in opac-bottom.inc which preferences we want exposed, and only do ones that it is safe to do so. Then I think it is a great idea.
Comment 3 Tomás Cohen Arazi 2019-11-12 18:04:33 UTC
I'm not sure we should expose system preferences to the public front end.
Comment 4 Katrin Fischer 2019-11-13 12:20:47 UTC
I agree it should be a 'whitelist' for the OPAC related features if we do this - some of the preferences might contain critical information.