From e43b5856c1723135f8c712de724e4b724768651c Mon Sep 17 00:00:00 2001
From: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
Date: Mon, 4 Oct 2021 16:28:49 +0200
Subject: [PATCH] Bug 29113: Hide code for additional contents and generate it

additional_contents.code is used to group DB rows together. Each row
represent one content in a given language, and the code is used to know
they are translation of the lang='default' one.

It's not really useful for the end user and we could hide it and
generate it.

Test plan:
Create/Edit/Delete additional contents (news and HTML customizations)
and confirm that they are correctly grouped together.
You need several languages installed to test this patch correctly.
---
 .../prog/en/modules/tools/additional-contents.tt  | 12 +-----------
 tools/additional-contents.pl                      | 15 ++++++++++++---
 2 files changed, 13 insertions(+), 14 deletions(-)

diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/additional-contents.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/tools/additional-contents.tt
index 2f74dfbcfc1..358f52ba220 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/additional-contents.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/tools/additional-contents.tt
@@ -171,20 +171,10 @@
 
     <form id="add_additional_content" method="post" action="/cgi-bin/koha/tools/additional-contents.pl" class="validate">
         <input type="hidden" name="op" value="add_validate" />
-        <input type="hidden" name="id" value="[% additional_content.idnew | html %]" />
         <input type="hidden" name="category" value="[% category | html %]" />
+        <input type="hidden" name="code" value="[% additional_content.code | html %]" />
         <fieldset class="rows">
             <ol>
-                <li>
-                    [% IF additional_content %]
-                        <span class="label">Code:</span> [% additional_content.code | html %]
-                        <input type="hidden" id="code" name="code" value="[% additional_content.code | html %]" />
-                    [% ELSE %]
-                        <label for="code" class="required">Code:</label>
-                        <input type="text" id="code" name="code" size="20" maxlength="20" value="" required="required"/>
-                        <span class="required">Required</span>
-                    [% END %]
-                </li>
                 <li>
                     <label for="location">Display location:</label>
                     <select id="location" name="location">
diff --git a/tools/additional-contents.pl b/tools/additional-contents.pl
index b78fb90fee0..c1caea8f767 100755
--- a/tools/additional-contents.pl
+++ b/tools/additional-contents.pl
@@ -106,7 +106,7 @@ elsif ( $op eq 'add_validate' ) {
     my $number = $cgi->param('number');
 
     my $success = 1;
-    for my $lang ( @lang ) {
+    for my $lang ( sort {$a ne 'default'} @lang ) { # Process 'default' first
         my $title = shift @title;
         my $content = shift @content;
         my $additional_content = Koha::AdditionalContents->find(
@@ -160,7 +160,7 @@ elsif ( $op eq 'add_validate' ) {
             my $additional_content = Koha::AdditionalContent->new(
                 {
                     category       => $category,
-                    code           => $code,
+                    code           => $code || 'tmp_code',
                     location       => $location,
                     branchcode     => $branchcode,
                     title          => $title,
@@ -172,7 +172,16 @@ elsif ( $op eq 'add_validate' ) {
                     borrowernumber => $borrowernumber,
                 }
             )->store;
-            eval { $additional_content->store; };
+            eval {
+                $additional_content->store;
+                unless ($code) {
+                    $additional_content->discard_changes;
+                    $code = $category eq 'news'
+                      ? 'News_' . $additional_content->idnew
+                      : $location . '_' . $additional_content->idnew;
+                    $additional_content->code($code)->store;
+                }
+            };
             if ($@) {
                 $success = 0;
                 push @messages, { type => 'error', code => 'error_on_insert' };
-- 
2.25.1