Bug 25728: Create AV when adding a new item
authorJonathan Druart <jonathan.druart@bugs.koha-community.org>
Mon, 8 Jun 2020 14:00:48 +0000 (16:00 +0200)
committerJonathan Druart <jonathan.druart@bugs.koha-community.org>
Mon, 24 Aug 2020 09:19:03 +0000 (11:19 +0200)
We do a bit of refactoring to make the code reusable.

Test plan:
Same as the first patch but when adding/editing an item

QA note: There is a warning from the QA tools
 FAIL   koha-tmpl/intranet-tmpl/prog/en/modules/cataloguing/additem.tt
   FAIL   js_in_body
                A <script> tag found inside head, must be moved to the body (see bug 17858)
I don't think how we could avoid it.

Sponsored-by: Orex Digital

Signed-off-by: Hugo Agud <hagud@orex.es>

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

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>

cataloguing/additem.pl
koha-tmpl/intranet-tmpl/prog/en/includes/modals/cataloguing_create_av.inc [new file with mode: 0644]
koha-tmpl/intranet-tmpl/prog/en/modules/authorities/authorities.tt
koha-tmpl/intranet-tmpl/prog/en/modules/cataloguing/addbiblio.tt
koha-tmpl/intranet-tmpl/prog/en/modules/cataloguing/additem.tt
koha-tmpl/intranet-tmpl/prog/js/cataloging.js

index a971089..2d0ba7e 100755 (executable)
@@ -247,6 +247,7 @@ sub generate_subfield_form {
                     id          => $subfield_data{id},
                     maxlength   => $subfield_data{maxlength},
                     value       => $value,
+                    ( ( grep { $_ eq $subfieldlib->{authorised_value}} ( qw(branches itemtypes cn_source) ) ) ? () : ( category => $subfieldlib->{authorised_value}) ),
                 };
             }
             else {
@@ -256,6 +257,7 @@ sub generate_subfield_form {
                     values   => \@authorised_values,
                     labels   => \%authorised_lib,
                     default  => $value,
+                    ( ( grep { $_ eq $subfieldlib->{authorised_value}} ( qw(branches itemtypes cn_source) ) ) ? () : ( category => $subfieldlib->{authorised_value}) ),
                 };
             }
         }
diff --git a/koha-tmpl/intranet-tmpl/prog/en/includes/modals/cataloguing_create_av.inc b/koha-tmpl/intranet-tmpl/prog/en/includes/modals/cataloguing_create_av.inc
new file mode 100644 (file)
index 0000000..995a5ae
--- /dev/null
@@ -0,0 +1,41 @@
+<div id="avCreate" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="avCreateLabel" aria-hidden="true">
+    <div class="modal-dialog">
+        <div class="modal-content">
+            <div class="modal-header">
+                <button type="button" class="closebtn" data-dismiss="modal" aria-hidden="true">×</button>
+                <h3 id="avCreateLabel">Create a new authorised value</h3>
+            </div>
+            <form id="add_new_av" method="post">
+                <div class="modal-body">
+                    <div class="error"></div>
+                    <fieldset class="rows">
+                        <ol>
+                            <li>
+                                <span class="label">Category:</span>
+                                <input type="hidden" name="category" value="" />
+                                <span id="new_av_category"></span>
+                            </li>
+                            <li>
+                                <span class="label" for="value">Authorised value:</span>
+                                <input type="text" id="value" name="value" />
+                            </li>
+                            <li>
+                                <span class="label" for="description">Description:</span>
+                                <input type="text" id="description" name="description" />
+                            </li>
+                            <li>
+                                <span class="label" for="opac_description">Description (OPAC):</span>
+                                <input type="text" id="opac_description" name="opac_description" />
+                            </li>
+                        </ol>
+                    </fieldset>
+                </div>
+                <div class="modal-footer">
+                    <button class="btn btn-default" data-dismiss="modal" aria-hidden="true">Close</button>
+                    <input type="hidden" name="select2" value="" />
+                    <input type="submit" class="btn btn-primary" value="Save">
+                </div>
+            </form>
+        </div> <!-- /.modal-content -->
+    </div> <!-- /.modal-dialog -->
+</div> <!-- /#avCreate -->
index 961b4b2..fb33a16 100644 (file)
@@ -5,6 +5,7 @@
 <title>Koha &rsaquo; Authorities &rsaquo; [% IF ( authid ) %]Modify authority #[% authid | html %] ([% authtypetext | html %])[% ELSE %]Adding authority ([% authtypetext | html %])[% END %]</title>
 [% INCLUDE 'doc-head-close.inc' %]
 [% Asset.js("lib/hc-sticky.js") | $raw %]
+[% INCLUDE 'select2.inc' %]
 [% Asset.js("js/cataloging.js") | $raw %]
 
 <script>
@@ -167,8 +168,6 @@ function confirmnotdup(redirect){
 </script>
 [% Asset.css("css/addbiblio.css") | $raw %]
 
-[% INCLUDE 'select2.inc' %]
-
 </head>
 <body id="auth_authorities" class="auth">
 
index 8de0932..4f4fc59 100644 (file)
@@ -5,6 +5,10 @@
 <title>Koha &rsaquo; Cataloging &rsaquo; [% IF ( biblionumber ) %]Editing [% title | html %] (Record number [% biblionumber | html %])[% ELSE %]Add MARC record[% END %]</title>
 [% INCLUDE 'doc-head-close.inc' %]
 [% Asset.js("lib/hc-sticky.js") | $raw %]
+<script>
+    var CAN_user_parameters_manage_auth_values = "[% CAN_user_parameters_manage_auth_values | html %]";
+</script>
+[% INCLUDE 'select2.inc' %]
 [% Asset.js("js/cataloging.js") | $raw %]
 [% INCLUDE 'strings.inc' %]
 [% Asset.js("js/browser.js") | $raw %]
 </script>
 [% Asset.css("css/addbiblio.css") | $raw %]
 
-[% INCLUDE 'select2.inc' %]
-<script>
-  $(document).ready(function() {
-      [% IF CAN_user_parameters_manage_auth_values %]
-          var current_select2;
-          $('.subfield_line select[data-category!=""]').select2({
-              tags: true,
-              createTag: function (tag) {
-                  return {
-                      id: tag.term,
-                      text: tag.term,
-                      newTag: true
-                  };
-              },
-              templateResult: function(state) {
-                  if (state.newTag) {
-                      return state.text + " " + "(select to create)";
-                  }
-                  return state.text;
-              }
-          }).on("select2:select", function(e) {
-              if(e.params.data.newTag){
-
-                  var category = $(this).data("category");
-                  $("#avCreate #new_av_category").html(category);
-                  $("#avCreate input[name='category']").val(category);
-                  $("#avCreate input[name='value']").val(e.params.data.text);
-                  $("#avCreate input[name='description']").val(e.params.data.text);
-                  $('#avCreate').modal({show:true});
-
-                  $(current_select2).val($(current_select2).find("option:first").val()).trigger('change');
-
-                  current_select2 = this;
-
-              }
-          }).on("select2:clear", function () {
-              $(this).on("select2:opening.cancelOpen", function (evt) {
-                  evt.preventDefault();
-
-                  $(this).off("select2:opening.cancelOpen");
-              });
-          });
-
-          $("#add_new_av").on("submit", function(){
-              var data = {
-                  category: $(this).find('input[name="category"]').val(),
-                  value: $(this).find('input[name="value"]').val(),
-                  description: $(this).find('input[name="description"]').val(),
-                  opac_description: $(this).find('input[name="opac_description"]').val(),
-              };
-              $.ajax({
-                  type: "POST",
-                  url: "/api/v1/authorised_values",
-                  data:JSON.stringify(data),
-                  success: function(response) {
-                      $('#avCreate').modal('hide');
-
-                      $(current_select2).append('<option selected value="'+data['value']+'">'+data['description']+'</option>');
-                  },
-                  error: function(err) {
-                      $("#avCreate .error").html(_("Something went wrong, maybe the value already exists?"))
-                  }
-              });
-              return false;
-          });
-      [% END %]
-  });
-</script>
-
 </head>
 <body id="cat_addbiblio" class="cat">
 
                 [%# End of fields for fast cataloging %]
             </form> <!-- /name=f -->
 
-            <div id="avCreate" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="avCreateLabel" aria-hidden="true">
-                <div class="modal-dialog">
-                    <div class="modal-content">
-                        <div class="modal-header">
-                            <button type="button" class="closebtn" data-dismiss="modal" aria-hidden="true">×</button>
-                            <h3 id="avCreateLabel">Create a new authorised value</h3>
-                        </div>
-                        <form id="add_new_av" method="post">
-                            <div class="modal-body">
-                                <div class="error"></div>
-                                <fieldset class="rows">
-                                    <ol>
-                                        <li>
-                                            <span class="label">Category:</span>
-                                            <input type="hidden" name="category" value="" />
-                                            <span id="new_av_category"></span>
-                                        </li>
-                                        <li>
-                                            <span class="label" for="value">Authorised value:</span>
-                                            <input type="text" id="value" name="value" />
-                                        </li>
-                                        <li>
-                                            <span class="label" for="description">Description:</span>
-                                            <input type="text" id="description" name="description" />
-                                        </li>
-                                        <li>
-                                            <span class="label" for="opac_description">Description (OPAC):</span>
-                                            <input type="text" id="opac_description" name="opac_description" />
-                                        </li>
-                                    </ol>
-                                </fieldset>
-                            </div>
-                            <div class="modal-footer">
-                                <button class="btn btn-default" data-dismiss="modal" aria-hidden="true">Close</button>
-                                <input type="hidden" name="select2" value="" />
-                                <input type="submit" class="btn btn-primary" value="Save">
-                            </div>
-                        </form>
-                    </div> <!-- /.modal-content -->
-                </div> <!-- /.modal-dialog -->
-            </div> <!-- /#avCreate -->
+            [% INCLUDE 'modals/cataloguing_create_av.inc' %]
 
         </div> <!-- /.col-md-10.col-md-offset-1 -->
     </div> <!-- /.row -->
index 5ae4103..e05b55f 100644 (file)
@@ -8,11 +8,14 @@
 [% INCLUDE 'doc-head-close.inc' %]
 [% Asset.css("css/addbiblio.css") | $raw %]
 [% INCLUDE 'datatables.inc' %]
+<script>
+    var CAN_user_parameters_manage_auth_values = "[% CAN_user_parameters_manage_auth_values | html %]";
+</script>
+[% INCLUDE 'select2.inc' %]
 [% Asset.js("js/cataloging.js") | $raw %]
 [% INCLUDE 'columns_settings.inc' %]
 [% INCLUDE 'strings.inc' %]
 [% Asset.js("js/browser.js") | $raw %]
-[% INCLUDE 'select2.inc' %]
 [% INCLUDE 'calendar.inc' %]
 [% INCLUDE 'str/cataloging_additem.inc' %]
 [% Asset.js("js/cataloging_additem.js") | $raw %]
                     [% IF ( mv.readonly || ite.IS_RETURN_CLAIM ) %]
                         <select name="field_value" id="[%- mv.id | html -%]" size="1" class="input_marceditor select2" readonly="readonly" disabled="disabled">
                     [% ELSE %]
-                        <select name="field_value" id="[%- mv.id | html -%]" size="1" class="input_marceditor select2">
+                        [% IF mv.category AND CAN_user_parameters_manage_auth_values %]
+                            <select name="field_value" id="[%- mv.id | html -%]" size="1" class="input_marceditor" data-category="[% mv.category | html %]">
+                        [% ELSE %]
+                            <select name="field_value" id="[%- mv.id | html -%]" size="1" class="input_marceditor select2">
+                        [% END %]
                     [% END %]
                     [% FOREACH aval IN mv.values %]
                         [% IF aval == mv.default %]
 
 
     </form>
+
+    [% INCLUDE 'modals/cataloguing_create_av.inc' %]
+
                             </div> <!-- /#cataloguing_additem_newitem -->
                         </div> <!-- /.col-sm-10 -->
                     </div> <!-- /.row -->
index c74d953..3c4d6fb 100644 (file)
@@ -515,9 +515,73 @@ function CheckImportantSubfields(p){
     return total;
 }
 
- $(document).ready(function() {
+$(document).ready(function() {
     $("input.input_marceditor, input.indicator").addClass('noEnterSubmit');
     $(document).ajaxSuccess(function() {
         $("input.input_marceditor, input.indicator").addClass('noEnterSubmit');
     });
+
+    if ( CAN_user_parameters_manage_auth_values ) {
+        var current_select2;
+        $('.subfield_line select[data-category!=""]').select2({
+            tags: true,
+            createTag: function (tag) {
+                return {
+                    id: tag.term,
+                    text: tag.term,
+                    newTag: true
+                };
+            },
+            templateResult: function(state) {
+                if (state.newTag) {
+                    return state.text + " " + __("(select to create)");
+                }
+                return state.text;
+            }
+        }).on("select2:select", function(e) {
+            if(e.params.data.newTag){
+
+                var category = $(this).data("category");
+                $("#avCreate #new_av_category").html(category);
+                $("#avCreate input[name='category']").val(category);
+                $("#avCreate input[name='value']").val(e.params.data.text);
+                $("#avCreate input[name='description']").val(e.params.data.text);
+                $('#avCreate').modal({show:true});
+
+                $(current_select2).val($(current_select2).find("option:first").val()).trigger('change');
+
+                current_select2 = this;
+
+            }
+        }).on("select2:clear", function () {
+            $(this).on("select2:opening.cancelOpen", function (evt) {
+                evt.preventDefault();
+
+                $(this).off("select2:opening.cancelOpen");
+            });
+        });
+
+        $("#add_new_av").on("submit", function(){
+            var data = {
+                category: $(this).find('input[name="category"]').val(),
+                value: $(this).find('input[name="value"]').val(),
+                description: $(this).find('input[name="description"]').val(),
+                opac_description: $(this).find('input[name="opac_description"]').val(),
+            };
+            $.ajax({
+                type: "POST",
+                url: "/api/v1/authorised_values",
+                data:JSON.stringify(data),
+                success: function(response) {
+                    $('#avCreate').modal('hide');
+
+                    $(current_select2).append('<option selected value="'+data['value']+'">'+data['description']+'</option>');
+                },
+                error: function(err) {
+                    $("#avCreate .error").html(_("Something went wrong, maybe the value already exists?"))
+                }
+            });
+            return false;
+        });
+    }
 });