From 018b3c4b6707aca403323122eceea8c6319b467a Mon Sep 17 00:00:00 2001 From: Nick Clemens Date: Tue, 3 Mar 2020 13:14:03 +0000 Subject: [PATCH] Bug 17268: Use API to store/retrieve values To test: 1 - Enable AdvancedCatalogingEditor 2 - Add some macros (they don't need to be valid, just have content) 3 - Apply patches 4 - updatedatabase 5 - Set user to have 'create_shared_macros' and 'delete_shared_macros' 6 - Load the advanced editor (Cataloging->Advanced editor) 7 - Click on 'Macros' 8 - Previous macros should not show, but you should have a 'Convert' button 9 - Convert old macros and confirm they show 10 - Edit the macros, changing content and the public checkbox, confirm 'Saved' shows in the top right of editor when updating 11 - Have at least on valid macro and run it, e.g.: new 100=Testing this out 12 - Run the macro, confirm it runs 13 - Try a macro with gibberish, confirm there is an error when running 14 - Ensure you have a few macros marked public 15 - In a private browser window sign in as a patron with neither shared macro permission 16 - Confirm the public macros load, but cannot be edited 17 - Grant create_shared_macros permission to this patron 18 - Reload editor, they should now be able to edit shared macros 19 - Confirm they cannot delete shared macros 20 - Grant delete_shared_macros permission 21 - Reload editor 22 - Confirm they can now delete shared macros Signed-off-by: Andrew Fuerste-Henry Signed-off-by: Heather Hernandez Signed-off-by: Katrin Fischer --- Koha/REST/V1/AdvancedEditorMacro.pm | 1 - .../prog/en/includes/cateditor-ui.inc | 227 +++++++++++++++++---- .../prog/en/modules/cataloguing/editor.tt | 6 + 3 files changed, 189 insertions(+), 45 deletions(-) diff --git a/Koha/REST/V1/AdvancedEditorMacro.pm b/Koha/REST/V1/AdvancedEditorMacro.pm index 92f6730c2a..6e0f04d129 100644 --- a/Koha/REST/V1/AdvancedEditorMacro.pm +++ b/Koha/REST/V1/AdvancedEditorMacro.pm @@ -150,7 +150,6 @@ sub add_shared { return $c->render( status => 403, openapi => { error => "To create private macros you must use advancededitor" } ); } - return try { my $macro = Koha::AdvancedEditorMacro->new( _to_model( $c->validation->param('body') ) ); $macro->store; diff --git a/koha-tmpl/intranet-tmpl/prog/en/includes/cateditor-ui.inc b/koha-tmpl/intranet-tmpl/prog/en/includes/cateditor-ui.inc index 8e231e3c6e..9b0c857988 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/includes/cateditor-ui.inc +++ b/koha-tmpl/intranet-tmpl/prog/en/includes/cateditor-ui.inc @@ -694,80 +694,221 @@ require( [ 'koha-backend', 'search', 'macros', 'marc-editor', 'marc-record', 'pr } //> Macro functions - function loadMacro( name ) { + var canCreatePublic = "[% CAN_user_editcatalogue_create_shared_macros | html %]"; + var canDeletePublic = "[% CAN_user_editcatalogue_delete_shared_macros | html %]"; + + function deleteMacro( id ){ + $( '#macro-list' ).empty(); + var shared = macroEditor.activeMacroShared; + var id = macroEditor.activeMacroId; + macroEditor.activeMacroId = null; + api_url = "/api/v1/advancededitormacros/"; + if( shared ) { api_url += "shared/" } + let options = { + url: api_url + id, + method: "DELETE", + contentType: "application/json", + }; + $.ajax(options) + .then(function(result) { + humanMsg.displayAlert( _("Macro successfully deleted") ); + showSavedMacros(); + }) + .fail(function(err) { + humanMsg.displayAlert( _("Failed to delete macro:") + err.responseText, { className: 'humanError' } ); + }); + } + + + function loadMacro( name, id, shared ) { $( '#macro-list li' ).removeClass( 'active' ); + $(".macro_shared").prop("checked",false).hide(); + $("#delete-macro").prop("disabled",true); + macroEditor.setOption( 'readOnly', false ); macroEditor.activeMacro = name; + macroEditor.activeMacroId = id; if ( !name ) { macroEditor.setValue( '' ); return; } + $( '#macro-list li[data-name="' + name + '"][data-id="' + id + '"]' ).addClass( 'active' ); + api_url = "/api/v1/advancededitormacros/"; + if( shared ) { api_url += "shared/" } + let options = { + url: api_url + id, + method: "GET", + contentType: "application/json", + }; + $.ajax(options) + .then(function(result) { + macroEditor.setValue( result.macro_text ); + $(".macro_shared").show(); + if( result.shared ){ + $(".macro_shared").prop("checked",true); + if( canCreatePublic ){ + macroEditor.setOption( 'readOnly', false ); + } else { + macroEditor.setOption( 'readOnly', true ); + } + if( canDeletePublic ){ + $("#delete-macro").prop("disabled",false); + } + } else { + macroEditor.setOption( 'readOnly', false ); + $("#delete-macro").prop("disabled",false); + } + macroEditor.activeMacroShared = result.shared; + }) + .fail(function(err) { + humanMsg.displayAlert( _("Failed to load macros:") + err.responseText, { className: 'humanError' } ); + }); - $( '#macro-list li[data-name="' + name + '"]' ).addClass( 'active' ); - var macro = Preferences.user.macros[name]; - macroEditor.setValue( macro.contents ); - macroEditor.setOption( 'readOnly', false ); - if ( macro.history ) macroEditor.setHistory( macro.history ); - } - - function storeMacro( name, macro ) { - if ( macro ) { - Preferences.user.macros[name] = macro; - } else { - delete Preferences.user.macros[name]; - } - - Preferences.Save( [% logged_in_user.borrowernumber | html %] ); } - function showSavedMacros( macros ) { - var scrollTop = $('#macro-list').scrollTop(); - $( '#macro-list' ).empty(); + function convertOldMacros(){ + $("#convert-macros").remove(); var macro_list = $.map( Preferences.user.macros, function( macro, name ) { return $.extend( { name: name }, macro ); } ); macro_list.sort( function( a, b ) { return a.name.localeCompare(b.name); } ); - $.each( macro_list, function( undef, macro ) { - var $li = $( '
  • ' + macro.name + '
    1. ' ); - $li.click( function() { - loadMacro(macro.name); - return false; - } ); - if ( macro.name == macroEditor.activeMacro ) $li.addClass( 'active' ); - var modified = macro.modified && new Date(macro.modified); - $li.find( '.macro-info' ).append( - '
    2. ' + _("Last changed:") + '' + - ( modified ? ( modified.toLocaleDateString() + ', ' + modified.toLocaleTimeString() ) : _("never") ) + '
    3. ' - ); - $('#macro-list').append($li); + $.each( macro_list, function( index, macro ) { + let options = { + url: "/api/v1/advancededitormacros/", + method: "POST", + contentType: "application/json", + data: JSON.stringify({ + name: macro.name, + patron_id: [% logged_in_user.borrowernumber | html %], + macro_text: macro.contents, + shared: false + }) + }; + $.ajax(options) + .then(function(undef, result) { + delete Preferences.user.macros[macro.name]; + Preferences.Save( [% logged_in_user.borrowernumber | html %] ); + if( index == macro_list.length -1 ){ + showSavedMacros(); + } + + }) + .fail(function(err) { + humanMsg.displayAlert( _("Failed to create macro:") + err.responseText, { className: 'humanError' } ); + }); } ); + } + + function showSavedMacros( macros ) { + var scrollTop = $('#macro-list').scrollTop(); + $( '#macro-list' ).empty(); + $("#convert-macros").remove(); + if( Object.keys(Preferences.user.macros).length ){ + $convert = $( '' ); + $convert.click( function(){ + if( !confirm( _("This will retrieve macros stored in the brower, save them in the database, and delete them from the browser. Proceed?") ) ){ + return; + } + convertOldMacros(); + }); + $("#macro-toolbar").prepend($convert); + } + let options = { + url: "/api/v1/advancededitormacros/", + method: "GET", + contentType: "application/json", + }; + $.ajax(options) + .then(function(result) { + $.each(result,function( undef, macro ){ + var $li = $( '
    4. '+ macro.macro_id + ' - ' + macro.name + '
      1. ' ); + if ( macro.macro_id == macroEditor.activeMacroId ) $li.addClass( 'active' ); + $li.click( function() { + loadMacro(macro.name, macro.macro_id, macro.shared); + return false; + } ); + $('#macro-list').append($li); + }); + }) + .fail(function(err) { + humanMsg.displayAlert( _("Failed to load macros:") + err.responseText, { className: 'humanError' } ); + }); var $new_li = $( '
      2. ' + _("New macro...") + '
      3. ' ); $new_li.click( function() { // TODO: make this a bit less retro var name = prompt(_("Please enter the name for the new macro:")); if (!name) return; - if ( !Preferences.user.macros[name] ) storeMacro( name, { format: "rancor", contents: "" } ); - showSavedMacros(); - loadMacro( name ); +// if ( !Preferences.user.macros[name] ) storeMacro( name, { format: "rancor", contents: "" } ); + let options = { + url: "/api/v1/advancededitormacros/", + method: "POST", + contentType: "application/json", + data: JSON.stringify({ + name: name, + patron_id: [% logged_in_user.borrowernumber | html %], + macro_text: "", + shared: false + }) + }; + $.ajax(options) + .then(function(undef, result) { + showSavedMacros(); + loadMacro( result.name, result.macro_id ); + }) + .fail(function(err) { + humanMsg.displayAlert( _("Failed to create macro:") + err.responseText, { className: 'humanError' } ); + }); } ); $('#macro-list').append($new_li); $('#macro-list').scrollTop(scrollTop); } - function saveMacro() { + function saveMacro(shared) { var name = macroEditor.activeMacro; + var macro_id = macroEditor.activeMacroId; + var was_shared = macroEditor.activeMacroShared; - if ( !name || macroEditor.savedGeneration == macroEditor.changeGeneration() ) return; + if ( !name || macroEditor.savedGeneration == macroEditor.changeGeneration() && was_shared == shared ) return; macroEditor.savedGeneration = macroEditor.changeGeneration(); - storeMacro( name, { contents: macroEditor.getValue(), modified: (new Date()).valueOf(), history: macroEditor.getHistory() } ); - $('#macro-save-message').text(_("Saved")); - showSavedMacros(); + api_url = "/api/v1/advancededitormacros/"; + if( shared || was_shared ) { api_url += "shared/" } + + let options = { + url: api_url + macro_id, + method: "PUT", + contentType: "application/json", + data: JSON.stringify({ + name: name, + patron_id: [% logged_in_user.borrowernumber | html %], + macro_text: macroEditor.getValue(), + shared: shared + }) + }; + $.ajax(options) + .then(function(result) { + $('#macro-save-message').text(_("Saved")); + macroEditor.activeMacroShared = shared; + showSavedMacros(); + }) + .fail(function(err) { + humanMsg.displayAlert( _("Failed to save macro:") + err.responseText, { className: 'humanError' } ); + }); } + $(".macro_shared").change(function(){ + if(this.checked){ + saveMacro(true); + } else { + saveMacro(false); + } + }); + + // END Macro functions + $(document).ready( function() { // Editor setup editor = new MARCEditor( { @@ -844,7 +985,7 @@ require( [ 'koha-backend', 'search', 'macros', 'marc-editor', 'marc-record', 'pr if ( saveTimeout ) clearTimeout( saveTimeout ); saveTimeout = setTimeout( function() { - saveMacro(); + saveMacro(macroEditor.activeMacroShared); saveTimeout = null; }, 500 ); @@ -995,9 +1136,7 @@ require( [ 'koha-backend', 'search', 'macros', 'marc-editor', 'marc-record', 'pr $('#delete-macro').click( function() { if ( !macroEditor.activeMacro || !confirm( _("Are you sure you want to delete this macro?") ) ) return; - - storeMacro( macroEditor.activeMacro, undefined ); - showSavedMacros(); + deleteMacro(); loadMacro( undefined ); return false; diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/cataloguing/editor.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/cataloguing/editor.tt index a4a2b4d71e..d28822651f 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/cataloguing/editor.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/cataloguing/editor.tt @@ -264,6 +264,12 @@
        + + [% IF CAN_user_editcatalogue_create_shared_macros %] + + [% ELSE %] + + [% END %]
        -- 2.11.0