Bug 25728: Create AV when cataloguing a bibliographic record
authorJonathan Druart <jonathan.druart@bugs.koha-community.org>
Fri, 12 Jun 2020 13:22:54 +0000 (15:22 +0200)
committerJonathan Druart <jonathan.druart@bugs.koha-community.org>
Mon, 24 Aug 2020 09:19:03 +0000 (11:19 +0200)
This whole patch set adds the ability to create a new authorised value
directly from the cataloguing module.
It will prevent the cataloger to leave and reload the page if they need
a new one.

Test plan:
If you are logged in with a patron with the manage_authorised_values
subpermission you will be able to:
* Edit a bibliographic record
* Search for a subfield linked to an authorised values (not the "fake"
AV like itemtypes, branches, cn)
* Click the dropdown list and start searching for a value that does not
exist
* Click enter
* Fill the fields in the popup
* Save
=> Your dropdown list has the new AV :)

Note: This does not work for the advanced editor. It does not have
select2 applied to the selects. I have hardly tried but failed.

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/addbiblio.pl
koha-tmpl/intranet-tmpl/prog/en/modules/cataloguing/addbiblio.tt

index 4895564..909b9e9 100755 (executable)
@@ -170,14 +170,15 @@ sub build_authorized_values_list {
     # builds list, depending on authorised value...
 
     #---- branch
-    if ( $tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "branches" ) {
+    my $category = $tagslib->{$tag}->{$subfield}->{authorised_value};
+    if ( $category eq "branches" ) {
         my $libraries = Koha::Libraries->search_filtered({}, {order_by => ['branchname']});
         while ( my $l = $libraries->next ) {
             push @authorised_values, $l->branchcode;;
             $authorised_lib{$l->branchcode} = $l->branchname;
         }
     }
-    elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes" ) {
+    elsif ( $category eq "itemtypes" ) {
         push @authorised_values, "";
 
         my $itemtype;
@@ -188,7 +189,7 @@ sub build_authorized_values_list {
         }
         $value = $itemtype unless ($value);
     }
-    elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "cn_source" ) {
+    elsif ( $category eq "cn_source" ) {
         push @authorised_values, "";
 
         my $class_sources = GetClassSources();
@@ -219,6 +220,7 @@ sub build_authorized_values_list {
         }
     }
     $authorised_values_sth->finish;
+
     return {
         type     => 'select',
         id       => "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield,
@@ -226,6 +228,7 @@ sub build_authorized_values_list {
         default  => $value,
         values   => \@authorised_values,
         labels   => \%authorised_lib,
+        ( ( grep { $_ eq $category } ( qw(branches itemtypes cn_source) ) ) ? () : ( category => $category ) ),
     };
 
 }
index ba0a120..8de0932 100644 (file)
 [% 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">
                                                 [% ELSIF ( mv.type == 'textarea' ) %]
                                                     <textarea cols="70" rows="4" id="[%- mv.id | html -%]" name="[%- mv.name | html -%]" class="input_marceditor" tabindex="1">[%- mv.value | html -%]</textarea>
                                                 [% ELSIF ( mv.type == 'select' ) %]
-                                                    <select name="[%- mv.name | html -%]" tabindex="1" size="1" class="input_marceditor select2" id="[%- mv.id | html -%]">
+                                                    [% IF mv.category AND CAN_user_parameters_manage_auth_values %]
+                                                        <select name="[%- mv.name | html -%]" tabindex="1" size="1" class="input_marceditor" id="[%- mv.id | html -%]" data-category="[% mv.category | html %]">
+                                                    [% ELSE %]
+                                                        <select name="[%- mv.name | html -%]" tabindex="1" size="1" class="input_marceditor select2" id="[%- mv.id | html -%]">
+                                                    [% END %]
                                                     [% FOREACH aval IN mv.values %]
                                                         [% IF aval == mv.default %]
                                                         <option value="[%- aval | html -%]" selected="selected">[%- mv.labels.$aval | html -%]</option>
                 <input type="hidden" name="duedatespec" value="[% duedatespec | html %]" />
                 [%# 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 -->
+
         </div> <!-- /.col-md-10.col-md-offset-1 -->
     </div> <!-- /.row -->